Project

General

Profile

« Previous | Next » 

Revision 7dce67e9

Added by Marc Dequènes over 14 years ago

  • ID 7dce67e920dbb65ac880a9346cc2d00a1b49db1a

[evol] IMAP IDLE support finished (closes #100)

View differences:

lib/cyborghood/imap.rb
#++
require 'net/imap'
require 'thread'
# IMAP IDLE support
class Net::IMAP
......
synchronize do
@idle_tag = generate_tag
add_response_handler @idle_response_handler
add_response_handler @idle_response_handler if @idle_response_handler
put_string "#{@idle_tag} IDLE#{CRLF}"
end
......
return unless @idle_mode
synchronize do
remove_response_handler @idle_response_handler
remove_response_handler @idle_response_handler if @idle_response_handler
put_string "DONE#{CRLF}"
end
......
@config = Config.instance
@imap = nil
@is_loggued = false
@available_mails = 0
@available_mails_mutex = Mutex.new
if not (@config.debug.nil? or @config.debug.flags.nil?) and @config.debug.flags.include?(:debug_imapverbose)
Net::IMAP.debug = true
......
end
def delete
if @config.debug.nil? or @config.debug.flags.nil? or not @config.debug.flags.include?(:debug_nomaildeletion)
@imap.store(@message_id, "+FLAGS", [:Deleted])
end
if @config.debug.nil? or @config.debug.flags.nil? or not @config.debug.flags.include?(:debug_nomaildeletion)
@imap.store(@message_id, "+FLAGS", [:Deleted])
end
end
end
......
true
end
def check_mails_once(&message_handler)
connect &&
authenticate &&
check_inbox(&message_handler)
if @imap.capability.include?("IDLE")
logger.debug "Waiting for new mails in idle mode"
@imap.idle do |resp|
if resp.kind_of?(Net::IMAP::UntaggedResponse) and resp.name == "EXISTS"
message_id = resp.data
logger.debug "*** Received mail ##{message_id}"
# TODO: can't do that, a new IMAP connection should be used, this one is dedicated to IDLE mode
message_handler.call IMAPMessage.new(@imap, message_id)
def listen_to_events
@imap.add_response_handler do |resp|
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
end
end
sleep(1) until @stop_mail_check
@imap.idle_done
end
true
end
def check_mails_once(&message_handler)
connect &&
authenticate &&
listen_to_events &&
wait_and_read_mails(&message_handler)
rescue SocketError, Net::IMAP::NoResponseError => e
logger.warn "IMAP or Network error: " + e.message
rescue Net::IMAP::Error => e
......
end
end
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
return if not (@config.debug.nil? or @config.debug.flags.nil?) and @config.debug.flags.include?(:debug_nomaildeletion)
end while waiting_mails?
if @imap.capability.include?("IDLE")
logger.debug "Waiting for new mails in idle mode"
@imap.idle #do |resp|
sleep(1) until @stop_mail_check or waiting_mails?
@imap.idle_done
end
end
end
def check_mails(&message_handler)
@stop_mail_check = false
until @stop_mail_check
......
end
logger.debug "Mail check finished"
# TODO: expunge after <n> mails processed in IDLE mode
@imap.expunge
end

Also available in: Unified diff