extend testing of pubkey encryption capability
authorAndrew Engelbrecht <sudoman@ninthfloor.org>
Tue, 22 Sep 2015 12:42:44 +0000 (08:42 -0400)
committerAndrew Engelbrecht <sudoman@ninthfloor.org>
Mon, 7 Dec 2015 18:29:55 +0000 (13:29 -0500)
test whether it is expired, invalid, disabled, etc.

edward

diff --git a/edward b/edward
index 8cffbcbe30ae1064c7710f203db447145c4c6890..e2d3db0be8ebf4af8e0a5b605fcf3b247fa3e661 100755 (executable)
--- a/edward
+++ b/edward
@@ -928,7 +928,7 @@ def get_key_from_fp (replyinfo_obj, gpgme_ctx):
             except gpgme.GpgmeError:
                 continue
 
-            if encrypt_to_key.can_encrypt == True and encrypt_to_key.revoked == False:
+            if is_key_usable(encrypt_to_key):
                 replyinfo_obj.encrypt_to_key = encrypt_to_key
                 replyinfo_obj.have_reply_key = True
                 replyinfo_obj.key_can_encrypt = True
@@ -1059,7 +1059,7 @@ def add_gpg_key (key_block, gpgme_ctx):
         except:
             pass
 
-        if key_obj.can_encrypt == True and key_obj.revoked == False:
+        if is_key_usable(key_obj):
             key_fingerprints += [fingerprint]
             key_cannot_encrypt = False
 
@@ -1202,7 +1202,7 @@ def get_signature_fp (sigs, gpgme_ctx):
                     sigkey_missing = True
                     continue
 
-            if key_obj.can_encrypt == True and key_obj.revoked == False:
+            if is_key_usable(key_obj):
                 fingerprints += [sig.fpr]
                 key_cannot_encrypt = False
                 sigkey_missing = False
@@ -1217,6 +1217,24 @@ def get_signature_fp (sigs, gpgme_ctx):
     return (fingerprints, sigkey_missing, key_cannot_encrypt)
 
 
+def is_key_usable (key_obj):
+    """Returns boolean representing key usability regarding encryption
+
+    Tests various feature of key and returns usability
+
+    Args:
+        key_obj: a gpgme key object
+
+    Returns:
+        A boolean representing key usability
+    """
+    if key_obj.can_encrypt and not key_obj.invalid and not key_obj.expired \
+            and not key_obj.revoked and not key_obj.disabled:
+        return True
+    else:
+        return False
+
+
 def email_to_from_subject (email_bytes):
     """Returns the values of the email's To:, From: and Subject: fields