pop3.rb 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://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. if options[:ssl] == 'off'
  33. ssl = false
  34. end
  35. ssl_verify = options.fetch(:ssl_verify, true)
  36. port = if options.key?(:port) && options[:port].present?
  37. options[:port].to_i
  38. elsif ssl == true
  39. 995
  40. else
  41. 110
  42. end
  43. Rails.logger.info "fetching pop3 (#{options[:host]}/#{options[:user]} port=#{port},ssl=#{ssl})"
  44. @pop = ::Net::POP3.new(options[:host], port)
  45. # @pop.set_debug_output $stderr
  46. # on check, reduce open_timeout to have faster probing
  47. @pop.open_timeout = 16
  48. @pop.read_timeout = 45
  49. if check_type == 'check'
  50. @pop.open_timeout = 4
  51. @pop.read_timeout = 6
  52. end
  53. if ssl
  54. Certificate::ApplySSLCertificates.ensure_fresh_ssl_context
  55. @pop.enable_ssl((ssl_verify ? OpenSSL::SSL::VERIFY_PEER : 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?(%r{(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.match?(%r{#{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. too_large_messages = []
  108. active_check_interval = 20
  109. notice = ''
  110. mails.first(2000).each do |m|
  111. count += 1
  112. break if (count % active_check_interval).zero? && channel_has_changed?(channel)
  113. Rails.logger.info " - message #{count}/#{count_all}"
  114. mail = m.pop
  115. next if !mail
  116. # ignore verify messages
  117. if mail.match?(%r{(X-Zammad-Ignore: true|X-Zammad-Verify: true)}) && mail =~ %r{X-Zammad-Verify-Time:\s(.+?)\s}
  118. begin
  119. verify_time = Time.zone.parse($1)
  120. if verify_time > 30.minutes.ago
  121. info = " - ignore message #{count}/#{count_all} - because it's a verify message"
  122. Rails.logger.info info
  123. next
  124. end
  125. rescue => e
  126. Rails.logger.error e
  127. end
  128. end
  129. # do not process too large messages, instead download and send postmaster reply
  130. max_message_size = Setting.get('postmaster_max_size').to_f
  131. real_message_size = mail.size.to_f / 1024 / 1024
  132. if real_message_size > max_message_size
  133. if Setting.get('postmaster_send_reject_if_mail_too_large') == true
  134. info = " - download message #{count}/#{count_all} - ignore message because it's too large (is:#{real_message_size} MB/max:#{max_message_size} MB)"
  135. Rails.logger.info info
  136. notice += "#{info}\n"
  137. process_oversized_mail(channel, mail)
  138. else
  139. info = " - ignore message #{count}/#{count_all} - because message is too large (is:#{real_message_size} MB/max:#{max_message_size} MB)"
  140. Rails.logger.info info
  141. notice += "#{info}\n"
  142. too_large_messages.push info
  143. next
  144. end
  145. # delete email from server after article was created
  146. else
  147. process(channel, m.pop, false)
  148. end
  149. m.delete
  150. count_fetched += 1
  151. end
  152. disconnect
  153. if count.zero?
  154. Rails.logger.info ' - no message'
  155. end
  156. if too_large_messages.present?
  157. raise too_large_messages.join("\n")
  158. end
  159. Rails.logger.info 'done'
  160. {
  161. result: 'ok',
  162. fetched: count_fetched,
  163. notice: notice,
  164. }
  165. end
  166. =begin
  167. instance = Channel::Driver::Pop3.new
  168. instance.fetchable?(channel)
  169. =end
  170. def fetchable?(_channel)
  171. true
  172. end
  173. =begin
  174. Channel::Driver::Pop3.streamable?
  175. returns
  176. true|false
  177. =end
  178. def self.streamable?
  179. false
  180. end
  181. =begin
  182. check if channel config has changed
  183. Channel::Driver::IMAP.channel_has_changed?(channel)
  184. returns
  185. true|false
  186. =end
  187. def channel_has_changed?(channel)
  188. current_channel = Channel.find_by(id: channel.id)
  189. if !current_channel
  190. Rails.logger.info "Channel with id #{channel.id} is deleted in the meantime. Stop fetching."
  191. return true
  192. end
  193. return false if channel.updated_at == current_channel.updated_at
  194. Rails.logger.info "Channel with id #{channel.id} has changed. Stop fetching."
  195. true
  196. end
  197. def disconnect
  198. return if !@pop
  199. @pop.finish
  200. end
  201. end