]> git.cworth.org Git - sup/blob - lib/sup/poll.rb
rewrite Singleton to not require i_am_the_instance
[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 "before-poll", <<EOS
15 Executes immediately before a poll for new messages commences.
16 No variables.
17 EOS
18
19   HookManager.register "after-poll", <<EOS
20 Executes immediately after a poll for new messages completes.
21 Variables:
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
29 EOS
30
31   DELAY = 300
32
33   def initialize
34     @mutex = Mutex.new
35     @thread = nil
36     @last_poll = nil
37     @polling = false
38   end
39
40   def buffer
41     b, new = BufferManager.spawn_unless_exists("poll for new messages", :hidden => true, :system => true) { PollMode.new }
42     b
43   end
44
45   def poll
46     return if @polling
47     @polling = true
48     HookManager.run "before-poll"
49
50     BufferManager.flash "Polling for new messages..."
51     num, numi, from_and_subj, from_and_subj_inbox = buffer.mode.poll
52     if num > 0
53       BufferManager.flash "Loaded #{num.pluralize 'new message'}, #{numi} to inbox." 
54     else
55       BufferManager.flash "No new messages." 
56     end
57
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] }
59
60     @polling = false
61     [num, numi]
62   end
63
64   def start
65     @thread = Redwood::reporting_thread("periodic poll") do
66       while true
67         sleep DELAY / 2
68         poll if @last_poll.nil? || (Time.now - @last_poll) >= DELAY
69       end
70     end
71   end
72
73   def stop
74     @thread.kill if @thread
75     @thread = nil
76   end
77
78   def do_poll
79     total_num = total_numi = 0
80     from_and_subj = []
81     from_and_subj_inbox = []
82
83     @mutex.synchronize do
84       SourceManager.usual_sources.each do |source|
85 #        yield "source #{source} is done? #{source.done?} (cur_offset #{source.cur_offset} >= #{source.end_offset})"
86         begin
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
91           next
92         end
93
94         num = 0
95         numi = 0
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
99           if old_m
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
108             else
109               yield "Skipping already-imported message at #{m.source_info}"
110             end
111           else
112             yield "Found new message at #{m.source_info} with labels #{m.labels.to_a * ','}"
113             Index.add_message m
114             num += 1
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]
118               numi += 1
119             end
120           end
121           m
122         end
123         yield "Found #{num} messages, #{numi} to inbox." unless num == 0
124         total_num += num
125         total_numi += numi
126       end
127
128       yield "Done polling; loaded #{total_num} new messages total"
129       @last_poll = Time.now
130       @polling = false
131     end
132     [total_num, total_numi, from_and_subj, from_and_subj_inbox]
133   end
134
135   ## like Source#each, but yields successive Message objects, which have their
136   ## labels and offsets set correctly.
137   ##
138   ## this is the primary mechanism for iterating over messages from a source.
139   def each_message_from source, opts={}
140     begin
141       return if source.done? || source.has_errors?
142
143       source.each do |offset, source_labels|
144         if source.has_errors?
145           Redwood::log "error loading messages from #{source}: #{source.error.message}"
146           return
147         end
148
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 }
153
154         HookManager.run "before-add-message", :message => m
155         yield m
156       end
157     rescue SourceError => e
158       Redwood::log "problem getting messages from #{source}: #{e.message}"
159       Redwood::report_broken_sources :force_to_top => true
160     end
161   end
162
163   ## TODO: see if we can do this within PollMode rather than by calling this
164   ## method.
165   ##
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
169     Index.add_message m
170     UpdateManager.relay self, :added, m
171   end
172 end
173
174 end