f34185bb9a52fdbac92f88d09fe96eb5f64ec67e
[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
38 static guint autocompletion_hook_id = 0;
39
40 static gboolean pgp_autocompletion_hook(gpointer source, gpointer data)
41 {
42         gpgme_ctx_t ctx;
43         gpgme_key_t key;
44         gpgme_error_t err;
45         gpgme_user_id_t uid;
46         address_entry *ae;
47         GList *addr_list = NULL;
48
49         /* just return if autocompletion is disabled */
50         if (!prefs_gpg_get_config()->autocompletion)
51                 return EXIT_SUCCESS;
52
53         /* initialize */
54         gpgme_check_version(NULL);
55
56         if ((err = gpgme_new(&ctx)) == 0) {
57                 err = gpgme_op_keylist_start(ctx, NULL, 0);
58
59                 /* walk the available keys */
60                 while (err == 0) {
61                         if ((err = gpgme_op_keylist_next(ctx, &key)) != 0)
62                                 break;
63
64                         /* skip keys that are revoked, expired, ... */
65                         if ((key->revoked || key->expired || key->disabled || key->invalid) == FALSE) {
66                                 uid = key->uids;
67
68                                 /* walk all user ids within a key */
69                                 while (uid != NULL) {
70                                         if (uid->email != NULL && *uid->email != 0) {
71                                                 ae = g_new0(address_entry, 1);
72
73                                                 ae->address = g_strdup(uid->email);
74                                                 addr_compl_add_address1(ae->address, ae);
75
76                                                 if (uid->name != NULL && *uid->name != 0) {
77                                                         ae->name = g_strdup(uid->name);
78                                                         addr_compl_add_address1(ae->name, ae);
79                                                 } else
80                                                         ae->name = NULL;
81
82                                                 ae->grp_emails = NULL;
83
84                                                 addr_list = g_list_prepend(addr_list, ae);
85
86                                                 debug_print("%s <%s>\n", uid->name, uid->email);
87                                         }
88                                         uid = uid->next;
89                                 }
90                         }
91                         gpgme_key_release(key);
92                 }
93                 gpgme_release(ctx);
94         }
95
96         if (gpg_err_code(err) != GPG_ERR_EOF) {
97                 debug_print("can not list keys: %s\n", gpgme_strerror(err));
98                 return EXIT_FAILURE;
99         }
100         *((GList **)source) = addr_list;
101
102         return EXIT_SUCCESS;
103 }
104
105 gboolean autocompletion_done(void)
106 {
107         if (autocompletion_hook_id > 0)
108         {
109                 hooks_unregister_hook(ADDDRESS_COMPLETION_BUILD_ADDRESS_LIST_HOOKLIST, autocompletion_hook_id);
110
111                 debug_print("PGP address autocompletion hook unregistered\n");
112         }
113
114         return TRUE;
115 }
116
117 gint autocompletion_init(gchar ** error)
118 {
119         if ((autocompletion_hook_id = hooks_register_hook(ADDDRESS_COMPLETION_BUILD_ADDRESS_LIST_HOOKLIST, pgp_autocompletion_hook, NULL)) == -1)
120         {
121                 *error = g_strdup(_("Failed to register PGP address autocompletion hook"));
122                 return -1;
123         }
124         debug_print("PGP address autocompletion hook registered\n");
125
126         return EXIT_SUCCESS;
127 }
128