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);
73 static gchar *_get_gpg_executable_name()
75 gpgme_engine_info_t e;
77 if (!gpgme_get_engine_info(&e)) {
79 if (e->protocol == GPGME_PROTOCOL_OpenPGP
80 && e->file_name != NULL) {
81 debug_print("Found gpg executable: '%s'\n", e->file_name);
97 static void *_import_threaded(void *arg)
99 struct _ImportCtx *ctx = (struct _ImportCtx *)arg;
102 PROCESS_INFORMATION pi = {0};
103 STARTUPINFO si = {0};
105 result = CreateProcess(NULL, ctx->cmd, NULL, NULL, FALSE,
106 NORMAL_PRIORITY_CLASS | CREATE_NO_WINDOW,
107 NULL, NULL, &si, &pi);
110 debug_print("Couldn't execute '%s'\n", ctx->cmd);
112 WaitForSingleObject(pi.hProcess, 10000);
113 result = GetExitCodeProcess(pi.hProcess, &ctx->exitcode);
114 if (ctx->exitcode == STILL_ACTIVE) {
115 debug_print("Process still running, terminating it.\n");
116 TerminateProcess(pi.hProcess, 255);
119 CloseHandle(pi.hProcess);
120 CloseHandle(pi.hThread);
123 debug_print("Process executed, but we couldn't get its exit code (huh?)\n");
132 static void pgpview_show_mime_part(TextView *textview, MimeInfo *partinfo)
135 GtkTextBuffer *buffer;
137 gpgme_data_t sigdata = NULL;
138 gpgme_verify_result_t sigstatus = NULL;
139 gpgme_ctx_t ctx = NULL;
140 gpgme_key_t key = NULL;
141 gpgme_signature_t sig = NULL;
142 gpgme_error_t err = 0;
143 gboolean imported = FALSE;
145 if (!partinfo) return;
147 textview_set_font(textview, NULL);
148 textview_clear(textview);
150 text = GTK_TEXT_VIEW(textview->text);
151 buffer = gtk_text_view_get_buffer(text);
152 gtk_text_buffer_get_start_iter(buffer, &iter);
154 err = gpgme_new (&ctx);
156 debug_print("err : %s\n", gpgme_strerror(err));
157 textview_show_mime_part(textview, partinfo);
161 sigdata = sgpgme_data_from_mimeinfo(partinfo);
163 g_warning("no sigdata");
164 textview_show_mime_part(textview, partinfo);
168 /* Here we do not care about what data we attempt to verify with the
169 * signature, or about result of the verification - all we care about
170 * is that we find out ID of the key used to make this signature. */
171 sigstatus = sgpgme_verify_signature(ctx, sigdata, NULL, sigdata);
172 if (!sigstatus || sigstatus == GINT_TO_POINTER(-GPG_ERR_SYSTEM_ERROR)) {
173 g_warning("no sigstatus");
174 textview_show_mime_part(textview, partinfo);
177 sig = sigstatus->signatures;
180 textview_show_mime_part(textview, partinfo);
183 gpgme_get_key(ctx, sig->fpr, &key, 0);
185 gchar *gpgbin = _get_gpg_executable_name();
186 gchar *cmd = g_strdup_printf("\"%s\" --no-tty --recv-keys %s",
187 (gpgbin ? gpgbin : "gpg"), sig->fpr);
188 AlertValue val = G_ALERTDEFAULT;
189 if (!prefs_common_get_prefs()->work_offline) {
190 val = alertpanel(_("Key import"),
191 _("This key is not in your keyring. Do you want "
192 "Claws Mail to try and import it from a "
194 GTK_STOCK_NO, "+" GTK_STOCK_YES, NULL);
197 if (val == G_ALERTDEFAULT) {
198 TEXTVIEW_INSERT(_("\n Key ID "));
199 TEXTVIEW_INSERT(sig->fpr);
200 TEXTVIEW_INSERT(":\n\n");
201 TEXTVIEW_INSERT(_(" This key is not in your keyring.\n"));
202 TEXTVIEW_INSERT(_(" It should be possible to import it "));
203 if (prefs_common_get_prefs()->work_offline)
204 TEXTVIEW_INSERT(_("when working online,\n or "));
205 TEXTVIEW_INSERT(_("with the following command: \n\n "));
206 TEXTVIEW_INSERT(cmd);
208 TEXTVIEW_INSERT(_("\n Importing key ID "));
209 TEXTVIEW_INSERT(sig->fpr);
210 TEXTVIEW_INSERT(":\n\n");
212 main_window_cursor_wait(mainwindow_get_mainwindow());
213 textview_cursor_wait(textview);
223 } else if (pid == 0) {
226 argv = strsplit_with_quote(cmd, " ", 0);
227 res = execvp(argv[0], argv);
231 time_t start_wait = time(NULL);
234 if (waitpid(pid, &status, WNOHANG) == 0 || !WIFEXITED(status)) {
237 res = WEXITSTATUS(status);
240 if (time(NULL) - start_wait > 9) {
241 debug_print("SIGTERM'ing gpg %d\n", pid);
244 if (time(NULL) - start_wait > 10) {
245 debug_print("SIGKILL'ing gpg %d\n", pid);
251 debug_print("res %d\n", res);
255 /* We need to call gpg in a separate thread, so that waiting for
256 * it to finish does not block the UI. */
258 struct _ImportCtx *ctx = malloc(sizeof(struct _ImportCtx));
261 ctx->exitcode = STILL_ACTIVE;
264 if (pthread_create(&pt, PTHREAD_CREATE_JOINABLE,
265 _import_threaded, (void *)ctx) != 0) {
266 debug_print("Couldn't create thread, continuing unthreaded.\n");
267 _import_threaded(ctx);
269 debug_print("Thread created, waiting for it to finish...\n");
274 debug_print("Thread finished.\n");
275 pthread_join(pt, NULL);
277 if (ctx->exitcode == 0) {
282 main_window_cursor_normal(mainwindow_get_mainwindow());
283 textview_cursor_normal(textview);
285 TEXTVIEW_INSERT(_(" This key has been imported to your keyring.\n"));
287 TEXTVIEW_INSERT(_(" This key couldn't be imported to your keyring.\n"));
288 TEXTVIEW_INSERT(_(" Key servers are sometimes slow.\n"));
289 TEXTVIEW_INSERT(_(" You can try to import it manually with the command:\n\n "));
290 TEXTVIEW_INSERT(cmd);
296 TEXTVIEW_INSERT(_("\n Key ID "));
297 TEXTVIEW_INSERT(sig->fpr);
298 TEXTVIEW_INSERT(":\n\n");
299 TEXTVIEW_INSERT(_(" This key is in your keyring.\n"));
301 gpgme_data_release(sigdata);
303 textview_show_icon(textview, GTK_STOCK_DIALOG_AUTHENTICATION);
307 static void pgp_show_mimepart(MimeViewer *_viewer,
311 PgpViewer *viewer = (PgpViewer *)_viewer;
312 debug_print("pgp_show_mimepart\n");
313 viewer->textview->messageview = _viewer->mimeview->messageview;
314 pgpview_show_mime_part(viewer->textview, partinfo);
317 static void pgp_clear_viewer(MimeViewer *_viewer)
319 PgpViewer *viewer = (PgpViewer *)_viewer;
320 debug_print("pgp_clear_viewer\n");
321 textview_clear(viewer->textview);
324 static void pgp_destroy_viewer(MimeViewer *_viewer)
326 PgpViewer *viewer = (PgpViewer *)_viewer;
327 debug_print("pgp_destroy_viewer\n");
328 textview_destroy(viewer->textview);
331 static MimeViewer *pgp_viewer_create(void)
335 debug_print("pgp_viewer_create\n");
337 viewer = g_new0(PgpViewer, 1);
338 viewer->mimeviewer.factory = &pgp_viewer_factory;
339 viewer->mimeviewer.get_widget = pgp_get_widget;
340 viewer->mimeviewer.show_mimepart = pgp_show_mimepart;
341 viewer->mimeviewer.clear_viewer = pgp_clear_viewer;
342 viewer->mimeviewer.destroy_viewer = pgp_destroy_viewer;
343 viewer->mimeviewer.get_selection = NULL;
344 viewer->textview = textview_create();
345 textview_init(viewer->textview);
347 gtk_widget_show_all(viewer->textview->vbox);
349 return (MimeViewer *) viewer;
352 static MimeViewerFactory pgp_viewer_factory =
360 void pgp_viewer_init(void)
362 mimeview_register_viewer_factory(&pgp_viewer_factory);
365 void pgp_viewer_done(void)
367 mimeview_unregister_viewer_factory(&pgp_viewer_factory);