8 HookManager.register "before-poll", <<EOS
9 Executes immediately before a poll for new messages commences.
13 HookManager.register "after-poll", <<EOS
14 Executes immediately before a poll for new messages commences.
16 num: the total number of new messages
17 num_inbox: the number of new messages appearing in the inbox (i.e. not
19 from_and_subj: an array of (from email address, subject) pairs
29 self.class.i_am_the_instance self
33 BufferManager.spawn_unless_exists("<poll for new messages>", :hidden => true) { PollMode.new }
37 HookManager.run "before-poll"
39 BufferManager.flash "Polling for new messages..."
40 num, numi, from_and_subj = buffer.mode.poll
42 BufferManager.flash "Loaded #{num} new messages, #{numi} to inbox."
44 BufferManager.flash "No new messages."
47 HookManager.run "after-poll", :num => num, :num_inbox => numi, :from_and_subj => from_and_subj
53 @thread = Redwood::reporting_thread do
56 poll if @last_poll.nil? || (Time.now - @last_poll) >= DELAY
62 @thread.kill if @thread
67 total_num = total_numi = 0
71 Index.usual_sources.each do |source|
72 # yield "source #{source} is done? #{source.done?} (cur_offset #{source.cur_offset} >= #{source.end_offset})"
74 yield "Loading from #{source}... " unless source.done? || source.has_errors?
75 rescue SourceError => e
76 Redwood::log "problem getting messages from #{source}: #{e.message}"
77 Redwood::report_broken_sources
83 add_messages_from source do |m, offset, entry|
84 ## always preserve the labels on disk.
85 m.labels = entry[:label].split(/\s+/).map { |x| x.intern } if entry
86 yield "Found message at #{offset} with labels {#{m.labels * ', '}}"
89 numi += 1 if m.labels.include? :inbox
90 from_and_subj << [m.from.longname, m.subj]
94 yield "Found #{num} messages, #{numi} to inbox." unless num == 0
99 yield "Done polling; loaded #{total_num} new messages total"
100 @last_poll = Time.now
103 [total_num, total_numi, from_and_subj]
106 ## this is the main mechanism for adding new messages to the
107 ## index. it's called both by sup-sync and by PollMode.
109 ## for each message in the source, starting from the source's
110 ## starting offset, this methods yields the message, the source
111 ## offset, and the index entry on disk (if any). it expects the
112 ## yield to return the message (possibly altered in some way), and
113 ## then adds it (if new) or updates it (if previously seen).
115 ## the labels of the yielded message are the default source
116 ## labels. it is likely that callers will want to replace these with
117 ## the index labels, if they exist, so that state is not lost when
118 ## e.g. a new version of a message from a mailing list comes in.
119 def add_messages_from source
121 return if source.done? || source.has_errors?
123 source.each do |offset, labels|
124 if source.has_errors?
125 Redwood::log "error loading messages from #{source}: #{source.error.message}"
129 labels.each { |l| LabelManager << l }
130 labels = labels + (source.archived? ? [] : [:inbox])
133 m = Message.new :source => source, :source_info => offset, :labels => labels
134 if m.source_marked_read?
135 m.remove_label :unread
136 labels.delete :unread
142 docid, entry = Index.load_entry_for_id m.id
143 m = yield(m, offset, entry) or next
144 Index.sync_message m, docid, entry
145 UpdateManager.relay self, :add, m unless entry
146 rescue MessageFormatError => e
147 Redwood::log "ignoring erroneous message at #{source}##{offset}: #{e.message}"
150 rescue SourceError => e
151 Redwood::log "problem getting messages from #{source}: #{e.message}"
152 Redwood::report_broken_sources