From 6db4ecfb75ec1969426adbb154bbb3ccbded6b1a Mon Sep 17 00:00:00 2001 From: Johannes Kulick Date: Sat, 15 Dec 2012 13:34:48 +0100 Subject: rmencrypt now accepts keyids/hints instead of indices We use an own has function for that, since pygpgme doesn't implement __cmp__() or similar --- alot/buffers.py | 4 ++-- alot/commands/envelope.py | 20 ++++++++------------ alot/crypto.py | 14 ++++++++++++++ alot/db/envelope.py | 5 +++-- 4 files changed, 27 insertions(+), 16 deletions(-) (limited to 'alot') diff --git a/alot/buffers.py b/alot/buffers.py index 8f344081..50cbd0c3 100644 --- a/alot/buffers.py +++ b/alot/buffers.py @@ -154,13 +154,13 @@ class EnvelopeBuffer(Buffer): if self.envelope.encrypt: description = 'Yes' - encrypt_keys = self.envelope.encrypt_keys + encrypt_keys = self.envelope.encrypt_keys.values() if len(encrypt_keys) == 1: description += ', with key ' elif len(encrypt_keys) > 1: description += ', with keys ' first_key = True - for key in encrypt_keys: + for key in encrypt_keys: if key is not None: if first_key: first_key = False diff --git a/alot/commands/envelope.py b/alot/commands/envelope.py index af0705bc..ccdbae65 100644 --- a/alot/commands/envelope.py +++ b/alot/commands/envelope.py @@ -482,18 +482,12 @@ class EncryptCommand(Command): if self.action == 'rmencrypt': try: for keyid in self.encrypt_keys: - # this is not so nice, but pygpgme doesn't supply a - # __cmp__() operator so list.remove(x) doesn't work - del envelope.encrypt_keys[int(keyid) - 1] + tmp_key = crypto.get_key(keyid) + del envelope.encrypt_keys[crypto.hash_key(tmp_key)] except gpgme.GpgmeError as e: if e.code == gpgme.ERR_INV_VALUE: - raise GPGProblem("Can not find key to encrypt.") + raise GPGProblem("Can not find key to remove.") raise GPGProblem(str(e)) - except ValueError as e: - raise Warning("Enter a the index of the key as argument to " + - "rmencrypt.") - except IndexError as e: - raise Warning("There are not so many encryption keys.") ui.current_buffer.rebuild() return elif self.action == 'encrypt': @@ -508,10 +502,12 @@ class EncryptCommand(Command): # cache all keys before appending to envelope, since otherwise # we get an error message but all earlier keys are added, but # not shown - keys = [] + keys = dict() for keyid in self.encrypt_keys: - keys.append(crypto.get_key(keyid)) - envelope.encrypt_keys.extend(keys) + tmp_key = crypto.get_key(keyid) + keys[crypto.hash_key(tmp_key)] = tmp_key + + envelope.encrypt_keys.update(keys) except gpgme.GpgmeError as e: if e.code == gpgme.ERR_INV_VALUE: raise GPGProblem("Can not find key to encrypt.") diff --git a/alot/crypto.py b/alot/crypto.py index be7e83e2..82b76ecb 100644 --- a/alot/crypto.py +++ b/alot/crypto.py @@ -171,3 +171,17 @@ def encrypt(plaintext_str, keys=None): encrypted_data.seek(0, 0) encrypted = encrypted_data.read() return encrypted + +def hash_key(key): + """ + Returns a hash of the given key. This is a workaround for + https://bugs.launchpad.net/pygpgme/+bug/1089865 + and can be removed if the missing feature is added to pygpgme + + :param key: the key we want a hash of + :rtype: a has of the key as string + """ + hash_str = "" + for tmp_key in key.subkeys: + hash_str += tmp_key.keyid + return hash_str diff --git a/alot/db/envelope.py b/alot/db/envelope.py index 00f7a22f..2b35c0b6 100644 --- a/alot/db/envelope.py +++ b/alot/db/envelope.py @@ -58,7 +58,7 @@ class Envelope(object): self.sign = sign self.sign_key = sign_key self.encrypt = encrypt - self.encrypt_keys = [] + self.encrypt_keys = {} self.tags = tags # tags to add after successful sendout self.sent_time = None self.modified_since_sent = False @@ -214,7 +214,8 @@ class Envelope(object): try: - encrypted_str = crypto.encrypt(plaintext, self.encrypt_keys) + encrypted_str = crypto.encrypt(plaintext, + self.encrypt_keys.values()) except gpgme.GpgmeError as e: raise GPGProblem(str(e)) -- cgit v1.2.3