pop3.rb 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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/i)
  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.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.each do |m|
  109. count += 1
  110. Rails.logger.info " - message #{count}/#{count_all}"
  111. mail = m.pop
  112. next if !mail
  113. # ignore to big messages
  114. max_message_size = Setting.get('postmaster_max_size').to_f
  115. real_message_size = mail.size.to_f / 1024 / 1024
  116. if real_message_size > max_message_size
  117. info = " - ignore message #{count}/#{count_all} - because message is too big (is:#{real_message_size} MB/max:#{max_message_size} MB)"
  118. Rails.logger.info info
  119. notice += "#{info}\n"
  120. next
  121. end
  122. # delete email from server after article was created
  123. process(channel, m.pop, false)
  124. m.delete
  125. count_fetched += 1
  126. end
  127. disconnect
  128. if count.zero?
  129. Rails.logger.info ' - no message'
  130. end
  131. Rails.logger.info 'done'
  132. {
  133. result: 'ok',
  134. fetched: count_fetched,
  135. notice: notice,
  136. }
  137. end
  138. =begin
  139. instance = Channel::Driver::Pop3.new
  140. instance.fetchable?(channel)
  141. =end
  142. def fetchable?(_channel)
  143. true
  144. end
  145. =begin
  146. Channel::Driver::Pop3.streamable?
  147. returns
  148. true|false
  149. =end
  150. def self.streamable?
  151. false
  152. end
  153. def disconnect
  154. return if !@pop
  155. @pop.finish
  156. end
  157. end