1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
|
# -*- coding: utf-8 -*-
# messages.py
# Copyright (C) 2013 LEAP
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
"""
LeapMessage and MessageCollection.
"""
import copy
import logging
import StringIO
from collections import namedtuple
from twisted.mail import imap4
from twisted.python import log
from u1db import errors as u1db_errors
from zope.interface import implements
from zope.proxy import sameProxiedObjects
from leap.common.check import leap_assert, leap_assert_type
from leap.common.mail import get_email_charset
from leap.mail.decorators import deferred
from leap.mail.imap.account import SoledadBackedAccount
from leap.mail.imap.index import IndexedDB
from leap.mail.imap.fields import fields, WithMsgFields
from leap.mail.imap.parser import MailParser, MBoxParser
from leap.mail.messageflow import IMessageConsumer, MessageProducer
logger = logging.getLogger(__name__)
class LeapMessage(fields, MailParser, MBoxParser):
implements(imap4.IMessage)
def __init__(self, soledad, uid, mbox):
"""
Initializes a LeapMessage.
:param soledad: a Soledad instance
:type soledad: Soledad
:param uid: the UID for the message.
:type uid: int or basestring
:param mbox: the mbox this message belongs to
:type mbox: basestring
"""
MailParser.__init__(self)
self._soledad = soledad
self._uid = int(uid)
self._mbox = self._parse_mailbox_name(mbox)
self._chash = None
self.__cdoc = None
@property
def _fdoc(self):
"""
An accessor to the flags document.
"""
return self._get_flags_doc()
@property
def _cdoc(self):
"""
An accessor to the content document.
"""
if not self.__cdoc:
self.__cdoc = self._get_content_doc()
return self.__cdoc
@property
def _chash(self):
"""
An accessor to the content hash for this message.
"""
if not self._fdoc:
return None
return self._fdoc.content.get(fields.CONTENT_HASH_KEY, None)
# IMessage implementation
def getUID(self):
"""
Retrieve the unique identifier associated with this message
:return: uid for this message
:rtype: int
"""
return self._uid
def getFlags(self):
"""
Retrieve the flags associated with this message
:return: The flags, represented as strings
:rtype: tuple
"""
if self._uid is None:
return []
flags = []
flag_doc = self._fdoc
if flag_doc:
flags = flag_doc.content.get(self.FLAGS_KEY, None)
if flags:
flags = map(str, flags)
return tuple(flags)
# setFlags, addFlags, removeFlags are not in the interface spec
# but we use them with store command.
def setFlags(self, flags):
"""
Sets the flags for this message
Returns a SoledadDocument that needs to be updated by the caller.
:param flags: the flags to update in the message.
:type flags: tuple of str
:return: a SoledadDocument instance
:rtype: SoledadDocument
"""
leap_assert(isinstance(flags, tuple), "flags need to be a tuple")
log.msg('setting flags: %s' % (self._uid))
doc = self._fdoc
doc.content[self.FLAGS_KEY] = flags
doc.content[self.SEEN_KEY] = self.SEEN_FLAG in flags
doc.content[self.RECENT_KEY] = self.RECENT_FLAG in flags
self._soledad.put_doc(doc)
def addFlags(self, flags):
"""
Adds flags to this message.
Returns a SoledadDocument that needs to be updated by the caller.
:param flags: the flags to add to the message.
:type flags: tuple of str
:return: a SoledadDocument instance
:rtype: SoledadDocument
"""
leap_assert(isinstance(flags, tuple), "flags need to be a tuple")
oldflags = self.getFlags()
self.setFlags(tuple(set(flags + oldflags)))
def removeFlags(self, flags):
"""
Remove flags from this message.
Returns a SoledadDocument that needs to be updated by the caller.
:param flags: the flags to be removed from the message.
:type flags: tuple of str
:return: a SoledadDocument instance
:rtype: SoledadDocument
"""
leap_assert(isinstance(flags, tuple), "flags need to be a tuple")
oldflags = self.getFlags()
self.setFlags(tuple(set(oldflags) - set(flags)))
def getInternalDate(self):
"""
Retrieve the date internally associated with this message
:rtype: C{str}
:return: An RFC822-formatted date string.
"""
return str(self._cdoc.content.get(self.DATE_KEY, ''))
#
# IMessagePart
#
# XXX we should implement this interface too for the subparts
# so we allow nested parts...
def getBodyFile(self):
"""
Retrieve a file object containing only the body of this message.
:return: file-like object opened for reading
:rtype: StringIO
"""
fd = StringIO.StringIO()
cdoc = self._cdoc
content = cdoc.content.get(self.RAW_KEY, '')
charset = get_email_charset(
unicode(cdoc.content.get(self.RAW_KEY, '')))
try:
content = content.encode(charset)
except (UnicodeEncodeError, UnicodeDecodeError) as e:
logger.error("Unicode error {0}".format(e))
content = content.encode(charset, 'replace')
raw = self._get_raw_msg()
msg = self._get_parsed_msg(raw)
body = msg.get_payload()
fd.write(body)
# XXX SHOULD use a separate BODY FIELD ...
fd.seek(0)
return fd
def getSize(self):
"""
Return the total size, in octets, of this message.
:return: size of the message, in octets
:rtype: int
"""
size = self._cdoc.content.get(self.SIZE_KEY, False)
if not size:
# XXX fallback, should remove when all migrated.
size = self.getBodyFile().len
return size
def _get_headers(self):
"""
Return the headers dict stored in this message document.
"""
# XXX get from the headers doc
return self._cdoc.content.get(self.HEADERS_KEY, {})
def getHeaders(self, negate, *names):
"""
Retrieve a group of message headers.
:param names: The names of the headers to retrieve or omit.
:type names: tuple of str
:param negate: If True, indicates that the headers listed in names
should be omitted from the return value, rather
than included.
:type negate: bool
:return: A mapping of header field names to header field values
:rtype: dict
"""
headers = self._get_headers()
names = map(lambda s: s.upper(), names)
if negate:
cond = lambda key: key.upper() not in names
else:
cond = lambda key: key.upper() in names
# unpack and filter original dict by negate-condition
filter_by_cond = [
map(str, (key, val)) for
key, val in headers.items()
if cond(key)]
return dict(filter_by_cond)
def isMultipart(self):
"""
Return True if this message is multipart.
"""
if self._cdoc:
retval = self._fdoc.content.get(self.MULTIPART_KEY, False)
return retval
def getSubPart(self, part):
"""
Retrieve a MIME submessage
:type part: C{int}
:param part: The number of the part to retrieve, indexed from 0.
:raise IndexError: Raised if the specified part does not exist.
:raise TypeError: Raised if this message is not multipart.
:rtype: Any object implementing C{IMessagePart}.
:return: The specified sub-part.
"""
if not self.isMultipart():
raise TypeError
msg = self._get_parsed_msg()
# XXX should wrap IMessagePart
return msg.get_payload()[part]
#
# accessors
#
def _get_flags_doc(self):
"""
Return the document that keeps the flags for this
message.
"""
flag_docs = self._soledad.get_from_index(
SoledadBackedAccount.TYPE_MBOX_UID_IDX,
fields.TYPE_FLAGS_VAL, self._mbox, str(self._uid))
flag_doc = flag_docs[0] if flag_docs else None
return flag_doc
def _get_content_doc(self):
"""
Return the document that keeps the flags for this
message.
"""
cont_docs = self._soledad.get_from_index(
SoledadBackedAccount.TYPE_HASH_IDX,
fields.TYPE_MESSAGE_VAL, self._content_hash, str(self._uid))
cont_doc = cont_docs[0] if cont_docs else None
return cont_doc
def _get_raw_msg(self):
"""
Return the raw msg.
:rtype: basestring
"""
return self._cdoc.content.get(self.RAW_KEY, '')
def __getitem__(self, key):
"""
Return the content of the message document.
:param key: The key
:type key: str
:return: The content value indexed by C{key} or None
:rtype: str
"""
return self._cdoc.content.get(key, None)
def does_exist(self):
"""
Return True if there is actually a message for this
UID and mbox.
"""
return bool(self._fdoc)
SoledadWriterPayload = namedtuple(
'SoledadWriterPayload', ['mode', 'payload'])
SoledadWriterPayload.CREATE = 1
SoledadWriterPayload.PUT = 2
class SoledadDocWriter(object):
"""
This writer will create docs serially in the local soledad database.
"""
implements(IMessageConsumer)
def __init__(self, soledad):
"""
Initialize the writer.
:param soledad: the soledad instance
:type soledad: Soledad
"""
self._soledad = soledad
def consume(self, queue):
"""
Creates a new document in soledad db.
:param queue: queue to get item from, with content of the document
to be inserted.
:type queue: Queue
"""
empty = queue.empty()
while not empty:
item = queue.get()
if item.mode == SoledadWriterPayload.CREATE:
call = self._soledad.create_doc
elif item.mode == SoledadWriterPayload.PUT:
call = self._soledad.put_doc
# should handle errors
try:
call(item.payload)
except u1db_errors.RevisionConflict as exc:
logger.error("Error: %r" % (exc,))
raise exc
empty = queue.empty()
class MessageCollection(WithMsgFields, IndexedDB, MailParser, MBoxParser):
"""
A collection of messages, surprisingly.
It is tied to a selected mailbox name that is passed to constructor.
Implements a filter query over the messages contained in a soledad
database.
"""
# XXX this should be able to produce a MessageSet methinks
EMPTY_MSG = {
fields.TYPE_KEY: fields.TYPE_MESSAGE_VAL,
fields.UID_KEY: 1,
fields.MBOX_KEY: fields.INBOX_VAL,
fields.SUBJECT_KEY: "",
fields.DATE_KEY: "",
fields.RAW_KEY: "",
# XXX should separate headers into another doc
fields.HEADERS_KEY: {},
}
EMPTY_FLAGS = {
fields.TYPE_KEY: fields.TYPE_FLAGS_VAL,
fields.UID_KEY: 1,
fields.MBOX_KEY: fields.INBOX_VAL,
fields.FLAGS_KEY: [],
fields.SEEN_KEY: False,
fields.RECENT_KEY: True,
fields.MULTIPART_KEY: False,
}
# get from SoledadBackedAccount the needed index-related constants
INDEXES = SoledadBackedAccount.INDEXES
TYPE_IDX = SoledadBackedAccount.TYPE_IDX
def __init__(self, mbox=None, soledad=None):
"""
Constructor for MessageCollection.
:param mbox: the name of the mailbox. It is the name
with which we filter the query over the
messages database
:type mbox: str
:param soledad: Soledad database
:type soledad: Soledad instance
"""
MailParser.__init__(self)
leap_assert(mbox, "Need a mailbox name to initialize")
leap_assert(mbox.strip() != "", "mbox cannot be blank space")
leap_assert(isinstance(mbox, (str, unicode)),
"mbox needs to be a string")
leap_assert(soledad, "Need a soledad instance to initialize")
# okay, all in order, keep going...
self.mbox = self._parse_mailbox_name(mbox)
self._soledad = soledad
self.initialize_db()
# I think of someone like nietzsche when reading this
# this will be the producer that will enqueue the content
# to be processed serially by the consumer (the writer). We just
# need to `put` the new material on its plate.
self.soledad_writer = MessageProducer(
SoledadDocWriter(soledad),
period=0.05)
def _get_empty_msg(self):
"""
Returns an empty message.
:return: a dict containing a default empty message
:rtype: dict
"""
return copy.deepcopy(self.EMPTY_MSG)
def _get_empty_flags_doc(self):
"""
Returns an empty doc for storing flags.
:return:
:rtype:
"""
return copy.deepcopy(self.EMPTY_FLAGS)
@deferred
def add_msg(self, raw, subject=None, flags=None, date=None, uid=1):
"""
Creates a new message document.
:param raw: the raw message
:type raw: str
:param subject: subject of the message.
:type subject: str
:param flags: flags
:type flags: list
:param date: the received date for the message
:type date: str
:param uid: the message uid for this mailbox
:type uid: int
"""
# TODO: split in smaller methods
logger.debug('adding message')
if flags is None:
flags = tuple()
leap_assert_type(flags, tuple)
content_doc = self._get_empty_msg()
flags_doc = self._get_empty_flags_doc()
content_doc[self.MBOX_KEY] = self.mbox
flags_doc[self.MBOX_KEY] = self.mbox
# ...should get a sanity check here.
content_doc[self.UID_KEY] = uid
flags_doc[self.UID_KEY] = uid
if flags:
flags_doc[self.FLAGS_KEY] = map(self._stringify, flags)
flags_doc[self.SEEN_KEY] = self.SEEN_FLAG in flags
msg = self._get_parsed_msg(raw)
headers = dict(msg)
logger.debug("adding. is multipart:%s" % msg.is_multipart())
flags_doc[self.MULTIPART_KEY] = msg.is_multipart()
# XXX get lower case for keys?
# XXX get headers doc
content_doc[self.HEADERS_KEY] = headers
# set subject based on message headers and eventually replace by
# subject given as param
if self.SUBJECT_FIELD in headers:
content_doc[self.SUBJECT_KEY] = headers[self.SUBJECT_FIELD]
if subject is not None:
content_doc[self.SUBJECT_KEY] = subject
# XXX could separate body into its own doc
# but should also separate multiparts
# that should be wrapped in MessagePart
content_doc[self.RAW_KEY] = self._stringify(raw)
content_doc[self.SIZE_KEY] = len(raw)
if not date and self.DATE_FIELD in headers:
content_doc[self.DATE_KEY] = headers[self.DATE_FIELD]
else:
content_doc[self.DATE_KEY] = date
logger.debug('enqueuing message for write')
ptuple = SoledadWriterPayload
self.soledad_writer.put(ptuple(
mode=ptuple.CREATE, payload=content_doc))
self.soledad_writer.put(ptuple(
mode=ptuple.CREATE, payload=flags_doc))
def remove(self, msg):
"""
Removes a message.
:param msg: a Leapmessage instance
:type msg: LeapMessage
"""
# XXX remove
#self._soledad.delete_doc(msg)
msg.remove()
# getters
def get_msg_by_uid(self, uid):
"""
Retrieves a LeapMessage by UID.
:param uid: the message uid to query by
:type uid: int
:return: A LeapMessage instance matching the query,
or None if not found.
:rtype: LeapMessage
"""
msg = LeapMessage(self._soledad, uid, self.mbox)
if not msg.does_exist():
return None
return msg
def get_all_docs(self, _type=fields.TYPE_FLAGS_VAL):
"""
Get all documents for the selected mailbox of the
passed type. By default, it returns the flag docs.
If you want acess to the content, use __iter__ instead
:return: a list of u1db documents
:rtype: list of SoledadDocument
"""
if _type not in fields.__dict__.values():
raise TypeError("Wrong type passed to get_all")
if sameProxiedObjects(self._soledad, None):
logger.warning('Tried to get messages but soledad is None!')
return []
all_docs = [doc for doc in self._soledad.get_from_index(
SoledadBackedAccount.TYPE_MBOX_IDX,
_type, self.mbox)]
# inneficient, but first let's grok it and then
# let's worry about efficiency.
# XXX FIXINDEX -- should implement order by in soledad
return sorted(all_docs, key=lambda item: item.content['uid'])
def all_msg_iter(self):
"""
Return an iterator trhough the UIDs of all messages, sorted in
ascending order.
"""
all_uids = (doc.content[self.UID_KEY] for doc in
self._soledad.get_from_index(
SoledadBackedAccount.TYPE_MBOX_IDX,
self.TYPE_FLAGS_VAL, self.mbox))
return (u for u in sorted(all_uids))
def count(self):
"""
Return the count of messages for this mailbox.
:rtype: int
"""
count = self._soledad.get_count_from_index(
SoledadBackedAccount.TYPE_MBOX_IDX,
fields.TYPE_FLAGS_VAL, self.mbox)
return count
# unseen messages
def unseen_iter(self):
"""
Get an iterator for the message UIDs with no `seen` flag
for this mailbox.
:return: iterator through unseen message doc UIDs
:rtype: iterable
"""
return (doc.content[self.UID_KEY] for doc in
self._soledad.get_from_index(
SoledadBackedAccount.TYPE_MBOX_SEEN_IDX,
self.TYPE_FLAGS_VAL, self.mbox, '0'))
def count_unseen(self):
"""
Count all messages with the `Unseen` flag.
:returns: count
:rtype: int
"""
count = self._soledad.get_count_from_index(
SoledadBackedAccount.TYPE_MBOX_SEEN_IDX,
self.TYPE_FLAGS_VAL, self.mbox, '0')
return count
def get_unseen(self):
"""
Get all messages with the `Unseen` flag
:returns: a list of LeapMessages
:rtype: list
"""
return [LeapMessage(self._soledad, docid, self.mbox)
for docid in self.unseen_iter()]
# recent messages
def recent_iter(self):
"""
Get an iterator for the message docs with `recent` flag.
:return: iterator through recent message docs
:rtype: iterable
"""
return (doc.content[self.UID_KEY] for doc in
self._soledad.get_from_index(
SoledadBackedAccount.TYPE_MBOX_RECT_IDX,
self.TYPE_FLAGS_VAL, self.mbox, '1'))
def get_recent(self):
"""
Get all messages with the `Recent` flag.
:returns: a list of LeapMessages
:rtype: list
"""
return [LeapMessage(self._soledad, docid, self.mbox)
for docid in self.recent_iter()]
def count_recent(self):
"""
Count all messages with the `Recent` flag.
:returns: count
:rtype: int
"""
count = self._soledad.get_count_from_index(
SoledadBackedAccount.TYPE_MBOX_RECT_IDX,
self.TYPE_FLAGS_VAL, self.mbox, '1')
return count
def __len__(self):
"""
Returns the number of messages on this mailbox.
:rtype: int
"""
return self.count()
def __iter__(self):
"""
Returns an iterator over all messages.
:returns: iterator of dicts with content for all messages.
:rtype: iterable
"""
return (LeapMessage(self._soledad, docuid, self.mbox)
for docuid in self.all_msg_iter())
def __repr__(self):
"""
Representation string for this object.
"""
return u"<MessageCollection: mbox '%s' (%s)>" % (
self.mbox, self.count())
# XXX should implement __eq__ also !!! --- use a hash
# of content for that, will be used for dedup.
|