blob: dbb2724b22a7d6483196840e101c2986dd9f4e5f [file] [log] [blame]
Guido van Rossumf06ee5f1996-11-27 19:52:01 +00001#! /usr/bin/env python
Guido van Rossum62448671996-09-17 21:33:15 +00002
3"""Classes to handle Unix style, MMDF style, and MH style mailboxes."""
4
5
Guido van Rossumc7b68821994-04-28 09:53:33 +00006import rfc822
Jack Jansen97157791995-10-23 13:59:53 +00007import os
Guido van Rossumc7b68821994-04-28 09:53:33 +00008
Barry Warsawffd05ee2002-03-01 22:39:14 +00009__all__ = ["UnixMailbox","MmdfMailbox","MHMailbox","Maildir","BabylMailbox",
10 "PortableUnixMailbox"]
Skip Montanaro17ab1232001-01-24 06:27:27 +000011
Guido van Rossumc7b68821994-04-28 09:53:33 +000012class _Mailbox:
Guido van Rossum4bf12542002-09-12 05:08:00 +000013
Barry Warsaw81ad67c2001-01-31 22:13:15 +000014 def __init__(self, fp, factory=rfc822.Message):
Fred Drakedbbf76b2000-07-09 16:44:26 +000015 self.fp = fp
16 self.seekp = 0
Barry Warsaw81ad67c2001-01-31 22:13:15 +000017 self.factory = factory
Guido van Rossum8ca84201998-03-26 20:56:10 +000018
Fred Drake72987a42001-05-02 20:20:53 +000019 def __iter__(self):
Guido van Rossum93a696f2001-09-13 01:29:13 +000020 return iter(self.next, None)
Fred Drake72987a42001-05-02 20:20:53 +000021
Fred Drakedbbf76b2000-07-09 16:44:26 +000022 def next(self):
23 while 1:
24 self.fp.seek(self.seekp)
25 try:
26 self._search_start()
27 except EOFError:
28 self.seekp = self.fp.tell()
29 return None
30 start = self.fp.tell()
31 self._search_end()
32 self.seekp = stop = self.fp.tell()
Fred Drake8152d322000-12-12 23:20:45 +000033 if start != stop:
Fred Drakedbbf76b2000-07-09 16:44:26 +000034 break
Barry Warsaw81ad67c2001-01-31 22:13:15 +000035 return self.factory(_Subfile(self.fp, start, stop))
Guido van Rossumc7b68821994-04-28 09:53:33 +000036
Guido van Rossumc7b68821994-04-28 09:53:33 +000037
38class _Subfile:
Guido van Rossum4bf12542002-09-12 05:08:00 +000039
Fred Drakedbbf76b2000-07-09 16:44:26 +000040 def __init__(self, fp, start, stop):
41 self.fp = fp
42 self.start = start
43 self.stop = stop
44 self.pos = self.start
Guido van Rossum8ca84201998-03-26 20:56:10 +000045
Andrew M. Kuchling1263bd82004-07-07 14:09:21 +000046
47 def _read(self, length, read_function):
Fred Drakedbbf76b2000-07-09 16:44:26 +000048 if self.pos >= self.stop:
49 return ''
50 remaining = self.stop - self.pos
Andrew M. Kuchling1263bd82004-07-07 14:09:21 +000051 if length is None or length < 0 or length > remaining:
Fred Drakedbbf76b2000-07-09 16:44:26 +000052 length = remaining
53 self.fp.seek(self.pos)
Andrew M. Kuchling1263bd82004-07-07 14:09:21 +000054 data = read_function(length)
Fred Drakedbbf76b2000-07-09 16:44:26 +000055 self.pos = self.fp.tell()
56 return data
Guido van Rossumc7b68821994-04-28 09:53:33 +000057
Andrew M. Kuchling1263bd82004-07-07 14:09:21 +000058 def read(self, length = None):
59 self._read(length, self.fp.read)
60
Fred Drakedbbf76b2000-07-09 16:44:26 +000061 def readline(self, length = None):
Andrew M. Kuchling1263bd82004-07-07 14:09:21 +000062 self._read(length, self.fp.readline)
Guido van Rossumc7b68821994-04-28 09:53:33 +000063
Fred Drakedbbf76b2000-07-09 16:44:26 +000064 def readlines(self, sizehint = -1):
65 lines = []
66 while 1:
67 line = self.readline()
68 if not line:
69 break
70 lines.append(line)
71 if sizehint >= 0:
72 sizehint = sizehint - len(line)
73 if sizehint <= 0:
74 break
75 return lines
Guido van Rossumc7b68821994-04-28 09:53:33 +000076
Fred Drakedbbf76b2000-07-09 16:44:26 +000077 def tell(self):
78 return self.pos - self.start
Guido van Rossume256a0f1999-03-24 16:20:45 +000079
Fred Drakedbbf76b2000-07-09 16:44:26 +000080 def seek(self, pos, whence=0):
81 if whence == 0:
82 self.pos = self.start + pos
83 elif whence == 1:
84 self.pos = self.pos + pos
85 elif whence == 2:
86 self.pos = self.stop + pos
Guido van Rossumc7b68821994-04-28 09:53:33 +000087
Fred Drakedbbf76b2000-07-09 16:44:26 +000088 def close(self):
89 del self.fp
Guido van Rossumc7b68821994-04-28 09:53:33 +000090
Guido van Rossumc7b68821994-04-28 09:53:33 +000091
Barry Warsawffd05ee2002-03-01 22:39:14 +000092# Recommended to use PortableUnixMailbox instead!
Guido van Rossumc7b68821994-04-28 09:53:33 +000093class UnixMailbox(_Mailbox):
Guido van Rossum4bf12542002-09-12 05:08:00 +000094
Fred Drakedbbf76b2000-07-09 16:44:26 +000095 def _search_start(self):
96 while 1:
97 pos = self.fp.tell()
98 line = self.fp.readline()
99 if not line:
100 raise EOFError
101 if line[:5] == 'From ' and self._isrealfromline(line):
102 self.fp.seek(pos)
103 return
Guido van Rossum8ca84201998-03-26 20:56:10 +0000104
Fred Drakedbbf76b2000-07-09 16:44:26 +0000105 def _search_end(self):
106 self.fp.readline() # Throw away header line
107 while 1:
108 pos = self.fp.tell()
109 line = self.fp.readline()
110 if not line:
111 return
112 if line[:5] == 'From ' and self._isrealfromline(line):
113 self.fp.seek(pos)
114 return
Guido van Rossumc7b68821994-04-28 09:53:33 +0000115
Barry Warsaw81ad67c2001-01-31 22:13:15 +0000116 # An overridable mechanism to test for From-line-ness. You can either
117 # specify a different regular expression or define a whole new
118 # _isrealfromline() method. Note that this only gets called for lines
119 # starting with the 5 characters "From ".
120 #
121 # BAW: According to
122 #http://home.netscape.com/eng/mozilla/2.0/relnotes/demo/content-length.html
123 # the only portable, reliable way to find message delimiters in a BSD (i.e
124 # Unix mailbox) style folder is to search for "\n\nFrom .*\n", or at the
125 # beginning of the file, "^From .*\n". While _fromlinepattern below seems
126 # like a good idea, in practice, there are too many variations for more
127 # strict parsing of the line to be completely accurate.
128 #
129 # _strict_isrealfromline() is the old version which tries to do stricter
130 # parsing of the From_ line. _portable_isrealfromline() simply returns
131 # true, since it's never called if the line doesn't already start with
132 # "From ".
133 #
134 # This algorithm, and the way it interacts with _search_start() and
135 # _search_end() may not be completely correct, because it doesn't check
136 # that the two characters preceding "From " are \n\n or the beginning of
137 # the file. Fixing this would require a more extensive rewrite than is
Barry Warsawda5628f2002-08-26 16:44:56 +0000138 # necessary. For convenience, we've added a PortableUnixMailbox class
139 # which uses the more lenient _fromlinepattern regular expression.
Guido van Rossumc7b68821994-04-28 09:53:33 +0000140
Fred Drakedbbf76b2000-07-09 16:44:26 +0000141 _fromlinepattern = r"From \s*[^\s]+\s+\w\w\w\s+\w\w\w\s+\d?\d\s+" \
142 r"\d?\d:\d\d(:\d\d)?(\s+[^\s]+)?\s+\d\d\d\d\s*$"
143 _regexp = None
Guido van Rossumfbe63de1998-04-03 16:04:05 +0000144
Barry Warsaw81ad67c2001-01-31 22:13:15 +0000145 def _strict_isrealfromline(self, line):
Fred Drakedbbf76b2000-07-09 16:44:26 +0000146 if not self._regexp:
147 import re
148 self._regexp = re.compile(self._fromlinepattern)
149 return self._regexp.match(line)
Guido van Rossumfbe63de1998-04-03 16:04:05 +0000150
Barry Warsaw81ad67c2001-01-31 22:13:15 +0000151 def _portable_isrealfromline(self, line):
Tim Petersbc0e9102002-04-04 22:55:58 +0000152 return True
Barry Warsaw81ad67c2001-01-31 22:13:15 +0000153
154 _isrealfromline = _strict_isrealfromline
155
156
157class PortableUnixMailbox(UnixMailbox):
158 _isrealfromline = UnixMailbox._portable_isrealfromline
159
Guido van Rossumfbe63de1998-04-03 16:04:05 +0000160
Guido van Rossumc7b68821994-04-28 09:53:33 +0000161class MmdfMailbox(_Mailbox):
Guido van Rossum4bf12542002-09-12 05:08:00 +0000162
Fred Drakedbbf76b2000-07-09 16:44:26 +0000163 def _search_start(self):
164 while 1:
165 line = self.fp.readline()
166 if not line:
167 raise EOFError
168 if line[:5] == '\001\001\001\001\n':
169 return
Guido van Rossum8ca84201998-03-26 20:56:10 +0000170
Fred Drakedbbf76b2000-07-09 16:44:26 +0000171 def _search_end(self):
172 while 1:
173 pos = self.fp.tell()
174 line = self.fp.readline()
175 if not line:
176 return
177 if line == '\001\001\001\001\n':
178 self.fp.seek(pos)
179 return
Guido van Rossumc7b68821994-04-28 09:53:33 +0000180
Guido van Rossumc7b68821994-04-28 09:53:33 +0000181
Jack Jansen97157791995-10-23 13:59:53 +0000182class MHMailbox:
Guido van Rossum4bf12542002-09-12 05:08:00 +0000183
Barry Warsaw81ad67c2001-01-31 22:13:15 +0000184 def __init__(self, dirname, factory=rfc822.Message):
Fred Drakedbbf76b2000-07-09 16:44:26 +0000185 import re
Guido van Rossum0707fea2000-08-10 03:05:26 +0000186 pat = re.compile('^[1-9][0-9]*$')
Fred Drakedbbf76b2000-07-09 16:44:26 +0000187 self.dirname = dirname
Sjoerd Mullenderd2653a92000-08-11 07:48:36 +0000188 # the three following lines could be combined into:
189 # list = map(long, filter(pat.match, os.listdir(self.dirname)))
190 list = os.listdir(self.dirname)
191 list = filter(pat.match, list)
Guido van Rossum0707fea2000-08-10 03:05:26 +0000192 list = map(long, list)
193 list.sort()
194 # This only works in Python 1.6 or later;
195 # before that str() added 'L':
196 self.boxes = map(str, list)
Raymond Hettingerb5ba8d72004-02-07 02:16:24 +0000197 self.boxes.reverse()
Barry Warsaw81ad67c2001-01-31 22:13:15 +0000198 self.factory = factory
Jack Jansen97157791995-10-23 13:59:53 +0000199
Fred Drake72987a42001-05-02 20:20:53 +0000200 def __iter__(self):
Guido van Rossum93a696f2001-09-13 01:29:13 +0000201 return iter(self.next, None)
Fred Drake72987a42001-05-02 20:20:53 +0000202
Fred Drakedbbf76b2000-07-09 16:44:26 +0000203 def next(self):
204 if not self.boxes:
205 return None
Raymond Hettingerb5ba8d72004-02-07 02:16:24 +0000206 fn = self.boxes.pop()
Fred Drakedbbf76b2000-07-09 16:44:26 +0000207 fp = open(os.path.join(self.dirname, fn))
Guido van Rossum4bf12542002-09-12 05:08:00 +0000208 msg = self.factory(fp)
209 try:
210 msg._mh_msgno = fn
211 except (AttributeError, TypeError):
212 pass
213 return msg
Guido van Rossum8ca84201998-03-26 20:56:10 +0000214
Guido van Rossum9a4d6371998-12-23 22:05:42 +0000215
216class Maildir:
Fred Drakedbbf76b2000-07-09 16:44:26 +0000217 # Qmail directory mailbox
Guido van Rossum9a4d6371998-12-23 22:05:42 +0000218
Barry Warsaw81ad67c2001-01-31 22:13:15 +0000219 def __init__(self, dirname, factory=rfc822.Message):
Fred Drakedbbf76b2000-07-09 16:44:26 +0000220 self.dirname = dirname
Barry Warsaw81ad67c2001-01-31 22:13:15 +0000221 self.factory = factory
Guido van Rossum9a4d6371998-12-23 22:05:42 +0000222
Fred Drakedbbf76b2000-07-09 16:44:26 +0000223 # check for new mail
224 newdir = os.path.join(self.dirname, 'new')
Fred Draked9a8dec2000-09-22 18:41:50 +0000225 boxes = [os.path.join(newdir, f)
226 for f in os.listdir(newdir) if f[0] != '.']
Guido van Rossum9a4d6371998-12-23 22:05:42 +0000227
Fred Drakedbbf76b2000-07-09 16:44:26 +0000228 # Now check for current mail in this maildir
229 curdir = os.path.join(self.dirname, 'cur')
Fred Draked9a8dec2000-09-22 18:41:50 +0000230 boxes += [os.path.join(curdir, f)
231 for f in os.listdir(curdir) if f[0] != '.']
Raymond Hettingerb5ba8d72004-02-07 02:16:24 +0000232 boxes.reverse()
Fred Drakede3518e2000-10-23 13:37:01 +0000233 self.boxes = boxes
234
Fred Drake72987a42001-05-02 20:20:53 +0000235 def __iter__(self):
Guido van Rossum93a696f2001-09-13 01:29:13 +0000236 return iter(self.next, None)
Fred Drake72987a42001-05-02 20:20:53 +0000237
Fred Drakedbbf76b2000-07-09 16:44:26 +0000238 def next(self):
239 if not self.boxes:
240 return None
Raymond Hettingerb5ba8d72004-02-07 02:16:24 +0000241 fn = self.boxes.pop()
Fred Drakee108a022000-09-14 14:44:43 +0000242 fp = open(fn)
Barry Warsaw81ad67c2001-01-31 22:13:15 +0000243 return self.factory(fp)
Guido van Rossum9a4d6371998-12-23 22:05:42 +0000244
Guido van Rossum9a4d6371998-12-23 22:05:42 +0000245
Guido van Rossumfdf58fe1997-05-15 14:33:09 +0000246class BabylMailbox(_Mailbox):
Guido van Rossum4bf12542002-09-12 05:08:00 +0000247
Fred Drakedbbf76b2000-07-09 16:44:26 +0000248 def _search_start(self):
249 while 1:
250 line = self.fp.readline()
251 if not line:
252 raise EOFError
253 if line == '*** EOOH ***\n':
254 return
Guido van Rossumfdf58fe1997-05-15 14:33:09 +0000255
Fred Drakedbbf76b2000-07-09 16:44:26 +0000256 def _search_end(self):
257 while 1:
258 pos = self.fp.tell()
259 line = self.fp.readline()
260 if not line:
261 return
262 if line == '\037\014\n':
263 self.fp.seek(pos)
264 return
Guido van Rossumfdf58fe1997-05-15 14:33:09 +0000265
266
Guido van Rossum62448671996-09-17 21:33:15 +0000267def _test():
Fred Drakedbbf76b2000-07-09 16:44:26 +0000268 import sys
Jack Jansen97157791995-10-23 13:59:53 +0000269
Fred Drakedbbf76b2000-07-09 16:44:26 +0000270 args = sys.argv[1:]
271 if not args:
272 for key in 'MAILDIR', 'MAIL', 'LOGNAME', 'USER':
Raymond Hettinger54f02222002-06-01 14:18:47 +0000273 if key in os.environ:
Fred Drakedbbf76b2000-07-09 16:44:26 +0000274 mbox = os.environ[key]
275 break
Fred Drake13a2c272000-02-10 17:17:14 +0000276 else:
Fred Drakedbbf76b2000-07-09 16:44:26 +0000277 print "$MAIL, $LOGNAME nor $USER set -- who are you?"
278 return
279 else:
280 mbox = args[0]
281 if mbox[:1] == '+':
282 mbox = os.environ['HOME'] + '/Mail/' + mbox[1:]
283 elif not '/' in mbox:
Neil Schemenauer03f3ee62002-03-24 01:38:38 +0000284 if os.path.isfile('/var/mail/' + mbox):
285 mbox = '/var/mail/' + mbox
286 else:
287 mbox = '/usr/mail/' + mbox
Fred Drakedbbf76b2000-07-09 16:44:26 +0000288 if os.path.isdir(mbox):
289 if os.path.isdir(os.path.join(mbox, 'cur')):
290 mb = Maildir(mbox)
Fred Drake13a2c272000-02-10 17:17:14 +0000291 else:
Fred Drakedbbf76b2000-07-09 16:44:26 +0000292 mb = MHMailbox(mbox)
293 else:
294 fp = open(mbox, 'r')
Barry Warsawffd05ee2002-03-01 22:39:14 +0000295 mb = PortableUnixMailbox(fp)
Fred Drakedbbf76b2000-07-09 16:44:26 +0000296
297 msgs = []
298 while 1:
299 msg = mb.next()
300 if msg is None:
301 break
302 msgs.append(msg)
303 if len(args) <= 1:
304 msg.fp = None
305 if len(args) > 1:
Fred Drakede3518e2000-10-23 13:37:01 +0000306 num = int(args[1])
Fred Drakedbbf76b2000-07-09 16:44:26 +0000307 print 'Message %d body:'%num
308 msg = msgs[num-1]
309 msg.rewindbody()
310 sys.stdout.write(msg.fp.read())
311 else:
312 print 'Mailbox',mbox,'has',len(msgs),'messages:'
313 for msg in msgs:
314 f = msg.getheader('from') or ""
315 s = msg.getheader('subject') or ""
316 d = msg.getheader('date') or ""
Fred Drakecc4adf22000-09-30 23:59:04 +0000317 print '-%20.20s %20.20s %-30.30s'%(f, d[5:], s)
Guido van Rossum62448671996-09-17 21:33:15 +0000318
319
320if __name__ == '__main__':
Fred Drakedbbf76b2000-07-09 16:44:26 +0000321 _test()