c64c63a4966ed076c527cd265644ee284063b874
[claws.git] / src / plugins / pgpcore / autocompletion.c
1 /*
2  * PGP/Core keyring autocompletion
3  *
4  * Copyright (C) 2014 Christian Hesse <mail@eworm.de>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19  */
20
21
22 #include <errno.h>
23 #include <inttypes.h>
24 #include <stdlib.h>
25
26 #include <glib.h>
27 #include <glib/gi18n.h>
28
29 #include <gpgme.h>
30
31 #include "autocompletion.h"
32
33 static guint autocompletion_hook_id = 0;
34
35 static gboolean pgp_autocompletion_hook(gpointer source, gpointer data)
36 {
37         gpgme_ctx_t ctx;
38         gpgme_key_t key;
39         gpgme_error_t err;
40         gpgme_user_id_t uid;
41         address_entry *ae;
42         GList *addr_list = NULL;
43
44         /* just return if autocompletion is disabled */
45         if (!prefs_gpg_get_config()->autocompletion)
46                 return EXIT_SUCCESS;
47
48         /* initialize */
49         gpgme_check_version(NULL);
50
51         if ((err = gpgme_new(&ctx)) == 0) {
52                 err = gpgme_op_keylist_start(ctx, NULL, 0);
53
54                 /* walk the available keys */
55                 while (err == 0) {
56                         if ((err = gpgme_op_keylist_next(ctx, &key)) != 0)
57                                 break;
58
59                         /* skip keys that are revoked, expired, ... */
60                         if ((key->revoked || key->expired || key->disabled || key->invalid) == FALSE) {
61                                 uid = key->uids;
62
63                                 /* walk all user ids within a key */
64                                 while (uid != NULL) {
65                                         if (uid->email != NULL && *uid->email != 0) {
66                                                 ae = g_new0(address_entry, 1);
67
68                                                 ae->address = g_strdup(uid->email);
69                                                 addr_compl_add_address1(ae->address, ae);
70
71                                                 if (uid->name != NULL && *uid->name != 0) {
72                                                         ae->name = g_strdup(uid->name);
73                                                         addr_compl_add_address1(ae->name, ae);
74                                                 } else
75                                                         ae->name = NULL;
76
77                                                 ae->grp_emails = NULL;
78
79                                                 addr_list = g_list_prepend(addr_list, ae);
80
81                                                 debug_print("%s <%s>\n", uid->name, uid->email);
82                                         }
83                                         uid = uid->next;
84                                 }
85                         }
86                         gpgme_key_release(key);
87                 }
88                 gpgme_release(ctx);
89         }
90
91         if (gpg_err_code(err) != GPG_ERR_EOF) {
92                 debug_print("can not list keys: %s\n", gpgme_strerror(err));
93                 return EXIT_FAILURE;
94         }
95         *((GList **)source) = addr_list;
96
97         return EXIT_SUCCESS;
98 }
99
100 gboolean autocompletion_done(void)
101 {
102         if (autocompletion_hook_id > 0)
103         {
104                 hooks_unregister_hook(ADDDRESS_COMPLETION_BUILD_ADDRESS_LIST_HOOKLIST, autocompletion_hook_id);
105
106                 debug_print("PGP address autocompletion hook unregistered\n");
107         }
108
109         return TRUE;
110 }
111
112 gint autocompletion_init(gchar ** error)
113 {
114         if ((autocompletion_hook_id = hooks_register_hook(ADDDRESS_COMPLETION_BUILD_ADDRESS_LIST_HOOKLIST, pgp_autocompletion_hook, NULL)) == -1)
115         {
116                 *error = g_strdup(_("Failed to register PGP address autocompletion hook"));
117                 return -1;
118         }
119         debug_print("PGP address autocompletion hook registered\n");
120
121         return EXIT_SUCCESS;
122 }
123