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
44 @mode ||= PollMode.new
45 HookManager.run "before-poll"
47 BufferManager.flash "Polling for new messages..."
48 num, numi, from_and_subj, from_and_subj_inbox = @mode.poll
50 BufferManager.flash "Loaded #{num.pluralize 'new message'}, #{numi} to inbox."
52 BufferManager.flash "No new messages."
55 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] }
62 @thread = Redwood::reporting_thread("periodic poll") do
65 poll if @last_poll.nil? || (Time.now - @last_poll) >= DELAY
71 @thread.kill if @thread
76 total_num = total_numi = 0
78 from_and_subj_inbox = []
81 SourceManager.usual_sources.each do |source|
82 # yield "source #{source} is done? #{source.done?} (cur_offset #{source.cur_offset} >= #{source.end_offset})"
84 yield "Loading from #{source}... " unless source.done? || (source.respond_to?(:has_errors?) && source.has_errors?)
85 rescue SourceError => e
86 warn "problem getting messages from #{source}: #{e.message}"
87 Redwood::report_broken_sources :force_to_top => true
93 each_message_from source do |m|
94 yield "Found message at #{m.source_info} with labels {#{m.labels.to_a * ', '}}"
95 old_m = Index.build_message m.id
97 if old_m.source.id != source.id || old_m.source_info != m.source_info
98 ## here we merge labels between new and old versions, but we don't let the new
99 ## message add :unread or :inbox labels. (they can exist in the old version,
100 ## just not be added.)
101 new_labels = old_m.labels + (m.labels - [:unread, :inbox])
102 yield "Message at #{m.source_info} is an updated of an old message. Updating labels from #{m.labels.to_a * ','} => #{new_labels.to_a * ','}"
103 m.labels = new_labels
104 Index.update_message m
106 yield "Skipping already-imported message at #{m.source_info}"
109 yield "Found new message at #{m.source_info} with labels #{m.labels.to_a * ','}"
112 from_and_subj << [m.from && m.from.longname, m.subj]
113 if (m.labels & [:inbox, :spam, :deleted, :killed]) == Set.new([:inbox])
114 from_and_subj_inbox << [m.from && m.from.longname, m.subj]
120 yield "Found #{num} messages, #{numi} to inbox." unless num == 0
125 yield "Done polling; loaded #{total_num} new messages total"
126 @last_poll = Time.now
129 [total_num, total_numi, from_and_subj, from_and_subj_inbox]
132 ## like Source#each, but yields successive Message objects, which have their
133 ## labels and offsets set correctly.
135 ## this is the primary mechanism for iterating over messages from a source.
136 def each_message_from source, opts={}
138 return if source.done? || source.has_errors?
140 source.each do |offset, source_labels|
141 if source.has_errors?
142 warn "error loading messages from #{source}: #{source.error.message}"
146 m = Message.build_from_source source, offset
147 m.labels += source_labels + (source.archived? ? [] : [:inbox])
148 m.labels.delete :unread if m.source_marked_read? # preserve read status if possible
149 m.labels.each { |l| LabelManager << l }
151 HookManager.run "before-add-message", :message => m
154 rescue SourceError => e
155 warn "problem getting messages from #{source}: #{e.message}"
156 Redwood::report_broken_sources :force_to_top => true
160 ## TODO: see if we can do this within PollMode rather than by calling this
163 ## a wrapper around Index.add_message that calls the proper hooks,
164 ## does the gui callback stuff, etc.
165 def add_new_message m
167 UpdateManager.relay self, :added, m