pop3.rb 4.1 KB

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