1 | n/a | """Read/write support for Maildir, mbox, MH, Babyl, and MMDF mailboxes.""" |
---|
2 | n/a | |
---|
3 | n/a | # Notes for authors of new mailbox subclasses: |
---|
4 | n/a | # |
---|
5 | n/a | # Remember to fsync() changes to disk before closing a modified file |
---|
6 | n/a | # or returning from a flush() method. See functions _sync_flush() and |
---|
7 | n/a | # _sync_close(). |
---|
8 | n/a | |
---|
9 | n/a | import os |
---|
10 | n/a | import time |
---|
11 | n/a | import calendar |
---|
12 | n/a | import socket |
---|
13 | n/a | import errno |
---|
14 | n/a | import copy |
---|
15 | n/a | import warnings |
---|
16 | n/a | import email |
---|
17 | n/a | import email.message |
---|
18 | n/a | import email.generator |
---|
19 | n/a | import io |
---|
20 | n/a | import contextlib |
---|
21 | n/a | try: |
---|
22 | n/a | import fcntl |
---|
23 | n/a | except ImportError: |
---|
24 | n/a | fcntl = None |
---|
25 | n/a | |
---|
26 | n/a | __all__ = ['Mailbox', 'Maildir', 'mbox', 'MH', 'Babyl', 'MMDF', |
---|
27 | n/a | 'Message', 'MaildirMessage', 'mboxMessage', 'MHMessage', |
---|
28 | n/a | 'BabylMessage', 'MMDFMessage', 'Error', 'NoSuchMailboxError', |
---|
29 | n/a | 'NotEmptyError', 'ExternalClashError', 'FormatError'] |
---|
30 | n/a | |
---|
31 | n/a | linesep = os.linesep.encode('ascii') |
---|
32 | n/a | |
---|
33 | n/a | class Mailbox: |
---|
34 | n/a | """A group of messages in a particular place.""" |
---|
35 | n/a | |
---|
36 | n/a | def __init__(self, path, factory=None, create=True): |
---|
37 | n/a | """Initialize a Mailbox instance.""" |
---|
38 | n/a | self._path = os.path.abspath(os.path.expanduser(path)) |
---|
39 | n/a | self._factory = factory |
---|
40 | n/a | |
---|
41 | n/a | def add(self, message): |
---|
42 | n/a | """Add message and return assigned key.""" |
---|
43 | n/a | raise NotImplementedError('Method must be implemented by subclass') |
---|
44 | n/a | |
---|
45 | n/a | def remove(self, key): |
---|
46 | n/a | """Remove the keyed message; raise KeyError if it doesn't exist.""" |
---|
47 | n/a | raise NotImplementedError('Method must be implemented by subclass') |
---|
48 | n/a | |
---|
49 | n/a | def __delitem__(self, key): |
---|
50 | n/a | self.remove(key) |
---|
51 | n/a | |
---|
52 | n/a | def discard(self, key): |
---|
53 | n/a | """If the keyed message exists, remove it.""" |
---|
54 | n/a | try: |
---|
55 | n/a | self.remove(key) |
---|
56 | n/a | except KeyError: |
---|
57 | n/a | pass |
---|
58 | n/a | |
---|
59 | n/a | def __setitem__(self, key, message): |
---|
60 | n/a | """Replace the keyed message; raise KeyError if it doesn't exist.""" |
---|
61 | n/a | raise NotImplementedError('Method must be implemented by subclass') |
---|
62 | n/a | |
---|
63 | n/a | def get(self, key, default=None): |
---|
64 | n/a | """Return the keyed message, or default if it doesn't exist.""" |
---|
65 | n/a | try: |
---|
66 | n/a | return self.__getitem__(key) |
---|
67 | n/a | except KeyError: |
---|
68 | n/a | return default |
---|
69 | n/a | |
---|
70 | n/a | def __getitem__(self, key): |
---|
71 | n/a | """Return the keyed message; raise KeyError if it doesn't exist.""" |
---|
72 | n/a | if not self._factory: |
---|
73 | n/a | return self.get_message(key) |
---|
74 | n/a | else: |
---|
75 | n/a | with contextlib.closing(self.get_file(key)) as file: |
---|
76 | n/a | return self._factory(file) |
---|
77 | n/a | |
---|
78 | n/a | def get_message(self, key): |
---|
79 | n/a | """Return a Message representation or raise a KeyError.""" |
---|
80 | n/a | raise NotImplementedError('Method must be implemented by subclass') |
---|
81 | n/a | |
---|
82 | n/a | def get_string(self, key): |
---|
83 | n/a | """Return a string representation or raise a KeyError. |
---|
84 | n/a | |
---|
85 | n/a | Uses email.message.Message to create a 7bit clean string |
---|
86 | n/a | representation of the message.""" |
---|
87 | n/a | return email.message_from_bytes(self.get_bytes(key)).as_string() |
---|
88 | n/a | |
---|
89 | n/a | def get_bytes(self, key): |
---|
90 | n/a | """Return a byte string representation or raise a KeyError.""" |
---|
91 | n/a | raise NotImplementedError('Method must be implemented by subclass') |
---|
92 | n/a | |
---|
93 | n/a | def get_file(self, key): |
---|
94 | n/a | """Return a file-like representation or raise a KeyError.""" |
---|
95 | n/a | raise NotImplementedError('Method must be implemented by subclass') |
---|
96 | n/a | |
---|
97 | n/a | def iterkeys(self): |
---|
98 | n/a | """Return an iterator over keys.""" |
---|
99 | n/a | raise NotImplementedError('Method must be implemented by subclass') |
---|
100 | n/a | |
---|
101 | n/a | def keys(self): |
---|
102 | n/a | """Return a list of keys.""" |
---|
103 | n/a | return list(self.iterkeys()) |
---|
104 | n/a | |
---|
105 | n/a | def itervalues(self): |
---|
106 | n/a | """Return an iterator over all messages.""" |
---|
107 | n/a | for key in self.iterkeys(): |
---|
108 | n/a | try: |
---|
109 | n/a | value = self[key] |
---|
110 | n/a | except KeyError: |
---|
111 | n/a | continue |
---|
112 | n/a | yield value |
---|
113 | n/a | |
---|
114 | n/a | def __iter__(self): |
---|
115 | n/a | return self.itervalues() |
---|
116 | n/a | |
---|
117 | n/a | def values(self): |
---|
118 | n/a | """Return a list of messages. Memory intensive.""" |
---|
119 | n/a | return list(self.itervalues()) |
---|
120 | n/a | |
---|
121 | n/a | def iteritems(self): |
---|
122 | n/a | """Return an iterator over (key, message) tuples.""" |
---|
123 | n/a | for key in self.iterkeys(): |
---|
124 | n/a | try: |
---|
125 | n/a | value = self[key] |
---|
126 | n/a | except KeyError: |
---|
127 | n/a | continue |
---|
128 | n/a | yield (key, value) |
---|
129 | n/a | |
---|
130 | n/a | def items(self): |
---|
131 | n/a | """Return a list of (key, message) tuples. Memory intensive.""" |
---|
132 | n/a | return list(self.iteritems()) |
---|
133 | n/a | |
---|
134 | n/a | def __contains__(self, key): |
---|
135 | n/a | """Return True if the keyed message exists, False otherwise.""" |
---|
136 | n/a | raise NotImplementedError('Method must be implemented by subclass') |
---|
137 | n/a | |
---|
138 | n/a | def __len__(self): |
---|
139 | n/a | """Return a count of messages in the mailbox.""" |
---|
140 | n/a | raise NotImplementedError('Method must be implemented by subclass') |
---|
141 | n/a | |
---|
142 | n/a | def clear(self): |
---|
143 | n/a | """Delete all messages.""" |
---|
144 | n/a | for key in self.keys(): |
---|
145 | n/a | self.discard(key) |
---|
146 | n/a | |
---|
147 | n/a | def pop(self, key, default=None): |
---|
148 | n/a | """Delete the keyed message and return it, or default.""" |
---|
149 | n/a | try: |
---|
150 | n/a | result = self[key] |
---|
151 | n/a | except KeyError: |
---|
152 | n/a | return default |
---|
153 | n/a | self.discard(key) |
---|
154 | n/a | return result |
---|
155 | n/a | |
---|
156 | n/a | def popitem(self): |
---|
157 | n/a | """Delete an arbitrary (key, message) pair and return it.""" |
---|
158 | n/a | for key in self.iterkeys(): |
---|
159 | n/a | return (key, self.pop(key)) # This is only run once. |
---|
160 | n/a | else: |
---|
161 | n/a | raise KeyError('No messages in mailbox') |
---|
162 | n/a | |
---|
163 | n/a | def update(self, arg=None): |
---|
164 | n/a | """Change the messages that correspond to certain keys.""" |
---|
165 | n/a | if hasattr(arg, 'iteritems'): |
---|
166 | n/a | source = arg.iteritems() |
---|
167 | n/a | elif hasattr(arg, 'items'): |
---|
168 | n/a | source = arg.items() |
---|
169 | n/a | else: |
---|
170 | n/a | source = arg |
---|
171 | n/a | bad_key = False |
---|
172 | n/a | for key, message in source: |
---|
173 | n/a | try: |
---|
174 | n/a | self[key] = message |
---|
175 | n/a | except KeyError: |
---|
176 | n/a | bad_key = True |
---|
177 | n/a | if bad_key: |
---|
178 | n/a | raise KeyError('No message with key(s)') |
---|
179 | n/a | |
---|
180 | n/a | def flush(self): |
---|
181 | n/a | """Write any pending changes to the disk.""" |
---|
182 | n/a | raise NotImplementedError('Method must be implemented by subclass') |
---|
183 | n/a | |
---|
184 | n/a | def lock(self): |
---|
185 | n/a | """Lock the mailbox.""" |
---|
186 | n/a | raise NotImplementedError('Method must be implemented by subclass') |
---|
187 | n/a | |
---|
188 | n/a | def unlock(self): |
---|
189 | n/a | """Unlock the mailbox if it is locked.""" |
---|
190 | n/a | raise NotImplementedError('Method must be implemented by subclass') |
---|
191 | n/a | |
---|
192 | n/a | def close(self): |
---|
193 | n/a | """Flush and close the mailbox.""" |
---|
194 | n/a | raise NotImplementedError('Method must be implemented by subclass') |
---|
195 | n/a | |
---|
196 | n/a | def _string_to_bytes(self, message): |
---|
197 | n/a | # If a message is not 7bit clean, we refuse to handle it since it |
---|
198 | n/a | # likely came from reading invalid messages in text mode, and that way |
---|
199 | n/a | # lies mojibake. |
---|
200 | n/a | try: |
---|
201 | n/a | return message.encode('ascii') |
---|
202 | n/a | except UnicodeError: |
---|
203 | n/a | raise ValueError("String input must be ASCII-only; " |
---|
204 | n/a | "use bytes or a Message instead") |
---|
205 | n/a | |
---|
206 | n/a | # Whether each message must end in a newline |
---|
207 | n/a | _append_newline = False |
---|
208 | n/a | |
---|
209 | n/a | def _dump_message(self, message, target, mangle_from_=False): |
---|
210 | n/a | # This assumes the target file is open in binary mode. |
---|
211 | n/a | """Dump message contents to target file.""" |
---|
212 | n/a | if isinstance(message, email.message.Message): |
---|
213 | n/a | buffer = io.BytesIO() |
---|
214 | n/a | gen = email.generator.BytesGenerator(buffer, mangle_from_, 0) |
---|
215 | n/a | gen.flatten(message) |
---|
216 | n/a | buffer.seek(0) |
---|
217 | n/a | data = buffer.read() |
---|
218 | n/a | data = data.replace(b'\n', linesep) |
---|
219 | n/a | target.write(data) |
---|
220 | n/a | if self._append_newline and not data.endswith(linesep): |
---|
221 | n/a | # Make sure the message ends with a newline |
---|
222 | n/a | target.write(linesep) |
---|
223 | n/a | elif isinstance(message, (str, bytes, io.StringIO)): |
---|
224 | n/a | if isinstance(message, io.StringIO): |
---|
225 | n/a | warnings.warn("Use of StringIO input is deprecated, " |
---|
226 | n/a | "use BytesIO instead", DeprecationWarning, 3) |
---|
227 | n/a | message = message.getvalue() |
---|
228 | n/a | if isinstance(message, str): |
---|
229 | n/a | message = self._string_to_bytes(message) |
---|
230 | n/a | if mangle_from_: |
---|
231 | n/a | message = message.replace(b'\nFrom ', b'\n>From ') |
---|
232 | n/a | message = message.replace(b'\n', linesep) |
---|
233 | n/a | target.write(message) |
---|
234 | n/a | if self._append_newline and not message.endswith(linesep): |
---|
235 | n/a | # Make sure the message ends with a newline |
---|
236 | n/a | target.write(linesep) |
---|
237 | n/a | elif hasattr(message, 'read'): |
---|
238 | n/a | if hasattr(message, 'buffer'): |
---|
239 | n/a | warnings.warn("Use of text mode files is deprecated, " |
---|
240 | n/a | "use a binary mode file instead", DeprecationWarning, 3) |
---|
241 | n/a | message = message.buffer |
---|
242 | n/a | lastline = None |
---|
243 | n/a | while True: |
---|
244 | n/a | line = message.readline() |
---|
245 | n/a | # Universal newline support. |
---|
246 | n/a | if line.endswith(b'\r\n'): |
---|
247 | n/a | line = line[:-2] + b'\n' |
---|
248 | n/a | elif line.endswith(b'\r'): |
---|
249 | n/a | line = line[:-1] + b'\n' |
---|
250 | n/a | if not line: |
---|
251 | n/a | break |
---|
252 | n/a | if mangle_from_ and line.startswith(b'From '): |
---|
253 | n/a | line = b'>From ' + line[5:] |
---|
254 | n/a | line = line.replace(b'\n', linesep) |
---|
255 | n/a | target.write(line) |
---|
256 | n/a | lastline = line |
---|
257 | n/a | if self._append_newline and lastline and not lastline.endswith(linesep): |
---|
258 | n/a | # Make sure the message ends with a newline |
---|
259 | n/a | target.write(linesep) |
---|
260 | n/a | else: |
---|
261 | n/a | raise TypeError('Invalid message type: %s' % type(message)) |
---|
262 | n/a | |
---|
263 | n/a | |
---|
264 | n/a | class Maildir(Mailbox): |
---|
265 | n/a | """A qmail-style Maildir mailbox.""" |
---|
266 | n/a | |
---|
267 | n/a | colon = ':' |
---|
268 | n/a | |
---|
269 | n/a | def __init__(self, dirname, factory=None, create=True): |
---|
270 | n/a | """Initialize a Maildir instance.""" |
---|
271 | n/a | Mailbox.__init__(self, dirname, factory, create) |
---|
272 | n/a | self._paths = { |
---|
273 | n/a | 'tmp': os.path.join(self._path, 'tmp'), |
---|
274 | n/a | 'new': os.path.join(self._path, 'new'), |
---|
275 | n/a | 'cur': os.path.join(self._path, 'cur'), |
---|
276 | n/a | } |
---|
277 | n/a | if not os.path.exists(self._path): |
---|
278 | n/a | if create: |
---|
279 | n/a | os.mkdir(self._path, 0o700) |
---|
280 | n/a | for path in self._paths.values(): |
---|
281 | n/a | os.mkdir(path, 0o700) |
---|
282 | n/a | else: |
---|
283 | n/a | raise NoSuchMailboxError(self._path) |
---|
284 | n/a | self._toc = {} |
---|
285 | n/a | self._toc_mtimes = {'cur': 0, 'new': 0} |
---|
286 | n/a | self._last_read = 0 # Records last time we read cur/new |
---|
287 | n/a | self._skewfactor = 0.1 # Adjust if os/fs clocks are skewing |
---|
288 | n/a | |
---|
289 | n/a | def add(self, message): |
---|
290 | n/a | """Add message and return assigned key.""" |
---|
291 | n/a | tmp_file = self._create_tmp() |
---|
292 | n/a | try: |
---|
293 | n/a | self._dump_message(message, tmp_file) |
---|
294 | n/a | except BaseException: |
---|
295 | n/a | tmp_file.close() |
---|
296 | n/a | os.remove(tmp_file.name) |
---|
297 | n/a | raise |
---|
298 | n/a | _sync_close(tmp_file) |
---|
299 | n/a | if isinstance(message, MaildirMessage): |
---|
300 | n/a | subdir = message.get_subdir() |
---|
301 | n/a | suffix = self.colon + message.get_info() |
---|
302 | n/a | if suffix == self.colon: |
---|
303 | n/a | suffix = '' |
---|
304 | n/a | else: |
---|
305 | n/a | subdir = 'new' |
---|
306 | n/a | suffix = '' |
---|
307 | n/a | uniq = os.path.basename(tmp_file.name).split(self.colon)[0] |
---|
308 | n/a | dest = os.path.join(self._path, subdir, uniq + suffix) |
---|
309 | n/a | if isinstance(message, MaildirMessage): |
---|
310 | n/a | os.utime(tmp_file.name, |
---|
311 | n/a | (os.path.getatime(tmp_file.name), message.get_date())) |
---|
312 | n/a | # No file modification should be done after the file is moved to its |
---|
313 | n/a | # final position in order to prevent race conditions with changes |
---|
314 | n/a | # from other programs |
---|
315 | n/a | try: |
---|
316 | n/a | try: |
---|
317 | n/a | os.link(tmp_file.name, dest) |
---|
318 | n/a | except (AttributeError, PermissionError): |
---|
319 | n/a | os.rename(tmp_file.name, dest) |
---|
320 | n/a | else: |
---|
321 | n/a | os.remove(tmp_file.name) |
---|
322 | n/a | except OSError as e: |
---|
323 | n/a | os.remove(tmp_file.name) |
---|
324 | n/a | if e.errno == errno.EEXIST: |
---|
325 | n/a | raise ExternalClashError('Name clash with existing message: %s' |
---|
326 | n/a | % dest) |
---|
327 | n/a | else: |
---|
328 | n/a | raise |
---|
329 | n/a | return uniq |
---|
330 | n/a | |
---|
331 | n/a | def remove(self, key): |
---|
332 | n/a | """Remove the keyed message; raise KeyError if it doesn't exist.""" |
---|
333 | n/a | os.remove(os.path.join(self._path, self._lookup(key))) |
---|
334 | n/a | |
---|
335 | n/a | def discard(self, key): |
---|
336 | n/a | """If the keyed message exists, remove it.""" |
---|
337 | n/a | # This overrides an inapplicable implementation in the superclass. |
---|
338 | n/a | try: |
---|
339 | n/a | self.remove(key) |
---|
340 | n/a | except (KeyError, FileNotFoundError): |
---|
341 | n/a | pass |
---|
342 | n/a | |
---|
343 | n/a | def __setitem__(self, key, message): |
---|
344 | n/a | """Replace the keyed message; raise KeyError if it doesn't exist.""" |
---|
345 | n/a | old_subpath = self._lookup(key) |
---|
346 | n/a | temp_key = self.add(message) |
---|
347 | n/a | temp_subpath = self._lookup(temp_key) |
---|
348 | n/a | if isinstance(message, MaildirMessage): |
---|
349 | n/a | # temp's subdir and suffix were specified by message. |
---|
350 | n/a | dominant_subpath = temp_subpath |
---|
351 | n/a | else: |
---|
352 | n/a | # temp's subdir and suffix were defaults from add(). |
---|
353 | n/a | dominant_subpath = old_subpath |
---|
354 | n/a | subdir = os.path.dirname(dominant_subpath) |
---|
355 | n/a | if self.colon in dominant_subpath: |
---|
356 | n/a | suffix = self.colon + dominant_subpath.split(self.colon)[-1] |
---|
357 | n/a | else: |
---|
358 | n/a | suffix = '' |
---|
359 | n/a | self.discard(key) |
---|
360 | n/a | tmp_path = os.path.join(self._path, temp_subpath) |
---|
361 | n/a | new_path = os.path.join(self._path, subdir, key + suffix) |
---|
362 | n/a | if isinstance(message, MaildirMessage): |
---|
363 | n/a | os.utime(tmp_path, |
---|
364 | n/a | (os.path.getatime(tmp_path), message.get_date())) |
---|
365 | n/a | # No file modification should be done after the file is moved to its |
---|
366 | n/a | # final position in order to prevent race conditions with changes |
---|
367 | n/a | # from other programs |
---|
368 | n/a | os.rename(tmp_path, new_path) |
---|
369 | n/a | |
---|
370 | n/a | def get_message(self, key): |
---|
371 | n/a | """Return a Message representation or raise a KeyError.""" |
---|
372 | n/a | subpath = self._lookup(key) |
---|
373 | n/a | with open(os.path.join(self._path, subpath), 'rb') as f: |
---|
374 | n/a | if self._factory: |
---|
375 | n/a | msg = self._factory(f) |
---|
376 | n/a | else: |
---|
377 | n/a | msg = MaildirMessage(f) |
---|
378 | n/a | subdir, name = os.path.split(subpath) |
---|
379 | n/a | msg.set_subdir(subdir) |
---|
380 | n/a | if self.colon in name: |
---|
381 | n/a | msg.set_info(name.split(self.colon)[-1]) |
---|
382 | n/a | msg.set_date(os.path.getmtime(os.path.join(self._path, subpath))) |
---|
383 | n/a | return msg |
---|
384 | n/a | |
---|
385 | n/a | def get_bytes(self, key): |
---|
386 | n/a | """Return a bytes representation or raise a KeyError.""" |
---|
387 | n/a | with open(os.path.join(self._path, self._lookup(key)), 'rb') as f: |
---|
388 | n/a | return f.read().replace(linesep, b'\n') |
---|
389 | n/a | |
---|
390 | n/a | def get_file(self, key): |
---|
391 | n/a | """Return a file-like representation or raise a KeyError.""" |
---|
392 | n/a | f = open(os.path.join(self._path, self._lookup(key)), 'rb') |
---|
393 | n/a | return _ProxyFile(f) |
---|
394 | n/a | |
---|
395 | n/a | def iterkeys(self): |
---|
396 | n/a | """Return an iterator over keys.""" |
---|
397 | n/a | self._refresh() |
---|
398 | n/a | for key in self._toc: |
---|
399 | n/a | try: |
---|
400 | n/a | self._lookup(key) |
---|
401 | n/a | except KeyError: |
---|
402 | n/a | continue |
---|
403 | n/a | yield key |
---|
404 | n/a | |
---|
405 | n/a | def __contains__(self, key): |
---|
406 | n/a | """Return True if the keyed message exists, False otherwise.""" |
---|
407 | n/a | self._refresh() |
---|
408 | n/a | return key in self._toc |
---|
409 | n/a | |
---|
410 | n/a | def __len__(self): |
---|
411 | n/a | """Return a count of messages in the mailbox.""" |
---|
412 | n/a | self._refresh() |
---|
413 | n/a | return len(self._toc) |
---|
414 | n/a | |
---|
415 | n/a | def flush(self): |
---|
416 | n/a | """Write any pending changes to disk.""" |
---|
417 | n/a | # Maildir changes are always written immediately, so there's nothing |
---|
418 | n/a | # to do. |
---|
419 | n/a | pass |
---|
420 | n/a | |
---|
421 | n/a | def lock(self): |
---|
422 | n/a | """Lock the mailbox.""" |
---|
423 | n/a | return |
---|
424 | n/a | |
---|
425 | n/a | def unlock(self): |
---|
426 | n/a | """Unlock the mailbox if it is locked.""" |
---|
427 | n/a | return |
---|
428 | n/a | |
---|
429 | n/a | def close(self): |
---|
430 | n/a | """Flush and close the mailbox.""" |
---|
431 | n/a | return |
---|
432 | n/a | |
---|
433 | n/a | def list_folders(self): |
---|
434 | n/a | """Return a list of folder names.""" |
---|
435 | n/a | result = [] |
---|
436 | n/a | for entry in os.listdir(self._path): |
---|
437 | n/a | if len(entry) > 1 and entry[0] == '.' and \ |
---|
438 | n/a | os.path.isdir(os.path.join(self._path, entry)): |
---|
439 | n/a | result.append(entry[1:]) |
---|
440 | n/a | return result |
---|
441 | n/a | |
---|
442 | n/a | def get_folder(self, folder): |
---|
443 | n/a | """Return a Maildir instance for the named folder.""" |
---|
444 | n/a | return Maildir(os.path.join(self._path, '.' + folder), |
---|
445 | n/a | factory=self._factory, |
---|
446 | n/a | create=False) |
---|
447 | n/a | |
---|
448 | n/a | def add_folder(self, folder): |
---|
449 | n/a | """Create a folder and return a Maildir instance representing it.""" |
---|
450 | n/a | path = os.path.join(self._path, '.' + folder) |
---|
451 | n/a | result = Maildir(path, factory=self._factory) |
---|
452 | n/a | maildirfolder_path = os.path.join(path, 'maildirfolder') |
---|
453 | n/a | if not os.path.exists(maildirfolder_path): |
---|
454 | n/a | os.close(os.open(maildirfolder_path, os.O_CREAT | os.O_WRONLY, |
---|
455 | n/a | 0o666)) |
---|
456 | n/a | return result |
---|
457 | n/a | |
---|
458 | n/a | def remove_folder(self, folder): |
---|
459 | n/a | """Delete the named folder, which must be empty.""" |
---|
460 | n/a | path = os.path.join(self._path, '.' + folder) |
---|
461 | n/a | for entry in os.listdir(os.path.join(path, 'new')) + \ |
---|
462 | n/a | os.listdir(os.path.join(path, 'cur')): |
---|
463 | n/a | if len(entry) < 1 or entry[0] != '.': |
---|
464 | n/a | raise NotEmptyError('Folder contains message(s): %s' % folder) |
---|
465 | n/a | for entry in os.listdir(path): |
---|
466 | n/a | if entry != 'new' and entry != 'cur' and entry != 'tmp' and \ |
---|
467 | n/a | os.path.isdir(os.path.join(path, entry)): |
---|
468 | n/a | raise NotEmptyError("Folder contains subdirectory '%s': %s" % |
---|
469 | n/a | (folder, entry)) |
---|
470 | n/a | for root, dirs, files in os.walk(path, topdown=False): |
---|
471 | n/a | for entry in files: |
---|
472 | n/a | os.remove(os.path.join(root, entry)) |
---|
473 | n/a | for entry in dirs: |
---|
474 | n/a | os.rmdir(os.path.join(root, entry)) |
---|
475 | n/a | os.rmdir(path) |
---|
476 | n/a | |
---|
477 | n/a | def clean(self): |
---|
478 | n/a | """Delete old files in "tmp".""" |
---|
479 | n/a | now = time.time() |
---|
480 | n/a | for entry in os.listdir(os.path.join(self._path, 'tmp')): |
---|
481 | n/a | path = os.path.join(self._path, 'tmp', entry) |
---|
482 | n/a | if now - os.path.getatime(path) > 129600: # 60 * 60 * 36 |
---|
483 | n/a | os.remove(path) |
---|
484 | n/a | |
---|
485 | n/a | _count = 1 # This is used to generate unique file names. |
---|
486 | n/a | |
---|
487 | n/a | def _create_tmp(self): |
---|
488 | n/a | """Create a file in the tmp subdirectory and open and return it.""" |
---|
489 | n/a | now = time.time() |
---|
490 | n/a | hostname = socket.gethostname() |
---|
491 | n/a | if '/' in hostname: |
---|
492 | n/a | hostname = hostname.replace('/', r'\057') |
---|
493 | n/a | if ':' in hostname: |
---|
494 | n/a | hostname = hostname.replace(':', r'\072') |
---|
495 | n/a | uniq = "%s.M%sP%sQ%s.%s" % (int(now), int(now % 1 * 1e6), os.getpid(), |
---|
496 | n/a | Maildir._count, hostname) |
---|
497 | n/a | path = os.path.join(self._path, 'tmp', uniq) |
---|
498 | n/a | try: |
---|
499 | n/a | os.stat(path) |
---|
500 | n/a | except FileNotFoundError: |
---|
501 | n/a | Maildir._count += 1 |
---|
502 | n/a | try: |
---|
503 | n/a | return _create_carefully(path) |
---|
504 | n/a | except FileExistsError: |
---|
505 | n/a | pass |
---|
506 | n/a | |
---|
507 | n/a | # Fall through to here if stat succeeded or open raised EEXIST. |
---|
508 | n/a | raise ExternalClashError('Name clash prevented file creation: %s' % |
---|
509 | n/a | path) |
---|
510 | n/a | |
---|
511 | n/a | def _refresh(self): |
---|
512 | n/a | """Update table of contents mapping.""" |
---|
513 | n/a | # If it has been less than two seconds since the last _refresh() call, |
---|
514 | n/a | # we have to unconditionally re-read the mailbox just in case it has |
---|
515 | n/a | # been modified, because os.path.mtime() has a 2 sec resolution in the |
---|
516 | n/a | # most common worst case (FAT) and a 1 sec resolution typically. This |
---|
517 | n/a | # results in a few unnecessary re-reads when _refresh() is called |
---|
518 | n/a | # multiple times in that interval, but once the clock ticks over, we |
---|
519 | n/a | # will only re-read as needed. Because the filesystem might be being |
---|
520 | n/a | # served by an independent system with its own clock, we record and |
---|
521 | n/a | # compare with the mtimes from the filesystem. Because the other |
---|
522 | n/a | # system's clock might be skewing relative to our clock, we add an |
---|
523 | n/a | # extra delta to our wait. The default is one tenth second, but is an |
---|
524 | n/a | # instance variable and so can be adjusted if dealing with a |
---|
525 | n/a | # particularly skewed or irregular system. |
---|
526 | n/a | if time.time() - self._last_read > 2 + self._skewfactor: |
---|
527 | n/a | refresh = False |
---|
528 | n/a | for subdir in self._toc_mtimes: |
---|
529 | n/a | mtime = os.path.getmtime(self._paths[subdir]) |
---|
530 | n/a | if mtime > self._toc_mtimes[subdir]: |
---|
531 | n/a | refresh = True |
---|
532 | n/a | self._toc_mtimes[subdir] = mtime |
---|
533 | n/a | if not refresh: |
---|
534 | n/a | return |
---|
535 | n/a | # Refresh toc |
---|
536 | n/a | self._toc = {} |
---|
537 | n/a | for subdir in self._toc_mtimes: |
---|
538 | n/a | path = self._paths[subdir] |
---|
539 | n/a | for entry in os.listdir(path): |
---|
540 | n/a | p = os.path.join(path, entry) |
---|
541 | n/a | if os.path.isdir(p): |
---|
542 | n/a | continue |
---|
543 | n/a | uniq = entry.split(self.colon)[0] |
---|
544 | n/a | self._toc[uniq] = os.path.join(subdir, entry) |
---|
545 | n/a | self._last_read = time.time() |
---|
546 | n/a | |
---|
547 | n/a | def _lookup(self, key): |
---|
548 | n/a | """Use TOC to return subpath for given key, or raise a KeyError.""" |
---|
549 | n/a | try: |
---|
550 | n/a | if os.path.exists(os.path.join(self._path, self._toc[key])): |
---|
551 | n/a | return self._toc[key] |
---|
552 | n/a | except KeyError: |
---|
553 | n/a | pass |
---|
554 | n/a | self._refresh() |
---|
555 | n/a | try: |
---|
556 | n/a | return self._toc[key] |
---|
557 | n/a | except KeyError: |
---|
558 | n/a | raise KeyError('No message with key: %s' % key) |
---|
559 | n/a | |
---|
560 | n/a | # This method is for backward compatibility only. |
---|
561 | n/a | def next(self): |
---|
562 | n/a | """Return the next message in a one-time iteration.""" |
---|
563 | n/a | if not hasattr(self, '_onetime_keys'): |
---|
564 | n/a | self._onetime_keys = self.iterkeys() |
---|
565 | n/a | while True: |
---|
566 | n/a | try: |
---|
567 | n/a | return self[next(self._onetime_keys)] |
---|
568 | n/a | except StopIteration: |
---|
569 | n/a | return None |
---|
570 | n/a | except KeyError: |
---|
571 | n/a | continue |
---|
572 | n/a | |
---|
573 | n/a | |
---|
574 | n/a | class _singlefileMailbox(Mailbox): |
---|
575 | n/a | """A single-file mailbox.""" |
---|
576 | n/a | |
---|
577 | n/a | def __init__(self, path, factory=None, create=True): |
---|
578 | n/a | """Initialize a single-file mailbox.""" |
---|
579 | n/a | Mailbox.__init__(self, path, factory, create) |
---|
580 | n/a | try: |
---|
581 | n/a | f = open(self._path, 'rb+') |
---|
582 | n/a | except OSError as e: |
---|
583 | n/a | if e.errno == errno.ENOENT: |
---|
584 | n/a | if create: |
---|
585 | n/a | f = open(self._path, 'wb+') |
---|
586 | n/a | else: |
---|
587 | n/a | raise NoSuchMailboxError(self._path) |
---|
588 | n/a | elif e.errno in (errno.EACCES, errno.EROFS): |
---|
589 | n/a | f = open(self._path, 'rb') |
---|
590 | n/a | else: |
---|
591 | n/a | raise |
---|
592 | n/a | self._file = f |
---|
593 | n/a | self._toc = None |
---|
594 | n/a | self._next_key = 0 |
---|
595 | n/a | self._pending = False # No changes require rewriting the file. |
---|
596 | n/a | self._pending_sync = False # No need to sync the file |
---|
597 | n/a | self._locked = False |
---|
598 | n/a | self._file_length = None # Used to record mailbox size |
---|
599 | n/a | |
---|
600 | n/a | def add(self, message): |
---|
601 | n/a | """Add message and return assigned key.""" |
---|
602 | n/a | self._lookup() |
---|
603 | n/a | self._toc[self._next_key] = self._append_message(message) |
---|
604 | n/a | self._next_key += 1 |
---|
605 | n/a | # _append_message appends the message to the mailbox file. We |
---|
606 | n/a | # don't need a full rewrite + rename, sync is enough. |
---|
607 | n/a | self._pending_sync = True |
---|
608 | n/a | return self._next_key - 1 |
---|
609 | n/a | |
---|
610 | n/a | def remove(self, key): |
---|
611 | n/a | """Remove the keyed message; raise KeyError if it doesn't exist.""" |
---|
612 | n/a | self._lookup(key) |
---|
613 | n/a | del self._toc[key] |
---|
614 | n/a | self._pending = True |
---|
615 | n/a | |
---|
616 | n/a | def __setitem__(self, key, message): |
---|
617 | n/a | """Replace the keyed message; raise KeyError if it doesn't exist.""" |
---|
618 | n/a | self._lookup(key) |
---|
619 | n/a | self._toc[key] = self._append_message(message) |
---|
620 | n/a | self._pending = True |
---|
621 | n/a | |
---|
622 | n/a | def iterkeys(self): |
---|
623 | n/a | """Return an iterator over keys.""" |
---|
624 | n/a | self._lookup() |
---|
625 | n/a | yield from self._toc.keys() |
---|
626 | n/a | |
---|
627 | n/a | def __contains__(self, key): |
---|
628 | n/a | """Return True if the keyed message exists, False otherwise.""" |
---|
629 | n/a | self._lookup() |
---|
630 | n/a | return key in self._toc |
---|
631 | n/a | |
---|
632 | n/a | def __len__(self): |
---|
633 | n/a | """Return a count of messages in the mailbox.""" |
---|
634 | n/a | self._lookup() |
---|
635 | n/a | return len(self._toc) |
---|
636 | n/a | |
---|
637 | n/a | def lock(self): |
---|
638 | n/a | """Lock the mailbox.""" |
---|
639 | n/a | if not self._locked: |
---|
640 | n/a | _lock_file(self._file) |
---|
641 | n/a | self._locked = True |
---|
642 | n/a | |
---|
643 | n/a | def unlock(self): |
---|
644 | n/a | """Unlock the mailbox if it is locked.""" |
---|
645 | n/a | if self._locked: |
---|
646 | n/a | _unlock_file(self._file) |
---|
647 | n/a | self._locked = False |
---|
648 | n/a | |
---|
649 | n/a | def flush(self): |
---|
650 | n/a | """Write any pending changes to disk.""" |
---|
651 | n/a | if not self._pending: |
---|
652 | n/a | if self._pending_sync: |
---|
653 | n/a | # Messages have only been added, so syncing the file |
---|
654 | n/a | # is enough. |
---|
655 | n/a | _sync_flush(self._file) |
---|
656 | n/a | self._pending_sync = False |
---|
657 | n/a | return |
---|
658 | n/a | |
---|
659 | n/a | # In order to be writing anything out at all, self._toc must |
---|
660 | n/a | # already have been generated (and presumably has been modified |
---|
661 | n/a | # by adding or deleting an item). |
---|
662 | n/a | assert self._toc is not None |
---|
663 | n/a | |
---|
664 | n/a | # Check length of self._file; if it's changed, some other process |
---|
665 | n/a | # has modified the mailbox since we scanned it. |
---|
666 | n/a | self._file.seek(0, 2) |
---|
667 | n/a | cur_len = self._file.tell() |
---|
668 | n/a | if cur_len != self._file_length: |
---|
669 | n/a | raise ExternalClashError('Size of mailbox file changed ' |
---|
670 | n/a | '(expected %i, found %i)' % |
---|
671 | n/a | (self._file_length, cur_len)) |
---|
672 | n/a | |
---|
673 | n/a | new_file = _create_temporary(self._path) |
---|
674 | n/a | try: |
---|
675 | n/a | new_toc = {} |
---|
676 | n/a | self._pre_mailbox_hook(new_file) |
---|
677 | n/a | for key in sorted(self._toc.keys()): |
---|
678 | n/a | start, stop = self._toc[key] |
---|
679 | n/a | self._file.seek(start) |
---|
680 | n/a | self._pre_message_hook(new_file) |
---|
681 | n/a | new_start = new_file.tell() |
---|
682 | n/a | while True: |
---|
683 | n/a | buffer = self._file.read(min(4096, |
---|
684 | n/a | stop - self._file.tell())) |
---|
685 | n/a | if not buffer: |
---|
686 | n/a | break |
---|
687 | n/a | new_file.write(buffer) |
---|
688 | n/a | new_toc[key] = (new_start, new_file.tell()) |
---|
689 | n/a | self._post_message_hook(new_file) |
---|
690 | n/a | self._file_length = new_file.tell() |
---|
691 | n/a | except: |
---|
692 | n/a | new_file.close() |
---|
693 | n/a | os.remove(new_file.name) |
---|
694 | n/a | raise |
---|
695 | n/a | _sync_close(new_file) |
---|
696 | n/a | # self._file is about to get replaced, so no need to sync. |
---|
697 | n/a | self._file.close() |
---|
698 | n/a | # Make sure the new file's mode is the same as the old file's |
---|
699 | n/a | mode = os.stat(self._path).st_mode |
---|
700 | n/a | os.chmod(new_file.name, mode) |
---|
701 | n/a | try: |
---|
702 | n/a | os.rename(new_file.name, self._path) |
---|
703 | n/a | except FileExistsError: |
---|
704 | n/a | os.remove(self._path) |
---|
705 | n/a | os.rename(new_file.name, self._path) |
---|
706 | n/a | self._file = open(self._path, 'rb+') |
---|
707 | n/a | self._toc = new_toc |
---|
708 | n/a | self._pending = False |
---|
709 | n/a | self._pending_sync = False |
---|
710 | n/a | if self._locked: |
---|
711 | n/a | _lock_file(self._file, dotlock=False) |
---|
712 | n/a | |
---|
713 | n/a | def _pre_mailbox_hook(self, f): |
---|
714 | n/a | """Called before writing the mailbox to file f.""" |
---|
715 | n/a | return |
---|
716 | n/a | |
---|
717 | n/a | def _pre_message_hook(self, f): |
---|
718 | n/a | """Called before writing each message to file f.""" |
---|
719 | n/a | return |
---|
720 | n/a | |
---|
721 | n/a | def _post_message_hook(self, f): |
---|
722 | n/a | """Called after writing each message to file f.""" |
---|
723 | n/a | return |
---|
724 | n/a | |
---|
725 | n/a | def close(self): |
---|
726 | n/a | """Flush and close the mailbox.""" |
---|
727 | n/a | try: |
---|
728 | n/a | self.flush() |
---|
729 | n/a | finally: |
---|
730 | n/a | try: |
---|
731 | n/a | if self._locked: |
---|
732 | n/a | self.unlock() |
---|
733 | n/a | finally: |
---|
734 | n/a | self._file.close() # Sync has been done by self.flush() above. |
---|
735 | n/a | |
---|
736 | n/a | def _lookup(self, key=None): |
---|
737 | n/a | """Return (start, stop) or raise KeyError.""" |
---|
738 | n/a | if self._toc is None: |
---|
739 | n/a | self._generate_toc() |
---|
740 | n/a | if key is not None: |
---|
741 | n/a | try: |
---|
742 | n/a | return self._toc[key] |
---|
743 | n/a | except KeyError: |
---|
744 | n/a | raise KeyError('No message with key: %s' % key) |
---|
745 | n/a | |
---|
746 | n/a | def _append_message(self, message): |
---|
747 | n/a | """Append message to mailbox and return (start, stop) offsets.""" |
---|
748 | n/a | self._file.seek(0, 2) |
---|
749 | n/a | before = self._file.tell() |
---|
750 | n/a | if len(self._toc) == 0 and not self._pending: |
---|
751 | n/a | # This is the first message, and the _pre_mailbox_hook |
---|
752 | n/a | # hasn't yet been called. If self._pending is True, |
---|
753 | n/a | # messages have been removed, so _pre_mailbox_hook must |
---|
754 | n/a | # have been called already. |
---|
755 | n/a | self._pre_mailbox_hook(self._file) |
---|
756 | n/a | try: |
---|
757 | n/a | self._pre_message_hook(self._file) |
---|
758 | n/a | offsets = self._install_message(message) |
---|
759 | n/a | self._post_message_hook(self._file) |
---|
760 | n/a | except BaseException: |
---|
761 | n/a | self._file.truncate(before) |
---|
762 | n/a | raise |
---|
763 | n/a | self._file.flush() |
---|
764 | n/a | self._file_length = self._file.tell() # Record current length of mailbox |
---|
765 | n/a | return offsets |
---|
766 | n/a | |
---|
767 | n/a | |
---|
768 | n/a | |
---|
769 | n/a | class _mboxMMDF(_singlefileMailbox): |
---|
770 | n/a | """An mbox or MMDF mailbox.""" |
---|
771 | n/a | |
---|
772 | n/a | _mangle_from_ = True |
---|
773 | n/a | |
---|
774 | n/a | def get_message(self, key): |
---|
775 | n/a | """Return a Message representation or raise a KeyError.""" |
---|
776 | n/a | start, stop = self._lookup(key) |
---|
777 | n/a | self._file.seek(start) |
---|
778 | n/a | from_line = self._file.readline().replace(linesep, b'') |
---|
779 | n/a | string = self._file.read(stop - self._file.tell()) |
---|
780 | n/a | msg = self._message_factory(string.replace(linesep, b'\n')) |
---|
781 | n/a | msg.set_from(from_line[5:].decode('ascii')) |
---|
782 | n/a | return msg |
---|
783 | n/a | |
---|
784 | n/a | def get_string(self, key, from_=False): |
---|
785 | n/a | """Return a string representation or raise a KeyError.""" |
---|
786 | n/a | return email.message_from_bytes( |
---|
787 | n/a | self.get_bytes(key)).as_string(unixfrom=from_) |
---|
788 | n/a | |
---|
789 | n/a | def get_bytes(self, key, from_=False): |
---|
790 | n/a | """Return a string representation or raise a KeyError.""" |
---|
791 | n/a | start, stop = self._lookup(key) |
---|
792 | n/a | self._file.seek(start) |
---|
793 | n/a | if not from_: |
---|
794 | n/a | self._file.readline() |
---|
795 | n/a | string = self._file.read(stop - self._file.tell()) |
---|
796 | n/a | return string.replace(linesep, b'\n') |
---|
797 | n/a | |
---|
798 | n/a | def get_file(self, key, from_=False): |
---|
799 | n/a | """Return a file-like representation or raise a KeyError.""" |
---|
800 | n/a | start, stop = self._lookup(key) |
---|
801 | n/a | self._file.seek(start) |
---|
802 | n/a | if not from_: |
---|
803 | n/a | self._file.readline() |
---|
804 | n/a | return _PartialFile(self._file, self._file.tell(), stop) |
---|
805 | n/a | |
---|
806 | n/a | def _install_message(self, message): |
---|
807 | n/a | """Format a message and blindly write to self._file.""" |
---|
808 | n/a | from_line = None |
---|
809 | n/a | if isinstance(message, str): |
---|
810 | n/a | message = self._string_to_bytes(message) |
---|
811 | n/a | if isinstance(message, bytes) and message.startswith(b'From '): |
---|
812 | n/a | newline = message.find(b'\n') |
---|
813 | n/a | if newline != -1: |
---|
814 | n/a | from_line = message[:newline] |
---|
815 | n/a | message = message[newline + 1:] |
---|
816 | n/a | else: |
---|
817 | n/a | from_line = message |
---|
818 | n/a | message = b'' |
---|
819 | n/a | elif isinstance(message, _mboxMMDFMessage): |
---|
820 | n/a | author = message.get_from().encode('ascii') |
---|
821 | n/a | from_line = b'From ' + author |
---|
822 | n/a | elif isinstance(message, email.message.Message): |
---|
823 | n/a | from_line = message.get_unixfrom() # May be None. |
---|
824 | n/a | if from_line is not None: |
---|
825 | n/a | from_line = from_line.encode('ascii') |
---|
826 | n/a | if from_line is None: |
---|
827 | n/a | from_line = b'From MAILER-DAEMON ' + time.asctime(time.gmtime()).encode() |
---|
828 | n/a | start = self._file.tell() |
---|
829 | n/a | self._file.write(from_line + linesep) |
---|
830 | n/a | self._dump_message(message, self._file, self._mangle_from_) |
---|
831 | n/a | stop = self._file.tell() |
---|
832 | n/a | return (start, stop) |
---|
833 | n/a | |
---|
834 | n/a | |
---|
835 | n/a | class mbox(_mboxMMDF): |
---|
836 | n/a | """A classic mbox mailbox.""" |
---|
837 | n/a | |
---|
838 | n/a | _mangle_from_ = True |
---|
839 | n/a | |
---|
840 | n/a | # All messages must end in a newline character, and |
---|
841 | n/a | # _post_message_hooks outputs an empty line between messages. |
---|
842 | n/a | _append_newline = True |
---|
843 | n/a | |
---|
844 | n/a | def __init__(self, path, factory=None, create=True): |
---|
845 | n/a | """Initialize an mbox mailbox.""" |
---|
846 | n/a | self._message_factory = mboxMessage |
---|
847 | n/a | _mboxMMDF.__init__(self, path, factory, create) |
---|
848 | n/a | |
---|
849 | n/a | def _post_message_hook(self, f): |
---|
850 | n/a | """Called after writing each message to file f.""" |
---|
851 | n/a | f.write(linesep) |
---|
852 | n/a | |
---|
853 | n/a | def _generate_toc(self): |
---|
854 | n/a | """Generate key-to-(start, stop) table of contents.""" |
---|
855 | n/a | starts, stops = [], [] |
---|
856 | n/a | last_was_empty = False |
---|
857 | n/a | self._file.seek(0) |
---|
858 | n/a | while True: |
---|
859 | n/a | line_pos = self._file.tell() |
---|
860 | n/a | line = self._file.readline() |
---|
861 | n/a | if line.startswith(b'From '): |
---|
862 | n/a | if len(stops) < len(starts): |
---|
863 | n/a | if last_was_empty: |
---|
864 | n/a | stops.append(line_pos - len(linesep)) |
---|
865 | n/a | else: |
---|
866 | n/a | # The last line before the "From " line wasn't |
---|
867 | n/a | # blank, but we consider it a start of a |
---|
868 | n/a | # message anyway. |
---|
869 | n/a | stops.append(line_pos) |
---|
870 | n/a | starts.append(line_pos) |
---|
871 | n/a | last_was_empty = False |
---|
872 | n/a | elif not line: |
---|
873 | n/a | if last_was_empty: |
---|
874 | n/a | stops.append(line_pos - len(linesep)) |
---|
875 | n/a | else: |
---|
876 | n/a | stops.append(line_pos) |
---|
877 | n/a | break |
---|
878 | n/a | elif line == linesep: |
---|
879 | n/a | last_was_empty = True |
---|
880 | n/a | else: |
---|
881 | n/a | last_was_empty = False |
---|
882 | n/a | self._toc = dict(enumerate(zip(starts, stops))) |
---|
883 | n/a | self._next_key = len(self._toc) |
---|
884 | n/a | self._file_length = self._file.tell() |
---|
885 | n/a | |
---|
886 | n/a | |
---|
887 | n/a | class MMDF(_mboxMMDF): |
---|
888 | n/a | """An MMDF mailbox.""" |
---|
889 | n/a | |
---|
890 | n/a | def __init__(self, path, factory=None, create=True): |
---|
891 | n/a | """Initialize an MMDF mailbox.""" |
---|
892 | n/a | self._message_factory = MMDFMessage |
---|
893 | n/a | _mboxMMDF.__init__(self, path, factory, create) |
---|
894 | n/a | |
---|
895 | n/a | def _pre_message_hook(self, f): |
---|
896 | n/a | """Called before writing each message to file f.""" |
---|
897 | n/a | f.write(b'\001\001\001\001' + linesep) |
---|
898 | n/a | |
---|
899 | n/a | def _post_message_hook(self, f): |
---|
900 | n/a | """Called after writing each message to file f.""" |
---|
901 | n/a | f.write(linesep + b'\001\001\001\001' + linesep) |
---|
902 | n/a | |
---|
903 | n/a | def _generate_toc(self): |
---|
904 | n/a | """Generate key-to-(start, stop) table of contents.""" |
---|
905 | n/a | starts, stops = [], [] |
---|
906 | n/a | self._file.seek(0) |
---|
907 | n/a | next_pos = 0 |
---|
908 | n/a | while True: |
---|
909 | n/a | line_pos = next_pos |
---|
910 | n/a | line = self._file.readline() |
---|
911 | n/a | next_pos = self._file.tell() |
---|
912 | n/a | if line.startswith(b'\001\001\001\001' + linesep): |
---|
913 | n/a | starts.append(next_pos) |
---|
914 | n/a | while True: |
---|
915 | n/a | line_pos = next_pos |
---|
916 | n/a | line = self._file.readline() |
---|
917 | n/a | next_pos = self._file.tell() |
---|
918 | n/a | if line == b'\001\001\001\001' + linesep: |
---|
919 | n/a | stops.append(line_pos - len(linesep)) |
---|
920 | n/a | break |
---|
921 | n/a | elif not line: |
---|
922 | n/a | stops.append(line_pos) |
---|
923 | n/a | break |
---|
924 | n/a | elif not line: |
---|
925 | n/a | break |
---|
926 | n/a | self._toc = dict(enumerate(zip(starts, stops))) |
---|
927 | n/a | self._next_key = len(self._toc) |
---|
928 | n/a | self._file.seek(0, 2) |
---|
929 | n/a | self._file_length = self._file.tell() |
---|
930 | n/a | |
---|
931 | n/a | |
---|
932 | n/a | class MH(Mailbox): |
---|
933 | n/a | """An MH mailbox.""" |
---|
934 | n/a | |
---|
935 | n/a | def __init__(self, path, factory=None, create=True): |
---|
936 | n/a | """Initialize an MH instance.""" |
---|
937 | n/a | Mailbox.__init__(self, path, factory, create) |
---|
938 | n/a | if not os.path.exists(self._path): |
---|
939 | n/a | if create: |
---|
940 | n/a | os.mkdir(self._path, 0o700) |
---|
941 | n/a | os.close(os.open(os.path.join(self._path, '.mh_sequences'), |
---|
942 | n/a | os.O_CREAT | os.O_EXCL | os.O_WRONLY, 0o600)) |
---|
943 | n/a | else: |
---|
944 | n/a | raise NoSuchMailboxError(self._path) |
---|
945 | n/a | self._locked = False |
---|
946 | n/a | |
---|
947 | n/a | def add(self, message): |
---|
948 | n/a | """Add message and return assigned key.""" |
---|
949 | n/a | keys = self.keys() |
---|
950 | n/a | if len(keys) == 0: |
---|
951 | n/a | new_key = 1 |
---|
952 | n/a | else: |
---|
953 | n/a | new_key = max(keys) + 1 |
---|
954 | n/a | new_path = os.path.join(self._path, str(new_key)) |
---|
955 | n/a | f = _create_carefully(new_path) |
---|
956 | n/a | closed = False |
---|
957 | n/a | try: |
---|
958 | n/a | if self._locked: |
---|
959 | n/a | _lock_file(f) |
---|
960 | n/a | try: |
---|
961 | n/a | try: |
---|
962 | n/a | self._dump_message(message, f) |
---|
963 | n/a | except BaseException: |
---|
964 | n/a | # Unlock and close so it can be deleted on Windows |
---|
965 | n/a | if self._locked: |
---|
966 | n/a | _unlock_file(f) |
---|
967 | n/a | _sync_close(f) |
---|
968 | n/a | closed = True |
---|
969 | n/a | os.remove(new_path) |
---|
970 | n/a | raise |
---|
971 | n/a | if isinstance(message, MHMessage): |
---|
972 | n/a | self._dump_sequences(message, new_key) |
---|
973 | n/a | finally: |
---|
974 | n/a | if self._locked: |
---|
975 | n/a | _unlock_file(f) |
---|
976 | n/a | finally: |
---|
977 | n/a | if not closed: |
---|
978 | n/a | _sync_close(f) |
---|
979 | n/a | return new_key |
---|
980 | n/a | |
---|
981 | n/a | def remove(self, key): |
---|
982 | n/a | """Remove the keyed message; raise KeyError if it doesn't exist.""" |
---|
983 | n/a | path = os.path.join(self._path, str(key)) |
---|
984 | n/a | try: |
---|
985 | n/a | f = open(path, 'rb+') |
---|
986 | n/a | except OSError as e: |
---|
987 | n/a | if e.errno == errno.ENOENT: |
---|
988 | n/a | raise KeyError('No message with key: %s' % key) |
---|
989 | n/a | else: |
---|
990 | n/a | raise |
---|
991 | n/a | else: |
---|
992 | n/a | f.close() |
---|
993 | n/a | os.remove(path) |
---|
994 | n/a | |
---|
995 | n/a | def __setitem__(self, key, message): |
---|
996 | n/a | """Replace the keyed message; raise KeyError if it doesn't exist.""" |
---|
997 | n/a | path = os.path.join(self._path, str(key)) |
---|
998 | n/a | try: |
---|
999 | n/a | f = open(path, 'rb+') |
---|
1000 | n/a | except OSError as e: |
---|
1001 | n/a | if e.errno == errno.ENOENT: |
---|
1002 | n/a | raise KeyError('No message with key: %s' % key) |
---|
1003 | n/a | else: |
---|
1004 | n/a | raise |
---|
1005 | n/a | try: |
---|
1006 | n/a | if self._locked: |
---|
1007 | n/a | _lock_file(f) |
---|
1008 | n/a | try: |
---|
1009 | n/a | os.close(os.open(path, os.O_WRONLY | os.O_TRUNC)) |
---|
1010 | n/a | self._dump_message(message, f) |
---|
1011 | n/a | if isinstance(message, MHMessage): |
---|
1012 | n/a | self._dump_sequences(message, key) |
---|
1013 | n/a | finally: |
---|
1014 | n/a | if self._locked: |
---|
1015 | n/a | _unlock_file(f) |
---|
1016 | n/a | finally: |
---|
1017 | n/a | _sync_close(f) |
---|
1018 | n/a | |
---|
1019 | n/a | def get_message(self, key): |
---|
1020 | n/a | """Return a Message representation or raise a KeyError.""" |
---|
1021 | n/a | try: |
---|
1022 | n/a | if self._locked: |
---|
1023 | n/a | f = open(os.path.join(self._path, str(key)), 'rb+') |
---|
1024 | n/a | else: |
---|
1025 | n/a | f = open(os.path.join(self._path, str(key)), 'rb') |
---|
1026 | n/a | except OSError as e: |
---|
1027 | n/a | if e.errno == errno.ENOENT: |
---|
1028 | n/a | raise KeyError('No message with key: %s' % key) |
---|
1029 | n/a | else: |
---|
1030 | n/a | raise |
---|
1031 | n/a | with f: |
---|
1032 | n/a | if self._locked: |
---|
1033 | n/a | _lock_file(f) |
---|
1034 | n/a | try: |
---|
1035 | n/a | msg = MHMessage(f) |
---|
1036 | n/a | finally: |
---|
1037 | n/a | if self._locked: |
---|
1038 | n/a | _unlock_file(f) |
---|
1039 | n/a | for name, key_list in self.get_sequences().items(): |
---|
1040 | n/a | if key in key_list: |
---|
1041 | n/a | msg.add_sequence(name) |
---|
1042 | n/a | return msg |
---|
1043 | n/a | |
---|
1044 | n/a | def get_bytes(self, key): |
---|
1045 | n/a | """Return a bytes representation or raise a KeyError.""" |
---|
1046 | n/a | try: |
---|
1047 | n/a | if self._locked: |
---|
1048 | n/a | f = open(os.path.join(self._path, str(key)), 'rb+') |
---|
1049 | n/a | else: |
---|
1050 | n/a | f = open(os.path.join(self._path, str(key)), 'rb') |
---|
1051 | n/a | except OSError as e: |
---|
1052 | n/a | if e.errno == errno.ENOENT: |
---|
1053 | n/a | raise KeyError('No message with key: %s' % key) |
---|
1054 | n/a | else: |
---|
1055 | n/a | raise |
---|
1056 | n/a | with f: |
---|
1057 | n/a | if self._locked: |
---|
1058 | n/a | _lock_file(f) |
---|
1059 | n/a | try: |
---|
1060 | n/a | return f.read().replace(linesep, b'\n') |
---|
1061 | n/a | finally: |
---|
1062 | n/a | if self._locked: |
---|
1063 | n/a | _unlock_file(f) |
---|
1064 | n/a | |
---|
1065 | n/a | def get_file(self, key): |
---|
1066 | n/a | """Return a file-like representation or raise a KeyError.""" |
---|
1067 | n/a | try: |
---|
1068 | n/a | f = open(os.path.join(self._path, str(key)), 'rb') |
---|
1069 | n/a | except OSError as e: |
---|
1070 | n/a | if e.errno == errno.ENOENT: |
---|
1071 | n/a | raise KeyError('No message with key: %s' % key) |
---|
1072 | n/a | else: |
---|
1073 | n/a | raise |
---|
1074 | n/a | return _ProxyFile(f) |
---|
1075 | n/a | |
---|
1076 | n/a | def iterkeys(self): |
---|
1077 | n/a | """Return an iterator over keys.""" |
---|
1078 | n/a | return iter(sorted(int(entry) for entry in os.listdir(self._path) |
---|
1079 | n/a | if entry.isdigit())) |
---|
1080 | n/a | |
---|
1081 | n/a | def __contains__(self, key): |
---|
1082 | n/a | """Return True if the keyed message exists, False otherwise.""" |
---|
1083 | n/a | return os.path.exists(os.path.join(self._path, str(key))) |
---|
1084 | n/a | |
---|
1085 | n/a | def __len__(self): |
---|
1086 | n/a | """Return a count of messages in the mailbox.""" |
---|
1087 | n/a | return len(list(self.iterkeys())) |
---|
1088 | n/a | |
---|
1089 | n/a | def lock(self): |
---|
1090 | n/a | """Lock the mailbox.""" |
---|
1091 | n/a | if not self._locked: |
---|
1092 | n/a | self._file = open(os.path.join(self._path, '.mh_sequences'), 'rb+') |
---|
1093 | n/a | _lock_file(self._file) |
---|
1094 | n/a | self._locked = True |
---|
1095 | n/a | |
---|
1096 | n/a | def unlock(self): |
---|
1097 | n/a | """Unlock the mailbox if it is locked.""" |
---|
1098 | n/a | if self._locked: |
---|
1099 | n/a | _unlock_file(self._file) |
---|
1100 | n/a | _sync_close(self._file) |
---|
1101 | n/a | del self._file |
---|
1102 | n/a | self._locked = False |
---|
1103 | n/a | |
---|
1104 | n/a | def flush(self): |
---|
1105 | n/a | """Write any pending changes to the disk.""" |
---|
1106 | n/a | return |
---|
1107 | n/a | |
---|
1108 | n/a | def close(self): |
---|
1109 | n/a | """Flush and close the mailbox.""" |
---|
1110 | n/a | if self._locked: |
---|
1111 | n/a | self.unlock() |
---|
1112 | n/a | |
---|
1113 | n/a | def list_folders(self): |
---|
1114 | n/a | """Return a list of folder names.""" |
---|
1115 | n/a | result = [] |
---|
1116 | n/a | for entry in os.listdir(self._path): |
---|
1117 | n/a | if os.path.isdir(os.path.join(self._path, entry)): |
---|
1118 | n/a | result.append(entry) |
---|
1119 | n/a | return result |
---|
1120 | n/a | |
---|
1121 | n/a | def get_folder(self, folder): |
---|
1122 | n/a | """Return an MH instance for the named folder.""" |
---|
1123 | n/a | return MH(os.path.join(self._path, folder), |
---|
1124 | n/a | factory=self._factory, create=False) |
---|
1125 | n/a | |
---|
1126 | n/a | def add_folder(self, folder): |
---|
1127 | n/a | """Create a folder and return an MH instance representing it.""" |
---|
1128 | n/a | return MH(os.path.join(self._path, folder), |
---|
1129 | n/a | factory=self._factory) |
---|
1130 | n/a | |
---|
1131 | n/a | def remove_folder(self, folder): |
---|
1132 | n/a | """Delete the named folder, which must be empty.""" |
---|
1133 | n/a | path = os.path.join(self._path, folder) |
---|
1134 | n/a | entries = os.listdir(path) |
---|
1135 | n/a | if entries == ['.mh_sequences']: |
---|
1136 | n/a | os.remove(os.path.join(path, '.mh_sequences')) |
---|
1137 | n/a | elif entries == []: |
---|
1138 | n/a | pass |
---|
1139 | n/a | else: |
---|
1140 | n/a | raise NotEmptyError('Folder not empty: %s' % self._path) |
---|
1141 | n/a | os.rmdir(path) |
---|
1142 | n/a | |
---|
1143 | n/a | def get_sequences(self): |
---|
1144 | n/a | """Return a name-to-key-list dictionary to define each sequence.""" |
---|
1145 | n/a | results = {} |
---|
1146 | n/a | with open(os.path.join(self._path, '.mh_sequences'), 'r', encoding='ASCII') as f: |
---|
1147 | n/a | all_keys = set(self.keys()) |
---|
1148 | n/a | for line in f: |
---|
1149 | n/a | try: |
---|
1150 | n/a | name, contents = line.split(':') |
---|
1151 | n/a | keys = set() |
---|
1152 | n/a | for spec in contents.split(): |
---|
1153 | n/a | if spec.isdigit(): |
---|
1154 | n/a | keys.add(int(spec)) |
---|
1155 | n/a | else: |
---|
1156 | n/a | start, stop = (int(x) for x in spec.split('-')) |
---|
1157 | n/a | keys.update(range(start, stop + 1)) |
---|
1158 | n/a | results[name] = [key for key in sorted(keys) \ |
---|
1159 | n/a | if key in all_keys] |
---|
1160 | n/a | if len(results[name]) == 0: |
---|
1161 | n/a | del results[name] |
---|
1162 | n/a | except ValueError: |
---|
1163 | n/a | raise FormatError('Invalid sequence specification: %s' % |
---|
1164 | n/a | line.rstrip()) |
---|
1165 | n/a | return results |
---|
1166 | n/a | |
---|
1167 | n/a | def set_sequences(self, sequences): |
---|
1168 | n/a | """Set sequences using the given name-to-key-list dictionary.""" |
---|
1169 | n/a | f = open(os.path.join(self._path, '.mh_sequences'), 'r+', encoding='ASCII') |
---|
1170 | n/a | try: |
---|
1171 | n/a | os.close(os.open(f.name, os.O_WRONLY | os.O_TRUNC)) |
---|
1172 | n/a | for name, keys in sequences.items(): |
---|
1173 | n/a | if len(keys) == 0: |
---|
1174 | n/a | continue |
---|
1175 | n/a | f.write(name + ':') |
---|
1176 | n/a | prev = None |
---|
1177 | n/a | completing = False |
---|
1178 | n/a | for key in sorted(set(keys)): |
---|
1179 | n/a | if key - 1 == prev: |
---|
1180 | n/a | if not completing: |
---|
1181 | n/a | completing = True |
---|
1182 | n/a | f.write('-') |
---|
1183 | n/a | elif completing: |
---|
1184 | n/a | completing = False |
---|
1185 | n/a | f.write('%s %s' % (prev, key)) |
---|
1186 | n/a | else: |
---|
1187 | n/a | f.write(' %s' % key) |
---|
1188 | n/a | prev = key |
---|
1189 | n/a | if completing: |
---|
1190 | n/a | f.write(str(prev) + '\n') |
---|
1191 | n/a | else: |
---|
1192 | n/a | f.write('\n') |
---|
1193 | n/a | finally: |
---|
1194 | n/a | _sync_close(f) |
---|
1195 | n/a | |
---|
1196 | n/a | def pack(self): |
---|
1197 | n/a | """Re-name messages to eliminate numbering gaps. Invalidates keys.""" |
---|
1198 | n/a | sequences = self.get_sequences() |
---|
1199 | n/a | prev = 0 |
---|
1200 | n/a | changes = [] |
---|
1201 | n/a | for key in self.iterkeys(): |
---|
1202 | n/a | if key - 1 != prev: |
---|
1203 | n/a | changes.append((key, prev + 1)) |
---|
1204 | n/a | try: |
---|
1205 | n/a | os.link(os.path.join(self._path, str(key)), |
---|
1206 | n/a | os.path.join(self._path, str(prev + 1))) |
---|
1207 | n/a | except (AttributeError, PermissionError): |
---|
1208 | n/a | os.rename(os.path.join(self._path, str(key)), |
---|
1209 | n/a | os.path.join(self._path, str(prev + 1))) |
---|
1210 | n/a | else: |
---|
1211 | n/a | os.unlink(os.path.join(self._path, str(key))) |
---|
1212 | n/a | prev += 1 |
---|
1213 | n/a | self._next_key = prev + 1 |
---|
1214 | n/a | if len(changes) == 0: |
---|
1215 | n/a | return |
---|
1216 | n/a | for name, key_list in sequences.items(): |
---|
1217 | n/a | for old, new in changes: |
---|
1218 | n/a | if old in key_list: |
---|
1219 | n/a | key_list[key_list.index(old)] = new |
---|
1220 | n/a | self.set_sequences(sequences) |
---|
1221 | n/a | |
---|
1222 | n/a | def _dump_sequences(self, message, key): |
---|
1223 | n/a | """Inspect a new MHMessage and update sequences appropriately.""" |
---|
1224 | n/a | pending_sequences = message.get_sequences() |
---|
1225 | n/a | all_sequences = self.get_sequences() |
---|
1226 | n/a | for name, key_list in all_sequences.items(): |
---|
1227 | n/a | if name in pending_sequences: |
---|
1228 | n/a | key_list.append(key) |
---|
1229 | n/a | elif key in key_list: |
---|
1230 | n/a | del key_list[key_list.index(key)] |
---|
1231 | n/a | for sequence in pending_sequences: |
---|
1232 | n/a | if sequence not in all_sequences: |
---|
1233 | n/a | all_sequences[sequence] = [key] |
---|
1234 | n/a | self.set_sequences(all_sequences) |
---|
1235 | n/a | |
---|
1236 | n/a | |
---|
1237 | n/a | class Babyl(_singlefileMailbox): |
---|
1238 | n/a | """An Rmail-style Babyl mailbox.""" |
---|
1239 | n/a | |
---|
1240 | n/a | _special_labels = frozenset({'unseen', 'deleted', 'filed', 'answered', |
---|
1241 | n/a | 'forwarded', 'edited', 'resent'}) |
---|
1242 | n/a | |
---|
1243 | n/a | def __init__(self, path, factory=None, create=True): |
---|
1244 | n/a | """Initialize a Babyl mailbox.""" |
---|
1245 | n/a | _singlefileMailbox.__init__(self, path, factory, create) |
---|
1246 | n/a | self._labels = {} |
---|
1247 | n/a | |
---|
1248 | n/a | def add(self, message): |
---|
1249 | n/a | """Add message and return assigned key.""" |
---|
1250 | n/a | key = _singlefileMailbox.add(self, message) |
---|
1251 | n/a | if isinstance(message, BabylMessage): |
---|
1252 | n/a | self._labels[key] = message.get_labels() |
---|
1253 | n/a | return key |
---|
1254 | n/a | |
---|
1255 | n/a | def remove(self, key): |
---|
1256 | n/a | """Remove the keyed message; raise KeyError if it doesn't exist.""" |
---|
1257 | n/a | _singlefileMailbox.remove(self, key) |
---|
1258 | n/a | if key in self._labels: |
---|
1259 | n/a | del self._labels[key] |
---|
1260 | n/a | |
---|
1261 | n/a | def __setitem__(self, key, message): |
---|
1262 | n/a | """Replace the keyed message; raise KeyError if it doesn't exist.""" |
---|
1263 | n/a | _singlefileMailbox.__setitem__(self, key, message) |
---|
1264 | n/a | if isinstance(message, BabylMessage): |
---|
1265 | n/a | self._labels[key] = message.get_labels() |
---|
1266 | n/a | |
---|
1267 | n/a | def get_message(self, key): |
---|
1268 | n/a | """Return a Message representation or raise a KeyError.""" |
---|
1269 | n/a | start, stop = self._lookup(key) |
---|
1270 | n/a | self._file.seek(start) |
---|
1271 | n/a | self._file.readline() # Skip b'1,' line specifying labels. |
---|
1272 | n/a | original_headers = io.BytesIO() |
---|
1273 | n/a | while True: |
---|
1274 | n/a | line = self._file.readline() |
---|
1275 | n/a | if line == b'*** EOOH ***' + linesep or not line: |
---|
1276 | n/a | break |
---|
1277 | n/a | original_headers.write(line.replace(linesep, b'\n')) |
---|
1278 | n/a | visible_headers = io.BytesIO() |
---|
1279 | n/a | while True: |
---|
1280 | n/a | line = self._file.readline() |
---|
1281 | n/a | if line == linesep or not line: |
---|
1282 | n/a | break |
---|
1283 | n/a | visible_headers.write(line.replace(linesep, b'\n')) |
---|
1284 | n/a | # Read up to the stop, or to the end |
---|
1285 | n/a | n = stop - self._file.tell() |
---|
1286 | n/a | assert n >= 0 |
---|
1287 | n/a | body = self._file.read(n) |
---|
1288 | n/a | body = body.replace(linesep, b'\n') |
---|
1289 | n/a | msg = BabylMessage(original_headers.getvalue() + body) |
---|
1290 | n/a | msg.set_visible(visible_headers.getvalue()) |
---|
1291 | n/a | if key in self._labels: |
---|
1292 | n/a | msg.set_labels(self._labels[key]) |
---|
1293 | n/a | return msg |
---|
1294 | n/a | |
---|
1295 | n/a | def get_bytes(self, key): |
---|
1296 | n/a | """Return a string representation or raise a KeyError.""" |
---|
1297 | n/a | start, stop = self._lookup(key) |
---|
1298 | n/a | self._file.seek(start) |
---|
1299 | n/a | self._file.readline() # Skip b'1,' line specifying labels. |
---|
1300 | n/a | original_headers = io.BytesIO() |
---|
1301 | n/a | while True: |
---|
1302 | n/a | line = self._file.readline() |
---|
1303 | n/a | if line == b'*** EOOH ***' + linesep or not line: |
---|
1304 | n/a | break |
---|
1305 | n/a | original_headers.write(line.replace(linesep, b'\n')) |
---|
1306 | n/a | while True: |
---|
1307 | n/a | line = self._file.readline() |
---|
1308 | n/a | if line == linesep or not line: |
---|
1309 | n/a | break |
---|
1310 | n/a | headers = original_headers.getvalue() |
---|
1311 | n/a | n = stop - self._file.tell() |
---|
1312 | n/a | assert n >= 0 |
---|
1313 | n/a | data = self._file.read(n) |
---|
1314 | n/a | data = data.replace(linesep, b'\n') |
---|
1315 | n/a | return headers + data |
---|
1316 | n/a | |
---|
1317 | n/a | def get_file(self, key): |
---|
1318 | n/a | """Return a file-like representation or raise a KeyError.""" |
---|
1319 | n/a | return io.BytesIO(self.get_bytes(key).replace(b'\n', linesep)) |
---|
1320 | n/a | |
---|
1321 | n/a | def get_labels(self): |
---|
1322 | n/a | """Return a list of user-defined labels in the mailbox.""" |
---|
1323 | n/a | self._lookup() |
---|
1324 | n/a | labels = set() |
---|
1325 | n/a | for label_list in self._labels.values(): |
---|
1326 | n/a | labels.update(label_list) |
---|
1327 | n/a | labels.difference_update(self._special_labels) |
---|
1328 | n/a | return list(labels) |
---|
1329 | n/a | |
---|
1330 | n/a | def _generate_toc(self): |
---|
1331 | n/a | """Generate key-to-(start, stop) table of contents.""" |
---|
1332 | n/a | starts, stops = [], [] |
---|
1333 | n/a | self._file.seek(0) |
---|
1334 | n/a | next_pos = 0 |
---|
1335 | n/a | label_lists = [] |
---|
1336 | n/a | while True: |
---|
1337 | n/a | line_pos = next_pos |
---|
1338 | n/a | line = self._file.readline() |
---|
1339 | n/a | next_pos = self._file.tell() |
---|
1340 | n/a | if line == b'\037\014' + linesep: |
---|
1341 | n/a | if len(stops) < len(starts): |
---|
1342 | n/a | stops.append(line_pos - len(linesep)) |
---|
1343 | n/a | starts.append(next_pos) |
---|
1344 | n/a | labels = [label.strip() for label |
---|
1345 | n/a | in self._file.readline()[1:].split(b',') |
---|
1346 | n/a | if label.strip()] |
---|
1347 | n/a | label_lists.append(labels) |
---|
1348 | n/a | elif line == b'\037' or line == b'\037' + linesep: |
---|
1349 | n/a | if len(stops) < len(starts): |
---|
1350 | n/a | stops.append(line_pos - len(linesep)) |
---|
1351 | n/a | elif not line: |
---|
1352 | n/a | stops.append(line_pos - len(linesep)) |
---|
1353 | n/a | break |
---|
1354 | n/a | self._toc = dict(enumerate(zip(starts, stops))) |
---|
1355 | n/a | self._labels = dict(enumerate(label_lists)) |
---|
1356 | n/a | self._next_key = len(self._toc) |
---|
1357 | n/a | self._file.seek(0, 2) |
---|
1358 | n/a | self._file_length = self._file.tell() |
---|
1359 | n/a | |
---|
1360 | n/a | def _pre_mailbox_hook(self, f): |
---|
1361 | n/a | """Called before writing the mailbox to file f.""" |
---|
1362 | n/a | babyl = b'BABYL OPTIONS:' + linesep |
---|
1363 | n/a | babyl += b'Version: 5' + linesep |
---|
1364 | n/a | labels = self.get_labels() |
---|
1365 | n/a | labels = (label.encode() for label in labels) |
---|
1366 | n/a | babyl += b'Labels:' + b','.join(labels) + linesep |
---|
1367 | n/a | babyl += b'\037' |
---|
1368 | n/a | f.write(babyl) |
---|
1369 | n/a | |
---|
1370 | n/a | def _pre_message_hook(self, f): |
---|
1371 | n/a | """Called before writing each message to file f.""" |
---|
1372 | n/a | f.write(b'\014' + linesep) |
---|
1373 | n/a | |
---|
1374 | n/a | def _post_message_hook(self, f): |
---|
1375 | n/a | """Called after writing each message to file f.""" |
---|
1376 | n/a | f.write(linesep + b'\037') |
---|
1377 | n/a | |
---|
1378 | n/a | def _install_message(self, message): |
---|
1379 | n/a | """Write message contents and return (start, stop).""" |
---|
1380 | n/a | start = self._file.tell() |
---|
1381 | n/a | if isinstance(message, BabylMessage): |
---|
1382 | n/a | special_labels = [] |
---|
1383 | n/a | labels = [] |
---|
1384 | n/a | for label in message.get_labels(): |
---|
1385 | n/a | if label in self._special_labels: |
---|
1386 | n/a | special_labels.append(label) |
---|
1387 | n/a | else: |
---|
1388 | n/a | labels.append(label) |
---|
1389 | n/a | self._file.write(b'1') |
---|
1390 | n/a | for label in special_labels: |
---|
1391 | n/a | self._file.write(b', ' + label.encode()) |
---|
1392 | n/a | self._file.write(b',,') |
---|
1393 | n/a | for label in labels: |
---|
1394 | n/a | self._file.write(b' ' + label.encode() + b',') |
---|
1395 | n/a | self._file.write(linesep) |
---|
1396 | n/a | else: |
---|
1397 | n/a | self._file.write(b'1,,' + linesep) |
---|
1398 | n/a | if isinstance(message, email.message.Message): |
---|
1399 | n/a | orig_buffer = io.BytesIO() |
---|
1400 | n/a | orig_generator = email.generator.BytesGenerator(orig_buffer, False, 0) |
---|
1401 | n/a | orig_generator.flatten(message) |
---|
1402 | n/a | orig_buffer.seek(0) |
---|
1403 | n/a | while True: |
---|
1404 | n/a | line = orig_buffer.readline() |
---|
1405 | n/a | self._file.write(line.replace(b'\n', linesep)) |
---|
1406 | n/a | if line == b'\n' or not line: |
---|
1407 | n/a | break |
---|
1408 | n/a | self._file.write(b'*** EOOH ***' + linesep) |
---|
1409 | n/a | if isinstance(message, BabylMessage): |
---|
1410 | n/a | vis_buffer = io.BytesIO() |
---|
1411 | n/a | vis_generator = email.generator.BytesGenerator(vis_buffer, False, 0) |
---|
1412 | n/a | vis_generator.flatten(message.get_visible()) |
---|
1413 | n/a | while True: |
---|
1414 | n/a | line = vis_buffer.readline() |
---|
1415 | n/a | self._file.write(line.replace(b'\n', linesep)) |
---|
1416 | n/a | if line == b'\n' or not line: |
---|
1417 | n/a | break |
---|
1418 | n/a | else: |
---|
1419 | n/a | orig_buffer.seek(0) |
---|
1420 | n/a | while True: |
---|
1421 | n/a | line = orig_buffer.readline() |
---|
1422 | n/a | self._file.write(line.replace(b'\n', linesep)) |
---|
1423 | n/a | if line == b'\n' or not line: |
---|
1424 | n/a | break |
---|
1425 | n/a | while True: |
---|
1426 | n/a | buffer = orig_buffer.read(4096) # Buffer size is arbitrary. |
---|
1427 | n/a | if not buffer: |
---|
1428 | n/a | break |
---|
1429 | n/a | self._file.write(buffer.replace(b'\n', linesep)) |
---|
1430 | n/a | elif isinstance(message, (bytes, str, io.StringIO)): |
---|
1431 | n/a | if isinstance(message, io.StringIO): |
---|
1432 | n/a | warnings.warn("Use of StringIO input is deprecated, " |
---|
1433 | n/a | "use BytesIO instead", DeprecationWarning, 3) |
---|
1434 | n/a | message = message.getvalue() |
---|
1435 | n/a | if isinstance(message, str): |
---|
1436 | n/a | message = self._string_to_bytes(message) |
---|
1437 | n/a | body_start = message.find(b'\n\n') + 2 |
---|
1438 | n/a | if body_start - 2 != -1: |
---|
1439 | n/a | self._file.write(message[:body_start].replace(b'\n', linesep)) |
---|
1440 | n/a | self._file.write(b'*** EOOH ***' + linesep) |
---|
1441 | n/a | self._file.write(message[:body_start].replace(b'\n', linesep)) |
---|
1442 | n/a | self._file.write(message[body_start:].replace(b'\n', linesep)) |
---|
1443 | n/a | else: |
---|
1444 | n/a | self._file.write(b'*** EOOH ***' + linesep + linesep) |
---|
1445 | n/a | self._file.write(message.replace(b'\n', linesep)) |
---|
1446 | n/a | elif hasattr(message, 'readline'): |
---|
1447 | n/a | if hasattr(message, 'buffer'): |
---|
1448 | n/a | warnings.warn("Use of text mode files is deprecated, " |
---|
1449 | n/a | "use a binary mode file instead", DeprecationWarning, 3) |
---|
1450 | n/a | message = message.buffer |
---|
1451 | n/a | original_pos = message.tell() |
---|
1452 | n/a | first_pass = True |
---|
1453 | n/a | while True: |
---|
1454 | n/a | line = message.readline() |
---|
1455 | n/a | # Universal newline support. |
---|
1456 | n/a | if line.endswith(b'\r\n'): |
---|
1457 | n/a | line = line[:-2] + b'\n' |
---|
1458 | n/a | elif line.endswith(b'\r'): |
---|
1459 | n/a | line = line[:-1] + b'\n' |
---|
1460 | n/a | self._file.write(line.replace(b'\n', linesep)) |
---|
1461 | n/a | if line == b'\n' or not line: |
---|
1462 | n/a | if first_pass: |
---|
1463 | n/a | first_pass = False |
---|
1464 | n/a | self._file.write(b'*** EOOH ***' + linesep) |
---|
1465 | n/a | message.seek(original_pos) |
---|
1466 | n/a | else: |
---|
1467 | n/a | break |
---|
1468 | n/a | while True: |
---|
1469 | n/a | line = message.readline() |
---|
1470 | n/a | if not line: |
---|
1471 | n/a | break |
---|
1472 | n/a | # Universal newline support. |
---|
1473 | n/a | if line.endswith(b'\r\n'): |
---|
1474 | n/a | line = line[:-2] + linesep |
---|
1475 | n/a | elif line.endswith(b'\r'): |
---|
1476 | n/a | line = line[:-1] + linesep |
---|
1477 | n/a | elif line.endswith(b'\n'): |
---|
1478 | n/a | line = line[:-1] + linesep |
---|
1479 | n/a | self._file.write(line) |
---|
1480 | n/a | else: |
---|
1481 | n/a | raise TypeError('Invalid message type: %s' % type(message)) |
---|
1482 | n/a | stop = self._file.tell() |
---|
1483 | n/a | return (start, stop) |
---|
1484 | n/a | |
---|
1485 | n/a | |
---|
1486 | n/a | class Message(email.message.Message): |
---|
1487 | n/a | """Message with mailbox-format-specific properties.""" |
---|
1488 | n/a | |
---|
1489 | n/a | def __init__(self, message=None): |
---|
1490 | n/a | """Initialize a Message instance.""" |
---|
1491 | n/a | if isinstance(message, email.message.Message): |
---|
1492 | n/a | self._become_message(copy.deepcopy(message)) |
---|
1493 | n/a | if isinstance(message, Message): |
---|
1494 | n/a | message._explain_to(self) |
---|
1495 | n/a | elif isinstance(message, bytes): |
---|
1496 | n/a | self._become_message(email.message_from_bytes(message)) |
---|
1497 | n/a | elif isinstance(message, str): |
---|
1498 | n/a | self._become_message(email.message_from_string(message)) |
---|
1499 | n/a | elif isinstance(message, io.TextIOWrapper): |
---|
1500 | n/a | self._become_message(email.message_from_file(message)) |
---|
1501 | n/a | elif hasattr(message, "read"): |
---|
1502 | n/a | self._become_message(email.message_from_binary_file(message)) |
---|
1503 | n/a | elif message is None: |
---|
1504 | n/a | email.message.Message.__init__(self) |
---|
1505 | n/a | else: |
---|
1506 | n/a | raise TypeError('Invalid message type: %s' % type(message)) |
---|
1507 | n/a | |
---|
1508 | n/a | def _become_message(self, message): |
---|
1509 | n/a | """Assume the non-format-specific state of message.""" |
---|
1510 | n/a | type_specific = getattr(message, '_type_specific_attributes', []) |
---|
1511 | n/a | for name in message.__dict__: |
---|
1512 | n/a | if name not in type_specific: |
---|
1513 | n/a | self.__dict__[name] = message.__dict__[name] |
---|
1514 | n/a | |
---|
1515 | n/a | def _explain_to(self, message): |
---|
1516 | n/a | """Copy format-specific state to message insofar as possible.""" |
---|
1517 | n/a | if isinstance(message, Message): |
---|
1518 | n/a | return # There's nothing format-specific to explain. |
---|
1519 | n/a | else: |
---|
1520 | n/a | raise TypeError('Cannot convert to specified type') |
---|
1521 | n/a | |
---|
1522 | n/a | |
---|
1523 | n/a | class MaildirMessage(Message): |
---|
1524 | n/a | """Message with Maildir-specific properties.""" |
---|
1525 | n/a | |
---|
1526 | n/a | _type_specific_attributes = ['_subdir', '_info', '_date'] |
---|
1527 | n/a | |
---|
1528 | n/a | def __init__(self, message=None): |
---|
1529 | n/a | """Initialize a MaildirMessage instance.""" |
---|
1530 | n/a | self._subdir = 'new' |
---|
1531 | n/a | self._info = '' |
---|
1532 | n/a | self._date = time.time() |
---|
1533 | n/a | Message.__init__(self, message) |
---|
1534 | n/a | |
---|
1535 | n/a | def get_subdir(self): |
---|
1536 | n/a | """Return 'new' or 'cur'.""" |
---|
1537 | n/a | return self._subdir |
---|
1538 | n/a | |
---|
1539 | n/a | def set_subdir(self, subdir): |
---|
1540 | n/a | """Set subdir to 'new' or 'cur'.""" |
---|
1541 | n/a | if subdir == 'new' or subdir == 'cur': |
---|
1542 | n/a | self._subdir = subdir |
---|
1543 | n/a | else: |
---|
1544 | n/a | raise ValueError("subdir must be 'new' or 'cur': %s" % subdir) |
---|
1545 | n/a | |
---|
1546 | n/a | def get_flags(self): |
---|
1547 | n/a | """Return as a string the flags that are set.""" |
---|
1548 | n/a | if self._info.startswith('2,'): |
---|
1549 | n/a | return self._info[2:] |
---|
1550 | n/a | else: |
---|
1551 | n/a | return '' |
---|
1552 | n/a | |
---|
1553 | n/a | def set_flags(self, flags): |
---|
1554 | n/a | """Set the given flags and unset all others.""" |
---|
1555 | n/a | self._info = '2,' + ''.join(sorted(flags)) |
---|
1556 | n/a | |
---|
1557 | n/a | def add_flag(self, flag): |
---|
1558 | n/a | """Set the given flag(s) without changing others.""" |
---|
1559 | n/a | self.set_flags(''.join(set(self.get_flags()) | set(flag))) |
---|
1560 | n/a | |
---|
1561 | n/a | def remove_flag(self, flag): |
---|
1562 | n/a | """Unset the given string flag(s) without changing others.""" |
---|
1563 | n/a | if self.get_flags(): |
---|
1564 | n/a | self.set_flags(''.join(set(self.get_flags()) - set(flag))) |
---|
1565 | n/a | |
---|
1566 | n/a | def get_date(self): |
---|
1567 | n/a | """Return delivery date of message, in seconds since the epoch.""" |
---|
1568 | n/a | return self._date |
---|
1569 | n/a | |
---|
1570 | n/a | def set_date(self, date): |
---|
1571 | n/a | """Set delivery date of message, in seconds since the epoch.""" |
---|
1572 | n/a | try: |
---|
1573 | n/a | self._date = float(date) |
---|
1574 | n/a | except ValueError: |
---|
1575 | n/a | raise TypeError("can't convert to float: %s" % date) |
---|
1576 | n/a | |
---|
1577 | n/a | def get_info(self): |
---|
1578 | n/a | """Get the message's "info" as a string.""" |
---|
1579 | n/a | return self._info |
---|
1580 | n/a | |
---|
1581 | n/a | def set_info(self, info): |
---|
1582 | n/a | """Set the message's "info" string.""" |
---|
1583 | n/a | if isinstance(info, str): |
---|
1584 | n/a | self._info = info |
---|
1585 | n/a | else: |
---|
1586 | n/a | raise TypeError('info must be a string: %s' % type(info)) |
---|
1587 | n/a | |
---|
1588 | n/a | def _explain_to(self, message): |
---|
1589 | n/a | """Copy Maildir-specific state to message insofar as possible.""" |
---|
1590 | n/a | if isinstance(message, MaildirMessage): |
---|
1591 | n/a | message.set_flags(self.get_flags()) |
---|
1592 | n/a | message.set_subdir(self.get_subdir()) |
---|
1593 | n/a | message.set_date(self.get_date()) |
---|
1594 | n/a | elif isinstance(message, _mboxMMDFMessage): |
---|
1595 | n/a | flags = set(self.get_flags()) |
---|
1596 | n/a | if 'S' in flags: |
---|
1597 | n/a | message.add_flag('R') |
---|
1598 | n/a | if self.get_subdir() == 'cur': |
---|
1599 | n/a | message.add_flag('O') |
---|
1600 | n/a | if 'T' in flags: |
---|
1601 | n/a | message.add_flag('D') |
---|
1602 | n/a | if 'F' in flags: |
---|
1603 | n/a | message.add_flag('F') |
---|
1604 | n/a | if 'R' in flags: |
---|
1605 | n/a | message.add_flag('A') |
---|
1606 | n/a | message.set_from('MAILER-DAEMON', time.gmtime(self.get_date())) |
---|
1607 | n/a | elif isinstance(message, MHMessage): |
---|
1608 | n/a | flags = set(self.get_flags()) |
---|
1609 | n/a | if 'S' not in flags: |
---|
1610 | n/a | message.add_sequence('unseen') |
---|
1611 | n/a | if 'R' in flags: |
---|
1612 | n/a | message.add_sequence('replied') |
---|
1613 | n/a | if 'F' in flags: |
---|
1614 | n/a | message.add_sequence('flagged') |
---|
1615 | n/a | elif isinstance(message, BabylMessage): |
---|
1616 | n/a | flags = set(self.get_flags()) |
---|
1617 | n/a | if 'S' not in flags: |
---|
1618 | n/a | message.add_label('unseen') |
---|
1619 | n/a | if 'T' in flags: |
---|
1620 | n/a | message.add_label('deleted') |
---|
1621 | n/a | if 'R' in flags: |
---|
1622 | n/a | message.add_label('answered') |
---|
1623 | n/a | if 'P' in flags: |
---|
1624 | n/a | message.add_label('forwarded') |
---|
1625 | n/a | elif isinstance(message, Message): |
---|
1626 | n/a | pass |
---|
1627 | n/a | else: |
---|
1628 | n/a | raise TypeError('Cannot convert to specified type: %s' % |
---|
1629 | n/a | type(message)) |
---|
1630 | n/a | |
---|
1631 | n/a | |
---|
1632 | n/a | class _mboxMMDFMessage(Message): |
---|
1633 | n/a | """Message with mbox- or MMDF-specific properties.""" |
---|
1634 | n/a | |
---|
1635 | n/a | _type_specific_attributes = ['_from'] |
---|
1636 | n/a | |
---|
1637 | n/a | def __init__(self, message=None): |
---|
1638 | n/a | """Initialize an mboxMMDFMessage instance.""" |
---|
1639 | n/a | self.set_from('MAILER-DAEMON', True) |
---|
1640 | n/a | if isinstance(message, email.message.Message): |
---|
1641 | n/a | unixfrom = message.get_unixfrom() |
---|
1642 | n/a | if unixfrom is not None and unixfrom.startswith('From '): |
---|
1643 | n/a | self.set_from(unixfrom[5:]) |
---|
1644 | n/a | Message.__init__(self, message) |
---|
1645 | n/a | |
---|
1646 | n/a | def get_from(self): |
---|
1647 | n/a | """Return contents of "From " line.""" |
---|
1648 | n/a | return self._from |
---|
1649 | n/a | |
---|
1650 | n/a | def set_from(self, from_, time_=None): |
---|
1651 | n/a | """Set "From " line, formatting and appending time_ if specified.""" |
---|
1652 | n/a | if time_ is not None: |
---|
1653 | n/a | if time_ is True: |
---|
1654 | n/a | time_ = time.gmtime() |
---|
1655 | n/a | from_ += ' ' + time.asctime(time_) |
---|
1656 | n/a | self._from = from_ |
---|
1657 | n/a | |
---|
1658 | n/a | def get_flags(self): |
---|
1659 | n/a | """Return as a string the flags that are set.""" |
---|
1660 | n/a | return self.get('Status', '') + self.get('X-Status', '') |
---|
1661 | n/a | |
---|
1662 | n/a | def set_flags(self, flags): |
---|
1663 | n/a | """Set the given flags and unset all others.""" |
---|
1664 | n/a | flags = set(flags) |
---|
1665 | n/a | status_flags, xstatus_flags = '', '' |
---|
1666 | n/a | for flag in ('R', 'O'): |
---|
1667 | n/a | if flag in flags: |
---|
1668 | n/a | status_flags += flag |
---|
1669 | n/a | flags.remove(flag) |
---|
1670 | n/a | for flag in ('D', 'F', 'A'): |
---|
1671 | n/a | if flag in flags: |
---|
1672 | n/a | xstatus_flags += flag |
---|
1673 | n/a | flags.remove(flag) |
---|
1674 | n/a | xstatus_flags += ''.join(sorted(flags)) |
---|
1675 | n/a | try: |
---|
1676 | n/a | self.replace_header('Status', status_flags) |
---|
1677 | n/a | except KeyError: |
---|
1678 | n/a | self.add_header('Status', status_flags) |
---|
1679 | n/a | try: |
---|
1680 | n/a | self.replace_header('X-Status', xstatus_flags) |
---|
1681 | n/a | except KeyError: |
---|
1682 | n/a | self.add_header('X-Status', xstatus_flags) |
---|
1683 | n/a | |
---|
1684 | n/a | def add_flag(self, flag): |
---|
1685 | n/a | """Set the given flag(s) without changing others.""" |
---|
1686 | n/a | self.set_flags(''.join(set(self.get_flags()) | set(flag))) |
---|
1687 | n/a | |
---|
1688 | n/a | def remove_flag(self, flag): |
---|
1689 | n/a | """Unset the given string flag(s) without changing others.""" |
---|
1690 | n/a | if 'Status' in self or 'X-Status' in self: |
---|
1691 | n/a | self.set_flags(''.join(set(self.get_flags()) - set(flag))) |
---|
1692 | n/a | |
---|
1693 | n/a | def _explain_to(self, message): |
---|
1694 | n/a | """Copy mbox- or MMDF-specific state to message insofar as possible.""" |
---|
1695 | n/a | if isinstance(message, MaildirMessage): |
---|
1696 | n/a | flags = set(self.get_flags()) |
---|
1697 | n/a | if 'O' in flags: |
---|
1698 | n/a | message.set_subdir('cur') |
---|
1699 | n/a | if 'F' in flags: |
---|
1700 | n/a | message.add_flag('F') |
---|
1701 | n/a | if 'A' in flags: |
---|
1702 | n/a | message.add_flag('R') |
---|
1703 | n/a | if 'R' in flags: |
---|
1704 | n/a | message.add_flag('S') |
---|
1705 | n/a | if 'D' in flags: |
---|
1706 | n/a | message.add_flag('T') |
---|
1707 | n/a | del message['status'] |
---|
1708 | n/a | del message['x-status'] |
---|
1709 | n/a | maybe_date = ' '.join(self.get_from().split()[-5:]) |
---|
1710 | n/a | try: |
---|
1711 | n/a | message.set_date(calendar.timegm(time.strptime(maybe_date, |
---|
1712 | n/a | '%a %b %d %H:%M:%S %Y'))) |
---|
1713 | n/a | except (ValueError, OverflowError): |
---|
1714 | n/a | pass |
---|
1715 | n/a | elif isinstance(message, _mboxMMDFMessage): |
---|
1716 | n/a | message.set_flags(self.get_flags()) |
---|
1717 | n/a | message.set_from(self.get_from()) |
---|
1718 | n/a | elif isinstance(message, MHMessage): |
---|
1719 | n/a | flags = set(self.get_flags()) |
---|
1720 | n/a | if 'R' not in flags: |
---|
1721 | n/a | message.add_sequence('unseen') |
---|
1722 | n/a | if 'A' in flags: |
---|
1723 | n/a | message.add_sequence('replied') |
---|
1724 | n/a | if 'F' in flags: |
---|
1725 | n/a | message.add_sequence('flagged') |
---|
1726 | n/a | del message['status'] |
---|
1727 | n/a | del message['x-status'] |
---|
1728 | n/a | elif isinstance(message, BabylMessage): |
---|
1729 | n/a | flags = set(self.get_flags()) |
---|
1730 | n/a | if 'R' not in flags: |
---|
1731 | n/a | message.add_label('unseen') |
---|
1732 | n/a | if 'D' in flags: |
---|
1733 | n/a | message.add_label('deleted') |
---|
1734 | n/a | if 'A' in flags: |
---|
1735 | n/a | message.add_label('answered') |
---|
1736 | n/a | del message['status'] |
---|
1737 | n/a | del message['x-status'] |
---|
1738 | n/a | elif isinstance(message, Message): |
---|
1739 | n/a | pass |
---|
1740 | n/a | else: |
---|
1741 | n/a | raise TypeError('Cannot convert to specified type: %s' % |
---|
1742 | n/a | type(message)) |
---|
1743 | n/a | |
---|
1744 | n/a | |
---|
1745 | n/a | class mboxMessage(_mboxMMDFMessage): |
---|
1746 | n/a | """Message with mbox-specific properties.""" |
---|
1747 | n/a | |
---|
1748 | n/a | |
---|
1749 | n/a | class MHMessage(Message): |
---|
1750 | n/a | """Message with MH-specific properties.""" |
---|
1751 | n/a | |
---|
1752 | n/a | _type_specific_attributes = ['_sequences'] |
---|
1753 | n/a | |
---|
1754 | n/a | def __init__(self, message=None): |
---|
1755 | n/a | """Initialize an MHMessage instance.""" |
---|
1756 | n/a | self._sequences = [] |
---|
1757 | n/a | Message.__init__(self, message) |
---|
1758 | n/a | |
---|
1759 | n/a | def get_sequences(self): |
---|
1760 | n/a | """Return a list of sequences that include the message.""" |
---|
1761 | n/a | return self._sequences[:] |
---|
1762 | n/a | |
---|
1763 | n/a | def set_sequences(self, sequences): |
---|
1764 | n/a | """Set the list of sequences that include the message.""" |
---|
1765 | n/a | self._sequences = list(sequences) |
---|
1766 | n/a | |
---|
1767 | n/a | def add_sequence(self, sequence): |
---|
1768 | n/a | """Add sequence to list of sequences including the message.""" |
---|
1769 | n/a | if isinstance(sequence, str): |
---|
1770 | n/a | if not sequence in self._sequences: |
---|
1771 | n/a | self._sequences.append(sequence) |
---|
1772 | n/a | else: |
---|
1773 | n/a | raise TypeError('sequence type must be str: %s' % type(sequence)) |
---|
1774 | n/a | |
---|
1775 | n/a | def remove_sequence(self, sequence): |
---|
1776 | n/a | """Remove sequence from the list of sequences including the message.""" |
---|
1777 | n/a | try: |
---|
1778 | n/a | self._sequences.remove(sequence) |
---|
1779 | n/a | except ValueError: |
---|
1780 | n/a | pass |
---|
1781 | n/a | |
---|
1782 | n/a | def _explain_to(self, message): |
---|
1783 | n/a | """Copy MH-specific state to message insofar as possible.""" |
---|
1784 | n/a | if isinstance(message, MaildirMessage): |
---|
1785 | n/a | sequences = set(self.get_sequences()) |
---|
1786 | n/a | if 'unseen' in sequences: |
---|
1787 | n/a | message.set_subdir('cur') |
---|
1788 | n/a | else: |
---|
1789 | n/a | message.set_subdir('cur') |
---|
1790 | n/a | message.add_flag('S') |
---|
1791 | n/a | if 'flagged' in sequences: |
---|
1792 | n/a | message.add_flag('F') |
---|
1793 | n/a | if 'replied' in sequences: |
---|
1794 | n/a | message.add_flag('R') |
---|
1795 | n/a | elif isinstance(message, _mboxMMDFMessage): |
---|
1796 | n/a | sequences = set(self.get_sequences()) |
---|
1797 | n/a | if 'unseen' not in sequences: |
---|
1798 | n/a | message.add_flag('RO') |
---|
1799 | n/a | else: |
---|
1800 | n/a | message.add_flag('O') |
---|
1801 | n/a | if 'flagged' in sequences: |
---|
1802 | n/a | message.add_flag('F') |
---|
1803 | n/a | if 'replied' in sequences: |
---|
1804 | n/a | message.add_flag('A') |
---|
1805 | n/a | elif isinstance(message, MHMessage): |
---|
1806 | n/a | for sequence in self.get_sequences(): |
---|
1807 | n/a | message.add_sequence(sequence) |
---|
1808 | n/a | elif isinstance(message, BabylMessage): |
---|
1809 | n/a | sequences = set(self.get_sequences()) |
---|
1810 | n/a | if 'unseen' in sequences: |
---|
1811 | n/a | message.add_label('unseen') |
---|
1812 | n/a | if 'replied' in sequences: |
---|
1813 | n/a | message.add_label('answered') |
---|
1814 | n/a | elif isinstance(message, Message): |
---|
1815 | n/a | pass |
---|
1816 | n/a | else: |
---|
1817 | n/a | raise TypeError('Cannot convert to specified type: %s' % |
---|
1818 | n/a | type(message)) |
---|
1819 | n/a | |
---|
1820 | n/a | |
---|
1821 | n/a | class BabylMessage(Message): |
---|
1822 | n/a | """Message with Babyl-specific properties.""" |
---|
1823 | n/a | |
---|
1824 | n/a | _type_specific_attributes = ['_labels', '_visible'] |
---|
1825 | n/a | |
---|
1826 | n/a | def __init__(self, message=None): |
---|
1827 | n/a | """Initialize a BabylMessage instance.""" |
---|
1828 | n/a | self._labels = [] |
---|
1829 | n/a | self._visible = Message() |
---|
1830 | n/a | Message.__init__(self, message) |
---|
1831 | n/a | |
---|
1832 | n/a | def get_labels(self): |
---|
1833 | n/a | """Return a list of labels on the message.""" |
---|
1834 | n/a | return self._labels[:] |
---|
1835 | n/a | |
---|
1836 | n/a | def set_labels(self, labels): |
---|
1837 | n/a | """Set the list of labels on the message.""" |
---|
1838 | n/a | self._labels = list(labels) |
---|
1839 | n/a | |
---|
1840 | n/a | def add_label(self, label): |
---|
1841 | n/a | """Add label to list of labels on the message.""" |
---|
1842 | n/a | if isinstance(label, str): |
---|
1843 | n/a | if label not in self._labels: |
---|
1844 | n/a | self._labels.append(label) |
---|
1845 | n/a | else: |
---|
1846 | n/a | raise TypeError('label must be a string: %s' % type(label)) |
---|
1847 | n/a | |
---|
1848 | n/a | def remove_label(self, label): |
---|
1849 | n/a | """Remove label from the list of labels on the message.""" |
---|
1850 | n/a | try: |
---|
1851 | n/a | self._labels.remove(label) |
---|
1852 | n/a | except ValueError: |
---|
1853 | n/a | pass |
---|
1854 | n/a | |
---|
1855 | n/a | def get_visible(self): |
---|
1856 | n/a | """Return a Message representation of visible headers.""" |
---|
1857 | n/a | return Message(self._visible) |
---|
1858 | n/a | |
---|
1859 | n/a | def set_visible(self, visible): |
---|
1860 | n/a | """Set the Message representation of visible headers.""" |
---|
1861 | n/a | self._visible = Message(visible) |
---|
1862 | n/a | |
---|
1863 | n/a | def update_visible(self): |
---|
1864 | n/a | """Update and/or sensibly generate a set of visible headers.""" |
---|
1865 | n/a | for header in self._visible.keys(): |
---|
1866 | n/a | if header in self: |
---|
1867 | n/a | self._visible.replace_header(header, self[header]) |
---|
1868 | n/a | else: |
---|
1869 | n/a | del self._visible[header] |
---|
1870 | n/a | for header in ('Date', 'From', 'Reply-To', 'To', 'CC', 'Subject'): |
---|
1871 | n/a | if header in self and header not in self._visible: |
---|
1872 | n/a | self._visible[header] = self[header] |
---|
1873 | n/a | |
---|
1874 | n/a | def _explain_to(self, message): |
---|
1875 | n/a | """Copy Babyl-specific state to message insofar as possible.""" |
---|
1876 | n/a | if isinstance(message, MaildirMessage): |
---|
1877 | n/a | labels = set(self.get_labels()) |
---|
1878 | n/a | if 'unseen' in labels: |
---|
1879 | n/a | message.set_subdir('cur') |
---|
1880 | n/a | else: |
---|
1881 | n/a | message.set_subdir('cur') |
---|
1882 | n/a | message.add_flag('S') |
---|
1883 | n/a | if 'forwarded' in labels or 'resent' in labels: |
---|
1884 | n/a | message.add_flag('P') |
---|
1885 | n/a | if 'answered' in labels: |
---|
1886 | n/a | message.add_flag('R') |
---|
1887 | n/a | if 'deleted' in labels: |
---|
1888 | n/a | message.add_flag('T') |
---|
1889 | n/a | elif isinstance(message, _mboxMMDFMessage): |
---|
1890 | n/a | labels = set(self.get_labels()) |
---|
1891 | n/a | if 'unseen' not in labels: |
---|
1892 | n/a | message.add_flag('RO') |
---|
1893 | n/a | else: |
---|
1894 | n/a | message.add_flag('O') |
---|
1895 | n/a | if 'deleted' in labels: |
---|
1896 | n/a | message.add_flag('D') |
---|
1897 | n/a | if 'answered' in labels: |
---|
1898 | n/a | message.add_flag('A') |
---|
1899 | n/a | elif isinstance(message, MHMessage): |
---|
1900 | n/a | labels = set(self.get_labels()) |
---|
1901 | n/a | if 'unseen' in labels: |
---|
1902 | n/a | message.add_sequence('unseen') |
---|
1903 | n/a | if 'answered' in labels: |
---|
1904 | n/a | message.add_sequence('replied') |
---|
1905 | n/a | elif isinstance(message, BabylMessage): |
---|
1906 | n/a | message.set_visible(self.get_visible()) |
---|
1907 | n/a | for label in self.get_labels(): |
---|
1908 | n/a | message.add_label(label) |
---|
1909 | n/a | elif isinstance(message, Message): |
---|
1910 | n/a | pass |
---|
1911 | n/a | else: |
---|
1912 | n/a | raise TypeError('Cannot convert to specified type: %s' % |
---|
1913 | n/a | type(message)) |
---|
1914 | n/a | |
---|
1915 | n/a | |
---|
1916 | n/a | class MMDFMessage(_mboxMMDFMessage): |
---|
1917 | n/a | """Message with MMDF-specific properties.""" |
---|
1918 | n/a | |
---|
1919 | n/a | |
---|
1920 | n/a | class _ProxyFile: |
---|
1921 | n/a | """A read-only wrapper of a file.""" |
---|
1922 | n/a | |
---|
1923 | n/a | def __init__(self, f, pos=None): |
---|
1924 | n/a | """Initialize a _ProxyFile.""" |
---|
1925 | n/a | self._file = f |
---|
1926 | n/a | if pos is None: |
---|
1927 | n/a | self._pos = f.tell() |
---|
1928 | n/a | else: |
---|
1929 | n/a | self._pos = pos |
---|
1930 | n/a | |
---|
1931 | n/a | def read(self, size=None): |
---|
1932 | n/a | """Read bytes.""" |
---|
1933 | n/a | return self._read(size, self._file.read) |
---|
1934 | n/a | |
---|
1935 | n/a | def read1(self, size=None): |
---|
1936 | n/a | """Read bytes.""" |
---|
1937 | n/a | return self._read(size, self._file.read1) |
---|
1938 | n/a | |
---|
1939 | n/a | def readline(self, size=None): |
---|
1940 | n/a | """Read a line.""" |
---|
1941 | n/a | return self._read(size, self._file.readline) |
---|
1942 | n/a | |
---|
1943 | n/a | def readlines(self, sizehint=None): |
---|
1944 | n/a | """Read multiple lines.""" |
---|
1945 | n/a | result = [] |
---|
1946 | n/a | for line in self: |
---|
1947 | n/a | result.append(line) |
---|
1948 | n/a | if sizehint is not None: |
---|
1949 | n/a | sizehint -= len(line) |
---|
1950 | n/a | if sizehint <= 0: |
---|
1951 | n/a | break |
---|
1952 | n/a | return result |
---|
1953 | n/a | |
---|
1954 | n/a | def __iter__(self): |
---|
1955 | n/a | """Iterate over lines.""" |
---|
1956 | n/a | while True: |
---|
1957 | n/a | line = self.readline() |
---|
1958 | n/a | if not line: |
---|
1959 | n/a | return |
---|
1960 | n/a | yield line |
---|
1961 | n/a | |
---|
1962 | n/a | def tell(self): |
---|
1963 | n/a | """Return the position.""" |
---|
1964 | n/a | return self._pos |
---|
1965 | n/a | |
---|
1966 | n/a | def seek(self, offset, whence=0): |
---|
1967 | n/a | """Change position.""" |
---|
1968 | n/a | if whence == 1: |
---|
1969 | n/a | self._file.seek(self._pos) |
---|
1970 | n/a | self._file.seek(offset, whence) |
---|
1971 | n/a | self._pos = self._file.tell() |
---|
1972 | n/a | |
---|
1973 | n/a | def close(self): |
---|
1974 | n/a | """Close the file.""" |
---|
1975 | n/a | if hasattr(self, '_file'): |
---|
1976 | n/a | try: |
---|
1977 | n/a | if hasattr(self._file, 'close'): |
---|
1978 | n/a | self._file.close() |
---|
1979 | n/a | finally: |
---|
1980 | n/a | del self._file |
---|
1981 | n/a | |
---|
1982 | n/a | def _read(self, size, read_method): |
---|
1983 | n/a | """Read size bytes using read_method.""" |
---|
1984 | n/a | if size is None: |
---|
1985 | n/a | size = -1 |
---|
1986 | n/a | self._file.seek(self._pos) |
---|
1987 | n/a | result = read_method(size) |
---|
1988 | n/a | self._pos = self._file.tell() |
---|
1989 | n/a | return result |
---|
1990 | n/a | |
---|
1991 | n/a | def __enter__(self): |
---|
1992 | n/a | """Context management protocol support.""" |
---|
1993 | n/a | return self |
---|
1994 | n/a | |
---|
1995 | n/a | def __exit__(self, *exc): |
---|
1996 | n/a | self.close() |
---|
1997 | n/a | |
---|
1998 | n/a | def readable(self): |
---|
1999 | n/a | return self._file.readable() |
---|
2000 | n/a | |
---|
2001 | n/a | def writable(self): |
---|
2002 | n/a | return self._file.writable() |
---|
2003 | n/a | |
---|
2004 | n/a | def seekable(self): |
---|
2005 | n/a | return self._file.seekable() |
---|
2006 | n/a | |
---|
2007 | n/a | def flush(self): |
---|
2008 | n/a | return self._file.flush() |
---|
2009 | n/a | |
---|
2010 | n/a | @property |
---|
2011 | n/a | def closed(self): |
---|
2012 | n/a | if not hasattr(self, '_file'): |
---|
2013 | n/a | return True |
---|
2014 | n/a | if not hasattr(self._file, 'closed'): |
---|
2015 | n/a | return False |
---|
2016 | n/a | return self._file.closed |
---|
2017 | n/a | |
---|
2018 | n/a | |
---|
2019 | n/a | class _PartialFile(_ProxyFile): |
---|
2020 | n/a | """A read-only wrapper of part of a file.""" |
---|
2021 | n/a | |
---|
2022 | n/a | def __init__(self, f, start=None, stop=None): |
---|
2023 | n/a | """Initialize a _PartialFile.""" |
---|
2024 | n/a | _ProxyFile.__init__(self, f, start) |
---|
2025 | n/a | self._start = start |
---|
2026 | n/a | self._stop = stop |
---|
2027 | n/a | |
---|
2028 | n/a | def tell(self): |
---|
2029 | n/a | """Return the position with respect to start.""" |
---|
2030 | n/a | return _ProxyFile.tell(self) - self._start |
---|
2031 | n/a | |
---|
2032 | n/a | def seek(self, offset, whence=0): |
---|
2033 | n/a | """Change position, possibly with respect to start or stop.""" |
---|
2034 | n/a | if whence == 0: |
---|
2035 | n/a | self._pos = self._start |
---|
2036 | n/a | whence = 1 |
---|
2037 | n/a | elif whence == 2: |
---|
2038 | n/a | self._pos = self._stop |
---|
2039 | n/a | whence = 1 |
---|
2040 | n/a | _ProxyFile.seek(self, offset, whence) |
---|
2041 | n/a | |
---|
2042 | n/a | def _read(self, size, read_method): |
---|
2043 | n/a | """Read size bytes using read_method, honoring start and stop.""" |
---|
2044 | n/a | remaining = self._stop - self._pos |
---|
2045 | n/a | if remaining <= 0: |
---|
2046 | n/a | return b'' |
---|
2047 | n/a | if size is None or size < 0 or size > remaining: |
---|
2048 | n/a | size = remaining |
---|
2049 | n/a | return _ProxyFile._read(self, size, read_method) |
---|
2050 | n/a | |
---|
2051 | n/a | def close(self): |
---|
2052 | n/a | # do *not* close the underlying file object for partial files, |
---|
2053 | n/a | # since it's global to the mailbox object |
---|
2054 | n/a | if hasattr(self, '_file'): |
---|
2055 | n/a | del self._file |
---|
2056 | n/a | |
---|
2057 | n/a | |
---|
2058 | n/a | def _lock_file(f, dotlock=True): |
---|
2059 | n/a | """Lock file f using lockf and dot locking.""" |
---|
2060 | n/a | dotlock_done = False |
---|
2061 | n/a | try: |
---|
2062 | n/a | if fcntl: |
---|
2063 | n/a | try: |
---|
2064 | n/a | fcntl.lockf(f, fcntl.LOCK_EX | fcntl.LOCK_NB) |
---|
2065 | n/a | except OSError as e: |
---|
2066 | n/a | if e.errno in (errno.EAGAIN, errno.EACCES, errno.EROFS): |
---|
2067 | n/a | raise ExternalClashError('lockf: lock unavailable: %s' % |
---|
2068 | n/a | f.name) |
---|
2069 | n/a | else: |
---|
2070 | n/a | raise |
---|
2071 | n/a | if dotlock: |
---|
2072 | n/a | try: |
---|
2073 | n/a | pre_lock = _create_temporary(f.name + '.lock') |
---|
2074 | n/a | pre_lock.close() |
---|
2075 | n/a | except OSError as e: |
---|
2076 | n/a | if e.errno in (errno.EACCES, errno.EROFS): |
---|
2077 | n/a | return # Without write access, just skip dotlocking. |
---|
2078 | n/a | else: |
---|
2079 | n/a | raise |
---|
2080 | n/a | try: |
---|
2081 | n/a | try: |
---|
2082 | n/a | os.link(pre_lock.name, f.name + '.lock') |
---|
2083 | n/a | dotlock_done = True |
---|
2084 | n/a | except (AttributeError, PermissionError): |
---|
2085 | n/a | os.rename(pre_lock.name, f.name + '.lock') |
---|
2086 | n/a | dotlock_done = True |
---|
2087 | n/a | else: |
---|
2088 | n/a | os.unlink(pre_lock.name) |
---|
2089 | n/a | except FileExistsError: |
---|
2090 | n/a | os.remove(pre_lock.name) |
---|
2091 | n/a | raise ExternalClashError('dot lock unavailable: %s' % |
---|
2092 | n/a | f.name) |
---|
2093 | n/a | except: |
---|
2094 | n/a | if fcntl: |
---|
2095 | n/a | fcntl.lockf(f, fcntl.LOCK_UN) |
---|
2096 | n/a | if dotlock_done: |
---|
2097 | n/a | os.remove(f.name + '.lock') |
---|
2098 | n/a | raise |
---|
2099 | n/a | |
---|
2100 | n/a | def _unlock_file(f): |
---|
2101 | n/a | """Unlock file f using lockf and dot locking.""" |
---|
2102 | n/a | if fcntl: |
---|
2103 | n/a | fcntl.lockf(f, fcntl.LOCK_UN) |
---|
2104 | n/a | if os.path.exists(f.name + '.lock'): |
---|
2105 | n/a | os.remove(f.name + '.lock') |
---|
2106 | n/a | |
---|
2107 | n/a | def _create_carefully(path): |
---|
2108 | n/a | """Create a file if it doesn't exist and open for reading and writing.""" |
---|
2109 | n/a | fd = os.open(path, os.O_CREAT | os.O_EXCL | os.O_RDWR, 0o666) |
---|
2110 | n/a | try: |
---|
2111 | n/a | return open(path, 'rb+') |
---|
2112 | n/a | finally: |
---|
2113 | n/a | os.close(fd) |
---|
2114 | n/a | |
---|
2115 | n/a | def _create_temporary(path): |
---|
2116 | n/a | """Create a temp file based on path and open for reading and writing.""" |
---|
2117 | n/a | return _create_carefully('%s.%s.%s.%s' % (path, int(time.time()), |
---|
2118 | n/a | socket.gethostname(), |
---|
2119 | n/a | os.getpid())) |
---|
2120 | n/a | |
---|
2121 | n/a | def _sync_flush(f): |
---|
2122 | n/a | """Ensure changes to file f are physically on disk.""" |
---|
2123 | n/a | f.flush() |
---|
2124 | n/a | if hasattr(os, 'fsync'): |
---|
2125 | n/a | os.fsync(f.fileno()) |
---|
2126 | n/a | |
---|
2127 | n/a | def _sync_close(f): |
---|
2128 | n/a | """Close file f, ensuring all changes are physically on disk.""" |
---|
2129 | n/a | _sync_flush(f) |
---|
2130 | n/a | f.close() |
---|
2131 | n/a | |
---|
2132 | n/a | |
---|
2133 | n/a | class Error(Exception): |
---|
2134 | n/a | """Raised for module-specific errors.""" |
---|
2135 | n/a | |
---|
2136 | n/a | class NoSuchMailboxError(Error): |
---|
2137 | n/a | """The specified mailbox does not exist and won't be created.""" |
---|
2138 | n/a | |
---|
2139 | n/a | class NotEmptyError(Error): |
---|
2140 | n/a | """The specified mailbox is not empty and deletion was requested.""" |
---|
2141 | n/a | |
---|
2142 | n/a | class ExternalClashError(Error): |
---|
2143 | n/a | """Another process caused an action to fail.""" |
---|
2144 | n/a | |
---|
2145 | n/a | class FormatError(Error): |
---|
2146 | n/a | """A file appears to have an invalid format.""" |
---|