Project

General

Profile

Download (7.2 KB) Statistics
| Branch: | Tag: | Revision:
d32ee48a Marc Dequenes
#--
# CyborgHood, a distributed system management software.
364e4a96 Marc Dequènes (Duck)
# Copyright (c) 2009-2011 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'
7dce67e9 Marc Dequènes (Duck)
require 'thread'
945de171 Marc Dequenes
3d5142d6 Marc Dequènes (Duck)
# IMAP IDLE support
class Net::IMAP
def idle(&response_handler)
return if @idle_mode

@idle_response_handler = response_handler

synchronize do
@idle_tag = generate_tag
7dce67e9 Marc Dequènes (Duck)
add_response_handler @idle_response_handler if @idle_response_handler
3d5142d6 Marc Dequènes (Duck)
put_string "#{@idle_tag} IDLE#{CRLF}"
end

@idle_mode = true
end

def idle_done
return unless @idle_mode

synchronize do
7dce67e9 Marc Dequènes (Duck)
remove_response_handler @idle_response_handler if @idle_response_handler
3d5142d6 Marc Dequènes (Duck)
put_string "DONE#{CRLF}"
end

@idle_mode = false

6ee45f3c Marc Dequènes (Duck)
begin
return get_tagged_response @idle_tag
rescue
end
3d5142d6 Marc Dequènes (Duck)
end

def idle?
@idle_mode || false
end
end

945de171 Marc Dequenes
module CyborgHood
class IMAP
6bc51624 Marc Dequènes (Duck)
def initialize(params, min_check_interval)
945de171 Marc Dequenes
@params = params
3d5142d6 Marc Dequènes (Duck)
@min_check_interval = min_check_interval

@config = Config.instance
b0a510ef Marc Dequènes (Duck)
@imap = nil
@is_loggued = false
7dce67e9 Marc Dequènes (Duck)
@available_mails = 0
@available_mails_mutex = Mutex.new
3d5142d6 Marc Dequènes (Duck)
58250ed9 Marc Dequènes (Duck)
if @config.debug.flags.include?('debug_imapverbose')
3d5142d6 Marc Dequènes (Duck)
Net::IMAP.debug = true
end
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
6bc51624 Marc Dequènes (Duck)
@stop_mail_check = false
d5a19360 Marc Dequenes
end

def content
return @content unless @content.nil?

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

def delete
2f1c6b5b Marc Dequènes (Duck)
if not @config.debug.flags.include?('debug_nomaildeletion')
7dce67e9 Marc Dequènes (Duck)
@imap.store(@message_id, "+FLAGS", [:Deleted])
end
d5a19360 Marc Dequenes
end
end

3f78d2ea Marc Dequènes (Duck)
def capabilities
return @imap_capab if @imap_capab
@imap_capab = @imap.capability
end

b0a510ef Marc Dequènes (Duck)
def connect
return true if @imap

2f1c6b5b Marc Dequènes (Duck)
# using SSL because TLS does not work in the NET::IMAP library in Ruby 1.8
# (but available in 1.9)
945de171 Marc Dequenes
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"
6bc51624 Marc Dequènes (Duck)
return false
b0a510ef Marc Dequènes (Duck)
end
3f78d2ea Marc Dequènes (Duck)
logger.debug "Connected (IMAP Capabilities: " + self.capabilities.join(", ") + ")"
b0a510ef Marc Dequènes (Duck)
#p @imap.getquotaroot("INBOX")
6bc51624 Marc Dequènes (Duck)
@stop_mail_check = false

b0a510ef Marc Dequènes (Duck)
true
end

34bef607 Marc Dequènes (Duck)
def connected?
not @imap.nil?
end

b0a510ef Marc Dequènes (Duck)
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"
6bc51624 Marc Dequènes (Duck)
disconnect
return false
b0a510ef Marc Dequènes (Duck)
end
945de171 Marc Dequenes
logger.debug "Authenticated as '#{@params.login}'"
b0a510ef Marc Dequènes (Duck)
@is_loggued = true
true
end
945de171 Marc Dequenes
7dce67e9 Marc Dequènes (Duck)
def listen_to_events
@imap.add_response_handler do |resp|
58250ed9 Marc Dequènes (Duck)
logger.debug "IMAP event: " + resp.inspect if @config.debug.flags.include?('debug_imapverbose')
7dce67e9 Marc Dequènes (Duck)
if resp.kind_of?(Net::IMAP::UntaggedResponse) and resp.name == "EXISTS"
@available_mails_mutex.synchronize do
@available_mails = resp.data.to_i
logger.debug "*** Received new mails (#{@available_mails})" if @available_mails > 0
3d5142d6 Marc Dequènes (Duck)
end
end
end
7dce67e9 Marc Dequènes (Duck)
true
end

def check_mails_once(&message_handler)
connect &&
authenticate &&
listen_to_events &&
wait_and_read_mails(&message_handler)
7bb2ac7f Marc Dequènes (Duck)
rescue Net::IMAP::NoResponseError => e
logger.warn "IMAP error: " + e.message.strip
b0a510ef Marc Dequènes (Duck)
rescue Net::IMAP::Error => e
7bb2ac7f Marc Dequènes (Duck)
logger.error "IMAP error: " + e.message.strip
6bc51624 Marc Dequènes (Duck)
ensure
begin
disconnect
rescue
end
end

7dce67e9 Marc Dequènes (Duck)
def remaining_mails
nb = nil
@available_mails_mutex.synchronize do
nb = @available_mails
end
nb
end

def waiting_mails?
remaining_mails > 0
end

def wait_and_read_mails(&message_handler)
until @stop_mail_check
begin
check_inbox(&message_handler)
# don't loop forever
2f1c6b5b Marc Dequènes (Duck)
return if @config.debug.flags.include?('debug_nomaildeletion')
7dce67e9 Marc Dequènes (Duck)
end while waiting_mails?

3f78d2ea Marc Dequènes (Duck)
if self.capabilities.include?("IDLE")
7dce67e9 Marc Dequènes (Duck)
logger.debug "Waiting for new mails in idle mode"
58250ed9 Marc Dequènes (Duck)
@imap.idle
7dce67e9 Marc Dequènes (Duck)
sleep(1) until @stop_mail_check or waiting_mails?
@imap.idle_done
end
end
end

6bc51624 Marc Dequènes (Duck)
def check_mails(&message_handler)
2fd36b40 Marc Dequènes (Duck)
@stop_mail_check = false
6bc51624 Marc Dequènes (Duck)
until @stop_mail_check
7bb2ac7f Marc Dequènes (Duck)
begin
t = Time.now.to_i
r = check_mails_once(&message_handler)
t2 = Time.now.to_i

sleep_time = @min_check_interval - (t2 - t)
rescue SocketError, Errno::EPIPE, Errno::ECONNREFUSED, Errno::ETIMEDOUT, Errno::ENETUNREACH => e
logger.warn "Network error: " + e.message.strip
sleep_time = @min_check_interval
end
6bc51624 Marc Dequènes (Duck)
3d5142d6 Marc Dequènes (Duck)
# wait before new check either if the IMAP server does not support IDLE mode
# or if an error occured
2fd36b40 Marc Dequènes (Duck)
if sleep_time > 0 and not @stop_mail_check
6bc51624 Marc Dequènes (Duck)
logger.debug "Having a break before new check..."
2fd36b40 Marc Dequènes (Duck)
t = Time.now.to_i
while Time.now.to_i - t < sleep_time and not @stop_mail_check
sleep(1)
6bc51624 Marc Dequènes (Duck)
end
end
end
b0a510ef Marc Dequènes (Duck)
end
945de171 Marc Dequenes
2fd36b40 Marc Dequènes (Duck)
def stop_mail_check
@stop_mail_check = true
end

3d5142d6 Marc Dequènes (Duck)
def check_inbox(&message_handler)
945de171 Marc Dequenes
logger.debug "Examining INBOX"
b0a510ef Marc Dequènes (Duck)
@imap.select('INBOX')
945de171 Marc Dequenes
6bc51624 Marc Dequènes (Duck)
logger.debug "Starting mail check"
b0a510ef Marc Dequènes (Duck)
@imap.search(["ALL"], "UTF-8").each do |message_id|
2fd36b40 Marc Dequènes (Duck)
break if @stop_mail_check

d5a19360 Marc Dequenes
logger.debug "*** Fetched mail ##{message_id}"
3d5142d6 Marc Dequènes (Duck)
unless message_handler.call IMAPMessage.new(@imap, message_id)
6bc51624 Marc Dequènes (Duck)
@stop_mail_check = true
break
end
945de171 Marc Dequenes
end
6bc51624 Marc Dequènes (Duck)
logger.debug "Mail check finished"
945de171 Marc Dequenes
b0a510ef Marc Dequènes (Duck)
@imap.expunge
end

def disconnect
return unless @imap

7bb2ac7f Marc Dequènes (Duck)
begin
@imap.logout if @is_loggued

logger.debug "Disconnecting from IMAP server..."
@imap.disconnect
logger.debug "Disconnected from IMAP server"
rescue IOError
logger.debug "Already diconnected from IMAP server"
ensure
@imap = nil
@is_loggued = false
@imap_capab = nil
end
945de171 Marc Dequenes
end
end
end