3 """Classes to handle Unix style, MMDF style, and MH style mailboxes."""
11 def __init__(self
, fp
):
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
25 self
.fp
.seek(self
.seekp
)
29 self
.seekp
= self
.fp
.tell()
31 start
= self
.fp
.tell()
33 self
.seekp
= stop
= self
.fp
.tell()
36 return rfc822
.Message(_Subfile(self
.fp
, start
, stop
))
40 def __init__(self
, fp
, start
, stop
):
46 def read(self
, length
= None):
47 if self
.pos
>= self
.stop
:
50 length
= self
.stop
- self
.pos
51 self
.fp
.seek(self
.pos
)
52 self
.pos
= self
.pos
+ length
53 return self
.fp
.read(length
)
55 def readline(self
, length
= None):
56 if self
.pos
>= self
.stop
:
59 length
= self
.stop
- self
.pos
60 self
.fp
.seek(self
.pos
)
61 data
= self
.fp
.readline(length
)
62 if len(data
) < length
:
64 self
.pos
= self
.pos
+ length
68 return self
.pos
- self
.start
70 def seek(self
, pos
, whence
=0):
72 self
.pos
= self
.start
+ pos
74 self
.pos
= self
.pos
+ pos
76 self
.pos
= self
.stop
+ pos
81 class UnixMailbox(_Mailbox
):
83 def _search_start(self
):
85 line
= self
.fp
.readline()
88 if line
[:5] == 'From ' and self
._isrealfromline
(line
):
91 def _search_end(self
):
94 line
= self
.fp
.readline()
97 if line
[:5] == 'From ' and self
._isrealfromline
(line
):
101 # An overridable mechanism to test for From-line-ness.
102 # You can either specify a different regular expression
103 # or define a whole new _isrealfromline() method.
104 # Note that this only gets called for lines starting with
105 # the 5 characters "From ".
107 _fromlinepattern
= r
"From \s*[^\s]+\s+\w\w\w\s+\w\w\w\s+\d?\d\s+" \
108 r
"\d?\d:\d\d:\d\d(\s+[^\s]+)?\s+\d\d\d\d\s*$"
111 def _isrealfromline(self
, line
):
114 self
._regexp
= re
.compile(self
._fromlinepattern
)
115 return self
._regexp
.match(line
)
117 class MmdfMailbox(_Mailbox
):
119 def _search_start(self
):
121 line
= self
.fp
.readline()
124 if line
[:5] == '\001\001\001\001\n':
127 def _search_end(self
):
130 line
= self
.fp
.readline()
133 if line
== '\001\001\001\001\n':
139 def __init__(self
, dirname
):
141 pat
= re
.compile('^[0-9][0-9]*$')
142 self
.dirname
= dirname
143 files
= os
.listdir(self
.dirname
)
154 fp
= open(os
.path
.join(self
.dirname
, fn
))
155 return rfc822
.Message(fp
)
158 class BabylMailbox(_Mailbox
):
160 def _search_start(self
):
162 line
= self
.fp
.readline()
165 if line
== '*** EOOH ***\n':
168 def _search_end(self
):
171 line
= self
.fp
.readline()
174 if line
== '\037\014\n':
187 for key
in 'MAIL', 'LOGNAME', 'USER':
188 if os
.environ
.has_key(key
):
189 mbox
= os
.environ
[key
]
192 print "$MAIL, $LOGNAME nor $USER set -- who are you?"
197 mbox
= os
.environ
['HOME'] + '/Mail/' + mbox
[1:]
198 elif not '/' in mbox
:
199 mbox
= '/usr/mail/' + mbox
200 if os
.path
.isdir(mbox
):
214 num
= string
.atoi(args
[1])
215 print 'Message %d body:'%num
218 sys
.stdout
.write(msg
.fp
.read())
220 print 'Mailbox',mbox
,'has',len(msgs
),'messages:'
222 f
= msg
.getheader('from') or ""
223 s
= msg
.getheader('subject') or ""
224 d
= msg
.getheader('date') or ""
225 print '%20.20s %18.18s %-30.30s'%(f
, d
[5:], s
)
228 if __name__
== '__main__':