14 self.class.i_am_the_instance self
18 BufferManager.spawn_unless_exists("<poll for new messages>", :hidden => true) { PollMode.new }
22 BufferManager.flash "Polling for new messages..."
23 num, numi = buffer.mode.poll
25 BufferManager.flash "Loaded #{num} new messages, #{numi} to inbox."
27 BufferManager.flash "No new messages."
33 Redwood::reporting_thread do
36 poll if @last_poll.nil? || (Time.now - @last_poll) >= DELAY
42 total_num = total_numi = 0
44 Index.usual_sources.each do |source|
45 # yield "source #{source} is done? #{source.done?} (cur_offset #{source.cur_offset} >= #{source.end_offset})"
47 yield "Loading from #{source}... " unless source.done? || source.has_errors?
48 rescue SourceError => e
49 Redwood::log "problem getting messages from #{source}: #{e.message}"
50 Redwood::report_broken_sources
56 add_messages_from source do |m, offset, entry|
57 ## always preserve the labels on disk.
58 m.labels = entry[:label].split(/\s+/).map { |x| x.intern } if entry
59 yield "Found message at #{offset} with labels {#{m.labels * ', '}}"
62 numi += 1 if m.labels.include? :inbox
66 yield "Found #{num} messages, #{numi} to inbox" unless num == 0
71 yield "Done polling; loaded #{total_num} new messages total"
75 [total_num, total_numi]
78 ## this is the main mechanism for adding new messages to the
79 ## index. it's called both by sup-sync and by PollMode.
81 ## for each message in the source, starting from the source's
82 ## starting offset, this methods yields the message, the source
83 ## offset, and the index entry on disk (if any). it expects the
84 ## yield to return the message (possibly altered in some way), and
85 ## then adds it (if new) or updates it (if previously seen).
87 ## the labels of the yielded message are the default source
88 ## labels. it is likely that callers will want to replace these with
89 ## the index labels, if they exist, so that state is not lost when
90 ## e.g. a new version of a message from a mailing list comes in.
91 def add_messages_from source
93 return if source.done? || source.has_errors?
95 source.each do |offset, labels|
97 Redwood::log "error loading messages from #{source}: #{source.error.message}"
101 labels.each { |l| LabelManager << l }
102 labels += [:inbox] unless source.archived?
105 m = Message.new :source => source, :source_info => offset, :labels => labels
106 if m.source_marked_read?
107 m.remove_label :unread
108 labels.delete :unread
114 docid, entry = Index.load_entry_for_id m.id
115 m = yield(m, offset, entry) or next
116 Index.sync_message m, docid, entry
117 UpdateManager.relay self, :add, m unless entry
118 rescue MessageFormatError => e
119 Redwood::log "ignoring erroneous message at #{source}##{offset}: #{e.message}"
122 rescue SourceError => e
123 Redwood::log "problem getting messages from #{source}: #{e.message}"
124 Redwood::report_broken_sources