- ## 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
+
+ def end_offset
+ unsynchronized_scan_mailbox
+ @ids.last + 1
+ end
+ synchronized :end_offset
+
+ def pct_done; 100.0 * (@ids.index(cur_offset) || 0).to_f / (@ids.length - 1).to_f; end
+
+private
+
+ def fetch ids, fields
+ results = safely { @imap.fetch ids, fields }
+ good_results =
+ if ids.respond_to? :member?
+ results.find_all { |r| ids.member?(r.seqno) && fields.all? { |f| r.attr.member?(f) } }
+ else
+ results.find_all { |r| ids == r.seqno && fields.all? { |f| r.attr.member?(f) } }
+ end
+
+ if good_results.empty?
+ raise FatalSourceError, "no IMAP response for #{ids} containing all fields #{fields.join(', ')} (got #{results.size} results)"
+ elsif good_results.size < results.size
+ Redwood::log "Your IMAP server sucks. It sent #{results.size} results for a request for #{good_results.size} messages. What are you using, Binc?"
+ end
+
+ good_results
+ end
+
+ def unsafe_connect
+ say "Connecting to IMAP server #{host}:#{port}..."
+
+ ## apparently imap.rb does a lot of threaded stuff internally and if
+ ## an exception occurs, it will catch it and re-raise it on the
+ ## calling thread. but i can't seem to catch that exception, so i've
+ ## resorted to initializing it in its own thread. surely there's a
+ ## better way.
+ exception = nil