fix bad parsing of message when re-editing/viewing a
[claws.git] / src / sgpgme.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2003 Hiroyuki Yamamoto & the Sylpheed-Claws team
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18  */
19  
20 #ifdef HAVE_CONFIG_H
21 #  include "config.h"
22 #endif
23  
24 #ifdef USE_GPGME
25
26 #include <gtk/gtk.h>
27 #include <gpgme.h>
28
29 #include "sgpgme.h"
30 #include "privacy.h"
31 #include "prefs_common.h"
32 #include "utils.h"
33 #include "alertpanel.h"
34 #include "passphrase.h"
35 #include "intl.h"
36
37 static void idle_function_for_gpgme(void)
38 {
39         while (gtk_events_pending())
40                 gtk_main_iteration();
41 }
42
43 static void sgpgme_disable_all(void)
44 {
45     /* FIXME: set a flag, so that we don't bother the user with failed
46      * gpgme messages */
47 }
48
49 GpgmeSigStat sgpgme_verify_signature(GpgmeCtx ctx, GpgmeData sig, 
50                                         GpgmeData plain)
51 {
52         GpgmeSigStat status;
53
54         if (gpgme_op_verify(ctx, sig, plain, &status) != GPGME_No_Error)
55                 return GPGME_SIG_STAT_ERROR;
56
57         return status;
58 }
59
60 SignatureStatus sgpgme_sigstat_gpgme_to_privacy(GpgmeSigStat status)
61 {
62         switch (status) {
63         case GPGME_SIG_STAT_GOOD:
64                 return SIGNATURE_OK;
65         case GPGME_SIG_STAT_GOOD_EXP:
66         case GPGME_SIG_STAT_GOOD_EXPKEY:
67         case GPGME_SIG_STAT_DIFF:
68                 return SIGNATURE_WARN;
69         case GPGME_SIG_STAT_BAD:
70                 return SIGNATURE_INVALID;
71         case GPGME_SIG_STAT_NOKEY:
72         case GPGME_SIG_STAT_NOSIG:
73         case GPGME_SIG_STAT_ERROR:
74                 return SIGNATURE_CHECK_FAILED;
75         case GPGME_SIG_STAT_NONE:
76                 return SIGNATURE_UNCHECKED;
77         }
78         return SIGNATURE_CHECK_FAILED;
79 }
80
81 static const gchar *get_validity_str(unsigned long validity)
82 {
83         switch (validity) {
84         case GPGME_VALIDITY_UNKNOWN:
85                 return _("Unknown");
86         case GPGME_VALIDITY_UNDEFINED:
87                 return _("Undefined");
88         case GPGME_VALIDITY_NEVER:
89                 return _("Never");
90         case GPGME_VALIDITY_MARGINAL:
91                 return _("Marginal");
92         case GPGME_VALIDITY_FULL:
93                 return _("Full");
94         case GPGME_VALIDITY_ULTIMATE:
95                 return _("Ultimate");
96         default:
97                 return _("Error");
98         }
99 }
100
101 gchar *sgpgme_sigstat_info_short(GpgmeCtx ctx, GpgmeSigStat status)
102 {
103         switch (status) {
104         case GPGME_SIG_STAT_GOOD:
105         {
106                 GpgmeKey key;
107                 unsigned long validity = 0;
108         
109                 if (gpgme_get_sig_key(ctx, 0, &key) != GPGME_No_Error)
110                         return g_strdup(_("Error"));
111
112                 validity = gpgme_get_sig_ulong_attr(ctx, 0,
113                         GPGME_ATTR_VALIDITY, 0);
114                 
115                 return g_strdup_printf(_("Valid signature by %s (Trust: %s)"),
116                         gpgme_key_get_string_attr(key, GPGME_ATTR_NAME, NULL, 0),
117                         get_validity_str(validity));
118         }
119         case GPGME_SIG_STAT_GOOD_EXP:
120                 return g_strdup(_("The signature of this part has expired"));
121         case GPGME_SIG_STAT_GOOD_EXPKEY:
122                 return g_strdup(_("The key that was used to sign this part has expired"));
123         case GPGME_SIG_STAT_DIFF:
124                 return g_strdup(_("Not all signatures are valid"));
125         case GPGME_SIG_STAT_BAD:
126                 return g_strdup(_("This signature is invalid"));
127         case GPGME_SIG_STAT_NOKEY:
128                 return g_strdup(_("You have no key to verify this signature"));
129         case GPGME_SIG_STAT_NOSIG:
130                 return g_strdup(_("Bo signature found"));
131         case GPGME_SIG_STAT_ERROR:
132                 return g_strdup(_("An error occured"));
133         case GPGME_SIG_STAT_NONE:
134                 return g_strdup(_("The signature of this part has not been checked"));
135         }
136         return g_strdup(_("Error"));
137 }
138
139 void sgpgme_init()
140 {
141         if (gpgme_engine_check_version(GPGME_PROTOCOL_OpenPGP) != 
142                         GPGME_No_Error) {  /* Also does some gpgme init */
143                 sgpgme_disable_all();
144                 debug_print("gpgme_engine_version:\n%s\n",
145                             gpgme_get_engine_info());
146
147                 if (prefs_common.gpg_warning) {
148                         AlertValue val;
149
150                         val = alertpanel_message_with_disable
151                                 (_("Warning"),
152                                  _("GnuPG is not installed properly, or needs to be upgraded.\n"
153                                    "OpenPGP support disabled."));
154                         if (val & G_ALERTDISABLE)
155                                 prefs_common.gpg_warning = FALSE;
156                 }
157         }
158
159         gpgme_register_idle(idle_function_for_gpgme);
160 }
161
162 void sgpgme_done()
163 {
164         gpgmegtk_free_passphrase();
165 }
166
167 #endif /* USE_GPGME */