Don't encrypt with a public key for which the email matches,
[claws.git] / src / plugins / pgpcore / select-keys.c
index e7dd5b4ab2447f09e489948c625b3c894c838664..423675c294e5881427810a050729ed0cb8695c8c 100644 (file)
@@ -1,5 +1,5 @@
 /* select-keys.c - GTK+ based key selection
- *      Copyright (C) 2001-2009 Werner Koch (dd9jn) and the Claws Mail team
+ *      Copyright (C) 2001-2012 Werner Koch (dd9jn) and the Claws Mail team
  *
  * 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
@@ -83,7 +83,7 @@ static void sort_keys (struct select_keys_s *sk, enum col_titles column);
 static void sort_keys_name (GtkWidget *widget, gpointer data);
 static void sort_keys_email (GtkWidget *widget, gpointer data);
 
-static gboolean use_untrusted (gpgme_key_t, gpgme_protocol_t proto);
+static gboolean use_untrusted (gpgme_key_t, gpgme_user_id_t uid, gpgme_protocol_t proto);
 
 static void
 update_progress (struct select_keys_s *sk, int running, const char *pattern)
@@ -191,11 +191,7 @@ set_row (GtkCMCList *clist, gpgme_key_t key, gpgme_protocol_t proto)
          gpgme_pubkey_algo_name(key->subkeys->pubkey_algo) );
     text[COL_ALGO] = algo_buf;
 
-    s = key->subkeys->keyid;
-    if (strlen (s) == 16)
-        s += 8; /* show only the short keyID */
-    text[COL_KEYID] = s;
-
+    text[COL_KEYID] = key->subkeys->keyid;
 
     s = key->uids->name;
     if (!s || !*s)
@@ -274,6 +270,7 @@ fill_clist (struct select_keys_s *sk, const char *pattern, gpgme_protocol_t prot
     int num_results = 0;
     gboolean exact_match = FALSE;
     gpgme_key_t last_key = NULL;
+    gpgme_user_id_t last_uid = NULL;
     cm_return_val_if_fail (sk, NULL);
     clist = sk->clist;
     cm_return_val_if_fail (clist, NULL);
@@ -311,10 +308,13 @@ fill_clist (struct select_keys_s *sk, const char *pattern, gpgme_protocol_t prot
 
                if (!uid->email)
                        continue;
+               if (uid->revoked || uid->invalid)
+                       continue;
                raw_mail = g_strdup(uid->email);
                extract_address(raw_mail);
                if (!strcasecmp(pattern, raw_mail)) {
                        exact_match = TRUE;
+                       last_uid = uid;
                        g_free(raw_mail);
                        break;
                }
@@ -330,7 +330,7 @@ fill_clist (struct select_keys_s *sk, const char *pattern, gpgme_protocol_t prot
  
     if (exact_match == TRUE && num_results == 1) {
            if (last_key->uids->validity < GPGME_VALIDITY_FULL && 
-               !use_untrusted(last_key, proto))
+               !use_untrusted(last_key, last_uid, proto))
                    exact_match = FALSE;
     }
 
@@ -363,7 +363,7 @@ create_dialog (struct select_keys_s *sk)
 
     g_assert (!sk->window);
     window = gtkut_window_new (GTK_WINDOW_TOPLEVEL, "select-keys");
-    gtk_widget_set_size_request (window, 520, 280);
+    gtk_widget_set_size_request (window, 560, 280);
     gtk_container_set_border_width (GTK_CONTAINER (window), 8);
     gtk_window_set_title (GTK_WINDOW (window), _("Select Keys"));
     gtk_window_set_modal (GTK_WINDOW (window), TRUE);
@@ -399,10 +399,10 @@ create_dialog (struct select_keys_s *sk)
 
     clist = gtk_cmclist_new_with_titles (N_COL_TITLES, (char**)titles);
     gtk_container_add (GTK_CONTAINER (scrolledwin), clist);
-    gtk_cmclist_set_column_width (GTK_CMCLIST(clist), COL_ALGO,      72);
-    gtk_cmclist_set_column_width (GTK_CMCLIST(clist), COL_KEYID,     76);
-    gtk_cmclist_set_column_width (GTK_CMCLIST(clist), COL_NAME,     130);
-    gtk_cmclist_set_column_width (GTK_CMCLIST(clist), COL_EMAIL,    130);
+    gtk_cmclist_set_column_width (GTK_CMCLIST(clist), COL_ALGO,      70);
+    gtk_cmclist_set_column_width (GTK_CMCLIST(clist), COL_KEYID,    120);
+    gtk_cmclist_set_column_width (GTK_CMCLIST(clist), COL_NAME,     115);
+    gtk_cmclist_set_column_width (GTK_CMCLIST(clist), COL_EMAIL,    140);
     gtk_cmclist_set_column_width (GTK_CMCLIST(clist), COL_VALIDITY,  20);
     gtk_cmclist_set_selection_mode (GTK_CMCLIST(clist), GTK_SELECTION_BROWSE);
     g_signal_connect (G_OBJECT(GTK_CMCLIST(clist)->column[COL_NAME].button),
@@ -487,7 +487,7 @@ key_pressed_cb (GtkWidget *widget, GdkEventKey *event, gpointer data)
     struct select_keys_s *sk = data;
 
     cm_return_val_if_fail (sk, FALSE);
-    if (event && event->keyval == GDK_Escape) {
+    if (event && event->keyval == GDK_KEY_Escape) {
         sk->okay = 0;
         gtk_main_quit ();
     }
@@ -511,8 +511,25 @@ select_btn_cb (GtkWidget *widget, gpointer data)
     row = GPOINTER_TO_INT(sk->clist->selection->data);
     key = gtk_cmclist_get_row_data(sk->clist, row);
     if (key) {
-        if ( key->uids->validity < GPGME_VALIDITY_FULL ) {
-            use_key = use_untrusted(key, sk->proto);
+        gpgme_user_id_t uid;
+       for (uid = key->uids; uid; uid = uid->next) {
+               gchar *raw_mail = NULL;
+
+               if (!uid->email)
+                       continue;
+               raw_mail = g_strdup(uid->email);
+               extract_address(raw_mail);
+               if (sk->pattern && !strcasecmp(sk->pattern, raw_mail)) {
+                       g_free(raw_mail);
+                       break;
+               }
+               g_free(raw_mail);
+       }
+       if (!uid)
+               uid = key->uids;
+
+        if ( uid->validity < GPGME_VALIDITY_FULL ) {
+            use_key = use_untrusted(key, uid, sk->proto);
             if (!use_key) {
                 debug_print ("** Key untrusted, will not encrypt");
                 return;
@@ -578,23 +595,26 @@ other_btn_cb (GtkWidget *widget, gpointer data)
 
 
 static gboolean
-use_untrusted (gpgme_key_t key, gpgme_protocol_t proto)
+use_untrusted (gpgme_key_t key, gpgme_user_id_t uid, gpgme_protocol_t proto)
 {
     AlertValue aval;
     gchar *buf = NULL;
-    
+    gchar *title = NULL;
     if (proto != GPGME_PROTOCOL_OpenPGP)
        return TRUE;
 
-    buf = g_strdup_printf(_("The key of '%s' is not fully trusted.\n"
-              "If you choose to encrypt the message with this key you don't\n"
-              "know for sure that it will go to the person you mean it to.\n"
-              "Do you trust it enough to use it anyway?"), key->uids->email);
+    title = g_strdup_printf(_("Encrypt to %s <%s>"), uid->name, uid->email);
+    buf = g_strdup_printf(_("This encryption key is not fully trusted.\n"
+              "If you choose to encrypt the message with this key, you don't\n"
+              "know for sure that it will go to the person you mean it to.\n\n"
+              "Key details: ID %s, primary identity %s &lt;%s&gt;\n\n"
+              "Do you trust this key enough to use it anyway?"), 
+              key->subkeys->keyid, key->uids->name, key->uids->email);
     aval = alertpanel
-           (_("Trust key"),
-            buf,
+           (title, buf,
             GTK_STOCK_NO, GTK_STOCK_YES, NULL);
     g_free(buf);
+    g_free(title);
     if (aval == G_ALERTALTERNATE)
        return TRUE;
     else