unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
* bug#30962: 26.0.91; Encrypt message when there is a key for each recipient
@ 2018-03-27 15:35 Damien Cassou
  2018-04-04  8:34 ` Nicolas Petton
  0 siblings, 1 reply; 6+ messages in thread
From: Damien Cassou @ 2018-03-27 15:35 UTC (permalink / raw)
  To: 30962

[-- Attachment #1: Type: text/plain, Size: 289 bytes --]

Attached patch adds support to automatically encrypt email messages when
the epg keyring contains a key for every recipient.

-- 
Damien Cassou
http://damiencassou.seasidehosting.st

"Success is the ability to go from one failure to another without
losing enthusiasm." --Winston Churchill

[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 0001-Detect-if-a-message-can-be-encrypted-and-add-an-MML-.patch --]
[-- Type: text/x-patch, Size: 3806 bytes --]

From 483d0f6723d945ee828348fb9705c403305486fd Mon Sep 17 00:00:00 2001
From: Damien Cassou <damien@cassou.me>
Date: Tue, 27 Mar 2018 16:57:51 +0200
Subject: [PATCH] Detect if a message can be encrypted and add an MML tag

* lisp/gnus/message.el (message-recipients): Return a list of pairs,
one for each recipient in To, Cc, Bcc.
(message-all-epg-keys-available-p): Check that there is a public key
in epg for each recipient of the current message.
(message-sign-encrypt-if-all-keys-available): Add MML tag to sign and
encrypt current message if there is a public key for every recipient
in current message.

* test/lisp/gnus/message-tests.el (message-recipients): Test for
message-recipients.
---
 etc/NEWS                        |  8 ++++++++
 lisp/gnus/message.el            | 30 ++++++++++++++++++++++++++++++
 test/lisp/gnus/message-tests.el | 12 ++++++++++++
 3 files changed, 50 insertions(+)

diff --git a/etc/NEWS b/etc/NEWS
index 04774c13e5..5ae52dfa38 100644
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -315,6 +315,14 @@ or NextCloud hosted files and directories.
 It was obsolete since Emacs 22.1, replaced by customize.
 
 \f
+** Message
+
++++
+*** Messages can now be systematically encrypted
+when the PGP keyring contains a public key for every recipient.  To
+achieve this, add 'message-add-encrypt-tag-if-can-encrypt' to
+'message-send-hook'.
+
 * New Modes and Packages in Emacs 27.1
 
 +++
diff --git a/lisp/gnus/message.el b/lisp/gnus/message.el
index 37b994de99..4747d83f4d 100644
--- a/lisp/gnus/message.el
+++ b/lisp/gnus/message.el
@@ -2582,6 +2582,36 @@ message-info
 		      (t
 		       'message)))))
 
+(defun message-recipients ()
+  "Return a list of all recipients in the message, looking at TO, CC and BCC.
+
+Each recipient is in the format of `mail-extract-address-components'."
+  (mapcan (lambda (header)
+            (let ((header-value (message-fetch-field header)))
+              (and
+               header-value
+               (mail-extract-address-components header-value t))))
+          '("To" "Cc" "Bcc")))
+
+(defun message-all-epg-keys-available-p ()
+  "Return non-nil if the pgp keyring has a public key for each recipient."
+  (require 'epa)
+  (let ((context (epg-make-context epa-protocol)))
+    (catch 'break
+      (dolist (recipient (message-recipients))
+        (let ((recipient-email (cadr recipient)))
+          (when (and recipient-email (not (epg-list-keys context recipient-email)))
+            (throw 'break nil))))
+      t)))
+
+(defun message-sign-encrypt-if-all-keys-available ()
+  "Add MML tag to encrypt message when there is a key for each recipient.
+
+Consider adding this function to `message-send-hook' to
+systematically send encrypted emails when possible."
+  (when (message-all-epg-keys-available-p)
+    (mml-secure-message-sign-encrypt)))
+
 \f
 
 ;;;
diff --git a/test/lisp/gnus/message-tests.el b/test/lisp/gnus/message-tests.el
index ec1f247020..3678fa8cc8 100644
--- a/test/lisp/gnus/message-tests.el
+++ b/test/lisp/gnus/message-tests.el
@@ -97,6 +97,18 @@
         (should (string= stripped-was
                          (message-strip-subject-trailing-was with-was)))))))
 
+(ert-deftest message-recipients ()
+  (ert-with-test-buffer (:name "message")
+    (insert "To: Person 1 <p1@p1.org>, Person 2 <p2@p2.org>\n")
+    (insert "CC: Person 3 <p3@p3.org>, Person 4 <p4@p4.org>\n")
+    (insert "BCC: Person 5 <p5@p5.org>, Person 6 <p6@p6.org>\n")
+    (should (equal (message-recipients)
+                   '(("Person 1" "p1@p1.org")
+                     ("Person 2" "p2@p2.org")
+                     ("Person 3" "p3@p3.org")
+                     ("Person 4" "p4@p4.org")
+                     ("Person 5" "p5@p5.org")
+                     ("Person 6" "p6@p6.org"))))))
 
 (provide 'message-mode-tests)
 
-- 
2.14.3


^ permalink raw reply related	[flat|nested] 6+ messages in thread

* bug#30962: 26.0.91; Encrypt message when there is a key for each recipient
  2018-03-27 15:35 bug#30962: 26.0.91; Encrypt message when there is a key for each recipient Damien Cassou
@ 2018-04-04  8:34 ` Nicolas Petton
  2018-04-04  9:35   ` Damien Cassou
  0 siblings, 1 reply; 6+ messages in thread
From: Nicolas Petton @ 2018-04-04  8:34 UTC (permalink / raw)
  To: Damien Cassou, 30962

[-- Attachment #1: Type: text/plain, Size: 1084 bytes --]

Damien Cassou <damien@cassou.me> writes:

Hi Damien,

> Attached patch adds support to automatically encrypt email messages when
> the epg keyring contains a key for every recipient.

Thanks, I've been using it for a few days without any issue.

> +(defun message-recipients ()
          ^^^^^^^^^^^^^^^^^^
          Would `message-all-recipients' be a better name?
> [...]
> +(ert-deftest message-recipients ()
> +  (ert-with-test-buffer (:name "message")
> +    (insert "To: Person 1 <p1@p1.org>, Person 2 <p2@p2.org>\n")
> +    (insert "CC: Person 3 <p3@p3.org>, Person 4 <p4@p4.org>\n")
> +    (insert "BCC: Person 5 <p5@p5.org>, Person 6 <p6@p6.org>\n")
> +    (should (equal (message-recipients)
> +                   '(("Person 1" "p1@p1.org")
> +                     ("Person 2" "p2@p2.org")
> +                     ("Person 3" "p3@p3.org")
> +                     ("Person 4" "p4@p4.org")
> +                     ("Person 5" "p5@p5.org")
> +                     ("Person 6" "p6@p6.org"))))))

Could you add tests for `message-all-epg-keys-available-p' as well?

Cheers,
Nico

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 487 bytes --]

^ permalink raw reply	[flat|nested] 6+ messages in thread

* bug#30962: 26.0.91; Encrypt message when there is a key for each recipient
  2018-04-04  8:34 ` Nicolas Petton
@ 2018-04-04  9:35   ` Damien Cassou
  2018-04-04 13:28     ` Nicolas Petton
  0 siblings, 1 reply; 6+ messages in thread
From: Damien Cassou @ 2018-04-04  9:35 UTC (permalink / raw)
  To: Nicolas Petton, 30962

[-- Attachment #1: Type: text/plain, Size: 394 bytes --]

Nicolas Petton <nicolas@petton.fr> writes:
>           Would `message-all-recipients' be a better name?


renamed


> Could you add tests for `message-all-epg-keys-available-p' as well?

done.

Please find attached a new version.

-- 
Damien Cassou
http://damiencassou.seasidehosting.st

"Success is the ability to go from one failure to another without
losing enthusiasm." --Winston Churchill

[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 0001-Detect-if-a-message-can-be-encrypted-and-add-an-MML-.patch --]
[-- Type: text/x-patch, Size: 5191 bytes --]

From 64bf9c2f6b15e82b5d077b1428408237ad1d1e74 Mon Sep 17 00:00:00 2001
From: Damien Cassou <damien@cassou.me>
Date: Tue, 27 Mar 2018 16:57:51 +0200
Subject: [PATCH] Detect if a message can be encrypted and add an MML tag

* lisp/gnus/message.el (message-recipients): Return a list of pairs,
one for each recipient in To, Cc, Bcc.
(message-all-epg-keys-available-p): Check that there is a public key
in epg for each recipient of the current message.
(message-sign-encrypt-if-all-keys-available): Add MML tag to sign and
encrypt current message if there is a public key for every recipient
in current message.

* test/lisp/gnus/message-tests.el (message-recipients): Test for
message-recipients.
---
 etc/NEWS                        |  8 +++++++
 lisp/gnus/message.el            | 30 +++++++++++++++++++++++++++
 test/lisp/gnus/message-tests.el | 46 +++++++++++++++++++++++++++++++++++++++++
 3 files changed, 84 insertions(+)

diff --git a/etc/NEWS b/etc/NEWS
index baff9664cf..02b31ecff4 100644
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -337,6 +337,14 @@ or NextCloud hosted files and directories.
 It was obsolete since Emacs 22.1, replaced by customize.
 
 \f
+** Message
+
++++
+*** Messages can now be systematically encrypted
+when the PGP keyring contains a public key for every recipient.  To
+achieve this, add 'message-add-encrypt-tag-if-can-encrypt' to
+'message-send-hook'.
+
 * New Modes and Packages in Emacs 27.1
 
 +++
diff --git a/lisp/gnus/message.el b/lisp/gnus/message.el
index 37b994de99..fdb296fc24 100644
--- a/lisp/gnus/message.el
+++ b/lisp/gnus/message.el
@@ -2582,6 +2582,36 @@ message-info
 		      (t
 		       'message)))))
 
+(defun message-all-recipients ()
+  "Return a list of all recipients in the message, looking at TO, CC and BCC.
+
+Each recipient is in the format of `mail-extract-address-components'."
+  (mapcan (lambda (header)
+            (let ((header-value (message-fetch-field header)))
+              (and
+               header-value
+               (mail-extract-address-components header-value t))))
+          '("To" "Cc" "Bcc")))
+
+(defun message-all-epg-keys-available-p ()
+  "Return non-nil if the pgp keyring has a public key for each recipient."
+  (require 'epa)
+  (let ((context (epg-make-context epa-protocol)))
+    (catch 'break
+      (dolist (recipient (message-all-recipients))
+        (let ((recipient-email (cadr recipient)))
+          (when (and recipient-email (not (epg-list-keys context recipient-email)))
+            (throw 'break nil))))
+      t)))
+
+(defun message-sign-encrypt-if-all-keys-available ()
+  "Add MML tag to encrypt message when there is a key for each recipient.
+
+Consider adding this function to `message-send-hook' to
+systematically send encrypted emails when possible."
+  (when (message-all-epg-keys-available-p)
+    (mml-secure-message-sign-encrypt)))
+
 \f
 
 ;;;
diff --git a/test/lisp/gnus/message-tests.el b/test/lisp/gnus/message-tests.el
index ec1f247020..9124dcf77a 100644
--- a/test/lisp/gnus/message-tests.el
+++ b/test/lisp/gnus/message-tests.el
@@ -29,6 +29,8 @@
 (require 'ert)
 (require 'ert-x)
 
+(require 'cl-lib)
+
 (ert-deftest message-mode-propertize ()
   (with-temp-buffer
     (unwind-protect
@@ -97,6 +99,50 @@
         (should (string= stripped-was
                          (message-strip-subject-trailing-was with-was)))))))
 
+(ert-deftest message-all-recipients ()
+  (ert-with-test-buffer (:name "message")
+    (insert "To: Person 1 <p1@p1.org>, Person 2 <p2@p2.org>\n")
+    (insert "CC: Person 3 <p3@p3.org>, Person 4 <p4@p4.org>\n")
+    (insert "BCC: Person 5 <p5@p5.org>, Person 6 <p6@p6.org>\n")
+    (should (equal (message-all-recipients)
+                   '(("Person 1" "p1@p1.org")
+                     ("Person 2" "p2@p2.org")
+                     ("Person 3" "p3@p3.org")
+                     ("Person 4" "p4@p4.org")
+                     ("Person 5" "p5@p5.org")
+                     ("Person 6" "p6@p6.org"))))))
+
+(ert-deftest message-all-epg-keys-available-p ()
+  (let ((person1 '("Person 1" "p1@p1.org"))
+        (person2 '("Person 2" "p2@p2.org"))
+        (person3 '("Person 3" "p3@p3.org"))
+        (recipients nil)
+        (keyring '("p1@p1.org" "p2@p2.org")))
+    (cl-letf (((symbol-function 'epg-list-keys)
+               (lambda (_ email) (cl-find email keyring :test #'string=)))
+              ((symbol-function 'message-all-recipients)
+               (lambda () recipients)))
+
+      (setq recipients (list))
+      (should (message-all-epg-keys-available-p))
+
+      (setq recipients (list person1))
+      (should (message-all-epg-keys-available-p))
+
+      (setq recipients (list person1 person2))
+      (should (message-all-epg-keys-available-p))
+
+      (setq recipients (list person3))
+      (should-not (message-all-epg-keys-available-p))
+
+      (setq recipients (list person1 person3))
+      (should-not (message-all-epg-keys-available-p))
+
+      (setq recipients (list person3 person1))
+      (should-not (message-all-epg-keys-available-p))
+
+      (setq recipients (list person1 person2 person3))
+      (should-not (message-all-epg-keys-available-p)))))
 
 (provide 'message-mode-tests)
 
-- 
2.14.3


^ permalink raw reply related	[flat|nested] 6+ messages in thread

* bug#30962: 26.0.91; Encrypt message when there is a key for each recipient
  2018-04-04  9:35   ` Damien Cassou
@ 2018-04-04 13:28     ` Nicolas Petton
  2018-04-04 15:43       ` Lars Ingebrigtsen
  0 siblings, 1 reply; 6+ messages in thread
From: Nicolas Petton @ 2018-04-04 13:28 UTC (permalink / raw)
  To: Damien Cassou, 30962; +Cc: larsi

[-- Attachment #1: Type: text/plain, Size: 380 bytes --]

Damien Cassou <damien@cassou.me> writes:

> Nicolas Petton <nicolas@petton.fr> writes:
>>           Would `message-all-recipients' be a better name?
>
>
> renamed
>
>
>> Could you add tests for `message-all-epg-keys-available-p' as well?
>
> done.
>
> Please find attached a new version.

Thanks.

Lars, if that's ok with you, I'll install Damien's patch on master.

Cheers,
Nico

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 487 bytes --]

^ permalink raw reply	[flat|nested] 6+ messages in thread

* bug#30962: 26.0.91; Encrypt message when there is a key for each recipient
  2018-04-04 13:28     ` Nicolas Petton
@ 2018-04-04 15:43       ` Lars Ingebrigtsen
  2018-04-04 18:35         ` Nicolas Petton
  0 siblings, 1 reply; 6+ messages in thread
From: Lars Ingebrigtsen @ 2018-04-04 15:43 UTC (permalink / raw)
  To: Nicolas Petton; +Cc: Damien Cassou, 30962

Nicolas Petton <nicolas@petton.fr> writes:

> Lars, if that's ok with you, I'll install Damien's patch on master.

Looks good to me.

-- 
(domestic pets only, the antidote for overdose, milk.)
   bloggy blog: http://lars.ingebrigtsen.no





^ permalink raw reply	[flat|nested] 6+ messages in thread

* bug#30962: 26.0.91; Encrypt message when there is a key for each recipient
  2018-04-04 15:43       ` Lars Ingebrigtsen
@ 2018-04-04 18:35         ` Nicolas Petton
  0 siblings, 0 replies; 6+ messages in thread
From: Nicolas Petton @ 2018-04-04 18:35 UTC (permalink / raw)
  To: Lars Ingebrigtsen; +Cc: Damien Cassou, 30962, 30962-done

[-- Attachment #1: Type: text/plain, Size: 209 bytes --]

Lars Ingebrigtsen <larsi@gnus.org> writes:

>> Lars, if that's ok with you, I'll install Damien's patch on master.
>
> Looks good to me.

Ok, I installed it in master, so I'm closing this issue.

Cheers,
Nico

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 487 bytes --]

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2018-04-04 18:35 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2018-03-27 15:35 bug#30962: 26.0.91; Encrypt message when there is a key for each recipient Damien Cassou
2018-04-04  8:34 ` Nicolas Petton
2018-04-04  9:35   ` Damien Cassou
2018-04-04 13:28     ` Nicolas Petton
2018-04-04 15:43       ` Lars Ingebrigtsen
2018-04-04 18:35         ` Nicolas Petton

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).