# Copyright (C) 2011-2012 Patrick Totzke # 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 `""" 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 ` 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.+?):(?P(.|\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)