pop3.rb 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. # Copyright (C) 2012-2016 Zammad Foundation, http://zammad-foundation.org/
  2. require 'net/pop'
  3. class Channel::Driver::Pop3 < Channel::EmailParser
  4. =begin
  5. fetch emails from Pop3 account
  6. instance = Channel::Driver::Pop3.new
  7. result = instance.fetch(params[:inbound][:options], channel, 'verify', subject_looking_for)
  8. returns
  9. {
  10. result: 'ok',
  11. fetched: 123,
  12. notice: 'e. g. message about to big emails in mailbox',
  13. }
  14. check if connect to Pop3 account is possible, return count of mails in mailbox
  15. instance = Channel::Driver::Pop3.new
  16. result = instance.fetch(params[:inbound][:options], channel, 'check')
  17. returns
  18. {
  19. result: 'ok',
  20. content_messages: 123,
  21. }
  22. verify Pop3 account, check if search email is in there
  23. instance = Channel::Driver::Pop3.new
  24. result = instance.fetch(params[:inbound][:options], channel, 'verify', subject_looking_for)
  25. returns
  26. {
  27. result: 'ok', # 'verify not ok'
  28. }
  29. =end
  30. def fetch(options, channel, check_type = '', verify_string = '')
  31. ssl = true
  32. port = 995
  33. if options.key?(:ssl) && options[:ssl] == false
  34. ssl = false
  35. port = 110
  36. end
  37. if options.key?(:port) && options[:port].present?
  38. port = options[:port]
  39. # disable ssl for non ssl ports
  40. if port == 110 && !options.key?(:ssl)
  41. ssl = false
  42. end
  43. end
  44. Rails.logger.info "fetching pop3 (#{options[:host]}/#{options[:user]} port=#{port},ssl=#{ssl})"
  45. @pop = ::Net::POP3.new(options[:host], port)
  46. #@pop.set_debug_output $stderr
  47. # on check, reduce open_timeout to have faster probing
  48. @pop.open_timeout = 16
  49. @pop.read_timeout = 45
  50. if check_type == 'check'
  51. @pop.open_timeout = 4
  52. @pop.read_timeout = 6
  53. end
  54. if ssl
  55. @pop.enable_ssl(OpenSSL::SSL::VERIFY_NONE)
  56. end
  57. @pop.start(options[:user], options[:password])
  58. mails = @pop.mails
  59. if check_type == 'check'
  60. Rails.logger.info 'check only mode, fetch no emails'
  61. content_max_check = 2
  62. content_messages = 0
  63. # check messages
  64. mails.each do |m|
  65. mail = m.pop
  66. next if !mail
  67. # check how many content messages we have, for notice used
  68. if !mail.match?(/(X-Zammad-Ignore: true|X-Zammad-Verify: true)/)
  69. content_messages += 1
  70. break if content_max_check < content_messages
  71. end
  72. end
  73. if content_messages >= content_max_check
  74. content_messages = mails.count
  75. end
  76. disconnect
  77. return {
  78. result: 'ok',
  79. content_messages: content_messages,
  80. }
  81. end
  82. # reverse message order to increase performance
  83. if check_type == 'verify'
  84. Rails.logger.info 'verify mode, fetch no emails'
  85. mails.reverse!
  86. # check for verify message
  87. mails.first(2000).each do |m|
  88. mail = m.pop
  89. next if !mail
  90. # check if verify message exists
  91. next if mail !~ /#{verify_string}/
  92. Rails.logger.info " - verify email #{verify_string} found"
  93. m.delete
  94. disconnect
  95. return {
  96. result: 'ok',
  97. }
  98. end
  99. return {
  100. result: 'verify not ok',
  101. }
  102. end
  103. # fetch regular messages
  104. count_all = mails.size
  105. count = 0
  106. count_fetched = 0
  107. notice = ''
  108. mails.first(2000).each do |m|
  109. count += 1
  110. Rails.logger.info " - message #{count}/#{count_all}"
  111. mail = m.pop
  112. next if !mail
  113. # ignore verify messages
  114. if mail.match?(/(X-Zammad-Ignore: true|X-Zammad-Verify: true)/)
  115. if mail =~ /X-Zammad-Verify-Time:\s(.+?)\s/
  116. begin
  117. verify_time = Time.zone.parse($1)
  118. if verify_time > Time.zone.now - 30.minutes
  119. info = " - ignore message #{count}/#{count_all} - because it's a verify message"
  120. Rails.logger.info info
  121. next
  122. end
  123. rescue => e
  124. Rails.logger.error e
  125. end
  126. end
  127. end
  128. # ignore to big messages
  129. max_message_size = Setting.get('postmaster_max_size').to_f
  130. real_message_size = mail.size.to_f / 1024 / 1024
  131. if real_message_size > max_message_size
  132. info = " - ignore message #{count}/#{count_all} - because message is too big (is:#{real_message_size} MB/max:#{max_message_size} MB)"
  133. Rails.logger.info info
  134. notice += "#{info}\n"
  135. next
  136. end
  137. # delete email from server after article was created
  138. process(channel, m.pop, false)
  139. m.delete
  140. count_fetched += 1
  141. end
  142. disconnect
  143. if count.zero?
  144. Rails.logger.info ' - no message'
  145. end
  146. Rails.logger.info 'done'
  147. {
  148. result: 'ok',
  149. fetched: count_fetched,
  150. notice: notice,
  151. }
  152. end
  153. =begin
  154. instance = Channel::Driver::Pop3.new
  155. instance.fetchable?(channel)
  156. =end
  157. def fetchable?(_channel)
  158. true
  159. end
  160. =begin
  161. Channel::Driver::Pop3.streamable?
  162. returns
  163. true|false
  164. =end
  165. def self.streamable?
  166. false
  167. end
  168. def disconnect
  169. return if !@pop
  170. @pop.finish
  171. end
  172. end