pop3.rb 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. # Copyright (C) 2012-2014 Zammad Foundation, http://zammad-foundation.org/
  2. require 'net/pop'
  3. class Channel::POP3 < Channel::EmailParser
  4. def fetch (channel, check_type = '', verify_string = '')
  5. ssl = false
  6. port = 110
  7. if channel[:options][:ssl].to_s == 'true'
  8. ssl = true
  9. port = 995
  10. end
  11. puts "fetching pop3 (#{channel[:options][:host]}/#{channel[:options][:user]} port=#{port},ssl=#{ssl})"
  12. @pop = Net::POP3.new( channel[:options][:host], port )
  13. # on check, reduce open_timeout to have faster probing
  14. if check_type == 'check'
  15. @pop.open_timeout = 4
  16. @pop.read_timeout = 6
  17. end
  18. if ssl
  19. @pop.enable_ssl(OpenSSL::SSL::VERIFY_NONE)
  20. end
  21. @pop.start( channel[:options][:user], channel[:options][:password] )
  22. if check_type == 'check'
  23. puts "check only mode, fetch no emails"
  24. disconnect
  25. return
  26. elsif check_type == 'verify'
  27. puts "verify mode, fetch no emails"
  28. end
  29. mails = @pop.mails
  30. count = 0
  31. count_all = mails.size
  32. # reverse message order to increase performance
  33. if check_type == 'verify'
  34. mails.reverse!
  35. end
  36. mails.each do |m|
  37. count += 1
  38. puts " - message #{count.to_s}/#{count_all.to_s}"
  39. # check for verify message
  40. if check_type == 'verify'
  41. mail = m.pop
  42. if mail && mail =~ /#{verify_string}/
  43. puts " - verify email #{verify_string} found"
  44. m.delete
  45. disconnect
  46. return 'verify ok'
  47. end
  48. else
  49. # delete email from server after article was created
  50. if process(channel, m.pop)
  51. m.delete
  52. end
  53. end
  54. end
  55. disconnect
  56. if count == 0
  57. puts " - no message"
  58. end
  59. puts "done"
  60. end
  61. def disconnect
  62. if @pop
  63. @pop.finish
  64. end
  65. end
  66. end