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
41 b, new = BufferManager.spawn_unless_exists("poll for new messages", :hidden => true, :system => 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.pluralize 'new message'}, #{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, :num_inbox_total_unread => lambda { Index.num_results_for :labels => [:inbox, :unread] }
65 @thread = Redwood::reporting_thread("periodic poll") 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 SourceManager.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.respond_to?(:has_errors?) && source.has_errors?)
88 rescue SourceError => e
89 Redwood::log "problem getting messages from #{source}: #{e.message}"
90 Redwood::report_broken_sources :force_to_top => true
96 each_message_from source do |m|
97 yield "Found message at #{m.source_info} with labels {#{m.labels.to_a * ', '}}"
98 old_m = Index.build_message m.id
100 if old_m.source.id != source.id || old_m.source_info != m.source_info
101 ## here we merge labels between new and old versions, but we don't let the new
102 ## message add :unread or :inbox labels. (they can exist in the old version,
103 ## just not be added.)
104 new_labels = old_m.labels + (m.labels - [:unread, :inbox])
105 yield "Message at #{m.source_info} is an updated of an old message. Updating labels from #{m.labels.to_a * ','} => #{new_labels.to_a * ','}"
106 m.labels = new_labels
107 Index.update_message m
109 yield "Skipping already-imported message at #{m.source_info}"
112 yield "Found new message at #{m.source_info} with labels #{m.labels.to_a * ','}"
115 from_and_subj << [m.from && m.from.longname, m.subj]
116 if (m.labels & [:inbox, :spam, :deleted, :killed]) == Set.new([:inbox])
117 from_and_subj_inbox << [m.from && m.from.longname, m.subj]
123 yield "Found #{num} messages, #{numi} to inbox." unless num == 0
128 yield "Done polling; loaded #{total_num} new messages total"
129 @last_poll = Time.now
132 [total_num, total_numi, from_and_subj, from_and_subj_inbox]
135 ## like Source#each, but yields successive Message objects, which have their
136 ## labels and offsets set correctly.
138 ## this is the primary mechanism for iterating over messages from a source.
139 def each_message_from source, opts={}
141 return if source.done? || source.has_errors?
143 source.each do |offset, source_labels|
144 if source.has_errors?
145 Redwood::log "error loading messages from #{source}: #{source.error.message}"
149 m = Message.build_from_source source, offset
150 m.labels += source_labels + (source.archived? ? [] : [:inbox])
151 m.labels.delete :unread if m.source_marked_read? # preserve read status if possible
152 m.labels.each { |l| LabelManager << l }
154 HookManager.run "before-add-message", :message => m
157 rescue SourceError => e
158 Redwood::log "problem getting messages from #{source}: #{e.message}"
159 Redwood::report_broken_sources :force_to_top => true
163 ## TODO: see if we can do this within PollMode rather than by calling this
166 ## a wrapper around Index.add_message that calls the proper hooks,
167 ## does the gui callback stuff, etc.
168 def add_new_message m
170 UpdateManager.relay self, :added, m