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