8 HookManager.register "before-add-message", <<EOS
9 Executes immediately before a message is added to the index.
11 message: the new message
14 HookManager.register "before-poll", <<EOS
15 Executes immediately before a poll for new messages commences.
19 HookManager.register "after-poll", <<EOS
20 Executes immediately after a poll for new messages completes.
22 num: the total number of new messages
23 num_inbox: the number of new messages appearing in the inbox (i.e.
25 from_and_subj: an array of (from email address, subject) pairs
26 from_and_subj_inbox: an array of (from email address, subject) pairs for
27 only those messages appearing in the inbox
38 self.class.i_am_the_instance self
42 b, new = BufferManager.spawn_unless_exists("<poll for new messages>", :hidden => true) { PollMode.new }
49 HookManager.run "before-poll"
51 BufferManager.flash "Polling for new messages..."
52 num, numi, from_and_subj, from_and_subj_inbox = buffer.mode.poll
54 BufferManager.flash "Loaded #{num.pluralize 'new message'}, #{numi} to inbox."
56 BufferManager.flash "No new messages."
59 HookManager.run "after-poll", :num => num, :num_inbox => numi, :from_and_subj => from_and_subj, :from_and_subj_inbox => from_and_subj_inbox
66 @thread = Redwood::reporting_thread("periodic poll") do
69 poll if @last_poll.nil? || (Time.now - @last_poll) >= DELAY
75 @thread.kill if @thread
80 total_num = total_numi = 0
82 from_and_subj_inbox = []
85 Index.usual_sources.each do |source|
86 # yield "source #{source} is done? #{source.done?} (cur_offset #{source.cur_offset} >= #{source.end_offset})"
88 yield "Loading from #{source}... " unless source.done? || source.has_errors?
89 rescue SourceError => e
90 Redwood::log "problem getting messages from #{source}: #{e.message}"
91 Redwood::report_broken_sources :force_to_top => true
97 add_messages_from source do |m, offset, entry|
98 ## always preserve the labels on disk.
99 m.labels = entry[:label].split(/\s+/).map { |x| x.intern } if entry
100 yield "Found message at #{offset} with labels {#{m.labels * ', '}}"
103 from_and_subj << [m.from.longname, m.subj]
104 if m.has_label?(:inbox) && ([:spam, :deleted, :killed] & m.labels).empty?
105 from_and_subj_inbox << [m.from.longname, m.subj]
111 yield "Found #{num} messages, #{numi} to inbox." unless num == 0
116 yield "Done polling; loaded #{total_num} new messages total"
117 @last_poll = Time.now
120 [total_num, total_numi, from_and_subj, from_and_subj_inbox]
123 ## this is the main mechanism for adding new messages to the
124 ## index. it's called both by sup-sync and by PollMode.
126 ## for each message in the source, starting from the source's
127 ## starting offset, this methods yields the message, the source
128 ## offset, and the index entry on disk (if any). it expects the
129 ## yield to return the message (possibly altered in some way), and
130 ## then adds it (if new) or updates it (if previously seen).
132 ## the labels of the yielded message are the default source
133 ## labels. it is likely that callers will want to replace these with
134 ## the index labels, if they exist, so that state is not lost when
135 ## e.g. a new version of a message from a mailing list comes in.
136 def add_messages_from source
138 return if source.done? || source.has_errors?
140 source.each do |offset, labels|
141 if source.has_errors?
142 Redwood::log "error loading messages from #{source}: #{source.error.message}"
146 labels.each { |l| LabelManager << l }
147 labels = labels + (source.archived? ? [] : [:inbox])
150 m = Message.new :source => source, :source_info => offset, :labels => labels
151 if m.source_marked_read?
152 m.remove_label :unread
153 labels.delete :unread
156 docid, entry = Index.load_entry_for_id m.id
157 HookManager.run "before-add-message", :message => m
158 m = yield(m, offset, entry) or next
159 Index.sync_message m, docid, entry
160 UpdateManager.relay self, :add, m unless entry
161 rescue MessageFormatError => e
162 Redwood::log "ignoring erroneous message at #{source}##{offset}: #{e.message}"
165 rescue SourceError => e
166 Redwood::log "problem getting messages from #{source}: #{e.message}"
167 Redwood::report_broken_sources :force_to_top => true