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 |
| 8 | import regex |
Guido van Rossum | c7b6882 | 1994-04-28 09:53:33 +0000 | [diff] [blame] | 9 | |
| 10 | class _Mailbox: |
| 11 | def __init__(self, fp): |
| 12 | self.fp = fp |
| 13 | self.seekp = 0 |
| 14 | |
Guido van Rossum | 44a4d59 | 1996-01-25 18:40:41 +0000 | [diff] [blame] | 15 | def seek(self, pos, whence=0): |
| 16 | if whence==1: # Relative to current position |
| 17 | self.pos = self.pos + pos |
| 18 | if whence==2: # Relative to file's end |
| 19 | self.pos = self.stop + pos |
| 20 | else: # Default - absolute position |
| 21 | self.pos = self.start + pos |
Guido van Rossum | c7b6882 | 1994-04-28 09:53:33 +0000 | [diff] [blame] | 22 | |
| 23 | def next(self): |
| 24 | while 1: |
| 25 | self.fp.seek(self.seekp) |
| 26 | try: |
| 27 | self._search_start() |
| 28 | except EOFError: |
| 29 | self.seekp = self.fp.tell() |
| 30 | return None |
| 31 | start = self.fp.tell() |
| 32 | self._search_end() |
| 33 | self.seekp = stop = self.fp.tell() |
| 34 | if start <> stop: |
| 35 | break |
| 36 | return rfc822.Message(_Subfile(self.fp, start, stop)) |
| 37 | |
| 38 | class _Subfile: |
| 39 | def __init__(self, fp, start, stop): |
| 40 | self.fp = fp |
| 41 | self.start = start |
| 42 | self.stop = stop |
| 43 | self.pos = self.start |
| 44 | |
Guido van Rossum | 2922c6d | 1994-05-06 14:28:19 +0000 | [diff] [blame] | 45 | def read(self, length = None): |
Guido van Rossum | c7b6882 | 1994-04-28 09:53:33 +0000 | [diff] [blame] | 46 | if self.pos >= self.stop: |
| 47 | return '' |
Guido van Rossum | 2922c6d | 1994-05-06 14:28:19 +0000 | [diff] [blame] | 48 | if length is None: |
Guido van Rossum | c7b6882 | 1994-04-28 09:53:33 +0000 | [diff] [blame] | 49 | length = self.stop - self.pos |
Guido van Rossum | c7b6882 | 1994-04-28 09:53:33 +0000 | [diff] [blame] | 50 | self.fp.seek(self.pos) |
| 51 | self.pos = self.pos + length |
| 52 | return self.fp.read(length) |
| 53 | |
Guido van Rossum | 2922c6d | 1994-05-06 14:28:19 +0000 | [diff] [blame] | 54 | def readline(self, length = None): |
Guido van Rossum | c7b6882 | 1994-04-28 09:53:33 +0000 | [diff] [blame] | 55 | if self.pos >= self.stop: |
| 56 | return '' |
Guido van Rossum | 2922c6d | 1994-05-06 14:28:19 +0000 | [diff] [blame] | 57 | if length is None: |
Guido van Rossum | c7b6882 | 1994-04-28 09:53:33 +0000 | [diff] [blame] | 58 | length = self.stop - self.pos |
Guido van Rossum | c7b6882 | 1994-04-28 09:53:33 +0000 | [diff] [blame] | 59 | self.fp.seek(self.pos) |
| 60 | data = self.fp.readline(length) |
| 61 | if len(data) < length: |
| 62 | length = len(data) |
| 63 | self.pos = self.pos + length |
| 64 | return data |
| 65 | |
| 66 | def tell(self): |
| 67 | return self.pos - self.start |
| 68 | |
Jack Jansen | 9715779 | 1995-10-23 13:59:53 +0000 | [diff] [blame] | 69 | def seek(self, pos, whence=0): |
| 70 | if whence == 0: |
| 71 | self.pos = self.start + pos |
| 72 | elif whence == 1: |
| 73 | self.pos = self.pos + pos |
| 74 | elif whence == 2: |
| 75 | self.pos = self.stop + pos |
Guido van Rossum | c7b6882 | 1994-04-28 09:53:33 +0000 | [diff] [blame] | 76 | |
| 77 | def close(self): |
| 78 | pass |
| 79 | |
| 80 | class UnixMailbox(_Mailbox): |
| 81 | def _search_start(self): |
| 82 | while 1: |
| 83 | line = self.fp.readline() |
| 84 | if not line: |
| 85 | raise EOFError |
| 86 | if line[:5] == 'From ': |
| 87 | return |
| 88 | |
| 89 | def _search_end(self): |
| 90 | while 1: |
| 91 | pos = self.fp.tell() |
| 92 | line = self.fp.readline() |
| 93 | if not line: |
| 94 | return |
| 95 | if line[:5] == 'From ': |
| 96 | self.fp.seek(pos) |
| 97 | return |
| 98 | |
| 99 | class MmdfMailbox(_Mailbox): |
| 100 | def _search_start(self): |
| 101 | while 1: |
| 102 | line = self.fp.readline() |
| 103 | if not line: |
| 104 | raise EOFError |
| 105 | if line[:5] == '\001\001\001\001\n': |
| 106 | return |
| 107 | |
| 108 | def _search_end(self): |
| 109 | while 1: |
| 110 | pos = self.fp.tell() |
| 111 | line = self.fp.readline() |
| 112 | if not line: |
| 113 | return |
| 114 | if line == '\001\001\001\001\n': |
| 115 | self.fp.seek(pos) |
| 116 | return |
| 117 | |
Jack Jansen | 9715779 | 1995-10-23 13:59:53 +0000 | [diff] [blame] | 118 | class MHMailbox: |
| 119 | def __init__(self, dirname): |
| 120 | pat = regex.compile('^[0-9][0-9]*$') |
| 121 | self.dirname = dirname |
| 122 | files = os.listdir(self.dirname) |
| 123 | self.boxes = [] |
| 124 | for f in files: |
| 125 | if pat.match(f) == len(f): |
| 126 | self.boxes.append(f) |
| 127 | |
| 128 | def next(self): |
| 129 | if not self.boxes: |
| 130 | return None |
| 131 | fn = self.boxes[0] |
| 132 | del self.boxes[0] |
| 133 | fp = open(os.path.join(self.dirname, fn)) |
| 134 | return rfc822.Message(fp) |
| 135 | |
| 136 | |
Guido van Rossum | fdf58fe | 1997-05-15 14:33:09 +0000 | [diff] [blame] | 137 | class BabylMailbox(_Mailbox): |
| 138 | def _search_start(self): |
| 139 | while 1: |
| 140 | line = self.fp.readline() |
| 141 | if not line: |
| 142 | raise EOFError |
| 143 | if line == '*** EOOH ***\n': |
| 144 | return |
| 145 | |
| 146 | def _search_end(self): |
| 147 | while 1: |
| 148 | pos = self.fp.tell() |
| 149 | line = self.fp.readline() |
| 150 | if not line: |
| 151 | return |
| 152 | if line == '\037\014\n': |
| 153 | self.fp.seek(pos) |
| 154 | return |
| 155 | |
| 156 | |
Guido van Rossum | 6244867 | 1996-09-17 21:33:15 +0000 | [diff] [blame] | 157 | def _test(): |
Guido van Rossum | c7b6882 | 1994-04-28 09:53:33 +0000 | [diff] [blame] | 158 | import time |
| 159 | import sys |
| 160 | import string |
Guido van Rossum | 6244867 | 1996-09-17 21:33:15 +0000 | [diff] [blame] | 161 | import os |
Jack Jansen | 9715779 | 1995-10-23 13:59:53 +0000 | [diff] [blame] | 162 | |
Guido van Rossum | 6244867 | 1996-09-17 21:33:15 +0000 | [diff] [blame] | 163 | args = sys.argv[1:] |
| 164 | if not args: |
Guido van Rossum | 6dd8783 | 1997-05-08 23:11:52 +0000 | [diff] [blame] | 165 | for key in 'MAIL', 'LOGNAME', 'USER': |
| 166 | if os.environ.has_key(key): |
| 167 | mbox = os.environ[key] |
| 168 | break |
Guido van Rossum | 6244867 | 1996-09-17 21:33:15 +0000 | [diff] [blame] | 169 | else: |
Guido van Rossum | 6dd8783 | 1997-05-08 23:11:52 +0000 | [diff] [blame] | 170 | print "$MAIL, $LOGNAME nor $USER set -- who are you?" |
Guido van Rossum | 6244867 | 1996-09-17 21:33:15 +0000 | [diff] [blame] | 171 | return |
| 172 | else: |
| 173 | mbox = args[0] |
| 174 | if mbox[:1] == '+': |
| 175 | mbox = os.environ['HOME'] + '/Mail/' + mbox[1:] |
| 176 | elif not '/' in mbox: |
| 177 | mbox = '/usr/mail/' + mbox |
| 178 | if os.path.isdir(mbox): |
| 179 | mb = MHMailbox(mbox) |
| 180 | else: |
| 181 | fp = open(mbox, 'r') |
| 182 | mb = UnixMailbox(fp) |
Jack Jansen | 9715779 | 1995-10-23 13:59:53 +0000 | [diff] [blame] | 183 | |
Guido van Rossum | c7b6882 | 1994-04-28 09:53:33 +0000 | [diff] [blame] | 184 | msgs = [] |
| 185 | while 1: |
| 186 | msg = mb.next() |
| 187 | if not msg: |
| 188 | break |
| 189 | msgs.append(msg) |
Guido van Rossum | 6244867 | 1996-09-17 21:33:15 +0000 | [diff] [blame] | 190 | if len(args) > 1: |
| 191 | num = string.atoi(args[1]) |
Guido van Rossum | c7b6882 | 1994-04-28 09:53:33 +0000 | [diff] [blame] | 192 | print 'Message %d body:'%num |
| 193 | msg = msgs[num-1] |
| 194 | msg.rewindbody() |
| 195 | sys.stdout.write(msg.fp.read()) |
Guido van Rossum | 6244867 | 1996-09-17 21:33:15 +0000 | [diff] [blame] | 196 | else: |
| 197 | print 'Mailbox',mbox,'has',len(msgs),'messages:' |
| 198 | for msg in msgs: |
| 199 | f = msg.getheader('from') |
| 200 | s = msg.getheader('subject') |
| 201 | d = (msg.getheader('date')) |
| 202 | print '%20.20s %18.18s %-30.30s'%(f, d[5:], s) |
| 203 | |
| 204 | |
| 205 | if __name__ == '__main__': |
| 206 | _test() |