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 "after-add-message", <<EOS
15 Executes after all messages are added to the index.
17 messages: an array of the new messages added
20 HookManager.register "before-poll", <<EOS
21 Executes immediately before a poll for new messages commences.
25 HookManager.register "after-poll", <<EOS
26 Executes immediately after a poll for new messages completes.
28 num: the total number of new messages added in this poll
29 num_inbox: the number of new messages added in this poll which
30 appear in the inbox (i.e. were not auto-archived).
31 num_inbox_total_unread: the total number of unread messages in the inbox
32 from_and_subj: an array of (from email address, subject) pairs
33 from_and_subj_inbox: an array of (from email address, subject) pairs for
34 only those messages appearing in the inbox
45 self.class.i_am_the_instance self
49 b, new = BufferManager.spawn_unless_exists("poll for new messages", :hidden => true, :system => true) { PollMode.new }
56 HookManager.run "before-poll"
58 BufferManager.flash "Polling for new messages..."
59 num, numi, from_and_subj, from_and_subj_inbox = buffer.mode.poll
61 BufferManager.flash "Loaded #{num.pluralize 'new message'}, #{numi} to inbox."
63 BufferManager.flash "No new messages."
66 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] }
73 @thread = Redwood::reporting_thread("periodic poll") do
76 poll if @last_poll.nil? || (Time.now - @last_poll) >= DELAY
82 @thread.kill if @thread
87 total_num = total_numi = 0
89 from_and_subj_inbox = []
92 SourceManager.usual_sources.each do |source|
93 # yield "source #{source} is done? #{source.done?} (cur_offset #{source.cur_offset} >= #{source.end_offset})"
95 yield "Loading from #{source}... " unless source.done? || (source.respond_to?(:has_errors?) && source.has_errors?)
96 rescue SourceError => e
97 Redwood::log "problem getting messages from #{source}: #{e.message}"
98 Redwood::report_broken_sources :force_to_top => true
104 add_messages_from source do |m_old, m, offset|
105 ## always preserve the labels on disk.
106 m.labels = ((m.labels - [:unread, :inbox]) + m_old.labels).uniq if m_old
107 yield "Found message at #{offset} with labels {#{m.labels * ', '}}"
110 from_and_subj << [m.from && m.from.longname, m.subj]
111 if m.has_label?(:inbox) && ([:spam, :deleted, :killed] & m.labels).empty?
112 from_and_subj_inbox << [m.from && m.from.longname, m.subj]
118 yield "Found #{num} messages, #{numi} to inbox." unless num == 0
123 yield "Done polling; loaded #{total_num} new messages total"
124 @last_poll = Time.now
127 [total_num, total_numi, from_and_subj, from_and_subj_inbox]
130 ## this is the main mechanism for adding new messages to the
131 ## index. it's called both by sup-sync and by PollMode.
133 ## for each message in the source, starting from the source's
134 ## starting offset, this methods yields the message, the source
135 ## offset, and the index entry on disk (if any). it expects the
136 ## yield to return the message (possibly altered in some way), and
137 ## then adds it (if new) or updates it (if previously seen).
139 ## the labels of the yielded message are the default source
140 ## labels. it is likely that callers will want to replace these with
141 ## the index labels, if they exist, so that state is not lost when
142 ## e.g. a new version of a message from a mailing list comes in.
143 def add_messages_from source, opts={}
145 return if source.done? || source.has_errors?
148 source.each do |offset, default_labels|
149 if source.has_errors?
150 Redwood::log "error loading messages from #{source}: #{source.error.message}"
154 m_new = Message.build_from_source source, offset
156 m_old = Index.build_message m_new.id
158 m_new.labels += default_labels + (source.archived? ? [] : [:inbox])
159 m_new.labels << :sent if source.uri.eql?(SentManager.source_uri)
160 m_new.labels.delete :unread if m_new.source_marked_read?
161 m_new.labels.each { |l| LabelManager << l }
163 HookManager.run "before-add-message", :message => m_new
164 m_ret = yield(m_old, m_new, offset) or next if block_given?
165 Index.sync_message m_ret, opts
166 UpdateManager.relay self, :added, m_ret unless m_old
168 HookManager.run "after-add-message", :messages => messages
170 rescue SourceError => e
171 Redwood::log "problem getting messages from #{source}: #{e.message}"
172 Redwood::report_broken_sources :force_to_top => true