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