[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Emacs-diffs] master 29006a6: Detect if a message can be encrypted and a
From: |
Nicolas Petton |
Subject: |
[Emacs-diffs] master 29006a6: Detect if a message can be encrypted and add an MML tag |
Date: |
Wed, 4 Apr 2018 14:33:04 -0400 (EDT) |
branch: master
commit 29006a6fd8e989013098b0281aa0b9efe13feb1d
Author: Damien Cassou <address@hidden>
Commit: Nicolas Petton <address@hidden>
Detect if a message can be encrypted and add an MML tag
* lisp/gnus/message.el (message-all-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-all-recipients): Test for
message-all-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 baff966..02b31ec 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.
+** 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 37b994d..fdb296f 100644
--- a/lisp/gnus/message.el
+++ b/lisp/gnus/message.el
@@ -2582,6 +2582,36 @@ PGG manual, depending on the value of `mml2015-use'."
(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)))
+
;;;
diff --git a/test/lisp/gnus/message-tests.el b/test/lisp/gnus/message-tests.el
index ec1f247..9124dcf 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 <address@hidden>, Person 2 <address@hidden>\n")
+ (insert "CC: Person 3 <address@hidden>, Person 4 <address@hidden>\n")
+ (insert "BCC: Person 5 <address@hidden>, Person 6 <address@hidden>\n")
+ (should (equal (message-all-recipients)
+ '(("Person 1" "address@hidden")
+ ("Person 2" "address@hidden")
+ ("Person 3" "address@hidden")
+ ("Person 4" "address@hidden")
+ ("Person 5" "address@hidden")
+ ("Person 6" "address@hidden"))))))
+
+(ert-deftest message-all-epg-keys-available-p ()
+ (let ((person1 '("Person 1" "address@hidden"))
+ (person2 '("Person 2" "address@hidden"))
+ (person3 '("Person 3" "address@hidden"))
+ (recipients nil)
+ (keyring '("address@hidden" "address@hidden")))
+ (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)
[Prev in Thread] |
Current Thread |
[Next in Thread] |
- [Emacs-diffs] master 29006a6: Detect if a message can be encrypted and add an MML tag,
Nicolas Petton <=