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, :system => 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 SourceManager.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.respond_to?(:has_errors?) && 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_old, m, offset|
99 ## always preserve the labels on disk.
100 m.labels = ((m.labels - [:unread, :inbox]) + m_old.labels).uniq if m_old
101 yield "Found message at #{offset} with labels {#{m.labels * ', '}}"
104 from_and_subj << [m.from && m.from.longname, m.subj]
105 if m.has_label?(:inbox) && ([:spam, :deleted, :killed] & m.labels).empty?
106 from_and_subj_inbox << [m.from && 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, opts={}
139 return if source.done? || source.has_errors?
141 source.each do |offset, default_labels|
142 if source.has_errors?
143 Redwood::log "error loading messages from #{source}: #{source.error.message}"
147 m_new = Message.build_from_source source, offset
148 m_old = Index.build_message m_new.id
150 m_new.labels = default_labels + (source.archived? ? [] : [:inbox])
151 m_new.labels << :sent if source.uri.eql?(SentManager.source_uri)
152 m_new.labels.delete :unread if m_new.source_marked_read?
153 m_new.labels.each { |l| LabelManager << l }
155 HookManager.run "before-add-message", :message => m_new
156 m_ret = yield(m_old, m_new, offset) or next if block_given?
157 Index.sync_message m_ret, opts
158 UpdateManager.relay self, :added, m_ret unless m_old
160 rescue SourceError => e
161 Redwood::log "problem getting messages from #{source}: #{e.message}"
162 Redwood::report_broken_sources :force_to_top => true