1996-11-28 03:52:01 +08:00
|
|
|
#! /usr/bin/env python
|
1996-09-18 05:33:15 +08:00
|
|
|
|
|
|
|
"""Classes to handle Unix style, MMDF style, and MH style mailboxes."""
|
|
|
|
|
|
|
|
|
1994-04-28 17:53:33 +08:00
|
|
|
import rfc822
|
1995-10-23 21:59:53 +08:00
|
|
|
import os
|
1994-04-28 17:53:33 +08:00
|
|
|
|
2002-03-02 06:39:14 +08:00
|
|
|
__all__ = ["UnixMailbox","MmdfMailbox","MHMailbox","Maildir","BabylMailbox",
|
|
|
|
"PortableUnixMailbox"]
|
2001-01-24 14:27:27 +08:00
|
|
|
|
1994-04-28 17:53:33 +08:00
|
|
|
class _Mailbox:
|
2002-09-12 13:08:00 +08:00
|
|
|
|
2001-02-01 06:13:15 +08:00
|
|
|
def __init__(self, fp, factory=rfc822.Message):
|
2000-07-10 00:44:26 +08:00
|
|
|
self.fp = fp
|
|
|
|
self.seekp = 0
|
2001-02-01 06:13:15 +08:00
|
|
|
self.factory = factory
|
2000-07-10 00:44:26 +08:00
|
|
|
|
2001-05-03 04:20:53 +08:00
|
|
|
def __iter__(self):
|
2001-09-13 09:29:13 +08:00
|
|
|
return iter(self.next, None)
|
2001-05-03 04:20:53 +08:00
|
|
|
|
2000-07-10 00:44:26 +08:00
|
|
|
def next(self):
|
|
|
|
while 1:
|
|
|
|
self.fp.seek(self.seekp)
|
|
|
|
try:
|
|
|
|
self._search_start()
|
|
|
|
except EOFError:
|
|
|
|
self.seekp = self.fp.tell()
|
|
|
|
return None
|
|
|
|
start = self.fp.tell()
|
|
|
|
self._search_end()
|
|
|
|
self.seekp = stop = self.fp.tell()
|
2000-12-13 07:20:45 +08:00
|
|
|
if start != stop:
|
2000-07-10 00:44:26 +08:00
|
|
|
break
|
2001-02-01 06:13:15 +08:00
|
|
|
return self.factory(_Subfile(self.fp, start, stop))
|
1998-03-27 04:56:10 +08:00
|
|
|
|
1994-04-28 17:53:33 +08:00
|
|
|
|
|
|
|
class _Subfile:
|
2002-09-12 13:08:00 +08:00
|
|
|
|
2000-07-10 00:44:26 +08:00
|
|
|
def __init__(self, fp, start, stop):
|
|
|
|
self.fp = fp
|
|
|
|
self.start = start
|
|
|
|
self.stop = stop
|
|
|
|
self.pos = self.start
|
|
|
|
|
|
|
|
def read(self, length = None):
|
|
|
|
if self.pos >= self.stop:
|
|
|
|
return ''
|
|
|
|
remaining = self.stop - self.pos
|
|
|
|
if length is None or length < 0:
|
|
|
|
length = remaining
|
|
|
|
elif length > remaining:
|
|
|
|
length = remaining
|
|
|
|
self.fp.seek(self.pos)
|
|
|
|
data = self.fp.read(length)
|
|
|
|
self.pos = self.fp.tell()
|
|
|
|
return data
|
|
|
|
|
|
|
|
def readline(self, length = None):
|
|
|
|
if self.pos >= self.stop:
|
|
|
|
return ''
|
|
|
|
if length is None:
|
|
|
|
length = self.stop - self.pos
|
|
|
|
self.fp.seek(self.pos)
|
|
|
|
data = self.fp.readline(length)
|
|
|
|
self.pos = self.fp.tell()
|
|
|
|
return data
|
|
|
|
|
|
|
|
def readlines(self, sizehint = -1):
|
|
|
|
lines = []
|
|
|
|
while 1:
|
|
|
|
line = self.readline()
|
|
|
|
if not line:
|
|
|
|
break
|
|
|
|
lines.append(line)
|
|
|
|
if sizehint >= 0:
|
|
|
|
sizehint = sizehint - len(line)
|
|
|
|
if sizehint <= 0:
|
|
|
|
break
|
|
|
|
return lines
|
|
|
|
|
|
|
|
def tell(self):
|
|
|
|
return self.pos - self.start
|
|
|
|
|
|
|
|
def seek(self, pos, whence=0):
|
|
|
|
if whence == 0:
|
|
|
|
self.pos = self.start + pos
|
|
|
|
elif whence == 1:
|
|
|
|
self.pos = self.pos + pos
|
|
|
|
elif whence == 2:
|
|
|
|
self.pos = self.stop + pos
|
|
|
|
|
|
|
|
def close(self):
|
|
|
|
del self.fp
|
1998-03-27 04:56:10 +08:00
|
|
|
|
1994-04-28 17:53:33 +08:00
|
|
|
|
2002-03-02 06:39:14 +08:00
|
|
|
# Recommended to use PortableUnixMailbox instead!
|
1994-04-28 17:53:33 +08:00
|
|
|
class UnixMailbox(_Mailbox):
|
2002-09-12 13:08:00 +08:00
|
|
|
|
2000-07-10 00:44:26 +08:00
|
|
|
def _search_start(self):
|
|
|
|
while 1:
|
|
|
|
pos = self.fp.tell()
|
|
|
|
line = self.fp.readline()
|
|
|
|
if not line:
|
|
|
|
raise EOFError
|
|
|
|
if line[:5] == 'From ' and self._isrealfromline(line):
|
|
|
|
self.fp.seek(pos)
|
|
|
|
return
|
|
|
|
|
|
|
|
def _search_end(self):
|
|
|
|
self.fp.readline() # Throw away header line
|
|
|
|
while 1:
|
|
|
|
pos = self.fp.tell()
|
|
|
|
line = self.fp.readline()
|
|
|
|
if not line:
|
|
|
|
return
|
|
|
|
if line[:5] == 'From ' and self._isrealfromline(line):
|
|
|
|
self.fp.seek(pos)
|
|
|
|
return
|
|
|
|
|
2001-02-01 06:13:15 +08:00
|
|
|
# An overridable mechanism to test for From-line-ness. You can either
|
|
|
|
# specify a different regular expression or define a whole new
|
|
|
|
# _isrealfromline() method. Note that this only gets called for lines
|
|
|
|
# starting with the 5 characters "From ".
|
|
|
|
#
|
|
|
|
# BAW: According to
|
|
|
|
#http://home.netscape.com/eng/mozilla/2.0/relnotes/demo/content-length.html
|
|
|
|
# the only portable, reliable way to find message delimiters in a BSD (i.e
|
|
|
|
# Unix mailbox) style folder is to search for "\n\nFrom .*\n", or at the
|
|
|
|
# beginning of the file, "^From .*\n". While _fromlinepattern below seems
|
|
|
|
# like a good idea, in practice, there are too many variations for more
|
|
|
|
# strict parsing of the line to be completely accurate.
|
|
|
|
#
|
|
|
|
# _strict_isrealfromline() is the old version which tries to do stricter
|
|
|
|
# parsing of the From_ line. _portable_isrealfromline() simply returns
|
|
|
|
# true, since it's never called if the line doesn't already start with
|
|
|
|
# "From ".
|
|
|
|
#
|
|
|
|
# This algorithm, and the way it interacts with _search_start() and
|
|
|
|
# _search_end() may not be completely correct, because it doesn't check
|
|
|
|
# that the two characters preceding "From " are \n\n or the beginning of
|
|
|
|
# the file. Fixing this would require a more extensive rewrite than is
|
2002-08-27 00:44:56 +08:00
|
|
|
# necessary. For convenience, we've added a PortableUnixMailbox class
|
|
|
|
# which uses the more lenient _fromlinepattern regular expression.
|
2000-07-10 00:44:26 +08:00
|
|
|
|
|
|
|
_fromlinepattern = r"From \s*[^\s]+\s+\w\w\w\s+\w\w\w\s+\d?\d\s+" \
|
|
|
|
r"\d?\d:\d\d(:\d\d)?(\s+[^\s]+)?\s+\d\d\d\d\s*$"
|
|
|
|
_regexp = None
|
|
|
|
|
2001-02-01 06:13:15 +08:00
|
|
|
def _strict_isrealfromline(self, line):
|
2000-07-10 00:44:26 +08:00
|
|
|
if not self._regexp:
|
|
|
|
import re
|
|
|
|
self._regexp = re.compile(self._fromlinepattern)
|
|
|
|
return self._regexp.match(line)
|
1998-03-27 04:56:10 +08:00
|
|
|
|
2001-02-01 06:13:15 +08:00
|
|
|
def _portable_isrealfromline(self, line):
|
2002-04-05 06:55:58 +08:00
|
|
|
return True
|
2001-02-01 06:13:15 +08:00
|
|
|
|
|
|
|
_isrealfromline = _strict_isrealfromline
|
|
|
|
|
|
|
|
|
|
|
|
class PortableUnixMailbox(UnixMailbox):
|
|
|
|
_isrealfromline = UnixMailbox._portable_isrealfromline
|
|
|
|
|
1998-04-04 00:04:05 +08:00
|
|
|
|
1994-04-28 17:53:33 +08:00
|
|
|
class MmdfMailbox(_Mailbox):
|
2002-09-12 13:08:00 +08:00
|
|
|
|
2000-07-10 00:44:26 +08:00
|
|
|
def _search_start(self):
|
|
|
|
while 1:
|
|
|
|
line = self.fp.readline()
|
|
|
|
if not line:
|
|
|
|
raise EOFError
|
|
|
|
if line[:5] == '\001\001\001\001\n':
|
|
|
|
return
|
|
|
|
|
|
|
|
def _search_end(self):
|
|
|
|
while 1:
|
|
|
|
pos = self.fp.tell()
|
|
|
|
line = self.fp.readline()
|
|
|
|
if not line:
|
|
|
|
return
|
|
|
|
if line == '\001\001\001\001\n':
|
|
|
|
self.fp.seek(pos)
|
|
|
|
return
|
1998-03-27 04:56:10 +08:00
|
|
|
|
1994-04-28 17:53:33 +08:00
|
|
|
|
1995-10-23 21:59:53 +08:00
|
|
|
class MHMailbox:
|
2002-09-12 13:08:00 +08:00
|
|
|
|
2001-02-01 06:13:15 +08:00
|
|
|
def __init__(self, dirname, factory=rfc822.Message):
|
2000-07-10 00:44:26 +08:00
|
|
|
import re
|
2000-08-10 11:05:26 +08:00
|
|
|
pat = re.compile('^[1-9][0-9]*$')
|
2000-07-10 00:44:26 +08:00
|
|
|
self.dirname = dirname
|
2000-08-11 15:48:36 +08:00
|
|
|
# the three following lines could be combined into:
|
|
|
|
# list = map(long, filter(pat.match, os.listdir(self.dirname)))
|
|
|
|
list = os.listdir(self.dirname)
|
|
|
|
list = filter(pat.match, list)
|
2000-08-10 11:05:26 +08:00
|
|
|
list = map(long, list)
|
|
|
|
list.sort()
|
|
|
|
# This only works in Python 1.6 or later;
|
|
|
|
# before that str() added 'L':
|
|
|
|
self.boxes = map(str, list)
|
2001-02-01 06:13:15 +08:00
|
|
|
self.factory = factory
|
2000-07-10 00:44:26 +08:00
|
|
|
|
2001-05-03 04:20:53 +08:00
|
|
|
def __iter__(self):
|
2001-09-13 09:29:13 +08:00
|
|
|
return iter(self.next, None)
|
2001-05-03 04:20:53 +08:00
|
|
|
|
2000-07-10 00:44:26 +08:00
|
|
|
def next(self):
|
|
|
|
if not self.boxes:
|
|
|
|
return None
|
2002-06-30 11:39:14 +08:00
|
|
|
fn = self.boxes.pop(0)
|
2000-07-10 00:44:26 +08:00
|
|
|
fp = open(os.path.join(self.dirname, fn))
|
2002-09-12 13:08:00 +08:00
|
|
|
msg = self.factory(fp)
|
|
|
|
try:
|
|
|
|
msg._mh_msgno = fn
|
|
|
|
except (AttributeError, TypeError):
|
|
|
|
pass
|
|
|
|
return msg
|
1998-03-27 04:56:10 +08:00
|
|
|
|
1998-12-24 06:05:42 +08:00
|
|
|
|
|
|
|
class Maildir:
|
2000-07-10 00:44:26 +08:00
|
|
|
# Qmail directory mailbox
|
|
|
|
|
2001-02-01 06:13:15 +08:00
|
|
|
def __init__(self, dirname, factory=rfc822.Message):
|
2000-07-10 00:44:26 +08:00
|
|
|
self.dirname = dirname
|
2001-02-01 06:13:15 +08:00
|
|
|
self.factory = factory
|
2000-07-10 00:44:26 +08:00
|
|
|
|
|
|
|
# check for new mail
|
|
|
|
newdir = os.path.join(self.dirname, 'new')
|
2000-09-23 02:41:50 +08:00
|
|
|
boxes = [os.path.join(newdir, f)
|
|
|
|
for f in os.listdir(newdir) if f[0] != '.']
|
2000-07-10 00:44:26 +08:00
|
|
|
|
|
|
|
# Now check for current mail in this maildir
|
|
|
|
curdir = os.path.join(self.dirname, 'cur')
|
2000-09-23 02:41:50 +08:00
|
|
|
boxes += [os.path.join(curdir, f)
|
|
|
|
for f in os.listdir(curdir) if f[0] != '.']
|
2000-07-10 00:44:26 +08:00
|
|
|
|
2000-10-23 21:37:01 +08:00
|
|
|
self.boxes = boxes
|
|
|
|
|
2001-05-03 04:20:53 +08:00
|
|
|
def __iter__(self):
|
2001-09-13 09:29:13 +08:00
|
|
|
return iter(self.next, None)
|
2001-05-03 04:20:53 +08:00
|
|
|
|
2000-07-10 00:44:26 +08:00
|
|
|
def next(self):
|
|
|
|
if not self.boxes:
|
|
|
|
return None
|
2002-06-30 11:39:14 +08:00
|
|
|
fn = self.boxes.pop(0)
|
2000-09-14 22:44:43 +08:00
|
|
|
fp = open(fn)
|
2001-02-01 06:13:15 +08:00
|
|
|
return self.factory(fp)
|
1998-12-24 06:05:42 +08:00
|
|
|
|
|
|
|
|
1997-05-15 22:33:09 +08:00
|
|
|
class BabylMailbox(_Mailbox):
|
2002-09-12 13:08:00 +08:00
|
|
|
|
2000-07-10 00:44:26 +08:00
|
|
|
def _search_start(self):
|
|
|
|
while 1:
|
|
|
|
line = self.fp.readline()
|
|
|
|
if not line:
|
|
|
|
raise EOFError
|
|
|
|
if line == '*** EOOH ***\n':
|
|
|
|
return
|
1998-03-27 04:56:10 +08:00
|
|
|
|
2000-07-10 00:44:26 +08:00
|
|
|
def _search_end(self):
|
|
|
|
while 1:
|
|
|
|
pos = self.fp.tell()
|
|
|
|
line = self.fp.readline()
|
|
|
|
if not line:
|
|
|
|
return
|
|
|
|
if line == '\037\014\n':
|
|
|
|
self.fp.seek(pos)
|
|
|
|
return
|
1997-05-15 22:33:09 +08:00
|
|
|
|
|
|
|
|
1996-09-18 05:33:15 +08:00
|
|
|
def _test():
|
2000-07-10 00:44:26 +08:00
|
|
|
import sys
|
|
|
|
|
|
|
|
args = sys.argv[1:]
|
|
|
|
if not args:
|
|
|
|
for key in 'MAILDIR', 'MAIL', 'LOGNAME', 'USER':
|
2002-06-01 22:18:47 +08:00
|
|
|
if key in os.environ:
|
2000-07-10 00:44:26 +08:00
|
|
|
mbox = os.environ[key]
|
|
|
|
break
|
2000-02-11 01:17:14 +08:00
|
|
|
else:
|
2000-07-10 00:44:26 +08:00
|
|
|
print "$MAIL, $LOGNAME nor $USER set -- who are you?"
|
|
|
|
return
|
|
|
|
else:
|
|
|
|
mbox = args[0]
|
|
|
|
if mbox[:1] == '+':
|
|
|
|
mbox = os.environ['HOME'] + '/Mail/' + mbox[1:]
|
|
|
|
elif not '/' in mbox:
|
2002-03-24 09:38:38 +08:00
|
|
|
if os.path.isfile('/var/mail/' + mbox):
|
|
|
|
mbox = '/var/mail/' + mbox
|
|
|
|
else:
|
|
|
|
mbox = '/usr/mail/' + mbox
|
2000-07-10 00:44:26 +08:00
|
|
|
if os.path.isdir(mbox):
|
|
|
|
if os.path.isdir(os.path.join(mbox, 'cur')):
|
|
|
|
mb = Maildir(mbox)
|
2000-02-11 01:17:14 +08:00
|
|
|
else:
|
2000-07-10 00:44:26 +08:00
|
|
|
mb = MHMailbox(mbox)
|
|
|
|
else:
|
|
|
|
fp = open(mbox, 'r')
|
2002-03-02 06:39:14 +08:00
|
|
|
mb = PortableUnixMailbox(fp)
|
2000-07-10 00:44:26 +08:00
|
|
|
|
|
|
|
msgs = []
|
|
|
|
while 1:
|
|
|
|
msg = mb.next()
|
|
|
|
if msg is None:
|
|
|
|
break
|
|
|
|
msgs.append(msg)
|
|
|
|
if len(args) <= 1:
|
|
|
|
msg.fp = None
|
|
|
|
if len(args) > 1:
|
2000-10-23 21:37:01 +08:00
|
|
|
num = int(args[1])
|
2000-07-10 00:44:26 +08:00
|
|
|
print 'Message %d body:'%num
|
|
|
|
msg = msgs[num-1]
|
|
|
|
msg.rewindbody()
|
|
|
|
sys.stdout.write(msg.fp.read())
|
|
|
|
else:
|
|
|
|
print 'Mailbox',mbox,'has',len(msgs),'messages:'
|
|
|
|
for msg in msgs:
|
|
|
|
f = msg.getheader('from') or ""
|
|
|
|
s = msg.getheader('subject') or ""
|
|
|
|
d = msg.getheader('date') or ""
|
2000-10-01 07:59:04 +08:00
|
|
|
print '-%20.20s %20.20s %-30.30s'%(f, d[5:], s)
|
1996-09-18 05:33:15 +08:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
2000-07-10 00:44:26 +08:00
|
|
|
_test()
|