+ def shutup
+ BufferManager.clear @say_id if BufferManager.instantiated?
+ @say_id = nil
+ end
+
+ def make_id imap_stuff
+ # use 7 digits for the size. why 7? seems nice.
+ %w(RFC822.SIZE INTERNALDATE).each do |w|
+ raise FatalSourceError, "requested data not in IMAP response: #{w}" unless imap_stuff.attr[w]
+ end
+
+ msize, mdate = imap_stuff.attr['RFC822.SIZE'] % 10000000, Time.parse(imap_stuff.attr["INTERNALDATE"])
+ sprintf("%d%07d", mdate.to_i, msize).to_i
+ end
+
+ def get_imap_fields id, *fields
+ raise OutOfSyncSourceError, "Unknown message id #{id}" unless @imap_state[id]
+
+ imap_id = @imap_state[id][:id]
+ result = fetch(imap_id, (fields + ['RFC822.SIZE', 'INTERNALDATE']).uniq).first
+ got_id = make_id result
+
+ ## I've turned off the following sanity check because Microsoft
+ ## Exchange fails it. Exchange actually reports two different
+ ## INTERNALDATEs for the exact same message when queried at different
+ ## points in time.
+ ##
+ ## RFC2060 defines the semantics of INTERNALDATE for messages that
+ ## arrive via SMTP for via various IMAP commands, but states that
+ ## "All other cases are implementation defined.". Great, thanks guys,
+ ## yet another useless field.
+ ##
+ ## Of course no OTHER imap server I've encountered returns DIFFERENT
+ ## values for the SAME message. But it's Microsoft; what do you
+ ## expect? If their programmers were any good they'd be working at
+ ## Google.
+
+ # raise OutOfSyncSourceError, "IMAP message mismatch: requested #{id}, got #{got_id}." unless got_id == id
+
+ fields.map { |f| result.attr[f] or raise FatalSourceError, "empty response from IMAP server: #{f}" }
+ end
+
+ ## execute a block, connected if unconnected, re-connected up to 3
+ ## times if a recoverable error occurs, and properly dying if an
+ ## unrecoverable error occurs.
+ def safely
+ retries = 0
+ begin
+ begin
+ unsafe_connect unless @imap
+ yield
+ rescue *RECOVERABLE_ERRORS => e
+ if (retries += 1) <= 3
+ @imap = nil
+ warn "got #{e.class.name}: #{e.message.inspect}"
+ sleep 2
+ retry
+ end
+ raise
+ end
+ rescue SocketError, Net::IMAP::Error, SystemCallError, IOError, OpenSSL::SSL::SSLError => e
+ raise FatalSourceError, "While communicating with IMAP server (type #{e.class.name}): #{e.message.inspect}"
+ end
+ end
+
+end