blob: 187603e19f5911009f19ec062bba5c95e2a66663 [file] [log] [blame]
Guido van Rossum56013131994-06-23 12:06:02 +00001# MH interface -- purely object-oriented (well, almost)
2#
3# Executive summary:
4#
5# import mhlib
6#
7# mh = mhlib.MH() # use default mailbox directory and profile
8# mh = mhlib.MH(mailbox) # override mailbox location (default from profile)
9# mh = mhlib.MH(mailbox, profile) # override mailbox and profile
10#
11# mh.error(format, ...) # print error message -- can be overridden
12# s = mh.getprofile(key) # profile entry (None if not set)
13# path = mh.getpath() # mailbox pathname
14# name = mh.getcontext() # name of current folder
15#
16# list = mh.listfolders() # names of top-level folders
17# list = mh.listallfolders() # names of all folders, including subfolders
18# list = mh.listsubfolders(name) # direct subfolders of given folder
19# list = mh.listallsubfolders(name) # all subfolders of given folder
20#
21# mh.makefolder(name) # create new folder
22# mh.deletefolder(name) # delete folder -- must have no subfolders
23#
24# f = mh.openfolder(name) # new open folder object
25#
26# f.error(format, ...) # same as mh.error(format, ...)
27# path = f.getfullname() # folder's full pathname
28# path = f.getsequencesfilename() # full pathname of folder's sequences file
29# path = f.getmessagefilename(n) # full pathname of message n in folder
30#
31# list = f.listmessages() # list of messages in folder (as numbers)
32# n = f.getcurrent() # get current message
33# f.setcurrent(n) # set current message
34#
35# dict = f.getsequences() # dictionary of sequences in folder {name: list}
36# f.putsequences(dict) # write sequences back to folder
37#
38# m = f.openmessage(n) # new open message object (costs a file descriptor)
39# s = m.getheadertext() # text of message's headers
40# s = m.getheadertext(pred) # text of message's headers, filtered by pred
41# s = m.getbodytext() # text of message's body, decoded
42# s = m.getbodytext(0) # text of message's body, not decoded
43#
44# XXX To do, functionality:
45# - remove, refile messages
46# - annotate messages
47# - create, send messages
48#
49# XXX To do, orgaanization:
50# - move IntSet to separate file
51# - move most Message functionality to module mimetools
52
53
54# Customizable defaults
55
56MH_PROFILE = '~/.mh_profile'
57PATH = '~/Mail'
58MH_SEQUENCES = '.mh_sequences'
59FOLDER_PROTECT = 0700
60
61
62# Imported modules
63
64import os
65from stat import ST_NLINK
66import regex
67import string
68import mimetools
69import multifile
70
71
72# Exported constants
73
74Error = 'mhlib.Error'
75
76
77# Class representing a particular collection of folders.
78# Optional constructor arguments are the pathname for the directory
79# containing the collection, and the MH profile to use.
80# If either is omitted or empty a default is used; the default
81# directory is taken from the MH profile if it is specified there.
82
83class MH:
84
85 # Constructor
86 def __init__(self, path = None, profile = None):
87 if not profile: profile = MH_PROFILE
88 self.profile = os.path.expanduser(profile)
89 if not path: path = self.getprofile('Path')
90 if not path: path = PATH
91 if not os.path.isabs(path) and path[0] != '~':
92 path = os.path.join('~', path)
93 path = os.path.expanduser(path)
94 if not os.path.isdir(path): raise Error, 'MH() path not found'
95 self.path = path
96
97 # String representation
98 def __repr__(self):
99 return 'MH(%s, %s)' % (`self.path`, `self.profile`)
100
101 # Routine to print an error. May be overridden by a derived class
102 def error(self, msg, *args):
103 sys.stderr.write('MH error: %\n' % (msg % args))
104
105 # Return a profile entry, None if not found
106 def getprofile(self, key):
107 return pickline(self.profile, key)
108
109 # Return the path (the name of the collection's directory)
110 def getpath(self):
111 return self.path
112
113 # Return the name of the current folder
114 def getcontext(self):
115 context = pickline(os.path.join(self.getpath(), 'context'),
116 'Current-Folder')
117 if not context: context = 'inbox'
118 return context
119
120 # Return the names of the top-level folders
121 def listfolders(self):
122 folders = []
123 path = self.getpath()
124 for name in os.listdir(path):
125 if name in (os.curdir, os.pardir): continue
126 fullname = os.path.join(path, name)
127 if os.path.isdir(fullname):
128 folders.append(name)
129 folders.sort()
130 return folders
131
132 # Return the names of the subfolders in a given folder
133 # (prefixed with the given folder name)
134 def listsubfolders(self, name):
135 fullname = os.path.join(self.path, name)
136 # Get the link count so we can avoid listing folders
137 # that have no subfolders.
138 st = os.stat(fullname)
139 nlinks = st[ST_NLINK]
140 if nlinks <= 2:
141 return []
142 subfolders = []
143 subnames = os.listdir(fullname)
144 for subname in subnames:
145 if subname in (os.curdir, os.pardir): continue
146 fullsubname = os.path.join(fullname, subname)
147 if os.path.isdir(fullsubname):
148 name_subname = os.path.join(name, subname)
149 subfolders.append(name_subname)
150 # Stop looking for subfolders when
151 # we've seen them all
152 nlinks = nlinks - 1
153 if nlinks <= 2:
154 break
155 subfolders.sort()
156 return subfolders
157
158 # Return the names of all folders, including subfolders, recursively
159 def listallfolders(self):
160 return self.listallsubfolders('')
161
162 # Return the names of subfolders in a given folder, recursively
163 def listallsubfolders(self, name):
164 fullname = os.path.join(self.path, name)
165 # Get the link count so we can avoid listing folders
166 # that have no subfolders.
167 st = os.stat(fullname)
168 nlinks = st[ST_NLINK]
169 if nlinks <= 2:
170 return []
171 subfolders = []
172 subnames = os.listdir(fullname)
173 for subname in subnames:
174 if subname in (os.curdir, os.pardir): continue
175 if subname[0] == ',' or isnumeric(subname): continue
176 fullsubname = os.path.join(fullname, subname)
177 if os.path.isdir(fullsubname):
178 name_subname = os.path.join(name, subname)
179 subfolders.append(name_subname)
180 if not os.path.islink(fullsubname):
181 subsubfolders = self.listallsubfolders(
182 name_subname)
183 subfolders = subfolders + subsubfolders
184 # Stop looking for subfolders when
185 # we've seen them all
186 nlinks = nlinks - 1
187 if nlinks <= 2:
188 break
189 subfolders.sort()
190 return subfolders
191
192 # Return a new Folder object for the named folder
193 def openfolder(self, name):
194 return Folder(self, name)
195
196 # Create a new folder. This raises os.error if the folder
197 # cannot be created
198 def makefolder(self, name):
199 protect = pickline(self.profile, 'Folder-Protect')
200 if protect and isnumeric(protect):
201 mode = eval('0' + protect)
202 else:
203 mode = FOLDER_PROTECT
204 os.mkdir(os.path.join(self.getpath(), name), mode)
205
206 # Delete a folder. This removes files in the folder but not
207 # subdirectories. If deleting the folder itself fails it
208 # raises os.error
209 def deletefolder(self, name):
210 fullname = os.path.join(self.getpath(), name)
211 for subname in os.listdir(fullname):
212 if subname in (os.curdir, os.pardir): continue
213 fullsubname = os.path.join(fullname, subname)
214 try:
215 os.unlink(fullsubname)
216 except os.error:
217 self.error('%s not deleted, continuing...' %
218 fullsubname)
219 os.rmdir(fullname)
220
221
222# Class representing a particular folder
223
224numericprog = regex.compile('[1-9][0-9]*')
225def isnumeric(str):
226 return numericprog.match(str) == len(str)
227
228class Folder:
229
230 # Constructor
231 def __init__(self, mh, name):
232 self.mh = mh
233 self.name = name
234 if not os.path.isdir(self.getfullname()):
235 raise Error, 'no folder %s' % name
236
237 # String representation
238 def __repr__(self):
239 return 'Folder(%s, %s)' % (`self.mh`, `self.name`)
240
241 # Error message handler
242 def error(self, *args):
243 apply(self.mh.error, args)
244
245 # Return the full pathname of the folder
246 def getfullname(self):
247 return os.path.join(self.mh.path, self.name)
248
249 # Return the full pathname of the folder's sequences file
250 def getsequencesfilename(self):
251 return os.path.join(self.getfullname(), MH_SEQUENCES)
252
253 # Return the full pathname of a message in the folder
254 def getmessagefilename(self, n):
255 return os.path.join(self.getfullname(), str(n))
256
257 # Return list of direct subfolders
258 def listsubfolders(self):
259 return self.mh.listsubfolders(self.name)
260
261 # Return list of all subfolders
262 def listallsubfolders(self):
263 return self.mh.listallsubfolders(self.name)
264
265 # Return the list of messages currently present in the folder.
266 # As a side effect, set self.last to the last message (or 0)
267 def listmessages(self):
268 messages = []
269 for name in os.listdir(self.getfullname()):
270 if isnumeric(name):
271 messages.append(eval(name))
272 messages.sort()
273 if messages:
274 self.last = max(messages)
275 else:
276 self.last = 0
277 return messages
278
279 # Return the set of sequences for the folder
280 def getsequences(self):
281 sequences = {}
282 fullname = self.getsequencesfilename()
283 try:
284 f = open(fullname, 'r')
285 except IOError:
286 return sequences
287 while 1:
288 line = f.readline()
289 if not line: break
290 fields = string.splitfields(line, ':')
291 if len(fields) <> 2:
292 self.error('bad sequence in %s: %s' %
293 (fullname, string.strip(line)))
294 key = string.strip(fields[0])
295 value = IntSet(string.strip(fields[1]), ' ').tolist()
296 sequences[key] = value
297 return sequences
298
299 # Write the set of sequences back to the folder
300 def putsequences(self, sequences):
301 fullname = self.getsequencesfilename()
302 f = open(fullname, 'w')
303 for key in sequences.keys():
304 s = IntSet('', ' ')
305 s.fromlist(sequences[key])
306 f.write('%s: %s\n' % (key, s.tostring()))
307
308 # Return the current message. Raise KeyError when there is none
309 def getcurrent(self):
310 return min(self.getsequences()['cur'])
311
312 # Set the current message
313 def setcurrent(self, n):
314 updateline(self.getsequencesfilename(), 'cur', str(n), 0)
315
316 # Open a message -- returns a mimetools.Message object
317 def openmessage(self, n):
318 path = self.getmessagefilename(n)
319 return Message(self, n)
320
321 # Remove one or more messages -- may raise os.error
322 def removemessages(self, list):
323 errors = []
324 deleted = []
325 for n in list:
326 path = self.getmessagefilename(n)
327 commapath = self.getmessagefilename(',' + str(n))
328 try:
329 os.unlink(commapath)
330 except os.error:
331 pass
332 try:
333 os.rename(path, commapath)
334 except os.error, msg:
335 errors.append(msg)
336 else:
337 deleted.append(n)
338 if deleted:
339 self.removefromallsequences(deleted)
340 if errors:
341 if len(errors) == 1:
342 raise os.error, errors[0]
343 else:
344 raise os.error, ('multiple errors:', errors)
345
346 # Refile one or more messages -- may raise os.error.
347 # 'tofolder' is an open folder object
348 def refilemessages(self, list, tofolder):
349 errors = []
350 refiled = []
351 for n in list:
352 ton = tofolder.getlast() + 1
353 path = self.getmessagefilename(n)
354 topath = tofolder.getmessagefilename(ton)
355 try:
356 os.rename(path, topath)
357 # XXX What if it's on a different filesystem?
358 except os.error, msg:
359 errors.append(msg)
360 else:
361 tofolder.setlast(ton)
362 refiled.append(n)
363 if refiled:
364 self.removefromallsequences(refiled)
365 if errors:
366 if len(errors) == 1:
367 raise os.error, errors[0]
368 else:
369 raise os.error, ('multiple errors:', errors)
370
371 # Remove one or more messages from all sequeuces (including last)
372 def removefromallsequences(self, list):
373 if hasattr(self, 'last') and self.last in list:
374 del self.last
375 sequences = self.getsequences()
376 changed = 0
377 for name, seq in sequences.items():
378 for n in list:
379 if n in seq:
380 seq.remove(n)
381 changed = 1
382 if not seq:
383 del sequences[name]
384 if changed:
385 self.putsequences()
386
387 # Return the last message number
388 def getlast(self):
389 if not hasattr(self, 'last'):
390 messages = self.listmessages()
391 return self.last
392
393 # Set the last message number
394 def setlast(self, last):
395 if last is None:
396 if hasattr(self, 'last'):
397 del self.last
398 else:
399 self.last = last
400
401class Message(mimetools.Message):
402
403 # Constructor
404 def __init__(self, f, n, fp = None):
405 self.folder = f
406 self.number = n
407 if not fp:
408 path = f.getmessagefilename(n)
409 fp = open(path, 'r')
410 mimetools.Message.__init__(self, fp)
411
412 # String representation
413 def __repr__(self):
414 return 'Message(%s, %s)' % (repr(self.folder), self.number)
415
416 # Return the message's header text as a string. If an
417 # argument is specified, it is used as a filter predicate to
418 # decide which headers to return (its argument is the header
419 # name converted to lower case).
420 def getheadertext(self, pred = None):
421 if not pred:
422 return string.joinfields(self.headers, '')
423 headers = []
424 hit = 0
425 for line in self.headers:
426 if line[0] not in string.whitespace:
427 i = string.find(line, ':')
428 if i > 0:
429 hit = pred(string.lower(line[:i]))
430 if hit: headers.append(line)
431 return string.joinfields(headers, '')
432
433 # Return the message's body text as string. This undoes a
434 # Content-Transfer-Encoding, but does not interpret other MIME
435 # features (e.g. multipart messages). To suppress to
436 # decoding, pass a 0 as argument
437 def getbodytext(self, decode = 1):
438 self.fp.seek(self.startofbody)
439 encoding = self.getencoding()
440 if not decode or encoding in ('7bit', '8bit', 'binary'):
441 return self.fp.read()
442 from StringIO import StringIO
443 output = StringIO()
444 mimetools.decode(self.fp, output, encoding)
445 return output.getvalue()
446
447 # Only for multipart messages: return the message's body as a
448 # list of SubMessage objects. Each submessage object behaves
449 # (almost) as a Message object.
450 def getbodyparts(self):
451 if self.getmaintype() != 'multipart':
452 raise Error, \
453 'Content-Type is not multipart/*'
454 bdry = self.getparam('boundary')
455 if not bdry:
456 raise Error, 'multipart/* without boundary param'
457 self.fp.seek(self.startofbody)
458 mf = multifile.MultiFile(self.fp)
459 mf.push(bdry)
460 parts = []
461 while mf.next():
462 n = str(self.number) + '.' + `1 + len(parts)`
463 part = SubMessage(self.folder, n, mf)
464 parts.append(part)
465 mf.pop()
466 return parts
467
468 # Return body, either a string or a list of messages
469 def getbody(self):
470 if self.getmaintype() == 'multipart':
471 return self.getbodyparts()
472 else:
473 return self.getbodytext()
474
475
476class SubMessage(Message):
477
478 # Constructor
479 def __init__(self, f, n, fp):
480 Message.__init__(self, f, n, fp)
481 if self.getmaintype() == 'multipart':
482 self.body = Message.getbodyparts(self)
483 else:
484 self.body = Message.getbodytext(self)
485 # XXX If this is big, should remember file pointers
486
487 # String representation
488 def __repr__(self):
489 f, n, fp = self.folder, self.number, self.fp
490 return 'SubMessage(%s, %s, %s)' % (f, n, fp)
491
492 def getbodytext(self):
493 if type(self.body) == type(''):
494 return self.body
495
496 def getbodyparts(self):
497 if type(self.body) == type([]):
498 return self.body
499
500 def getbody(self):
501 return self.body
502
503
504# Class implementing sets of integers.
505#
506# This is an efficient representation for sets consisting of several
507# continuous ranges, e.g. 1-100,200-400,402-1000 is represented
508# internally as a list of three pairs: [(1,100), (200,400),
509# (402,1000)]. The internal representation is always kept normalized.
510#
511# The constructor has up to three arguments:
512# - the string used to initialize the set (default ''),
513# - the separator between ranges (default ',')
514# - the separator between begin and end of a range (default '-')
515# The separators may be regular expressions and should be different.
516#
517# The tostring() function yields a string that can be passed to another
518# IntSet constructor; __repr__() is a valid IntSet constructor itself.
519#
520# XXX The default begin/end separator means that negative numbers are
521# not supported very well.
522#
523# XXX There are currently no operations to remove set elements.
524
525class IntSet:
526
527 def __init__(self, data = None, sep = ',', rng = '-'):
528 self.pairs = []
529 self.sep = sep
530 self.rng = rng
531 if data: self.fromstring(data)
532
533 def reset(self):
534 self.pairs = []
535
536 def __cmp__(self, other):
537 return cmp(self.pairs, other.pairs)
538
539 def __hash__(self):
540 return hash(self.pairs)
541
542 def __repr__(self):
543 return 'IntSet(%s, %s, %s)' % (`self.tostring()`,
544 `self.sep`, `self.rng`)
545
546 def normalize(self):
547 self.pairs.sort()
548 i = 1
549 while i < len(self.pairs):
550 alo, ahi = self.pairs[i-1]
551 blo, bhi = self.pairs[i]
552 if ahi >= blo-1:
553 self.pairs[i-1:i+1] = [
554 (alo, max(ahi, bhi))]
555 else:
556 i = i+1
557
558 def tostring(self):
559 s = ''
560 for lo, hi in self.pairs:
561 if lo == hi: t = `lo`
562 else: t = `lo` + self.rng + `hi`
563 if s: s = s + (self.sep + t)
564 else: s = t
565 return s
566
567 def tolist(self):
568 l = []
569 for lo, hi in self.pairs:
570 m = range(lo, hi+1)
571 l = l + m
572 return l
573
574 def fromlist(self, list):
575 for i in list:
576 self.append(i)
577
578 def clone(self):
579 new = IntSet()
580 new.pairs = self.pairs[:]
581 return new
582
583 def min(self):
584 return self.pairs[0][0]
585
586 def max(self):
587 return self.pairs[-1][-1]
588
589 def contains(self, x):
590 for lo, hi in self.pairs:
591 if lo <= x <= hi: return 1
592 return 0
593
594 def append(self, x):
595 for i in range(len(self.pairs)):
596 lo, hi = self.pairs[i]
597 if x < lo: # Need to insert before
598 if x+1 == lo:
599 self.pairs[i] = (x, hi)
600 else:
601 self.pairs.insert(i, (x, x))
602 if i > 0 and x-1 == self.pairs[i-1][1]:
603 # Merge with previous
604 self.pairs[i-1:i+1] = [
605 (self.pairs[i-1][0],
606 self.pairs[i][1])
607 ]
608 return
609 if x <= hi: # Already in set
610 return
611 i = len(self.pairs) - 1
612 if i >= 0:
613 lo, hi = self.pairs[i]
614 if x-1 == hi:
615 self.pairs[i] = lo, x
616 return
617 self.pairs.append((x, x))
618
619 def addpair(self, xlo, xhi):
620 if xlo > xhi: return
621 self.pairs.append((xlo, xhi))
622 self.normalize()
623
624 def fromstring(self, data):
625 import string, regsub
626 new = []
627 for part in regsub.split(data, self.sep):
628 list = []
629 for subp in regsub.split(part, self.rng):
630 s = string.strip(subp)
631 list.append(string.atoi(s))
632 if len(list) == 1:
633 new.append((list[0], list[0]))
634 elif len(list) == 2 and list[0] <= list[1]:
635 new.append((list[0], list[1]))
636 else:
637 raise ValueError, 'bad data passed to IntSet'
638 self.pairs = self.pairs + new
639 self.normalize()
640
641
642# Subroutines to read/write entries in .mh_profile and .mh_sequences
643
644def pickline(file, key, casefold = 1):
645 try:
646 f = open(file, 'r')
647 except IOError:
648 return None
649 pat = key + ':'
650 if casefold:
651 prog = regex.compile(pat, regex.casefold)
652 else:
653 prog = regex.compile(pat)
654 while 1:
655 line = f.readline()
656 if not line: break
657 if prog.match(line) == len(line):
658 text = line[len(key)+1:]
659 while 1:
660 line = f.readline()
661 if not line or \
662 line[0] not in string.whitespace:
663 break
664 text = text + line
665 return string.strip(text)
666 return None
667
668def updateline(file, key, value, casefold = 1):
669 try:
670 f = open(file, 'r')
671 lines = f.readlines()
672 f.close()
673 except IOError:
674 lines = []
675 pat = key + ':\(.*\)\n'
676 if casefold:
677 prog = regex.compile(pat, regex.casefold)
678 else:
679 prog = regex.compile(pat)
680 if value is None:
681 newline = None
682 else:
683 newline = '%s: %s' % (key, value)
684 for i in range(len(lines)):
685 line = lines[i]
686 if prog.match(line) == len(line):
687 if newline is None:
688 del lines[i]
689 else:
690 lines[i] = newline
691 break
692 else:
693 if newline is not None:
694 lines.append(newline)
695 f = open(tempfile, 'w')
696 for line in lines:
697 f.write(line)
698 f.close()
699
700
701# Test program
702
703def test():
704 global mh, f
705 os.system('rm -rf $HOME/Mail/@test')
706 mh = MH()
707 def do(s): print s; print eval(s)
708 do('mh.listfolders()')
709 do('mh.listallfolders()')
710 testfolders = ['@test', '@test/test1', '@test/test2',
711 '@test/test1/test11', '@test/test1/test12',
712 '@test/test1/test11/test111']
713 for t in testfolders: do('mh.makefolder(%s)' % `t`)
714 do('mh.listsubfolders(\'@test\')')
715 do('mh.listallsubfolders(\'@test\')')
716 f = mh.openfolder('@test')
717 do('f.listsubfolders()')
718 do('f.listallsubfolders()')
719 do('f.getsequences()')
720 seqs = f.getsequences()
721 seqs['foo'] = IntSet('1-10 12-20', ' ').tolist()
722 print seqs
723 f.putsequences(seqs)
724 do('f.getsequences()')
725 testfolders.reverse()
726 for t in testfolders: do('mh.deletefolder(%s)' % `t`)
727 do('mh.getcontext()')
728 context = mh.getcontext()
729 f = mh.openfolder(context)
730 do('f.listmessages()')
731 do('f.getcurrent()')
732
733
734if __name__ == '__main__':
735 test()