- ## ok, this is FUCKING ANNOYING.
- ##
- ## what imap.rb likes to do is, if an exception occurs, catch it
- ## and re-raise it on the calling thread. seems reasonable. but
- ## what that REALLY means is that the only way to reasonably
- ## initialize imap is in its own thread, because otherwise, you
- ## will never be able to catch the exception it raises on the
- ## calling thread, and the backtrace will not make any sense at
- ## all, and you will waste HOURS of your life on this fucking
- ## problem.
- ##
- ## FUCK!!!!!!!!!
+ def scan_mailbox force=false
+ return if !force && @last_scan && (Time.now - @last_scan) < SCAN_INTERVAL
+ last_id = safely do
+ @imap.examine mailbox
+ @imap.responses["EXISTS"].last
+ end
+ @last_scan = Time.now
+
+ @ids = [] if force
+ return if last_id == @ids.length
+
+ range = (@ids.length + 1) .. last_id
+ Redwood::log "fetching IMAP headers #{range}"
+ fetch(range, ['RFC822.SIZE', 'INTERNALDATE', 'FLAGS']).each do |v|
+ id = make_id v
+ @ids << id
+ @imap_state[id] = { :id => v.seqno, :flags => v.attr["FLAGS"] }
+ end
+ Redwood::log "done fetching IMAP headers"
+ end
+ synchronized :scan_mailbox
+
+ def each
+ return unless start_offset
+
+ ids =
+ @mutex.synchronize do
+ unsynchronized_scan_mailbox
+ @ids
+ end
+
+ start = ids.index(cur_offset || start_offset) or raise OutOfSyncSourceError, "Unknown message id #{cur_offset || start_offset}."
+
+ start.upto(ids.length - 1) do |i|
+ id = ids[i]
+ state = @mutex.synchronize { @imap_state[id] } or next
+ self.cur_offset = id
+ labels = { :Flagged => :starred,
+ :Deleted => :deleted
+ }.inject(@labels) do |cur, (imap, sup)|
+ cur + (state[:flags].include?(imap) ? [sup] : [])
+ end
+
+ labels += [:unread] unless state[:flags].include?(:Seen)
+
+ yield id, labels
+ end
+ end
+
+ def start_offset
+ unsynchronized_scan_mailbox
+ @ids.first
+ end
+ synchronized :start_offset