about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorJames Booth <boothj5@gmail.com>2015-08-25 23:44:03 +0100
committerJames Booth <boothj5@gmail.com>2015-08-25 23:44:03 +0100
commitcb19be2ffcc1f2881427cc12f01ac8790d9236c1 (patch)
treefac2e88be2d777db348696fb00088ae45a87301f
parent55c2d1cc21974936e5b431de49ae81e6fc3845be (diff)
downloadprofani-tty-cb19be2ffcc1f2881427cc12f01ac8790d9236c1.tar.gz
Added PGP key autocompleter
-rw-r--r--src/command/command.c26
-rw-r--r--src/pgp/gpg.c32
-rw-r--r--src/pgp/gpg.h2
-rw-r--r--tests/unittests/pgp/stub_gpg.c7
4 files changed, 67 insertions, 0 deletions
diff --git a/src/command/command.c b/src/command/command.c
index 58a2e295..487c9e02 100644
--- a/src/command/command.c
+++ b/src/command/command.c
@@ -59,6 +59,9 @@
 #ifdef HAVE_LIBOTR
 #include "otr/otr.h"
 #endif
+#ifdef HAVE_LIBGPGME
+#include "pgp/gpg.h"
+#endif
 #include "profanity.h"
 #include "tools/autocomplete.h"
 #include "tools/parser.h"
@@ -2242,6 +2245,9 @@ cmd_reset_autocomplete(ProfWin *window)
     accounts_reset_enabled_search();
     prefs_reset_boolean_choice();
     presence_reset_sub_request_search();
+#ifdef HAVE_LIBGPGME
+    p_gpg_autocomplete_key_reset();
+#endif
     autocomplete_reset(help_ac);
     autocomplete_reset(help_commands_ac);
     autocomplete_reset(notify_ac);
@@ -2983,6 +2989,26 @@ _pgp_autocomplete(ProfWin *window, const char * const input)
         return found;
     }
 
+#ifdef HAVE_LIBGPGME
+    gboolean result;
+    gchar **args = parse_args(input, 2, 3, &result);
+    if ((strncmp(input, "/pgp", 4) == 0) && (result == TRUE)) {
+        GString *beginning = g_string_new("/pgp ");
+        g_string_append(beginning, args[0]);
+        if (args[1]) {
+            g_string_append(beginning, " ");
+            g_string_append(beginning, args[1]);
+        }
+        found = autocomplete_param_with_func(input, beginning->str, p_gpg_autocomplete_key);
+        g_string_free(beginning, TRUE);
+        if (found) {
+            g_strfreev(args);
+            return found;
+        }
+    }
+    g_strfreev(args);
+#endif
+
     found = autocomplete_param_with_func(input, "/pgp setkey", roster_barejid_autocomplete);
     if (found) {
         return found;
diff --git a/src/pgp/gpg.c b/src/pgp/gpg.c
index b4bcef48..8d075cd9 100644
--- a/src/pgp/gpg.c
+++ b/src/pgp/gpg.c
@@ -47,6 +47,7 @@
 #include "pgp/gpg.h"
 #include "log.h"
 #include "common.h"
+#include "tools/autocomplete.h"
 
 #define PGP_SIGNATURE_HEADER "-----BEGIN PGP SIGNATURE-----"
 #define PGP_SIGNATURE_FOOTER "-----END PGP SIGNATURE-----"
@@ -59,6 +60,8 @@ static GHashTable *pubkeys;
 static gchar *pubsloc;
 static GKeyFile *pubkeyfile;
 
+static Autocomplete key_ac;
+
 static char* _remove_header_footer(char *str, const char * const footer);
 static char* _add_header_footer(const char * const str, const char * const header, const char * const footer);
 static void _save_pubkeys(void);
@@ -80,6 +83,10 @@ p_gpg_init(void)
     gpgme_set_locale(NULL, LC_CTYPE, setlocale(LC_CTYPE, NULL));
 
     pubkeys = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, (GDestroyNotify)_p_gpg_free_pubkeyid);
+
+    key_ac = autocomplete_new();
+    GHashTable *keys = p_gpg_list_keys();
+    p_gpg_free_keys(keys);
 }
 
 void
@@ -97,6 +104,9 @@ p_gpg_close(void)
 
     free(pubsloc);
     pubsloc = NULL;
+
+    autocomplete_free(key_ac);
+    key_ac = NULL;
 }
 
 void
@@ -327,6 +337,16 @@ p_gpg_list_keys(void)
 
     gpgme_release(ctx);
 
+    autocomplete_clear(key_ac);
+    GList *ids = g_hash_table_get_keys(result);
+    GList *curr = ids;
+    while (curr) {
+        ProfPGPKey *key = g_hash_table_lookup(result, curr->data);
+        autocomplete_add(key_ac, key->id);
+        curr = curr->next;
+    }
+    g_list_free(ids);
+
     return result;
 }
 
@@ -639,6 +659,18 @@ p_gpg_free_decrypted(char *decrypted)
     g_free(decrypted);
 }
 
+char *
+p_gpg_autocomplete_key(const char * const search_str)
+{
+    return autocomplete_complete(key_ac, search_str, TRUE);
+}
+
+void
+p_gpg_autocomplete_key_reset(void)
+{
+    autocomplete_reset(key_ac);
+}
+
 static char*
 _remove_header_footer(char *str, const char * const footer)
 {
diff --git a/src/pgp/gpg.h b/src/pgp/gpg.h
index 2d3a78a3..48742dd7 100644
--- a/src/pgp/gpg.h
+++ b/src/pgp/gpg.h
@@ -67,5 +67,7 @@ void p_gpg_verify(const char * const barejid, const char *const sign);
 char* p_gpg_encrypt(const char * const barejid, const char * const message);
 char* p_gpg_decrypt(const char * const cipher);
 void p_gpg_free_decrypted(char *decrypted);
+char* p_gpg_autocomplete_key(const char * const search_str);
+void p_gpg_autocomplete_key_reset(void);
 
 #endif
diff --git a/tests/unittests/pgp/stub_gpg.c b/tests/unittests/pgp/stub_gpg.c
index d82f0933..4ac51ab5 100644
--- a/tests/unittests/pgp/stub_gpg.c
+++ b/tests/unittests/pgp/stub_gpg.c
@@ -54,3 +54,10 @@ void p_gpg_free_decrypted(char *decrypted) {}
 
 void p_gpg_free_keys(GHashTable *keys) {}
 
+void p_gpg_autocomplete_key_reset(void) {}
+
+char * p_gpg_autocomplete_key(const char * const search_str)
+{
+    return NULL;
+}
+