Project

General

Profile

Download (2.43 KB) Statistics
| Branch: | Tag: | Revision:
d32ee48a Marc Dequenes
#--
# CyborgHood, a distributed system management software.
e7315259 Marc Dequènes (Duck)
# Copyright (c) 2009-2010 Marc Dequènes (Duck) <Duck@DuckCorp.org>
d32ee48a Marc Dequenes
#
# 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/>.
#++

945de171 Marc Dequenes
require 'net/imap'

module CyborgHood
class IMAP
def initialize(params)
@params = params
end

ed09e1e5 Marc Dequènes (Duck)
class IMAPMessage
d5a19360 Marc Dequenes
def initialize(imap, message_id)
@imap = imap
@message_id = message_id

4a2747e1 Marc Dequènes (Duck)
@config = Config.instance
d5a19360 Marc Dequenes
@content = nil
end

def content
return @content unless @content.nil?

data = @imap.fetch(@message_id, ["RFC822"])[0]
@content = data.attr["RFC822"]
end

def delete
4a2747e1 Marc Dequènes (Duck)
if @config.debug.nil? or @config.debug.flags.nil? or not @config.debug.flags.include?(:debug_nomaildeletion)
@imap.store(@message_id, "+FLAGS", [:Deleted])
end
d5a19360 Marc Dequenes
end
end

945de171 Marc Dequenes
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|
d5a19360 Marc Dequenes
logger.debug "*** Fetched mail ##{message_id}"
ed09e1e5 Marc Dequènes (Duck)
yield IMAPMessage.new(imap, message_id)
945de171 Marc Dequenes
end

d5a19360 Marc Dequenes
imap.expunge
945de171 Marc Dequenes
imap.logout
960373b7 Marc Dequenes
imap.disconnect
945de171 Marc Dequenes
logger.debug "Disconnected from IMAP server"
rescue SocketError, Net::IMAP::Error => e
raise CyberError.new(:unrecoverable, "protocol/imap", e.message)
end
end
end