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