From: Kevin McCarthy Date: Mon, 30 Mar 2015 22:45:53 +0000 (-0700) Subject: Implement crypt_opportunistic_encrypt(). X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=75a15bc04e64f718f9d67d0c14e30cd28233d92f;p=neomutt 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 index 0d5f456c5..f02a31688 100644 --- a/crypt.c +++ b/crypt.c @@ -707,7 +707,7 @@ void crypt_extract_keys_from_messages (HEADER * h) -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); @@ -732,12 +732,12 @@ int crypt_get_keys (HEADER *msg, char **keylist) *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); @@ -747,7 +747,7 @@ int crypt_get_keys (HEADER *msg, char **keylist) 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); @@ -761,6 +761,32 @@ int crypt_get_keys (HEADER *msg, char **keylist) } +/* + * 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) { diff --git a/mutt_crypt.h b/mutt_crypt.h index dc92ff3f0..7ef52e4b0 100644 --- a/mutt_crypt.h +++ b/mutt_crypt.h @@ -140,8 +140,14 @@ 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); diff --git a/send.c b/send.c index e28f26747..c74b89f05 100644 --- a/send.c +++ b/send.c @@ -1668,7 +1668,7 @@ main_loop: /* 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);