2006-10-12 [colin] 2.5.5cvs3
[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                                 _exit(res);
151                         } else {
152                                 int status = 0;
153                                 time_t start_wait = time(NULL);
154                                 res = -1;
155                                 do {
156                                         if (waitpid(pid, &status, WNOHANG) == 0 || !WIFEXITED(status)) {
157                                                 usleep(200000);
158                                         } else {
159                                                 res = WEXITSTATUS(status);
160                                                 break;
161                                         }
162                                         if (time(NULL) - start_wait > 5) {
163                                                 debug_print("SIGTERM'ing gpg\n");
164                                                 kill(pid, SIGTERM);
165                                         }
166                                         if (time(NULL) - start_wait > 6) {
167                                                 debug_print("SIGKILL'ing gpg\n");
168                                                 kill(pid, SIGKILL);
169                                                 break;
170                                         }
171                                 } while(1);
172                         }
173                         main_window_cursor_normal(mainwindow_get_mainwindow());
174                         if (res == 0) {
175                                 TEXTVIEW_INSERT(_("   This key has been imported to your keyring.\n"));
176                         } else {
177                                 TEXTVIEW_INSERT(_("   This key couldn't be imported to your keyring.\n"));
178                                 TEXTVIEW_INSERT(_("   You can try to import it manually with the command:\n\n     "));
179                                 TEXTVIEW_INSERT(cmd);
180                         }
181 #else
182                         TEXTVIEW_INSERT(_("   This key is not in your keyring.\n"));
183                         TEXTVIEW_INSERT(_("   Key import isn't implemented in Windows.\n"));
184 #endif
185                 }
186                 g_free(cmd);
187                 return;
188         } else {
189                 TEXTVIEW_INSERT(_("\n  Key ID "));
190                 TEXTVIEW_INSERT(sig->fpr);
191                 TEXTVIEW_INSERT(":\n\n");
192                 TEXTVIEW_INSERT(_("   This key is in your keyring.\n"));
193         }
194         gpgme_data_release(sigdata);
195         gpgme_release(ctx);
196         textview_show_icon(textview, GTK_STOCK_DIALOG_AUTHENTICATION);
197 }
198
199
200 static void pgp_show_mimepart(MimeViewer *_viewer,
201                                 const gchar *infile,
202                                 MimeInfo *partinfo)
203 {
204         PgpViewer *viewer = (PgpViewer *)_viewer;
205         debug_print("pgp_show_mimepart\n");
206         viewer->textview->messageview = _viewer->mimeview->messageview;
207         pgpview_show_mime_part(viewer->textview, partinfo);
208 }
209
210 static void pgp_clear_viewer(MimeViewer *_viewer)
211 {
212         PgpViewer *viewer = (PgpViewer *)_viewer;
213         debug_print("pgp_clear_viewer\n");
214         textview_clear(viewer->textview);
215 }
216
217 static void pgp_destroy_viewer(MimeViewer *_viewer)
218 {
219         PgpViewer *viewer = (PgpViewer *)_viewer;
220         debug_print("pgp_destroy_viewer\n");
221         textview_destroy(viewer->textview);
222 }
223
224 static MimeViewer *pgp_viewer_create(void)
225 {
226         PgpViewer *viewer;
227
228         debug_print("pgp_viewer_create\n");
229         
230         viewer = g_new0(PgpViewer, 1);
231         viewer->mimeviewer.factory = &pgp_viewer_factory;
232         viewer->mimeviewer.get_widget = pgp_get_widget;
233         viewer->mimeviewer.show_mimepart = pgp_show_mimepart;
234         viewer->mimeviewer.clear_viewer = pgp_clear_viewer;
235         viewer->mimeviewer.destroy_viewer = pgp_destroy_viewer; 
236         viewer->mimeviewer.get_selection = NULL;
237         viewer->textview = textview_create();
238         textview_init(viewer->textview);
239
240         gtk_widget_show_all(viewer->textview->vbox);
241
242         return (MimeViewer *) viewer;
243 }
244
245 static MimeViewerFactory pgp_viewer_factory =
246 {
247         content_types,  
248         0,
249
250         pgp_viewer_create,
251 };
252
253 void pgp_viewer_init(void)
254 {
255         mimeview_register_viewer_factory(&pgp_viewer_factory);
256 }
257
258 void pgp_viewer_done(void)
259 {
260         mimeview_unregister_viewer_factory(&pgp_viewer_factory);
261
262 }