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 BufferManager.spawn_unless_exists("<poll for new messages>", :hidden => true) { PollMode.new }
48 HookManager.run "before-poll"
50 BufferManager.flash "Polling for new messages..."
51 num, numi, from_and_subj, from_and_subj_inbox = buffer.mode.poll
53 BufferManager.flash "Loaded #{num} new messages, #{numi} to inbox."
55 BufferManager.flash "No new messages."
58 HookManager.run "after-poll", :num => num, :num_inbox => numi, :from_and_subj => from_and_subj, :from_and_subj_inbox => from_and_subj_inbox
65 @thread = Redwood::reporting_thread do
68 poll if @last_poll.nil? || (Time.now - @last_poll) >= DELAY
74 @thread.kill if @thread
79 total_num = total_numi = 0
81 from_and_subj_inbox = []
84 Index.usual_sources.each do |source|
85 # yield "source #{source} is done? #{source.done?} (cur_offset #{source.cur_offset} >= #{source.end_offset})"
87 yield "Loading from #{source}... " unless source.done? || source.has_errors?
88 rescue SourceError => e
89 Redwood::log "problem getting messages from #{source}: #{e.message}"
90 Redwood::report_broken_sources
96 add_messages_from source do |m, offset, entry|
97 ## always preserve the labels on disk.
98 m.labels = entry[:label].split(/\s+/).map { |x| x.intern } if entry
99 yield "Found message at #{offset} with labels {#{m.labels * ', '}}"
102 from_and_subj << [m.from.longname, m.subj]
103 if m.labels.include? :inbox
104 from_and_subj_inbox << [m.from.longname, m.subj]
110 yield "Found #{num} messages, #{numi} to inbox." unless num == 0
115 yield "Done polling; loaded #{total_num} new messages total"
116 @last_poll = Time.now
119 [total_num, total_numi, from_and_subj, from_and_subj_inbox]
122 ## this is the main mechanism for adding new messages to the
123 ## index. it's called both by sup-sync and by PollMode.
125 ## for each message in the source, starting from the source's
126 ## starting offset, this methods yields the message, the source
127 ## offset, and the index entry on disk (if any). it expects the
128 ## yield to return the message (possibly altered in some way), and
129 ## then adds it (if new) or updates it (if previously seen).
131 ## the labels of the yielded message are the default source
132 ## labels. it is likely that callers will want to replace these with
133 ## the index labels, if they exist, so that state is not lost when
134 ## e.g. a new version of a message from a mailing list comes in.
135 def add_messages_from source
137 return if source.done? || source.has_errors?
139 source.each do |offset, labels|
140 if source.has_errors?
141 Redwood::log "error loading messages from #{source}: #{source.error.message}"
145 labels.each { |l| LabelManager << l }
146 labels = labels + (source.archived? ? [] : [:inbox])
149 m = Message.new :source => source, :source_info => offset, :labels => labels
150 if m.source_marked_read?
151 m.remove_label :unread
152 labels.delete :unread
155 docid, entry = Index.load_entry_for_id m.id
156 HookManager.run "before-add-message", :message => m
157 m = yield(m, offset, entry) or next
158 Index.sync_message m, docid, entry
159 UpdateManager.relay self, :add, m unless entry
160 rescue MessageFormatError => e
161 Redwood::log "ignoring erroneous message at #{source}##{offset}: #{e.message}"
164 rescue SourceError => e
165 Redwood::log "problem getting messages from #{source}: #{e.message}"
166 Redwood::report_broken_sources