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