From 7fd9c2f09bb3425ad1af39470698a5622f6085a9 Mon Sep 17 00:00:00 2001 From: Ruben Pollan Date: Mon, 4 Jul 2016 20:20:11 +0200 Subject: Encrypt for the CC addresses too --- alot/commands/envelope.py | 24 +++--------------------- alot/commands/globals.py | 39 +++------------------------------------ alot/commands/utils.py | 42 +++++++++++++++++++++++++++++++++++++++++- 3 files changed, 47 insertions(+), 58 deletions(-) (limited to 'alot/commands') diff --git a/alot/commands/envelope.py b/alot/commands/envelope.py index 7ce553b6..d293a616 100644 --- a/alot/commands/envelope.py +++ b/alot/commands/envelope.py @@ -14,7 +14,7 @@ from twisted.internet.defer import inlineCallbacks from . import Command, registerCommand from . import globals -from .utils import get_keys +from .utils import set_encrypt from .. import buffers from .. import commands from .. import crypto @@ -563,27 +563,9 @@ class EncryptCommand(Command): encrypt = False elif self.action == 'toggleencrypt': encrypt = not envelope.encrypt - envelope.encrypt = encrypt if encrypt: - if not self.encrypt_keys: - for recipient in envelope.headers['To'][0].split(','): - if not recipient: - continue - match = re.search("<(.*@.*)>", recipient) - if match: - recipient = match.group(1) - self.encrypt_keys.append(recipient) - - logging.debug("encryption keys: %s", self.encrypt_keys) - keys = yield get_keys(ui, self.encrypt_keys, - signed_only=self.trusted) - if self.trusted: - logging.debug("filtered encrytion keys: " + - " ".join(x.uids[0].uid for x in keys.itervalues())) - if keys: - envelope.encrypt_keys.update(keys) - else: - envelope.encrypt = False + yield set_encrypt(ui, envelope, signed_only=self.trusted) + envelope.encrypt = encrypt if not envelope.encrypt: # This is an extra conditional as it can even happen if encrypt is # True. diff --git a/alot/commands/globals.py b/alot/commands/globals.py index 5090e653..92f6c7cb 100644 --- a/alot/commands/globals.py +++ b/alot/commands/globals.py @@ -7,7 +7,6 @@ import email import glob import logging import os -import re import subprocess from StringIO import StringIO @@ -17,7 +16,7 @@ from twisted.internet import threads from . import Command, registerCommand from . import CommandCanceled -from .utils import get_keys +from .utils import set_encrypt from .. import commands from .. import buffers @@ -864,12 +863,12 @@ class ComposeCommand(Command): logging.debug("Trying to encrypt message because encrypt=%s and " "encrypt_by_default=%s", self.encrypt, account.encrypt_by_default) - yield self._set_encrypt(ui, self.envelope) + yield set_encrypt(ui, self.envelope, block_error=self.encrypt) elif account.encrypt_by_default == u"trusted": logging.debug("Trying to encrypt message because " "account.encrypt_by_default=%s", account.encrypt_by_default) - yield self._set_encrypt(ui, self.envelope, trusted_only=True) + yield set_encrypt(ui, self.envelope, block_error=self.encrypt, signed_only=True) else: logging.debug("No encryption by default, encrypt_by_default=%s", account.encrypt_by_default) @@ -879,38 +878,6 @@ class ComposeCommand(Command): refocus=False) ui.apply_command(cmd) - @inlineCallbacks - def _set_encrypt(self, ui, envelope, trusted_only=False): - """Find and set the encryption keys in an envolope. - - :param ui: the main user interface object - :type ui: alot.ui.UI - :param envolope: the envolope buffer object - :type envolope: alot.buffers.EnvelopeBuffer - :param trusted_only: only add keys to the list of encryption - keys whose uid is signed (trusted to belong to the key) - :type trusted_only: bool - - """ - encrypt_keys = [] - for recipient in envelope.headers['To'][0].split(','): - recipient = recipient.strip() - if not recipient: - continue - match = re.search("<(.*@.*)>", recipient) - if match: - recipient = match.group(1) - encrypt_keys.append(recipient) - - logging.debug("encryption keys: %s", encrypt_keys) - keys = yield get_keys(ui, encrypt_keys, block_error=self.encrypt, - signed_only=trusted_only) - if keys: - envelope.encrypt_keys.update(keys) - envelope.encrypt = True - else: - envelope.encrypt = False - @registerCommand( MODE, 'move', help='move focus in current buffer', diff --git a/alot/commands/utils.py b/alot/commands/utils.py index 7249b9ee..3239f865 100644 --- a/alot/commands/utils.py +++ b/alot/commands/utils.py @@ -1,6 +1,8 @@ # Copyright (C) 2015 Patrick Totzke # This file is released under the GNU GPL, version 3 or a later revision. # For further details see the COPYING file +import re +import logging from twisted.internet.defer import inlineCallbacks, returnValue from ..errors import GPGProblem, GPGCode @@ -8,7 +10,45 @@ from .. import crypto @inlineCallbacks -def get_keys(ui, encrypt_keyids, block_error=False, signed_only=False): +def set_encrypt(ui, envelope, block_error=False, signed_only=False): + """Find and set the encryption keys in an envolope. + + :param ui: the main user interface object + :type ui: alot.ui.UI + :param envolope: the envolope buffer object + :type envolope: alot.buffers.EnvelopeBuffer + :param block_error: wether error messages for the user should expire + automatically or block the ui + :type block_error: bool + :param signed_only: only use keys whose uid is signed (trusted to belong + to the key) + :type signed_only: bool + """ + encrypt_keys = [] + for header in ('To', 'Cc'): + if header not in envelope.headers: + continue + + for recipient in envelope.headers[header][0].split(','): + if not recipient: + continue + match = re.search("<(.*@.*)>", recipient) + if match: + recipient = match.group(1) + encrypt_keys.append(recipient) + + logging.debug("encryption keys: " + str(encrypt_keys)) + keys = yield _get_keys(ui, encrypt_keys, block_error=block_error, + signed_only=signed_only) + if keys: + envelope.encrypt_keys.update(keys) + envelope.encrypt = True + else: + envelope.encrypt = False + + +@inlineCallbacks +def _get_keys(ui, encrypt_keyids, block_error=False, signed_only=False): """Get several keys from the GPG keyring. The keys are selected by keyid and are checked if they can be used for encryption. -- cgit v1.2.3