]> git.cworth.org Git - sup/blobdiff - lib/sup/poll.rb
add --search option to sup (thanks to Marcus Williams)
[sup] / lib / sup / poll.rb
index adf1d06d0719df10659e533ca52bc4926379f9f5..f36c37fb0407e3746513c520023f48ebea029851 100644 (file)
@@ -5,11 +5,29 @@ module Redwood
 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
@@ -19,18 +37,26 @@ class PollManager
   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
@@ -38,8 +64,16 @@ class PollManager
     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})"
@@ -59,11 +93,15 @@ class PollManager
           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
@@ -72,7 +110,7 @@ class PollManager
       @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
@@ -94,12 +132,12 @@ class PollManager
       
       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