]> git.cworth.org Git - sup/blob - lib/sup/poll.rb
fix (mostly!) the updates system
[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
23             num_inbox: the number of new messages appearing in the inbox (i.e.
24                        not auto-archived).
25         from_and_subj: an array of (from email address, subject) pairs
26   from_and_subj_inbox: an array of (from email address, subject) pairs for
27                        only those messages appearing in the inbox
28 EOS
29
30   DELAY = 300
31
32   def initialize
33     @mutex = Mutex.new
34     @thread = nil
35     @last_poll = nil
36     @polling = false
37     
38     self.class.i_am_the_instance self
39   end
40
41   def buffer
42     b, new = BufferManager.spawn_unless_exists("<poll for new messages>", :hidden => true) { PollMode.new }
43     b
44   end
45
46   def poll
47     return if @polling
48     @polling = true
49     HookManager.run "before-poll"
50
51     BufferManager.flash "Polling for new messages..."
52     num, numi, from_and_subj, from_and_subj_inbox = buffer.mode.poll
53     if num > 0
54       BufferManager.flash "Loaded #{num.pluralize 'new message'}, #{numi} to inbox." 
55     else
56       BufferManager.flash "No new messages." 
57     end
58
59     HookManager.run "after-poll", :num => num, :num_inbox => numi, :from_and_subj => from_and_subj, :from_and_subj_inbox => from_and_subj_inbox
60
61     @polling = false
62     [num, numi]
63   end
64
65   def start
66     @thread = Redwood::reporting_thread("periodic poll") do
67       while true
68         sleep DELAY / 2
69         poll if @last_poll.nil? || (Time.now - @last_poll) >= DELAY
70       end
71     end
72   end
73
74   def stop
75     @thread.kill if @thread
76     @thread = nil
77   end
78
79   def do_poll
80     total_num = total_numi = 0
81     from_and_subj = []
82     from_and_subj_inbox = []
83
84     @mutex.synchronize do
85       Index.usual_sources.each do |source|
86 #        yield "source #{source} is done? #{source.done?} (cur_offset #{source.cur_offset} >= #{source.end_offset})"
87         begin
88           yield "Loading from #{source}... " unless source.done? || source.has_errors?
89         rescue SourceError => e
90           Redwood::log "problem getting messages from #{source}: #{e.message}"
91           Redwood::report_broken_sources :force_to_top => true
92           next
93         end
94
95         num = 0
96         numi = 0
97         add_messages_from source do |m, offset, entry|
98           ## always preserve the labels on disk.
99           m.labels = entry[:label].split(/\s+/).map { |x| x.intern } if entry
100           yield "Found message at #{offset} with labels {#{m.labels * ', '}}"
101           unless entry
102             num += 1
103             from_and_subj << [m.from.longname, m.subj]
104             if m.has_label?(:inbox) && ([:spam, :deleted, :killed] & m.labels).empty?
105               from_and_subj_inbox << [m.from.longname, m.subj]
106               numi += 1 
107             end
108           end
109           m
110         end
111         yield "Found #{num} messages, #{numi} to inbox." unless num == 0
112         total_num += num
113         total_numi += numi
114       end
115
116       yield "Done polling; loaded #{total_num} new messages total"
117       @last_poll = Time.now
118       @polling = false
119     end
120     [total_num, total_numi, from_and_subj, from_and_subj_inbox]
121   end
122
123   ## this is the main mechanism for adding new messages to the
124   ## index. it's called both by sup-sync and by PollMode.
125   ##
126   ## for each message in the source, starting from the source's
127   ## starting offset, this methods yields the message, the source
128   ## offset, and the index entry on disk (if any). it expects the
129   ## yield to return the message (possibly altered in some way), and
130   ## then adds it (if new) or updates it (if previously seen).
131   ##
132   ## the labels of the yielded message are the default source
133   ## labels. it is likely that callers will want to replace these with
134   ## the index labels, if they exist, so that state is not lost when
135   ## e.g. a new version of a message from a mailing list comes in.
136   def add_messages_from source
137     begin
138       return if source.done? || source.has_errors?
139       
140       source.each do |offset, labels|
141         if source.has_errors?
142           Redwood::log "error loading messages from #{source}: #{source.error.message}"
143           return
144         end
145       
146         labels.each { |l| LabelManager << l }
147         labels = labels + (source.archived? ? [] : [:inbox])
148
149         begin
150           m = Message.new :source => source, :source_info => offset, :labels => labels
151           if m.source_marked_read?
152             m.remove_label :unread
153             labels.delete :unread
154           end
155
156           docid, entry = Index.load_entry_for_id m.id
157           HookManager.run "before-add-message", :message => m
158           m = yield(m, offset, entry) or next
159           Index.sync_message m, docid, entry
160           UpdateManager.relay self, :added, m unless entry
161         rescue MessageFormatError => e
162           Redwood::log "ignoring erroneous message at #{source}##{offset}: #{e.message}"
163         end
164       end
165     rescue SourceError => e
166       Redwood::log "problem getting messages from #{source}: #{e.message}"
167       Redwood::report_broken_sources :force_to_top => true
168     end
169   end
170 end
171
172 end