pop3.rb 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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. Rails.logger.info "fetching pop3 (#{options[:host]}/#{options[:user]} port=#{port},ssl=#{ssl})"
  38. @pop = Net::POP3.new(options[:host], port)
  39. #@pop.set_debug_output $stderr
  40. # on check, reduce open_timeout to have faster probing
  41. @pop.open_timeout = 8
  42. @pop.read_timeout = 12
  43. if check_type == 'check'
  44. @pop.open_timeout = 4
  45. @pop.read_timeout = 6
  46. end
  47. if ssl
  48. @pop.enable_ssl(OpenSSL::SSL::VERIFY_NONE)
  49. end
  50. @pop.start(options[:user], options[:password])
  51. mails = @pop.mails
  52. if check_type == 'check'
  53. Rails.logger.info 'check only mode, fetch no emails'
  54. content_max_check = 2
  55. content_messages = 0
  56. # check messages
  57. mails.each do |m|
  58. mail = m.pop
  59. next if !mail
  60. # check how many content messages we have, for notice used
  61. if mail !~ /x-zammad-ignore/i
  62. content_messages += 1
  63. break if content_max_check < content_messages
  64. end
  65. end
  66. if content_messages >= content_max_check
  67. content_messages = mails.count
  68. end
  69. disconnect
  70. return {
  71. result: 'ok',
  72. content_messages: content_messages,
  73. }
  74. end
  75. # reverse message order to increase performance
  76. if check_type == 'verify'
  77. Rails.logger.info 'verify mode, fetch no emails'
  78. mails.reverse!
  79. # check for verify message
  80. mails.each do |m|
  81. mail = m.pop
  82. next if !mail
  83. # check if verify message exists
  84. next if mail !~ /#{verify_string}/
  85. Rails.logger.info " - verify email #{verify_string} found"
  86. m.delete
  87. disconnect
  88. return {
  89. result: 'ok',
  90. }
  91. end
  92. return {
  93. result: 'verify not ok',
  94. }
  95. end
  96. # fetch regular messages
  97. count_all = mails.size
  98. count = 0
  99. count_fetched = 0
  100. notice = ''
  101. mails.each do |m|
  102. count += 1
  103. Rails.logger.info " - message #{count}/#{count_all}"
  104. mail = m.pop
  105. next if !mail
  106. # ignore to big messages
  107. max_message_size = Setting.get('postmaster_max_size').to_f
  108. real_message_size = mail.size.to_f / 1024 / 1024
  109. if real_message_size > max_message_size
  110. info = " - ignore message #{count}/#{count_all} - because message is too big (is:#{real_message_size} MB/max:#{max_message_size} MB)"
  111. Rails.logger.info info
  112. notice += "#{info}\n"
  113. next
  114. end
  115. # delete email from server after article was created
  116. if process(channel, m.pop)
  117. m.delete
  118. count_fetched += 1
  119. end
  120. end
  121. disconnect
  122. if count.zero?
  123. Rails.logger.info ' - no message'
  124. end
  125. Rails.logger.info 'done'
  126. {
  127. result: 'ok',
  128. fetched: count_fetched,
  129. notice: notice,
  130. }
  131. end
  132. =begin
  133. instance = Channel::Driver::Pop3.new
  134. instance.fetchable?(channel)
  135. =end
  136. def fetchable?(_channel)
  137. true
  138. end
  139. def disconnect
  140. return if !@pop
  141. @pop.finish
  142. end
  143. end