15 self.class.i_am_the_instance self
19 BufferManager.spawn_unless_exists("<poll for new messages>", :hidden => true) { PollMode.new }
23 BufferManager.flash "Polling for new messages..."
24 num, numi = buffer.mode.poll
26 BufferManager.flash "Loaded #{num} new messages, #{numi} to inbox."
28 BufferManager.flash "No new messages."
34 @thread = Redwood::reporting_thread do
37 poll if @last_poll.nil? || (Time.now - @last_poll) >= DELAY
43 @thread.kill if @thread
48 total_num = total_numi = 0
50 Index.usual_sources.each do |source|
51 # yield "source #{source} is done? #{source.done?} (cur_offset #{source.cur_offset} >= #{source.end_offset})"
53 yield "Loading from #{source}... " unless source.done? || source.has_errors?
54 rescue SourceError => e
55 Redwood::log "problem getting messages from #{source}: #{e.message}"
56 Redwood::report_broken_sources
62 add_messages_from source do |m, offset, entry|
63 ## always preserve the labels on disk.
64 m.labels = entry[:label].split(/\s+/).map { |x| x.intern } if entry
65 yield "Found message at #{offset} with labels {#{m.labels * ', '}}"
68 numi += 1 if m.labels.include? :inbox
72 yield "Found #{num} messages, #{numi} to inbox." unless num == 0
77 yield "Done polling; loaded #{total_num} new messages total"
81 [total_num, total_numi]
84 ## this is the main mechanism for adding new messages to the
85 ## index. it's called both by sup-sync and by PollMode.
87 ## for each message in the source, starting from the source's
88 ## starting offset, this methods yields the message, the source
89 ## offset, and the index entry on disk (if any). it expects the
90 ## yield to return the message (possibly altered in some way), and
91 ## then adds it (if new) or updates it (if previously seen).
93 ## the labels of the yielded message are the default source
94 ## labels. it is likely that callers will want to replace these with
95 ## the index labels, if they exist, so that state is not lost when
96 ## e.g. a new version of a message from a mailing list comes in.
97 def add_messages_from source
99 return if source.done? || source.has_errors?
101 source.each do |offset, labels|
102 if source.has_errors?
103 Redwood::log "error loading messages from #{source}: #{source.error.message}"
107 labels.each { |l| LabelManager << l }
108 labels = labels + (source.archived? ? [] : [:inbox])
111 m = Message.new :source => source, :source_info => offset, :labels => labels
112 if m.source_marked_read?
113 m.remove_label :unread
114 labels.delete :unread
120 docid, entry = Index.load_entry_for_id m.id
121 m = yield(m, offset, entry) or next
122 Index.sync_message m, docid, entry
123 UpdateManager.relay self, :add, m unless entry
124 rescue MessageFormatError => e
125 Redwood::log "ignoring erroneous message at #{source}##{offset}: #{e.message}"
128 rescue SourceError => e
129 Redwood::log "problem getting messages from #{source}: #{e.message}"
130 Redwood::report_broken_sources