2355bd81daabcc7cc292566c8d057f08467e6529
[claws.git] / src / plugins / pgpcore / autocompletion.c
1 /*
2  * PGP/Core keyring autocompletion
3  *
4  * Copyright (C) 2014 Christian Hesse <mail@eworm.de> and the Claws Mail team
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 #ifdef HAVE_CONFIG_H
23 #  include "config.h"
24 #include "claws-features.h"
25 #endif
26
27 #include <errno.h>
28 #include <inttypes.h>
29 #include <stdlib.h>
30
31 #include <glib.h>
32 #include <glib/gi18n.h>
33
34 #include <gpgme.h>
35
36 #include "autocompletion.h"
37 #include "addr_compl.h"
38 #include "prefs_gpg.h"
39 #include "hooks.h"
40 #include "utils.h"
41
42 static guint autocompletion_hook_id = 0;
43
44 static gboolean pgp_autocompletion_hook(gpointer source, gpointer data)
45 {
46         gpgme_ctx_t ctx;
47         gpgme_key_t key;
48         gpgme_error_t err;
49         gpgme_user_id_t uid;
50         address_entry *ae;
51         GList *addr_list = NULL;
52
53         /* just return if autocompletion is disabled */
54         if (!prefs_gpg_get_config()->autocompletion)
55                 return EXIT_SUCCESS;
56
57         /* initialize */
58         gpgme_check_version(NULL);
59
60         if ((err = gpgme_new(&ctx)) == 0) {
61                 err = gpgme_op_keylist_start(ctx, NULL, 0);
62
63                 /* walk the available keys */
64                 while (err == 0) {
65                         if ((err = gpgme_op_keylist_next(ctx, &key)) != 0)
66                                 break;
67
68                         /* skip keys that are revoked, expired, ... */
69                         if ((key->revoked || key->expired || key->disabled || key->invalid) == FALSE) {
70                                 uid = key->uids;
71
72                                 /* walk all user ids within a key */
73                                 while (uid != NULL) {
74                                         if (uid->email != NULL && *uid->email != 0) {
75                                                 ae = g_new0(address_entry, 1);
76
77                                                 ae->address = g_strdup(uid->email);
78                                                 addr_compl_add_address1(ae->address, ae);
79
80                                                 if (uid->name != NULL && *uid->name != 0) {
81                                                         ae->name = g_strdup(uid->name);
82                                                         addr_compl_add_address1(ae->name, ae);
83                                                 } else
84                                                         ae->name = NULL;
85
86                                                 ae->grp_emails = NULL;
87
88                                                 addr_list = g_list_prepend(addr_list, ae);
89
90                                                 debug_print("%s <%s>\n", uid->name, uid->email);
91                                         }
92                                         uid = uid->next;
93                                 }
94                         }
95                         gpgme_key_release(key);
96                 }
97                 gpgme_release(ctx);
98         }
99
100         if (gpg_err_code(err) != GPG_ERR_EOF) {
101                 debug_print("can not list keys: %s\n", gpgme_strerror(err));
102                 return EXIT_FAILURE;
103         }
104         *((GList **)source) = addr_list;
105
106         return EXIT_SUCCESS;
107 }
108
109 gboolean autocompletion_done(void)
110 {
111         if (autocompletion_hook_id > 0)
112         {
113                 hooks_unregister_hook(ADDDRESS_COMPLETION_BUILD_ADDRESS_LIST_HOOKLIST, autocompletion_hook_id);
114
115                 debug_print("PGP address autocompletion hook unregistered\n");
116         }
117
118         return TRUE;
119 }
120
121 gint autocompletion_init(gchar ** error)
122 {
123         if ((autocompletion_hook_id = hooks_register_hook(ADDDRESS_COMPLETION_BUILD_ADDRESS_LIST_HOOKLIST, pgp_autocompletion_hook, NULL)) == -1)
124         {
125                 *error = g_strdup(_("Failed to register PGP address autocompletion hook"));
126                 return -1;
127         }
128         debug_print("PGP address autocompletion hook registered\n");
129
130         return EXIT_SUCCESS;
131 }
132