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