root/lib/cyborghood/imap.rb @ d5a0d3bc
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
|
|||
b0a510ef | Marc Dequènes (Duck) | @imap = nil
|
|
@is_loggued = false
|
|||
945de171 | Marc Dequenes | 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
|
|||
b0a510ef | Marc Dequènes (Duck) | def connect
|
|
return true if @imap
|
|||
945de171 | Marc Dequenes | # 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
|
|||
b0a510ef | Marc Dequènes (Duck) | 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
|
|||
945de171 | Marc Dequenes | ||
b0a510ef | Marc Dequènes (Duck) | 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
|
|||
945de171 | Marc Dequenes | logger.debug "Authenticated as '#{@params.login}'"
|
|
b0a510ef | Marc Dequènes (Duck) | @is_loggued = true
|
|
true
|
|||
end
|
|||
945de171 | Marc Dequenes | ||
b0a510ef | Marc Dequènes (Duck) | 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
|
|||
945de171 | Marc Dequenes | ||
b0a510ef | Marc Dequènes (Duck) | def check_inbox
|
|
945de171 | Marc Dequenes | logger.debug "Examining INBOX"
|
|
b0a510ef | Marc Dequènes (Duck) | @imap.select('INBOX')
|
|
945de171 | Marc Dequenes | ||
b0a510ef | Marc Dequènes (Duck) | @imap.search(["ALL"], "UTF-8").each do |message_id|
|
|
d5a19360 | Marc Dequenes | logger.debug "*** Fetched mail ##{message_id}"
|
|
b0a510ef | Marc Dequènes (Duck) | yield IMAPMessage.new(@imap, message_id)
|
|
945de171 | Marc Dequenes | end
|
|
b0a510ef | Marc Dequènes (Duck) | @imap.expunge
|
|
end
|
|||
def disconnect
|
|||
return unless @imap
|
|||
@imap.logout if @is_loggued
|
|||
@imap.disconnect
|
|||
945de171 | Marc Dequenes | logger.debug "Disconnected from IMAP server"
|
|
b0a510ef | Marc Dequènes (Duck) | @imap = nil
|
|
@is_loggued = false
|
|||
945de171 | Marc Dequenes | end
|
|
end
|
|||
end
|