X-Git-Url: https://git.cworth.org/git?a=blobdiff_plain;f=lib%2Fsup%2Fpoll.rb;h=87cf9963796286f73a92f87478c207e23300cfa0;hb=70a488fdcda9d20c65e7c81edca7c4d34aa88672;hp=690caa295c45e98deebd9710853529d12ef29e3a;hpb=e8012219ca2879c235e3c60abc0947cd5212f1dd;p=sup diff --git a/lib/sup/poll.rb b/lib/sup/poll.rb index 690caa2..87cf996 100644 --- a/lib/sup/poll.rb +++ b/lib/sup/poll.rb @@ -5,32 +5,66 @@ module Redwood class PollManager include Singleton + HookManager.register "before-add-message", <", :hidden => true) { PollMode.new } + b, new = BufferManager.spawn_unless_exists("", :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 = 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." + 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, :num_inbox_total_unread => lambda { Index.num_results_for :labels => [:inbox, :unread] } + + @polling = false [num, numi] end - def start_thread - Redwood::reporting_thread do + def start + @thread = Redwood::reporting_thread("periodic poll") do while true sleep DELAY / 2 poll if @last_poll.nil? || (Time.now - @last_poll) >= DELAY @@ -38,8 +72,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})" @@ -47,7 +89,7 @@ class PollManager 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 @@ -59,11 +101,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.has_label?(:inbox) && ([:spam, :deleted, :killed] & m.labels).empty? + 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 +118,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 @@ -88,7 +134,7 @@ class PollManager ## 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? @@ -99,7 +145,7 @@ class PollManager 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 @@ -109,16 +155,17 @@ class PollManager 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