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: |
Guido van Rossum | 4bf1254 | 2002-09-12 05:08:00 +0000 | [diff] [blame] | 13 | |
Barry Warsaw | 81ad67c | 2001-01-31 22:13:15 +0000 | [diff] [blame] | 14 | def __init__(self, fp, factory=rfc822.Message): |
Fred Drake | dbbf76b | 2000-07-09 16:44:26 +0000 | [diff] [blame] | 15 | self.fp = fp |
| 16 | self.seekp = 0 |
Barry Warsaw | 81ad67c | 2001-01-31 22:13:15 +0000 | [diff] [blame] | 17 | self.factory = factory |
Guido van Rossum | 8ca8420 | 1998-03-26 20:56:10 +0000 | [diff] [blame] | 18 | |
Fred Drake | 72987a4 | 2001-05-02 20:20:53 +0000 | [diff] [blame] | 19 | def __iter__(self): |
Guido van Rossum | 93a696f | 2001-09-13 01:29:13 +0000 | [diff] [blame] | 20 | return iter(self.next, None) |
Fred Drake | 72987a4 | 2001-05-02 20:20:53 +0000 | [diff] [blame] | 21 | |
Fred Drake | dbbf76b | 2000-07-09 16:44:26 +0000 | [diff] [blame] | 22 | 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 Drake | 8152d32 | 2000-12-12 23:20:45 +0000 | [diff] [blame] | 33 | if start != stop: |
Fred Drake | dbbf76b | 2000-07-09 16:44:26 +0000 | [diff] [blame] | 34 | break |
Barry Warsaw | 81ad67c | 2001-01-31 22:13:15 +0000 | [diff] [blame] | 35 | return self.factory(_Subfile(self.fp, start, stop)) |
Guido van Rossum | c7b6882 | 1994-04-28 09:53:33 +0000 | [diff] [blame] | 36 | |
Guido van Rossum | c7b6882 | 1994-04-28 09:53:33 +0000 | [diff] [blame] | 37 | |
| 38 | class _Subfile: |
Guido van Rossum | 4bf1254 | 2002-09-12 05:08:00 +0000 | [diff] [blame] | 39 | |
Fred Drake | dbbf76b | 2000-07-09 16:44:26 +0000 | [diff] [blame] | 40 | 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 Rossum | 8ca8420 | 1998-03-26 20:56:10 +0000 | [diff] [blame] | 45 | |
Andrew M. Kuchling | 1263bd8 | 2004-07-07 14:09:21 +0000 | [diff] [blame] | 46 | |
| 47 | def _read(self, length, read_function): |
Fred Drake | dbbf76b | 2000-07-09 16:44:26 +0000 | [diff] [blame] | 48 | if self.pos >= self.stop: |
| 49 | return '' |
| 50 | remaining = self.stop - self.pos |
Andrew M. Kuchling | 1263bd8 | 2004-07-07 14:09:21 +0000 | [diff] [blame] | 51 | if length is None or length < 0 or length > remaining: |
Fred Drake | dbbf76b | 2000-07-09 16:44:26 +0000 | [diff] [blame] | 52 | length = remaining |
| 53 | self.fp.seek(self.pos) |
Andrew M. Kuchling | 1263bd8 | 2004-07-07 14:09:21 +0000 | [diff] [blame] | 54 | data = read_function(length) |
Fred Drake | dbbf76b | 2000-07-09 16:44:26 +0000 | [diff] [blame] | 55 | self.pos = self.fp.tell() |
| 56 | return data |
Guido van Rossum | c7b6882 | 1994-04-28 09:53:33 +0000 | [diff] [blame] | 57 | |
Andrew M. Kuchling | 1263bd8 | 2004-07-07 14:09:21 +0000 | [diff] [blame] | 58 | def read(self, length = None): |
| 59 | self._read(length, self.fp.read) |
| 60 | |
Fred Drake | dbbf76b | 2000-07-09 16:44:26 +0000 | [diff] [blame] | 61 | def readline(self, length = None): |
Andrew M. Kuchling | 1263bd8 | 2004-07-07 14:09:21 +0000 | [diff] [blame] | 62 | self._read(length, self.fp.readline) |
Guido van Rossum | c7b6882 | 1994-04-28 09:53:33 +0000 | [diff] [blame] | 63 | |
Fred Drake | dbbf76b | 2000-07-09 16:44:26 +0000 | [diff] [blame] | 64 | 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 Rossum | c7b6882 | 1994-04-28 09:53:33 +0000 | [diff] [blame] | 76 | |
Fred Drake | dbbf76b | 2000-07-09 16:44:26 +0000 | [diff] [blame] | 77 | def tell(self): |
| 78 | return self.pos - self.start |
Guido van Rossum | e256a0f | 1999-03-24 16:20:45 +0000 | [diff] [blame] | 79 | |
Fred Drake | dbbf76b | 2000-07-09 16:44:26 +0000 | [diff] [blame] | 80 | 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 Rossum | c7b6882 | 1994-04-28 09:53:33 +0000 | [diff] [blame] | 87 | |
Fred Drake | dbbf76b | 2000-07-09 16:44:26 +0000 | [diff] [blame] | 88 | def close(self): |
| 89 | del self.fp |
Guido van Rossum | c7b6882 | 1994-04-28 09:53:33 +0000 | [diff] [blame] | 90 | |
Guido van Rossum | c7b6882 | 1994-04-28 09:53:33 +0000 | [diff] [blame] | 91 | |
Barry Warsaw | ffd05ee | 2002-03-01 22:39:14 +0000 | [diff] [blame] | 92 | # Recommended to use PortableUnixMailbox instead! |
Guido van Rossum | c7b6882 | 1994-04-28 09:53:33 +0000 | [diff] [blame] | 93 | class UnixMailbox(_Mailbox): |
Guido van Rossum | 4bf1254 | 2002-09-12 05:08:00 +0000 | [diff] [blame] | 94 | |
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 |
Barry Warsaw | da5628f | 2002-08-26 16:44:56 +0000 | [diff] [blame] | 138 | # necessary. For convenience, we've added a PortableUnixMailbox class |
| 139 | # which uses the more lenient _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): |
Tim Peters | bc0e910 | 2002-04-04 22:55:58 +0000 | [diff] [blame] | 152 | return True |
Barry Warsaw | 81ad67c | 2001-01-31 22:13:15 +0000 | [diff] [blame] | 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): |
Guido van Rossum | 4bf1254 | 2002-09-12 05:08:00 +0000 | [diff] [blame] | 162 | |
Fred Drake | dbbf76b | 2000-07-09 16:44:26 +0000 | [diff] [blame] | 163 | 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 Rossum | 8ca8420 | 1998-03-26 20:56:10 +0000 | [diff] [blame] | 170 | |
Fred Drake | dbbf76b | 2000-07-09 16:44:26 +0000 | [diff] [blame] | 171 | 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 Rossum | c7b6882 | 1994-04-28 09:53:33 +0000 | [diff] [blame] | 180 | |
Guido van Rossum | c7b6882 | 1994-04-28 09:53:33 +0000 | [diff] [blame] | 181 | |
Jack Jansen | 9715779 | 1995-10-23 13:59:53 +0000 | [diff] [blame] | 182 | class MHMailbox: |
Guido van Rossum | 4bf1254 | 2002-09-12 05:08:00 +0000 | [diff] [blame] | 183 | |
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) |
Raymond Hettinger | b5ba8d7 | 2004-02-07 02:16:24 +0000 | [diff] [blame] | 197 | self.boxes.reverse() |
Barry Warsaw | 81ad67c | 2001-01-31 22:13:15 +0000 | [diff] [blame] | 198 | self.factory = factory |
Jack Jansen | 9715779 | 1995-10-23 13:59:53 +0000 | [diff] [blame] | 199 | |
Fred Drake | 72987a4 | 2001-05-02 20:20:53 +0000 | [diff] [blame] | 200 | def __iter__(self): |
Guido van Rossum | 93a696f | 2001-09-13 01:29:13 +0000 | [diff] [blame] | 201 | return iter(self.next, None) |
Fred Drake | 72987a4 | 2001-05-02 20:20:53 +0000 | [diff] [blame] | 202 | |
Fred Drake | dbbf76b | 2000-07-09 16:44:26 +0000 | [diff] [blame] | 203 | def next(self): |
| 204 | if not self.boxes: |
| 205 | return None |
Raymond Hettinger | b5ba8d7 | 2004-02-07 02:16:24 +0000 | [diff] [blame] | 206 | fn = self.boxes.pop() |
Fred Drake | dbbf76b | 2000-07-09 16:44:26 +0000 | [diff] [blame] | 207 | fp = open(os.path.join(self.dirname, fn)) |
Guido van Rossum | 4bf1254 | 2002-09-12 05:08:00 +0000 | [diff] [blame] | 208 | msg = self.factory(fp) |
| 209 | try: |
| 210 | msg._mh_msgno = fn |
| 211 | except (AttributeError, TypeError): |
| 212 | pass |
| 213 | return msg |
Guido van Rossum | 8ca8420 | 1998-03-26 20:56:10 +0000 | [diff] [blame] | 214 | |
Guido van Rossum | 9a4d637 | 1998-12-23 22:05:42 +0000 | [diff] [blame] | 215 | |
| 216 | class Maildir: |
Fred Drake | dbbf76b | 2000-07-09 16:44:26 +0000 | [diff] [blame] | 217 | # Qmail directory mailbox |
Guido van Rossum | 9a4d637 | 1998-12-23 22:05:42 +0000 | [diff] [blame] | 218 | |
Barry Warsaw | 81ad67c | 2001-01-31 22:13:15 +0000 | [diff] [blame] | 219 | def __init__(self, dirname, factory=rfc822.Message): |
Fred Drake | dbbf76b | 2000-07-09 16:44:26 +0000 | [diff] [blame] | 220 | self.dirname = dirname |
Barry Warsaw | 81ad67c | 2001-01-31 22:13:15 +0000 | [diff] [blame] | 221 | self.factory = factory |
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 | # check for new mail |
| 224 | newdir = os.path.join(self.dirname, 'new') |
Fred Drake | d9a8dec | 2000-09-22 18:41:50 +0000 | [diff] [blame] | 225 | boxes = [os.path.join(newdir, f) |
| 226 | for f in os.listdir(newdir) if f[0] != '.'] |
Guido van Rossum | 9a4d637 | 1998-12-23 22:05:42 +0000 | [diff] [blame] | 227 | |
Fred Drake | dbbf76b | 2000-07-09 16:44:26 +0000 | [diff] [blame] | 228 | # Now check for current mail in this maildir |
| 229 | curdir = os.path.join(self.dirname, 'cur') |
Fred Drake | d9a8dec | 2000-09-22 18:41:50 +0000 | [diff] [blame] | 230 | boxes += [os.path.join(curdir, f) |
| 231 | for f in os.listdir(curdir) if f[0] != '.'] |
Raymond Hettinger | b5ba8d7 | 2004-02-07 02:16:24 +0000 | [diff] [blame] | 232 | boxes.reverse() |
Fred Drake | de3518e | 2000-10-23 13:37:01 +0000 | [diff] [blame] | 233 | self.boxes = boxes |
| 234 | |
Fred Drake | 72987a4 | 2001-05-02 20:20:53 +0000 | [diff] [blame] | 235 | def __iter__(self): |
Guido van Rossum | 93a696f | 2001-09-13 01:29:13 +0000 | [diff] [blame] | 236 | return iter(self.next, None) |
Fred Drake | 72987a4 | 2001-05-02 20:20:53 +0000 | [diff] [blame] | 237 | |
Fred Drake | dbbf76b | 2000-07-09 16:44:26 +0000 | [diff] [blame] | 238 | def next(self): |
| 239 | if not self.boxes: |
| 240 | return None |
Raymond Hettinger | b5ba8d7 | 2004-02-07 02:16:24 +0000 | [diff] [blame] | 241 | fn = self.boxes.pop() |
Fred Drake | e108a02 | 2000-09-14 14:44:43 +0000 | [diff] [blame] | 242 | fp = open(fn) |
Barry Warsaw | 81ad67c | 2001-01-31 22:13:15 +0000 | [diff] [blame] | 243 | return self.factory(fp) |
Guido van Rossum | 9a4d637 | 1998-12-23 22:05:42 +0000 | [diff] [blame] | 244 | |
Guido van Rossum | 9a4d637 | 1998-12-23 22:05:42 +0000 | [diff] [blame] | 245 | |
Guido van Rossum | fdf58fe | 1997-05-15 14:33:09 +0000 | [diff] [blame] | 246 | class BabylMailbox(_Mailbox): |
Guido van Rossum | 4bf1254 | 2002-09-12 05:08:00 +0000 | [diff] [blame] | 247 | |
Fred Drake | dbbf76b | 2000-07-09 16:44:26 +0000 | [diff] [blame] | 248 | 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 Rossum | fdf58fe | 1997-05-15 14:33:09 +0000 | [diff] [blame] | 255 | |
Fred Drake | dbbf76b | 2000-07-09 16:44:26 +0000 | [diff] [blame] | 256 | 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 Rossum | fdf58fe | 1997-05-15 14:33:09 +0000 | [diff] [blame] | 265 | |
| 266 | |
Guido van Rossum | 6244867 | 1996-09-17 21:33:15 +0000 | [diff] [blame] | 267 | def _test(): |
Fred Drake | dbbf76b | 2000-07-09 16:44:26 +0000 | [diff] [blame] | 268 | import sys |
Jack Jansen | 9715779 | 1995-10-23 13:59:53 +0000 | [diff] [blame] | 269 | |
Fred Drake | dbbf76b | 2000-07-09 16:44:26 +0000 | [diff] [blame] | 270 | args = sys.argv[1:] |
| 271 | if not args: |
| 272 | for key in 'MAILDIR', 'MAIL', 'LOGNAME', 'USER': |
Raymond Hettinger | 54f0222 | 2002-06-01 14:18:47 +0000 | [diff] [blame] | 273 | if key in os.environ: |
Fred Drake | dbbf76b | 2000-07-09 16:44:26 +0000 | [diff] [blame] | 274 | mbox = os.environ[key] |
| 275 | break |
Fred Drake | 13a2c27 | 2000-02-10 17:17:14 +0000 | [diff] [blame] | 276 | else: |
Fred Drake | dbbf76b | 2000-07-09 16:44:26 +0000 | [diff] [blame] | 277 | 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 Schemenauer | 03f3ee6 | 2002-03-24 01:38:38 +0000 | [diff] [blame] | 284 | if os.path.isfile('/var/mail/' + mbox): |
| 285 | mbox = '/var/mail/' + mbox |
| 286 | else: |
| 287 | mbox = '/usr/mail/' + mbox |
Fred Drake | dbbf76b | 2000-07-09 16:44:26 +0000 | [diff] [blame] | 288 | if os.path.isdir(mbox): |
| 289 | if os.path.isdir(os.path.join(mbox, 'cur')): |
| 290 | mb = Maildir(mbox) |
Fred Drake | 13a2c27 | 2000-02-10 17:17:14 +0000 | [diff] [blame] | 291 | else: |
Fred Drake | dbbf76b | 2000-07-09 16:44:26 +0000 | [diff] [blame] | 292 | mb = MHMailbox(mbox) |
| 293 | else: |
| 294 | fp = open(mbox, 'r') |
Barry Warsaw | ffd05ee | 2002-03-01 22:39:14 +0000 | [diff] [blame] | 295 | mb = PortableUnixMailbox(fp) |
Fred Drake | dbbf76b | 2000-07-09 16:44:26 +0000 | [diff] [blame] | 296 | |
| 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 Drake | de3518e | 2000-10-23 13:37:01 +0000 | [diff] [blame] | 306 | num = int(args[1]) |
Fred Drake | dbbf76b | 2000-07-09 16:44:26 +0000 | [diff] [blame] | 307 | 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 Drake | cc4adf2 | 2000-09-30 23:59:04 +0000 | [diff] [blame] | 317 | print '-%20.20s %20.20s %-30.30s'%(f, d[5:], s) |
Guido van Rossum | 6244867 | 1996-09-17 21:33:15 +0000 | [diff] [blame] | 318 | |
| 319 | |
| 320 | if __name__ == '__main__': |
Fred Drake | dbbf76b | 2000-07-09 16:44:26 +0000 | [diff] [blame] | 321 | _test() |