Add the crypt_opportunistic_encrypt option and calls.

This patch creates the OPTCRYPTOPPORTUNISTICENCRYPT option and
documentation.

It also adds calls to crypt_opportunistic_encrypt() during initial
message composition, after updating to, cc, or bcc, and after editing
the message (if edit_headers is enabled).

-Kevin
# HG changeset patch
# User Kevin McCarthy <[email protected]>
# Date 1423248573 28800
#      Fri Feb 06 10:49:33 2015 -0800
# Node ID 5e7d9353476447940b9118254d812a1eb4c24359
# Parent  63581cd1bedeadb3731b333b6720fa75b2d93075
Add the crypt_opportunistic_encrypt option and calls.

This patch creates the OPTCRYPTOPPORTUNISTICENCRYPT option and
documentation.

It also adds calls to crypt_opportunistic_encrypt() during initial
message composition, after updating to, cc, or bcc, and after editing
the message (if edit_headers is enabled).

diff --git a/compose.c b/compose.c
--- a/compose.c
+++ b/compose.c
@@ -521,24 +521,39 @@
        menu->pagelen = LINES - HDR_ATTACH - 2;
        break;
       case OP_COMPOSE_EDIT_FROM:
        menu->redraw = edit_address_list (HDR_FROM, &msg->env->from);
         mutt_message_hook (NULL, msg, M_SEND2HOOK);
        break;
       case OP_COMPOSE_EDIT_TO:
        menu->redraw = edit_address_list (HDR_TO, &msg->env->to);
+       if (option (OPTCRYPTOPPORTUNISTICENCRYPT))
+       {
+         crypt_opportunistic_encrypt (msg);
+         redraw_crypt_lines (msg);
+       }
         mutt_message_hook (NULL, msg, M_SEND2HOOK);
         break;
       case OP_COMPOSE_EDIT_BCC:
        menu->redraw = edit_address_list (HDR_BCC, &msg->env->bcc);
+       if (option (OPTCRYPTOPPORTUNISTICENCRYPT))
+       {
+         crypt_opportunistic_encrypt (msg);
+         redraw_crypt_lines (msg);
+       }
         mutt_message_hook (NULL, msg, M_SEND2HOOK);
        break;
       case OP_COMPOSE_EDIT_CC:
        menu->redraw = edit_address_list (HDR_CC, &msg->env->cc);
+       if (option (OPTCRYPTOPPORTUNISTICENCRYPT))
+       {
+         crypt_opportunistic_encrypt (msg);
+         redraw_crypt_lines (msg);
+       }
         mutt_message_hook (NULL, msg, M_SEND2HOOK);    
         break;
       case OP_COMPOSE_EDIT_SUBJECT:
        if (msg->env->subject)
          strfcpy (buf, msg->env->subject, sizeof (buf));
        else
          buf[0] = 0;
        if (mutt_get_field ("Subject: ", buf, sizeof (buf), 0) == 0)
@@ -588,16 +603,18 @@
          mutt_env_to_local (msg->env);
          mutt_edit_headers (NONULL (Editor), msg->content->filename, msg,
                             fcc, fcclen);
          if (mutt_env_to_idna (msg->env, &tag, &err))
          {
            mutt_error (_("Bad IDN in \"%s\": '%s'"), tag, err);
            FREE (&err);
          }
+         if (option (OPTCRYPTOPPORTUNISTICENCRYPT))
+           crypt_opportunistic_encrypt (msg);
        }
        else
        {
          /* this is grouped with OP_COMPOSE_EDIT_HEADERS because the
             attachment list could change if the user invokes ~v to edit
             the message with headers, in which we need to execute the
             code below to regenerate the index array */
          mutt_builtin_editor (msg->content->filename, msg, cur);
diff --git a/crypt.c b/crypt.c
--- a/crypt.c
+++ b/crypt.c
@@ -765,16 +765,19 @@
  * 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;
 
+  if (!WithCrypto)
+    return;
+
   /* crypt_autoencrypt should override crypt_opportunistic_encrypt */
   if (option (OPTCRYPTAUTOENCRYPT))
     return;
 
   crypt_get_keys (msg, &pgpkeylist, 1);
   if (pgpkeylist != NULL )
   {
     msg->security |= ENCRYPT;
diff --git a/init.h b/init.h
--- a/init.h
+++ b/init.h
@@ -486,16 +486,37 @@
   { "crypt_autosmime", DT_BOOL, R_NONE, OPTCRYPTAUTOSMIME, 1 },
   /*
   ** .pp
   ** This variable controls whether or not mutt may automatically enable
   ** S/MIME encryption/signing for messages. See also $$crypt_autoencrypt,
   ** $$crypt_replyencrypt,
   ** $$crypt_autosign, $$crypt_replysign and $$smime_is_default.
   */
+  { "crypt_opportunistic_encrypt", DT_BOOL, R_NONE, 
OPTCRYPTOPPORTUNISTICENCRYPT, 0 },
+  /*
+  ** .pp
+  ** Setting this variable will cause Mutt to automatically enable and
+  ** disable encryption, based on whether all message recipient keys
+  ** can be located by mutt.
+  ** .pp
+  ** When this option is enabled, mutt will determine the encryption
+  ** setting each time the TO, CC, and BCC lists are edited.  If
+  ** $$edit_headers is set, mutt will also do so each time the message
+  ** is edited.
+  ** .pp
+  ** While this is set, encryption settings can't be manually changed.
+  ** The pgp or smime menus provide an option to disable the option for
+  ** a particular message.
+  ** .pp
+  ** If $$crypt_autoencrypt or $$crypt_replyencrypt enable encryption for
+  ** a mesage, this option will be disabled for the message.  It can
+  ** be manually re-enabled in the pgp or smime menus.
+  ** (Crypto only)
+   */
   { "pgp_replyencrypt",                DT_SYN,  R_NONE, UL 
"crypt_replyencrypt", 1  },
   { "crypt_replyencrypt",      DT_BOOL, R_NONE, OPTCRYPTREPLYENCRYPT, 1 },
   /*
   ** .pp
   ** If \fIset\fP, automatically PGP or OpenSSL encrypt replies to messages 
which are
   ** encrypted.
   ** (Crypto only)
   */
diff --git a/mutt.h b/mutt.h
--- a/mutt.h
+++ b/mutt.h
@@ -456,16 +456,17 @@
   OPTCRYPTUSEPKA,
 
   /* PGP options */
   
   OPTCRYPTAUTOSIGN,
   OPTCRYPTAUTOENCRYPT,
   OPTCRYPTAUTOPGP,
   OPTCRYPTAUTOSMIME,
+  OPTCRYPTOPPORTUNISTICENCRYPT,
   OPTCRYPTREPLYENCRYPT,
   OPTCRYPTREPLYSIGN,
   OPTCRYPTREPLYSIGNENCRYPTED,
   OPTCRYPTTIMESTAMP,
   OPTSMIMEISDEFAULT,
   OPTASKCERTLABEL,
   OPTSDEFAULTDECRYPTKEY,
   OPTPGPIGNORESUB,
diff --git a/send.c b/send.c
--- a/send.c
+++ b/send.c
@@ -1472,17 +1472,17 @@
     if (WithCrypto & APPLICATION_PGP && (msg->security & (ENCRYPT | SIGN)))
     {
       if (option (OPTPGPAUTOINLINE))
        msg->security |= INLINE;
       if (option (OPTPGPREPLYINLINE) && cur && (cur->security & INLINE))
        msg->security |= INLINE;
     }
 
-    if (msg->security)
+    if (msg->security || option (OPTCRYPTOPPORTUNISTICENCRYPT))
     {
       /* 
        * When replying / forwarding, use the original message's
        * crypto system.  According to the documentation,
        * smime_is_default should be disregarded here.
        * 
        * Problem: At least with forwarding, this doesn't really
        * make much sense. Should we have an option to completely
@@ -1509,16 +1509,22 @@
          msg->security |= APPLICATION_SMIME;
        else if ((WithCrypto & APPLICATION_PGP) && option (OPTCRYPTAUTOPGP))
          msg->security |= APPLICATION_PGP;
        else if ((WithCrypto & APPLICATION_SMIME) && option (OPTCRYPTAUTOSMIME))
          msg->security |= APPLICATION_SMIME;
       }
     }
 
+    /* opportunistic encrypt relys on SMIME or PGP already being selected */
+    if (option (OPTCRYPTOPPORTUNISTICENCRYPT))
+    {
+      crypt_opportunistic_encrypt(msg);
+    }
+
     /* No permissible mechanisms found.  Don't sign or encrypt. */
     if (!(msg->security & (APPLICATION_SMIME|APPLICATION_PGP)))
       msg->security = 0;
   }
 
   /* specify a default fcc.  if we are in batchmode, only save a copy of
    * the message if the value of $copy is yes or ask-yes */
 

Attachment: signature.asc
Description: PGP signature

Reply via email to