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 after a poll for new messages completes.
16 num: the total number of new messages
17 num_inbox: the number of new messages appearing in the inbox (i.e.
19 from_and_subj: an array of (from email address, subject) pairs
20 from_and_subj_inbox: an array of (from email address, subject) pairs for
21 messages appearing in the inbox
32 self.class.i_am_the_instance self
36 BufferManager.spawn_unless_exists("<poll for new messages>", :hidden => true) { PollMode.new }
42 HookManager.run "before-poll"
44 BufferManager.flash "Polling for new messages..."
45 num, numi, from_and_subj, from_and_subj_inbox = buffer.mode.poll
47 BufferManager.flash "Loaded #{num} new messages, #{numi} to inbox."
49 BufferManager.flash "No new messages."
52 HookManager.run "after-poll", :num => num, :num_inbox => numi, :from_and_subj => from_and_subj, :from_and_subj_inbox => from_and_subj_inbox
59 @thread = Redwood::reporting_thread do
62 poll if @last_poll.nil? || (Time.now - @last_poll) >= DELAY
68 @thread.kill if @thread
73 total_num = total_numi = 0
75 from_and_subj_inbox = []
78 Index.usual_sources.each do |source|
79 # yield "source #{source} is done? #{source.done?} (cur_offset #{source.cur_offset} >= #{source.end_offset})"
81 yield "Loading from #{source}... " unless source.done? || source.has_errors?
82 rescue SourceError => e
83 Redwood::log "problem getting messages from #{source}: #{e.message}"
84 Redwood::report_broken_sources
90 add_messages_from source do |m, offset, entry|
91 ## always preserve the labels on disk.
92 m.labels = entry[:label].split(/\s+/).map { |x| x.intern } if entry
93 yield "Found message at #{offset} with labels {#{m.labels * ', '}}"
96 from_and_subj << [m.from.longname, m.subj]
97 if m.labels.include? :inbox
98 from_and_subj_inbox << [m.from.longname, m.subj]
104 yield "Found #{num} messages, #{numi} to inbox." unless num == 0
109 yield "Done polling; loaded #{total_num} new messages total"
110 @last_poll = Time.now
113 [total_num, total_numi, from_and_subj, from_and_subj_inbox]
116 ## this is the main mechanism for adding new messages to the
117 ## index. it's called both by sup-sync and by PollMode.
119 ## for each message in the source, starting from the source's
120 ## starting offset, this methods yields the message, the source
121 ## offset, and the index entry on disk (if any). it expects the
122 ## yield to return the message (possibly altered in some way), and
123 ## then adds it (if new) or updates it (if previously seen).
125 ## the labels of the yielded message are the default source
126 ## labels. it is likely that callers will want to replace these with
127 ## the index labels, if they exist, so that state is not lost when
128 ## e.g. a new version of a message from a mailing list comes in.
129 def add_messages_from source
131 return if source.done? || source.has_errors?
133 source.each do |offset, labels|
134 if source.has_errors?
135 Redwood::log "error loading messages from #{source}: #{source.error.message}"
139 labels.each { |l| LabelManager << l }
140 labels = labels + (source.archived? ? [] : [:inbox])
143 m = Message.new :source => source, :source_info => offset, :labels => labels
144 if m.source_marked_read?
145 m.remove_label :unread
146 labels.delete :unread
152 docid, entry = Index.load_entry_for_id m.id
153 m = yield(m, offset, entry) or next
154 Index.sync_message m, docid, entry
155 UpdateManager.relay self, :add, m unless entry
156 rescue MessageFormatError => e
157 Redwood::log "ignoring erroneous message at #{source}##{offset}: #{e.message}"
160 rescue SourceError => e
161 Redwood::log "problem getting messages from #{source}: #{e.message}"
162 Redwood::report_broken_sources