|
require 'cyborghood/base'
|
|
require 'net/imap'
|
|
|
|
module CyborgHood
|
|
class IMAP
|
|
def initialize(params)
|
|
@params = params
|
|
end
|
|
|
|
def check_mail
|
|
# 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(", ") + ")"
|
|
|
|
imap.authenticate('LOGIN', @params.login, @params.passwd)
|
|
logger.debug "Authenticated as '#{@params.login}'"
|
|
|
|
#p imap.getquotaroot("INBOX")
|
|
|
|
imap.select('INBOX')
|
|
logger.debug "Examining INBOX"
|
|
|
|
imap.search(["ALL"], "UTF-8").each do |message_id|
|
|
data = imap.fetch(message_id, ["UID", "RFC822"])[0]
|
|
uid = data.attr["UID"]
|
|
logger.debug "*** Fetched mail ##{uid}"
|
|
msg = data.attr["RFC822"]
|
|
yield msg
|
|
end
|
|
|
|
imap.logout
|
|
logger.debug "Disconnected from IMAP server"
|
|
rescue SocketError, Net::IMAP::Error => e
|
|
raise CyberError.new(:unrecoverable, "protocol/imap", e.message)
|
|
end
|
|
end
|
|
end
|