|
|
@ -33,37 +33,79 @@ module Redmine |
|
|
|
module IMAP |
|
|
|
module IMAP |
|
|
|
class << self |
|
|
|
class << self |
|
|
|
def check(imap_options = {}, options = {}) |
|
|
|
def check(imap_options = {}, options = {}) |
|
|
|
host = imap_options[:host] || '127.0.0.1' |
|
|
|
|
|
|
|
port = imap_options[:port] || '143' |
|
|
|
|
|
|
|
ssl = !imap_options[:ssl].nil? |
|
|
|
|
|
|
|
folder = imap_options[:folder] || 'INBOX' |
|
|
|
folder = imap_options[:folder] || 'INBOX' |
|
|
|
|
|
|
|
imap = connect_imap(imap_options) |
|
|
|
|
|
|
|
|
|
|
|
imap = Net::IMAP.new(host, port, ssl) |
|
|
|
|
|
|
|
imap.login(imap_options[:username], imap_options[:password]) unless imap_options[:username].nil? |
|
|
|
|
|
|
|
imap.select(folder) |
|
|
|
imap.select(folder) |
|
|
|
imap.search(['NOT', 'SEEN']).each do |message_id| |
|
|
|
imap.search(['NOT', 'SEEN']).each do |message_id| |
|
|
|
msg = imap.fetch(message_id, 'RFC822')[0].attr['RFC822'] |
|
|
|
receive(message_id, imap, imap_options, options) |
|
|
|
logger.debug "Receiving message #{message_id}" if logger && logger.debug? |
|
|
|
|
|
|
|
if MailHandler.receive(msg, options) |
|
|
|
|
|
|
|
logger.debug "Message #{message_id} successfully received" if logger && logger.debug? |
|
|
|
|
|
|
|
if imap_options[:move_on_success] |
|
|
|
|
|
|
|
imap.copy(message_id, imap_options[:move_on_success]) |
|
|
|
|
|
|
|
end |
|
|
|
|
|
|
|
imap.store(message_id, '+FLAGS', [:Seen, :Deleted]) |
|
|
|
|
|
|
|
else |
|
|
|
|
|
|
|
logger.debug "Message #{message_id} can not be processed" if logger && logger.debug? |
|
|
|
|
|
|
|
imap.store(message_id, '+FLAGS', [:Seen]) |
|
|
|
|
|
|
|
if imap_options[:move_on_failure] |
|
|
|
|
|
|
|
imap.copy(message_id, imap_options[:move_on_failure]) |
|
|
|
|
|
|
|
imap.store(message_id, '+FLAGS', [:Deleted]) |
|
|
|
|
|
|
|
end |
|
|
|
|
|
|
|
end |
|
|
|
|
|
|
|
end |
|
|
|
end |
|
|
|
|
|
|
|
|
|
|
|
imap.expunge |
|
|
|
imap.expunge |
|
|
|
end |
|
|
|
end |
|
|
|
|
|
|
|
|
|
|
|
private |
|
|
|
private |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def connect_imap(imap_options) |
|
|
|
|
|
|
|
host = imap_options[:host] || '127.0.0.1' |
|
|
|
|
|
|
|
port = imap_options[:port] || '143' |
|
|
|
|
|
|
|
ssl = ssl_option(imap_options) |
|
|
|
|
|
|
|
imap = Net::IMAP.new(host, port, ssl) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
imap.login(imap_options[:username], imap_options[:password]) unless imap_options[:username].nil? |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
imap |
|
|
|
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def ssl_option(imap_options) |
|
|
|
|
|
|
|
if imap_options[:ssl] |
|
|
|
|
|
|
|
if imap_options[:ssl_verification] |
|
|
|
|
|
|
|
true # use SSL with verification |
|
|
|
|
|
|
|
else |
|
|
|
|
|
|
|
{ verify_mode: OpenSSL::SSL::VERIFY_NONE } # use SSL without verification |
|
|
|
|
|
|
|
end |
|
|
|
|
|
|
|
else |
|
|
|
|
|
|
|
false # don't use SSL |
|
|
|
|
|
|
|
end |
|
|
|
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def receive(message_id, imap, imap_options, options) |
|
|
|
|
|
|
|
msg = imap.fetch(message_id, 'RFC822')[0].attr['RFC822'] |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
log_debug { "Receiving message #{message_id}" } |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if MailHandler.receive(msg, options) |
|
|
|
|
|
|
|
message_received(message_id, imap, imap_options) |
|
|
|
|
|
|
|
else |
|
|
|
|
|
|
|
message_error(message_id, imap, imap_options) |
|
|
|
|
|
|
|
end |
|
|
|
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def message_received(message_id, imap, imap_options) |
|
|
|
|
|
|
|
log_debug { "Message #{message_id} successfully received" } |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if imap_options[:move_on_success] |
|
|
|
|
|
|
|
imap.copy(message_id, imap_options[:move_on_success]) |
|
|
|
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
imap.store(message_id, '+FLAGS', [:Seen, :Deleted]) |
|
|
|
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def message_error(message_id, imap, imap_options) |
|
|
|
|
|
|
|
log_debug { "Message #{message_id} can not be processed" } |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
imap.store(message_id, '+FLAGS', [:Seen]) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if imap_options[:move_on_failure] |
|
|
|
|
|
|
|
imap.copy(message_id, imap_options[:move_on_failure]) |
|
|
|
|
|
|
|
imap.store(message_id, '+FLAGS', [:Deleted]) |
|
|
|
|
|
|
|
end |
|
|
|
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def log_debug(&_message) |
|
|
|
|
|
|
|
logger.debug(yield) if logger && logger.debug? |
|
|
|
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
|
|
def logger |
|
|
|
def logger |
|
|
|
Rails.logger |
|
|
|
Rails.logger |
|
|
|
end |
|
|
|
end |
|
|
|