3 """Classes to handle Unix style, MMDF style, and MH style mailboxes."""
10 def __init__(self
, fp
):
14 def seek(self
, pos
, whence
=0):
15 if whence
==1: # Relative to current position
16 self
.pos
= self
.pos
+ pos
17 if whence
==2: # Relative to file's end
18 self
.pos
= self
.stop
+ pos
19 else: # Default - absolute position
20 self
.pos
= self
.start
+ pos
24 self
.fp
.seek(self
.seekp
)
28 self
.seekp
= self
.fp
.tell()
30 start
= self
.fp
.tell()
32 self
.seekp
= stop
= self
.fp
.tell()
35 return rfc822
.Message(_Subfile(self
.fp
, start
, stop
))
39 def __init__(self
, fp
, start
, stop
):
45 def read(self
, length
= None):
46 if self
.pos
>= self
.stop
:
48 remaining
= self
.stop
- self
.pos
49 if length
is None or length
< 0:
51 elif length
> remaining
:
53 self
.fp
.seek(self
.pos
)
54 data
= self
.fp
.read(length
)
55 self
.pos
= self
.fp
.tell()
58 def readline(self
, length
= None):
59 if self
.pos
>= self
.stop
:
62 length
= self
.stop
- self
.pos
63 self
.fp
.seek(self
.pos
)
64 data
= self
.fp
.readline(length
)
65 self
.pos
= self
.fp
.tell()
68 def readlines(self
, sizehint
= -1):
71 line
= self
.readline()
76 sizehint
= sizehint
- len(line
)
82 return self
.pos
- self
.start
84 def seek(self
, pos
, whence
=0):
86 self
.pos
= self
.start
+ pos
88 self
.pos
= self
.pos
+ pos
90 self
.pos
= self
.stop
+ pos
96 class UnixMailbox(_Mailbox
):
97 def _search_start(self
):
100 line
= self
.fp
.readline()
103 if line
[:5] == 'From ' and self
._isrealfromline
(line
):
107 def _search_end(self
):
108 self
.fp
.readline() # Throw away header line
111 line
= self
.fp
.readline()
114 if line
[:5] == 'From ' and self
._isrealfromline
(line
):
118 # An overridable mechanism to test for From-line-ness.
119 # You can either specify a different regular expression
120 # or define a whole new _isrealfromline() method.
121 # Note that this only gets called for lines starting with
122 # the 5 characters "From ".
124 _fromlinepattern
= r
"From \s*[^\s]+\s+\w\w\w\s+\w\w\w\s+\d?\d\s+" \
125 r
"\d?\d:\d\d(:\d\d)?(\s+[^\s]+)?\s+\d\d\d\d\s*$"
128 def _isrealfromline(self
, line
):
131 self
._regexp
= re
.compile(self
._fromlinepattern
)
132 return self
._regexp
.match(line
)
135 class MmdfMailbox(_Mailbox
):
136 def _search_start(self
):
138 line
= self
.fp
.readline()
141 if line
[:5] == '\001\001\001\001\n':
144 def _search_end(self
):
147 line
= self
.fp
.readline()
150 if line
== '\001\001\001\001\n':
156 def __init__(self
, dirname
):
158 pat
= re
.compile('^[1-9][0-9]*$')
159 self
.dirname
= dirname
160 # the three following lines could be combined into:
161 # list = map(long, filter(pat.match, os.listdir(self.dirname)))
162 list = os
.listdir(self
.dirname
)
163 list = filter(pat
.match
, list)
164 list = map(long, list)
166 # This only works in Python 1.6 or later;
167 # before that str() added 'L':
168 self
.boxes
= map(str, list)
175 fp
= open(os
.path
.join(self
.dirname
, fn
))
176 return rfc822
.Message(fp
)
180 # Qmail directory mailbox
182 def __init__(self
, dirname
):
184 self
.dirname
= dirname
187 newdir
= os
.path
.join(self
.dirname
, 'new')
188 boxes
= [os
.path
.join(newdir
, f
)
189 for f
in os
.listdir(newdir
) if f
[0] != '.']
191 # Now check for current mail in this maildir
192 curdir
= os
.path
.join(self
.dirname
, 'cur')
193 boxes
+= [os
.path
.join(curdir
, f
)
194 for f
in os
.listdir(curdir
) if f
[0] != '.']
202 return rfc822
.Message(fp
)
205 class BabylMailbox(_Mailbox
):
206 def _search_start(self
):
208 line
= self
.fp
.readline()
211 if line
== '*** EOOH ***\n':
214 def _search_end(self
):
217 line
= self
.fp
.readline()
220 if line
== '\037\014\n':
233 for key
in 'MAILDIR', 'MAIL', 'LOGNAME', 'USER':
234 if os
.environ
.has_key(key
):
235 mbox
= os
.environ
[key
]
238 print "$MAIL, $LOGNAME nor $USER set -- who are you?"
243 mbox
= os
.environ
['HOME'] + '/Mail/' + mbox
[1:]
244 elif not '/' in mbox
:
245 mbox
= '/usr/mail/' + mbox
246 if os
.path
.isdir(mbox
):
247 if os
.path
.isdir(os
.path
.join(mbox
, 'cur')):
264 num
= string
.atoi(args
[1])
265 print 'Message %d body:'%num
268 sys
.stdout
.write(msg
.fp
.read())
270 print 'Mailbox',mbox
,'has',len(msgs
),'messages:'
272 f
= msg
.getheader('from') or ""
273 s
= msg
.getheader('subject') or ""
274 d
= msg
.getheader('date') or ""
275 print '-%20.20s %20.20s %-30.30s'%(f
, d
[5:], s
)
278 if __name__
== '__main__':