You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. # frozen_string_literal: false
  2. # Redmine - project management software
  3. # Copyright (C) 2006- Jean-Philippe Lang
  4. #
  5. # This program is free software; you can redistribute it and/or
  6. # modify it under the terms of the GNU General Public License
  7. # as published by the Free Software Foundation; either version 2
  8. # of the License, or (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with this program; if not, write to the Free Software
  17. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. require 'net/pop'
  19. module Redmine
  20. module POP3
  21. class << self
  22. def check(pop_options={}, options={})
  23. if pop_options[:ssl]
  24. ssl = true
  25. if pop_options[:ssl] == 'force'
  26. Net::POP3.enable_ssl(OpenSSL::SSL::VERIFY_NONE)
  27. else
  28. Net::POP3.enable_ssl(OpenSSL::SSL::VERIFY_PEER)
  29. end
  30. else
  31. ssl = false
  32. end
  33. host = pop_options[:host] || '127.0.0.1'
  34. port = pop_options[:port]
  35. port ||= ssl ? '995' : '110'
  36. apop = (pop_options[:apop].to_s == '1')
  37. delete_unprocessed = (pop_options[:delete_unprocessed].to_s == '1')
  38. pop = Net::POP3.APOP(apop).new(host,port)
  39. logger.debug "Connecting to #{host}..." if logger && logger.debug?
  40. pop.start(pop_options[:username], pop_options[:password]) do |pop_session|
  41. if pop_session.mails.empty?
  42. logger.debug "No email to process" if logger && logger.debug?
  43. else
  44. logger.debug "#{pop_session.mails.size} email(s) to process..." if logger && logger.debug?
  45. pop_session.each_mail do |msg|
  46. message = msg.pop
  47. message_id = (message =~ /^Message-I[dD]: (.*)/ ? $1 : '').strip
  48. if MailHandler.safe_receive(message, options)
  49. msg.delete
  50. logger.debug "--> Message #{message_id} processed and deleted from the server" if logger && logger.debug?
  51. else
  52. if delete_unprocessed
  53. msg.delete
  54. logger.debug "--> Message #{message_id} NOT processed and deleted from the server" if logger && logger.debug?
  55. else
  56. logger.debug "--> Message #{message_id} NOT processed and left on the server" if logger && logger.debug?
  57. end
  58. end
  59. end
  60. end
  61. end
  62. end
  63. private
  64. def logger
  65. ::Rails.logger
  66. end
  67. end
  68. end
  69. end