]> git.cworth.org Git - sup/blob - lib/sup/poll.rb
Merge branch 'hook-local-vars' into next
[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     @mode = nil
39   end
40
41   def poll
42     return if @polling
43     @polling = true
44     @mode ||= PollMode.new
45     HookManager.run "before-poll"
46
47     BufferManager.flash "Polling for new messages..."
48     num, numi, from_and_subj, from_and_subj_inbox = @mode.poll
49     if num > 0
50       BufferManager.flash "Loaded #{num.pluralize 'new message'}, #{numi} to inbox." 
51     else
52       BufferManager.flash "No new messages." 
53     end
54
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] }
56
57     @polling = false
58     [num, numi]
59   end
60
61   def start
62     @thread = Redwood::reporting_thread("periodic poll") do
63       while true
64         sleep DELAY / 2
65         poll if @last_poll.nil? || (Time.now - @last_poll) >= DELAY
66       end
67     end
68   end
69
70   def stop
71     @thread.kill if @thread
72     @thread = nil
73   end
74
75   def do_poll
76     total_num = total_numi = 0
77     from_and_subj = []
78     from_and_subj_inbox = []
79
80     @mutex.synchronize do
81       SourceManager.usual_sources.each do |source|
82 #        yield "source #{source} is done? #{source.done?} (cur_offset #{source.cur_offset} >= #{source.end_offset})"
83         begin
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
88           next
89         end
90
91         num = 0
92         numi = 0
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
96           if old_m
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
105             else
106               yield "Skipping already-imported message at #{m.source_info}"
107             end
108           else
109             yield "Found new message at #{m.source_info} with labels #{m.labels.to_a * ','}"
110             add_new_message m
111             num += 1
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]
115               numi += 1
116             end
117           end
118           m
119         end
120         yield "Found #{num} messages, #{numi} to inbox." unless num == 0
121         total_num += num
122         total_numi += numi
123       end
124
125       yield "Done polling; loaded #{total_num} new messages total"
126       @last_poll = Time.now
127       @polling = false
128     end
129     [total_num, total_numi, from_and_subj, from_and_subj_inbox]
130   end
131
132   ## like Source#each, but yields successive Message objects, which have their
133   ## labels and offsets set correctly.
134   ##
135   ## this is the primary mechanism for iterating over messages from a source.
136   def each_message_from source, opts={}
137     begin
138       return if source.done? || source.has_errors?
139
140       source.each do |offset, source_labels|
141         if source.has_errors?
142           warn "error loading messages from #{source}: #{source.error.message}"
143           return
144         end
145
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 }
150
151         HookManager.run "before-add-message", :message => m
152         yield m
153       end
154     rescue SourceError => e
155       warn "problem getting messages from #{source}: #{e.message}"
156       Redwood::report_broken_sources :force_to_top => true
157     end
158   end
159
160   ## TODO: see if we can do this within PollMode rather than by calling this
161   ## method.
162   ##
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
166     Index.add_message m
167     UpdateManager.relay self, :added, m
168   end
169 end
170
171 end