Class: MailRoom::MailboxWatcher

Inherits:
Object
  • Object
show all
Defined in:
lib/mail_room/mailbox_watcher.rb

Overview

Watch a Mailbox

Author:

  • Tony Pitale

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(mailbox) ⇒ MailboxWatcher

Watch a new mailbox

Parameters:



11
12
13
14
15
16
# File 'lib/mail_room/mailbox_watcher.rb', line 11

def initialize(mailbox)
  @mailbox = mailbox

  reset
  @running = false
end

Instance Attribute Details

#idling_threadObject

Returns the value of attribute idling_thread.



7
8
9
# File 'lib/mail_room/mailbox_watcher.rb', line 7

def idling_thread
  @idling_thread
end

Instance Method Details

#disconnected?Boolean

is the imap connection closed?

Returns:

  • (Boolean)


48
49
50
# File 'lib/mail_room/mailbox_watcher.rb', line 48

def disconnected?
  @imap.disconnected?
end

#handlerObject

build a handler to process mailbox messages



24
25
26
# File 'lib/mail_room/mailbox_watcher.rb', line 24

def handler
  @handler ||= MailboxHandler.new(@mailbox, imap)
end

#idleObject

maintain an imap idle connection



92
93
94
95
96
97
98
99
100
# File 'lib/mail_room/mailbox_watcher.rb', line 92

def idle
  return unless ready_to_idle?

  @idling = true

  imap.idle(&idle_handler)
ensure
  @idling = false
end

#idling?Boolean

is the connection blocked idling?

Returns:

  • (Boolean)


42
43
44
# File 'lib/mail_room/mailbox_watcher.rb', line 42

def idling?
  @idling
end

#imapObject

build a net/imap connection to google imap



19
20
21
# File 'lib/mail_room/mailbox_watcher.rb', line 19

def imap
  @imap ||= Net::IMAP.new(@mailbox.host, :port => @mailbox.port, :ssl => @mailbox.ssl)
end

#log_inObject

send the imap login command to google



81
82
83
84
# File 'lib/mail_room/mailbox_watcher.rb', line 81

def 
  imap.(@mailbox.email, @mailbox.password)
  @logged_in = true
end

#logged_in?Boolean

is the connection logged in?

Returns:

  • (Boolean)


36
37
38
# File 'lib/mail_room/mailbox_watcher.rb', line 36

def logged_in?
  @logged_in
end

#message_exists?(response) ⇒ Boolean

is the response for a new message?

Parameters:

  • response (Net::IMAP::TaggedResponse)

    the imap response from idle

Returns:

  • (Boolean)


61
62
63
# File 'lib/mail_room/mailbox_watcher.rb', line 61

def message_exists?(response)
  response.respond_to?(:name) && response.name == 'EXISTS'
end

#process_mailboxObject

trigger the handler to process this mailbox for new messages



145
146
147
# File 'lib/mail_room/mailbox_watcher.rb', line 145

def process_mailbox
  handler.process
end

#quitObject

stop running



138
139
140
141
142
# File 'lib/mail_room/mailbox_watcher.rb', line 138

def quit
  @running = false
  stop_idling
  # disconnect
end

#ready_to_idle?Boolean

is the connection ready to idle?

Returns:

  • (Boolean)


54
55
56
# File 'lib/mail_room/mailbox_watcher.rb', line 54

def ready_to_idle?
  logged_in? && !idling?
end

#resetObject

clear disconnected imap reset imap state



74
75
76
77
78
# File 'lib/mail_room/mailbox_watcher.rb', line 74

def reset
  @imap = nil
  @logged_in = false
  @idling = false
end

#runObject

run the mailbox watcher



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/mail_room/mailbox_watcher.rb', line 111

def run
  setup

  @running = true

  # prefetch messages before first idle
  process_mailbox

  self.idling_thread = Thread.start do
    while(running?) do
      begin
        # block 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?Boolean

are we running?

Returns:

  • (Boolean)


30
31
32
# File 'lib/mail_room/mailbox_watcher.rb', line 30

def running?
  @running
end

#set_mailboxObject

select the mailbox name we want to use



87
88
89
# File 'lib/mail_room/mailbox_watcher.rb', line 87

def set_mailbox
  imap.select(@mailbox.name) if logged_in?
end

#setupObject

log in and set the mailbox



66
67
68
69
70
# File 'lib/mail_room/mailbox_watcher.rb', line 66

def setup
  reset
  
  set_mailbox
end

#stop_idlingObject

trigger the idle to finish and wait for the thread to finish



103
104
105
106
107
108
# File 'lib/mail_room/mailbox_watcher.rb', line 103

def stop_idling
  return unless idling?

  imap.idle_done
  idling_thread.join
end