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 | |
Barry Warsaw | ffd05ee | 2002-03-01 22:39:14 +0000 | [diff] [blame] | 9 | __all__ = ["UnixMailbox","MmdfMailbox","MHMailbox","Maildir","BabylMailbox", |
| 10 | "PortableUnixMailbox"] |
Skip Montanaro | 17ab123 | 2001-01-24 06:27:27 +0000 | [diff] [blame] | 11 | |
Guido van Rossum | c7b6882 | 1994-04-28 09:53:33 +0000 | [diff] [blame] | 12 | class _Mailbox: |
Barry Warsaw | 81ad67c | 2001-01-31 22:13:15 +0000 | [diff] [blame] | 13 | def __init__(self, fp, factory=rfc822.Message): |
Fred Drake | dbbf76b | 2000-07-09 16:44:26 +0000 | [diff] [blame] | 14 | self.fp = fp |
| 15 | self.seekp = 0 |
Barry Warsaw | 81ad67c | 2001-01-31 22:13:15 +0000 | [diff] [blame] | 16 | self.factory = factory |
Guido van Rossum | 8ca8420 | 1998-03-26 20:56:10 +0000 | [diff] [blame] | 17 | |
Fred Drake | 72987a4 | 2001-05-02 20:20:53 +0000 | [diff] [blame] | 18 | def __iter__(self): |
Guido van Rossum | 93a696f | 2001-09-13 01:29:13 +0000 | [diff] [blame] | 19 | return iter(self.next, None) |
Fred Drake | 72987a4 | 2001-05-02 20:20:53 +0000 | [diff] [blame] | 20 | |
Fred Drake | dbbf76b | 2000-07-09 16:44:26 +0000 | [diff] [blame] | 21 | def next(self): |
| 22 | while 1: |
| 23 | self.fp.seek(self.seekp) |
| 24 | try: |
| 25 | self._search_start() |
| 26 | except EOFError: |
| 27 | self.seekp = self.fp.tell() |
| 28 | return None |
| 29 | start = self.fp.tell() |
| 30 | self._search_end() |
| 31 | self.seekp = stop = self.fp.tell() |
Fred Drake | 8152d32 | 2000-12-12 23:20:45 +0000 | [diff] [blame] | 32 | if start != stop: |
Fred Drake | dbbf76b | 2000-07-09 16:44:26 +0000 | [diff] [blame] | 33 | break |
Barry Warsaw | 81ad67c | 2001-01-31 22:13:15 +0000 | [diff] [blame] | 34 | return self.factory(_Subfile(self.fp, start, stop)) |
Guido van Rossum | c7b6882 | 1994-04-28 09:53:33 +0000 | [diff] [blame] | 35 | |
Guido van Rossum | c7b6882 | 1994-04-28 09:53:33 +0000 | [diff] [blame] | 36 | |
| 37 | class _Subfile: |
Fred Drake | dbbf76b | 2000-07-09 16:44:26 +0000 | [diff] [blame] | 38 | def __init__(self, fp, start, stop): |
| 39 | self.fp = fp |
| 40 | self.start = start |
| 41 | self.stop = stop |
| 42 | self.pos = self.start |
Guido van Rossum | 8ca8420 | 1998-03-26 20:56:10 +0000 | [diff] [blame] | 43 | |
Fred Drake | dbbf76b | 2000-07-09 16:44:26 +0000 | [diff] [blame] | 44 | def read(self, length = None): |
| 45 | if self.pos >= self.stop: |
| 46 | return '' |
| 47 | remaining = self.stop - self.pos |
| 48 | if length is None or length < 0: |
| 49 | length = remaining |
| 50 | elif length > remaining: |
| 51 | length = remaining |
| 52 | self.fp.seek(self.pos) |
| 53 | data = self.fp.read(length) |
| 54 | self.pos = self.fp.tell() |
| 55 | return data |
Guido van Rossum | c7b6882 | 1994-04-28 09:53:33 +0000 | [diff] [blame] | 56 | |
Fred Drake | dbbf76b | 2000-07-09 16:44:26 +0000 | [diff] [blame] | 57 | def readline(self, length = None): |
| 58 | if self.pos >= self.stop: |
| 59 | return '' |
| 60 | if length is None: |
| 61 | length = self.stop - self.pos |
| 62 | self.fp.seek(self.pos) |
| 63 | data = self.fp.readline(length) |
| 64 | self.pos = self.fp.tell() |
| 65 | return data |
Guido van Rossum | c7b6882 | 1994-04-28 09:53:33 +0000 | [diff] [blame] | 66 | |
Fred Drake | dbbf76b | 2000-07-09 16:44:26 +0000 | [diff] [blame] | 67 | def readlines(self, sizehint = -1): |
| 68 | lines = [] |
| 69 | while 1: |
| 70 | line = self.readline() |
| 71 | if not line: |
| 72 | break |
| 73 | lines.append(line) |
| 74 | if sizehint >= 0: |
| 75 | sizehint = sizehint - len(line) |
| 76 | if sizehint <= 0: |
| 77 | break |
| 78 | return lines |
Guido van Rossum | c7b6882 | 1994-04-28 09:53:33 +0000 | [diff] [blame] | 79 | |
Fred Drake | dbbf76b | 2000-07-09 16:44:26 +0000 | [diff] [blame] | 80 | def tell(self): |
| 81 | return self.pos - self.start |
Guido van Rossum | e256a0f | 1999-03-24 16:20:45 +0000 | [diff] [blame] | 82 | |
Fred Drake | dbbf76b | 2000-07-09 16:44:26 +0000 | [diff] [blame] | 83 | def seek(self, pos, whence=0): |
| 84 | if whence == 0: |
| 85 | self.pos = self.start + pos |
| 86 | elif whence == 1: |
| 87 | self.pos = self.pos + pos |
| 88 | elif whence == 2: |
| 89 | self.pos = self.stop + pos |
Guido van Rossum | c7b6882 | 1994-04-28 09:53:33 +0000 | [diff] [blame] | 90 | |
Fred Drake | dbbf76b | 2000-07-09 16:44:26 +0000 | [diff] [blame] | 91 | def close(self): |
| 92 | del self.fp |
Guido van Rossum | c7b6882 | 1994-04-28 09:53:33 +0000 | [diff] [blame] | 93 | |
Guido van Rossum | c7b6882 | 1994-04-28 09:53:33 +0000 | [diff] [blame] | 94 | |
Barry Warsaw | ffd05ee | 2002-03-01 22:39:14 +0000 | [diff] [blame] | 95 | # Recommended to use PortableUnixMailbox instead! |
Guido van Rossum | c7b6882 | 1994-04-28 09:53:33 +0000 | [diff] [blame] | 96 | class UnixMailbox(_Mailbox): |
Fred Drake | dbbf76b | 2000-07-09 16:44:26 +0000 | [diff] [blame] | 97 | def _search_start(self): |
| 98 | while 1: |
| 99 | pos = self.fp.tell() |
| 100 | line = self.fp.readline() |
| 101 | if not line: |
| 102 | raise EOFError |
| 103 | if line[:5] == 'From ' and self._isrealfromline(line): |
| 104 | self.fp.seek(pos) |
| 105 | return |
Guido van Rossum | 8ca8420 | 1998-03-26 20:56:10 +0000 | [diff] [blame] | 106 | |
Fred Drake | dbbf76b | 2000-07-09 16:44:26 +0000 | [diff] [blame] | 107 | def _search_end(self): |
| 108 | self.fp.readline() # Throw away header line |
| 109 | while 1: |
| 110 | pos = self.fp.tell() |
| 111 | line = self.fp.readline() |
| 112 | if not line: |
| 113 | return |
| 114 | if line[:5] == 'From ' and self._isrealfromline(line): |
| 115 | self.fp.seek(pos) |
| 116 | return |
Guido van Rossum | c7b6882 | 1994-04-28 09:53:33 +0000 | [diff] [blame] | 117 | |
Barry Warsaw | 81ad67c | 2001-01-31 22:13:15 +0000 | [diff] [blame] | 118 | # An overridable mechanism to test for From-line-ness. You can either |
| 119 | # specify a different regular expression or define a whole new |
| 120 | # _isrealfromline() method. Note that this only gets called for lines |
| 121 | # starting with the 5 characters "From ". |
| 122 | # |
| 123 | # BAW: According to |
| 124 | #http://home.netscape.com/eng/mozilla/2.0/relnotes/demo/content-length.html |
| 125 | # the only portable, reliable way to find message delimiters in a BSD (i.e |
| 126 | # Unix mailbox) style folder is to search for "\n\nFrom .*\n", or at the |
| 127 | # beginning of the file, "^From .*\n". While _fromlinepattern below seems |
| 128 | # like a good idea, in practice, there are too many variations for more |
| 129 | # strict parsing of the line to be completely accurate. |
| 130 | # |
| 131 | # _strict_isrealfromline() is the old version which tries to do stricter |
| 132 | # parsing of the From_ line. _portable_isrealfromline() simply returns |
| 133 | # true, since it's never called if the line doesn't already start with |
| 134 | # "From ". |
| 135 | # |
| 136 | # This algorithm, and the way it interacts with _search_start() and |
| 137 | # _search_end() may not be completely correct, because it doesn't check |
| 138 | # that the two characters preceding "From " are \n\n or the beginning of |
| 139 | # the file. Fixing this would require a more extensive rewrite than is |
| 140 | # necessary. For convenience, we've added a StrictUnixMailbox class which |
| 141 | # uses the older, more strict _fromlinepattern regular expression. |
Guido van Rossum | c7b6882 | 1994-04-28 09:53:33 +0000 | [diff] [blame] | 142 | |
Fred Drake | dbbf76b | 2000-07-09 16:44:26 +0000 | [diff] [blame] | 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*$" |
| 145 | _regexp = None |
Guido van Rossum | fbe63de | 1998-04-03 16:04:05 +0000 | [diff] [blame] | 146 | |
Barry Warsaw | 81ad67c | 2001-01-31 22:13:15 +0000 | [diff] [blame] | 147 | def _strict_isrealfromline(self, line): |
Fred Drake | dbbf76b | 2000-07-09 16:44:26 +0000 | [diff] [blame] | 148 | if not self._regexp: |
| 149 | import re |
| 150 | self._regexp = re.compile(self._fromlinepattern) |
| 151 | return self._regexp.match(line) |
Guido van Rossum | fbe63de | 1998-04-03 16:04:05 +0000 | [diff] [blame] | 152 | |
Barry Warsaw | 81ad67c | 2001-01-31 22:13:15 +0000 | [diff] [blame] | 153 | def _portable_isrealfromline(self, line): |
Tim Peters | bc0e910 | 2002-04-04 22:55:58 +0000 | [diff] [blame] | 154 | return True |
Barry Warsaw | 81ad67c | 2001-01-31 22:13:15 +0000 | [diff] [blame] | 155 | |
| 156 | _isrealfromline = _strict_isrealfromline |
| 157 | |
| 158 | |
| 159 | class PortableUnixMailbox(UnixMailbox): |
| 160 | _isrealfromline = UnixMailbox._portable_isrealfromline |
| 161 | |
Guido van Rossum | fbe63de | 1998-04-03 16:04:05 +0000 | [diff] [blame] | 162 | |
Guido van Rossum | c7b6882 | 1994-04-28 09:53:33 +0000 | [diff] [blame] | 163 | class MmdfMailbox(_Mailbox): |
Fred Drake | dbbf76b | 2000-07-09 16:44:26 +0000 | [diff] [blame] | 164 | def _search_start(self): |
| 165 | while 1: |
| 166 | line = self.fp.readline() |
| 167 | if not line: |
| 168 | raise EOFError |
| 169 | if line[:5] == '\001\001\001\001\n': |
| 170 | return |
Guido van Rossum | 8ca8420 | 1998-03-26 20:56:10 +0000 | [diff] [blame] | 171 | |
Fred Drake | dbbf76b | 2000-07-09 16:44:26 +0000 | [diff] [blame] | 172 | def _search_end(self): |
| 173 | while 1: |
| 174 | pos = self.fp.tell() |
| 175 | line = self.fp.readline() |
| 176 | if not line: |
| 177 | return |
| 178 | if line == '\001\001\001\001\n': |
| 179 | self.fp.seek(pos) |
| 180 | return |
Guido van Rossum | c7b6882 | 1994-04-28 09:53:33 +0000 | [diff] [blame] | 181 | |
Guido van Rossum | c7b6882 | 1994-04-28 09:53:33 +0000 | [diff] [blame] | 182 | |
Jack Jansen | 9715779 | 1995-10-23 13:59:53 +0000 | [diff] [blame] | 183 | class MHMailbox: |
Barry Warsaw | 81ad67c | 2001-01-31 22:13:15 +0000 | [diff] [blame] | 184 | def __init__(self, dirname, factory=rfc822.Message): |
Fred Drake | dbbf76b | 2000-07-09 16:44:26 +0000 | [diff] [blame] | 185 | import re |
Guido van Rossum | 0707fea | 2000-08-10 03:05:26 +0000 | [diff] [blame] | 186 | pat = re.compile('^[1-9][0-9]*$') |
Fred Drake | dbbf76b | 2000-07-09 16:44:26 +0000 | [diff] [blame] | 187 | self.dirname = dirname |
Sjoerd Mullender | d2653a9 | 2000-08-11 07:48:36 +0000 | [diff] [blame] | 188 | # 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 Rossum | 0707fea | 2000-08-10 03:05:26 +0000 | [diff] [blame] | 192 | 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) |
Barry Warsaw | 81ad67c | 2001-01-31 22:13:15 +0000 | [diff] [blame] | 197 | self.factory = factory |
Jack Jansen | 9715779 | 1995-10-23 13:59:53 +0000 | [diff] [blame] | 198 | |
Fred Drake | 72987a4 | 2001-05-02 20:20:53 +0000 | [diff] [blame] | 199 | def __iter__(self): |
Guido van Rossum | 93a696f | 2001-09-13 01:29:13 +0000 | [diff] [blame] | 200 | return iter(self.next, None) |
Fred Drake | 72987a4 | 2001-05-02 20:20:53 +0000 | [diff] [blame] | 201 | |
Fred Drake | dbbf76b | 2000-07-09 16:44:26 +0000 | [diff] [blame] | 202 | def next(self): |
| 203 | if not self.boxes: |
| 204 | return None |
| 205 | fn = self.boxes[0] |
| 206 | del self.boxes[0] |
| 207 | fp = open(os.path.join(self.dirname, fn)) |
Barry Warsaw | 81ad67c | 2001-01-31 22:13:15 +0000 | [diff] [blame] | 208 | return self.factory(fp) |
Guido van Rossum | 8ca8420 | 1998-03-26 20:56:10 +0000 | [diff] [blame] | 209 | |
Guido van Rossum | 9a4d637 | 1998-12-23 22:05:42 +0000 | [diff] [blame] | 210 | |
| 211 | class Maildir: |
Fred Drake | dbbf76b | 2000-07-09 16:44:26 +0000 | [diff] [blame] | 212 | # Qmail directory mailbox |
Guido van Rossum | 9a4d637 | 1998-12-23 22:05:42 +0000 | [diff] [blame] | 213 | |
Barry Warsaw | 81ad67c | 2001-01-31 22:13:15 +0000 | [diff] [blame] | 214 | def __init__(self, dirname, factory=rfc822.Message): |
Fred Drake | dbbf76b | 2000-07-09 16:44:26 +0000 | [diff] [blame] | 215 | self.dirname = dirname |
Barry Warsaw | 81ad67c | 2001-01-31 22:13:15 +0000 | [diff] [blame] | 216 | self.factory = factory |
Guido van Rossum | 9a4d637 | 1998-12-23 22:05:42 +0000 | [diff] [blame] | 217 | |
Fred Drake | dbbf76b | 2000-07-09 16:44:26 +0000 | [diff] [blame] | 218 | # check for new mail |
| 219 | newdir = os.path.join(self.dirname, 'new') |
Fred Drake | d9a8dec | 2000-09-22 18:41:50 +0000 | [diff] [blame] | 220 | boxes = [os.path.join(newdir, f) |
| 221 | for f in os.listdir(newdir) if f[0] != '.'] |
Guido van Rossum | 9a4d637 | 1998-12-23 22:05:42 +0000 | [diff] [blame] | 222 | |
Fred Drake | dbbf76b | 2000-07-09 16:44:26 +0000 | [diff] [blame] | 223 | # Now check for current mail in this maildir |
| 224 | curdir = os.path.join(self.dirname, 'cur') |
Fred Drake | d9a8dec | 2000-09-22 18:41:50 +0000 | [diff] [blame] | 225 | boxes += [os.path.join(curdir, f) |
| 226 | for f in os.listdir(curdir) if f[0] != '.'] |
Guido van Rossum | 9a4d637 | 1998-12-23 22:05:42 +0000 | [diff] [blame] | 227 | |
Fred Drake | de3518e | 2000-10-23 13:37:01 +0000 | [diff] [blame] | 228 | self.boxes = boxes |
| 229 | |
Fred Drake | 72987a4 | 2001-05-02 20:20:53 +0000 | [diff] [blame] | 230 | def __iter__(self): |
Guido van Rossum | 93a696f | 2001-09-13 01:29:13 +0000 | [diff] [blame] | 231 | return iter(self.next, None) |
Fred Drake | 72987a4 | 2001-05-02 20:20:53 +0000 | [diff] [blame] | 232 | |
Fred Drake | dbbf76b | 2000-07-09 16:44:26 +0000 | [diff] [blame] | 233 | def next(self): |
| 234 | if not self.boxes: |
| 235 | return None |
| 236 | fn = self.boxes[0] |
| 237 | del self.boxes[0] |
Fred Drake | e108a02 | 2000-09-14 14:44:43 +0000 | [diff] [blame] | 238 | fp = open(fn) |
Barry Warsaw | 81ad67c | 2001-01-31 22:13:15 +0000 | [diff] [blame] | 239 | return self.factory(fp) |
Guido van Rossum | 9a4d637 | 1998-12-23 22:05:42 +0000 | [diff] [blame] | 240 | |
Guido van Rossum | 9a4d637 | 1998-12-23 22:05:42 +0000 | [diff] [blame] | 241 | |
Guido van Rossum | fdf58fe | 1997-05-15 14:33:09 +0000 | [diff] [blame] | 242 | class BabylMailbox(_Mailbox): |
Fred Drake | dbbf76b | 2000-07-09 16:44:26 +0000 | [diff] [blame] | 243 | def _search_start(self): |
| 244 | while 1: |
| 245 | line = self.fp.readline() |
| 246 | if not line: |
| 247 | raise EOFError |
| 248 | if line == '*** EOOH ***\n': |
| 249 | return |
Guido van Rossum | fdf58fe | 1997-05-15 14:33:09 +0000 | [diff] [blame] | 250 | |
Fred Drake | dbbf76b | 2000-07-09 16:44:26 +0000 | [diff] [blame] | 251 | def _search_end(self): |
| 252 | while 1: |
| 253 | pos = self.fp.tell() |
| 254 | line = self.fp.readline() |
| 255 | if not line: |
| 256 | return |
| 257 | if line == '\037\014\n': |
| 258 | self.fp.seek(pos) |
| 259 | return |
Guido van Rossum | fdf58fe | 1997-05-15 14:33:09 +0000 | [diff] [blame] | 260 | |
| 261 | |
Guido van Rossum | 6244867 | 1996-09-17 21:33:15 +0000 | [diff] [blame] | 262 | def _test(): |
Fred Drake | dbbf76b | 2000-07-09 16:44:26 +0000 | [diff] [blame] | 263 | import sys |
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: |
Neil Schemenauer | 03f3ee6 | 2002-03-24 01:38:38 +0000 | [diff] [blame] | 279 | if os.path.isfile('/var/mail/' + mbox): |
| 280 | mbox = '/var/mail/' + mbox |
| 281 | else: |
| 282 | mbox = '/usr/mail/' + mbox |
Fred Drake | dbbf76b | 2000-07-09 16:44:26 +0000 | [diff] [blame] | 283 | if os.path.isdir(mbox): |
| 284 | if os.path.isdir(os.path.join(mbox, 'cur')): |
| 285 | mb = Maildir(mbox) |
Fred Drake | 13a2c27 | 2000-02-10 17:17:14 +0000 | [diff] [blame] | 286 | else: |
Fred Drake | dbbf76b | 2000-07-09 16:44:26 +0000 | [diff] [blame] | 287 | mb = MHMailbox(mbox) |
| 288 | else: |
| 289 | fp = open(mbox, 'r') |
Barry Warsaw | ffd05ee | 2002-03-01 22:39:14 +0000 | [diff] [blame] | 290 | mb = PortableUnixMailbox(fp) |
Fred Drake | dbbf76b | 2000-07-09 16:44:26 +0000 | [diff] [blame] | 291 | |
| 292 | msgs = [] |
| 293 | while 1: |
| 294 | msg = mb.next() |
| 295 | if msg is None: |
| 296 | break |
| 297 | msgs.append(msg) |
| 298 | if len(args) <= 1: |
| 299 | msg.fp = None |
| 300 | if len(args) > 1: |
Fred Drake | de3518e | 2000-10-23 13:37:01 +0000 | [diff] [blame] | 301 | num = int(args[1]) |
Fred Drake | dbbf76b | 2000-07-09 16:44:26 +0000 | [diff] [blame] | 302 | print 'Message %d body:'%num |
| 303 | msg = msgs[num-1] |
| 304 | msg.rewindbody() |
| 305 | sys.stdout.write(msg.fp.read()) |
| 306 | else: |
| 307 | print 'Mailbox',mbox,'has',len(msgs),'messages:' |
| 308 | for msg in msgs: |
| 309 | f = msg.getheader('from') or "" |
| 310 | s = msg.getheader('subject') or "" |
| 311 | d = msg.getheader('date') or "" |
Fred Drake | cc4adf2 | 2000-09-30 23:59:04 +0000 | [diff] [blame] | 312 | print '-%20.20s %20.20s %-30.30s'%(f, d[5:], s) |
Guido van Rossum | 6244867 | 1996-09-17 21:33:15 +0000 | [diff] [blame] | 313 | |
| 314 | |
| 315 | if __name__ == '__main__': |
Fred Drake | dbbf76b | 2000-07-09 16:44:26 +0000 | [diff] [blame] | 316 | _test() |