a2ed2c9c5193610424dcd7e9519ef7ede24ab16a
[claws.git] / src / plugins / pgpcore / pgp_viewer.c
1 /*
2  * Claws Mail -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2007 the Claws Mail team
4  * This file Copyright (C) 2006 Colin Leroy <colin@colino.net>
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, see <http://www.gnu.org/licenses/>.
18  * 
19  */
20
21 #ifdef HAVE_CONFIG_H
22 #  include "config.h"
23 #endif
24
25 #include <stddef.h>
26 #include <glib.h>
27 #include <glib/gi18n.h>
28 #include <sys/types.h>
29 #ifndef G_OS_WIN32
30 #  include <sys/wait.h>
31 #endif
32 #if (defined(__DragonFly__) || defined(SOLARIS) || defined (__NetBSD__) || defined (__FreeBSD__) || defined (__OpenBSD__))
33 #  include <sys/signal.h>
34 #endif
35
36 #include "version.h"
37 #include "common/claws.h"
38 #include "mainwindow.h"
39 #include "mimeview.h"
40 #include "textview.h"
41 #include "sgpgme.h"
42 #include "prefs_common.h"
43 #include "prefs_gpg.h"
44 #include "alertpanel.h"
45 #include "plugin.h"
46
47 typedef struct _PgpViewer PgpViewer;
48
49 static MimeViewerFactory pgp_viewer_factory;
50
51 struct _PgpViewer
52 {
53         MimeViewer       mimeviewer;
54         TextView        *textview;      
55 };
56
57 static gchar *content_types[] = 
58         {"application/pgp-signature", NULL};
59
60 static GtkWidget *pgp_get_widget(MimeViewer *_viewer)
61 {
62         PgpViewer *viewer = (PgpViewer *) _viewer;
63
64         debug_print("pgp_get_widget\n");
65
66         return GTK_WIDGET(viewer->textview->vbox);
67 }
68
69 static void pgpview_show_mime_part(TextView *textview, MimeInfo *partinfo)
70 {
71         GtkTextView *text;
72         GtkTextBuffer *buffer;
73         GtkTextIter iter;
74         gpgme_data_t sigdata = NULL;
75         gpgme_verify_result_t sigstatus = NULL;
76         gpgme_ctx_t ctx = NULL;
77         gpgme_key_t key = NULL;
78         gpgme_signature_t sig = NULL;
79         gpgme_error_t err = 0;
80         if (!partinfo) return;
81
82         
83         textview_set_font(textview, NULL);
84         textview_clear(textview);
85
86         text = GTK_TEXT_VIEW(textview->text);
87         buffer = gtk_text_view_get_buffer(text);
88         gtk_text_buffer_get_start_iter(buffer, &iter);
89
90         err = gpgme_new (&ctx);
91         if (err) {
92                 debug_print("err : %s\n", gpgme_strerror(err));
93                 textview_show_mime_part(textview, partinfo);
94                 return;
95         }
96         
97         sigdata = sgpgme_data_from_mimeinfo(partinfo);
98         if (!sigdata) {
99                 g_warning("no sigdata");
100                 textview_show_mime_part(textview, partinfo);
101                 return;
102         }
103         sigstatus = sgpgme_verify_signature(ctx, sigdata, sigdata, NULL);
104         if (!sigstatus || sigstatus == GINT_TO_POINTER(-GPG_ERR_SYSTEM_ERROR)) {
105                 g_warning("no sigstatus");
106                 textview_show_mime_part(textview, partinfo);
107                 return;
108         }
109         sig = sigstatus->signatures;
110         if (!sig) {
111                 g_warning("no sig");
112                 textview_show_mime_part(textview, partinfo);
113                 return;
114         }
115         gpgme_get_key(ctx, sig->fpr, &key, 0);
116         if (!key) {
117                 gchar *cmd = g_strdup_printf("gpg --no-tty --recv-keys %s", sig->fpr);
118                 AlertValue val = G_ALERTDEFAULT;
119                 if (!prefs_common_get_prefs()->work_offline) {
120                         val = alertpanel(_("Key import"),
121                                 _("This key is not in your keyring. Do you want "
122                                   "Claws Mail to try and import it from a "
123                                   "keyserver?"),
124                                   GTK_STOCK_NO, "+" GTK_STOCK_YES, NULL);
125                         GTK_EVENTS_FLUSH();
126                 }
127                 if (val == G_ALERTDEFAULT) {
128                         TEXTVIEW_INSERT(_("\n  Key ID "));
129                         TEXTVIEW_INSERT(sig->fpr);
130                         TEXTVIEW_INSERT(":\n\n");
131                         TEXTVIEW_INSERT(_("   This key is not in your keyring.\n"));
132                         TEXTVIEW_INSERT(_("   It should be possible to import it "));
133                         if (prefs_common_get_prefs()->work_offline)
134                                 TEXTVIEW_INSERT(_("when working online,\n   or "));
135                         TEXTVIEW_INSERT(_("with the following command: \n\n     "));
136                         TEXTVIEW_INSERT(cmd);
137                 } else {
138 #ifndef G_OS_WIN32
139                         int res = 0;
140                         pid_t pid = 0;
141         
142                         TEXTVIEW_INSERT(_("\n  Importing key ID "));
143                         TEXTVIEW_INSERT(sig->fpr);
144                         TEXTVIEW_INSERT(":\n\n");
145
146                         main_window_cursor_wait(mainwindow_get_mainwindow());
147                         GTK_EVENTS_FLUSH();
148
149                         pid = fork();
150                         if (pid == -1) {
151                                 res = -1;
152                         } else if (pid == 0) {
153                                 /* son */
154                                 res = system(cmd);
155                                 res = WEXITSTATUS(res);
156                                 _exit(res);
157                         } else {
158                                 int status = 0;
159                                 time_t start_wait = time(NULL);
160                                 res = -1;
161                                 do {
162                                         if (waitpid(pid, &status, WNOHANG) == 0 || !WIFEXITED(status)) {
163                                                 usleep(200000);
164                                         } else {
165                                                 res = WEXITSTATUS(status);
166                                                 break;
167                                         }
168                                         if (time(NULL) - start_wait > 5) {
169                                                 debug_print("SIGTERM'ing gpg\n");
170                                                 kill(pid, SIGTERM);
171                                         }
172                                         if (time(NULL) - start_wait > 6) {
173                                                 debug_print("SIGKILL'ing gpg\n");
174                                                 kill(pid, SIGKILL);
175                                                 break;
176                                         }
177                                 } while(1);
178                         }
179                         main_window_cursor_normal(mainwindow_get_mainwindow());
180                         if (res == 0) {
181                                 TEXTVIEW_INSERT(_("   This key has been imported to your keyring.\n"));
182                         } else {
183                                 TEXTVIEW_INSERT(_("   This key couldn't be imported to your keyring.\n"));
184                                 TEXTVIEW_INSERT(_("   You can try to import it manually with the command:\n\n     "));
185                                 TEXTVIEW_INSERT(cmd);
186                         }
187 #else
188                         TEXTVIEW_INSERT(_("   This key is not in your keyring.\n"));
189                         TEXTVIEW_INSERT(_("   Key import isn't implemented in Windows.\n"));
190 #endif
191                 }
192                 g_free(cmd);
193                 return;
194         } else {
195                 TEXTVIEW_INSERT(_("\n  Key ID "));
196                 TEXTVIEW_INSERT(sig->fpr);
197                 TEXTVIEW_INSERT(":\n\n");
198                 TEXTVIEW_INSERT(_("   This key is in your keyring.\n"));
199         }
200         gpgme_data_release(sigdata);
201         gpgme_release(ctx);
202         textview_show_icon(textview, GTK_STOCK_DIALOG_AUTHENTICATION);
203 }
204
205
206 static void pgp_show_mimepart(MimeViewer *_viewer,
207                                 const gchar *infile,
208                                 MimeInfo *partinfo)
209 {
210         PgpViewer *viewer = (PgpViewer *)_viewer;
211         debug_print("pgp_show_mimepart\n");
212         viewer->textview->messageview = _viewer->mimeview->messageview;
213         pgpview_show_mime_part(viewer->textview, partinfo);
214 }
215
216 static void pgp_clear_viewer(MimeViewer *_viewer)
217 {
218         PgpViewer *viewer = (PgpViewer *)_viewer;
219         debug_print("pgp_clear_viewer\n");
220         textview_clear(viewer->textview);
221 }
222
223 static void pgp_destroy_viewer(MimeViewer *_viewer)
224 {
225         PgpViewer *viewer = (PgpViewer *)_viewer;
226         debug_print("pgp_destroy_viewer\n");
227         textview_destroy(viewer->textview);
228 }
229
230 static MimeViewer *pgp_viewer_create(void)
231 {
232         PgpViewer *viewer;
233
234         debug_print("pgp_viewer_create\n");
235         
236         viewer = g_new0(PgpViewer, 1);
237         viewer->mimeviewer.factory = &pgp_viewer_factory;
238         viewer->mimeviewer.get_widget = pgp_get_widget;
239         viewer->mimeviewer.show_mimepart = pgp_show_mimepart;
240         viewer->mimeviewer.clear_viewer = pgp_clear_viewer;
241         viewer->mimeviewer.destroy_viewer = pgp_destroy_viewer; 
242         viewer->mimeviewer.get_selection = NULL;
243         viewer->textview = textview_create();
244         textview_init(viewer->textview);
245
246         gtk_widget_show_all(viewer->textview->vbox);
247
248         return (MimeViewer *) viewer;
249 }
250
251 static MimeViewerFactory pgp_viewer_factory =
252 {
253         content_types,  
254         0,
255
256         pgp_viewer_create,
257 };
258
259 void pgp_viewer_init(void)
260 {
261         mimeview_register_viewer_factory(&pgp_viewer_factory);
262 }
263
264 void pgp_viewer_done(void)
265 {
266         mimeview_unregister_viewer_factory(&pgp_viewer_factory);
267
268 }