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.
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
31 self.class.i_am_the_instance self
35 BufferManager.spawn_unless_exists("<poll for new messages>", :hidden => true) { PollMode.new }
39 HookManager.run "before-poll"
41 BufferManager.flash "Polling for new messages..."
42 num, numi, from_and_subj, from_and_subj_inbox = buffer.mode.poll
44 BufferManager.flash "Loaded #{num} new messages, #{numi} to inbox."
46 BufferManager.flash "No new messages."
49 HookManager.run "after-poll", :num => num, :num_inbox => numi, :from_and_subj => from_and_subj, :from_and_subj_inbox => from_and_subj_inbox
55 @thread = Redwood::reporting_thread do
58 poll if @last_poll.nil? || (Time.now - @last_poll) >= DELAY
64 @thread.kill if @thread
69 total_num = total_numi = 0
71 from_and_subj_inbox = []
74 Index.usual_sources.each do |source|
75 # yield "source #{source} is done? #{source.done?} (cur_offset #{source.cur_offset} >= #{source.end_offset})"
77 yield "Loading from #{source}... " unless source.done? || source.has_errors?
78 rescue SourceError => e
79 Redwood::log "problem getting messages from #{source}: #{e.message}"
80 Redwood::report_broken_sources
86 add_messages_from source do |m, offset, entry|
87 ## always preserve the labels on disk.
88 m.labels = entry[:label].split(/\s+/).map { |x| x.intern } if entry
89 yield "Found message at #{offset} with labels {#{m.labels * ', '}}"
92 from_and_subj << [m.from.longname, m.subj]
93 if m.labels.include? :inbox
94 from_and_subj_inbox << [m.from.longname, m.subj]
100 yield "Found #{num} messages, #{numi} to inbox." unless num == 0
105 yield "Done polling; loaded #{total_num} new messages total"
106 @last_poll = Time.now
109 [total_num, total_numi, from_and_subj, from_and_subj_inbox]
112 ## this is the main mechanism for adding new messages to the
113 ## index. it's called both by sup-sync and by PollMode.
115 ## for each message in the source, starting from the source's
116 ## starting offset, this methods yields the message, the source
117 ## offset, and the index entry on disk (if any). it expects the
118 ## yield to return the message (possibly altered in some way), and
119 ## then adds it (if new) or updates it (if previously seen).
121 ## the labels of the yielded message are the default source
122 ## labels. it is likely that callers will want to replace these with
123 ## the index labels, if they exist, so that state is not lost when
124 ## e.g. a new version of a message from a mailing list comes in.
125 def add_messages_from source
127 return if source.done? || source.has_errors?
129 source.each do |offset, labels|
130 if source.has_errors?
131 Redwood::log "error loading messages from #{source}: #{source.error.message}"
135 labels.each { |l| LabelManager << l }
136 labels = labels + (source.archived? ? [] : [:inbox])
139 m = Message.new :source => source, :source_info => offset, :labels => labels
140 if m.source_marked_read?
141 m.remove_label :unread
142 labels.delete :unread
148 docid, entry = Index.load_entry_for_id m.id
149 m = yield(m, offset, entry) or next
150 Index.sync_message m, docid, entry
151 UpdateManager.relay self, :add, m unless entry
152 rescue MessageFormatError => e
153 Redwood::log "ignoring erroneous message at #{source}##{offset}: #{e.message}"
156 rescue SourceError => e
157 Redwood::log "problem getting messages from #{source}: #{e.message}"
158 Redwood::report_broken_sources