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