Implement crypt_opportunistic_encrypt().

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

-Kevin
# HG changeset patch
# User Kevin McCarthy <[email protected]>
# Date 1423248572 28800
#      Fri Feb 06 10:49:32 2015 -0800
# Node ID 63581cd1bedeadb3731b333b6720fa75b2d93075
# Parent  f6dc07414374fee69baaff7f0508dcbc0a444ee0
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 oppenc_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,71 @@
   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 (oppenc_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, oppenc_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, oppenc_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_autoencrypt should override crypt_opportunistic_encrypt */
+  if (option (OPTCRYPTAUTOENCRYPT))
+    return;
+
+  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 oppenc_mode is true, only keys that can be determined without
+   prompting will be used.  */
+int crypt_get_keys (HEADER *msg, char **keylist, int oppenc_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
@@ -1663,17 +1663,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;

Attachment: signature.asc
Description: PGP signature

Reply via email to