Revision c77a4651
Added by Marc Dequènes almost 12 years ago
lib/cyborghood/imap.rb | ||
---|---|---|
require 'net/imap'
|
||
require 'thread'
|
||
|
||
# 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
|
||
add_response_handler @idle_response_handler if @idle_response_handler
|
||
put_string "#{@idle_tag} IDLE#{CRLF}"
|
||
end
|
||
|
||
@idle_mode = true
|
||
end
|
||
|
||
def idle_done
|
||
return unless @idle_mode
|
||
|
||
synchronize do
|
||
remove_response_handler @idle_response_handler if @idle_response_handler
|
||
put_string "DONE#{CRLF}"
|
||
end
|
||
|
||
@idle_mode = false
|
||
|
||
begin
|
||
return get_tagged_response @idle_tag
|
||
rescue
|
||
end
|
||
end
|
||
|
||
def idle?
|
||
@idle_mode || false
|
||
end
|
||
end
|
||
|
||
module CyborgHood
|
||
class IMAP
|
||
... | ... | |
def connect
|
||
return true if @imap
|
||
|
||
# using SSL because TLS does not work in the NET::IMAP library in Ruby 1.8
|
||
# (but available in 1.9)
|
||
ssl = (not @params.ca_cert.nil?)
|
||
port = @params.port || (ssl ? 993 : 143)
|
||
check_ca = ssl
|
||
logger.debug "Connecting to the IMAP server..."
|
||
begin
|
||
@imap = Net::IMAP.new(@params.host, port, ssl, @params.ca_cert, check_ca)
|
||
@imap = Net::IMAP.new(@params.host, {:port => port, :ssl => ssl,
|
||
:options => {:ca_file => @params.ca_cert, :verify_mode => OpenSSL::SSL::VERIFY_PEER}})
|
||
rescue SocketError
|
||
logger.warn "Could not connect to the IMAP server"
|
||
return false
|
||
... | ... | |
logger.debug "Waiting for new mails in idle mode"
|
||
until @stop_mail_check
|
||
return if @imap.nil? or not connected?
|
||
@imap.idle
|
||
@imap.idle do |ev|
|
||
return if ev.kind_of? Net::IMAP::ContinuationRequest
|
||
return if ev.name == "OK"
|
||
@imap.idle_done
|
||
end
|
||
# restart IDLE after 5 min
|
||
t = Time.now.to_i
|
||
sleep(1) until Time.now.to_i - t >= 300 or @stop_mail_check or waiting_mails?
|
||
@imap.idle_done
|
||
end
|
||
end
|
||
end
|
Also available in: Unified diff
[evol] updated IMAP handling for Ruby 1.9