X-Git-Url: https://git.cworth.org/git?a=blobdiff_plain;f=lib%2Fsup%2Fpoll.rb;h=4374242cefe39af7682b1db80f34f9ac0a06dedf;hb=4e496c5bd66de3d916795901ecdbb52e6f1cee84;hp=cbad96c3f2a2f1804b3881966874473ee2f04125;hpb=ee34231b2932ae29c7b9c93f1198f33c9fab8372;p=sup diff --git a/lib/sup/poll.rb b/lib/sup/poll.rb index cbad96c..4374242 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("poll for new messages", :hidden => true, :system => 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,24 +72,57 @@ 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 - found = {} - 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})" - yield "Loading from #{source}... " unless source.done? || source.broken? + begin + 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 + next + end + num = 0 numi = 0 - add_new_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 - yield "Found message at #{offset} with labels #{m.labels * ', '}" - num += 1 - numi += 1 if m.labels.include? :inbox + each_message_from source do |m| + yield "Found message at #{m.source_info} with labels {#{m.labels.to_a * ', '}}" + old_m = Index.build_message m.id + if old_m + if old_m.source.id != source.id || old_m.source_info != m.source_info + ## here we merge labels between new and old versions, but we don't let the new + ## message add :unread or :inbox labels. (they can exist in the old version, + ## just not be added.) + new_labels = old_m.labels + (m.labels - [:unread, :inbox]) + yield "Message at #{m.source_info} is an updated of an old message. Updating labels from #{m.labels.to_a * ','} => #{new_labels.to_a * ','}" + m.labels = new_labels + Index.update_message m + else + yield "Skipping already-imported message at #{m.source_info}" + end + else + yield "Found new message at #{m.source_info} with labels #{m.labels.to_a * ','}" + Index.add_message m + num += 1 + from_and_subj << [m.from && m.from.longname, m.subj] + if (m.labels & [:inbox, :spam, :deleted, :killed]) == Set.new([:inbox]) + from_and_subj_inbox << [m.from && 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 @@ -64,61 +131,46 @@ 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 - ## index. it's called both by sup-import and by PollMode. - ## - ## for each new message in the source, this yields the message, the - ## source offset, and the index entry on disk (if any). it expects - ## the yield to return the message (possibly altered in some way), - ## and then adds it (if new) or updates it (if previously seen). + ## like Source#each, but yields successive Message objects, which have their + ## labels and offsets set correctly. ## - ## the labels of the yielded message are the source 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_new_messages_from source - found = {} - return if source.done? || source.broken? - - source.each do |offset, labels| - if source.broken? - Redwood::log "error loading messages from #{source}: #{source.broken_msg}" - return - end - - labels.each { |l| LabelManager << l } + ## this is the primary mechanism for iterating over messages from a source. + def each_message_from source, opts={} + begin + return if source.done? || source.has_errors? - begin - m = Message.new :source => source, :source_info => offset, :labels => labels - if found[m.id] - Redwood::log "skipping duplicate message #{m.id}" - next - else - found[m.id] = true + source.each do |offset, source_labels| + if source.has_errors? + Redwood::log "error loading messages from #{source}: #{source.error.message}" + return end - if m.source_marked_read? - m.remove_label :unread - labels.delete :unread - end + m = Message.build_from_source source, offset + m.labels += source_labels + (source.archived? ? [] : [:inbox]) + m.labels.delete :unread if m.source_marked_read? # preserve read status if possible + m.labels.each { |l| LabelManager << l } - docid, entry = Index.load_entry_for_id m.id - m = yield m, offset, entry - next unless m - if entry - Index.update_message m, docid, entry - else - Index.add_message m - UpdateManager.relay :add, m - end - rescue MessageFormatError, SourceError => e - Redwood::log "ignoring erroneous message at #{source}##{offset}: #{e.message}" + HookManager.run "before-add-message", :message => m + yield m end + rescue SourceError => e + Redwood::log "problem getting messages from #{source}: #{e.message}" + Redwood::report_broken_sources :force_to_top => true end end + + ## TODO: see if we can do this within PollMode rather than by calling this + ## method. + ## + ## a wrapper around Index.add_message that calls the proper hooks, + ## does the gui callback stuff, etc. + def add_new_message m + Index.add_message m + UpdateManager.relay self, :added, m + end end end