class PollManager
include Singleton
+ HookManager.register "before-poll", <<EOS
+Executes immediately before a poll for new messages commences.
+No variables.
+EOS
+
+ HookManager.register "after-poll", <<EOS
+Executes immediately after a poll for new messages completes.
+Variables:
+ num: the total number of new messages
+ num_inbox: the number of new messages appearing in the inbox (i.e.
+ not auto-archived).
+ from_and_subj: an array of (from email address, subject) pairs
+ from_and_subj_inbox: an array of (from email address, subject) pairs for
+ messages appearing in the inbox
+EOS
+
DELAY = 300
def initialize
@mutex = Mutex.new
+ @thread = nil
@last_poll = nil
+ @polling = false
self.class.i_am_the_instance self
end
end
def poll
+ return if @polling
+ @polling = true
+ HookManager.run "before-poll"
+
BufferManager.flash "Polling for new messages..."
- num, numi = buffer.mode.poll
+ num, numi, from_and_subj, from_and_subj_inbox = buffer.mode.poll
if num > 0
BufferManager.flash "Loaded #{num} new messages, #{numi} to inbox."
else
BufferManager.flash "No new messages."
end
+
+ HookManager.run "after-poll", :num => num, :num_inbox => numi, :from_and_subj => from_and_subj, :from_and_subj_inbox => from_and_subj_inbox
+
+ @polling = false
[num, numi]
end
- def start_thread
- Redwood::reporting_thread do
+ def start
+ @thread = Redwood::reporting_thread do
while true
sleep DELAY / 2
poll if @last_poll.nil? || (Time.now - @last_poll) >= DELAY
end
end
+ def stop
+ @thread.kill if @thread
+ @thread = nil
+ end
+
def do_poll
total_num = total_numi = 0
+ from_and_subj = []
+ from_and_subj_inbox = []
+
@mutex.synchronize do
Index.usual_sources.each do |source|
# yield "source #{source} is done? #{source.done?} (cur_offset #{source.cur_offset} >= #{source.end_offset})"
yield "Found message at #{offset} with labels {#{m.labels * ', '}}"
unless entry
num += 1
- numi += 1 if m.labels.include? :inbox
+ from_and_subj << [m.from.longname, m.subj]
+ if m.labels.include? :inbox
+ from_and_subj_inbox << [m.from.longname, m.subj]
+ numi += 1
+ end
end
m
end
- yield "Found #{num} messages, #{numi} to inbox" unless num == 0
+ yield "Found #{num} messages, #{numi} to inbox." unless num == 0
total_num += num
total_numi += numi
end
@last_poll = Time.now
@polling = false
end
- [total_num, total_numi]
+ [total_num, total_numi, from_and_subj, from_and_subj_inbox]
end
## this is the main mechanism for adding new messages to the
source.each do |offset, labels|
if source.has_errors?
- Redwood::log "error loading messages from #{source}: #{source.broken_msg}"
+ Redwood::log "error loading messages from #{source}: #{source.error.message}"
return
end
labels.each { |l| LabelManager << l }
- labels += [:inbox] unless source.archived?
+ labels = labels + (source.archived? ? [] : [:inbox])
begin
m = Message.new :source => source, :source_info => offset, :labels => labels