2 * Claws Mail -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2012 the Claws Mail team
4 * This file Copyright (C) 2006 Colin Leroy <colin@colino.net>
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.
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.
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/>.
23 #include "claws-features.h"
28 #include <glib/gi18n.h>
29 #include <sys/types.h>
31 # include <sys/wait.h>
36 #if (defined(__DragonFly__) || defined(SOLARIS) || defined (__NetBSD__) || defined (__FreeBSD__) || defined (__OpenBSD__))
37 # include <sys/signal.h>
41 #include "common/claws.h"
42 #include "mainwindow.h"
46 #include "prefs_common.h"
47 #include "prefs_gpg.h"
48 #include "alertpanel.h"
51 typedef struct _PgpViewer PgpViewer;
53 static MimeViewerFactory pgp_viewer_factory;
57 MimeViewer mimeviewer;
61 static gchar *content_types[] =
62 {"application/pgp-signature", NULL};
64 static GtkWidget *pgp_get_widget(MimeViewer *_viewer)
66 PgpViewer *viewer = (PgpViewer *) _viewer;
68 debug_print("pgp_get_widget\n");
70 return GTK_WIDGET(viewer->textview->vbox);
80 static void *_import_threaded(void *arg)
82 struct _ImportCtx *ctx = (struct _ImportCtx *)arg;
85 PROCESS_INFORMATION pi = {0};
88 result = CreateProcess(NULL, ctx->cmd, NULL, NULL, FALSE,
89 NORMAL_PRIORITY_CLASS | CREATE_NO_WINDOW,
90 NULL, NULL, &si, &pi);
93 debug_print("Couldn't execute '%s'\n", ctx->cmd);
95 WaitForSingleObject(pi.hProcess, 10000);
96 result = GetExitCodeProcess(pi.hProcess, &ctx->exitcode);
97 if (ctx->exitcode == STILL_ACTIVE) {
98 debug_print("Process still running, terminating it.\n");
99 TerminateProcess(pi.hProcess, 255);
102 CloseHandle(pi.hProcess);
103 CloseHandle(pi.hThread);
106 debug_print("Process executed, but we couldn't get its exit code (huh?)\n");
115 static void pgpview_show_mime_part(TextView *textview, MimeInfo *partinfo)
118 GtkTextBuffer *buffer;
120 gpgme_data_t sigdata = NULL;
121 gpgme_verify_result_t sigstatus = NULL;
122 gpgme_ctx_t ctx = NULL;
123 gpgme_key_t key = NULL;
124 gpgme_signature_t sig = NULL;
125 gpgme_error_t err = 0;
126 gboolean imported = FALSE;
128 if (!partinfo) return;
130 textview_set_font(textview, NULL);
131 textview_clear(textview);
133 text = GTK_TEXT_VIEW(textview->text);
134 buffer = gtk_text_view_get_buffer(text);
135 gtk_text_buffer_get_start_iter(buffer, &iter);
137 err = gpgme_new (&ctx);
139 debug_print("err : %s\n", gpgme_strerror(err));
140 textview_show_mime_part(textview, partinfo);
144 sigdata = sgpgme_data_from_mimeinfo(partinfo);
146 g_warning("no sigdata");
147 textview_show_mime_part(textview, partinfo);
151 /* Here we do not care about what data we attempt to verify with the
152 * signature, or about result of the verification - all we care about
153 * is that we find out ID of the key used to make this signature. */
154 sigstatus = sgpgme_verify_signature(ctx, sigdata, NULL, sigdata);
155 if (!sigstatus || sigstatus == GINT_TO_POINTER(-GPG_ERR_SYSTEM_ERROR)) {
156 g_warning("no sigstatus");
157 textview_show_mime_part(textview, partinfo);
160 sig = sigstatus->signatures;
163 textview_show_mime_part(textview, partinfo);
166 gpgme_get_key(ctx, sig->fpr, &key, 0);
168 gchar *gpgbin = get_gpg_executable_name();
169 gchar *cmd = g_strdup_printf("\"%s\" --batch --no-tty --recv-keys %s",
170 (gpgbin ? gpgbin : "gpg"), sig->fpr);
171 AlertValue val = G_ALERTDEFAULT;
172 if (!prefs_common_get_prefs()->work_offline) {
173 val = alertpanel(_("Key import"),
174 _("This key is not in your keyring. Do you want "
175 "Claws Mail to try and import it from a "
177 GTK_STOCK_NO, GTK_STOCK_YES, NULL, ALERTFOCUS_SECOND);
180 if (val == G_ALERTDEFAULT) {
181 TEXTVIEW_INSERT(_("\n Key ID "));
182 TEXTVIEW_INSERT(sig->fpr);
183 TEXTVIEW_INSERT(":\n\n");
184 TEXTVIEW_INSERT(_(" This key is not in your keyring.\n"));
185 TEXTVIEW_INSERT(_(" It should be possible to import it "));
186 if (prefs_common_get_prefs()->work_offline)
187 TEXTVIEW_INSERT(_("when working online,\n or "));
188 TEXTVIEW_INSERT(_("with the following command: \n\n "));
189 TEXTVIEW_INSERT(cmd);
191 TEXTVIEW_INSERT(_("\n Importing key ID "));
192 TEXTVIEW_INSERT(sig->fpr);
193 TEXTVIEW_INSERT(":\n\n");
195 main_window_cursor_wait(mainwindow_get_mainwindow());
196 textview_cursor_wait(textview);
206 } else if (pid == 0) {
209 argv = strsplit_with_quote(cmd, " ", 0);
210 res = execvp(argv[0], argv);
215 time_t start_wait = time(NULL);
218 if (waitpid(pid, &status, WNOHANG) == 0 || !WIFEXITED(status)) {
221 res = WEXITSTATUS(status);
224 if (time(NULL) - start_wait > 9) {
225 debug_print("SIGTERM'ing gpg %d\n", pid);
228 if (time(NULL) - start_wait > 10) {
229 debug_print("SIGKILL'ing gpg %d\n", pid);
235 debug_print("res %d\n", res);
239 /* We need to call gpg in a separate thread, so that waiting for
240 * it to finish does not block the UI. */
242 struct _ImportCtx *ctx = malloc(sizeof(struct _ImportCtx));
245 ctx->exitcode = STILL_ACTIVE;
248 if (pthread_create(&pt, NULL,
249 _import_threaded, (void *)ctx) != 0) {
250 debug_print("Couldn't create thread, continuing unthreaded.\n");
251 _import_threaded(ctx);
253 debug_print("Thread created, waiting for it to finish...\n");
258 debug_print("Thread finished.\n");
259 pthread_join(pt, NULL);
261 if (ctx->exitcode == 0) {
266 main_window_cursor_normal(mainwindow_get_mainwindow());
267 textview_cursor_normal(textview);
269 TEXTVIEW_INSERT(_(" This key has been imported to your keyring.\n"));
271 TEXTVIEW_INSERT(_(" This key couldn't be imported to your keyring.\n"));
272 TEXTVIEW_INSERT(_(" Key servers are sometimes slow.\n"));
273 TEXTVIEW_INSERT(_(" You can try to import it manually with the command:\n\n "));
274 TEXTVIEW_INSERT(cmd);
280 TEXTVIEW_INSERT(_("\n Key ID "));
282 #if defined GPGME_VERSION_NUMBER && GPGME_VERSION_NUMBER >= 0x010700
283 TEXTVIEW_INSERT(key->fpr);
285 TEXTVIEW_INSERT(sig->fpr);
288 TEXTVIEW_INSERT(":\n\n");
289 TEXTVIEW_INSERT(_(" This key is in your keyring.\n"));
291 gpgme_data_release(sigdata);
293 textview_show_icon(textview, GTK_STOCK_DIALOG_AUTHENTICATION);
297 static void pgp_show_mimepart(MimeViewer *_viewer,
301 PgpViewer *viewer = (PgpViewer *)_viewer;
302 debug_print("pgp_show_mimepart\n");
303 viewer->textview->messageview = _viewer->mimeview->messageview;
304 pgpview_show_mime_part(viewer->textview, partinfo);
307 static void pgp_clear_viewer(MimeViewer *_viewer)
309 PgpViewer *viewer = (PgpViewer *)_viewer;
310 debug_print("pgp_clear_viewer\n");
311 textview_clear(viewer->textview);
314 static void pgp_destroy_viewer(MimeViewer *_viewer)
316 PgpViewer *viewer = (PgpViewer *)_viewer;
317 debug_print("pgp_destroy_viewer\n");
318 textview_destroy(viewer->textview);
321 static MimeViewer *pgp_viewer_create(void)
325 debug_print("pgp_viewer_create\n");
327 viewer = g_new0(PgpViewer, 1);
328 viewer->mimeviewer.factory = &pgp_viewer_factory;
329 viewer->mimeviewer.get_widget = pgp_get_widget;
330 viewer->mimeviewer.show_mimepart = pgp_show_mimepart;
331 viewer->mimeviewer.clear_viewer = pgp_clear_viewer;
332 viewer->mimeviewer.destroy_viewer = pgp_destroy_viewer;
333 viewer->mimeviewer.get_selection = NULL;
334 viewer->textview = textview_create();
335 textview_init(viewer->textview);
337 gtk_widget_show_all(viewer->textview->vbox);
339 return (MimeViewer *) viewer;
342 static MimeViewerFactory pgp_viewer_factory =
350 void pgp_viewer_init(void)
352 mimeview_register_viewer_factory(&pgp_viewer_factory);
355 void pgp_viewer_done(void)
357 mimeview_unregister_viewer_factory(&pgp_viewer_factory);