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
:
49 remaining
= self
.stop
- self
.pos
50 if length
is None or length
< 0:
52 elif length
> remaining
:
54 self
.fp
.seek(self
.pos
)
55 data
= self
.fp
.read(length
)
56 self
.pos
= self
.fp
.tell()
59 def readline(self
, length
= None):
60 if self
.pos
>= self
.stop
:
63 length
= self
.stop
- self
.pos
64 self
.fp
.seek(self
.pos
)
65 data
= self
.fp
.readline(length
)
66 self
.pos
= self
.fp
.tell()
69 def readlines(self
, sizehint
= -1):
72 line
= self
.readline()
77 sizehint
= sizehint
- len(line
)
83 return self
.pos
- self
.start
85 def seek(self
, pos
, whence
=0):
87 self
.pos
= self
.start
+ pos
89 self
.pos
= self
.pos
+ pos
91 self
.pos
= self
.stop
+ pos
96 class UnixMailbox(_Mailbox
):
98 def _search_start(self
):
100 line
= self
.fp
.readline()
103 if line
[:5] == 'From ' and self
._isrealfromline
(line
):
106 def _search_end(self
):
109 line
= self
.fp
.readline()
112 if line
[:5] == 'From ' and self
._isrealfromline
(line
):
116 # An overridable mechanism to test for From-line-ness.
117 # You can either specify a different regular expression
118 # or define a whole new _isrealfromline() method.
119 # Note that this only gets called for lines starting with
120 # the 5 characters "From ".
122 _fromlinepattern
= r
"From \s*[^\s]+\s+\w\w\w\s+\w\w\w\s+\d?\d\s+" \
123 r
"\d?\d:\d\d(:\d\d)?(\s+[^\s]+)?\s+\d\d\d\d\s*$"
126 def _isrealfromline(self
, line
):
129 self
._regexp
= re
.compile(self
._fromlinepattern
)
130 return self
._regexp
.match(line
)
132 class MmdfMailbox(_Mailbox
):
134 def _search_start(self
):
136 line
= self
.fp
.readline()
139 if line
[:5] == '\001\001\001\001\n':
142 def _search_end(self
):
145 line
= self
.fp
.readline()
148 if line
== '\001\001\001\001\n':
154 def __init__(self
, dirname
):
156 pat
= re
.compile('^[0-9][0-9]*$')
157 self
.dirname
= dirname
158 files
= os
.listdir(self
.dirname
)
169 fp
= open(os
.path
.join(self
.dirname
, fn
))
170 return rfc822
.Message(fp
)
174 # Qmail directory mailbox
176 def __init__(self
, dirname
):
178 self
.dirname
= dirname
182 newdir
= os
.path
.join(self
.dirname
, 'new')
183 for file in os
.listdir(newdir
):
184 if len(string
.split(file, '.')) > 2:
185 self
.boxes
.append(os
.path
.join(newdir
, file))
187 # Now check for current mail in this maildir
188 curdir
= os
.path
.join(self
.dirname
, 'cur')
189 for file in os
.listdir(curdir
):
190 if len(string
.split(file, '.')) > 2:
191 self
.boxes
.append(os
.path
.join(curdir
, file))
198 fp
= open(os
.path
.join(self
.dirname
, fn
))
199 return rfc822
.Message(fp
)
201 class BabylMailbox(_Mailbox
):
203 def _search_start(self
):
205 line
= self
.fp
.readline()
208 if line
== '*** EOOH ***\n':
211 def _search_end(self
):
214 line
= self
.fp
.readline()
217 if line
== '\037\014\n':
230 for key
in 'MAILDIR', 'MAIL', 'LOGNAME', 'USER':
231 if os
.environ
.has_key(key
):
232 mbox
= os
.environ
[key
]
235 print "$MAIL, $LOGNAME nor $USER set -- who are you?"
240 mbox
= os
.environ
['HOME'] + '/Mail/' + mbox
[1:]
241 elif not '/' in mbox
:
242 mbox
= '/usr/mail/' + mbox
243 if os
.path
.isdir(mbox
):
244 if os
.path
.isdir(os
.path
.join(mbox
, 'cur')):
260 num
= string
.atoi(args
[1])
261 print 'Message %d body:'%num
264 sys
.stdout
.write(msg
.fp
.read())
266 print 'Mailbox',mbox
,'has',len(msgs
),'messages:'
268 f
= msg
.getheader('from') or ""
269 s
= msg
.getheader('subject') or ""
270 d
= msg
.getheader('date') or ""
271 print '%20.20s %18.18s %-30.30s'%(f
, d
[5:], s
)
274 if __name__
== '__main__':