Guido van Rossum | f06ee5f | 1996-11-27 19:52:01 +0000 | [diff] [blame] | 1 | #! /usr/bin/env python |
Guido van Rossum | 6244867 | 1996-09-17 21:33:15 +0000 | [diff] [blame] | 2 | |
| 3 | """Classes to handle Unix style, MMDF style, and MH style mailboxes.""" |
| 4 | |
| 5 | |
Guido van Rossum | c7b6882 | 1994-04-28 09:53:33 +0000 | [diff] [blame] | 6 | import rfc822 |
Jack Jansen | 9715779 | 1995-10-23 13:59:53 +0000 | [diff] [blame] | 7 | import os |
Guido van Rossum | c7b6882 | 1994-04-28 09:53:33 +0000 | [diff] [blame] | 8 | |
Skip Montanaro | 17ab123 | 2001-01-24 06:27:27 +0000 | [diff] [blame] | 9 | __all__ = ["UnixMailbox","MmdfMailbox","MHMailbox","Maildir","BabylMailbox"] |
| 10 | |
Guido van Rossum | c7b6882 | 1994-04-28 09:53:33 +0000 | [diff] [blame] | 11 | class _Mailbox: |
Barry Warsaw | 81ad67c | 2001-01-31 22:13:15 +0000 | [diff] [blame] | 12 | def __init__(self, fp, factory=rfc822.Message): |
Fred Drake | dbbf76b | 2000-07-09 16:44:26 +0000 | [diff] [blame] | 13 | self.fp = fp |
| 14 | self.seekp = 0 |
Barry Warsaw | 81ad67c | 2001-01-31 22:13:15 +0000 | [diff] [blame] | 15 | self.factory = factory |
Guido van Rossum | 8ca8420 | 1998-03-26 20:56:10 +0000 | [diff] [blame] | 16 | |
Fred Drake | 72987a4 | 2001-05-02 20:20:53 +0000 | [diff] [blame] | 17 | def __iter__(self): |
| 18 | return self |
| 19 | |
Fred Drake | dbbf76b | 2000-07-09 16:44:26 +0000 | [diff] [blame] | 20 | def next(self): |
| 21 | while 1: |
| 22 | self.fp.seek(self.seekp) |
| 23 | try: |
| 24 | self._search_start() |
| 25 | except EOFError: |
| 26 | self.seekp = self.fp.tell() |
| 27 | return None |
| 28 | start = self.fp.tell() |
| 29 | self._search_end() |
| 30 | self.seekp = stop = self.fp.tell() |
Fred Drake | 8152d32 | 2000-12-12 23:20:45 +0000 | [diff] [blame] | 31 | if start != stop: |
Fred Drake | dbbf76b | 2000-07-09 16:44:26 +0000 | [diff] [blame] | 32 | break |
Barry Warsaw | 81ad67c | 2001-01-31 22:13:15 +0000 | [diff] [blame] | 33 | return self.factory(_Subfile(self.fp, start, stop)) |
Guido van Rossum | c7b6882 | 1994-04-28 09:53:33 +0000 | [diff] [blame] | 34 | |
Guido van Rossum | c7b6882 | 1994-04-28 09:53:33 +0000 | [diff] [blame] | 35 | |
| 36 | class _Subfile: |
Fred Drake | dbbf76b | 2000-07-09 16:44:26 +0000 | [diff] [blame] | 37 | def __init__(self, fp, start, stop): |
| 38 | self.fp = fp |
| 39 | self.start = start |
| 40 | self.stop = stop |
| 41 | self.pos = self.start |
Guido van Rossum | 8ca8420 | 1998-03-26 20:56:10 +0000 | [diff] [blame] | 42 | |
Fred Drake | dbbf76b | 2000-07-09 16:44:26 +0000 | [diff] [blame] | 43 | def read(self, length = None): |
| 44 | if self.pos >= self.stop: |
| 45 | return '' |
| 46 | remaining = self.stop - self.pos |
| 47 | if length is None or length < 0: |
| 48 | length = remaining |
| 49 | elif length > remaining: |
| 50 | length = remaining |
| 51 | self.fp.seek(self.pos) |
| 52 | data = self.fp.read(length) |
| 53 | self.pos = self.fp.tell() |
| 54 | return data |
Guido van Rossum | c7b6882 | 1994-04-28 09:53:33 +0000 | [diff] [blame] | 55 | |
Fred Drake | dbbf76b | 2000-07-09 16:44:26 +0000 | [diff] [blame] | 56 | def readline(self, length = None): |
| 57 | if self.pos >= self.stop: |
| 58 | return '' |
| 59 | if length is None: |
| 60 | length = self.stop - self.pos |
| 61 | self.fp.seek(self.pos) |
| 62 | data = self.fp.readline(length) |
| 63 | self.pos = self.fp.tell() |
| 64 | return data |
Guido van Rossum | c7b6882 | 1994-04-28 09:53:33 +0000 | [diff] [blame] | 65 | |
Fred Drake | dbbf76b | 2000-07-09 16:44:26 +0000 | [diff] [blame] | 66 | def readlines(self, sizehint = -1): |
| 67 | lines = [] |
| 68 | while 1: |
| 69 | line = self.readline() |
| 70 | if not line: |
| 71 | break |
| 72 | lines.append(line) |
| 73 | if sizehint >= 0: |
| 74 | sizehint = sizehint - len(line) |
| 75 | if sizehint <= 0: |
| 76 | break |
| 77 | return lines |
Guido van Rossum | c7b6882 | 1994-04-28 09:53:33 +0000 | [diff] [blame] | 78 | |
Fred Drake | dbbf76b | 2000-07-09 16:44:26 +0000 | [diff] [blame] | 79 | def tell(self): |
| 80 | return self.pos - self.start |
Guido van Rossum | e256a0f | 1999-03-24 16:20:45 +0000 | [diff] [blame] | 81 | |
Fred Drake | dbbf76b | 2000-07-09 16:44:26 +0000 | [diff] [blame] | 82 | def seek(self, pos, whence=0): |
| 83 | if whence == 0: |
| 84 | self.pos = self.start + pos |
| 85 | elif whence == 1: |
| 86 | self.pos = self.pos + pos |
| 87 | elif whence == 2: |
| 88 | self.pos = self.stop + pos |
Guido van Rossum | c7b6882 | 1994-04-28 09:53:33 +0000 | [diff] [blame] | 89 | |
Fred Drake | dbbf76b | 2000-07-09 16:44:26 +0000 | [diff] [blame] | 90 | def close(self): |
| 91 | del self.fp |
Guido van Rossum | c7b6882 | 1994-04-28 09:53:33 +0000 | [diff] [blame] | 92 | |
Guido van Rossum | c7b6882 | 1994-04-28 09:53:33 +0000 | [diff] [blame] | 93 | |
| 94 | class UnixMailbox(_Mailbox): |
Fred Drake | dbbf76b | 2000-07-09 16:44:26 +0000 | [diff] [blame] | 95 | 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 Rossum | 8ca8420 | 1998-03-26 20:56:10 +0000 | [diff] [blame] | 104 | |
Fred Drake | dbbf76b | 2000-07-09 16:44:26 +0000 | [diff] [blame] | 105 | 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 Rossum | c7b6882 | 1994-04-28 09:53:33 +0000 | [diff] [blame] | 115 | |
Barry Warsaw | 81ad67c | 2001-01-31 22:13:15 +0000 | [diff] [blame] | 116 | # 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 |
| 138 | # necessary. For convenience, we've added a StrictUnixMailbox class which |
| 139 | # uses the older, more strict _fromlinepattern regular expression. |
Guido van Rossum | c7b6882 | 1994-04-28 09:53:33 +0000 | [diff] [blame] | 140 | |
Fred Drake | dbbf76b | 2000-07-09 16:44:26 +0000 | [diff] [blame] | 141 | _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 Rossum | fbe63de | 1998-04-03 16:04:05 +0000 | [diff] [blame] | 144 | |
Barry Warsaw | 81ad67c | 2001-01-31 22:13:15 +0000 | [diff] [blame] | 145 | def _strict_isrealfromline(self, line): |
Fred Drake | dbbf76b | 2000-07-09 16:44:26 +0000 | [diff] [blame] | 146 | if not self._regexp: |
| 147 | import re |
| 148 | self._regexp = re.compile(self._fromlinepattern) |
| 149 | return self._regexp.match(line) |
Guido van Rossum | fbe63de | 1998-04-03 16:04:05 +0000 | [diff] [blame] | 150 | |
Barry Warsaw | 81ad67c | 2001-01-31 22:13:15 +0000 | [diff] [blame] | 151 | def _portable_isrealfromline(self, line): |
| 152 | return 1 |
| 153 | |
| 154 | _isrealfromline = _strict_isrealfromline |
| 155 | |
| 156 | |
| 157 | class PortableUnixMailbox(UnixMailbox): |
| 158 | _isrealfromline = UnixMailbox._portable_isrealfromline |
| 159 | |
Guido van Rossum | fbe63de | 1998-04-03 16:04:05 +0000 | [diff] [blame] | 160 | |
Guido van Rossum | c7b6882 | 1994-04-28 09:53:33 +0000 | [diff] [blame] | 161 | class MmdfMailbox(_Mailbox): |
Fred Drake | dbbf76b | 2000-07-09 16:44:26 +0000 | [diff] [blame] | 162 | def _search_start(self): |
| 163 | while 1: |
| 164 | line = self.fp.readline() |
| 165 | if not line: |
| 166 | raise EOFError |
| 167 | if line[:5] == '\001\001\001\001\n': |
| 168 | return |
Guido van Rossum | 8ca8420 | 1998-03-26 20:56:10 +0000 | [diff] [blame] | 169 | |
Fred Drake | dbbf76b | 2000-07-09 16:44:26 +0000 | [diff] [blame] | 170 | def _search_end(self): |
| 171 | while 1: |
| 172 | pos = self.fp.tell() |
| 173 | line = self.fp.readline() |
| 174 | if not line: |
| 175 | return |
| 176 | if line == '\001\001\001\001\n': |
| 177 | self.fp.seek(pos) |
| 178 | return |
Guido van Rossum | c7b6882 | 1994-04-28 09:53:33 +0000 | [diff] [blame] | 179 | |
Guido van Rossum | c7b6882 | 1994-04-28 09:53:33 +0000 | [diff] [blame] | 180 | |
Jack Jansen | 9715779 | 1995-10-23 13:59:53 +0000 | [diff] [blame] | 181 | class MHMailbox: |
Barry Warsaw | 81ad67c | 2001-01-31 22:13:15 +0000 | [diff] [blame] | 182 | def __init__(self, dirname, factory=rfc822.Message): |
Fred Drake | dbbf76b | 2000-07-09 16:44:26 +0000 | [diff] [blame] | 183 | import re |
Guido van Rossum | 0707fea | 2000-08-10 03:05:26 +0000 | [diff] [blame] | 184 | pat = re.compile('^[1-9][0-9]*$') |
Fred Drake | dbbf76b | 2000-07-09 16:44:26 +0000 | [diff] [blame] | 185 | self.dirname = dirname |
Sjoerd Mullender | d2653a9 | 2000-08-11 07:48:36 +0000 | [diff] [blame] | 186 | # the three following lines could be combined into: |
| 187 | # list = map(long, filter(pat.match, os.listdir(self.dirname))) |
| 188 | list = os.listdir(self.dirname) |
| 189 | list = filter(pat.match, list) |
Guido van Rossum | 0707fea | 2000-08-10 03:05:26 +0000 | [diff] [blame] | 190 | list = map(long, list) |
| 191 | list.sort() |
| 192 | # This only works in Python 1.6 or later; |
| 193 | # before that str() added 'L': |
| 194 | self.boxes = map(str, list) |
Barry Warsaw | 81ad67c | 2001-01-31 22:13:15 +0000 | [diff] [blame] | 195 | self.factory = factory |
Jack Jansen | 9715779 | 1995-10-23 13:59:53 +0000 | [diff] [blame] | 196 | |
Fred Drake | 72987a4 | 2001-05-02 20:20:53 +0000 | [diff] [blame] | 197 | def __iter__(self): |
| 198 | return self |
| 199 | |
Fred Drake | dbbf76b | 2000-07-09 16:44:26 +0000 | [diff] [blame] | 200 | def next(self): |
| 201 | if not self.boxes: |
| 202 | return None |
| 203 | fn = self.boxes[0] |
| 204 | del self.boxes[0] |
| 205 | fp = open(os.path.join(self.dirname, fn)) |
Barry Warsaw | 81ad67c | 2001-01-31 22:13:15 +0000 | [diff] [blame] | 206 | return self.factory(fp) |
Guido van Rossum | 8ca8420 | 1998-03-26 20:56:10 +0000 | [diff] [blame] | 207 | |
Guido van Rossum | 9a4d637 | 1998-12-23 22:05:42 +0000 | [diff] [blame] | 208 | |
| 209 | class Maildir: |
Fred Drake | dbbf76b | 2000-07-09 16:44:26 +0000 | [diff] [blame] | 210 | # Qmail directory mailbox |
Guido van Rossum | 9a4d637 | 1998-12-23 22:05:42 +0000 | [diff] [blame] | 211 | |
Barry Warsaw | 81ad67c | 2001-01-31 22:13:15 +0000 | [diff] [blame] | 212 | def __init__(self, dirname, factory=rfc822.Message): |
Fred Drake | dbbf76b | 2000-07-09 16:44:26 +0000 | [diff] [blame] | 213 | self.dirname = dirname |
Barry Warsaw | 81ad67c | 2001-01-31 22:13:15 +0000 | [diff] [blame] | 214 | self.factory = factory |
Guido van Rossum | 9a4d637 | 1998-12-23 22:05:42 +0000 | [diff] [blame] | 215 | |
Fred Drake | dbbf76b | 2000-07-09 16:44:26 +0000 | [diff] [blame] | 216 | # check for new mail |
| 217 | newdir = os.path.join(self.dirname, 'new') |
Fred Drake | d9a8dec | 2000-09-22 18:41:50 +0000 | [diff] [blame] | 218 | boxes = [os.path.join(newdir, f) |
| 219 | for f in os.listdir(newdir) if f[0] != '.'] |
Guido van Rossum | 9a4d637 | 1998-12-23 22:05:42 +0000 | [diff] [blame] | 220 | |
Fred Drake | dbbf76b | 2000-07-09 16:44:26 +0000 | [diff] [blame] | 221 | # Now check for current mail in this maildir |
| 222 | curdir = os.path.join(self.dirname, 'cur') |
Fred Drake | d9a8dec | 2000-09-22 18:41:50 +0000 | [diff] [blame] | 223 | boxes += [os.path.join(curdir, f) |
| 224 | for f in os.listdir(curdir) if f[0] != '.'] |
Guido van Rossum | 9a4d637 | 1998-12-23 22:05:42 +0000 | [diff] [blame] | 225 | |
Fred Drake | de3518e | 2000-10-23 13:37:01 +0000 | [diff] [blame] | 226 | self.boxes = boxes |
| 227 | |
Fred Drake | 72987a4 | 2001-05-02 20:20:53 +0000 | [diff] [blame] | 228 | def __iter__(self): |
| 229 | return self |
| 230 | |
Fred Drake | dbbf76b | 2000-07-09 16:44:26 +0000 | [diff] [blame] | 231 | def next(self): |
| 232 | if not self.boxes: |
| 233 | return None |
| 234 | fn = self.boxes[0] |
| 235 | del self.boxes[0] |
Fred Drake | e108a02 | 2000-09-14 14:44:43 +0000 | [diff] [blame] | 236 | fp = open(fn) |
Barry Warsaw | 81ad67c | 2001-01-31 22:13:15 +0000 | [diff] [blame] | 237 | return self.factory(fp) |
Guido van Rossum | 9a4d637 | 1998-12-23 22:05:42 +0000 | [diff] [blame] | 238 | |
Guido van Rossum | 9a4d637 | 1998-12-23 22:05:42 +0000 | [diff] [blame] | 239 | |
Guido van Rossum | fdf58fe | 1997-05-15 14:33:09 +0000 | [diff] [blame] | 240 | class BabylMailbox(_Mailbox): |
Fred Drake | dbbf76b | 2000-07-09 16:44:26 +0000 | [diff] [blame] | 241 | def _search_start(self): |
| 242 | while 1: |
| 243 | line = self.fp.readline() |
| 244 | if not line: |
| 245 | raise EOFError |
| 246 | if line == '*** EOOH ***\n': |
| 247 | return |
Guido van Rossum | fdf58fe | 1997-05-15 14:33:09 +0000 | [diff] [blame] | 248 | |
Fred Drake | dbbf76b | 2000-07-09 16:44:26 +0000 | [diff] [blame] | 249 | def _search_end(self): |
| 250 | while 1: |
| 251 | pos = self.fp.tell() |
| 252 | line = self.fp.readline() |
| 253 | if not line: |
| 254 | return |
| 255 | if line == '\037\014\n': |
| 256 | self.fp.seek(pos) |
| 257 | return |
Guido van Rossum | fdf58fe | 1997-05-15 14:33:09 +0000 | [diff] [blame] | 258 | |
| 259 | |
Guido van Rossum | 6244867 | 1996-09-17 21:33:15 +0000 | [diff] [blame] | 260 | def _test(): |
Fred Drake | dbbf76b | 2000-07-09 16:44:26 +0000 | [diff] [blame] | 261 | import time |
| 262 | import sys |
Fred Drake | dbbf76b | 2000-07-09 16:44:26 +0000 | [diff] [blame] | 263 | import os |
Jack Jansen | 9715779 | 1995-10-23 13:59:53 +0000 | [diff] [blame] | 264 | |
Fred Drake | dbbf76b | 2000-07-09 16:44:26 +0000 | [diff] [blame] | 265 | args = sys.argv[1:] |
| 266 | if not args: |
| 267 | for key in 'MAILDIR', 'MAIL', 'LOGNAME', 'USER': |
| 268 | if os.environ.has_key(key): |
| 269 | mbox = os.environ[key] |
| 270 | break |
Fred Drake | 13a2c27 | 2000-02-10 17:17:14 +0000 | [diff] [blame] | 271 | else: |
Fred Drake | dbbf76b | 2000-07-09 16:44:26 +0000 | [diff] [blame] | 272 | print "$MAIL, $LOGNAME nor $USER set -- who are you?" |
| 273 | return |
| 274 | else: |
| 275 | mbox = args[0] |
| 276 | if mbox[:1] == '+': |
| 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')): |
| 282 | mb = Maildir(mbox) |
Fred Drake | 13a2c27 | 2000-02-10 17:17:14 +0000 | [diff] [blame] | 283 | else: |
Fred Drake | dbbf76b | 2000-07-09 16:44:26 +0000 | [diff] [blame] | 284 | mb = MHMailbox(mbox) |
| 285 | else: |
| 286 | fp = open(mbox, 'r') |
| 287 | mb = UnixMailbox(fp) |
| 288 | |
| 289 | msgs = [] |
| 290 | while 1: |
| 291 | msg = mb.next() |
| 292 | if msg is None: |
| 293 | break |
| 294 | msgs.append(msg) |
| 295 | if len(args) <= 1: |
| 296 | msg.fp = None |
| 297 | if len(args) > 1: |
Fred Drake | de3518e | 2000-10-23 13:37:01 +0000 | [diff] [blame] | 298 | num = int(args[1]) |
Fred Drake | dbbf76b | 2000-07-09 16:44:26 +0000 | [diff] [blame] | 299 | print 'Message %d body:'%num |
| 300 | msg = msgs[num-1] |
| 301 | msg.rewindbody() |
| 302 | sys.stdout.write(msg.fp.read()) |
| 303 | else: |
| 304 | print 'Mailbox',mbox,'has',len(msgs),'messages:' |
| 305 | for msg in msgs: |
| 306 | f = msg.getheader('from') or "" |
| 307 | s = msg.getheader('subject') or "" |
| 308 | d = msg.getheader('date') or "" |
Fred Drake | cc4adf2 | 2000-09-30 23:59:04 +0000 | [diff] [blame] | 309 | print '-%20.20s %20.20s %-30.30s'%(f, d[5:], s) |
Guido van Rossum | 6244867 | 1996-09-17 21:33:15 +0000 | [diff] [blame] | 310 | |
| 311 | |
| 312 | if __name__ == '__main__': |
Fred Drake | dbbf76b | 2000-07-09 16:44:26 +0000 | [diff] [blame] | 313 | _test() |