summaryrefslogtreecommitdiff
path: root/alot/db/message.py
blob: 19a0f043e8f9e26258496b065f4947e0f5ca4e77 (plain)
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
# Copyright (C) 2011-2012  Patrick Totzke <patricktotzke@gmail.com>
# This file is released under the GNU GPL, version 3 or a later revision.
# For further details see the COPYING file

import codecs
import email
import email.charset as charset
import email.policy
from functools import cached_property
import logging
import os
import subprocess

from datetime import datetime

from urwid.util import detected_encoding

from ..mail.attachment import Attachment
from .. import crypto
from .. import helper
from ..errors import GPGProblem
from ..settings.const import settings
from ..utils.mailcap import MailcapHandler

charset.add_charset('utf-8', charset.QP, charset.QP, 'utf-8')

_APP_PGP_SIG = 'application/pgp-signature'
_APP_PGP_ENC = 'application/pgp-encrypted'

def _render_part_external(payload, ctype, params, filename):
    """
    renders a non-multipart email part into displayable plaintext by piping its
    payload through an external script. The handler itself is determined by
    the mailcap entry for this part's ctype.
    """
    h = MailcapHandler(payload, ctype, params, filename, 'copiousoutput')
    if not h or h.needs_terminal:
        return

    def decode(buf):
        return buf.decode(detected_encoding, errors = 'backslashreplace')

    with h:
        logging.debug('Rendering part %s: %s', ctype, h.cmd)

        try:
            result = subprocess.run(h.cmd, shell = True, check = True,
                                    capture_output = True, input = h.stdin,
                                    stdin = None if h.stdin else subprocess.DEVNULL)
        except subprocess.CalledProcessError as e:
            logging.error('Calling mailcap handler "%s" failed with code %d: %s',
                          h.cmd, e.returncode, decode(e.stderr))
            return None

        return decode(result.stdout)

class _MessageHeaders:
    _msg = None

    def __init__(self, msg):
        self._msg = msg

    def __contains__(self, key):
        return key in self._msg

    # workaround for https://bugs.python.org/issue39100
    def _exc_is_bp39100(self, e):
        if (e.args and len(e.args) >= 1 and
            'Group' in e.args[0] and 'local_part' in e.args[0]):
            logging.error('Working around Python bug 39100')
            return True
        return False

    def _get_all_wrapper(self, key, failobj = None):
        try:
            return self._msg.get_all(key, failobj)
        except AttributeError as e:
            if self._exc_is_bp39100(e):
                return []
            raise

    def __getitem__(self, key):
        if not key in self._msg:
            raise KeyError(key)
        return self._get_all_wrapper(key)

    def get(self, key):
        """
        Get the value of the first header with the name equal to key,
        None if the header is not present.
        """
        try:
            return self._msg.get(key)
        except AttributeError as e:
            if self._exc_is_bp39100(e):
                return []
            raise

    def get_all(self, key):
        """
        Get the list of all values of the header with the name equal to key,
        empty list of the header is not present.
        """
        return self._get_all_wrapper(key, [])

    def keys(self):
        try:
            return self._msg.keys()
        except AttributeError as e:
            if self._exc_is_bp39100(e):
                return []
            raise

    def items(self):
        try:
            return self._msg.items()
        except AttributeError as e:
            if self._exc_is_bp39100(e):
                return []
            raise

class _MimeTree:
    _part = None

    # content-type: (maintype, subtype)
    # may differ from that indicated in _part,
    # e.g. when it reports octet-stream, but we guess
    # something more specific
    _ctype_val = None

    is_signed     = False
    is_encrypted  = False
    sig_valid     = None
    sig_trusted   = None
    signer_id     = None
    crypt_error   = None

    is_alternative = False

    headers    = None
    children   = None
    attachment = None

    def __init__(self, part, session_keys = None):
        self._part = part
        self.headers = _MessageHeaders(self._part)

        if part.is_multipart():
            st = part.get_content_subtype()

            # handle signed/encrypted messages
            if st == 'signed' and part.get_param('protocol') == _APP_PGP_SIG:
                return self._handle_signed_pgp(session_keys)
            elif st == 'encrypted':
                return self._handle_encrypted(session_keys)

            # for all other cases, we just put all the sub-parts into children
            # multipart/alternative are flagged as such
            # everything else is treated as multipart/mixed
            if st == 'alternative':
                self.is_alternative = True

            children = []
            for part in part.iter_parts():
                children.append(_MimeTree(part, session_keys))
            self.children = children
        else:
            if self.content_maintype == 'text':
                self._fixup_charset()

            cd = part.get_content_disposition()
            fn = part.get_filename()
            if cd == 'attachment' or fn is not None:
                data   = part.get_content()
                params = part.get_params()

                # make sure data is always a byte-sequence
                if isinstance(data, str):
                    data = data.encode('utf-8', errors = 'replace')
                    params = [p for p in params if p[0] != 'charset'] + \
                             [('charset', 'utf-8')]

                self.attachment = Attachment(data, self.content_type,
                                             fn, params)

    def __str__(self):
        return 'MimePart(%s)' % self.content_type

    def _fixup_charset(self):
        """
        If a text MIME part declares an invalid or no charset, replace it
        with UTF-8.
        """
        charset = self._part.get_param('charset')
        if charset is not None:
            try:
                return codecs.lookup(charset)
            except LookupError:
                pass

        self._part.set_param('charset', 'utf-8')

    def render_str(self, alt_preference = None):
        if self.children is not None:
            if self.is_alternative and len(self.children) > 0:
                child = None
                for ch in self.children:
                    if ch.content_type == alt_preference:
                        child = ch
                        break
                if child is None:
                    child = self.children[0]
                return child.render_str(alt_preference)

            parts = []
            for ch in self.children:
                part = ch.render_str(alt_preference)
                if part:
                    parts.append(part)
            return '\n'.join(parts)

        content = self._part.get_content()

        # try procesing content with an external program
        rendered = _render_part_external(content, self.content_type,
                                         self._part.get_params(),
                                         self.filename)
        if rendered:
            return rendered

        # return text parts without a handler as-is
        if self.content_maintype == 'text':
            if not isinstance(content, str):
                content = content.decode('utf-8', errors = 'backslashreplace')
            return content

        return None

    @property
    def raw_data(self):
        return self._part.as_bytes()

    @property
    def _ctype(self):
        if self._ctype_val is None:
            ctype = self._part.get_content_type()

            # replace underspecified mime description by a better guess
            if ctype in ['octet/stream', 'application/octet-stream',
                         'application/octetstream']:
                ctype = helper.guess_mimetype(self._part.get_content())
                logging.debug('Overriding octet-stream content type to %s', ctype)

            maintype, _, subtype = ctype.partition('/')
            self._ctype_val = (maintype, subtype)

        return self._ctype_val

    @property
    def content_type(self):
        return '/'.join(self._ctype)
    @property
    def content_maintype(self):
        return self._ctype[0]
    @property
    def content_subtype(self):
        return self._ctype[1]

    @property
    def filename(self):
        return self._part.get_filename()

    def _handle_signed_pgp(self, session_keys):
        """
        Handle PGP-signed data.

        RFC 3156 is quite strict:
        * exactly two messages
        * the second is of type 'application/pgp-signature'
        * the second contains the detached signature
        """
        self.is_signed = True
        payload = list(self._part.iter_parts())

        if len(payload) != 2:
            self.crypt_error = 'expected exactly two messages, got %d' % len(payload)
            return

        self.children = [_MimeTree(payload[0], session_keys)]

        ct = payload[1].get_content_type()
        if ct != _APP_PGP_SIG:
            self.crypt_error = 'expected Content-Type: {0}, got: {1}'.format(
                _APP_PGP_SIG, ct)
            return

        # TODO: RFC 3156 says the alg has to be lower case, but I've seen a message
        # with 'PGP-'. maybe we should be more permissive here, or maybe not, this
        # is crypto stuff...
        micalg = self._part.get_param('micalg', '')
        if not micalg.startswith('pgp-'):
            self.crypt_error = 'expected micalg=pgp-..., got: {0}'.format(micalg)
            return

        part_data = payload[0].as_bytes()
        sig_data  = payload[1].get_content()

        # verify the signature
        sig     = None
        gpg_err = None
        try:
            sigs = crypto.verify_detached(part_data, sig_data)
            self.sig_valid = True
            if len(sigs) == 1:
                sig = sigs[0]
            else:
                gpg_err = 'exactly one signature expected'
        except GPGProblem as e:
            self.sig_valid = False
            gpg_err = str(e)

        # get the signer
        if sig is not None:
            self.signer_id, self.sig_trusted = self._sig_check_key(sig.fpr)

        if gpg_err:
            self.crypt_error = gpg_err

    def _sig_check_key(self, fingerprint):
        try:
            key = crypto.get_key(fingerprint)
            for uid in key.uids:
                if crypto.check_uid_validity(key, uid.email):
                    return uid.uid, True

            # No trusted uid found
            sig_from = key.uids[0].uid
        except GPGProblem:
            sig_from = fingerprint

        return sig_from, False

    def _handle_encrypted(self, session_keys):
        """
        Handle encrypted messages.

        RFC 3156 is quite strict:
        * exactly two parts
        * the first is of type 'application/pgp-encrypted'
        * the first contains 'Version: 1'
        * the second is of type 'application/octet-stream'
        * the second contains the encrypted and possibly signed data
        """
        self.is_encrypted = True
        payload = list(self._part.iter_parts())

        if len(payload) != 2:
            self.crypt_error = 'expected exactly two messages, got %d' % len(payload)
            return

        part_control = payload[0]
        part_body    = payload[1]

        ct = part_control.get_content_type()
        if ct != _APP_PGP_ENC:
            self.crypt_error = 'expected Content-Type: %s, got: %s' % (_APP_PGP_ENC, ct)
            return

        want = 'application/octet-stream'
        ct = part_body.get_content_type()
        if ct != want:
            self.crypt_error = 'expected Content-Type: %s, got: %s' % (want, ct)
            return

        payload = part_body.get_content()
        try:
            sigs, d = crypto.decrypt_verify(payload, session_keys)
        except GPGProblem as e:
            # signature verification failures end up here too if the combined
            # method is used, currently this prevents the interpretation of the
            # recovered plain text mail. maybe that's a feature.
            self.crypt_error = 'Failed to decrypt message: %s' % str(e)
            sigs = None
            d    = None

        if d:
            child = email.message_from_bytes(d, policy = email.policy.SMTP)
            self.children = [_MimeTree(child, session_keys)]

        if sigs:
            self.is_signed = True
            self.sig_valid = True
            if len(sigs) == 1:
                self.signer_id, self.sig_trusted = self._sig_check_key(sigs[0].fpr)

class Message:
    """
    a persistent notmuch message object.
    It it uses a :class:`~alot.db.DBManager` for cached manipulation
    and lazy lookups.
    """

    """the :class:`~alot.db.Thread` this Message belongs to"""
    thread = None

    """value of the Date header value as :class:`~datetime.datetime`"""
    date = None

    """value of the Message-Id header (str)"""
    id = None

    """Paths to all files corresponding to this message"""
    filenames = None

    """this message's depth in the thread tree"""
    depth = None

    """A list of replies to this message"""
    replies = None

    """
    This message parent in the list (i.e. the message this message is a reply
    to). None when this message is top-level.
    """
    parent = None

    _session_keys = None

    _nm_from = None
    "From header stored in the database"

    def __init__(self, dbman, thread, msg, depth):
        """
        :param dbman: db manager that is used for further lookups
        :type dbman: alot.db.DBManager
        :param thread: this messages thread
        :type thread: :class:`~alot.db.Thread`
        :param msg: the wrapped message
        :type msg: notmuch.database.Message
        :param depth: depth of this message in the thread tree (0 for toplevel
                      messages, 1 for their replies etc.)
        :type depth int
        """
        self._dbman = dbman
        self.id = msg.messageid
        self.thread = thread
        self.depth   = depth
        try:
            self.date = datetime.fromtimestamp(msg.date)
        except ValueError:
            self.date = None

        self.filenames = list(msg.filenamesb())
        if len(self.filenames) == 0:
            raise ValueError('No filenames for a message returned')

        session_keys = []
        for name, value in msg.properties.getall('session-key', exact = True):
            session_keys.append(value)
        self._session_keys = session_keys

        self._tags    = set(msg.tags)
        try:
            self._nm_from = msg.header('from')
        except LookupError:
            self._nm_from = None

    def __str__(self):
        """prettyprint the message"""
        aname, aaddress = self.get_author()
        if not aname:
            aname = aaddress
        return "%s (%s)" % (aname, self.get_datestring())

    def __hash__(self):
        """needed for sets of Messages"""
        return hash(self.id)

    def __eq__(self, other):
        if isinstance(other, type(self)):
            return self.id == other.id
        return NotImplemented

    @cached_property
    def body(self):
        """
        A MimeTree object containing the body of the message.
        """
        return _MimeTree(self._email, self._session_keys)

    @property
    def headers(self):
        """
        The object providing access to the email's headers.
        """
        return self.body.headers

    @property
    def filename(self):
        return self.filenames[0]

    @cached_property
    def _email(self):
        try:
            with open(self.filename, 'rb') as f:
                mail = email.message_from_bytes(f.read(), policy = email.policy.SMTP)
        except IOError:
            warning = b"Subject: Caution!\n"\
                      b"Message file is no longer accessible:\n%s" % self.filename
            mail = email.message_from_bytes(warning, policy = email.policy.SMTP)

        return mail

    def as_bytes(self):
        return self._email.as_bytes()

    def get_tags(self):
        """returns tags attached to this message as set of strings"""
        return self._tags

    def get_datestring(self):
        """
        returns reformated datestring for this message.

        It uses :meth:`SettingsManager.represent_datetime` to represent
        this messages `Date` header

        :rtype: str
        """
        if self.date is None:
            return None
        return settings.represent_datetime(self.date)

    def get_author(self):
        """
        returns realname and address of this messages author

        :rtype: (str,str)
        """
        sender = self._nm_from
        if sender is None:
            sender = self.headers.get('Sender')

        if not sender:
            if 'draft' in self._tags:
                acc = settings.get_accounts()[0]
                sender = '"{}" <{}>'.format(acc.realname, str(acc.address))
            else:
                sender = '"Unknown" <>'

        return email.utils.parseaddr(sender)

    def tags_add(self, tags):
        """
        Asynchronously add tags to message

        :param tags: a set of tags to be added
        :type tags: set of str
        """
        def myafterwards(fut):
            self._tags = self._tags.union(tags)

        fut = self._dbman.tags_add('id:' + self.id, tags)
        fut.add_done_callback(myafterwards)
        return fut

    def tags_set(self, tags):
        """
        Asynchronously set tags for a message

        :param tags: a set of new tags to replace the existing set
        :type tags: set of str
        """
        def myafterwards(fut):
            self._tags = set(tags)

        fut = self._dbman.tags_set('id:' + self.id, tags)
        fut.add_done_callback(myafterwards)
        return fut

    def tags_remove(self, tags):
        """Asynchronously remove tags from message

        :param tags: a set of tags to be removed
        :type tags: set of str
        """
        def myafterwards(fut):
            self._tags = self._tags.difference(tags)

        fut = self._dbman.tags_remove('id:' + self.id, tags)
        fut.add_done_callback(myafterwards)
        return fut

    def iter_attachments(self):
        """
        Iterate over all the attachments in this message.
        """
        def tree_walk(mime_tree):
            if mime_tree.attachment:
                yield mime_tree.attachment

            if mime_tree.children is not None:
                for ch in mime_tree.children:
                    yield from tree_walk(ch)

        yield from tree_walk(self.body)

    def get_body_text(self):
        """
        Returns a string view of a Message.

        This consults :ref:`prefer_plaintext <prefer-plaintext>`
        to determine if a "text/plain" alternative is preferred over a "text/html"
        part.

        :returns: The combined text of any parts to be used
        :rtype: str
        """
        if settings.get('prefer_plaintext'):
            alt_preference = 'text/plain'
        else:
            alt_preference = 'text/html'

        return self.body.render_str(alt_preference)

    def matches(self, querystring):
        """tests if this messages is in the resultset for `querystring`"""
        searchfor = '( {} ) AND id:{}'.format(querystring, self.id)
        return self._dbman.count_messages(searchfor) > 0

    def parents(self):
        """
        A generator iterating over this message's parents up to the topmost
        level.
        """
        m = self.parent
        while m:
            yield m
            m = m.parent