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 added in this poll
23 num_inbox: the number of new messages added in this poll which
24 appear in the inbox (i.e. were not auto-archived).
25 num_inbox_total_unread: the total number of unread messages in the inbox
26 from_and_subj: an array of (from email address, subject) pairs
27 from_and_subj_inbox: an array of (from email address, subject) pairs for
28 only those messages appearing in the inbox
39 self.class.i_am_the_instance self
43 b, new = BufferManager.spawn_unless_exists("<poll for new messages>", :hidden => true) { PollMode.new }
50 HookManager.run "before-poll"
52 BufferManager.flash "Polling for new messages..."
53 num, numi, from_and_subj, from_and_subj_inbox = buffer.mode.poll
55 BufferManager.flash "Loaded #{num.pluralize 'new message'}, #{numi} to inbox."
57 BufferManager.flash "No new messages."
60 HookManager.run "after-poll", :num => num, :num_inbox => numi, :from_and_subj => from_and_subj, :from_and_subj_inbox => from_and_subj_inbox, :num_inbox_total_unread => lambda { Index.num_results_for :labels => [:inbox, :unread] }
67 @thread = Redwood::reporting_thread("periodic poll") do
70 poll if @last_poll.nil? || (Time.now - @last_poll) >= DELAY
76 @thread.kill if @thread
81 total_num = total_numi = 0
83 from_and_subj_inbox = []
86 Index.usual_sources.each do |source|
87 # yield "source #{source} is done? #{source.done?} (cur_offset #{source.cur_offset} >= #{source.end_offset})"
89 yield "Loading from #{source}... " unless source.done? || source.has_errors?
90 rescue SourceError => e
91 Redwood::log "problem getting messages from #{source}: #{e.message}"
92 Redwood::report_broken_sources :force_to_top => true
98 add_messages_from source do |m, offset, entry|
99 ## always preserve the labels on disk.
100 m.labels = entry[:label].split(/\s+/).map { |x| x.intern } if entry
101 yield "Found message at #{offset} with labels {#{m.labels * ', '}}"
104 from_and_subj << [m.from.longname, m.subj]
105 if m.has_label?(:inbox) && ([:spam, :deleted, :killed] & m.labels).empty?
106 from_and_subj_inbox << [m.from.longname, m.subj]
112 yield "Found #{num} messages, #{numi} to inbox." unless num == 0
117 yield "Done polling; loaded #{total_num} new messages total"
118 @last_poll = Time.now
121 [total_num, total_numi, from_and_subj, from_and_subj_inbox]
124 ## this is the main mechanism for adding new messages to the
125 ## index. it's called both by sup-sync and by PollMode.
127 ## for each message in the source, starting from the source's
128 ## starting offset, this methods yields the message, the source
129 ## offset, and the index entry on disk (if any). it expects the
130 ## yield to return the message (possibly altered in some way), and
131 ## then adds it (if new) or updates it (if previously seen).
133 ## the labels of the yielded message are the default source
134 ## labels. it is likely that callers will want to replace these with
135 ## the index labels, if they exist, so that state is not lost when
136 ## e.g. a new version of a message from a mailing list comes in.
137 def add_messages_from source
139 return if source.done? || source.has_errors?
141 source.each do |offset, labels|
142 if source.has_errors?
143 Redwood::log "error loading messages from #{source}: #{source.error.message}"
147 labels.each { |l| LabelManager << l }
148 labels = labels + (source.archived? ? [] : [:inbox])
151 m = Message.new :source => source, :source_info => offset, :labels => labels
152 if m.source_marked_read?
153 m.remove_label :unread
154 labels.delete :unread
157 docid, entry = Index.load_entry_for_id m.id
158 HookManager.run "before-add-message", :message => m
159 m = yield(m, offset, entry) or next if block_given?
160 Index.sync_message m, docid, entry
161 UpdateManager.relay self, :added, m unless entry
162 rescue MessageFormatError => e
163 Redwood::log "ignoring erroneous message at #{source}##{offset}: #{e.message}"
166 rescue SourceError => e
167 Redwood::log "problem getting messages from #{source}: #{e.message}"
168 Redwood::report_broken_sources :force_to_top => true