class MailRoom::MailboxWatcher

Watch a Mailbox @author Tony Pitale

Attributes

idling_thread[RW]

Public Class Methods

new(mailbox) click to toggle source

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

disconnected?() click to toggle source

is the imap connection closed? @return [Boolean]

# File lib/mail_room/mailbox_watcher.rb, line 48
def disconnected?
  @imap.disconnected?
end
handler() click to toggle source

build a handler to process mailbox messages

# File lib/mail_room/mailbox_watcher.rb, line 24
def handler
  @handler ||= MailboxHandler.new(@mailbox, imap)
end
idle() click to toggle source

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
idling?() click to toggle source

is the connection blocked idling? @return [Boolean]

# File lib/mail_room/mailbox_watcher.rb, line 42
def idling?
  @idling
end
imap() click to toggle source

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
log_in() click to toggle source

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
logged_in?() click to toggle source

is the connection logged in? @return [Boolean]

# File lib/mail_room/mailbox_watcher.rb, line 36
def logged_in?
  @logged_in
end
message_exists?(response) click to toggle source

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
process_mailbox() click to toggle source

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
quit() click to toggle source

stop running

# File lib/mail_room/mailbox_watcher.rb, line 146
def quit
  @running = false
  stop_idling
  # disconnect
end
ready_to_idle?() click to toggle source

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
reset() click to toggle source

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() click to toggle source

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
running?() click to toggle source

are we running? @return [Boolean]

# File lib/mail_room/mailbox_watcher.rb, line 30
def running?
  @running
end
set_mailbox() click to toggle source

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
setup() click to toggle source

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_tls() click to toggle source

start a TLS session

# File lib/mail_room/mailbox_watcher.rb, line 82
def start_tls
  imap.starttls if @mailbox.start_tls
end
stop_idling() click to toggle source

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

idle_handler() click to toggle source

@private

# File lib/mail_room/mailbox_watcher.rb, line 159
def idle_handler
  lambda {|response| imap.idle_done if message_exists?(response)}
end