]> git.cworth.org Git - sup/blobdiff - lib/sup/poll.rb
add an after-add-message hook
[sup] / lib / sup / poll.rb
index 2b9a9c5c310d71037527ba8d9461d57992f9cd5b..fb3aacf4311a297bca2923c3e7aeda050dc63c32 100644 (file)
@@ -11,6 +11,12 @@ Variables:
   message: the new message
 EOS
 
+  HookManager.register "after-add-message", <<EOS
+Executes after all messages are added to the index.
+Variables:
+  messages: an array of the new messages added
+EOS
+
   HookManager.register "before-poll", <<EOS
 Executes immediately before a poll for new messages commences.
 No variables.
@@ -19,12 +25,13 @@ 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
-                       only those 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
@@ -39,7 +46,7 @@ EOS
   end
 
   def buffer
-    b, new = BufferManager.spawn_unless_exists("<poll for new messages>", :hidden => true) { PollMode.new }
+    b, new = BufferManager.spawn_unless_exists("poll for new messages", :hidden => true, :system => true) { PollMode.new }
     b
   end
 
@@ -56,7 +63,7 @@ EOS
       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]
@@ -82,10 +89,10 @@ EOS
     from_and_subj_inbox = []
 
     @mutex.synchronize do
-      Index.usual_sources.each do |source|
+      SourceManager.usual_sources.each do |source|
 #        yield "source #{source} is done? #{source.done?} (cur_offset #{source.cur_offset} >= #{source.end_offset})"
         begin
-          yield "Loading from #{source}... " unless source.done? || source.has_errors?
+          yield "Loading from #{source}... " unless source.done? || (source.respond_to?(:has_errors?) && source.has_errors?)
         rescue SourceError => e
           Redwood::log "problem getting messages from #{source}: #{e.message}"
           Redwood::report_broken_sources :force_to_top => true
@@ -94,15 +101,15 @@ EOS
 
         num = 0
         numi = 0
-        add_messages_from source do |m, offset, entry|
+        add_messages_from source do |m_old, m, offset|
           ## always preserve the labels on disk.
-          m.labels = entry[:label].split(/\s+/).map { |x| x.intern } if entry
+          m.labels = ((m.labels - [:unread, :inbox]) + m_old.labels).uniq if m_old
           yield "Found message at #{offset} with labels {#{m.labels * ', '}}"
-          unless entry
+          unless m_old
             num += 1
-            from_and_subj << [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.longname, m.subj]
+              from_and_subj_inbox << [m.from && m.from.longname, m.subj]
               numi += 1 
             end
           end
@@ -133,35 +140,33 @@ 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?
-      
-      source.each do |offset, labels|
+
+      messages = []
+      source.each do |offset, default_labels|
         if source.has_errors?
           Redwood::log "error loading messages from #{source}: #{source.error.message}"
           return
         end
-      
-        labels.each { |l| LabelManager << l }
-        labels = labels + (source.archived? ? [] : [:inbox])
 
-        begin
-          m = Message.new :source => source, :source_info => offset, :labels => labels
-          if m.source_marked_read?
-            m.remove_label :unread
-            labels.delete :unread
-          end
+        m_new = Message.build_from_source source, offset
+        messages.push(m_new)
+        m_old = Index.build_message m_new.id
 
-          docid, entry = Index.load_entry_for_id m.id
-          HookManager.run "before-add-message", :message => m
-          m = yield(m, offset, entry) or next
-          Index.sync_message m, docid, entry
-          UpdateManager.relay self, :added, m unless entry
-        rescue MessageFormatError => e
-          Redwood::log "ignoring erroneous message at #{source}##{offset}: #{e.message}"
-        end
+        m_new.labels += default_labels + (source.archived? ? [] : [:inbox])
+        m_new.labels << :sent if source.uri.eql?(SentManager.source_uri)
+        m_new.labels.delete :unread if m_new.source_marked_read?
+        m_new.labels.each { |l| LabelManager << l }
+
+        HookManager.run "before-add-message", :message => m_new
+        m_ret = yield(m_old, m_new, offset) or next if block_given?
+        Index.sync_message m_ret, opts
+        UpdateManager.relay self, :added, m_ret unless m_old
       end
+      HookManager.run "after-add-message", :messages => messages
+
     rescue SourceError => e
       Redwood::log "problem getting messages from #{source}: #{e.message}"
       Redwood::report_broken_sources :force_to_top => true