blob: 3fe0ce42883055d05782c1c6d488514a3d5429ad [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
200 def _dump_message(self, message, target, mangle_from_=False):
201 # Most files are opened in binary mode to allow predictable seeking.
202 # To get native line endings on disk, the user-friendly \n line endings
203 # used in strings and by email.Message are translated here.
204 """Dump message contents to target file."""
Georg Brandl5a096e12007-01-22 19:40:21 +0000205 if isinstance(message, email.message.Message):
Andrew M. Kuchling1da4a942006-04-22 02:32:43 +0000206 buffer = StringIO.StringIO()
Georg Brandl5a096e12007-01-22 19:40:21 +0000207 gen = email.generator.Generator(buffer, mangle_from_, 0)
Andrew M. Kuchling1da4a942006-04-22 02:32:43 +0000208 gen.flatten(message)
209 buffer.seek(0)
210 target.write(buffer.read().replace('\n', os.linesep))
211 elif isinstance(message, str):
212 if mangle_from_:
213 message = message.replace('\nFrom ', '\n>From ')
214 message = message.replace('\n', os.linesep)
215 target.write(message)
216 elif hasattr(message, 'read'):
217 while True:
218 line = message.readline()
219 if line == '':
220 break
221 if mangle_from_ and line.startswith('From '):
222 line = '>From ' + line[5:]
223 line = line.replace('\n', os.linesep)
224 target.write(line)
225 else:
226 raise TypeError('Invalid message type: %s' % type(message))
227
228
229class Maildir(Mailbox):
230 """A qmail-style Maildir mailbox."""
231
232 colon = ':'
233
234 def __init__(self, dirname, factory=rfc822.Message, create=True):
235 """Initialize a Maildir instance."""
236 Mailbox.__init__(self, dirname, factory, create)
R David Murray8b26c4b2011-05-06 21:56:22 -0400237 self._paths = {
238 'tmp': os.path.join(self._path, 'tmp'),
239 'new': os.path.join(self._path, 'new'),
240 'cur': os.path.join(self._path, 'cur'),
241 }
Andrew M. Kuchling1da4a942006-04-22 02:32:43 +0000242 if not os.path.exists(self._path):
243 if create:
244 os.mkdir(self._path, 0700)
R David Murray8b26c4b2011-05-06 21:56:22 -0400245 for path in self._paths.values():
246 os.mkdir(path, 0o700)
Andrew M. Kuchling1da4a942006-04-22 02:32:43 +0000247 else:
248 raise NoSuchMailboxError(self._path)
249 self._toc = {}
Petri Lehtinen49aa72e2011-11-05 09:50:37 +0200250 self._toc_mtimes = {'cur': 0, 'new': 0}
251 self._last_read = 0 # Records last time we read cur/new
252 self._skewfactor = 0.1 # Adjust if os/fs clocks are skewing
Andrew M. Kuchling1da4a942006-04-22 02:32:43 +0000253
254 def add(self, message):
255 """Add message and return assigned key."""
256 tmp_file = self._create_tmp()
257 try:
258 self._dump_message(message, tmp_file)
R. David Murray008c0442011-02-11 23:03:13 +0000259 except BaseException:
260 tmp_file.close()
261 os.remove(tmp_file.name)
262 raise
263 _sync_close(tmp_file)
Andrew M. Kuchling1da4a942006-04-22 02:32:43 +0000264 if isinstance(message, MaildirMessage):
265 subdir = message.get_subdir()
266 suffix = self.colon + message.get_info()
267 if suffix == self.colon:
268 suffix = ''
269 else:
270 subdir = 'new'
271 suffix = ''
272 uniq = os.path.basename(tmp_file.name).split(self.colon)[0]
273 dest = os.path.join(self._path, subdir, uniq + suffix)
Andrew M. Kuchling978d8282006-11-09 21:16:46 +0000274 try:
275 if hasattr(os, 'link'):
276 os.link(tmp_file.name, dest)
277 os.remove(tmp_file.name)
278 else:
279 os.rename(tmp_file.name, dest)
280 except OSError, e:
281 os.remove(tmp_file.name)
282 if e.errno == errno.EEXIST:
283 raise ExternalClashError('Name clash with existing message: %s'
284 % dest)
285 else:
286 raise
Andrew M. Kuchling1da4a942006-04-22 02:32:43 +0000287 if isinstance(message, MaildirMessage):
288 os.utime(dest, (os.path.getatime(dest), message.get_date()))
289 return uniq
290
291 def remove(self, key):
292 """Remove the keyed message; raise KeyError if it doesn't exist."""
293 os.remove(os.path.join(self._path, self._lookup(key)))
294
295 def discard(self, key):
296 """If the keyed message exists, remove it."""
297 # This overrides an inapplicable implementation in the superclass.
298 try:
299 self.remove(key)
300 except KeyError:
301 pass
302 except OSError, e:
Martin v. Löwis08041d52006-05-04 14:27:52 +0000303 if e.errno != errno.ENOENT:
Tim Peters6d7cd7d2006-04-22 05:52:59 +0000304 raise
Andrew M. Kuchling1da4a942006-04-22 02:32:43 +0000305
306 def __setitem__(self, key, message):
307 """Replace the keyed message; raise KeyError if it doesn't exist."""
308 old_subpath = self._lookup(key)
309 temp_key = self.add(message)
310 temp_subpath = self._lookup(temp_key)
311 if isinstance(message, MaildirMessage):
312 # temp's subdir and suffix were specified by message.
313 dominant_subpath = temp_subpath
314 else:
315 # temp's subdir and suffix were defaults from add().
316 dominant_subpath = old_subpath
317 subdir = os.path.dirname(dominant_subpath)
318 if self.colon in dominant_subpath:
319 suffix = self.colon + dominant_subpath.split(self.colon)[-1]
320 else:
321 suffix = ''
322 self.discard(key)
323 new_path = os.path.join(self._path, subdir, key + suffix)
324 os.rename(os.path.join(self._path, temp_subpath), new_path)
325 if isinstance(message, MaildirMessage):
326 os.utime(new_path, (os.path.getatime(new_path),
327 message.get_date()))
328
329 def get_message(self, key):
330 """Return a Message representation or raise a KeyError."""
331 subpath = self._lookup(key)
Andrew M. Kuchling214db632006-05-02 21:44:33 +0000332 f = open(os.path.join(self._path, subpath), 'r')
Andrew M. Kuchling1da4a942006-04-22 02:32:43 +0000333 try:
Andrew M. Kuchling15ce8802008-01-19 20:12:04 +0000334 if self._factory:
335 msg = self._factory(f)
336 else:
337 msg = MaildirMessage(f)
Andrew M. Kuchling1da4a942006-04-22 02:32:43 +0000338 finally:
339 f.close()
340 subdir, name = os.path.split(subpath)
341 msg.set_subdir(subdir)
342 if self.colon in name:
343 msg.set_info(name.split(self.colon)[-1])
344 msg.set_date(os.path.getmtime(os.path.join(self._path, subpath)))
345 return msg
346
347 def get_string(self, key):
348 """Return a string representation or raise a KeyError."""
Andrew M. Kuchling214db632006-05-02 21:44:33 +0000349 f = open(os.path.join(self._path, self._lookup(key)), 'r')
Andrew M. Kuchling1da4a942006-04-22 02:32:43 +0000350 try:
351 return f.read()
352 finally:
353 f.close()
354
355 def get_file(self, key):
356 """Return a file-like representation or raise a KeyError."""
Andrew M. Kuchling214db632006-05-02 21:44:33 +0000357 f = open(os.path.join(self._path, self._lookup(key)), 'rb')
Andrew M. Kuchling1da4a942006-04-22 02:32:43 +0000358 return _ProxyFile(f)
359
360 def iterkeys(self):
361 """Return an iterator over keys."""
362 self._refresh()
363 for key in self._toc:
364 try:
365 self._lookup(key)
366 except KeyError:
367 continue
368 yield key
369
370 def has_key(self, key):
371 """Return True if the keyed message exists, False otherwise."""
372 self._refresh()
373 return key in self._toc
374
375 def __len__(self):
376 """Return a count of messages in the mailbox."""
377 self._refresh()
378 return len(self._toc)
379
380 def flush(self):
381 """Write any pending changes to disk."""
Antoine Pitroue4c6b162009-11-01 21:29:33 +0000382 # Maildir changes are always written immediately, so there's nothing
R David Murray8b26c4b2011-05-06 21:56:22 -0400383 # to do.
384 pass
Andrew M. Kuchling1da4a942006-04-22 02:32:43 +0000385
386 def lock(self):
387 """Lock the mailbox."""
388 return
389
390 def unlock(self):
391 """Unlock the mailbox if it is locked."""
392 return
393
394 def close(self):
395 """Flush and close the mailbox."""
396 return
397
398 def list_folders(self):
399 """Return a list of folder names."""
400 result = []
401 for entry in os.listdir(self._path):
402 if len(entry) > 1 and entry[0] == '.' and \
403 os.path.isdir(os.path.join(self._path, entry)):
404 result.append(entry[1:])
405 return result
406
407 def get_folder(self, folder):
408 """Return a Maildir instance for the named folder."""
Andrew M. Kuchlinga3e5d372006-11-09 13:27:07 +0000409 return Maildir(os.path.join(self._path, '.' + folder),
410 factory=self._factory,
411 create=False)
Andrew M. Kuchling1da4a942006-04-22 02:32:43 +0000412
413 def add_folder(self, folder):
414 """Create a folder and return a Maildir instance representing it."""
415 path = os.path.join(self._path, '.' + folder)
Andrew M. Kuchlinga3e5d372006-11-09 13:27:07 +0000416 result = Maildir(path, factory=self._factory)
Andrew M. Kuchling1da4a942006-04-22 02:32:43 +0000417 maildirfolder_path = os.path.join(path, 'maildirfolder')
418 if not os.path.exists(maildirfolder_path):
Andrew M. Kuchling70a6dbd2008-08-04 01:43:43 +0000419 os.close(os.open(maildirfolder_path, os.O_CREAT | os.O_WRONLY,
420 0666))
Andrew M. Kuchling1da4a942006-04-22 02:32:43 +0000421 return result
422
423 def remove_folder(self, folder):
424 """Delete the named folder, which must be empty."""
425 path = os.path.join(self._path, '.' + folder)
426 for entry in os.listdir(os.path.join(path, 'new')) + \
427 os.listdir(os.path.join(path, 'cur')):
428 if len(entry) < 1 or entry[0] != '.':
429 raise NotEmptyError('Folder contains message(s): %s' % folder)
430 for entry in os.listdir(path):
431 if entry != 'new' and entry != 'cur' and entry != 'tmp' and \
432 os.path.isdir(os.path.join(path, entry)):
433 raise NotEmptyError("Folder contains subdirectory '%s': %s" %
434 (folder, entry))
435 for root, dirs, files in os.walk(path, topdown=False):
436 for entry in files:
437 os.remove(os.path.join(root, entry))
438 for entry in dirs:
439 os.rmdir(os.path.join(root, entry))
440 os.rmdir(path)
441
442 def clean(self):
443 """Delete old files in "tmp"."""
444 now = time.time()
445 for entry in os.listdir(os.path.join(self._path, 'tmp')):
446 path = os.path.join(self._path, 'tmp', entry)
447 if now - os.path.getatime(path) > 129600: # 60 * 60 * 36
448 os.remove(path)
449
450 _count = 1 # This is used to generate unique file names.
451
452 def _create_tmp(self):
453 """Create a file in the tmp subdirectory and open and return it."""
454 now = time.time()
455 hostname = socket.gethostname()
456 if '/' in hostname:
457 hostname = hostname.replace('/', r'\057')
458 if ':' in hostname:
459 hostname = hostname.replace(':', r'\072')
460 uniq = "%s.M%sP%sQ%s.%s" % (int(now), int(now % 1 * 1e6), os.getpid(),
461 Maildir._count, hostname)
462 path = os.path.join(self._path, 'tmp', uniq)
463 try:
464 os.stat(path)
465 except OSError, e:
466 if e.errno == errno.ENOENT:
467 Maildir._count += 1
Andrew M. Kuchling978d8282006-11-09 21:16:46 +0000468 try:
469 return _create_carefully(path)
470 except OSError, e:
471 if e.errno != errno.EEXIST:
472 raise
Andrew M. Kuchling1da4a942006-04-22 02:32:43 +0000473 else:
474 raise
Andrew M. Kuchling978d8282006-11-09 21:16:46 +0000475
476 # Fall through to here if stat succeeded or open raised EEXIST.
477 raise ExternalClashError('Name clash prevented file creation: %s' %
478 path)
Andrew M. Kuchling1da4a942006-04-22 02:32:43 +0000479
480 def _refresh(self):
481 """Update table of contents mapping."""
R David Murray8b26c4b2011-05-06 21:56:22 -0400482 # If it has been less than two seconds since the last _refresh() call,
483 # we have to unconditionally re-read the mailbox just in case it has
484 # been modified, because os.path.mtime() has a 2 sec resolution in the
485 # most common worst case (FAT) and a 1 sec resolution typically. This
486 # results in a few unnecessary re-reads when _refresh() is called
487 # multiple times in that interval, but once the clock ticks over, we
488 # will only re-read as needed. Because the filesystem might be being
489 # served by an independent system with its own clock, we record and
490 # compare with the mtimes from the filesystem. Because the other
491 # system's clock might be skewing relative to our clock, we add an
492 # extra delta to our wait. The default is one tenth second, but is an
493 # instance variable and so can be adjusted if dealing with a
494 # particularly skewed or irregular system.
495 if time.time() - self._last_read > 2 + self._skewfactor:
496 refresh = False
497 for subdir in self._toc_mtimes:
498 mtime = os.path.getmtime(self._paths[subdir])
499 if mtime > self._toc_mtimes[subdir]:
500 refresh = True
501 self._toc_mtimes[subdir] = mtime
502 if not refresh:
Antoine Pitroud35b8c72009-11-01 00:30:13 +0000503 return
R David Murray8b26c4b2011-05-06 21:56:22 -0400504 # Refresh toc
Andrew M. Kuchling1da4a942006-04-22 02:32:43 +0000505 self._toc = {}
R David Murray8b26c4b2011-05-06 21:56:22 -0400506 for subdir in self._toc_mtimes:
507 path = self._paths[subdir]
Andrew M. Kuchling420d4eb2009-05-02 19:17:28 +0000508 for entry in os.listdir(path):
509 p = os.path.join(path, entry)
Andrew M. Kuchling2b09ef02007-07-14 21:56:19 +0000510 if os.path.isdir(p):
511 continue
Andrew M. Kuchling1da4a942006-04-22 02:32:43 +0000512 uniq = entry.split(self.colon)[0]
513 self._toc[uniq] = os.path.join(subdir, entry)
R David Murray8b26c4b2011-05-06 21:56:22 -0400514 self._last_read = time.time()
Andrew M. Kuchling420d4eb2009-05-02 19:17:28 +0000515
Andrew M. Kuchling1da4a942006-04-22 02:32:43 +0000516 def _lookup(self, key):
517 """Use TOC to return subpath for given key, or raise a KeyError."""
518 try:
519 if os.path.exists(os.path.join(self._path, self._toc[key])):
520 return self._toc[key]
521 except KeyError:
522 pass
523 self._refresh()
524 try:
525 return self._toc[key]
526 except KeyError:
527 raise KeyError('No message with key: %s' % key)
528
529 # This method is for backward compatibility only.
530 def next(self):
531 """Return the next message in a one-time iteration."""
532 if not hasattr(self, '_onetime_keys'):
533 self._onetime_keys = self.iterkeys()
534 while True:
535 try:
536 return self[self._onetime_keys.next()]
537 except StopIteration:
538 return None
539 except KeyError:
540 continue
541
542
543class _singlefileMailbox(Mailbox):
544 """A single-file mailbox."""
545
546 def __init__(self, path, factory=None, create=True):
547 """Initialize a single-file mailbox."""
548 Mailbox.__init__(self, path, factory, create)
549 try:
Andrew M. Kuchling214db632006-05-02 21:44:33 +0000550 f = open(self._path, 'rb+')
Andrew M. Kuchling1da4a942006-04-22 02:32:43 +0000551 except IOError, e:
552 if e.errno == errno.ENOENT:
553 if create:
Andrew M. Kuchling214db632006-05-02 21:44:33 +0000554 f = open(self._path, 'wb+')
Andrew M. Kuchling1da4a942006-04-22 02:32:43 +0000555 else:
556 raise NoSuchMailboxError(self._path)
R. David Murray1a337902011-03-03 18:17:40 +0000557 elif e.errno in (errno.EACCES, errno.EROFS):
Andrew M. Kuchling214db632006-05-02 21:44:33 +0000558 f = open(self._path, 'rb')
Andrew M. Kuchling1da4a942006-04-22 02:32:43 +0000559 else:
560 raise
561 self._file = f
562 self._toc = None
563 self._next_key = 0
Petri Lehtinen45f0d982012-06-28 13:48:17 +0300564 self._pending = False # No changes require rewriting the file.
565 self._pending_sync = False # No need to sync the file
Andrew M. Kuchling1da4a942006-04-22 02:32:43 +0000566 self._locked = False
Petri Lehtinen45f0d982012-06-28 13:48:17 +0300567 self._file_length = None # Used to record mailbox size
Andrew M. Kuchling1da4a942006-04-22 02:32:43 +0000568
569 def add(self, message):
570 """Add message and return assigned key."""
571 self._lookup()
572 self._toc[self._next_key] = self._append_message(message)
573 self._next_key += 1
Petri Lehtinen45f0d982012-06-28 13:48:17 +0300574 # _append_message appends the message to the mailbox file. We
575 # don't need a full rewrite + rename, sync is enough.
576 self._pending_sync = True
Andrew M. Kuchling1da4a942006-04-22 02:32:43 +0000577 return self._next_key - 1
578
579 def remove(self, key):
580 """Remove the keyed message; raise KeyError if it doesn't exist."""
581 self._lookup(key)
582 del self._toc[key]
583 self._pending = True
584
585 def __setitem__(self, key, message):
586 """Replace the keyed message; raise KeyError if it doesn't exist."""
587 self._lookup(key)
588 self._toc[key] = self._append_message(message)
589 self._pending = True
590
591 def iterkeys(self):
592 """Return an iterator over keys."""
593 self._lookup()
594 for key in self._toc.keys():
595 yield key
596
597 def has_key(self, key):
598 """Return True if the keyed message exists, False otherwise."""
599 self._lookup()
600 return key in self._toc
601
602 def __len__(self):
603 """Return a count of messages in the mailbox."""
604 self._lookup()
605 return len(self._toc)
606
607 def lock(self):
608 """Lock the mailbox."""
609 if not self._locked:
610 _lock_file(self._file)
611 self._locked = True
612
613 def unlock(self):
614 """Unlock the mailbox if it is locked."""
615 if self._locked:
616 _unlock_file(self._file)
617 self._locked = False
618
619 def flush(self):
620 """Write any pending changes to disk."""
621 if not self._pending:
Petri Lehtinen45f0d982012-06-28 13:48:17 +0300622 if self._pending_sync:
623 # Messages have only been added, so syncing the file
624 # is enough.
625 _sync_flush(self._file)
626 self._pending_sync = False
Andrew M. Kuchling1da4a942006-04-22 02:32:43 +0000627 return
Andrew M. Kuchlingeca4c312006-12-20 19:48:20 +0000628
629 # In order to be writing anything out at all, self._toc must
630 # already have been generated (and presumably has been modified
631 # by adding or deleting an item).
632 assert self._toc is not None
Tim Petersf733abb2007-01-30 03:03:46 +0000633
Andrew M. Kuchlingeca4c312006-12-20 19:48:20 +0000634 # Check length of self._file; if it's changed, some other process
635 # has modified the mailbox since we scanned it.
636 self._file.seek(0, 2)
637 cur_len = self._file.tell()
638 if cur_len != self._file_length:
639 raise ExternalClashError('Size of mailbox file changed '
640 '(expected %i, found %i)' %
641 (self._file_length, cur_len))
Tim Petersf733abb2007-01-30 03:03:46 +0000642
Andrew M. Kuchling1da4a942006-04-22 02:32:43 +0000643 new_file = _create_temporary(self._path)
644 try:
645 new_toc = {}
646 self._pre_mailbox_hook(new_file)
647 for key in sorted(self._toc.keys()):
648 start, stop = self._toc[key]
649 self._file.seek(start)
650 self._pre_message_hook(new_file)
651 new_start = new_file.tell()
652 while True:
653 buffer = self._file.read(min(4096,
654 stop - self._file.tell()))
655 if buffer == '':
656 break
657 new_file.write(buffer)
658 new_toc[key] = (new_start, new_file.tell())
659 self._post_message_hook(new_file)
Petri Lehtinen7cf66992012-06-15 20:50:51 +0300660 self._file_length = new_file.tell()
Andrew M. Kuchling1da4a942006-04-22 02:32:43 +0000661 except:
662 new_file.close()
663 os.remove(new_file.name)
664 raise
Andrew M. Kuchlingb5686da2006-11-09 13:51:14 +0000665 _sync_close(new_file)
666 # self._file is about to get replaced, so no need to sync.
Andrew M. Kuchling1da4a942006-04-22 02:32:43 +0000667 self._file.close()
Petri Lehtinend07de402012-06-29 15:09:12 +0300668 # Make sure the new file's mode is the same as the old file's
669 mode = os.stat(self._path).st_mode
670 os.chmod(new_file.name, mode)
Andrew M. Kuchling1da4a942006-04-22 02:32:43 +0000671 try:
672 os.rename(new_file.name, self._path)
673 except OSError, e:
Andrew MacIntyreafa358f2006-07-23 13:04:00 +0000674 if e.errno == errno.EEXIST or \
675 (os.name == 'os2' and e.errno == errno.EACCES):
Andrew M. Kuchling1da4a942006-04-22 02:32:43 +0000676 os.remove(self._path)
677 os.rename(new_file.name, self._path)
678 else:
679 raise
Andrew M. Kuchling214db632006-05-02 21:44:33 +0000680 self._file = open(self._path, 'rb+')
Andrew M. Kuchling1da4a942006-04-22 02:32:43 +0000681 self._toc = new_toc
682 self._pending = False
Petri Lehtinen45f0d982012-06-28 13:48:17 +0300683 self._pending_sync = False
Andrew M. Kuchling1da4a942006-04-22 02:32:43 +0000684 if self._locked:
Andrew M. Kuchling0f871832006-10-27 16:55:34 +0000685 _lock_file(self._file, dotlock=False)
Andrew M. Kuchling1da4a942006-04-22 02:32:43 +0000686
687 def _pre_mailbox_hook(self, f):
688 """Called before writing the mailbox to file f."""
689 return
690
691 def _pre_message_hook(self, f):
692 """Called before writing each message to file f."""
693 return
694
695 def _post_message_hook(self, f):
696 """Called after writing each message to file f."""
697 return
698
699 def close(self):
700 """Flush and close the mailbox."""
701 self.flush()
702 if self._locked:
703 self.unlock()
Andrew M. Kuchlingb5686da2006-11-09 13:51:14 +0000704 self._file.close() # Sync has been done by self.flush() above.
Andrew M. Kuchling1da4a942006-04-22 02:32:43 +0000705
706 def _lookup(self, key=None):
707 """Return (start, stop) or raise KeyError."""
708 if self._toc is None:
709 self._generate_toc()
710 if key is not None:
711 try:
712 return self._toc[key]
713 except KeyError:
714 raise KeyError('No message with key: %s' % key)
715
716 def _append_message(self, message):
717 """Append message to mailbox and return (start, stop) offsets."""
718 self._file.seek(0, 2)
R. David Murray008c0442011-02-11 23:03:13 +0000719 before = self._file.tell()
Petri Lehtinen4e6e5a02012-06-29 13:43:37 +0300720 if len(self._toc) == 0 and not self._pending:
721 # This is the first message, and the _pre_mailbox_hook
722 # hasn't yet been called. If self._pending is True,
723 # messages have been removed, so _pre_mailbox_hook must
724 # have been called already.
Petri Lehtinen45f0d982012-06-28 13:48:17 +0300725 self._pre_mailbox_hook(self._file)
R. David Murray008c0442011-02-11 23:03:13 +0000726 try:
727 self._pre_message_hook(self._file)
728 offsets = self._install_message(message)
729 self._post_message_hook(self._file)
730 except BaseException:
731 self._file.truncate(before)
732 raise
Andrew M. Kuchling1da4a942006-04-22 02:32:43 +0000733 self._file.flush()
Andrew M. Kuchlingeca4c312006-12-20 19:48:20 +0000734 self._file_length = self._file.tell() # Record current length of mailbox
Andrew M. Kuchling1da4a942006-04-22 02:32:43 +0000735 return offsets
736
737
738
739class _mboxMMDF(_singlefileMailbox):
740 """An mbox or MMDF mailbox."""
741
742 _mangle_from_ = True
743
744 def get_message(self, key):
745 """Return a Message representation or raise a KeyError."""
746 start, stop = self._lookup(key)
747 self._file.seek(start)
748 from_line = self._file.readline().replace(os.linesep, '')
749 string = self._file.read(stop - self._file.tell())
750 msg = self._message_factory(string.replace(os.linesep, '\n'))
751 msg.set_from(from_line[5:])
752 return msg
753
754 def get_string(self, key, from_=False):
755 """Return a string representation or raise a KeyError."""
756 start, stop = self._lookup(key)
757 self._file.seek(start)
758 if not from_:
759 self._file.readline()
760 string = self._file.read(stop - self._file.tell())
761 return string.replace(os.linesep, '\n')
762
763 def get_file(self, key, from_=False):
764 """Return a file-like representation or raise a KeyError."""
765 start, stop = self._lookup(key)
766 self._file.seek(start)
767 if not from_:
768 self._file.readline()
769 return _PartialFile(self._file, self._file.tell(), stop)
770
771 def _install_message(self, message):
772 """Format a message and blindly write to self._file."""
773 from_line = None
774 if isinstance(message, str) and message.startswith('From '):
775 newline = message.find('\n')
776 if newline != -1:
777 from_line = message[:newline]
778 message = message[newline + 1:]
779 else:
780 from_line = message
781 message = ''
782 elif isinstance(message, _mboxMMDFMessage):
783 from_line = 'From ' + message.get_from()
Georg Brandl5a096e12007-01-22 19:40:21 +0000784 elif isinstance(message, email.message.Message):
Andrew M. Kuchling1da4a942006-04-22 02:32:43 +0000785 from_line = message.get_unixfrom() # May be None.
786 if from_line is None:
787 from_line = 'From MAILER-DAEMON %s' % time.asctime(time.gmtime())
788 start = self._file.tell()
789 self._file.write(from_line + os.linesep)
790 self._dump_message(message, self._file, self._mangle_from_)
791 stop = self._file.tell()
792 return (start, stop)
793
794
795class mbox(_mboxMMDF):
796 """A classic mbox mailbox."""
797
798 _mangle_from_ = True
799
800 def __init__(self, path, factory=None, create=True):
801 """Initialize an mbox mailbox."""
802 self._message_factory = mboxMessage
803 _mboxMMDF.__init__(self, path, factory, create)
804
805 def _pre_message_hook(self, f):
806 """Called before writing each message to file f."""
807 if f.tell() != 0:
808 f.write(os.linesep)
809
810 def _generate_toc(self):
811 """Generate key-to-(start, stop) table of contents."""
812 starts, stops = [], []
813 self._file.seek(0)
814 while True:
815 line_pos = self._file.tell()
816 line = self._file.readline()
817 if line.startswith('From '):
818 if len(stops) < len(starts):
819 stops.append(line_pos - len(os.linesep))
820 starts.append(line_pos)
821 elif line == '':
822 stops.append(line_pos)
823 break
824 self._toc = dict(enumerate(zip(starts, stops)))
825 self._next_key = len(self._toc)
Andrew M. Kuchlingeca4c312006-12-20 19:48:20 +0000826 self._file_length = self._file.tell()
Andrew M. Kuchling1da4a942006-04-22 02:32:43 +0000827
828
829class MMDF(_mboxMMDF):
830 """An MMDF mailbox."""
831
832 def __init__(self, path, factory=None, create=True):
833 """Initialize an MMDF mailbox."""
834 self._message_factory = MMDFMessage
835 _mboxMMDF.__init__(self, path, factory, create)
836
837 def _pre_message_hook(self, f):
838 """Called before writing each message to file f."""
839 f.write('\001\001\001\001' + os.linesep)
840
841 def _post_message_hook(self, f):
842 """Called after writing each message to file f."""
843 f.write(os.linesep + '\001\001\001\001' + os.linesep)
844
845 def _generate_toc(self):
846 """Generate key-to-(start, stop) table of contents."""
847 starts, stops = [], []
848 self._file.seek(0)
849 next_pos = 0
850 while True:
851 line_pos = next_pos
852 line = self._file.readline()
853 next_pos = self._file.tell()
854 if line.startswith('\001\001\001\001' + os.linesep):
855 starts.append(next_pos)
856 while True:
857 line_pos = next_pos
858 line = self._file.readline()
859 next_pos = self._file.tell()
860 if line == '\001\001\001\001' + os.linesep:
861 stops.append(line_pos - len(os.linesep))
862 break
863 elif line == '':
864 stops.append(line_pos)
865 break
866 elif line == '':
867 break
868 self._toc = dict(enumerate(zip(starts, stops)))
869 self._next_key = len(self._toc)
Andrew M. Kuchlingeca4c312006-12-20 19:48:20 +0000870 self._file.seek(0, 2)
871 self._file_length = self._file.tell()
Andrew M. Kuchling1da4a942006-04-22 02:32:43 +0000872
873
874class MH(Mailbox):
875 """An MH mailbox."""
876
877 def __init__(self, path, factory=None, create=True):
878 """Initialize an MH instance."""
879 Mailbox.__init__(self, path, factory, create)
880 if not os.path.exists(self._path):
881 if create:
882 os.mkdir(self._path, 0700)
883 os.close(os.open(os.path.join(self._path, '.mh_sequences'),
884 os.O_CREAT | os.O_EXCL | os.O_WRONLY, 0600))
885 else:
886 raise NoSuchMailboxError(self._path)
887 self._locked = False
888
889 def add(self, message):
890 """Add message and return assigned key."""
891 keys = self.keys()
892 if len(keys) == 0:
893 new_key = 1
894 else:
895 new_key = max(keys) + 1
896 new_path = os.path.join(self._path, str(new_key))
897 f = _create_carefully(new_path)
R. David Murrayf9e34232011-02-12 02:03:56 +0000898 closed = False
Andrew M. Kuchling1da4a942006-04-22 02:32:43 +0000899 try:
900 if self._locked:
901 _lock_file(f)
902 try:
R. David Murray008c0442011-02-11 23:03:13 +0000903 try:
904 self._dump_message(message, f)
905 except BaseException:
R. David Murrayf9e34232011-02-12 02:03:56 +0000906 # Unlock and close so it can be deleted on Windows
907 if self._locked:
908 _unlock_file(f)
909 _sync_close(f)
910 closed = True
R. David Murray008c0442011-02-11 23:03:13 +0000911 os.remove(new_path)
912 raise
Andrew M. Kuchling1da4a942006-04-22 02:32:43 +0000913 if isinstance(message, MHMessage):
914 self._dump_sequences(message, new_key)
915 finally:
916 if self._locked:
917 _unlock_file(f)
918 finally:
R. David Murrayf9e34232011-02-12 02:03:56 +0000919 if not closed:
920 _sync_close(f)
Andrew M. Kuchling1da4a942006-04-22 02:32:43 +0000921 return new_key
922
923 def remove(self, key):
924 """Remove the keyed message; raise KeyError if it doesn't exist."""
925 path = os.path.join(self._path, str(key))
926 try:
Andrew M. Kuchling214db632006-05-02 21:44:33 +0000927 f = open(path, 'rb+')
Andrew M. Kuchling1da4a942006-04-22 02:32:43 +0000928 except IOError, e:
929 if e.errno == errno.ENOENT:
930 raise KeyError('No message with key: %s' % key)
931 else:
932 raise
Andrew M. Kuchlingb72b0eb2010-02-22 18:42:07 +0000933 else:
Andrew M. Kuchling1da4a942006-04-22 02:32:43 +0000934 f.close()
Andrew M. Kuchlingb72b0eb2010-02-22 18:42:07 +0000935 os.remove(path)
Andrew M. Kuchling1da4a942006-04-22 02:32:43 +0000936
937 def __setitem__(self, key, message):
938 """Replace the keyed message; raise KeyError if it doesn't exist."""
939 path = os.path.join(self._path, str(key))
940 try:
Andrew M. Kuchling214db632006-05-02 21:44:33 +0000941 f = open(path, 'rb+')
Andrew M. Kuchling1da4a942006-04-22 02:32:43 +0000942 except IOError, e:
943 if e.errno == errno.ENOENT:
944 raise KeyError('No message with key: %s' % key)
945 else:
946 raise
947 try:
948 if self._locked:
949 _lock_file(f)
950 try:
951 os.close(os.open(path, os.O_WRONLY | os.O_TRUNC))
952 self._dump_message(message, f)
953 if isinstance(message, MHMessage):
954 self._dump_sequences(message, key)
955 finally:
956 if self._locked:
957 _unlock_file(f)
958 finally:
Andrew M. Kuchlingb5686da2006-11-09 13:51:14 +0000959 _sync_close(f)
Andrew M. Kuchling1da4a942006-04-22 02:32:43 +0000960
961 def get_message(self, key):
962 """Return a Message representation or raise a KeyError."""
963 try:
964 if self._locked:
Andrew M. Kuchling214db632006-05-02 21:44:33 +0000965 f = open(os.path.join(self._path, str(key)), 'r+')
Andrew M. Kuchling1da4a942006-04-22 02:32:43 +0000966 else:
Andrew M. Kuchling214db632006-05-02 21:44:33 +0000967 f = open(os.path.join(self._path, str(key)), 'r')
Andrew M. Kuchling1da4a942006-04-22 02:32:43 +0000968 except IOError, e:
969 if e.errno == errno.ENOENT:
970 raise KeyError('No message with key: %s' % key)
971 else:
972 raise
973 try:
974 if self._locked:
975 _lock_file(f)
976 try:
977 msg = MHMessage(f)
978 finally:
979 if self._locked:
980 _unlock_file(f)
981 finally:
982 f.close()
R. David Murray52720c52009-04-02 14:05:35 +0000983 for name, key_list in self.get_sequences().iteritems():
Andrew M. Kuchling1da4a942006-04-22 02:32:43 +0000984 if key in key_list:
985 msg.add_sequence(name)
986 return msg
987
988 def get_string(self, key):
989 """Return a string representation or raise a KeyError."""
990 try:
991 if self._locked:
Andrew M. Kuchling214db632006-05-02 21:44:33 +0000992 f = open(os.path.join(self._path, str(key)), 'r+')
Andrew M. Kuchling1da4a942006-04-22 02:32:43 +0000993 else:
Andrew M. Kuchling214db632006-05-02 21:44:33 +0000994 f = open(os.path.join(self._path, str(key)), 'r')
Andrew M. Kuchling1da4a942006-04-22 02:32:43 +0000995 except IOError, e:
996 if e.errno == errno.ENOENT:
997 raise KeyError('No message with key: %s' % key)
998 else:
999 raise
1000 try:
1001 if self._locked:
1002 _lock_file(f)
1003 try:
1004 return f.read()
1005 finally:
1006 if self._locked:
1007 _unlock_file(f)
1008 finally:
1009 f.close()
1010
1011 def get_file(self, key):
1012 """Return a file-like representation or raise a KeyError."""
1013 try:
Andrew M. Kuchling214db632006-05-02 21:44:33 +00001014 f = open(os.path.join(self._path, str(key)), 'rb')
Andrew M. Kuchling1da4a942006-04-22 02:32:43 +00001015 except IOError, e:
1016 if e.errno == errno.ENOENT:
1017 raise KeyError('No message with key: %s' % key)
1018 else:
1019 raise
1020 return _ProxyFile(f)
1021
1022 def iterkeys(self):
1023 """Return an iterator over keys."""
1024 return iter(sorted(int(entry) for entry in os.listdir(self._path)
1025 if entry.isdigit()))
1026
1027 def has_key(self, key):
1028 """Return True if the keyed message exists, False otherwise."""
1029 return os.path.exists(os.path.join(self._path, str(key)))
1030
1031 def __len__(self):
1032 """Return a count of messages in the mailbox."""
1033 return len(list(self.iterkeys()))
1034
1035 def lock(self):
1036 """Lock the mailbox."""
1037 if not self._locked:
Andrew M. Kuchling214db632006-05-02 21:44:33 +00001038 self._file = open(os.path.join(self._path, '.mh_sequences'), 'rb+')
Andrew M. Kuchling1da4a942006-04-22 02:32:43 +00001039 _lock_file(self._file)
1040 self._locked = True
1041
1042 def unlock(self):
1043 """Unlock the mailbox if it is locked."""
1044 if self._locked:
1045 _unlock_file(self._file)
Andrew M. Kuchlingb5686da2006-11-09 13:51:14 +00001046 _sync_close(self._file)
Andrew M. Kuchling1da4a942006-04-22 02:32:43 +00001047 del self._file
1048 self._locked = False
1049
1050 def flush(self):
1051 """Write any pending changes to the disk."""
1052 return
1053
1054 def close(self):
1055 """Flush and close the mailbox."""
1056 if self._locked:
1057 self.unlock()
1058
1059 def list_folders(self):
1060 """Return a list of folder names."""
1061 result = []
1062 for entry in os.listdir(self._path):
1063 if os.path.isdir(os.path.join(self._path, entry)):
1064 result.append(entry)
1065 return result
1066
1067 def get_folder(self, folder):
1068 """Return an MH instance for the named folder."""
Andrew M. Kuchlinga3e5d372006-11-09 13:27:07 +00001069 return MH(os.path.join(self._path, folder),
1070 factory=self._factory, create=False)
Andrew M. Kuchling1da4a942006-04-22 02:32:43 +00001071
1072 def add_folder(self, folder):
1073 """Create a folder and return an MH instance representing it."""
Andrew M. Kuchlinga3e5d372006-11-09 13:27:07 +00001074 return MH(os.path.join(self._path, folder),
1075 factory=self._factory)
Andrew M. Kuchling1da4a942006-04-22 02:32:43 +00001076
1077 def remove_folder(self, folder):
1078 """Delete the named folder, which must be empty."""
1079 path = os.path.join(self._path, folder)
1080 entries = os.listdir(path)
1081 if entries == ['.mh_sequences']:
1082 os.remove(os.path.join(path, '.mh_sequences'))
1083 elif entries == []:
1084 pass
1085 else:
1086 raise NotEmptyError('Folder not empty: %s' % self._path)
1087 os.rmdir(path)
1088
1089 def get_sequences(self):
1090 """Return a name-to-key-list dictionary to define each sequence."""
1091 results = {}
Andrew M. Kuchling214db632006-05-02 21:44:33 +00001092 f = open(os.path.join(self._path, '.mh_sequences'), 'r')
Andrew M. Kuchling1da4a942006-04-22 02:32:43 +00001093 try:
1094 all_keys = set(self.keys())
1095 for line in f:
1096 try:
1097 name, contents = line.split(':')
1098 keys = set()
1099 for spec in contents.split():
1100 if spec.isdigit():
1101 keys.add(int(spec))
1102 else:
1103 start, stop = (int(x) for x in spec.split('-'))
1104 keys.update(range(start, stop + 1))
1105 results[name] = [key for key in sorted(keys) \
1106 if key in all_keys]
1107 if len(results[name]) == 0:
1108 del results[name]
1109 except ValueError:
1110 raise FormatError('Invalid sequence specification: %s' %
1111 line.rstrip())
1112 finally:
1113 f.close()
1114 return results
1115
1116 def set_sequences(self, sequences):
1117 """Set sequences using the given name-to-key-list dictionary."""
Andrew M. Kuchling214db632006-05-02 21:44:33 +00001118 f = open(os.path.join(self._path, '.mh_sequences'), 'r+')
Andrew M. Kuchling1da4a942006-04-22 02:32:43 +00001119 try:
1120 os.close(os.open(f.name, os.O_WRONLY | os.O_TRUNC))
1121 for name, keys in sequences.iteritems():
1122 if len(keys) == 0:
1123 continue
1124 f.write('%s:' % name)
1125 prev = None
1126 completing = False
1127 for key in sorted(set(keys)):
1128 if key - 1 == prev:
1129 if not completing:
1130 completing = True
1131 f.write('-')
1132 elif completing:
1133 completing = False
1134 f.write('%s %s' % (prev, key))
1135 else:
1136 f.write(' %s' % key)
1137 prev = key
1138 if completing:
1139 f.write(str(prev) + '\n')
1140 else:
1141 f.write('\n')
1142 finally:
Andrew M. Kuchlingb5686da2006-11-09 13:51:14 +00001143 _sync_close(f)
Andrew M. Kuchling1da4a942006-04-22 02:32:43 +00001144
1145 def pack(self):
1146 """Re-name messages to eliminate numbering gaps. Invalidates keys."""
1147 sequences = self.get_sequences()
1148 prev = 0
1149 changes = []
1150 for key in self.iterkeys():
1151 if key - 1 != prev:
1152 changes.append((key, prev + 1))
Andrew M. Kuchling8c456f32006-11-17 13:30:25 +00001153 if hasattr(os, 'link'):
1154 os.link(os.path.join(self._path, str(key)),
1155 os.path.join(self._path, str(prev + 1)))
1156 os.unlink(os.path.join(self._path, str(key)))
1157 else:
1158 os.rename(os.path.join(self._path, str(key)),
1159 os.path.join(self._path, str(prev + 1)))
Andrew M. Kuchling1da4a942006-04-22 02:32:43 +00001160 prev += 1
1161 self._next_key = prev + 1
1162 if len(changes) == 0:
1163 return
1164 for name, key_list in sequences.items():
1165 for old, new in changes:
1166 if old in key_list:
1167 key_list[key_list.index(old)] = new
1168 self.set_sequences(sequences)
1169
1170 def _dump_sequences(self, message, key):
1171 """Inspect a new MHMessage and update sequences appropriately."""
1172 pending_sequences = message.get_sequences()
1173 all_sequences = self.get_sequences()
1174 for name, key_list in all_sequences.iteritems():
1175 if name in pending_sequences:
1176 key_list.append(key)
1177 elif key in key_list:
1178 del key_list[key_list.index(key)]
1179 for sequence in pending_sequences:
1180 if sequence not in all_sequences:
1181 all_sequences[sequence] = [key]
1182 self.set_sequences(all_sequences)
1183
1184
1185class Babyl(_singlefileMailbox):
1186 """An Rmail-style Babyl mailbox."""
1187
1188 _special_labels = frozenset(('unseen', 'deleted', 'filed', 'answered',
1189 'forwarded', 'edited', 'resent'))
1190
1191 def __init__(self, path, factory=None, create=True):
1192 """Initialize a Babyl mailbox."""
1193 _singlefileMailbox.__init__(self, path, factory, create)
1194 self._labels = {}
1195
1196 def add(self, message):
1197 """Add message and return assigned key."""
1198 key = _singlefileMailbox.add(self, message)
1199 if isinstance(message, BabylMessage):
1200 self._labels[key] = message.get_labels()
1201 return key
1202
1203 def remove(self, key):
1204 """Remove the keyed message; raise KeyError if it doesn't exist."""
1205 _singlefileMailbox.remove(self, key)
1206 if key in self._labels:
1207 del self._labels[key]
1208
1209 def __setitem__(self, key, message):
1210 """Replace the keyed message; raise KeyError if it doesn't exist."""
1211 _singlefileMailbox.__setitem__(self, key, message)
1212 if isinstance(message, BabylMessage):
1213 self._labels[key] = message.get_labels()
1214
1215 def get_message(self, key):
1216 """Return a Message representation or raise a KeyError."""
1217 start, stop = self._lookup(key)
1218 self._file.seek(start)
1219 self._file.readline() # Skip '1,' line specifying labels.
1220 original_headers = StringIO.StringIO()
1221 while True:
1222 line = self._file.readline()
1223 if line == '*** EOOH ***' + os.linesep or line == '':
1224 break
1225 original_headers.write(line.replace(os.linesep, '\n'))
1226 visible_headers = StringIO.StringIO()
1227 while True:
1228 line = self._file.readline()
1229 if line == os.linesep or line == '':
1230 break
1231 visible_headers.write(line.replace(os.linesep, '\n'))
1232 body = self._file.read(stop - self._file.tell()).replace(os.linesep,
1233 '\n')
1234 msg = BabylMessage(original_headers.getvalue() + body)
1235 msg.set_visible(visible_headers.getvalue())
1236 if key in self._labels:
1237 msg.set_labels(self._labels[key])
1238 return msg
1239
1240 def get_string(self, key):
1241 """Return a string representation or raise a KeyError."""
1242 start, stop = self._lookup(key)
1243 self._file.seek(start)
1244 self._file.readline() # Skip '1,' line specifying labels.
1245 original_headers = StringIO.StringIO()
1246 while True:
1247 line = self._file.readline()
1248 if line == '*** EOOH ***' + os.linesep or line == '':
1249 break
1250 original_headers.write(line.replace(os.linesep, '\n'))
1251 while True:
1252 line = self._file.readline()
1253 if line == os.linesep or line == '':
1254 break
1255 return original_headers.getvalue() + \
1256 self._file.read(stop - self._file.tell()).replace(os.linesep,
1257 '\n')
1258
1259 def get_file(self, key):
1260 """Return a file-like representation or raise a KeyError."""
1261 return StringIO.StringIO(self.get_string(key).replace('\n',
1262 os.linesep))
1263
1264 def get_labels(self):
1265 """Return a list of user-defined labels in the mailbox."""
1266 self._lookup()
1267 labels = set()
1268 for label_list in self._labels.values():
1269 labels.update(label_list)
1270 labels.difference_update(self._special_labels)
1271 return list(labels)
1272
1273 def _generate_toc(self):
1274 """Generate key-to-(start, stop) table of contents."""
1275 starts, stops = [], []
1276 self._file.seek(0)
1277 next_pos = 0
1278 label_lists = []
1279 while True:
1280 line_pos = next_pos
1281 line = self._file.readline()
1282 next_pos = self._file.tell()
1283 if line == '\037\014' + os.linesep:
1284 if len(stops) < len(starts):
1285 stops.append(line_pos - len(os.linesep))
1286 starts.append(next_pos)
1287 labels = [label.strip() for label
1288 in self._file.readline()[1:].split(',')
1289 if label.strip() != '']
1290 label_lists.append(labels)
1291 elif line == '\037' or line == '\037' + os.linesep:
1292 if len(stops) < len(starts):
1293 stops.append(line_pos - len(os.linesep))
1294 elif line == '':
1295 stops.append(line_pos - len(os.linesep))
1296 break
1297 self._toc = dict(enumerate(zip(starts, stops)))
1298 self._labels = dict(enumerate(label_lists))
1299 self._next_key = len(self._toc)
Andrew M. Kuchlingeca4c312006-12-20 19:48:20 +00001300 self._file.seek(0, 2)
1301 self._file_length = self._file.tell()
Tim Petersf733abb2007-01-30 03:03:46 +00001302
Andrew M. Kuchling1da4a942006-04-22 02:32:43 +00001303 def _pre_mailbox_hook(self, f):
1304 """Called before writing the mailbox to file f."""
1305 f.write('BABYL OPTIONS:%sVersion: 5%sLabels:%s%s\037' %
1306 (os.linesep, os.linesep, ','.join(self.get_labels()),
1307 os.linesep))
1308
1309 def _pre_message_hook(self, f):
1310 """Called before writing each message to file f."""
1311 f.write('\014' + os.linesep)
1312
1313 def _post_message_hook(self, f):
1314 """Called after writing each message to file f."""
1315 f.write(os.linesep + '\037')
1316
1317 def _install_message(self, message):
1318 """Write message contents and return (start, stop)."""
1319 start = self._file.tell()
1320 if isinstance(message, BabylMessage):
1321 special_labels = []
1322 labels = []
1323 for label in message.get_labels():
1324 if label in self._special_labels:
1325 special_labels.append(label)
1326 else:
1327 labels.append(label)
1328 self._file.write('1')
1329 for label in special_labels:
1330 self._file.write(', ' + label)
1331 self._file.write(',,')
1332 for label in labels:
1333 self._file.write(' ' + label + ',')
1334 self._file.write(os.linesep)
1335 else:
1336 self._file.write('1,,' + os.linesep)
Georg Brandl5a096e12007-01-22 19:40:21 +00001337 if isinstance(message, email.message.Message):
Andrew M. Kuchling1da4a942006-04-22 02:32:43 +00001338 orig_buffer = StringIO.StringIO()
Georg Brandl5a096e12007-01-22 19:40:21 +00001339 orig_generator = email.generator.Generator(orig_buffer, False, 0)
Andrew M. Kuchling1da4a942006-04-22 02:32:43 +00001340 orig_generator.flatten(message)
1341 orig_buffer.seek(0)
1342 while True:
1343 line = orig_buffer.readline()
1344 self._file.write(line.replace('\n', os.linesep))
1345 if line == '\n' or line == '':
1346 break
1347 self._file.write('*** EOOH ***' + os.linesep)
1348 if isinstance(message, BabylMessage):
1349 vis_buffer = StringIO.StringIO()
Georg Brandl5a096e12007-01-22 19:40:21 +00001350 vis_generator = email.generator.Generator(vis_buffer, False, 0)
Andrew M. Kuchling1da4a942006-04-22 02:32:43 +00001351 vis_generator.flatten(message.get_visible())
1352 while True:
1353 line = vis_buffer.readline()
1354 self._file.write(line.replace('\n', os.linesep))
1355 if line == '\n' or line == '':
1356 break
1357 else:
1358 orig_buffer.seek(0)
1359 while True:
1360 line = orig_buffer.readline()
1361 self._file.write(line.replace('\n', os.linesep))
1362 if line == '\n' or line == '':
1363 break
1364 while True:
1365 buffer = orig_buffer.read(4096) # Buffer size is arbitrary.
1366 if buffer == '':
1367 break
1368 self._file.write(buffer.replace('\n', os.linesep))
1369 elif isinstance(message, str):
1370 body_start = message.find('\n\n') + 2
1371 if body_start - 2 != -1:
1372 self._file.write(message[:body_start].replace('\n',
1373 os.linesep))
1374 self._file.write('*** EOOH ***' + os.linesep)
1375 self._file.write(message[:body_start].replace('\n',
1376 os.linesep))
1377 self._file.write(message[body_start:].replace('\n',
1378 os.linesep))
1379 else:
1380 self._file.write('*** EOOH ***' + os.linesep + os.linesep)
1381 self._file.write(message.replace('\n', os.linesep))
1382 elif hasattr(message, 'readline'):
1383 original_pos = message.tell()
1384 first_pass = True
1385 while True:
1386 line = message.readline()
1387 self._file.write(line.replace('\n', os.linesep))
1388 if line == '\n' or line == '':
Andrew M. Kuchling1da4a942006-04-22 02:32:43 +00001389 if first_pass:
1390 first_pass = False
Petri Lehtinen2d44cee2012-08-15 14:22:46 +03001391 self._file.write('*** EOOH ***' + os.linesep)
Andrew M. Kuchling1da4a942006-04-22 02:32:43 +00001392 message.seek(original_pos)
1393 else:
1394 break
1395 while True:
1396 buffer = message.read(4096) # Buffer size is arbitrary.
1397 if buffer == '':
1398 break
1399 self._file.write(buffer.replace('\n', os.linesep))
1400 else:
1401 raise TypeError('Invalid message type: %s' % type(message))
1402 stop = self._file.tell()
1403 return (start, stop)
1404
1405
Georg Brandl5a096e12007-01-22 19:40:21 +00001406class Message(email.message.Message):
Andrew M. Kuchling1da4a942006-04-22 02:32:43 +00001407 """Message with mailbox-format-specific properties."""
1408
1409 def __init__(self, message=None):
1410 """Initialize a Message instance."""
Georg Brandl5a096e12007-01-22 19:40:21 +00001411 if isinstance(message, email.message.Message):
Andrew M. Kuchling1da4a942006-04-22 02:32:43 +00001412 self._become_message(copy.deepcopy(message))
1413 if isinstance(message, Message):
1414 message._explain_to(self)
1415 elif isinstance(message, str):
1416 self._become_message(email.message_from_string(message))
1417 elif hasattr(message, "read"):
1418 self._become_message(email.message_from_file(message))
1419 elif message is None:
Georg Brandl5a096e12007-01-22 19:40:21 +00001420 email.message.Message.__init__(self)
Andrew M. Kuchling1da4a942006-04-22 02:32:43 +00001421 else:
1422 raise TypeError('Invalid message type: %s' % type(message))
1423
1424 def _become_message(self, message):
1425 """Assume the non-format-specific state of message."""
1426 for name in ('_headers', '_unixfrom', '_payload', '_charset',
1427 'preamble', 'epilogue', 'defects', '_default_type'):
1428 self.__dict__[name] = message.__dict__[name]
1429
1430 def _explain_to(self, message):
1431 """Copy format-specific state to message insofar as possible."""
1432 if isinstance(message, Message):
1433 return # There's nothing format-specific to explain.
1434 else:
1435 raise TypeError('Cannot convert to specified type')
1436
1437
1438class MaildirMessage(Message):
1439 """Message with Maildir-specific properties."""
1440
1441 def __init__(self, message=None):
1442 """Initialize a MaildirMessage instance."""
1443 self._subdir = 'new'
1444 self._info = ''
1445 self._date = time.time()
1446 Message.__init__(self, message)
1447
1448 def get_subdir(self):
1449 """Return 'new' or 'cur'."""
1450 return self._subdir
1451
1452 def set_subdir(self, subdir):
1453 """Set subdir to 'new' or 'cur'."""
1454 if subdir == 'new' or subdir == 'cur':
1455 self._subdir = subdir
1456 else:
1457 raise ValueError("subdir must be 'new' or 'cur': %s" % subdir)
1458
1459 def get_flags(self):
1460 """Return as a string the flags that are set."""
1461 if self._info.startswith('2,'):
1462 return self._info[2:]
1463 else:
1464 return ''
1465
1466 def set_flags(self, flags):
1467 """Set the given flags and unset all others."""
1468 self._info = '2,' + ''.join(sorted(flags))
1469
1470 def add_flag(self, flag):
1471 """Set the given flag(s) without changing others."""
1472 self.set_flags(''.join(set(self.get_flags()) | set(flag)))
1473
1474 def remove_flag(self, flag):
1475 """Unset the given string flag(s) without changing others."""
1476 if self.get_flags() != '':
1477 self.set_flags(''.join(set(self.get_flags()) - set(flag)))
1478
1479 def get_date(self):
1480 """Return delivery date of message, in seconds since the epoch."""
1481 return self._date
1482
1483 def set_date(self, date):
1484 """Set delivery date of message, in seconds since the epoch."""
1485 try:
1486 self._date = float(date)
1487 except ValueError:
1488 raise TypeError("can't convert to float: %s" % date)
1489
1490 def get_info(self):
1491 """Get the message's "info" as a string."""
1492 return self._info
1493
1494 def set_info(self, info):
1495 """Set the message's "info" string."""
1496 if isinstance(info, str):
1497 self._info = info
1498 else:
1499 raise TypeError('info must be a string: %s' % type(info))
1500
1501 def _explain_to(self, message):
1502 """Copy Maildir-specific state to message insofar as possible."""
1503 if isinstance(message, MaildirMessage):
1504 message.set_flags(self.get_flags())
1505 message.set_subdir(self.get_subdir())
1506 message.set_date(self.get_date())
1507 elif isinstance(message, _mboxMMDFMessage):
1508 flags = set(self.get_flags())
1509 if 'S' in flags:
1510 message.add_flag('R')
1511 if self.get_subdir() == 'cur':
1512 message.add_flag('O')
1513 if 'T' in flags:
1514 message.add_flag('D')
1515 if 'F' in flags:
1516 message.add_flag('F')
1517 if 'R' in flags:
1518 message.add_flag('A')
1519 message.set_from('MAILER-DAEMON', time.gmtime(self.get_date()))
1520 elif isinstance(message, MHMessage):
1521 flags = set(self.get_flags())
1522 if 'S' not in flags:
1523 message.add_sequence('unseen')
1524 if 'R' in flags:
1525 message.add_sequence('replied')
1526 if 'F' in flags:
1527 message.add_sequence('flagged')
1528 elif isinstance(message, BabylMessage):
1529 flags = set(self.get_flags())
1530 if 'S' not in flags:
1531 message.add_label('unseen')
1532 if 'T' in flags:
1533 message.add_label('deleted')
1534 if 'R' in flags:
1535 message.add_label('answered')
1536 if 'P' in flags:
1537 message.add_label('forwarded')
1538 elif isinstance(message, Message):
1539 pass
1540 else:
1541 raise TypeError('Cannot convert to specified type: %s' %
1542 type(message))
1543
1544
1545class _mboxMMDFMessage(Message):
1546 """Message with mbox- or MMDF-specific properties."""
1547
1548 def __init__(self, message=None):
1549 """Initialize an mboxMMDFMessage instance."""
1550 self.set_from('MAILER-DAEMON', True)
Georg Brandl5a096e12007-01-22 19:40:21 +00001551 if isinstance(message, email.message.Message):
Andrew M. Kuchling1da4a942006-04-22 02:32:43 +00001552 unixfrom = message.get_unixfrom()
1553 if unixfrom is not None and unixfrom.startswith('From '):
1554 self.set_from(unixfrom[5:])
1555 Message.__init__(self, message)
1556
1557 def get_from(self):
1558 """Return contents of "From " line."""
1559 return self._from
1560
1561 def set_from(self, from_, time_=None):
1562 """Set "From " line, formatting and appending time_ if specified."""
1563 if time_ is not None:
1564 if time_ is True:
1565 time_ = time.gmtime()
1566 from_ += ' ' + time.asctime(time_)
1567 self._from = from_
1568
1569 def get_flags(self):
1570 """Return as a string the flags that are set."""
1571 return self.get('Status', '') + self.get('X-Status', '')
1572
1573 def set_flags(self, flags):
1574 """Set the given flags and unset all others."""
1575 flags = set(flags)
1576 status_flags, xstatus_flags = '', ''
1577 for flag in ('R', 'O'):
1578 if flag in flags:
1579 status_flags += flag
1580 flags.remove(flag)
1581 for flag in ('D', 'F', 'A'):
1582 if flag in flags:
1583 xstatus_flags += flag
1584 flags.remove(flag)
1585 xstatus_flags += ''.join(sorted(flags))
1586 try:
1587 self.replace_header('Status', status_flags)
1588 except KeyError:
1589 self.add_header('Status', status_flags)
1590 try:
1591 self.replace_header('X-Status', xstatus_flags)
1592 except KeyError:
1593 self.add_header('X-Status', xstatus_flags)
1594
1595 def add_flag(self, flag):
1596 """Set the given flag(s) without changing others."""
1597 self.set_flags(''.join(set(self.get_flags()) | set(flag)))
1598
1599 def remove_flag(self, flag):
1600 """Unset the given string flag(s) without changing others."""
1601 if 'Status' in self or 'X-Status' in self:
1602 self.set_flags(''.join(set(self.get_flags()) - set(flag)))
1603
1604 def _explain_to(self, message):
1605 """Copy mbox- or MMDF-specific state to message insofar as possible."""
1606 if isinstance(message, MaildirMessage):
1607 flags = set(self.get_flags())
1608 if 'O' in flags:
1609 message.set_subdir('cur')
1610 if 'F' in flags:
1611 message.add_flag('F')
1612 if 'A' in flags:
1613 message.add_flag('R')
1614 if 'R' in flags:
1615 message.add_flag('S')
1616 if 'D' in flags:
1617 message.add_flag('T')
1618 del message['status']
1619 del message['x-status']
1620 maybe_date = ' '.join(self.get_from().split()[-5:])
1621 try:
1622 message.set_date(calendar.timegm(time.strptime(maybe_date,
1623 '%a %b %d %H:%M:%S %Y')))
1624 except (ValueError, OverflowError):
1625 pass
1626 elif isinstance(message, _mboxMMDFMessage):
1627 message.set_flags(self.get_flags())
1628 message.set_from(self.get_from())
1629 elif isinstance(message, MHMessage):
1630 flags = set(self.get_flags())
1631 if 'R' not in flags:
1632 message.add_sequence('unseen')
1633 if 'A' in flags:
1634 message.add_sequence('replied')
1635 if 'F' in flags:
1636 message.add_sequence('flagged')
1637 del message['status']
1638 del message['x-status']
1639 elif isinstance(message, BabylMessage):
1640 flags = set(self.get_flags())
1641 if 'R' not in flags:
1642 message.add_label('unseen')
1643 if 'D' in flags:
1644 message.add_label('deleted')
1645 if 'A' in flags:
1646 message.add_label('answered')
1647 del message['status']
1648 del message['x-status']
1649 elif isinstance(message, Message):
1650 pass
1651 else:
1652 raise TypeError('Cannot convert to specified type: %s' %
1653 type(message))
1654
1655
1656class mboxMessage(_mboxMMDFMessage):
1657 """Message with mbox-specific properties."""
1658
1659
1660class MHMessage(Message):
1661 """Message with MH-specific properties."""
1662
1663 def __init__(self, message=None):
1664 """Initialize an MHMessage instance."""
1665 self._sequences = []
1666 Message.__init__(self, message)
1667
1668 def get_sequences(self):
1669 """Return a list of sequences that include the message."""
1670 return self._sequences[:]
1671
1672 def set_sequences(self, sequences):
1673 """Set the list of sequences that include the message."""
1674 self._sequences = list(sequences)
1675
1676 def add_sequence(self, sequence):
1677 """Add sequence to list of sequences including the message."""
1678 if isinstance(sequence, str):
1679 if not sequence in self._sequences:
1680 self._sequences.append(sequence)
1681 else:
1682 raise TypeError('sequence must be a string: %s' % type(sequence))
1683
1684 def remove_sequence(self, sequence):
1685 """Remove sequence from the list of sequences including the message."""
1686 try:
1687 self._sequences.remove(sequence)
1688 except ValueError:
1689 pass
1690
1691 def _explain_to(self, message):
1692 """Copy MH-specific state to message insofar as possible."""
1693 if isinstance(message, MaildirMessage):
1694 sequences = set(self.get_sequences())
1695 if 'unseen' in sequences:
1696 message.set_subdir('cur')
1697 else:
1698 message.set_subdir('cur')
1699 message.add_flag('S')
1700 if 'flagged' in sequences:
1701 message.add_flag('F')
1702 if 'replied' in sequences:
1703 message.add_flag('R')
1704 elif isinstance(message, _mboxMMDFMessage):
1705 sequences = set(self.get_sequences())
1706 if 'unseen' not in sequences:
1707 message.add_flag('RO')
1708 else:
1709 message.add_flag('O')
1710 if 'flagged' in sequences:
1711 message.add_flag('F')
1712 if 'replied' in sequences:
1713 message.add_flag('A')
1714 elif isinstance(message, MHMessage):
1715 for sequence in self.get_sequences():
1716 message.add_sequence(sequence)
1717 elif isinstance(message, BabylMessage):
1718 sequences = set(self.get_sequences())
1719 if 'unseen' in sequences:
1720 message.add_label('unseen')
1721 if 'replied' in sequences:
1722 message.add_label('answered')
1723 elif isinstance(message, Message):
1724 pass
1725 else:
1726 raise TypeError('Cannot convert to specified type: %s' %
1727 type(message))
1728
1729
1730class BabylMessage(Message):
1731 """Message with Babyl-specific properties."""
1732
1733 def __init__(self, message=None):
1734 """Initialize an BabylMessage instance."""
1735 self._labels = []
1736 self._visible = Message()
1737 Message.__init__(self, message)
1738
1739 def get_labels(self):
1740 """Return a list of labels on the message."""
1741 return self._labels[:]
1742
1743 def set_labels(self, labels):
1744 """Set the list of labels on the message."""
1745 self._labels = list(labels)
1746
1747 def add_label(self, label):
1748 """Add label to list of labels on the message."""
1749 if isinstance(label, str):
1750 if label not in self._labels:
1751 self._labels.append(label)
1752 else:
1753 raise TypeError('label must be a string: %s' % type(label))
1754
1755 def remove_label(self, label):
1756 """Remove label from the list of labels on the message."""
1757 try:
1758 self._labels.remove(label)
1759 except ValueError:
1760 pass
Tim Peters6d7cd7d2006-04-22 05:52:59 +00001761
Andrew M. Kuchling1da4a942006-04-22 02:32:43 +00001762 def get_visible(self):
1763 """Return a Message representation of visible headers."""
1764 return Message(self._visible)
1765
1766 def set_visible(self, visible):
1767 """Set the Message representation of visible headers."""
1768 self._visible = Message(visible)
1769
1770 def update_visible(self):
1771 """Update and/or sensibly generate a set of visible headers."""
1772 for header in self._visible.keys():
1773 if header in self:
1774 self._visible.replace_header(header, self[header])
1775 else:
1776 del self._visible[header]
1777 for header in ('Date', 'From', 'Reply-To', 'To', 'CC', 'Subject'):
1778 if header in self and header not in self._visible:
1779 self._visible[header] = self[header]
1780
1781 def _explain_to(self, message):
1782 """Copy Babyl-specific state to message insofar as possible."""
1783 if isinstance(message, MaildirMessage):
1784 labels = set(self.get_labels())
1785 if 'unseen' in labels:
1786 message.set_subdir('cur')
1787 else:
1788 message.set_subdir('cur')
1789 message.add_flag('S')
1790 if 'forwarded' in labels or 'resent' in labels:
1791 message.add_flag('P')
1792 if 'answered' in labels:
1793 message.add_flag('R')
1794 if 'deleted' in labels:
1795 message.add_flag('T')
1796 elif isinstance(message, _mboxMMDFMessage):
1797 labels = set(self.get_labels())
1798 if 'unseen' not in labels:
1799 message.add_flag('RO')
1800 else:
1801 message.add_flag('O')
1802 if 'deleted' in labels:
1803 message.add_flag('D')
1804 if 'answered' in labels:
1805 message.add_flag('A')
1806 elif isinstance(message, MHMessage):
1807 labels = set(self.get_labels())
1808 if 'unseen' in labels:
1809 message.add_sequence('unseen')
1810 if 'answered' in labels:
1811 message.add_sequence('replied')
1812 elif isinstance(message, BabylMessage):
1813 message.set_visible(self.get_visible())
1814 for label in self.get_labels():
1815 message.add_label(label)
1816 elif isinstance(message, Message):
1817 pass
1818 else:
1819 raise TypeError('Cannot convert to specified type: %s' %
1820 type(message))
1821
1822
1823class MMDFMessage(_mboxMMDFMessage):
1824 """Message with MMDF-specific properties."""
1825
1826
1827class _ProxyFile:
1828 """A read-only wrapper of a file."""
1829
1830 def __init__(self, f, pos=None):
1831 """Initialize a _ProxyFile."""
1832 self._file = f
1833 if pos is None:
1834 self._pos = f.tell()
1835 else:
1836 self._pos = pos
1837
1838 def read(self, size=None):
1839 """Read bytes."""
1840 return self._read(size, self._file.read)
1841
1842 def readline(self, size=None):
1843 """Read a line."""
1844 return self._read(size, self._file.readline)
1845
1846 def readlines(self, sizehint=None):
1847 """Read multiple lines."""
1848 result = []
1849 for line in self:
1850 result.append(line)
1851 if sizehint is not None:
1852 sizehint -= len(line)
1853 if sizehint <= 0:
1854 break
1855 return result
1856
1857 def __iter__(self):
1858 """Iterate over lines."""
1859 return iter(self.readline, "")
1860
1861 def tell(self):
1862 """Return the position."""
1863 return self._pos
1864
1865 def seek(self, offset, whence=0):
1866 """Change position."""
1867 if whence == 1:
1868 self._file.seek(self._pos)
1869 self._file.seek(offset, whence)
1870 self._pos = self._file.tell()
1871
1872 def close(self):
1873 """Close the file."""
R David Murrayf1138bb2011-06-17 22:23:04 -04001874 if hasattr(self, '_file'):
1875 if hasattr(self._file, 'close'):
1876 self._file.close()
1877 del self._file
Andrew M. Kuchling1da4a942006-04-22 02:32:43 +00001878
1879 def _read(self, size, read_method):
1880 """Read size bytes using read_method."""
1881 if size is None:
1882 size = -1
1883 self._file.seek(self._pos)
1884 result = read_method(size)
1885 self._pos = self._file.tell()
1886 return result
1887
1888
1889class _PartialFile(_ProxyFile):
1890 """A read-only wrapper of part of a file."""
1891
1892 def __init__(self, f, start=None, stop=None):
1893 """Initialize a _PartialFile."""
1894 _ProxyFile.__init__(self, f, start)
1895 self._start = start
1896 self._stop = stop
1897
1898 def tell(self):
1899 """Return the position with respect to start."""
1900 return _ProxyFile.tell(self) - self._start
1901
1902 def seek(self, offset, whence=0):
1903 """Change position, possibly with respect to start or stop."""
1904 if whence == 0:
1905 self._pos = self._start
1906 whence = 1
1907 elif whence == 2:
1908 self._pos = self._stop
1909 whence = 1
1910 _ProxyFile.seek(self, offset, whence)
1911
1912 def _read(self, size, read_method):
1913 """Read size bytes using read_method, honoring start and stop."""
1914 remaining = self._stop - self._pos
1915 if remaining <= 0:
1916 return ''
1917 if size is None or size < 0 or size > remaining:
1918 size = remaining
1919 return _ProxyFile._read(self, size, read_method)
1920
R David Murrayf1138bb2011-06-17 22:23:04 -04001921 def close(self):
1922 # do *not* close the underlying file object for partial files,
1923 # since it's global to the mailbox object
1924 if hasattr(self, '_file'):
1925 del self._file
1926
Andrew M. Kuchling1da4a942006-04-22 02:32:43 +00001927
1928def _lock_file(f, dotlock=True):
Andrew M. Kuchling55732592006-06-26 13:12:16 +00001929 """Lock file f using lockf and dot locking."""
Andrew M. Kuchling1da4a942006-04-22 02:32:43 +00001930 dotlock_done = False
1931 try:
1932 if fcntl:
1933 try:
1934 fcntl.lockf(f, fcntl.LOCK_EX | fcntl.LOCK_NB)
1935 except IOError, e:
R. David Murray1a337902011-03-03 18:17:40 +00001936 if e.errno in (errno.EAGAIN, errno.EACCES, errno.EROFS):
Andrew M. Kuchling1da4a942006-04-22 02:32:43 +00001937 raise ExternalClashError('lockf: lock unavailable: %s' %
1938 f.name)
1939 else:
1940 raise
Andrew M. Kuchling1da4a942006-04-22 02:32:43 +00001941 if dotlock:
1942 try:
1943 pre_lock = _create_temporary(f.name + '.lock')
1944 pre_lock.close()
1945 except IOError, e:
R. David Murray1a337902011-03-03 18:17:40 +00001946 if e.errno in (errno.EACCES, errno.EROFS):
Andrew M. Kuchling1da4a942006-04-22 02:32:43 +00001947 return # Without write access, just skip dotlocking.
1948 else:
1949 raise
1950 try:
1951 if hasattr(os, 'link'):
1952 os.link(pre_lock.name, f.name + '.lock')
1953 dotlock_done = True
1954 os.unlink(pre_lock.name)
1955 else:
1956 os.rename(pre_lock.name, f.name + '.lock')
1957 dotlock_done = True
1958 except OSError, e:
Andrew MacIntyreafa358f2006-07-23 13:04:00 +00001959 if e.errno == errno.EEXIST or \
1960 (os.name == 'os2' and e.errno == errno.EACCES):
Andrew M. Kuchling1da4a942006-04-22 02:32:43 +00001961 os.remove(pre_lock.name)
Tim Peters6d7cd7d2006-04-22 05:52:59 +00001962 raise ExternalClashError('dot lock unavailable: %s' %
Andrew M. Kuchling1da4a942006-04-22 02:32:43 +00001963 f.name)
1964 else:
1965 raise
1966 except:
1967 if fcntl:
1968 fcntl.lockf(f, fcntl.LOCK_UN)
Andrew M. Kuchling1da4a942006-04-22 02:32:43 +00001969 if dotlock_done:
1970 os.remove(f.name + '.lock')
1971 raise
1972
1973def _unlock_file(f):
Andrew M. Kuchling55732592006-06-26 13:12:16 +00001974 """Unlock file f using lockf and dot locking."""
Andrew M. Kuchling1da4a942006-04-22 02:32:43 +00001975 if fcntl:
1976 fcntl.lockf(f, fcntl.LOCK_UN)
Andrew M. Kuchling1da4a942006-04-22 02:32:43 +00001977 if os.path.exists(f.name + '.lock'):
1978 os.remove(f.name + '.lock')
1979
1980def _create_carefully(path):
1981 """Create a file if it doesn't exist and open for reading and writing."""
Andrew M. Kuchling70a6dbd2008-08-04 01:43:43 +00001982 fd = os.open(path, os.O_CREAT | os.O_EXCL | os.O_RDWR, 0666)
Andrew M. Kuchling1da4a942006-04-22 02:32:43 +00001983 try:
Andrew M. Kuchling214db632006-05-02 21:44:33 +00001984 return open(path, 'rb+')
Andrew M. Kuchling1da4a942006-04-22 02:32:43 +00001985 finally:
1986 os.close(fd)
1987
1988def _create_temporary(path):
1989 """Create a temp file based on path and open for reading and writing."""
1990 return _create_carefully('%s.%s.%s.%s' % (path, int(time.time()),
1991 socket.gethostname(),
1992 os.getpid()))
1993
Andrew M. Kuchlingb5686da2006-11-09 13:51:14 +00001994def _sync_flush(f):
1995 """Ensure changes to file f are physically on disk."""
1996 f.flush()
Andrew M. Kuchling16465682006-12-14 18:57:53 +00001997 if hasattr(os, 'fsync'):
1998 os.fsync(f.fileno())
Andrew M. Kuchlingb5686da2006-11-09 13:51:14 +00001999
2000def _sync_close(f):
2001 """Close file f, ensuring all changes are physically on disk."""
2002 _sync_flush(f)
2003 f.close()
Andrew M. Kuchling1da4a942006-04-22 02:32:43 +00002004
2005## Start: classes from the original module (for backward compatibility).
2006
2007# Note that the Maildir class, whose name is unchanged, itself offers a next()
2008# method for backward compatibility.
Skip Montanaro17ab1232001-01-24 06:27:27 +00002009
Guido van Rossumc7b68821994-04-28 09:53:33 +00002010class _Mailbox:
Guido van Rossum4bf12542002-09-12 05:08:00 +00002011
Barry Warsaw81ad67c2001-01-31 22:13:15 +00002012 def __init__(self, fp, factory=rfc822.Message):
Fred Drakedbbf76b2000-07-09 16:44:26 +00002013 self.fp = fp
2014 self.seekp = 0
Barry Warsaw81ad67c2001-01-31 22:13:15 +00002015 self.factory = factory
Guido van Rossum8ca84201998-03-26 20:56:10 +00002016
Fred Drake72987a42001-05-02 20:20:53 +00002017 def __iter__(self):
Guido van Rossum93a696f2001-09-13 01:29:13 +00002018 return iter(self.next, None)
Fred Drake72987a42001-05-02 20:20:53 +00002019
Fred Drakedbbf76b2000-07-09 16:44:26 +00002020 def next(self):
2021 while 1:
2022 self.fp.seek(self.seekp)
2023 try:
2024 self._search_start()
2025 except EOFError:
2026 self.seekp = self.fp.tell()
2027 return None
2028 start = self.fp.tell()
2029 self._search_end()
2030 self.seekp = stop = self.fp.tell()
Fred Drake8152d322000-12-12 23:20:45 +00002031 if start != stop:
Fred Drakedbbf76b2000-07-09 16:44:26 +00002032 break
Andrew M. Kuchling1da4a942006-04-22 02:32:43 +00002033 return self.factory(_PartialFile(self.fp, start, stop))
Guido van Rossumc7b68821994-04-28 09:53:33 +00002034
Barry Warsawffd05ee2002-03-01 22:39:14 +00002035# Recommended to use PortableUnixMailbox instead!
Guido van Rossumc7b68821994-04-28 09:53:33 +00002036class UnixMailbox(_Mailbox):
Guido van Rossum4bf12542002-09-12 05:08:00 +00002037
Fred Drakedbbf76b2000-07-09 16:44:26 +00002038 def _search_start(self):
2039 while 1:
2040 pos = self.fp.tell()
2041 line = self.fp.readline()
2042 if not line:
2043 raise EOFError
2044 if line[:5] == 'From ' and self._isrealfromline(line):
2045 self.fp.seek(pos)
2046 return
Guido van Rossum8ca84201998-03-26 20:56:10 +00002047
Fred Drakedbbf76b2000-07-09 16:44:26 +00002048 def _search_end(self):
2049 self.fp.readline() # Throw away header line
2050 while 1:
2051 pos = self.fp.tell()
2052 line = self.fp.readline()
2053 if not line:
2054 return
2055 if line[:5] == 'From ' and self._isrealfromline(line):
2056 self.fp.seek(pos)
2057 return
Guido van Rossumc7b68821994-04-28 09:53:33 +00002058
Barry Warsaw81ad67c2001-01-31 22:13:15 +00002059 # An overridable mechanism to test for From-line-ness. You can either
2060 # specify a different regular expression or define a whole new
2061 # _isrealfromline() method. Note that this only gets called for lines
2062 # starting with the 5 characters "From ".
2063 #
2064 # BAW: According to
2065 #http://home.netscape.com/eng/mozilla/2.0/relnotes/demo/content-length.html
2066 # the only portable, reliable way to find message delimiters in a BSD (i.e
2067 # Unix mailbox) style folder is to search for "\n\nFrom .*\n", or at the
2068 # beginning of the file, "^From .*\n". While _fromlinepattern below seems
2069 # like a good idea, in practice, there are too many variations for more
2070 # strict parsing of the line to be completely accurate.
2071 #
2072 # _strict_isrealfromline() is the old version which tries to do stricter
2073 # parsing of the From_ line. _portable_isrealfromline() simply returns
2074 # true, since it's never called if the line doesn't already start with
2075 # "From ".
2076 #
2077 # This algorithm, and the way it interacts with _search_start() and
2078 # _search_end() may not be completely correct, because it doesn't check
2079 # that the two characters preceding "From " are \n\n or the beginning of
2080 # the file. Fixing this would require a more extensive rewrite than is
Barry Warsawda5628f2002-08-26 16:44:56 +00002081 # necessary. For convenience, we've added a PortableUnixMailbox class
Andrew M. Kuchlingb94c0c32007-01-22 20:27:50 +00002082 # which does no checking of the format of the 'From' line.
Guido van Rossumc7b68821994-04-28 09:53:33 +00002083
Andrew M. Kuchlingb78bb742007-01-22 20:26:40 +00002084 _fromlinepattern = (r"From \s*[^\s]+\s+\w\w\w\s+\w\w\w\s+\d?\d\s+"
2085 r"\d?\d:\d\d(:\d\d)?(\s+[^\s]+)?\s+\d\d\d\d\s*"
2086 r"[^\s]*\s*"
2087 "$")
Fred Drakedbbf76b2000-07-09 16:44:26 +00002088 _regexp = None
Guido van Rossumfbe63de1998-04-03 16:04:05 +00002089
Barry Warsaw81ad67c2001-01-31 22:13:15 +00002090 def _strict_isrealfromline(self, line):
Fred Drakedbbf76b2000-07-09 16:44:26 +00002091 if not self._regexp:
2092 import re
2093 self._regexp = re.compile(self._fromlinepattern)
2094 return self._regexp.match(line)
Guido van Rossumfbe63de1998-04-03 16:04:05 +00002095
Barry Warsaw81ad67c2001-01-31 22:13:15 +00002096 def _portable_isrealfromline(self, line):
Tim Petersbc0e9102002-04-04 22:55:58 +00002097 return True
Barry Warsaw81ad67c2001-01-31 22:13:15 +00002098
2099 _isrealfromline = _strict_isrealfromline
2100
2101
2102class PortableUnixMailbox(UnixMailbox):
2103 _isrealfromline = UnixMailbox._portable_isrealfromline
2104
Guido van Rossumfbe63de1998-04-03 16:04:05 +00002105
Guido van Rossumc7b68821994-04-28 09:53:33 +00002106class MmdfMailbox(_Mailbox):
Guido van Rossum4bf12542002-09-12 05:08:00 +00002107
Fred Drakedbbf76b2000-07-09 16:44:26 +00002108 def _search_start(self):
2109 while 1:
2110 line = self.fp.readline()
2111 if not line:
2112 raise EOFError
2113 if line[:5] == '\001\001\001\001\n':
2114 return
Guido van Rossum8ca84201998-03-26 20:56:10 +00002115
Fred Drakedbbf76b2000-07-09 16:44:26 +00002116 def _search_end(self):
2117 while 1:
2118 pos = self.fp.tell()
2119 line = self.fp.readline()
2120 if not line:
2121 return
2122 if line == '\001\001\001\001\n':
2123 self.fp.seek(pos)
2124 return
Guido van Rossumc7b68821994-04-28 09:53:33 +00002125
Guido van Rossumc7b68821994-04-28 09:53:33 +00002126
Jack Jansen97157791995-10-23 13:59:53 +00002127class MHMailbox:
Guido van Rossum4bf12542002-09-12 05:08:00 +00002128
Barry Warsaw81ad67c2001-01-31 22:13:15 +00002129 def __init__(self, dirname, factory=rfc822.Message):
Fred Drakedbbf76b2000-07-09 16:44:26 +00002130 import re
Guido van Rossum0707fea2000-08-10 03:05:26 +00002131 pat = re.compile('^[1-9][0-9]*$')
Fred Drakedbbf76b2000-07-09 16:44:26 +00002132 self.dirname = dirname
Sjoerd Mullenderd2653a92000-08-11 07:48:36 +00002133 # the three following lines could be combined into:
2134 # list = map(long, filter(pat.match, os.listdir(self.dirname)))
2135 list = os.listdir(self.dirname)
2136 list = filter(pat.match, list)
Guido van Rossum0707fea2000-08-10 03:05:26 +00002137 list = map(long, list)
2138 list.sort()
2139 # This only works in Python 1.6 or later;
2140 # before that str() added 'L':
2141 self.boxes = map(str, list)
Raymond Hettingerb5ba8d72004-02-07 02:16:24 +00002142 self.boxes.reverse()
Barry Warsaw81ad67c2001-01-31 22:13:15 +00002143 self.factory = factory
Jack Jansen97157791995-10-23 13:59:53 +00002144
Fred Drake72987a42001-05-02 20:20:53 +00002145 def __iter__(self):
Guido van Rossum93a696f2001-09-13 01:29:13 +00002146 return iter(self.next, None)
Fred Drake72987a42001-05-02 20:20:53 +00002147
Fred Drakedbbf76b2000-07-09 16:44:26 +00002148 def next(self):
2149 if not self.boxes:
2150 return None
Raymond Hettingerb5ba8d72004-02-07 02:16:24 +00002151 fn = self.boxes.pop()
Fred Drakedbbf76b2000-07-09 16:44:26 +00002152 fp = open(os.path.join(self.dirname, fn))
Guido van Rossum4bf12542002-09-12 05:08:00 +00002153 msg = self.factory(fp)
2154 try:
2155 msg._mh_msgno = fn
2156 except (AttributeError, TypeError):
2157 pass
2158 return msg
Guido van Rossum8ca84201998-03-26 20:56:10 +00002159
Guido van Rossum9a4d6371998-12-23 22:05:42 +00002160
Guido van Rossumfdf58fe1997-05-15 14:33:09 +00002161class BabylMailbox(_Mailbox):
Guido van Rossum4bf12542002-09-12 05:08:00 +00002162
Fred Drakedbbf76b2000-07-09 16:44:26 +00002163 def _search_start(self):
2164 while 1:
2165 line = self.fp.readline()
2166 if not line:
2167 raise EOFError
2168 if line == '*** EOOH ***\n':
2169 return
Guido van Rossumfdf58fe1997-05-15 14:33:09 +00002170
Fred Drakedbbf76b2000-07-09 16:44:26 +00002171 def _search_end(self):
2172 while 1:
2173 pos = self.fp.tell()
2174 line = self.fp.readline()
2175 if not line:
2176 return
Johannes Gijsbers6abc6852004-08-21 12:30:26 +00002177 if line == '\037\014\n' or line == '\037':
Fred Drakedbbf76b2000-07-09 16:44:26 +00002178 self.fp.seek(pos)
2179 return
Guido van Rossumfdf58fe1997-05-15 14:33:09 +00002180
Andrew M. Kuchling1da4a942006-04-22 02:32:43 +00002181## End: classes from the original module (for backward compatibility).
Guido van Rossum62448671996-09-17 21:33:15 +00002182
2183
Andrew M. Kuchling1da4a942006-04-22 02:32:43 +00002184class Error(Exception):
2185 """Raised for module-specific errors."""
2186
2187class NoSuchMailboxError(Error):
2188 """The specified mailbox does not exist and won't be created."""
2189
2190class NotEmptyError(Error):
2191 """The specified mailbox is not empty and deletion was requested."""
2192
2193class ExternalClashError(Error):
2194 """Another process caused an action to fail."""
2195
2196class FormatError(Error):
2197 """A file appears to have an invalid format."""