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