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.

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. # frozen_string_literal: false
  2. # Redmine - project management software
  3. # Copyright (C) 2006-2017 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/imap'
  19. module Redmine
  20. module IMAP
  21. class << self
  22. def check(imap_options={}, options={})
  23. host = imap_options[:host] || '127.0.0.1'
  24. port = imap_options[:port] || '143'
  25. ssl = !imap_options[:ssl].nil?
  26. starttls = !imap_options[:starttls].nil?
  27. folder = imap_options[:folder] || 'INBOX'
  28. imap = Net::IMAP.new(host, port, ssl)
  29. if starttls
  30. imap.starttls
  31. end
  32. imap.login(imap_options[:username], imap_options[:password]) unless imap_options[:username].nil?
  33. imap.select(folder)
  34. imap.uid_search(['NOT', 'SEEN']).each do |uid|
  35. msg = imap.uid_fetch(uid,'RFC822')[0].attr['RFC822']
  36. logger.debug "Receiving message #{uid}" if logger && logger.debug?
  37. if MailHandler.safe_receive(msg, options)
  38. logger.debug "Message #{uid} successfully received" if logger && logger.debug?
  39. if imap_options[:move_on_success]
  40. imap.uid_copy(uid, imap_options[:move_on_success])
  41. end
  42. imap.uid_store(uid, "+FLAGS", [:Seen, :Deleted])
  43. else
  44. logger.debug "Message #{uid} can not be processed" if logger && logger.debug?
  45. imap.uid_store(uid, "+FLAGS", [:Seen])
  46. if imap_options[:move_on_failure]
  47. imap.uid_copy(uid, imap_options[:move_on_failure])
  48. imap.uid_store(uid, "+FLAGS", [:Deleted])
  49. end
  50. end
  51. end
  52. imap.expunge
  53. imap.logout
  54. imap.disconnect
  55. end
  56. private
  57. def logger
  58. ::Rails.logger
  59. end
  60. end
  61. end
  62. end