summaryrefslogtreecommitdiff
path: root/alot/mail/envelope.py
blob: 55b3f7ff58c3b2a1e74fcea91653a14414330608 (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
# 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 glob
import logging
import os
import re
import email
import email.policy
from email.message import MIMEPart
import email.charset as charset
from urllib.parse import unquote

import gpg

from .attachment import Attachment
from .. import __version__
from .. import helper
from .. import crypto
from ..settings.const import settings
from ..errors import GPGProblem, GPGCode

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

class Envelope:
    """
    a message that is not yet sent and still editable.

    It holds references to unencoded! body text and mail headers among other
    things.  Envelope implements the python container API for easy access of
    header values.  So `e['To']`, `e['To'] = 'foo@bar.baz'` and
    'e.get_all('To')' would work for an envelope `e`..
    """

    headers = None
    """
    dict containing the mail headers (a list of strings for each header key)
    """
    body = None
    """mail body as unicode string"""
    tmpfile = None
    """template text for initial content"""
    attachments = None
    """list of :class:`Attachments <alot.mail.attachment.Attachment>`"""
    tags = None
    """tags to add after successful sendout"""
    account = None
    """account to send from"""

    def __init__(
            self, template=None, bodytext=None, headers=None, attachments=None,
            sign=False, sign_key=None, encrypt=False, tags=None, replied=None,
            passed=None, account=None):
        """
        :param template: if not None, the envelope will be initialised by
                         :meth:`parsing <parse_template>` this string before
                         setting any other values given to this constructor.
        :type template: str
        :param bodytext: text used as body part
        :type bodytext: str
        :param headers: unencoded header values
        :type headers: dict (str -> [unicode])
        :param attachments: file attachments to include
        :type attachments: list of :class:`~alot.mail.attachment.Attachment`
        :param tags: tags to add after successful sendout and saving this msg
        :type tags: set of str
        :param replied: message being replied to
        :type replied: :class:`~alot.db.message.Message`
        :param passed: message being passed on
        :type replied: :class:`~alot.db.message.Message`
        :param account: account to send from
        :type account: :class:`Account`
        """
        logging.debug('TEMPLATE: %s', template)
        if template:
            self.parse_template(template)
            logging.debug('PARSED TEMPLATE: %s', template)
            logging.debug('BODY: %s', self.body)
        self.body = bodytext or ''
        # TODO: if this was as collections.defaultdict a number of methods
        # could be simplified.
        self.headers = headers or {}
        self.attachments = list(attachments) if attachments is not None else []
        self.sign = sign
        self.sign_key = sign_key
        self.encrypt = encrypt
        self.encrypt_keys = {}
        self.tags = tags or frozenset()  # tags to add after successful sendout
        self.replied = replied  # message being replied to
        self.passed = passed  # message being passed on
        self.sent_time = None
        self.modified_since_sent = False
        self.sending = False  # semaphore to avoid accidental double sendout
        self.account = account

    def __str__(self):
        return "Envelope (%s)\n%s" % (self.headers, self.body)

    def __setitem__(self, name, val):
        """setter for header values. This allows adding header like so:
        envelope['Subject'] = 'sm\xf8rebr\xf8d'
        """
        if name not in self.headers:
            self.headers[name] = []
        self.headers[name].append(val)

        if self.sent_time:
            self.modified_since_sent = True

    def __getitem__(self, name):
        """getter for header values.
        :raises: KeyError if undefined
        """
        return self.headers[name][0]

    def __delitem__(self, name):
        del self.headers[name]

        if self.sent_time:
            self.modified_since_sent = True

    def __contains__(self, name):
        return name in self.headers

    def get(self, key, fallback=None):
        """secure getter for header values that allows specifying a `fallback`
        return string (defaults to None). This returns the first matching value
        and doesn't raise KeyErrors"""
        if key in self.headers:
            value = self.headers[key][0]
        else:
            value = fallback
        return value

    def get_all(self, key, fallback=None):
        """returns all header values for given key"""
        if key in self.headers:
            value = self.headers[key]
        else:
            value = fallback or []
        return value

    def add(self, key, value):
        """add header value"""
        if key not in self.headers:
            self.headers[key] = []
        self.headers[key].append(value)

        if self.sent_time:
            self.modified_since_sent = True

    def attach_file(self, path, filename = None):
        """
        Attach a file at a given path.

        :param path path to the file to attach
        :type path str
        :param filename filename to write into attachment properties
        :type filename str or None
        """
        with open(path, 'rb') as f:
            data = f.read()

        ctype = helper.guess_mimetype(data)

        params = []

        # accept only valid utf-8 as text attachments
        if ctype.partition('/')[0] == 'text':
            try:
                data.decode('utf-8')
            except UnicodeDecodeError as e:
                raise ValueError('Attachment is not valid UTF-8') from e

            params.append(('charset', 'utf-8'))

        # Set the filename parameter
        if not filename:
            filename = os.path.basename(path)

        attachment = Attachment(data, ctype, filename, tuple(params))
        self.attach(attachment)

    def attach(self, attachment):
        """
        attach data

        :param attachment: data to attach
        :type attachment: :class:`~alot.mail.attachment.Attachment`
        """
        self.attachments.append(attachment)
        if self.sent_time:
            self.modified_since_sent = True

    def construct_mail(self):
        """
        compiles the information contained in this envelope into a
        :class:`email.message.EmailMessage`.
        """
        # make suire everything is 7-bit clean to avoid
        # compatibility problems
        # TODO: consider SMTPUTF8 support?
        policy = email.policy.SMTP.clone(cte_type = '7bit')

        # we actually use MIMEPart instead of EmailMessage, to
        # avoid the subparts getting spurious MIME-Version everywhere
        mail = MIMEPart(policy = policy)
        mail.set_content(self.body, subtype = 'plain', charset = 'utf-8')

        # add attachments
        for a in self.attachments:
            mail.add_attachment(a.data, filename = a.filename,
                                maintype = a.content_maintype,
                                subtype  = a.content_subtype,
                                params   = { k : v for (k, v) in a.params })

        if self.sign:
            to_sign = mail
            plaintext = to_sign.as_bytes()
            logging.debug('signing plaintext: %s', plaintext)

            try:
                signatures, signature_blob = crypto.detached_signature_for(
                    plaintext, [self.sign_key])
                if len(signatures) != 1:
                    raise GPGProblem("Could not sign message (GPGME "
                                     "did not return a signature)",
                                     code=GPGCode.KEY_CANNOT_SIGN)
            except gpg.errors.GPGMEError as e:
                if e.getcode() == gpg.errors.BAD_PASSPHRASE:
                    # If GPG_AGENT_INFO is unset or empty, the user just does
                    # not have gpg-agent running (properly).
                    if os.environ.get('GPG_AGENT_INFO', '').strip() == '':
                        msg = "Got invalid passphrase and GPG_AGENT_INFO\
                                not set. Please set up gpg-agent."
                        raise GPGProblem(msg, code=GPGCode.BAD_PASSPHRASE)
                    else:
                        raise GPGProblem("Bad passphrase. Is gpg-agent "
                                         "running?",
                                         code=GPGCode.BAD_PASSPHRASE)
                raise GPGProblem(str(e), code=GPGCode.KEY_CANNOT_SIGN)

            signature_mime = MIMEPart(policy = to_sign.policy)
            signature_mime.set_content(signature_blob, maintype = 'application',
                                       subtype = 'pgp-signature')
            signature_mime.set_param('name', 'signature.asc')
            signature_mime['Content-Description'] = 'signature'

            # FIXME: this uses private methods, because
            # python's "new" EmailMessage API does not
            # allow arbitrary multipart constructs
            mail = MIMEPart(policy = to_sign.policy)
            mail._make_multipart('signed', (), None)
            mail.set_param('protocol', 'application/pgp-signature')
            mail.set_param('micalg', crypto.RFC3156_micalg_from_algo(signatures[0].hash_algo))
            mail.attach(to_sign)
            mail.attach(signature_mime)

        if self.encrypt:
            to_encrypt = mail
            plaintext  = to_encrypt.as_bytes()
            logging.debug('encrypting plaintext: %s', plaintext)

            try:
                encrypted_blob = crypto.encrypt(
                    plaintext, list(self.encrypt_keys.values()))
            except gpg.errors.GPGMEError as e:
                raise GPGProblem(str(e), code=GPGCode.KEY_CANNOT_ENCRYPT)

            version_str = b'Version: 1'
            encryption_mime = MIMEPart(policy = to_encrypt.policy)
            encryption_mime.set_content(version_str, maintype = 'application',
                                        subtype = 'pgp-encrypted')

            encrypted_mime = MIMEPart(policy = to_encrypt.policy)
            encrypted_mime.set_content(encrypted_blob, maintype = 'application',
                                       subtype = 'octet-stream')

            mail = MIMEPart(policy = to_encrypt.policy)
            mail._make_multipart('encrypted', (), None)
            mail.set_param('protocol', 'application/pgp-encrypted')
            mail.attach(encryption_mime)
            mail.attach(encrypted_mime)

        headers = self.headers.copy()

        # add Date header
        if 'Date' not in headers:
            headers['Date'] = [email.utils.formatdate(localtime=True)]

        # add Message-ID
        if 'Message-ID' not in headers:
            domain = settings.get('message_id_domain')
            headers['Message-ID'] = [email.utils.make_msgid(domain=domain)]

        if 'User-Agent' in headers:
            uastring_format = headers['User-Agent'][0]
        else:
            uastring_format = settings.get('user_agent').strip()
        uastring = uastring_format.format(version=__version__)
        if uastring:
            headers['User-Agent'] = [uastring]

        # copy headers from envelope to mail
        for k, vlist in headers.items():
            for v in vlist:
                mail.add_header(k, v)

        # as we are using MIMEPart instead of EmailMessage, set the
        # MIME version manually
        del mail['MIME-Version']
        mail['MIME-Version'] = '1.0'

        return mail

    def parse_template(self, raw, reset=False, only_body=False):
        """parses a template or user edited string to fills this envelope.

        :param raw: the string to parse.
        :type raw: str
        :param reset: remove previous envelope content
        :type reset: bool
        :param only_body: do not parse headers
        :type only_body: bool
        """
        logging.debug('GoT: """\n%s\n"""', raw)

        if self.sent_time:
            self.modified_since_sent = True

        if reset:
            self.headers = {}

        headerEndPos = 0
        if not only_body:
            # go through multiline, utf-8 encoded headers
            # locally, lines are separated by a simple LF, not CRLF
            # we decode the edited text ourselves here as
            # email.message_from_file can't deal with raw utf8 header values
            headerRe = re.compile(r'^(?P<k>.+?):(?P<v>(.|\n[ \t\r\f\v])+)$',
                                  re.MULTILINE)
            for header in headerRe.finditer(raw):
                if header.start() > headerEndPos + 1:
                    break  # switched to body

                key = header.group('k')
                # simple unfolding as decribed in
                # https://tools.ietf.org/html/rfc2822#section-2.2.3
                unfoldedValue = header.group('v').replace('\n', '')
                self.add(key, unfoldedValue.strip())
                headerEndPos = header.end()

            # interpret 'Attach' pseudo header
            if 'Attach' in self:
                to_attach = []
                for line in self.get_all('Attach'):
                    gpath = os.path.expanduser(line.strip())
                    to_attach += [g for g in glob.glob(gpath)
                                  if os.path.isfile(g)]
                logging.debug('Attaching: %s', to_attach)
                for path in to_attach:
                    self.attach_file(path)
                del self['Attach']

        self.body = raw[headerEndPos:].strip()

    _MAILTO_PREFIX       = 'mailto:'
    _MAILTO_SAFE_HEADERS = ('Subject', 'Cc', 'Keywords')

    @classmethod
    def from_mailto(cls, mailto):
        if not mailto.startswith(cls._MAILTO_PREFIX):
            raise ValueError('Invalid mailto string: %s' % mailto)
        mailto = mailto[len(cls._MAILTO_PREFIX):]

        headers = {}
        body    = ''

        to, _, hfields = mailto.partition('?')
        to = unquote(to)
        if to:
            headers['To'] = [to]

        for hfield in hfields.split('&'):
            key, _, value = hfield.partition('=')

            key   = key.capitalize()
            value = unquote(value)
            if not value:
                continue

            if key == 'Body':
                body = value
            elif key in cls._MAILTO_SAFE_HEADERS:
                headers[key] = [value]

        return cls(headers = headers, bodytext = body)