Project

General

Profile

« Previous | Next » 

Revision b0a510ef

Added by Marc Dequènes almost 14 years ago

  • ID b0a510ef431ae20b57c23922aa63b6871c6375b3

[evol] split IMAP mail checking in several methods and handle errors a bit better

View differences:

lib/cyborghood/imap.rb
class IMAP
def initialize(params)
@params = params
@imap = nil
@is_loggued = false
end
class IMAPMessage
......
end
end
def check_mail
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 IMAP server..."
imap = Net::IMAP.new(@params.host, port, ssl, @params.ca_cert, check_ca)
logger.debug "Connected (IMAP Capabilities: " + imap.capability.join(", ") + ")"
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")
true
end
def authenticate
raise CyberError.new(:unrecoverable, "fixme", "Trying to authenticate to the IMAP server, but we are not connected yet") unless @imap
imap.authenticate('LOGIN', @params.login, @params.passwd)
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
#p imap.getquotaroot("INBOX")
def check_mail(&message_handler)
connect &&
authenticate &&
check_inbox(&message_handler) &&
disconnect
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)
end
imap.select('INBOX')
def check_inbox
logger.debug "Examining INBOX"
@imap.select('INBOX')
imap.search(["ALL"], "UTF-8").each do |message_id|
@imap.search(["ALL"], "UTF-8").each do |message_id|
logger.debug "*** Fetched mail ##{message_id}"
yield IMAPMessage.new(imap, message_id)
yield IMAPMessage.new(@imap, message_id)
end
imap.expunge
imap.logout
imap.disconnect
@imap.expunge
end
def disconnect
return unless @imap
@imap.logout if @is_loggued
@imap.disconnect
logger.debug "Disconnected from IMAP server"
rescue SocketError, Net::IMAP::Error => e
raise CyberError.new(:unrecoverable, "protocol/imap", e.message)
@imap = nil
@is_loggued = false
end
end
end

Also available in: Unified diff