3 """Classes to handle Unix style, MMDF style, and MH style mailboxes."""
9 __all__ = [
"UnixMailbox",
"MmdfMailbox",
"MHMailbox",
"Maildir",
"BabylMailbox",
10 "PortableUnixMailbox"]
13 def __init__(self, fp, factory=rfc822.Message):
19 return iter(self.
next,
None)
23 self.fp.seek(self.
seekp)
27 self.
seekp = self.fp.tell()
29 start = self.fp.tell()
31 self.
seekp = stop = self.fp.tell()
44 def read(self, length = None):
47 remaining = self.
stop - self.
pos
48 if length
is None or length < 0:
50 elif length > remaining:
52 self.fp.seek(self.
pos)
53 data = self.fp.read(length)
54 self.
pos = self.fp.tell()
62 self.fp.seek(self.
pos)
63 data = self.fp.readline(length)
64 self.
pos = self.fp.tell()
75 sizehint = sizehint - len(line)
83 def seek(self, pos, whence=0):
97 def _search_start(self):
100 line = self.fp.readline()
107 def _search_end(self):
111 line = self.fp.readline()
143 _fromlinepattern =
r"From \s*[^\s]+\s+\w\w\w\s+\w\w\w\s+\d?\d\s+" \
144 r"\d?\d:\d\d(:\d\d)?(\s+[^\s]+)?\s+\d\d\d\d\s*$"
147 def _strict_isrealfromline(self, line):
151 return self._regexp.match(line)
153 def _portable_isrealfromline(self, line):
156 _isrealfromline = _strict_isrealfromline
160 _isrealfromline = UnixMailbox._portable_isrealfromline
164 def _search_start(self):
166 line = self.fp.readline()
169 if line[:5] ==
'\001\001\001\001\n':
172 def _search_end(self):
175 line = self.fp.readline()
178 if line ==
'\001\001\001\001\n':
184 def __init__(self, dirname, factory=rfc822.Message):
186 pat = re.compile(
'^[1-9][0-9]*$')
190 list = os.listdir(self.
dirname)
191 list =
filter(pat.match, list)
192 list = map(long, list)
200 return iter(self.
next,
None)
214 def __init__(self, dirname, factory=rfc822.Message):
219 newdir = os.path.join(self.
dirname,
'new')
220 boxes = [os.path.join(newdir, f)
221 for f
in os.listdir(newdir)
if f[0] !=
'.']
224 curdir = os.path.join(self.
dirname,
'cur')
225 boxes += [os.path.join(curdir, f)
226 for f
in os.listdir(curdir)
if f[0] !=
'.']
231 return iter(self.
next,
None)
243 def _search_start(self):
245 line = self.fp.readline()
248 if line ==
'*** EOOH ***\n':
251 def _search_end(self):
254 line = self.fp.readline()
257 if line ==
'\037\014\n':
267 for key
in 'MAILDIR',
'MAIL',
'LOGNAME',
'USER':
268 if os.environ.has_key(key):
269 mbox = os.environ[key]
272 print "$MAIL, $LOGNAME nor $USER set -- who are you?"
277 mbox = os.environ[
'HOME'] +
'/Mail/' + mbox[1:]
278 elif not '/' in mbox:
279 mbox =
'/usr/mail/' + mbox
280 if os.path.isdir(mbox):
281 if os.path.isdir(os.path.join(mbox,
'cur')):
299 print 'Message %d body:'%num
302 sys.stdout.write(msg.fp.read())
304 print 'Mailbox',mbox,
'has',len(msgs),
'messages:'
306 f = msg.getheader(
'from')
or ""
307 s = msg.getheader(
'subject')
or ""
308 d = msg.getheader(
'date')
or ""
309 print '-%20.20s %20.20s %-30.30s'%(f, d[5:], s)
312 if __name__ ==
'__main__':