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