This function will be called to flip encryption on and off based on
message recipients.


 crypt.c      |  30 ++++++++++++++++++++++++++----
 mutt_crypt.h |  10 ++++++++--
 send.c       |   2 +-
 3 files changed, 35 insertions(+), 7 deletions(-)


# HG changeset patch
# User Kevin McCarthy <ke...@8t8.us>
# Date 1363403034 25200
# Branch HEAD
# Node ID 38da327f54ef5147c47c51e240381d1e716f8a06
# Parent  1e48ca012c852879e46b94bd56d463e3db838831
Implement crypt_opportunistic_encrypt().

This function will be called to flip encryption on and off based on
message recipients.

diff --git a/crypt.c b/crypt.c
--- a/crypt.c
+++ b/crypt.c
@@ -702,17 +702,17 @@
   mutt_unlink (tempfname);
 
   if ((WithCrypto & APPLICATION_PGP))
     unset_option (OPTDONTHANDLEPGPKEYS);
 }
 
 
 
-int crypt_get_keys (HEADER *msg, char **keylist)
+int crypt_get_keys (HEADER *msg, char **keylist, int auto_mode)
 {
   ADDRESS *adrlist = NULL, *last = NULL;
   const char *fqdn = mutt_fqdn (1);
 
   /* Do a quick check to make sure that we can find all of the encryption
    * keys if the user has requested this service.
    */
 
@@ -727,45 +727,67 @@
   rfc822_append (last ? &last : &adrlist, msg->env->bcc, 0);
 
   if (fqdn)
     rfc822_qualify (adrlist, fqdn);
   adrlist = mutt_remove_duplicates (adrlist);
 
   *keylist = NULL;
 
-  if (msg->security & ENCRYPT)
+  if (auto_mode || (msg->security & ENCRYPT))
   {
      if ((WithCrypto & APPLICATION_PGP)
          && (msg->security & APPLICATION_PGP))
      {
-       if ((*keylist = crypt_pgp_findkeys (adrlist, 0)) == NULL)
+       if ((*keylist = crypt_pgp_findkeys (adrlist, auto_mode)) == NULL)
        {
            rfc822_free_address (&adrlist);
            return (-1);
        }
        unset_option (OPTPGPCHECKTRUST);
      }
      if ((WithCrypto & APPLICATION_SMIME)
          && (msg->security & APPLICATION_SMIME))
      {
-       if ((*keylist = crypt_smime_findkeys (adrlist, 0)) == NULL)
+       if ((*keylist = crypt_smime_findkeys (adrlist, auto_mode)) == NULL)
        {
            rfc822_free_address (&adrlist);
            return (-1);
        }
      }
   }
 
   rfc822_free_address (&adrlist);
     
   return (0);
 }
 
 
+/*
+ * Check if all recipients keys can be automatically determined.
+ * Enable encryption if they can, otherwise disable encryption.
+ */
+
+void crypt_opportunistic_encrypt(HEADER *msg)
+{
+  char *pgpkeylist = NULL;
+
+  crypt_get_keys (msg, &pgpkeylist, 1);
+  if (pgpkeylist != NULL )
+  {
+    msg->security |= ENCRYPT;
+    FREE (&pgpkeylist);
+  }
+  else
+  {
+    msg->security &= ~ENCRYPT;
+  }
+}
+
+
 
 static void crypt_fetch_signatures (BODY ***signatures, BODY *a, int *n)
 {
   if (!WithCrypto)
     return;
 
   for (; a; a = a->next)
   {
diff --git a/mutt_crypt.h b/mutt_crypt.h
--- a/mutt_crypt.h
+++ b/mutt_crypt.h
@@ -135,18 +135,24 @@
    values if there are any. */
 int crypt_query (BODY *m);
 
 /* Fixme: To be documented. */
 void crypt_extract_keys_from_messages (HEADER *h);
 
 /* Do a quick check to make sure that we can find all of the
    encryption keys if the user has requested this service. 
-   Return the list of keys in KEYLIST. */
-int crypt_get_keys (HEADER *msg, char **keylist);
+   Return the list of keys in KEYLIST.
+   If auto_mode is true, only keys that can be determined without
+   prompting will be used.  */
+int crypt_get_keys (HEADER *msg, char **keylist, int auto_mode);
+
+/* Check if all recipients keys can be automatically determined.
+ * Enable encryption if they can, otherwise disable encryption.  */
+void crypt_opportunistic_encrypt(HEADER *msg);
 
 /* Forget a passphrase and display a message. */
 void crypt_forget_passphrase (void);
 
 /* Check that we have a usable passphrase, ask if not. */
 int crypt_valid_passphrase (int);
 
 /* Write the message body/part A described by state S to a the given
diff --git a/send.c b/send.c
--- a/send.c
+++ b/send.c
@@ -1635,17 +1635,17 @@
   
   if (WithCrypto)
   {
     if (msg->security)  
     {
       /* save the decrypted attachments */
       clear_content = msg->content;
   
-      if ((crypt_get_keys (msg, &pgpkeylist) == -1) ||
+      if ((crypt_get_keys (msg, &pgpkeylist, 0) == -1) ||
           mutt_protect (msg, pgpkeylist) == -1)
       {
         msg->content = mutt_remove_multipart (msg->content);
         
 	FREE (&pgpkeylist);
         
         decode_descriptions (msg->content);
         goto main_loop;

Reply via email to