]> git.cworth.org Git - sup/blob - lib/sup/poll.rb
add an after-add-message hook
[sup] / lib / sup / poll.rb
1 require 'thread'
2
3 module Redwood
4
5 class PollManager
6   include Singleton
7
8   HookManager.register "before-add-message", <<EOS
9 Executes immediately before a message is added to the index.
10 Variables:
11   message: the new message
12 EOS
13
14   HookManager.register "after-add-message", <<EOS
15 Executes after all messages are added to the index.
16 Variables:
17   messages: an array of the new messages added
18 EOS
19
20   HookManager.register "before-poll", <<EOS
21 Executes immediately before a poll for new messages commences.
22 No variables.
23 EOS
24
25   HookManager.register "after-poll", <<EOS
26 Executes immediately after a poll for new messages completes.
27 Variables:
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
35 EOS
36
37   DELAY = 300
38
39   def initialize
40     @mutex = Mutex.new
41     @thread = nil
42     @last_poll = nil
43     @polling = false
44     
45     self.class.i_am_the_instance self
46   end
47
48   def buffer
49     b, new = BufferManager.spawn_unless_exists("poll for new messages", :hidden => true, :system => true) { PollMode.new }
50     b
51   end
52
53   def poll
54     return if @polling
55     @polling = true
56     HookManager.run "before-poll"
57
58     BufferManager.flash "Polling for new messages..."
59     num, numi, from_and_subj, from_and_subj_inbox = buffer.mode.poll
60     if num > 0
61       BufferManager.flash "Loaded #{num.pluralize 'new message'}, #{numi} to inbox." 
62     else
63       BufferManager.flash "No new messages." 
64     end
65
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] }
67
68     @polling = false
69     [num, numi]
70   end
71
72   def start
73     @thread = Redwood::reporting_thread("periodic poll") do
74       while true
75         sleep DELAY / 2
76         poll if @last_poll.nil? || (Time.now - @last_poll) >= DELAY
77       end
78     end
79   end
80
81   def stop
82     @thread.kill if @thread
83     @thread = nil
84   end
85
86   def do_poll
87     total_num = total_numi = 0
88     from_and_subj = []
89     from_and_subj_inbox = []
90
91     @mutex.synchronize do
92       SourceManager.usual_sources.each do |source|
93 #        yield "source #{source} is done? #{source.done?} (cur_offset #{source.cur_offset} >= #{source.end_offset})"
94         begin
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
99           next
100         end
101
102         num = 0
103         numi = 0
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 * ', '}}"
108           unless m_old
109             num += 1
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]
113               numi += 1 
114             end
115           end
116           m
117         end
118         yield "Found #{num} messages, #{numi} to inbox." unless num == 0
119         total_num += num
120         total_numi += numi
121       end
122
123       yield "Done polling; loaded #{total_num} new messages total"
124       @last_poll = Time.now
125       @polling = false
126     end
127     [total_num, total_numi, from_and_subj, from_and_subj_inbox]
128   end
129
130   ## this is the main mechanism for adding new messages to the
131   ## index. it's called both by sup-sync and by PollMode.
132   ##
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).
138   ##
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={}
144     begin
145       return if source.done? || source.has_errors?
146
147       messages = []
148       source.each do |offset, default_labels|
149         if source.has_errors?
150           Redwood::log "error loading messages from #{source}: #{source.error.message}"
151           return
152         end
153
154         m_new = Message.build_from_source source, offset
155         messages.push(m_new)
156         m_old = Index.build_message m_new.id
157
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 }
162
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
167       end
168       HookManager.run "after-add-message", :messages => messages
169
170     rescue SourceError => e
171       Redwood::log "problem getting messages from #{source}: #{e.message}"
172       Redwood::report_broken_sources :force_to_top => true
173     end
174   end
175 end
176
177 end