blob: 4cdd7d45ea175222c6c8c5e244992a52f29de762 [file] [log] [blame]
Guido van Rossumf06ee5f1996-11-27 19:52:01 +00001#! /usr/bin/env python
Guido van Rossum62448671996-09-17 21:33:15 +00002
Andrew M. Kuchling1da4a942006-04-22 02:32:43 +00003"""Read/write support for Maildir, mbox, MH, Babyl, and MMDF mailboxes."""
Guido van Rossum62448671996-09-17 21:33:15 +00004
Andrew M. Kuchlingb5686da2006-11-09 13:51:14 +00005# Notes for authors of new mailbox subclasses:
6#
7# Remember to fsync() changes to disk before closing a modified file
8# or returning from a flush() method. See functions _sync_flush() and
9# _sync_close().
10
Martin v. Löwis08041d52006-05-04 14:27:52 +000011import sys
Jack Jansen97157791995-10-23 13:59:53 +000012import os
Andrew M. Kuchling1da4a942006-04-22 02:32:43 +000013import time
14import calendar
15import socket
16import errno
17import copy
18import email
Georg Brandl5a096e12007-01-22 19:40:21 +000019import email.message
20import email.generator
Andrew M. Kuchling1da4a942006-04-22 02:32:43 +000021import StringIO
22try:
Andrew MacIntyreafa358f2006-07-23 13:04:00 +000023 if sys.platform == 'os2emx':
24 # OS/2 EMX fcntl() not adequate
25 raise ImportError
Andrew M. Kuchlinga7ee9eb2006-06-26 13:08:24 +000026 import fcntl
Andrew M. Kuchling1da4a942006-04-22 02:32:43 +000027except ImportError:
28 fcntl = None
Guido van Rossumc7b68821994-04-28 09:53:33 +000029
Antoine Pitroub9d49632010-01-04 23:22:44 +000030import warnings
31with warnings.catch_warnings():
32 if sys.py3kwarning:
33 warnings.filterwarnings("ignore", ".*rfc822 has been removed",
34 DeprecationWarning)
35 import rfc822
36
Andrew M. Kuchling1da4a942006-04-22 02:32:43 +000037__all__ = [ 'Mailbox', 'Maildir', 'mbox', 'MH', 'Babyl', 'MMDF',
38 'Message', 'MaildirMessage', 'mboxMessage', 'MHMessage',
39 'BabylMessage', 'MMDFMessage', 'UnixMailbox',
40 'PortableUnixMailbox', 'MmdfMailbox', 'MHMailbox', 'BabylMailbox' ]
41
Andrew M. Kuchling1da4a942006-04-22 02:32:43 +000042class Mailbox:
43 """A group of messages in a particular place."""
44
45 def __init__(self, path, factory=None, create=True):
46 """Initialize a Mailbox instance."""
47 self._path = os.path.abspath(os.path.expanduser(path))
48 self._factory = factory
49
50 def add(self, message):
51 """Add message and return assigned key."""
52 raise NotImplementedError('Method must be implemented by subclass')
53
54 def remove(self, key):
55 """Remove the keyed message; raise KeyError if it doesn't exist."""
56 raise NotImplementedError('Method must be implemented by subclass')
57
58 def __delitem__(self, key):
59 self.remove(key)
60
61 def discard(self, key):
62 """If the keyed message exists, remove it."""
63 try:
64 self.remove(key)
65 except KeyError:
66 pass
67
68 def __setitem__(self, key, message):
69 """Replace the keyed message; raise KeyError if it doesn't exist."""
70 raise NotImplementedError('Method must be implemented by subclass')
71
72 def get(self, key, default=None):
73 """Return the keyed message, or default if it doesn't exist."""
74 try:
75 return self.__getitem__(key)
76 except KeyError:
77 return default
78
79 def __getitem__(self, key):
80 """Return the keyed message; raise KeyError if it doesn't exist."""
81 if not self._factory:
82 return self.get_message(key)
83 else:
84 return self._factory(self.get_file(key))
85
86 def get_message(self, key):
87 """Return a Message representation or raise a KeyError."""
88 raise NotImplementedError('Method must be implemented by subclass')
89
90 def get_string(self, key):
91 """Return a string representation or raise a KeyError."""
92 raise NotImplementedError('Method must be implemented by subclass')
93
94 def get_file(self, key):
95 """Return a file-like representation or raise a KeyError."""
96 raise NotImplementedError('Method must be implemented by subclass')
97
98 def iterkeys(self):
99 """Return an iterator over keys."""
100 raise NotImplementedError('Method must be implemented by subclass')
101
102 def keys(self):
103 """Return a list of keys."""
104 return list(self.iterkeys())
105
106 def itervalues(self):
107 """Return an iterator over all messages."""
108 for key in self.iterkeys():
109 try:
110 value = self[key]
111 except KeyError:
112 continue
113 yield value
114
115 def __iter__(self):
116 return self.itervalues()
117
118 def values(self):
119 """Return a list of messages. Memory intensive."""
120 return list(self.itervalues())
121
122 def iteritems(self):
123 """Return an iterator over (key, message) tuples."""
124 for key in self.iterkeys():
125 try:
126 value = self[key]
127 except KeyError:
128 continue
129 yield (key, value)
130
131 def items(self):
132 """Return a list of (key, message) tuples. Memory intensive."""
133 return list(self.iteritems())
134
135 def has_key(self, key):
136 """Return True if the keyed message exists, False otherwise."""
137 raise NotImplementedError('Method must be implemented by subclass')
138
139 def __contains__(self, key):
140 return self.has_key(key)
141
142 def __len__(self):
143 """Return a count of messages in the mailbox."""
144 raise NotImplementedError('Method must be implemented by subclass')
145
146 def clear(self):
147 """Delete all messages."""
148 for key in self.iterkeys():
149 self.discard(key)
150
151 def pop(self, key, default=None):
152 """Delete the keyed message and return it, or default."""
153 try:
154 result = self[key]
155 except KeyError:
156 return default
157 self.discard(key)
158 return result
159
160 def popitem(self):
161 """Delete an arbitrary (key, message) pair and return it."""
162 for key in self.iterkeys():
163 return (key, self.pop(key)) # This is only run once.
164 else:
165 raise KeyError('No messages in mailbox')
166
167 def update(self, arg=None):
168 """Change the messages that correspond to certain keys."""
169 if hasattr(arg, 'iteritems'):
170 source = arg.iteritems()
171 elif hasattr(arg, 'items'):
172 source = arg.items()
173 else:
174 source = arg
175 bad_key = False
176 for key, message in source:
177 try:
178 self[key] = message
179 except KeyError:
180 bad_key = True
181 if bad_key:
182 raise KeyError('No message with key(s)')
183
184 def flush(self):
185 """Write any pending changes to the disk."""
186 raise NotImplementedError('Method must be implemented by subclass')
187
188 def lock(self):
189 """Lock the mailbox."""
190 raise NotImplementedError('Method must be implemented by subclass')
191
192 def unlock(self):
193 """Unlock the mailbox if it is locked."""
194 raise NotImplementedError('Method must be implemented by subclass')
195
196 def close(self):
197 """Flush and close the mailbox."""
198 raise NotImplementedError('Method must be implemented by subclass')
199
Petri Lehtinena4fd0dc2012-09-25 21:57:59 +0300200 # Whether each message must end in a newline
201 _append_newline = False
202
Andrew M. Kuchling1da4a942006-04-22 02:32:43 +0000203 def _dump_message(self, message, target, mangle_from_=False):
204 # Most files are opened in binary mode to allow predictable seeking.
205 # To get native line endings on disk, the user-friendly \n line endings
206 # used in strings and by email.Message are translated here.
207 """Dump message contents to target file."""
Georg Brandl5a096e12007-01-22 19:40:21 +0000208 if isinstance(message, email.message.Message):
Andrew M. Kuchling1da4a942006-04-22 02:32:43 +0000209 buffer = StringIO.StringIO()
Georg Brandl5a096e12007-01-22 19:40:21 +0000210 gen = email.generator.Generator(buffer, mangle_from_, 0)
Andrew M. Kuchling1da4a942006-04-22 02:32:43 +0000211 gen.flatten(message)
212 buffer.seek(0)
Petri Lehtinena4fd0dc2012-09-25 21:57:59 +0300213 data = buffer.read().replace('\n', os.linesep)
214 target.write(data)
215 if self._append_newline and not data.endswith(os.linesep):
216 # Make sure the message ends with a newline
217 target.write(os.linesep)
Andrew M. Kuchling1da4a942006-04-22 02:32:43 +0000218 elif isinstance(message, str):
219 if mangle_from_:
220 message = message.replace('\nFrom ', '\n>From ')
221 message = message.replace('\n', os.linesep)
222 target.write(message)
Petri Lehtinena4fd0dc2012-09-25 21:57:59 +0300223 if self._append_newline and not message.endswith(os.linesep):
224 # Make sure the message ends with a newline
225 target.write(os.linesep)
Andrew M. Kuchling1da4a942006-04-22 02:32:43 +0000226 elif hasattr(message, 'read'):
Petri Lehtinena4fd0dc2012-09-25 21:57:59 +0300227 lastline = None
Andrew M. Kuchling1da4a942006-04-22 02:32:43 +0000228 while True:
229 line = message.readline()
230 if line == '':
231 break
232 if mangle_from_ and line.startswith('From '):
233 line = '>From ' + line[5:]
234 line = line.replace('\n', os.linesep)
235 target.write(line)
Petri Lehtinena4fd0dc2012-09-25 21:57:59 +0300236 lastline = line
237 if self._append_newline and lastline and not lastline.endswith(os.linesep):
238 # Make sure the message ends with a newline
239 target.write(os.linesep)
Andrew M. Kuchling1da4a942006-04-22 02:32:43 +0000240 else:
241 raise TypeError('Invalid message type: %s' % type(message))
242
243
244class Maildir(Mailbox):
245 """A qmail-style Maildir mailbox."""
246
247 colon = ':'
248
249 def __init__(self, dirname, factory=rfc822.Message, create=True):
250 """Initialize a Maildir instance."""
251 Mailbox.__init__(self, dirname, factory, create)
R David Murray8b26c4b2011-05-06 21:56:22 -0400252 self._paths = {
253 'tmp': os.path.join(self._path, 'tmp'),
254 'new': os.path.join(self._path, 'new'),
255 'cur': os.path.join(self._path, 'cur'),
256 }
Andrew M. Kuchling1da4a942006-04-22 02:32:43 +0000257 if not os.path.exists(self._path):
258 if create:
259 os.mkdir(self._path, 0700)
R David Murray8b26c4b2011-05-06 21:56:22 -0400260 for path in self._paths.values():
261 os.mkdir(path, 0o700)
Andrew M. Kuchling1da4a942006-04-22 02:32:43 +0000262 else:
263 raise NoSuchMailboxError(self._path)
264 self._toc = {}
Petri Lehtinen49aa72e2011-11-05 09:50:37 +0200265 self._toc_mtimes = {'cur': 0, 'new': 0}
266 self._last_read = 0 # Records last time we read cur/new
267 self._skewfactor = 0.1 # Adjust if os/fs clocks are skewing
Andrew M. Kuchling1da4a942006-04-22 02:32:43 +0000268
269 def add(self, message):
270 """Add message and return assigned key."""
271 tmp_file = self._create_tmp()
272 try:
273 self._dump_message(message, tmp_file)
R. David Murray008c0442011-02-11 23:03:13 +0000274 except BaseException:
275 tmp_file.close()
276 os.remove(tmp_file.name)
277 raise
278 _sync_close(tmp_file)
Andrew M. Kuchling1da4a942006-04-22 02:32:43 +0000279 if isinstance(message, MaildirMessage):
280 subdir = message.get_subdir()
281 suffix = self.colon + message.get_info()
282 if suffix == self.colon:
283 suffix = ''
284 else:
285 subdir = 'new'
286 suffix = ''
287 uniq = os.path.basename(tmp_file.name).split(self.colon)[0]
288 dest = os.path.join(self._path, subdir, uniq + suffix)
R David Murrayc64566e2013-09-18 08:35:45 -0400289 if isinstance(message, MaildirMessage):
290 os.utime(tmp_file.name,
291 (os.path.getatime(tmp_file.name), message.get_date()))
292 # No file modification should be done after the file is moved to its
293 # final position in order to prevent race conditions with changes
294 # from other programs
Andrew M. Kuchling978d8282006-11-09 21:16:46 +0000295 try:
296 if hasattr(os, 'link'):
297 os.link(tmp_file.name, dest)
298 os.remove(tmp_file.name)
299 else:
300 os.rename(tmp_file.name, dest)
301 except OSError, e:
302 os.remove(tmp_file.name)
303 if e.errno == errno.EEXIST:
304 raise ExternalClashError('Name clash with existing message: %s'
305 % dest)
306 else:
307 raise
Andrew M. Kuchling1da4a942006-04-22 02:32:43 +0000308 return uniq
309
310 def remove(self, key):
311 """Remove the keyed message; raise KeyError if it doesn't exist."""
312 os.remove(os.path.join(self._path, self._lookup(key)))
313
314 def discard(self, key):
315 """If the keyed message exists, remove it."""
316 # This overrides an inapplicable implementation in the superclass.
317 try:
318 self.remove(key)
319 except KeyError:
320 pass
321 except OSError, e:
Martin v. Löwis08041d52006-05-04 14:27:52 +0000322 if e.errno != errno.ENOENT:
Tim Peters6d7cd7d2006-04-22 05:52:59 +0000323 raise
Andrew M. Kuchling1da4a942006-04-22 02:32:43 +0000324
325 def __setitem__(self, key, message):
326 """Replace the keyed message; raise KeyError if it doesn't exist."""
327 old_subpath = self._lookup(key)
328 temp_key = self.add(message)
329 temp_subpath = self._lookup(temp_key)
330 if isinstance(message, MaildirMessage):
331 # temp's subdir and suffix were specified by message.
332 dominant_subpath = temp_subpath
333 else:
334 # temp's subdir and suffix were defaults from add().
335 dominant_subpath = old_subpath
336 subdir = os.path.dirname(dominant_subpath)
337 if self.colon in dominant_subpath:
338 suffix = self.colon + dominant_subpath.split(self.colon)[-1]
339 else:
340 suffix = ''
341 self.discard(key)
R David Murrayc64566e2013-09-18 08:35:45 -0400342 tmp_path = os.path.join(self._path, temp_subpath)
Andrew M. Kuchling1da4a942006-04-22 02:32:43 +0000343 new_path = os.path.join(self._path, subdir, key + suffix)
Andrew M. Kuchling1da4a942006-04-22 02:32:43 +0000344 if isinstance(message, MaildirMessage):
R David Murrayc64566e2013-09-18 08:35:45 -0400345 os.utime(tmp_path,
346 (os.path.getatime(tmp_path), message.get_date()))
347 # No file modification should be done after the file is moved to its
348 # final position in order to prevent race conditions with changes
349 # from other programs
350 os.rename(tmp_path, new_path)
Andrew M. Kuchling1da4a942006-04-22 02:32:43 +0000351
352 def get_message(self, key):
353 """Return a Message representation or raise a KeyError."""
354 subpath = self._lookup(key)
Andrew M. Kuchling214db632006-05-02 21:44:33 +0000355 f = open(os.path.join(self._path, subpath), 'r')
Andrew M. Kuchling1da4a942006-04-22 02:32:43 +0000356 try:
Andrew M. Kuchling15ce8802008-01-19 20:12:04 +0000357 if self._factory:
358 msg = self._factory(f)
359 else:
360 msg = MaildirMessage(f)
Andrew M. Kuchling1da4a942006-04-22 02:32:43 +0000361 finally:
362 f.close()
363 subdir, name = os.path.split(subpath)
364 msg.set_subdir(subdir)
365 if self.colon in name:
366 msg.set_info(name.split(self.colon)[-1])
367 msg.set_date(os.path.getmtime(os.path.join(self._path, subpath)))
368 return msg
369
370 def get_string(self, key):
371 """Return a string representation or raise a KeyError."""
Andrew M. Kuchling214db632006-05-02 21:44:33 +0000372 f = open(os.path.join(self._path, self._lookup(key)), 'r')
Andrew M. Kuchling1da4a942006-04-22 02:32:43 +0000373 try:
374 return f.read()
375 finally:
376 f.close()
377
378 def get_file(self, key):
379 """Return a file-like representation or raise a KeyError."""
Andrew M. Kuchling214db632006-05-02 21:44:33 +0000380 f = open(os.path.join(self._path, self._lookup(key)), 'rb')
Andrew M. Kuchling1da4a942006-04-22 02:32:43 +0000381 return _ProxyFile(f)
382
383 def iterkeys(self):
384 """Return an iterator over keys."""
385 self._refresh()
386 for key in self._toc:
387 try:
388 self._lookup(key)
389 except KeyError:
390 continue
391 yield key
392
393 def has_key(self, key):
394 """Return True if the keyed message exists, False otherwise."""
395 self._refresh()
396 return key in self._toc
397
398 def __len__(self):
399 """Return a count of messages in the mailbox."""
400 self._refresh()
401 return len(self._toc)
402
403 def flush(self):
404 """Write any pending changes to disk."""
Antoine Pitroue4c6b162009-11-01 21:29:33 +0000405 # Maildir changes are always written immediately, so there's nothing
R David Murray8b26c4b2011-05-06 21:56:22 -0400406 # to do.
407 pass
Andrew M. Kuchling1da4a942006-04-22 02:32:43 +0000408
409 def lock(self):
410 """Lock the mailbox."""
411 return
412
413 def unlock(self):
414 """Unlock the mailbox if it is locked."""
415 return
416
417 def close(self):
418 """Flush and close the mailbox."""
419 return
420
421 def list_folders(self):
422 """Return a list of folder names."""
423 result = []
424 for entry in os.listdir(self._path):
425 if len(entry) > 1 and entry[0] == '.' and \
426 os.path.isdir(os.path.join(self._path, entry)):
427 result.append(entry[1:])
428 return result
429
430 def get_folder(self, folder):
431 """Return a Maildir instance for the named folder."""
Andrew M. Kuchlinga3e5d372006-11-09 13:27:07 +0000432 return Maildir(os.path.join(self._path, '.' + folder),
433 factory=self._factory,
434 create=False)
Andrew M. Kuchling1da4a942006-04-22 02:32:43 +0000435
436 def add_folder(self, folder):
437 """Create a folder and return a Maildir instance representing it."""
438 path = os.path.join(self._path, '.' + folder)
Andrew M. Kuchlinga3e5d372006-11-09 13:27:07 +0000439 result = Maildir(path, factory=self._factory)
Andrew M. Kuchling1da4a942006-04-22 02:32:43 +0000440 maildirfolder_path = os.path.join(path, 'maildirfolder')
441 if not os.path.exists(maildirfolder_path):
Andrew M. Kuchling70a6dbd2008-08-04 01:43:43 +0000442 os.close(os.open(maildirfolder_path, os.O_CREAT | os.O_WRONLY,
443 0666))
Andrew M. Kuchling1da4a942006-04-22 02:32:43 +0000444 return result
445
446 def remove_folder(self, folder):
447 """Delete the named folder, which must be empty."""
448 path = os.path.join(self._path, '.' + folder)
449 for entry in os.listdir(os.path.join(path, 'new')) + \
450 os.listdir(os.path.join(path, 'cur')):
451 if len(entry) < 1 or entry[0] != '.':
452 raise NotEmptyError('Folder contains message(s): %s' % folder)
453 for entry in os.listdir(path):
454 if entry != 'new' and entry != 'cur' and entry != 'tmp' and \
455 os.path.isdir(os.path.join(path, entry)):
456 raise NotEmptyError("Folder contains subdirectory '%s': %s" %
457 (folder, entry))
458 for root, dirs, files in os.walk(path, topdown=False):
459 for entry in files:
460 os.remove(os.path.join(root, entry))
461 for entry in dirs:
462 os.rmdir(os.path.join(root, entry))
463 os.rmdir(path)
464
465 def clean(self):
466 """Delete old files in "tmp"."""
467 now = time.time()
468 for entry in os.listdir(os.path.join(self._path, 'tmp')):
469 path = os.path.join(self._path, 'tmp', entry)
470 if now - os.path.getatime(path) > 129600: # 60 * 60 * 36
471 os.remove(path)
472
473 _count = 1 # This is used to generate unique file names.
474
475 def _create_tmp(self):
476 """Create a file in the tmp subdirectory and open and return it."""
477 now = time.time()
478 hostname = socket.gethostname()
479 if '/' in hostname:
480 hostname = hostname.replace('/', r'\057')
481 if ':' in hostname:
482 hostname = hostname.replace(':', r'\072')
483 uniq = "%s.M%sP%sQ%s.%s" % (int(now), int(now % 1 * 1e6), os.getpid(),
484 Maildir._count, hostname)
485 path = os.path.join(self._path, 'tmp', uniq)
486 try:
487 os.stat(path)
488 except OSError, e:
489 if e.errno == errno.ENOENT:
490 Maildir._count += 1
Andrew M. Kuchling978d8282006-11-09 21:16:46 +0000491 try:
492 return _create_carefully(path)
493 except OSError, e:
494 if e.errno != errno.EEXIST:
495 raise
Andrew M. Kuchling1da4a942006-04-22 02:32:43 +0000496 else:
497 raise
Andrew M. Kuchling978d8282006-11-09 21:16:46 +0000498
499 # Fall through to here if stat succeeded or open raised EEXIST.
500 raise ExternalClashError('Name clash prevented file creation: %s' %
501 path)
Andrew M. Kuchling1da4a942006-04-22 02:32:43 +0000502
503 def _refresh(self):
504 """Update table of contents mapping."""
R David Murray8b26c4b2011-05-06 21:56:22 -0400505 # If it has been less than two seconds since the last _refresh() call,
506 # we have to unconditionally re-read the mailbox just in case it has
507 # been modified, because os.path.mtime() has a 2 sec resolution in the
508 # most common worst case (FAT) and a 1 sec resolution typically. This
509 # results in a few unnecessary re-reads when _refresh() is called
510 # multiple times in that interval, but once the clock ticks over, we
511 # will only re-read as needed. Because the filesystem might be being
512 # served by an independent system with its own clock, we record and
513 # compare with the mtimes from the filesystem. Because the other
514 # system's clock might be skewing relative to our clock, we add an
515 # extra delta to our wait. The default is one tenth second, but is an
516 # instance variable and so can be adjusted if dealing with a
517 # particularly skewed or irregular system.
518 if time.time() - self._last_read > 2 + self._skewfactor:
519 refresh = False
520 for subdir in self._toc_mtimes:
521 mtime = os.path.getmtime(self._paths[subdir])
522 if mtime > self._toc_mtimes[subdir]:
523 refresh = True
524 self._toc_mtimes[subdir] = mtime
525 if not refresh:
Antoine Pitroud35b8c72009-11-01 00:30:13 +0000526 return
R David Murray8b26c4b2011-05-06 21:56:22 -0400527 # Refresh toc
Andrew M. Kuchling1da4a942006-04-22 02:32:43 +0000528 self._toc = {}
R David Murray8b26c4b2011-05-06 21:56:22 -0400529 for subdir in self._toc_mtimes:
530 path = self._paths[subdir]
Andrew M. Kuchling420d4eb2009-05-02 19:17:28 +0000531 for entry in os.listdir(path):
532 p = os.path.join(path, entry)
Andrew M. Kuchling2b09ef02007-07-14 21:56:19 +0000533 if os.path.isdir(p):
534 continue
Andrew M. Kuchling1da4a942006-04-22 02:32:43 +0000535 uniq = entry.split(self.colon)[0]
536 self._toc[uniq] = os.path.join(subdir, entry)
R David Murray8b26c4b2011-05-06 21:56:22 -0400537 self._last_read = time.time()
Andrew M. Kuchling420d4eb2009-05-02 19:17:28 +0000538
Andrew M. Kuchling1da4a942006-04-22 02:32:43 +0000539 def _lookup(self, key):
540 """Use TOC to return subpath for given key, or raise a KeyError."""
541 try:
542 if os.path.exists(os.path.join(self._path, self._toc[key])):
543 return self._toc[key]
544 except KeyError:
545 pass
546 self._refresh()
547 try:
548 return self._toc[key]
549 except KeyError:
550 raise KeyError('No message with key: %s' % key)
551
552 # This method is for backward compatibility only.
553 def next(self):
554 """Return the next message in a one-time iteration."""
555 if not hasattr(self, '_onetime_keys'):
556 self._onetime_keys = self.iterkeys()
557 while True:
558 try:
559 return self[self._onetime_keys.next()]
560 except StopIteration:
561 return None
562 except KeyError:
563 continue
564
565
566class _singlefileMailbox(Mailbox):
567 """A single-file mailbox."""
568
569 def __init__(self, path, factory=None, create=True):
570 """Initialize a single-file mailbox."""
571 Mailbox.__init__(self, path, factory, create)
572 try:
Andrew M. Kuchling214db632006-05-02 21:44:33 +0000573 f = open(self._path, 'rb+')
Andrew M. Kuchling1da4a942006-04-22 02:32:43 +0000574 except IOError, e:
575 if e.errno == errno.ENOENT:
576 if create:
Andrew M. Kuchling214db632006-05-02 21:44:33 +0000577 f = open(self._path, 'wb+')
Andrew M. Kuchling1da4a942006-04-22 02:32:43 +0000578 else:
579 raise NoSuchMailboxError(self._path)
R. David Murray1a337902011-03-03 18:17:40 +0000580 elif e.errno in (errno.EACCES, errno.EROFS):
Andrew M. Kuchling214db632006-05-02 21:44:33 +0000581 f = open(self._path, 'rb')
Andrew M. Kuchling1da4a942006-04-22 02:32:43 +0000582 else:
583 raise
584 self._file = f
585 self._toc = None
586 self._next_key = 0
Petri Lehtinen45f0d982012-06-28 13:48:17 +0300587 self._pending = False # No changes require rewriting the file.
588 self._pending_sync = False # No need to sync the file
Andrew M. Kuchling1da4a942006-04-22 02:32:43 +0000589 self._locked = False
Petri Lehtinen45f0d982012-06-28 13:48:17 +0300590 self._file_length = None # Used to record mailbox size
Andrew M. Kuchling1da4a942006-04-22 02:32:43 +0000591
592 def add(self, message):
593 """Add message and return assigned key."""
594 self._lookup()
595 self._toc[self._next_key] = self._append_message(message)
596 self._next_key += 1
Petri Lehtinen45f0d982012-06-28 13:48:17 +0300597 # _append_message appends the message to the mailbox file. We
598 # don't need a full rewrite + rename, sync is enough.
599 self._pending_sync = True
Andrew M. Kuchling1da4a942006-04-22 02:32:43 +0000600 return self._next_key - 1
601
602 def remove(self, key):
603 """Remove the keyed message; raise KeyError if it doesn't exist."""
604 self._lookup(key)
605 del self._toc[key]
606 self._pending = True
607
608 def __setitem__(self, key, message):
609 """Replace the keyed message; raise KeyError if it doesn't exist."""
610 self._lookup(key)
611 self._toc[key] = self._append_message(message)
612 self._pending = True
613
614 def iterkeys(self):
615 """Return an iterator over keys."""
616 self._lookup()
617 for key in self._toc.keys():
618 yield key
619
620 def has_key(self, key):
621 """Return True if the keyed message exists, False otherwise."""
622 self._lookup()
623 return key in self._toc
624
625 def __len__(self):
626 """Return a count of messages in the mailbox."""
627 self._lookup()
628 return len(self._toc)
629
630 def lock(self):
631 """Lock the mailbox."""
632 if not self._locked:
633 _lock_file(self._file)
634 self._locked = True
635
636 def unlock(self):
637 """Unlock the mailbox if it is locked."""
638 if self._locked:
639 _unlock_file(self._file)
640 self._locked = False
641
642 def flush(self):
643 """Write any pending changes to disk."""
644 if not self._pending:
Petri Lehtinen45f0d982012-06-28 13:48:17 +0300645 if self._pending_sync:
646 # Messages have only been added, so syncing the file
647 # is enough.
648 _sync_flush(self._file)
649 self._pending_sync = False
Andrew M. Kuchling1da4a942006-04-22 02:32:43 +0000650 return
Andrew M. Kuchlingeca4c312006-12-20 19:48:20 +0000651
652 # In order to be writing anything out at all, self._toc must
653 # already have been generated (and presumably has been modified
654 # by adding or deleting an item).
655 assert self._toc is not None
Tim Petersf733abb2007-01-30 03:03:46 +0000656
Andrew M. Kuchlingeca4c312006-12-20 19:48:20 +0000657 # Check length of self._file; if it's changed, some other process
658 # has modified the mailbox since we scanned it.
659 self._file.seek(0, 2)
660 cur_len = self._file.tell()
661 if cur_len != self._file_length:
662 raise ExternalClashError('Size of mailbox file changed '
663 '(expected %i, found %i)' %
664 (self._file_length, cur_len))
Tim Petersf733abb2007-01-30 03:03:46 +0000665
Andrew M. Kuchling1da4a942006-04-22 02:32:43 +0000666 new_file = _create_temporary(self._path)
667 try:
668 new_toc = {}
669 self._pre_mailbox_hook(new_file)
670 for key in sorted(self._toc.keys()):
671 start, stop = self._toc[key]
672 self._file.seek(start)
673 self._pre_message_hook(new_file)
674 new_start = new_file.tell()
675 while True:
676 buffer = self._file.read(min(4096,
677 stop - self._file.tell()))
678 if buffer == '':
679 break
680 new_file.write(buffer)
681 new_toc[key] = (new_start, new_file.tell())
682 self._post_message_hook(new_file)
Petri Lehtinen7cf66992012-06-15 20:50:51 +0300683 self._file_length = new_file.tell()
Andrew M. Kuchling1da4a942006-04-22 02:32:43 +0000684 except:
685 new_file.close()
686 os.remove(new_file.name)
687 raise
Andrew M. Kuchlingb5686da2006-11-09 13:51:14 +0000688 _sync_close(new_file)
689 # self._file is about to get replaced, so no need to sync.
Andrew M. Kuchling1da4a942006-04-22 02:32:43 +0000690 self._file.close()
Petri Lehtinend07de402012-06-29 15:09:12 +0300691 # Make sure the new file's mode is the same as the old file's
692 mode = os.stat(self._path).st_mode
693 os.chmod(new_file.name, mode)
Andrew M. Kuchling1da4a942006-04-22 02:32:43 +0000694 try:
695 os.rename(new_file.name, self._path)
696 except OSError, e:
Andrew MacIntyreafa358f2006-07-23 13:04:00 +0000697 if e.errno == errno.EEXIST or \
698 (os.name == 'os2' and e.errno == errno.EACCES):
Andrew M. Kuchling1da4a942006-04-22 02:32:43 +0000699 os.remove(self._path)
700 os.rename(new_file.name, self._path)
701 else:
702 raise
Andrew M. Kuchling214db632006-05-02 21:44:33 +0000703 self._file = open(self._path, 'rb+')
Andrew M. Kuchling1da4a942006-04-22 02:32:43 +0000704 self._toc = new_toc
705 self._pending = False
Petri Lehtinen45f0d982012-06-28 13:48:17 +0300706 self._pending_sync = False
Andrew M. Kuchling1da4a942006-04-22 02:32:43 +0000707 if self._locked:
Andrew M. Kuchling0f871832006-10-27 16:55:34 +0000708 _lock_file(self._file, dotlock=False)
Andrew M. Kuchling1da4a942006-04-22 02:32:43 +0000709
710 def _pre_mailbox_hook(self, f):
711 """Called before writing the mailbox to file f."""
712 return
713
714 def _pre_message_hook(self, f):
715 """Called before writing each message to file f."""
716 return
717
718 def _post_message_hook(self, f):
719 """Called after writing each message to file f."""
720 return
721
722 def close(self):
723 """Flush and close the mailbox."""
724 self.flush()
725 if self._locked:
726 self.unlock()
Andrew M. Kuchlingb5686da2006-11-09 13:51:14 +0000727 self._file.close() # Sync has been done by self.flush() above.
Andrew M. Kuchling1da4a942006-04-22 02:32:43 +0000728
729 def _lookup(self, key=None):
730 """Return (start, stop) or raise KeyError."""
731 if self._toc is None:
732 self._generate_toc()
733 if key is not None:
734 try:
735 return self._toc[key]
736 except KeyError:
737 raise KeyError('No message with key: %s' % key)
738
739 def _append_message(self, message):
740 """Append message to mailbox and return (start, stop) offsets."""
741 self._file.seek(0, 2)
R. David Murray008c0442011-02-11 23:03:13 +0000742 before = self._file.tell()
Petri Lehtinen4e6e5a02012-06-29 13:43:37 +0300743 if len(self._toc) == 0 and not self._pending:
744 # This is the first message, and the _pre_mailbox_hook
745 # hasn't yet been called. If self._pending is True,
746 # messages have been removed, so _pre_mailbox_hook must
747 # have been called already.
Petri Lehtinen45f0d982012-06-28 13:48:17 +0300748 self._pre_mailbox_hook(self._file)
R. David Murray008c0442011-02-11 23:03:13 +0000749 try:
750 self._pre_message_hook(self._file)
751 offsets = self._install_message(message)
752 self._post_message_hook(self._file)
753 except BaseException:
754 self._file.truncate(before)
755 raise
Andrew M. Kuchling1da4a942006-04-22 02:32:43 +0000756 self._file.flush()
Andrew M. Kuchlingeca4c312006-12-20 19:48:20 +0000757 self._file_length = self._file.tell() # Record current length of mailbox
Andrew M. Kuchling1da4a942006-04-22 02:32:43 +0000758 return offsets
759
760
761
762class _mboxMMDF(_singlefileMailbox):
763 """An mbox or MMDF mailbox."""
764
765 _mangle_from_ = True
766
767 def get_message(self, key):
768 """Return a Message representation or raise a KeyError."""
769 start, stop = self._lookup(key)
770 self._file.seek(start)
771 from_line = self._file.readline().replace(os.linesep, '')
772 string = self._file.read(stop - self._file.tell())
773 msg = self._message_factory(string.replace(os.linesep, '\n'))
774 msg.set_from(from_line[5:])
775 return msg
776
777 def get_string(self, key, from_=False):
778 """Return a string representation or raise a KeyError."""
779 start, stop = self._lookup(key)
780 self._file.seek(start)
781 if not from_:
782 self._file.readline()
783 string = self._file.read(stop - self._file.tell())
784 return string.replace(os.linesep, '\n')
785
786 def get_file(self, key, from_=False):
787 """Return a file-like representation or raise a KeyError."""
788 start, stop = self._lookup(key)
789 self._file.seek(start)
790 if not from_:
791 self._file.readline()
792 return _PartialFile(self._file, self._file.tell(), stop)
793
794 def _install_message(self, message):
795 """Format a message and blindly write to self._file."""
796 from_line = None
797 if isinstance(message, str) and message.startswith('From '):
798 newline = message.find('\n')
799 if newline != -1:
800 from_line = message[:newline]
801 message = message[newline + 1:]
802 else:
803 from_line = message
804 message = ''
805 elif isinstance(message, _mboxMMDFMessage):
806 from_line = 'From ' + message.get_from()
Georg Brandl5a096e12007-01-22 19:40:21 +0000807 elif isinstance(message, email.message.Message):
Andrew M. Kuchling1da4a942006-04-22 02:32:43 +0000808 from_line = message.get_unixfrom() # May be None.
809 if from_line is None:
810 from_line = 'From MAILER-DAEMON %s' % time.asctime(time.gmtime())
811 start = self._file.tell()
812 self._file.write(from_line + os.linesep)
813 self._dump_message(message, self._file, self._mangle_from_)
814 stop = self._file.tell()
815 return (start, stop)
816
817
818class mbox(_mboxMMDF):
819 """A classic mbox mailbox."""
820
821 _mangle_from_ = True
822
Petri Lehtinena4fd0dc2012-09-25 21:57:59 +0300823 # All messages must end in a newline character, and
824 # _post_message_hooks outputs an empty line between messages.
825 _append_newline = True
826
Andrew M. Kuchling1da4a942006-04-22 02:32:43 +0000827 def __init__(self, path, factory=None, create=True):
828 """Initialize an mbox mailbox."""
829 self._message_factory = mboxMessage
830 _mboxMMDF.__init__(self, path, factory, create)
831
Petri Lehtinena4fd0dc2012-09-25 21:57:59 +0300832 def _post_message_hook(self, f):
833 """Called after writing each message to file f."""
834 f.write(os.linesep)
Andrew M. Kuchling1da4a942006-04-22 02:32:43 +0000835
836 def _generate_toc(self):
837 """Generate key-to-(start, stop) table of contents."""
838 starts, stops = [], []
Petri Lehtinena4fd0dc2012-09-25 21:57:59 +0300839 last_was_empty = False
Andrew M. Kuchling1da4a942006-04-22 02:32:43 +0000840 self._file.seek(0)
841 while True:
842 line_pos = self._file.tell()
843 line = self._file.readline()
844 if line.startswith('From '):
845 if len(stops) < len(starts):
Petri Lehtinena4fd0dc2012-09-25 21:57:59 +0300846 if last_was_empty:
847 stops.append(line_pos - len(os.linesep))
848 else:
849 # The last line before the "From " line wasn't
850 # blank, but we consider it a start of a
851 # message anyway.
852 stops.append(line_pos)
Andrew M. Kuchling1da4a942006-04-22 02:32:43 +0000853 starts.append(line_pos)
Petri Lehtinena4fd0dc2012-09-25 21:57:59 +0300854 last_was_empty = False
855 elif not line:
856 if last_was_empty:
857 stops.append(line_pos - len(os.linesep))
858 else:
859 stops.append(line_pos)
Andrew M. Kuchling1da4a942006-04-22 02:32:43 +0000860 break
Petri Lehtinena4fd0dc2012-09-25 21:57:59 +0300861 elif line == os.linesep:
862 last_was_empty = True
863 else:
864 last_was_empty = False
Andrew M. Kuchling1da4a942006-04-22 02:32:43 +0000865 self._toc = dict(enumerate(zip(starts, stops)))
866 self._next_key = len(self._toc)
Andrew M. Kuchlingeca4c312006-12-20 19:48:20 +0000867 self._file_length = self._file.tell()
Andrew M. Kuchling1da4a942006-04-22 02:32:43 +0000868
869
870class MMDF(_mboxMMDF):
871 """An MMDF mailbox."""
872
873 def __init__(self, path, factory=None, create=True):
874 """Initialize an MMDF mailbox."""
875 self._message_factory = MMDFMessage
876 _mboxMMDF.__init__(self, path, factory, create)
877
878 def _pre_message_hook(self, f):
879 """Called before writing each message to file f."""
880 f.write('\001\001\001\001' + os.linesep)
881
882 def _post_message_hook(self, f):
883 """Called after writing each message to file f."""
884 f.write(os.linesep + '\001\001\001\001' + os.linesep)
885
886 def _generate_toc(self):
887 """Generate key-to-(start, stop) table of contents."""
888 starts, stops = [], []
889 self._file.seek(0)
890 next_pos = 0
891 while True:
892 line_pos = next_pos
893 line = self._file.readline()
894 next_pos = self._file.tell()
895 if line.startswith('\001\001\001\001' + os.linesep):
896 starts.append(next_pos)
897 while True:
898 line_pos = next_pos
899 line = self._file.readline()
900 next_pos = self._file.tell()
901 if line == '\001\001\001\001' + os.linesep:
902 stops.append(line_pos - len(os.linesep))
903 break
904 elif line == '':
905 stops.append(line_pos)
906 break
907 elif line == '':
908 break
909 self._toc = dict(enumerate(zip(starts, stops)))
910 self._next_key = len(self._toc)
Andrew M. Kuchlingeca4c312006-12-20 19:48:20 +0000911 self._file.seek(0, 2)
912 self._file_length = self._file.tell()
Andrew M. Kuchling1da4a942006-04-22 02:32:43 +0000913
914
915class MH(Mailbox):
916 """An MH mailbox."""
917
918 def __init__(self, path, factory=None, create=True):
919 """Initialize an MH instance."""
920 Mailbox.__init__(self, path, factory, create)
921 if not os.path.exists(self._path):
922 if create:
923 os.mkdir(self._path, 0700)
924 os.close(os.open(os.path.join(self._path, '.mh_sequences'),
925 os.O_CREAT | os.O_EXCL | os.O_WRONLY, 0600))
926 else:
927 raise NoSuchMailboxError(self._path)
928 self._locked = False
929
930 def add(self, message):
931 """Add message and return assigned key."""
932 keys = self.keys()
933 if len(keys) == 0:
934 new_key = 1
935 else:
936 new_key = max(keys) + 1
937 new_path = os.path.join(self._path, str(new_key))
938 f = _create_carefully(new_path)
R. David Murrayf9e34232011-02-12 02:03:56 +0000939 closed = False
Andrew M. Kuchling1da4a942006-04-22 02:32:43 +0000940 try:
941 if self._locked:
942 _lock_file(f)
943 try:
R. David Murray008c0442011-02-11 23:03:13 +0000944 try:
945 self._dump_message(message, f)
946 except BaseException:
R. David Murrayf9e34232011-02-12 02:03:56 +0000947 # Unlock and close so it can be deleted on Windows
948 if self._locked:
949 _unlock_file(f)
950 _sync_close(f)
951 closed = True
R. David Murray008c0442011-02-11 23:03:13 +0000952 os.remove(new_path)
953 raise
Andrew M. Kuchling1da4a942006-04-22 02:32:43 +0000954 if isinstance(message, MHMessage):
955 self._dump_sequences(message, new_key)
956 finally:
957 if self._locked:
958 _unlock_file(f)
959 finally:
R. David Murrayf9e34232011-02-12 02:03:56 +0000960 if not closed:
961 _sync_close(f)
Andrew M. Kuchling1da4a942006-04-22 02:32:43 +0000962 return new_key
963
964 def remove(self, key):
965 """Remove the keyed message; raise KeyError if it doesn't exist."""
966 path = os.path.join(self._path, str(key))
967 try:
Andrew M. Kuchling214db632006-05-02 21:44:33 +0000968 f = open(path, 'rb+')
Andrew M. Kuchling1da4a942006-04-22 02:32:43 +0000969 except IOError, e:
970 if e.errno == errno.ENOENT:
971 raise KeyError('No message with key: %s' % key)
972 else:
973 raise
Andrew M. Kuchlingb72b0eb2010-02-22 18:42:07 +0000974 else:
Andrew M. Kuchling1da4a942006-04-22 02:32:43 +0000975 f.close()
Andrew M. Kuchlingb72b0eb2010-02-22 18:42:07 +0000976 os.remove(path)
Andrew M. Kuchling1da4a942006-04-22 02:32:43 +0000977
978 def __setitem__(self, key, message):
979 """Replace the keyed message; raise KeyError if it doesn't exist."""
980 path = os.path.join(self._path, str(key))
981 try:
Andrew M. Kuchling214db632006-05-02 21:44:33 +0000982 f = open(path, 'rb+')
Andrew M. Kuchling1da4a942006-04-22 02:32:43 +0000983 except IOError, e:
984 if e.errno == errno.ENOENT:
985 raise KeyError('No message with key: %s' % key)
986 else:
987 raise
988 try:
989 if self._locked:
990 _lock_file(f)
991 try:
992 os.close(os.open(path, os.O_WRONLY | os.O_TRUNC))
993 self._dump_message(message, f)
994 if isinstance(message, MHMessage):
995 self._dump_sequences(message, key)
996 finally:
997 if self._locked:
998 _unlock_file(f)
999 finally:
Andrew M. Kuchlingb5686da2006-11-09 13:51:14 +00001000 _sync_close(f)
Andrew M. Kuchling1da4a942006-04-22 02:32:43 +00001001
1002 def get_message(self, key):
1003 """Return a Message representation or raise a KeyError."""
1004 try:
1005 if self._locked:
Andrew M. Kuchling214db632006-05-02 21:44:33 +00001006 f = open(os.path.join(self._path, str(key)), 'r+')
Andrew M. Kuchling1da4a942006-04-22 02:32:43 +00001007 else:
Andrew M. Kuchling214db632006-05-02 21:44:33 +00001008 f = open(os.path.join(self._path, str(key)), 'r')
Andrew M. Kuchling1da4a942006-04-22 02:32:43 +00001009 except IOError, e:
1010 if e.errno == errno.ENOENT:
1011 raise KeyError('No message with key: %s' % key)
1012 else:
1013 raise
1014 try:
1015 if self._locked:
1016 _lock_file(f)
1017 try:
1018 msg = MHMessage(f)
1019 finally:
1020 if self._locked:
1021 _unlock_file(f)
1022 finally:
1023 f.close()
R. David Murray52720c52009-04-02 14:05:35 +00001024 for name, key_list in self.get_sequences().iteritems():
Andrew M. Kuchling1da4a942006-04-22 02:32:43 +00001025 if key in key_list:
1026 msg.add_sequence(name)
1027 return msg
1028
1029 def get_string(self, key):
1030 """Return a string representation or raise a KeyError."""
1031 try:
1032 if self._locked:
Andrew M. Kuchling214db632006-05-02 21:44:33 +00001033 f = open(os.path.join(self._path, str(key)), 'r+')
Andrew M. Kuchling1da4a942006-04-22 02:32:43 +00001034 else:
Andrew M. Kuchling214db632006-05-02 21:44:33 +00001035 f = open(os.path.join(self._path, str(key)), 'r')
Andrew M. Kuchling1da4a942006-04-22 02:32:43 +00001036 except IOError, e:
1037 if e.errno == errno.ENOENT:
1038 raise KeyError('No message with key: %s' % key)
1039 else:
1040 raise
1041 try:
1042 if self._locked:
1043 _lock_file(f)
1044 try:
1045 return f.read()
1046 finally:
1047 if self._locked:
1048 _unlock_file(f)
1049 finally:
1050 f.close()
1051
1052 def get_file(self, key):
1053 """Return a file-like representation or raise a KeyError."""
1054 try:
Andrew M. Kuchling214db632006-05-02 21:44:33 +00001055 f = open(os.path.join(self._path, str(key)), 'rb')
Andrew M. Kuchling1da4a942006-04-22 02:32:43 +00001056 except IOError, e:
1057 if e.errno == errno.ENOENT:
1058 raise KeyError('No message with key: %s' % key)
1059 else:
1060 raise
1061 return _ProxyFile(f)
1062
1063 def iterkeys(self):
1064 """Return an iterator over keys."""
1065 return iter(sorted(int(entry) for entry in os.listdir(self._path)
1066 if entry.isdigit()))
1067
1068 def has_key(self, key):
1069 """Return True if the keyed message exists, False otherwise."""
1070 return os.path.exists(os.path.join(self._path, str(key)))
1071
1072 def __len__(self):
1073 """Return a count of messages in the mailbox."""
1074 return len(list(self.iterkeys()))
1075
1076 def lock(self):
1077 """Lock the mailbox."""
1078 if not self._locked:
Andrew M. Kuchling214db632006-05-02 21:44:33 +00001079 self._file = open(os.path.join(self._path, '.mh_sequences'), 'rb+')
Andrew M. Kuchling1da4a942006-04-22 02:32:43 +00001080 _lock_file(self._file)
1081 self._locked = True
1082
1083 def unlock(self):
1084 """Unlock the mailbox if it is locked."""
1085 if self._locked:
1086 _unlock_file(self._file)
Andrew M. Kuchlingb5686da2006-11-09 13:51:14 +00001087 _sync_close(self._file)
Andrew M. Kuchling1da4a942006-04-22 02:32:43 +00001088 del self._file
1089 self._locked = False
1090
1091 def flush(self):
1092 """Write any pending changes to the disk."""
1093 return
1094
1095 def close(self):
1096 """Flush and close the mailbox."""
1097 if self._locked:
1098 self.unlock()
1099
1100 def list_folders(self):
1101 """Return a list of folder names."""
1102 result = []
1103 for entry in os.listdir(self._path):
1104 if os.path.isdir(os.path.join(self._path, entry)):
1105 result.append(entry)
1106 return result
1107
1108 def get_folder(self, folder):
1109 """Return an MH instance for the named folder."""
Andrew M. Kuchlinga3e5d372006-11-09 13:27:07 +00001110 return MH(os.path.join(self._path, folder),
1111 factory=self._factory, create=False)
Andrew M. Kuchling1da4a942006-04-22 02:32:43 +00001112
1113 def add_folder(self, folder):
1114 """Create a folder and return an MH instance representing it."""
Andrew M. Kuchlinga3e5d372006-11-09 13:27:07 +00001115 return MH(os.path.join(self._path, folder),
1116 factory=self._factory)
Andrew M. Kuchling1da4a942006-04-22 02:32:43 +00001117
1118 def remove_folder(self, folder):
1119 """Delete the named folder, which must be empty."""
1120 path = os.path.join(self._path, folder)
1121 entries = os.listdir(path)
1122 if entries == ['.mh_sequences']:
1123 os.remove(os.path.join(path, '.mh_sequences'))
1124 elif entries == []:
1125 pass
1126 else:
1127 raise NotEmptyError('Folder not empty: %s' % self._path)
1128 os.rmdir(path)
1129
1130 def get_sequences(self):
1131 """Return a name-to-key-list dictionary to define each sequence."""
1132 results = {}
Andrew M. Kuchling214db632006-05-02 21:44:33 +00001133 f = open(os.path.join(self._path, '.mh_sequences'), 'r')
Andrew M. Kuchling1da4a942006-04-22 02:32:43 +00001134 try:
1135 all_keys = set(self.keys())
1136 for line in f:
1137 try:
1138 name, contents = line.split(':')
1139 keys = set()
1140 for spec in contents.split():
1141 if spec.isdigit():
1142 keys.add(int(spec))
1143 else:
1144 start, stop = (int(x) for x in spec.split('-'))
1145 keys.update(range(start, stop + 1))
1146 results[name] = [key for key in sorted(keys) \
1147 if key in all_keys]
1148 if len(results[name]) == 0:
1149 del results[name]
1150 except ValueError:
1151 raise FormatError('Invalid sequence specification: %s' %
1152 line.rstrip())
1153 finally:
1154 f.close()
1155 return results
1156
1157 def set_sequences(self, sequences):
1158 """Set sequences using the given name-to-key-list dictionary."""
Andrew M. Kuchling214db632006-05-02 21:44:33 +00001159 f = open(os.path.join(self._path, '.mh_sequences'), 'r+')
Andrew M. Kuchling1da4a942006-04-22 02:32:43 +00001160 try:
1161 os.close(os.open(f.name, os.O_WRONLY | os.O_TRUNC))
1162 for name, keys in sequences.iteritems():
1163 if len(keys) == 0:
1164 continue
1165 f.write('%s:' % name)
1166 prev = None
1167 completing = False
1168 for key in sorted(set(keys)):
1169 if key - 1 == prev:
1170 if not completing:
1171 completing = True
1172 f.write('-')
1173 elif completing:
1174 completing = False
1175 f.write('%s %s' % (prev, key))
1176 else:
1177 f.write(' %s' % key)
1178 prev = key
1179 if completing:
1180 f.write(str(prev) + '\n')
1181 else:
1182 f.write('\n')
1183 finally:
Andrew M. Kuchlingb5686da2006-11-09 13:51:14 +00001184 _sync_close(f)
Andrew M. Kuchling1da4a942006-04-22 02:32:43 +00001185
1186 def pack(self):
1187 """Re-name messages to eliminate numbering gaps. Invalidates keys."""
1188 sequences = self.get_sequences()
1189 prev = 0
1190 changes = []
1191 for key in self.iterkeys():
1192 if key - 1 != prev:
1193 changes.append((key, prev + 1))
Andrew M. Kuchling8c456f32006-11-17 13:30:25 +00001194 if hasattr(os, 'link'):
1195 os.link(os.path.join(self._path, str(key)),
1196 os.path.join(self._path, str(prev + 1)))
1197 os.unlink(os.path.join(self._path, str(key)))
1198 else:
1199 os.rename(os.path.join(self._path, str(key)),
1200 os.path.join(self._path, str(prev + 1)))
Andrew M. Kuchling1da4a942006-04-22 02:32:43 +00001201 prev += 1
1202 self._next_key = prev + 1
1203 if len(changes) == 0:
1204 return
1205 for name, key_list in sequences.items():
1206 for old, new in changes:
1207 if old in key_list:
1208 key_list[key_list.index(old)] = new
1209 self.set_sequences(sequences)
1210
1211 def _dump_sequences(self, message, key):
1212 """Inspect a new MHMessage and update sequences appropriately."""
1213 pending_sequences = message.get_sequences()
1214 all_sequences = self.get_sequences()
1215 for name, key_list in all_sequences.iteritems():
1216 if name in pending_sequences:
1217 key_list.append(key)
1218 elif key in key_list:
1219 del key_list[key_list.index(key)]
1220 for sequence in pending_sequences:
1221 if sequence not in all_sequences:
1222 all_sequences[sequence] = [key]
1223 self.set_sequences(all_sequences)
1224
1225
1226class Babyl(_singlefileMailbox):
1227 """An Rmail-style Babyl mailbox."""
1228
1229 _special_labels = frozenset(('unseen', 'deleted', 'filed', 'answered',
1230 'forwarded', 'edited', 'resent'))
1231
1232 def __init__(self, path, factory=None, create=True):
1233 """Initialize a Babyl mailbox."""
1234 _singlefileMailbox.__init__(self, path, factory, create)
1235 self._labels = {}
1236
1237 def add(self, message):
1238 """Add message and return assigned key."""
1239 key = _singlefileMailbox.add(self, message)
1240 if isinstance(message, BabylMessage):
1241 self._labels[key] = message.get_labels()
1242 return key
1243
1244 def remove(self, key):
1245 """Remove the keyed message; raise KeyError if it doesn't exist."""
1246 _singlefileMailbox.remove(self, key)
1247 if key in self._labels:
1248 del self._labels[key]
1249
1250 def __setitem__(self, key, message):
1251 """Replace the keyed message; raise KeyError if it doesn't exist."""
1252 _singlefileMailbox.__setitem__(self, key, message)
1253 if isinstance(message, BabylMessage):
1254 self._labels[key] = message.get_labels()
1255
1256 def get_message(self, key):
1257 """Return a Message representation or raise a KeyError."""
1258 start, stop = self._lookup(key)
1259 self._file.seek(start)
1260 self._file.readline() # Skip '1,' line specifying labels.
1261 original_headers = StringIO.StringIO()
1262 while True:
1263 line = self._file.readline()
1264 if line == '*** EOOH ***' + os.linesep or line == '':
1265 break
1266 original_headers.write(line.replace(os.linesep, '\n'))
1267 visible_headers = StringIO.StringIO()
1268 while True:
1269 line = self._file.readline()
1270 if line == os.linesep or line == '':
1271 break
1272 visible_headers.write(line.replace(os.linesep, '\n'))
1273 body = self._file.read(stop - self._file.tell()).replace(os.linesep,
1274 '\n')
1275 msg = BabylMessage(original_headers.getvalue() + body)
1276 msg.set_visible(visible_headers.getvalue())
1277 if key in self._labels:
1278 msg.set_labels(self._labels[key])
1279 return msg
1280
1281 def get_string(self, key):
1282 """Return a string representation or raise a KeyError."""
1283 start, stop = self._lookup(key)
1284 self._file.seek(start)
1285 self._file.readline() # Skip '1,' line specifying labels.
1286 original_headers = StringIO.StringIO()
1287 while True:
1288 line = self._file.readline()
1289 if line == '*** EOOH ***' + os.linesep or line == '':
1290 break
1291 original_headers.write(line.replace(os.linesep, '\n'))
1292 while True:
1293 line = self._file.readline()
1294 if line == os.linesep or line == '':
1295 break
1296 return original_headers.getvalue() + \
1297 self._file.read(stop - self._file.tell()).replace(os.linesep,
1298 '\n')
1299
1300 def get_file(self, key):
1301 """Return a file-like representation or raise a KeyError."""
1302 return StringIO.StringIO(self.get_string(key).replace('\n',
1303 os.linesep))
1304
1305 def get_labels(self):
1306 """Return a list of user-defined labels in the mailbox."""
1307 self._lookup()
1308 labels = set()
1309 for label_list in self._labels.values():
1310 labels.update(label_list)
1311 labels.difference_update(self._special_labels)
1312 return list(labels)
1313
1314 def _generate_toc(self):
1315 """Generate key-to-(start, stop) table of contents."""
1316 starts, stops = [], []
1317 self._file.seek(0)
1318 next_pos = 0
1319 label_lists = []
1320 while True:
1321 line_pos = next_pos
1322 line = self._file.readline()
1323 next_pos = self._file.tell()
1324 if line == '\037\014' + os.linesep:
1325 if len(stops) < len(starts):
1326 stops.append(line_pos - len(os.linesep))
1327 starts.append(next_pos)
1328 labels = [label.strip() for label
1329 in self._file.readline()[1:].split(',')
1330 if label.strip() != '']
1331 label_lists.append(labels)
1332 elif line == '\037' or line == '\037' + os.linesep:
1333 if len(stops) < len(starts):
1334 stops.append(line_pos - len(os.linesep))
1335 elif line == '':
1336 stops.append(line_pos - len(os.linesep))
1337 break
1338 self._toc = dict(enumerate(zip(starts, stops)))
1339 self._labels = dict(enumerate(label_lists))
1340 self._next_key = len(self._toc)
Andrew M. Kuchlingeca4c312006-12-20 19:48:20 +00001341 self._file.seek(0, 2)
1342 self._file_length = self._file.tell()
Tim Petersf733abb2007-01-30 03:03:46 +00001343
Andrew M. Kuchling1da4a942006-04-22 02:32:43 +00001344 def _pre_mailbox_hook(self, f):
1345 """Called before writing the mailbox to file f."""
1346 f.write('BABYL OPTIONS:%sVersion: 5%sLabels:%s%s\037' %
1347 (os.linesep, os.linesep, ','.join(self.get_labels()),
1348 os.linesep))
1349
1350 def _pre_message_hook(self, f):
1351 """Called before writing each message to file f."""
1352 f.write('\014' + os.linesep)
1353
1354 def _post_message_hook(self, f):
1355 """Called after writing each message to file f."""
1356 f.write(os.linesep + '\037')
1357
1358 def _install_message(self, message):
1359 """Write message contents and return (start, stop)."""
1360 start = self._file.tell()
1361 if isinstance(message, BabylMessage):
1362 special_labels = []
1363 labels = []
1364 for label in message.get_labels():
1365 if label in self._special_labels:
1366 special_labels.append(label)
1367 else:
1368 labels.append(label)
1369 self._file.write('1')
1370 for label in special_labels:
1371 self._file.write(', ' + label)
1372 self._file.write(',,')
1373 for label in labels:
1374 self._file.write(' ' + label + ',')
1375 self._file.write(os.linesep)
1376 else:
1377 self._file.write('1,,' + os.linesep)
Georg Brandl5a096e12007-01-22 19:40:21 +00001378 if isinstance(message, email.message.Message):
Andrew M. Kuchling1da4a942006-04-22 02:32:43 +00001379 orig_buffer = StringIO.StringIO()
Georg Brandl5a096e12007-01-22 19:40:21 +00001380 orig_generator = email.generator.Generator(orig_buffer, False, 0)
Andrew M. Kuchling1da4a942006-04-22 02:32:43 +00001381 orig_generator.flatten(message)
1382 orig_buffer.seek(0)
1383 while True:
1384 line = orig_buffer.readline()
1385 self._file.write(line.replace('\n', os.linesep))
1386 if line == '\n' or line == '':
1387 break
1388 self._file.write('*** EOOH ***' + os.linesep)
1389 if isinstance(message, BabylMessage):
1390 vis_buffer = StringIO.StringIO()
Georg Brandl5a096e12007-01-22 19:40:21 +00001391 vis_generator = email.generator.Generator(vis_buffer, False, 0)
Andrew M. Kuchling1da4a942006-04-22 02:32:43 +00001392 vis_generator.flatten(message.get_visible())
1393 while True:
1394 line = vis_buffer.readline()
1395 self._file.write(line.replace('\n', os.linesep))
1396 if line == '\n' or line == '':
1397 break
1398 else:
1399 orig_buffer.seek(0)
1400 while True:
1401 line = orig_buffer.readline()
1402 self._file.write(line.replace('\n', os.linesep))
1403 if line == '\n' or line == '':
1404 break
1405 while True:
1406 buffer = orig_buffer.read(4096) # Buffer size is arbitrary.
1407 if buffer == '':
1408 break
1409 self._file.write(buffer.replace('\n', os.linesep))
1410 elif isinstance(message, str):
1411 body_start = message.find('\n\n') + 2
1412 if body_start - 2 != -1:
1413 self._file.write(message[:body_start].replace('\n',
1414 os.linesep))
1415 self._file.write('*** EOOH ***' + os.linesep)
1416 self._file.write(message[:body_start].replace('\n',
1417 os.linesep))
1418 self._file.write(message[body_start:].replace('\n',
1419 os.linesep))
1420 else:
1421 self._file.write('*** EOOH ***' + os.linesep + os.linesep)
1422 self._file.write(message.replace('\n', os.linesep))
1423 elif hasattr(message, 'readline'):
1424 original_pos = message.tell()
1425 first_pass = True
1426 while True:
1427 line = message.readline()
1428 self._file.write(line.replace('\n', os.linesep))
1429 if line == '\n' or line == '':
Andrew M. Kuchling1da4a942006-04-22 02:32:43 +00001430 if first_pass:
1431 first_pass = False
Petri Lehtinen2d44cee2012-08-15 14:22:46 +03001432 self._file.write('*** EOOH ***' + os.linesep)
Andrew M. Kuchling1da4a942006-04-22 02:32:43 +00001433 message.seek(original_pos)
1434 else:
1435 break
1436 while True:
1437 buffer = message.read(4096) # Buffer size is arbitrary.
1438 if buffer == '':
1439 break
1440 self._file.write(buffer.replace('\n', os.linesep))
1441 else:
1442 raise TypeError('Invalid message type: %s' % type(message))
1443 stop = self._file.tell()
1444 return (start, stop)
1445
1446
Georg Brandl5a096e12007-01-22 19:40:21 +00001447class Message(email.message.Message):
Andrew M. Kuchling1da4a942006-04-22 02:32:43 +00001448 """Message with mailbox-format-specific properties."""
1449
1450 def __init__(self, message=None):
1451 """Initialize a Message instance."""
Georg Brandl5a096e12007-01-22 19:40:21 +00001452 if isinstance(message, email.message.Message):
Andrew M. Kuchling1da4a942006-04-22 02:32:43 +00001453 self._become_message(copy.deepcopy(message))
1454 if isinstance(message, Message):
1455 message._explain_to(self)
1456 elif isinstance(message, str):
1457 self._become_message(email.message_from_string(message))
1458 elif hasattr(message, "read"):
1459 self._become_message(email.message_from_file(message))
1460 elif message is None:
Georg Brandl5a096e12007-01-22 19:40:21 +00001461 email.message.Message.__init__(self)
Andrew M. Kuchling1da4a942006-04-22 02:32:43 +00001462 else:
1463 raise TypeError('Invalid message type: %s' % type(message))
1464
1465 def _become_message(self, message):
1466 """Assume the non-format-specific state of message."""
1467 for name in ('_headers', '_unixfrom', '_payload', '_charset',
1468 'preamble', 'epilogue', 'defects', '_default_type'):
1469 self.__dict__[name] = message.__dict__[name]
1470
1471 def _explain_to(self, message):
1472 """Copy format-specific state to message insofar as possible."""
1473 if isinstance(message, Message):
1474 return # There's nothing format-specific to explain.
1475 else:
1476 raise TypeError('Cannot convert to specified type')
1477
1478
1479class MaildirMessage(Message):
1480 """Message with Maildir-specific properties."""
1481
1482 def __init__(self, message=None):
1483 """Initialize a MaildirMessage instance."""
1484 self._subdir = 'new'
1485 self._info = ''
1486 self._date = time.time()
1487 Message.__init__(self, message)
1488
1489 def get_subdir(self):
1490 """Return 'new' or 'cur'."""
1491 return self._subdir
1492
1493 def set_subdir(self, subdir):
1494 """Set subdir to 'new' or 'cur'."""
1495 if subdir == 'new' or subdir == 'cur':
1496 self._subdir = subdir
1497 else:
1498 raise ValueError("subdir must be 'new' or 'cur': %s" % subdir)
1499
1500 def get_flags(self):
1501 """Return as a string the flags that are set."""
1502 if self._info.startswith('2,'):
1503 return self._info[2:]
1504 else:
1505 return ''
1506
1507 def set_flags(self, flags):
1508 """Set the given flags and unset all others."""
1509 self._info = '2,' + ''.join(sorted(flags))
1510
1511 def add_flag(self, flag):
1512 """Set the given flag(s) without changing others."""
1513 self.set_flags(''.join(set(self.get_flags()) | set(flag)))
1514
1515 def remove_flag(self, flag):
1516 """Unset the given string flag(s) without changing others."""
1517 if self.get_flags() != '':
1518 self.set_flags(''.join(set(self.get_flags()) - set(flag)))
1519
1520 def get_date(self):
1521 """Return delivery date of message, in seconds since the epoch."""
1522 return self._date
1523
1524 def set_date(self, date):
1525 """Set delivery date of message, in seconds since the epoch."""
1526 try:
1527 self._date = float(date)
1528 except ValueError:
1529 raise TypeError("can't convert to float: %s" % date)
1530
1531 def get_info(self):
1532 """Get the message's "info" as a string."""
1533 return self._info
1534
1535 def set_info(self, info):
1536 """Set the message's "info" string."""
1537 if isinstance(info, str):
1538 self._info = info
1539 else:
1540 raise TypeError('info must be a string: %s' % type(info))
1541
1542 def _explain_to(self, message):
1543 """Copy Maildir-specific state to message insofar as possible."""
1544 if isinstance(message, MaildirMessage):
1545 message.set_flags(self.get_flags())
1546 message.set_subdir(self.get_subdir())
1547 message.set_date(self.get_date())
1548 elif isinstance(message, _mboxMMDFMessage):
1549 flags = set(self.get_flags())
1550 if 'S' in flags:
1551 message.add_flag('R')
1552 if self.get_subdir() == 'cur':
1553 message.add_flag('O')
1554 if 'T' in flags:
1555 message.add_flag('D')
1556 if 'F' in flags:
1557 message.add_flag('F')
1558 if 'R' in flags:
1559 message.add_flag('A')
1560 message.set_from('MAILER-DAEMON', time.gmtime(self.get_date()))
1561 elif isinstance(message, MHMessage):
1562 flags = set(self.get_flags())
1563 if 'S' not in flags:
1564 message.add_sequence('unseen')
1565 if 'R' in flags:
1566 message.add_sequence('replied')
1567 if 'F' in flags:
1568 message.add_sequence('flagged')
1569 elif isinstance(message, BabylMessage):
1570 flags = set(self.get_flags())
1571 if 'S' not in flags:
1572 message.add_label('unseen')
1573 if 'T' in flags:
1574 message.add_label('deleted')
1575 if 'R' in flags:
1576 message.add_label('answered')
1577 if 'P' in flags:
1578 message.add_label('forwarded')
1579 elif isinstance(message, Message):
1580 pass
1581 else:
1582 raise TypeError('Cannot convert to specified type: %s' %
1583 type(message))
1584
1585
1586class _mboxMMDFMessage(Message):
1587 """Message with mbox- or MMDF-specific properties."""
1588
1589 def __init__(self, message=None):
1590 """Initialize an mboxMMDFMessage instance."""
1591 self.set_from('MAILER-DAEMON', True)
Georg Brandl5a096e12007-01-22 19:40:21 +00001592 if isinstance(message, email.message.Message):
Andrew M. Kuchling1da4a942006-04-22 02:32:43 +00001593 unixfrom = message.get_unixfrom()
1594 if unixfrom is not None and unixfrom.startswith('From '):
1595 self.set_from(unixfrom[5:])
1596 Message.__init__(self, message)
1597
1598 def get_from(self):
1599 """Return contents of "From " line."""
1600 return self._from
1601
1602 def set_from(self, from_, time_=None):
1603 """Set "From " line, formatting and appending time_ if specified."""
1604 if time_ is not None:
1605 if time_ is True:
1606 time_ = time.gmtime()
1607 from_ += ' ' + time.asctime(time_)
1608 self._from = from_
1609
1610 def get_flags(self):
1611 """Return as a string the flags that are set."""
1612 return self.get('Status', '') + self.get('X-Status', '')
1613
1614 def set_flags(self, flags):
1615 """Set the given flags and unset all others."""
1616 flags = set(flags)
1617 status_flags, xstatus_flags = '', ''
1618 for flag in ('R', 'O'):
1619 if flag in flags:
1620 status_flags += flag
1621 flags.remove(flag)
1622 for flag in ('D', 'F', 'A'):
1623 if flag in flags:
1624 xstatus_flags += flag
1625 flags.remove(flag)
1626 xstatus_flags += ''.join(sorted(flags))
1627 try:
1628 self.replace_header('Status', status_flags)
1629 except KeyError:
1630 self.add_header('Status', status_flags)
1631 try:
1632 self.replace_header('X-Status', xstatus_flags)
1633 except KeyError:
1634 self.add_header('X-Status', xstatus_flags)
1635
1636 def add_flag(self, flag):
1637 """Set the given flag(s) without changing others."""
1638 self.set_flags(''.join(set(self.get_flags()) | set(flag)))
1639
1640 def remove_flag(self, flag):
1641 """Unset the given string flag(s) without changing others."""
1642 if 'Status' in self or 'X-Status' in self:
1643 self.set_flags(''.join(set(self.get_flags()) - set(flag)))
1644
1645 def _explain_to(self, message):
1646 """Copy mbox- or MMDF-specific state to message insofar as possible."""
1647 if isinstance(message, MaildirMessage):
1648 flags = set(self.get_flags())
1649 if 'O' in flags:
1650 message.set_subdir('cur')
1651 if 'F' in flags:
1652 message.add_flag('F')
1653 if 'A' in flags:
1654 message.add_flag('R')
1655 if 'R' in flags:
1656 message.add_flag('S')
1657 if 'D' in flags:
1658 message.add_flag('T')
1659 del message['status']
1660 del message['x-status']
1661 maybe_date = ' '.join(self.get_from().split()[-5:])
1662 try:
1663 message.set_date(calendar.timegm(time.strptime(maybe_date,
1664 '%a %b %d %H:%M:%S %Y')))
1665 except (ValueError, OverflowError):
1666 pass
1667 elif isinstance(message, _mboxMMDFMessage):
1668 message.set_flags(self.get_flags())
1669 message.set_from(self.get_from())
1670 elif isinstance(message, MHMessage):
1671 flags = set(self.get_flags())
1672 if 'R' not in flags:
1673 message.add_sequence('unseen')
1674 if 'A' in flags:
1675 message.add_sequence('replied')
1676 if 'F' in flags:
1677 message.add_sequence('flagged')
1678 del message['status']
1679 del message['x-status']
1680 elif isinstance(message, BabylMessage):
1681 flags = set(self.get_flags())
1682 if 'R' not in flags:
1683 message.add_label('unseen')
1684 if 'D' in flags:
1685 message.add_label('deleted')
1686 if 'A' in flags:
1687 message.add_label('answered')
1688 del message['status']
1689 del message['x-status']
1690 elif isinstance(message, Message):
1691 pass
1692 else:
1693 raise TypeError('Cannot convert to specified type: %s' %
1694 type(message))
1695
1696
1697class mboxMessage(_mboxMMDFMessage):
1698 """Message with mbox-specific properties."""
1699
1700
1701class MHMessage(Message):
1702 """Message with MH-specific properties."""
1703
1704 def __init__(self, message=None):
1705 """Initialize an MHMessage instance."""
1706 self._sequences = []
1707 Message.__init__(self, message)
1708
1709 def get_sequences(self):
1710 """Return a list of sequences that include the message."""
1711 return self._sequences[:]
1712
1713 def set_sequences(self, sequences):
1714 """Set the list of sequences that include the message."""
1715 self._sequences = list(sequences)
1716
1717 def add_sequence(self, sequence):
1718 """Add sequence to list of sequences including the message."""
1719 if isinstance(sequence, str):
1720 if not sequence in self._sequences:
1721 self._sequences.append(sequence)
1722 else:
1723 raise TypeError('sequence must be a string: %s' % type(sequence))
1724
1725 def remove_sequence(self, sequence):
1726 """Remove sequence from the list of sequences including the message."""
1727 try:
1728 self._sequences.remove(sequence)
1729 except ValueError:
1730 pass
1731
1732 def _explain_to(self, message):
1733 """Copy MH-specific state to message insofar as possible."""
1734 if isinstance(message, MaildirMessage):
1735 sequences = set(self.get_sequences())
1736 if 'unseen' in sequences:
1737 message.set_subdir('cur')
1738 else:
1739 message.set_subdir('cur')
1740 message.add_flag('S')
1741 if 'flagged' in sequences:
1742 message.add_flag('F')
1743 if 'replied' in sequences:
1744 message.add_flag('R')
1745 elif isinstance(message, _mboxMMDFMessage):
1746 sequences = set(self.get_sequences())
1747 if 'unseen' not in sequences:
1748 message.add_flag('RO')
1749 else:
1750 message.add_flag('O')
1751 if 'flagged' in sequences:
1752 message.add_flag('F')
1753 if 'replied' in sequences:
1754 message.add_flag('A')
1755 elif isinstance(message, MHMessage):
1756 for sequence in self.get_sequences():
1757 message.add_sequence(sequence)
1758 elif isinstance(message, BabylMessage):
1759 sequences = set(self.get_sequences())
1760 if 'unseen' in sequences:
1761 message.add_label('unseen')
1762 if 'replied' in sequences:
1763 message.add_label('answered')
1764 elif isinstance(message, Message):
1765 pass
1766 else:
1767 raise TypeError('Cannot convert to specified type: %s' %
1768 type(message))
1769
1770
1771class BabylMessage(Message):
1772 """Message with Babyl-specific properties."""
1773
1774 def __init__(self, message=None):
1775 """Initialize an BabylMessage instance."""
1776 self._labels = []
1777 self._visible = Message()
1778 Message.__init__(self, message)
1779
1780 def get_labels(self):
1781 """Return a list of labels on the message."""
1782 return self._labels[:]
1783
1784 def set_labels(self, labels):
1785 """Set the list of labels on the message."""
1786 self._labels = list(labels)
1787
1788 def add_label(self, label):
1789 """Add label to list of labels on the message."""
1790 if isinstance(label, str):
1791 if label not in self._labels:
1792 self._labels.append(label)
1793 else:
1794 raise TypeError('label must be a string: %s' % type(label))
1795
1796 def remove_label(self, label):
1797 """Remove label from the list of labels on the message."""
1798 try:
1799 self._labels.remove(label)
1800 except ValueError:
1801 pass
Tim Peters6d7cd7d2006-04-22 05:52:59 +00001802
Andrew M. Kuchling1da4a942006-04-22 02:32:43 +00001803 def get_visible(self):
1804 """Return a Message representation of visible headers."""
1805 return Message(self._visible)
1806
1807 def set_visible(self, visible):
1808 """Set the Message representation of visible headers."""
1809 self._visible = Message(visible)
1810
1811 def update_visible(self):
1812 """Update and/or sensibly generate a set of visible headers."""
1813 for header in self._visible.keys():
1814 if header in self:
1815 self._visible.replace_header(header, self[header])
1816 else:
1817 del self._visible[header]
1818 for header in ('Date', 'From', 'Reply-To', 'To', 'CC', 'Subject'):
1819 if header in self and header not in self._visible:
1820 self._visible[header] = self[header]
1821
1822 def _explain_to(self, message):
1823 """Copy Babyl-specific state to message insofar as possible."""
1824 if isinstance(message, MaildirMessage):
1825 labels = set(self.get_labels())
1826 if 'unseen' in labels:
1827 message.set_subdir('cur')
1828 else:
1829 message.set_subdir('cur')
1830 message.add_flag('S')
1831 if 'forwarded' in labels or 'resent' in labels:
1832 message.add_flag('P')
1833 if 'answered' in labels:
1834 message.add_flag('R')
1835 if 'deleted' in labels:
1836 message.add_flag('T')
1837 elif isinstance(message, _mboxMMDFMessage):
1838 labels = set(self.get_labels())
1839 if 'unseen' not in labels:
1840 message.add_flag('RO')
1841 else:
1842 message.add_flag('O')
1843 if 'deleted' in labels:
1844 message.add_flag('D')
1845 if 'answered' in labels:
1846 message.add_flag('A')
1847 elif isinstance(message, MHMessage):
1848 labels = set(self.get_labels())
1849 if 'unseen' in labels:
1850 message.add_sequence('unseen')
1851 if 'answered' in labels:
1852 message.add_sequence('replied')
1853 elif isinstance(message, BabylMessage):
1854 message.set_visible(self.get_visible())
1855 for label in self.get_labels():
1856 message.add_label(label)
1857 elif isinstance(message, Message):
1858 pass
1859 else:
1860 raise TypeError('Cannot convert to specified type: %s' %
1861 type(message))
1862
1863
1864class MMDFMessage(_mboxMMDFMessage):
1865 """Message with MMDF-specific properties."""
1866
1867
1868class _ProxyFile:
1869 """A read-only wrapper of a file."""
1870
1871 def __init__(self, f, pos=None):
1872 """Initialize a _ProxyFile."""
1873 self._file = f
1874 if pos is None:
1875 self._pos = f.tell()
1876 else:
1877 self._pos = pos
1878
1879 def read(self, size=None):
1880 """Read bytes."""
1881 return self._read(size, self._file.read)
1882
1883 def readline(self, size=None):
1884 """Read a line."""
1885 return self._read(size, self._file.readline)
1886
1887 def readlines(self, sizehint=None):
1888 """Read multiple lines."""
1889 result = []
1890 for line in self:
1891 result.append(line)
1892 if sizehint is not None:
1893 sizehint -= len(line)
1894 if sizehint <= 0:
1895 break
1896 return result
1897
1898 def __iter__(self):
1899 """Iterate over lines."""
1900 return iter(self.readline, "")
1901
1902 def tell(self):
1903 """Return the position."""
1904 return self._pos
1905
1906 def seek(self, offset, whence=0):
1907 """Change position."""
1908 if whence == 1:
1909 self._file.seek(self._pos)
1910 self._file.seek(offset, whence)
1911 self._pos = self._file.tell()
1912
1913 def close(self):
1914 """Close the file."""
R David Murrayf1138bb2011-06-17 22:23:04 -04001915 if hasattr(self, '_file'):
1916 if hasattr(self._file, 'close'):
1917 self._file.close()
1918 del self._file
Andrew M. Kuchling1da4a942006-04-22 02:32:43 +00001919
1920 def _read(self, size, read_method):
1921 """Read size bytes using read_method."""
1922 if size is None:
1923 size = -1
1924 self._file.seek(self._pos)
1925 result = read_method(size)
1926 self._pos = self._file.tell()
1927 return result
1928
1929
1930class _PartialFile(_ProxyFile):
1931 """A read-only wrapper of part of a file."""
1932
1933 def __init__(self, f, start=None, stop=None):
1934 """Initialize a _PartialFile."""
1935 _ProxyFile.__init__(self, f, start)
1936 self._start = start
1937 self._stop = stop
1938
1939 def tell(self):
1940 """Return the position with respect to start."""
1941 return _ProxyFile.tell(self) - self._start
1942
1943 def seek(self, offset, whence=0):
1944 """Change position, possibly with respect to start or stop."""
1945 if whence == 0:
1946 self._pos = self._start
1947 whence = 1
1948 elif whence == 2:
1949 self._pos = self._stop
1950 whence = 1
1951 _ProxyFile.seek(self, offset, whence)
1952
1953 def _read(self, size, read_method):
1954 """Read size bytes using read_method, honoring start and stop."""
1955 remaining = self._stop - self._pos
1956 if remaining <= 0:
1957 return ''
1958 if size is None or size < 0 or size > remaining:
1959 size = remaining
1960 return _ProxyFile._read(self, size, read_method)
1961
R David Murrayf1138bb2011-06-17 22:23:04 -04001962 def close(self):
1963 # do *not* close the underlying file object for partial files,
1964 # since it's global to the mailbox object
1965 if hasattr(self, '_file'):
1966 del self._file
1967
Andrew M. Kuchling1da4a942006-04-22 02:32:43 +00001968
1969def _lock_file(f, dotlock=True):
Andrew M. Kuchling55732592006-06-26 13:12:16 +00001970 """Lock file f using lockf and dot locking."""
Andrew M. Kuchling1da4a942006-04-22 02:32:43 +00001971 dotlock_done = False
1972 try:
1973 if fcntl:
1974 try:
1975 fcntl.lockf(f, fcntl.LOCK_EX | fcntl.LOCK_NB)
1976 except IOError, e:
R. David Murray1a337902011-03-03 18:17:40 +00001977 if e.errno in (errno.EAGAIN, errno.EACCES, errno.EROFS):
Andrew M. Kuchling1da4a942006-04-22 02:32:43 +00001978 raise ExternalClashError('lockf: lock unavailable: %s' %
1979 f.name)
1980 else:
1981 raise
Andrew M. Kuchling1da4a942006-04-22 02:32:43 +00001982 if dotlock:
1983 try:
1984 pre_lock = _create_temporary(f.name + '.lock')
1985 pre_lock.close()
1986 except IOError, e:
R. David Murray1a337902011-03-03 18:17:40 +00001987 if e.errno in (errno.EACCES, errno.EROFS):
Andrew M. Kuchling1da4a942006-04-22 02:32:43 +00001988 return # Without write access, just skip dotlocking.
1989 else:
1990 raise
1991 try:
1992 if hasattr(os, 'link'):
1993 os.link(pre_lock.name, f.name + '.lock')
1994 dotlock_done = True
1995 os.unlink(pre_lock.name)
1996 else:
1997 os.rename(pre_lock.name, f.name + '.lock')
1998 dotlock_done = True
1999 except OSError, e:
Andrew MacIntyreafa358f2006-07-23 13:04:00 +00002000 if e.errno == errno.EEXIST or \
2001 (os.name == 'os2' and e.errno == errno.EACCES):
Andrew M. Kuchling1da4a942006-04-22 02:32:43 +00002002 os.remove(pre_lock.name)
Tim Peters6d7cd7d2006-04-22 05:52:59 +00002003 raise ExternalClashError('dot lock unavailable: %s' %
Andrew M. Kuchling1da4a942006-04-22 02:32:43 +00002004 f.name)
2005 else:
2006 raise
2007 except:
2008 if fcntl:
2009 fcntl.lockf(f, fcntl.LOCK_UN)
Andrew M. Kuchling1da4a942006-04-22 02:32:43 +00002010 if dotlock_done:
2011 os.remove(f.name + '.lock')
2012 raise
2013
2014def _unlock_file(f):
Andrew M. Kuchling55732592006-06-26 13:12:16 +00002015 """Unlock file f using lockf and dot locking."""
Andrew M. Kuchling1da4a942006-04-22 02:32:43 +00002016 if fcntl:
2017 fcntl.lockf(f, fcntl.LOCK_UN)
Andrew M. Kuchling1da4a942006-04-22 02:32:43 +00002018 if os.path.exists(f.name + '.lock'):
2019 os.remove(f.name + '.lock')
2020
2021def _create_carefully(path):
2022 """Create a file if it doesn't exist and open for reading and writing."""
Andrew M. Kuchling70a6dbd2008-08-04 01:43:43 +00002023 fd = os.open(path, os.O_CREAT | os.O_EXCL | os.O_RDWR, 0666)
Andrew M. Kuchling1da4a942006-04-22 02:32:43 +00002024 try:
Andrew M. Kuchling214db632006-05-02 21:44:33 +00002025 return open(path, 'rb+')
Andrew M. Kuchling1da4a942006-04-22 02:32:43 +00002026 finally:
2027 os.close(fd)
2028
2029def _create_temporary(path):
2030 """Create a temp file based on path and open for reading and writing."""
2031 return _create_carefully('%s.%s.%s.%s' % (path, int(time.time()),
2032 socket.gethostname(),
2033 os.getpid()))
2034
Andrew M. Kuchlingb5686da2006-11-09 13:51:14 +00002035def _sync_flush(f):
2036 """Ensure changes to file f are physically on disk."""
2037 f.flush()
Andrew M. Kuchling16465682006-12-14 18:57:53 +00002038 if hasattr(os, 'fsync'):
2039 os.fsync(f.fileno())
Andrew M. Kuchlingb5686da2006-11-09 13:51:14 +00002040
2041def _sync_close(f):
2042 """Close file f, ensuring all changes are physically on disk."""
2043 _sync_flush(f)
2044 f.close()
Andrew M. Kuchling1da4a942006-04-22 02:32:43 +00002045
2046## Start: classes from the original module (for backward compatibility).
2047
2048# Note that the Maildir class, whose name is unchanged, itself offers a next()
2049# method for backward compatibility.
Skip Montanaro17ab1232001-01-24 06:27:27 +00002050
Guido van Rossumc7b68821994-04-28 09:53:33 +00002051class _Mailbox:
Guido van Rossum4bf12542002-09-12 05:08:00 +00002052
Barry Warsaw81ad67c2001-01-31 22:13:15 +00002053 def __init__(self, fp, factory=rfc822.Message):
Fred Drakedbbf76b2000-07-09 16:44:26 +00002054 self.fp = fp
2055 self.seekp = 0
Barry Warsaw81ad67c2001-01-31 22:13:15 +00002056 self.factory = factory
Guido van Rossum8ca84201998-03-26 20:56:10 +00002057
Fred Drake72987a42001-05-02 20:20:53 +00002058 def __iter__(self):
Guido van Rossum93a696f2001-09-13 01:29:13 +00002059 return iter(self.next, None)
Fred Drake72987a42001-05-02 20:20:53 +00002060
Fred Drakedbbf76b2000-07-09 16:44:26 +00002061 def next(self):
2062 while 1:
2063 self.fp.seek(self.seekp)
2064 try:
2065 self._search_start()
2066 except EOFError:
2067 self.seekp = self.fp.tell()
2068 return None
2069 start = self.fp.tell()
2070 self._search_end()
2071 self.seekp = stop = self.fp.tell()
Fred Drake8152d322000-12-12 23:20:45 +00002072 if start != stop:
Fred Drakedbbf76b2000-07-09 16:44:26 +00002073 break
Andrew M. Kuchling1da4a942006-04-22 02:32:43 +00002074 return self.factory(_PartialFile(self.fp, start, stop))
Guido van Rossumc7b68821994-04-28 09:53:33 +00002075
Barry Warsawffd05ee2002-03-01 22:39:14 +00002076# Recommended to use PortableUnixMailbox instead!
Guido van Rossumc7b68821994-04-28 09:53:33 +00002077class UnixMailbox(_Mailbox):
Guido van Rossum4bf12542002-09-12 05:08:00 +00002078
Fred Drakedbbf76b2000-07-09 16:44:26 +00002079 def _search_start(self):
2080 while 1:
2081 pos = self.fp.tell()
2082 line = self.fp.readline()
2083 if not line:
2084 raise EOFError
2085 if line[:5] == 'From ' and self._isrealfromline(line):
2086 self.fp.seek(pos)
2087 return
Guido van Rossum8ca84201998-03-26 20:56:10 +00002088
Fred Drakedbbf76b2000-07-09 16:44:26 +00002089 def _search_end(self):
2090 self.fp.readline() # Throw away header line
2091 while 1:
2092 pos = self.fp.tell()
2093 line = self.fp.readline()
2094 if not line:
2095 return
2096 if line[:5] == 'From ' and self._isrealfromline(line):
2097 self.fp.seek(pos)
2098 return
Guido van Rossumc7b68821994-04-28 09:53:33 +00002099
Barry Warsaw81ad67c2001-01-31 22:13:15 +00002100 # An overridable mechanism to test for From-line-ness. You can either
2101 # specify a different regular expression or define a whole new
2102 # _isrealfromline() method. Note that this only gets called for lines
2103 # starting with the 5 characters "From ".
2104 #
2105 # BAW: According to
2106 #http://home.netscape.com/eng/mozilla/2.0/relnotes/demo/content-length.html
2107 # the only portable, reliable way to find message delimiters in a BSD (i.e
2108 # Unix mailbox) style folder is to search for "\n\nFrom .*\n", or at the
2109 # beginning of the file, "^From .*\n". While _fromlinepattern below seems
2110 # like a good idea, in practice, there are too many variations for more
2111 # strict parsing of the line to be completely accurate.
2112 #
2113 # _strict_isrealfromline() is the old version which tries to do stricter
2114 # parsing of the From_ line. _portable_isrealfromline() simply returns
2115 # true, since it's never called if the line doesn't already start with
2116 # "From ".
2117 #
2118 # This algorithm, and the way it interacts with _search_start() and
2119 # _search_end() may not be completely correct, because it doesn't check
2120 # that the two characters preceding "From " are \n\n or the beginning of
2121 # the file. Fixing this would require a more extensive rewrite than is
Barry Warsawda5628f2002-08-26 16:44:56 +00002122 # necessary. For convenience, we've added a PortableUnixMailbox class
Andrew M. Kuchlingb94c0c32007-01-22 20:27:50 +00002123 # which does no checking of the format of the 'From' line.
Guido van Rossumc7b68821994-04-28 09:53:33 +00002124
Andrew M. Kuchlingb78bb742007-01-22 20:26:40 +00002125 _fromlinepattern = (r"From \s*[^\s]+\s+\w\w\w\s+\w\w\w\s+\d?\d\s+"
2126 r"\d?\d:\d\d(:\d\d)?(\s+[^\s]+)?\s+\d\d\d\d\s*"
2127 r"[^\s]*\s*"
2128 "$")
Fred Drakedbbf76b2000-07-09 16:44:26 +00002129 _regexp = None
Guido van Rossumfbe63de1998-04-03 16:04:05 +00002130
Barry Warsaw81ad67c2001-01-31 22:13:15 +00002131 def _strict_isrealfromline(self, line):
Fred Drakedbbf76b2000-07-09 16:44:26 +00002132 if not self._regexp:
2133 import re
2134 self._regexp = re.compile(self._fromlinepattern)
2135 return self._regexp.match(line)
Guido van Rossumfbe63de1998-04-03 16:04:05 +00002136
Barry Warsaw81ad67c2001-01-31 22:13:15 +00002137 def _portable_isrealfromline(self, line):
Tim Petersbc0e9102002-04-04 22:55:58 +00002138 return True
Barry Warsaw81ad67c2001-01-31 22:13:15 +00002139
2140 _isrealfromline = _strict_isrealfromline
2141
2142
2143class PortableUnixMailbox(UnixMailbox):
2144 _isrealfromline = UnixMailbox._portable_isrealfromline
2145
Guido van Rossumfbe63de1998-04-03 16:04:05 +00002146
Guido van Rossumc7b68821994-04-28 09:53:33 +00002147class MmdfMailbox(_Mailbox):
Guido van Rossum4bf12542002-09-12 05:08:00 +00002148
Fred Drakedbbf76b2000-07-09 16:44:26 +00002149 def _search_start(self):
2150 while 1:
2151 line = self.fp.readline()
2152 if not line:
2153 raise EOFError
2154 if line[:5] == '\001\001\001\001\n':
2155 return
Guido van Rossum8ca84201998-03-26 20:56:10 +00002156
Fred Drakedbbf76b2000-07-09 16:44:26 +00002157 def _search_end(self):
2158 while 1:
2159 pos = self.fp.tell()
2160 line = self.fp.readline()
2161 if not line:
2162 return
2163 if line == '\001\001\001\001\n':
2164 self.fp.seek(pos)
2165 return
Guido van Rossumc7b68821994-04-28 09:53:33 +00002166
Guido van Rossumc7b68821994-04-28 09:53:33 +00002167
Jack Jansen97157791995-10-23 13:59:53 +00002168class MHMailbox:
Guido van Rossum4bf12542002-09-12 05:08:00 +00002169
Barry Warsaw81ad67c2001-01-31 22:13:15 +00002170 def __init__(self, dirname, factory=rfc822.Message):
Fred Drakedbbf76b2000-07-09 16:44:26 +00002171 import re
Guido van Rossum0707fea2000-08-10 03:05:26 +00002172 pat = re.compile('^[1-9][0-9]*$')
Fred Drakedbbf76b2000-07-09 16:44:26 +00002173 self.dirname = dirname
Sjoerd Mullenderd2653a92000-08-11 07:48:36 +00002174 # the three following lines could be combined into:
2175 # list = map(long, filter(pat.match, os.listdir(self.dirname)))
2176 list = os.listdir(self.dirname)
2177 list = filter(pat.match, list)
Guido van Rossum0707fea2000-08-10 03:05:26 +00002178 list = map(long, list)
2179 list.sort()
2180 # This only works in Python 1.6 or later;
2181 # before that str() added 'L':
2182 self.boxes = map(str, list)
Raymond Hettingerb5ba8d72004-02-07 02:16:24 +00002183 self.boxes.reverse()
Barry Warsaw81ad67c2001-01-31 22:13:15 +00002184 self.factory = factory
Jack Jansen97157791995-10-23 13:59:53 +00002185
Fred Drake72987a42001-05-02 20:20:53 +00002186 def __iter__(self):
Guido van Rossum93a696f2001-09-13 01:29:13 +00002187 return iter(self.next, None)
Fred Drake72987a42001-05-02 20:20:53 +00002188
Fred Drakedbbf76b2000-07-09 16:44:26 +00002189 def next(self):
2190 if not self.boxes:
2191 return None
Raymond Hettingerb5ba8d72004-02-07 02:16:24 +00002192 fn = self.boxes.pop()
Fred Drakedbbf76b2000-07-09 16:44:26 +00002193 fp = open(os.path.join(self.dirname, fn))
Guido van Rossum4bf12542002-09-12 05:08:00 +00002194 msg = self.factory(fp)
2195 try:
2196 msg._mh_msgno = fn
2197 except (AttributeError, TypeError):
2198 pass
2199 return msg
Guido van Rossum8ca84201998-03-26 20:56:10 +00002200
Guido van Rossum9a4d6371998-12-23 22:05:42 +00002201
Guido van Rossumfdf58fe1997-05-15 14:33:09 +00002202class BabylMailbox(_Mailbox):
Guido van Rossum4bf12542002-09-12 05:08:00 +00002203
Fred Drakedbbf76b2000-07-09 16:44:26 +00002204 def _search_start(self):
2205 while 1:
2206 line = self.fp.readline()
2207 if not line:
2208 raise EOFError
2209 if line == '*** EOOH ***\n':
2210 return
Guido van Rossumfdf58fe1997-05-15 14:33:09 +00002211
Fred Drakedbbf76b2000-07-09 16:44:26 +00002212 def _search_end(self):
2213 while 1:
2214 pos = self.fp.tell()
2215 line = self.fp.readline()
2216 if not line:
2217 return
Johannes Gijsbers6abc6852004-08-21 12:30:26 +00002218 if line == '\037\014\n' or line == '\037':
Fred Drakedbbf76b2000-07-09 16:44:26 +00002219 self.fp.seek(pos)
2220 return
Guido van Rossumfdf58fe1997-05-15 14:33:09 +00002221
Andrew M. Kuchling1da4a942006-04-22 02:32:43 +00002222## End: classes from the original module (for backward compatibility).
Guido van Rossum62448671996-09-17 21:33:15 +00002223
2224
Andrew M. Kuchling1da4a942006-04-22 02:32:43 +00002225class Error(Exception):
2226 """Raised for module-specific errors."""
2227
2228class NoSuchMailboxError(Error):
2229 """The specified mailbox does not exist and won't be created."""
2230
2231class NotEmptyError(Error):
2232 """The specified mailbox is not empty and deletion was requested."""
2233
2234class ExternalClashError(Error):
2235 """Another process caused an action to fail."""
2236
2237class FormatError(Error):
2238 """A file appears to have an invalid format."""