lib/sup/poll.rb in sup-0.2 vs lib/sup/poll.rb in sup-0.3

- old
+ new

@@ -3,10 +3,16 @@ 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 @@ -16,11 +22,11 @@ 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 + only those messages appearing in the inbox EOS DELAY = 300 def initialize @@ -31,22 +37,23 @@ 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 @@ -54,11 +61,11 @@ @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 end end @@ -79,11 +86,11 @@ # 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? 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 num = 0 numi = 0 @@ -92,11 +99,11 @@ m.labels = entry[:label].split(/\s+/).map { |x| x.intern } 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 + if m.has_label?(:inbox) && ([:spam, :deleted, :killed] & m.labels).empty? from_and_subj_inbox << [m.from.longname, m.subj] numi += 1 end end m @@ -145,19 +152,20 @@ m.remove_label :unread labels.delete :unread end 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, :add, 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 end