Added --batch option to manual gpg executable calls.
[claws.git] / src / plugins / pgpcore / pgp_viewer.c
index d5e2a38f618fceca3e222a83e4a4a85f87ddeebd..d1b0448a56d5ecb323744c33f61c9c182d57b969 100644 (file)
@@ -1,11 +1,11 @@
 /*
- * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
- * Copyright (C) 1999-2006 Hiroyuki Yamamoto & the Sylpheed-Claws team
+ * Claws Mail -- a GTK+ based, lightweight, and fast e-mail client
+ * Copyright (C) 1999-2012 the Claws Mail team
  * This file Copyright (C) 2006 Colin Leroy <colin@colino.net>
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
+ * the Free Software Foundation; either version 3 of the License, or
  * (at your option) any later version.
  *
  * This program is distributed in the hope that it will be useful,
  * GNU General Public License for more details.
  *
  * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ * 
  */
 
 #ifdef HAVE_CONFIG_H
 #  include "config.h"
+#include "claws-features.h"
 #endif
 
 #include <stddef.h>
 #include <glib.h>
 #include <glib/gi18n.h>
 #include <sys/types.h>
-#include <sys/wait.h>
-#if (defined(__DragonFly__) || defined (__NetBSD__) || defined (__FreeBSD__) || defined (__OpenBSD__))
+#ifndef G_OS_WIN32
+#  include <sys/wait.h>
+#else
+#  include <pthread.h>
+#  include <windows.h>
+#endif
+#if (defined(__DragonFly__) || defined(SOLARIS) || defined (__NetBSD__) || defined (__FreeBSD__) || defined (__OpenBSD__))
 #  include <sys/signal.h>
 #endif
 
 #include "version.h"
-#include "common/sylpheed.h"
+#include "common/claws.h"
 #include "mainwindow.h"
 #include "mimeview.h"
 #include "textview.h"
@@ -64,6 +70,48 @@ static GtkWidget *pgp_get_widget(MimeViewer *_viewer)
        return GTK_WIDGET(viewer->textview->vbox);
 }
 
+#ifdef G_OS_WIN32
+struct _ImportCtx {
+       gboolean done;
+       gchar *cmd;
+       DWORD exitcode;
+};
+
+static void *_import_threaded(void *arg)
+{
+       struct _ImportCtx *ctx = (struct _ImportCtx *)arg;
+       gboolean result;
+
+       PROCESS_INFORMATION pi = {0};
+       STARTUPINFO si = {0};
+
+       result = CreateProcess(NULL, ctx->cmd, NULL, NULL, FALSE,
+                       NORMAL_PRIORITY_CLASS | CREATE_NO_WINDOW,
+                       NULL, NULL, &si, &pi);
+
+       if (!result) {
+               debug_print("Couldn't execute '%s'\n", ctx->cmd);
+       } else {
+               WaitForSingleObject(pi.hProcess, 10000);
+               result = GetExitCodeProcess(pi.hProcess, &ctx->exitcode);
+               if (ctx->exitcode == STILL_ACTIVE) {
+                       debug_print("Process still running, terminating it.\n");
+                       TerminateProcess(pi.hProcess, 255);
+               }
+
+               CloseHandle(pi.hProcess);
+               CloseHandle(pi.hThread);
+
+               if (!result) {
+                       debug_print("Process executed, but we couldn't get its exit code (huh?)\n");
+               }
+       }
+
+       ctx->done = TRUE;
+       return NULL;
+}
+#endif
+
 static void pgpview_show_mime_part(TextView *textview, MimeInfo *partinfo)
 {
        GtkTextView *text;
@@ -75,8 +123,9 @@ static void pgpview_show_mime_part(TextView *textview, MimeInfo *partinfo)
        gpgme_key_t key = NULL;
        gpgme_signature_t sig = NULL;
        gpgme_error_t err = 0;
-       if (!partinfo) return;
+       gboolean imported = FALSE;
 
+       if (!partinfo) return;
        
        textview_set_font(textview, NULL);
        textview_clear(textview);
@@ -98,7 +147,11 @@ static void pgpview_show_mime_part(TextView *textview, MimeInfo *partinfo)
                textview_show_mime_part(textview, partinfo);
                return;
        }
-       sigstatus = sgpgme_verify_signature(ctx, sigdata, sigdata, NULL);
+
+       /* Here we do not care about what data we attempt to verify with the
+        * signature, or about result of the verification - all we care about
+        * is that we find out ID of the key used to make this signature. */
+       sigstatus = sgpgme_verify_signature(ctx, sigdata, NULL, sigdata);
        if (!sigstatus || sigstatus == GINT_TO_POINTER(-GPG_ERR_SYSTEM_ERROR)) {
                g_warning("no sigstatus");
                textview_show_mime_part(textview, partinfo);
@@ -112,12 +165,14 @@ static void pgpview_show_mime_part(TextView *textview, MimeInfo *partinfo)
        }
        gpgme_get_key(ctx, sig->fpr, &key, 0);
        if (!key) {
-               gchar *cmd = g_strdup_printf("gpg --no-tty --recv-keys %s", sig->fpr);
+               gchar *gpgbin = get_gpg_executable_name();
+               gchar *cmd = g_strdup_printf("\"%s\" --batch --no-tty --recv-keys %s",
+                               (gpgbin ? gpgbin : "gpg"), sig->fpr);
                AlertValue val = G_ALERTDEFAULT;
-               if (!prefs_common.work_offline) {
+               if (!prefs_common_get_prefs()->work_offline) {
                        val = alertpanel(_("Key import"),
                                _("This key is not in your keyring. Do you want "
-                                 "Sylpheed-Claws to try and import it from a "
+                                 "Claws Mail to try and import it from a "
                                  "keyserver?"),
                                  GTK_STOCK_NO, "+" GTK_STOCK_YES, NULL);
                        GTK_EVENTS_FLUSH();
@@ -128,30 +183,32 @@ static void pgpview_show_mime_part(TextView *textview, MimeInfo *partinfo)
                        TEXTVIEW_INSERT(":\n\n");
                        TEXTVIEW_INSERT(_("   This key is not in your keyring.\n"));
                        TEXTVIEW_INSERT(_("   It should be possible to import it "));
-                       if (prefs_common.work_offline)
+                       if (prefs_common_get_prefs()->work_offline)
                                TEXTVIEW_INSERT(_("when working online,\n   or "));
                        TEXTVIEW_INSERT(_("with the following command: \n\n     "));
                        TEXTVIEW_INSERT(cmd);
                } else {
-#ifndef G_OS_WIN32
-                       int res = 0;
-                       pid_t pid = 0;
-       
                        TEXTVIEW_INSERT(_("\n  Importing key ID "));
                        TEXTVIEW_INSERT(sig->fpr);
                        TEXTVIEW_INSERT(":\n\n");
 
                        main_window_cursor_wait(mainwindow_get_mainwindow());
+                       textview_cursor_wait(textview);
                        GTK_EVENTS_FLUSH();
 
+#ifndef G_OS_WIN32
+                       int res = 0;
+                       pid_t pid = 0;
+
                        pid = fork();
                        if (pid == -1) {
                                res = -1;
                        } else if (pid == 0) {
                                /* son */
-                               res = system(cmd);
-                               res = WEXITSTATUS(res);
-                               _exit(res);
+                               gchar **argv;
+                               argv = strsplit_with_quote(cmd, " ", 0);
+                               res = execvp(argv[0], argv);
+                               exit(255);
                        } else {
                                int status = 0;
                                time_t start_wait = time(NULL);
@@ -163,29 +220,58 @@ static void pgpview_show_mime_part(TextView *textview, MimeInfo *partinfo)
                                                res = WEXITSTATUS(status);
                                                break;
                                        }
-                                       if (time(NULL) - start_wait > 5) {
-                                               debug_print("SIGTERM'ing gpg\n");
+                                       if (time(NULL) - start_wait > 9) {
+                                               debug_print("SIGTERM'ing gpg %d\n", pid);
                                                kill(pid, SIGTERM);
                                        }
-                                       if (time(NULL) - start_wait > 6) {
-                                               debug_print("SIGKILL'ing gpg\n");
+                                       if (time(NULL) - start_wait > 10) {
+                                               debug_print("SIGKILL'ing gpg %d\n", pid);
                                                kill(pid, SIGKILL);
                                                break;
                                        }
                                } while(1);
                        }
+                       debug_print("res %d\n", res);
+                       if (res == 0)
+                               imported = TRUE;
+#else
+                       /* We need to call gpg in a separate thread, so that waiting for
+                        * it to finish does not block the UI. */
+                       pthread_t pt;
+                       struct _ImportCtx *ctx = malloc(sizeof(struct _ImportCtx));
+
+                       ctx->done = FALSE;
+                       ctx->exitcode = STILL_ACTIVE;
+                       ctx->cmd = cmd;
+
+                       if (pthread_create(&pt, PTHREAD_CREATE_JOINABLE,
+                                               _import_threaded, (void *)ctx) != 0) {
+                               debug_print("Couldn't create thread, continuing unthreaded.\n");
+                               _import_threaded(ctx);
+                       } else {
+                               debug_print("Thread created, waiting for it to finish...\n");
+                               while (!ctx->done)
+                                       claws_do_idle();
+                       }
+
+                       debug_print("Thread finished.\n");
+                       pthread_join(pt, NULL);
+
+                       if (ctx->exitcode == 0) {
+                               imported = TRUE;
+                       }
+                       g_free(ctx);
+#endif
                        main_window_cursor_normal(mainwindow_get_mainwindow());
-                       if (res == 0) {
+                       textview_cursor_normal(textview);
+                       if (imported) {
                                TEXTVIEW_INSERT(_("   This key has been imported to your keyring.\n"));
                        } else {
                                TEXTVIEW_INSERT(_("   This key couldn't be imported to your keyring.\n"));
+                               TEXTVIEW_INSERT(_("   Key servers are sometimes slow.\n"));
                                TEXTVIEW_INSERT(_("   You can try to import it manually with the command:\n\n     "));
                                TEXTVIEW_INSERT(cmd);
                        }
-#else
-                       TEXTVIEW_INSERT(_("   This key is not in your keyring.\n"));
-                       TEXTVIEW_INSERT(_("   Key import isn't implemented in Windows.\n"));
-#endif
                }
                g_free(cmd);
                return;