]> git.cworth.org Git - sup/blobdiff - lib/sup/poll.rb
bugfix: poll.rb assuming all messages have froms
[sup] / lib / sup / poll.rb
index 45e62103355c3cdaf532e0f91b0e2362966d4f00..5027218c49d66318b309a062c1d222d81699d3a1 100644 (file)
@@ -5,20 +5,27 @@ module Redwood
 class PollManager
   include Singleton
 
+  HookManager.register "before-add-message", <<EOS
+Executes immediately before a message is added to the index.
+Variables:
+  message: the new message
+EOS
+
   HookManager.register "before-poll", <<EOS
 Executes immediately before a poll for new messages commences.
 No variables.
 EOS
 
   HookManager.register "after-poll", <<EOS
-Executes immediately before a poll for new messages commences.
+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
+                   num: the total number of new messages added in this poll
+             num_inbox: the number of new messages added in this poll which
+                        appear in the inbox (i.e. were not auto-archived).
+num_inbox_total_unread: the total number of unread messages in the inbox
+         from_and_subj: an array of (from email address, subject) pairs
+   from_and_subj_inbox: an array of (from email address, subject) pairs for
+                        only those messages appearing in the inbox
 EOS
 
   DELAY = 300
@@ -27,32 +34,37 @@ EOS
     @mutex = Mutex.new
     @thread = nil
     @last_poll = nil
+    @polling = false
     
     self.class.i_am_the_instance self
   end
 
   def buffer
-    BufferManager.spawn_unless_exists("<poll for new messages>", :hidden => true) { PollMode.new }
+    b, new = BufferManager.spawn_unless_exists("<poll for new messages>", :hidden => true) { PollMode.new }
+    b
   end
 
   def poll
+    return if @polling
+    @polling = true
     HookManager.run "before-poll"
 
     BufferManager.flash "Polling for new messages..."
     num, numi, from_and_subj, from_and_subj_inbox = buffer.mode.poll
     if num > 0
-      BufferManager.flash "Loaded #{num} new messages, #{numi} to inbox." 
+      BufferManager.flash "Loaded #{num.pluralize 'new message'}, #{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
+    HookManager.run "after-poll", :num => num, :num_inbox => numi, :from_and_subj => from_and_subj, :from_and_subj_inbox => from_and_subj_inbox, :num_inbox_total_unread => lambda { Index.num_results_for :labels => [:inbox, :unread] }
 
+    @polling = false
     [num, numi]
   end
 
   def start
-    @thread = Redwood::reporting_thread do
+    @thread = Redwood::reporting_thread("periodic poll") do
       while true
         sleep DELAY / 2
         poll if @last_poll.nil? || (Time.now - @last_poll) >= DELAY
@@ -77,7 +89,7 @@ EOS
           yield "Loading from #{source}... " unless source.done? || source.has_errors?
         rescue SourceError => e
           Redwood::log "problem getting messages from #{source}: #{e.message}"
-          Redwood::report_broken_sources
+          Redwood::report_broken_sources :force_to_top => true
           next
         end
 
@@ -85,13 +97,13 @@ EOS
         numi = 0
         add_messages_from source do |m, offset, entry|
           ## always preserve the labels on disk.
-          m.labels = entry[:label].split(/\s+/).map { |x| x.intern } if entry
+          m.labels = ((m.labels - [:unread, :inbox]) + entry[:label].split(/\s+/).map { |x| x.intern }).uniq if entry
           yield "Found message at #{offset} with labels {#{m.labels * ', '}}"
           unless entry
             num += 1
-            from_and_subj << [m.from.longname, m.subj]
-            if m.labels.include? :inbox
-              from_and_subj_inbox << [m.from.longname, m.subj]
+            from_and_subj << [m.from && m.from.longname, m.subj]
+            if m.has_label?(:inbox) && ([:spam, :deleted, :killed] & m.labels).empty?
+              from_and_subj_inbox << [m.from && m.from.longname, m.subj]
               numi += 1 
             end
           end
@@ -122,7 +134,7 @@ EOS
   ## labels. it is likely that callers will want to replace these with
   ## the index labels, if they exist, so that state is not lost when
   ## e.g. a new version of a message from a mailing list comes in.
-  def add_messages_from source
+  def add_messages_from source, opts={}
     begin
       return if source.done? || source.has_errors?
       
@@ -140,22 +152,20 @@ EOS
           if m.source_marked_read?
             m.remove_label :unread
             labels.delete :unread
-          else
-            m.add_label :unread
-            labels << :unread
           end
 
           docid, entry = Index.load_entry_for_id m.id
-          m = yield(m, offset, entry) or next
-          Index.sync_message m, docid, entry
-          UpdateManager.relay self, :add, m unless entry
+          HookManager.run "before-add-message", :message => m
+          m = yield(m, offset, entry) or next if block_given?
+          Index.sync_message m, docid, entry, opts
+          UpdateManager.relay self, :added, m unless entry
         rescue MessageFormatError => e
           Redwood::log "ignoring erroneous message at #{source}##{offset}: #{e.message}"
         end
       end
     rescue SourceError => e
       Redwood::log "problem getting messages from #{source}: #{e.message}"
-      Redwood::report_broken_sources
+      Redwood::report_broken_sources :force_to_top => true
     end
   end
 end