|
#--
|
|
# CyborgHood, a distributed system management software.
|
|
# Copyright (c) 2009-2010 Marc Dequènes (Duck) <Duck@DuckCorp.org>
|
|
#
|
|
# This program is free software: you can redistribute it and/or modify
|
|
# it under the terms of the GNU General Public License as published by
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
# (at your option) any later version.
|
|
#
|
|
# This program is distributed in the hope that it will be useful,
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
# GNU General Public License for more details.
|
|
#
|
|
# You should have received a copy of the GNU General Public License
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
#++
|
|
|
|
require 'net/imap'
|
|
|
|
# IMAP IDLE support
|
|
class Net::IMAP
|
|
def idle(&response_handler)
|
|
return if @idle_mode
|
|
|
|
@idle_response_handler = response_handler
|
|
|
|
synchronize do
|
|
@idle_tag = generate_tag
|
|
add_response_handler @idle_response_handler
|
|
put_string "#{@idle_tag} IDLE#{CRLF}"
|
|
end
|
|
|
|
@idle_mode = true
|
|
end
|
|
|
|
def idle_done
|
|
return unless @idle_mode
|
|
|
|
synchronize do
|
|
remove_response_handler @idle_response_handler
|
|
put_string "DONE#{CRLF}"
|
|
end
|
|
|
|
@idle_mode = false
|
|
|
|
get_tagged_response @idle_tag
|
|
end
|
|
|
|
def idle?
|
|
@idle_mode || false
|
|
end
|
|
end
|
|
|
|
module CyborgHood
|
|
class IMAP
|
|
def initialize(params, min_check_interval)
|
|
@params = params
|
|
@min_check_interval = min_check_interval
|
|
|
|
@config = Config.instance
|
|
@imap = nil
|
|
@is_loggued = false
|
|
|
|
if not (@config.debug.nil? or @config.debug.flags.nil?) and @config.debug.flags.include?(:debug_imapverbose)
|
|
Net::IMAP.debug = true
|
|
end
|
|
end
|
|
|
|
class IMAPMessage
|
|
def initialize(imap, message_id)
|
|
@imap = imap
|
|
@message_id = message_id
|
|
|
|
@config = Config.instance
|
|
@content = nil
|
|
@stop_mail_check = false
|
|
end
|
|
|
|
def content
|
|
return @content unless @content.nil?
|
|
|
|
data = @imap.fetch(@message_id, ["RFC822"])[0]
|
|
@content = data.attr["RFC822"]
|
|
end
|
|
|
|
def delete
|
|
if @config.debug.nil? or @config.debug.flags.nil? or not @config.debug.flags.include?(:debug_nomaildeletion)
|
|
@imap.store(@message_id, "+FLAGS", [:Deleted])
|
|
end
|
|
end
|
|
end
|
|
|
|
def connect
|
|
return true if @imap
|
|
|
|
# using SSL because TLS does not work in the NET::IMAP library
|
|
ssl = (not @params.ca_cert.nil?)
|
|
port = @params.port || (ssl ? 993 : 143)
|
|
check_ca = ssl
|
|
logger.debug "Connecting to the IMAP server..."
|
|
begin
|
|
@imap = Net::IMAP.new(@params.host, port, ssl, @params.ca_cert, check_ca)
|
|
rescue SocketError
|
|
logger.warn "Could not connect to the IMAP server"
|
|
return false
|
|
end
|
|
logger.debug "Connected (IMAP Capabilities: " + @imap.capability.join(", ") + ")"
|
|
#p @imap.getquotaroot("INBOX")
|
|
|
|
@stop_mail_check = false
|
|
|
|
true
|
|
end
|
|
|
|
def authenticate
|
|
raise CyberError.new(:unrecoverable, "fixme", "Trying to authenticate to the IMAP server, but we are not connected yet") unless @imap
|
|
|
|
logger.debug "Authenticating to the IMAP server..."
|
|
begin
|
|
@imap.authenticate('LOGIN', @params.login, @params.passwd)
|
|
rescue Net::IMAP::NoResponseError
|
|
logger.warn "Could not authenticate to the IMAP server"
|
|
disconnect
|
|
return false
|
|
end
|
|
logger.debug "Authenticated as '#{@params.login}'"
|
|
@is_loggued = true
|
|
true
|
|
end
|
|
|
|
def check_mails_once(&message_handler)
|
|
connect &&
|
|
authenticate &&
|
|
check_inbox(&message_handler)
|
|
if @imap.capability.include?("IDLE")
|
|
logger.debug "Waiting for new mails in idle mode"
|
|
@imap.idle do |resp|
|
|
if resp.kind_of?(Net::IMAP::UntaggedResponse) and resp.name == "EXISTS"
|
|
message_id = resp.data
|
|
logger.debug "*** Received mail ##{message_id}"
|
|
# TODO: can't do that, a new IMAP connection should be used, this one is dedicated to IDLE mode
|
|
message_handler.call IMAPMessage.new(@imap, message_id)
|
|
end
|
|
end
|
|
sleep(1) until @stop_mail_check
|
|
p idle_done
|
|
# TODO: ensure all thread for IDLE processing have finished before closing the connection
|
|
end
|
|
rescue SocketError, Net::IMAP::NoResponseError => e
|
|
logger.warn "IMAP or Network error: " + e.message
|
|
rescue Net::IMAP::Error => e
|
|
raise CyberError.new(:unrecoverable, "protocol/imap", e.message)
|
|
ensure
|
|
begin
|
|
disconnect
|
|
rescue
|
|
end
|
|
end
|
|
|
|
def check_mails(&message_handler)
|
|
@stop_mail_check = false
|
|
until @stop_mail_check
|
|
t = Time.now.to_i
|
|
r = check_mails_once(&message_handler)
|
|
t2 = Time.now.to_i
|
|
|
|
# wait before new check either if the IMAP server does not support IDLE mode
|
|
# or if an error occured
|
|
sleep_time = @min_check_interval - (t2 - t)
|
|
if sleep_time > 0 and not @stop_mail_check
|
|
logger.debug "Having a break before new check..."
|
|
t = Time.now.to_i
|
|
while Time.now.to_i - t < sleep_time and not @stop_mail_check
|
|
sleep(1)
|
|
end
|
|
end
|
|
end
|
|
end
|
|
|
|
def stop_mail_check
|
|
@stop_mail_check = true
|
|
end
|
|
|
|
def check_inbox(&message_handler)
|
|
logger.debug "Examining INBOX"
|
|
@imap.select('INBOX')
|
|
|
|
logger.debug "Starting mail check"
|
|
@imap.search(["ALL"], "UTF-8").each do |message_id|
|
|
break if @stop_mail_check
|
|
|
|
logger.debug "*** Fetched mail ##{message_id}"
|
|
unless message_handler.call IMAPMessage.new(@imap, message_id)
|
|
@stop_mail_check = true
|
|
break
|
|
end
|
|
end
|
|
logger.debug "Mail check finished"
|
|
|
|
# TODO: expunge after <n> mails processed in IDLE mode
|
|
@imap.expunge
|
|
end
|
|
|
|
def disconnect
|
|
return unless @imap
|
|
|
|
@imap.logout if @is_loggued
|
|
|
|
@imap.disconnect
|
|
logger.debug "Disconnected from IMAP server"
|
|
@imap = nil
|
|
@is_loggued = false
|
|
end
|
|
end
|
|
end
|