class MailRoom::MailboxWatcher
Watch a Mailbox @author Tony Pitale
Attributes
Public Class Methods
Watch a new mailbox @param mailbox [MailRoom::Mailbox] the mailbox to watch
# File lib/mail_room/mailbox_watcher.rb, line 11 def initialize(mailbox) @mailbox = mailbox reset @running = false end
Public Instance Methods
is the imap connection closed? @return [Boolean]
# File lib/mail_room/mailbox_watcher.rb, line 48 def disconnected? @imap.disconnected? end
build a handler to process mailbox messages
# File lib/mail_room/mailbox_watcher.rb, line 24 def handler @handler ||= MailboxHandler.new(@mailbox, imap) end
maintain an imap idle connection
# File lib/mail_room/mailbox_watcher.rb, line 98 def idle return unless ready_to_idle? @idling = true imap.idle(@mailbox.idle_timeout, &idle_handler) ensure @idling = false end
is the connection blocked idling? @return [Boolean]
# File lib/mail_room/mailbox_watcher.rb, line 42 def idling? @idling end
build a net/imap connection to google imap
# File lib/mail_room/mailbox_watcher.rb, line 19 def imap @imap ||= MailRoom::IMAP.new(@mailbox.host, :port => @mailbox.port, :ssl => @mailbox.ssl_options) end
send the imap login command to google
# File lib/mail_room/mailbox_watcher.rb, line 87 def log_in imap.login(@mailbox.email, @mailbox.password) @logged_in = true end
is the connection logged in? @return [Boolean]
# File lib/mail_room/mailbox_watcher.rb, line 36 def logged_in? @logged_in end
is the response for a new message? @param response [Net::IMAP::TaggedResponse] the imap response from idle @return [Boolean]
# File lib/mail_room/mailbox_watcher.rb, line 61 def message_exists?(response) response.respond_to?(:name) && response.name == 'EXISTS' end
trigger the handler to process this mailbox for new messages
# File lib/mail_room/mailbox_watcher.rb, line 153 def process_mailbox handler.process end
stop running
# File lib/mail_room/mailbox_watcher.rb, line 146 def quit @running = false stop_idling # disconnect end
is the connection ready to idle? @return [Boolean]
# File lib/mail_room/mailbox_watcher.rb, line 54 def ready_to_idle? logged_in? && !idling? end
clear disconnected imap reset imap state
# File lib/mail_room/mailbox_watcher.rb, line 75 def reset @imap = nil @logged_in = false @idling = false end
run the mailbox watcher
# File lib/mail_room/mailbox_watcher.rb, line 119 def run setup @running = true # prefetch messages before first idle process_mailbox self.idling_thread = Thread.start do while(running?) do begin # block for idle_timeout until we stop idling idle # when new messages are ready process_mailbox rescue Net::IMAP::Error, IOError => e # we've been disconnected, so re-setup setup end end end idling_thread.abort_on_exception = true end
are we running? @return [Boolean]
# File lib/mail_room/mailbox_watcher.rb, line 30 def running? @running end
select the mailbox name we want to use
# File lib/mail_room/mailbox_watcher.rb, line 93 def set_mailbox imap.select(@mailbox.name) if logged_in? end
log in and set the mailbox
# File lib/mail_room/mailbox_watcher.rb, line 66 def setup reset start_tls log_in set_mailbox end
start a TLS session
# File lib/mail_room/mailbox_watcher.rb, line 82 def start_tls imap.starttls if @mailbox.start_tls end
trigger the idle to finish and wait for the thread to finish
# File lib/mail_room/mailbox_watcher.rb, line 109 def stop_idling return unless idling? imap.idle_done idling_thread.join self.idling_thread = nil end
Private Instance Methods
@private
# File lib/mail_room/mailbox_watcher.rb, line 159 def idle_handler lambda {|response| imap.idle_done if message_exists?(response)} end