Project

General

Profile

« Previous | Next » 

Revision 945de171

Added by Marc Dequènes about 15 years ago

  • ID 945de1718898ba0630350fe9d1daf3b070b7f170

[evol] move IMAP code into a new class

View differences:

lib/cyborghood/imap.rb
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
postman
#!/usr/bin/ruby -Ku
# http://www.ruby-doc.org/stdlib/libdoc/net/imap/rdoc/index.html
# http://tmail.rubyforge.org/reference/index.html
# http://tools.ietf.org/html/rfc3156
$: << "./lib"
......
end
def run
# using SSL because TLS does not work in the NET::IMAP library
#imap = Net::IMAP.new('imap.duckcorp.org', 993, true, "/etc/ssl/certs/duckcorp.crt", true)
imap = Net::IMAP.new('localhost')
logger.debug "Connected to IMAP server"
logger.debug "IMAP Capabilities: " + imap.capability.join(", ")
imap.authenticate('LOGIN', @config.imap.login, @config.imap.passwd)
logger.debug "Logged into IMAP account"
#p imap.getquotaroot("INBOX")
imap.select('INBOX')
imap.search(["ALL"], "UTF-8").each do |message_id|
msg = imap.fetch(message_id, "RFC822")[0].attr["RFC822"]
imap = IMAP.new(@config.imap)
imap.check_mail do |msg|
mail = Mail.new(msg)
logger.info "Mail #{mail.message_id}: #{mail.from_addrs} -> #{mail.to_addrs} (#{mail.subject})"
#logger.set_prefix()
logger.debug "######################################"
#logger.set_prefix("[#{mail.message_id}] ")
logger.info "New mail #{mail.message_id}: #{mail.from_addrs} -> #{mail.to_addrs} (#{mail.subject})"
# ignore mails not signed
unless mail.is_pgp_signed?
logger.info "Mail not signed or not RFC3156 compliant, ignoring..."
......
begin
order = mail.parse
rescue CyberError => e
logger.error "Internal processing error, skipping mail (#{e.message})"
next
case e.severity
when :dangerous
logger.fatal " (#{e.message})"
exit 2
when :unrecoverable
logger.error "Internal processing error, skipping mail (#{e.message})"
next
when :ignorable
end
end
if order.nil?
logger.info "Mail is invalid, ignoring..."
......
CommandParser.run(order)
end
imap.logout
end
def ask_to_stop

Also available in: Unified diff