diff options
author | Dylan Baker <baker.dylan.c@gmail.com> | 2017-07-27 15:04:26 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-07-27 15:04:26 -0700 |
commit | 78d069ea38277721e31b621daf8df0d8b8174041 (patch) | |
tree | 41c287cf4aa1d52a2c52b804c41f5643465da8fc /alot | |
parent | 054eab33e52bd9687a4b567e9ea369d447cc33ee (diff) | |
parent | 084e37cc9b5148d75f88936a353d5249824a4b60 (diff) |
Merge pull request #1089 from dcbaker/submit/sign-without-hint
commands/envelope: Fall back to account for signing key
Diffstat (limited to 'alot')
-rw-r--r-- | alot/commands/envelope.py | 62 | ||||
-rw-r--r-- | alot/commands/utils.py | 1 | ||||
-rw-r--r-- | alot/settings/errors.py | 5 | ||||
-rw-r--r-- | alot/settings/manager.py | 26 |
4 files changed, 60 insertions, 34 deletions
diff --git a/alot/commands/envelope.py b/alot/commands/envelope.py index a2945fbe..4a1482b6 100644 --- a/alot/commands/envelope.py +++ b/alot/commands/envelope.py @@ -16,7 +16,7 @@ from twisted.internet.defer import inlineCallbacks from . import Command, registerCommand from . import globals -from .utils import set_encrypt +from . import utils from .. import buffers from .. import commands from .. import crypto @@ -26,6 +26,7 @@ from ..errors import GPGProblem from ..helper import email_as_string from ..helper import string_decode from ..settings import settings +from ..settings.errors import NoMatchingAccount from ..utils import argparse as cargparse @@ -114,18 +115,17 @@ class SaveCommand(Command): envelope = ui.current_buffer.envelope # determine account to use - _, saddr = email.Utils.parseaddr(envelope.get('From')) - account = settings.get_account_by_address(saddr) - if account is None: - if not settings.get_accounts(): - ui.notify('no accounts set.', priority='error') - return - else: - account = settings.get_accounts()[0] + try: + account = settings.get_account_by_address( + envelope.get('From'), return_default=True) + except NoMatchingAccount: + ui.notify('no accounts set.', priority='error') + return if account.draft_box is None: - ui.notify('abort: account <%s> has no draft_box set.' % saddr, - priority='error') + ui.notify( + 'abort: account <%s> has no draft_box set.' % envelope.get('From'), + priority='error') return mail = envelope.construct_mail() @@ -214,14 +214,12 @@ class SendCommand(Command): msg = self.mail if not isinstance(msg, email.message.Message): msg = email.message_from_string(self.mail) - _, saddr = email.Utils.parseaddr(msg.get('From', '')) - account = settings.get_account_by_address(saddr) - if account is None: - if not settings.get_accounts(): - ui.notify('no accounts set', priority='error') - return - else: - account = settings.get_accounts()[0] + try: + account = settings.get_account_by_address( + msg.get('From', ''), return_default=True) + except NoMatchingAccount: + ui.notify('no accounts set', priority='error') + return # make sure self.mail is a string logging.debug(self.mail.__class__) @@ -472,7 +470,6 @@ class SignCommand(Command): def apply(self, ui): sign = None - key = None envelope = ui.current_buffer.envelope # sign status if self.action == 'sign': @@ -483,17 +480,32 @@ class SignCommand(Command): sign = not envelope.sign envelope.sign = sign - # try to find key if hint given as parameter if sign: - if len(self.keyid) > 0: + if self.keyid: + # try to find key if hint given as parameter keyid = str(' '.join(self.keyid)) try: - key = crypto.get_key(keyid, validate=True, sign=True) + envelope.sign_key = crypto.get_key(keyid, validate=True, + sign=True) except GPGProblem as e: envelope.sign = False ui.notify(e.message, priority='error') return - envelope.sign_key = key + else: + try: + acc = settings.get_account_by_address( + envelope.headers['From'][0]) + except NoMatchingAccount: + envelope.sign = False + ui.notify('Unable to find a matching account', + priority='error') + return + if not acc.gpg_key: + envelope.sign = False + ui.notify('Account for {} has no gpg key'.format(acc.address), + priority='error') + return + envelope.sign_key = acc.gpg_key else: envelope.sign_key = None @@ -562,7 +574,7 @@ class EncryptCommand(Command): elif self.action == 'toggleencrypt': encrypt = not envelope.encrypt if encrypt: - yield set_encrypt(ui, envelope, signed_only=self.trusted) + yield utils.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 diff --git a/alot/commands/utils.py b/alot/commands/utils.py index f54d4f09..f8e5067d 100644 --- a/alot/commands/utils.py +++ b/alot/commands/utils.py @@ -5,6 +5,7 @@ from __future__ import absolute_import import re import logging + from twisted.internet.defer import inlineCallbacks, returnValue from ..errors import GPGProblem, GPGCode diff --git a/alot/settings/errors.py b/alot/settings/errors.py index 606f78e1..baf794ff 100644 --- a/alot/settings/errors.py +++ b/alot/settings/errors.py @@ -6,3 +6,8 @@ class ConfigError(Exception): """could not parse user config""" pass + + +class NoMatchingAccount(ConfigError): + """No account matching requirements found.""" + pass diff --git a/alot/settings/manager.py b/alot/settings/manager.py index ab68659e..796f98a5 100644 --- a/alot/settings/manager.py +++ b/alot/settings/manager.py @@ -17,7 +17,7 @@ from ..addressbook.external import ExternalAddressbook from ..helper import pretty_datetime, string_decode from ..utils import configobj as checks -from .errors import ConfigError +from .errors import ConfigError, NoMatchingAccount from .utils import read_config from .utils import resolve_att from .theme import Theme @@ -430,19 +430,27 @@ class SettingsManager(object): """ return self._accounts - def get_account_by_address(self, address): - """ - returns :class:`Account` for a given email address (str) + def get_account_by_address(self, address, return_default=False): + """returns :class:`Account` for a given email address (str) - :param address: address to look up - :type address: string - :rtype: :class:`Account` or None + :param str address: address to look up + :param bool return_default: If True and no address can be found, then + the default account wil be returned + :rtype: :class:`Account` + :raises ~alot.settings.errors.NoMatchingAccount: If no account can be + found. Thsi includes if return_default is True and there are no + accounts defined. """ - for myad in self.get_addresses(): if myad == address: return self._accountmap[myad] - return None + if return_default: + try: + return self.get_accounts()[0] + except IndexError: + # Fall through + pass + raise NoMatchingAccount def get_main_addresses(self): """returns addresses of known accounts without its aliases""" |