This is copyright Dan Stromberg despite having been partially written while
Dan was in the employ of Birchstreet Systems.  Birchstreet Systems gave an explicit OK
to keep code under my copyright.  Also, almost all of this was done after leaving Birchstreet.

Bugs:
	1) On large mailboxes done all at once, it's possible to get this traceback:
	Processed 14355 of 36681 (39.1%) messages (12305 found locally, 2050 newly retrieved from imap server)
	Traceback (most recent call last):
	  File "./subject-search", line 156, in <module>
		 typ, data = imap_connection.fetch(uid, '(BODY.PEEK[HEADER])')
	  File "/usr/lib/python2.6/imaplib.py", line 436, in fetch
		 typ, dat = self._simple_command(name, message_set, message_parts)
	  File "/usr/lib/python2.6/imaplib.py", line 1058, in _simple_command
		 return self._command_complete(name, self._command(name, *args))
	  File "/usr/lib/python2.6/imaplib.py", line 888, in _command_complete
		 typ, data = self._get_tagged_response(tag)
	  File "/usr/lib/python2.6/imaplib.py", line 989, in _get_tagged_response
		 self._get_response()
	  File "/usr/lib/python2.6/imaplib.py", line 906, in _get_response
		 resp = self._get_line()
	  File "/usr/lib/python2.6/imaplib.py", line 999, in _get_line
		 line = self.readline()
	  File "/usr/lib/python2.6/imaplib.py", line 1169, in readline
		 line.append(char)
	MemoryError
	make: *** [go] Error 1

	The code in question looks like - seems pretty innocuous, but perhaps there's a message in my inbox with a huge linelength:
		def readline(self):
			"""Read line from remote."""
			line = []
			while 1:
				char = self.sslobj.read(1)
				line.append(char)
				if char == "\n": return ''.join(line)

	I checked for imports in imaplib.py, but it doesn't seem to be importing anything unusual - except binascii apparently
		isn't pure python, and probably doesn't get used that much.  The thing is, I don't really think it should be using
		binascii for anything - though I can't rule it out.

	During the (a?) problem, the RES column in top goes up by about a megabyte per second.

	Also, an strace shows lots of this:
	read(3, "", 5)                          = 0
	read(3, "", 5)                          = 0
	read(3, "", 5)                          = 0
	read(3, "", 5)                          = 0
	read(3, "", 5)                          = 0
	read(3, "", 5)                          = 0
	read(3, "", 5)                          = 0
	read(3, "", 5)                          = 0
	read(3, "", 5)                          = 0
	read(3, "", 5)                          = 0
	read(3, "", 5)                          = 0
	read(3, "", 5)                          = 0

	It appears to be hitting EOF and ending up returning an infinite sequence of empty strings:
		 def read(self, len=1024):

			  """Read up to LEN bytes and return them.
			  Return zero-length string on EOF."""

			  try:
					return self._sslobj.read(len)
			  except SSLError, x:
					if x.args[0] == SSL_ERROR_EOF and self.suppress_ragged_eofs:
						 return ''
					else:
						 raise

	I suspect this is causing the bug.  It's in imaplib.py - the IMAP4_SSL readline:
		  def readline(self):
            """Read line from remote."""
            line = []
            while 1:
                char = self.sslobj.read(1)
                line.append(char)
                if char == "\n": return ''.join(line)