+ yield "Done polling; loaded #{total_num} new messages total"
+ @last_poll = Time.now
+ @polling = false
+ end
+ [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-sync and by PollMode.
+ ##
+ ## for each message in the source, starting from the source's
+ ## starting offset, this methods 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).
+ ##
+ ## the labels of the yielded message are the default 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_messages_from source
+ begin
+ return if source.done? || source.has_errors?
+
+ source.each do |offset, 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
+
+ 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}"