Project

General

Profile

Download (2.3 KB) Statistics
| Branch: | Tag: | Revision:
#--
# CyborgHood, a distributed system management software.
# Copyright (c) 2009 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 'cyborghood/base'
require 'net/imap'

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

class Message
def initialize(imap, message_id)
@imap = imap
@message_id = message_id

@content = nil
end

def content
return @content unless @content.nil?

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

def delete
@imap.store(@message_id, "+FLAGS", [:Deleted])
end
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|
logger.debug "*** Fetched mail ##{message_id}"
yield Message.new(imap, message_id)
end

imap.expunge
imap.logout
imap.disconnect
logger.debug "Disconnected from IMAP server"
rescue SocketError, Net::IMAP::Error => e
raise CyberError.new(:unrecoverable, "protocol/imap", e.message)
end
end
end
(3-3/7)