Subject: [PATCH] Fix issues with OpenPGP header

From: "Philip K." <[email-masked]>
Date: Tue, 18 Aug 2020 15:21:53 +0200

Hi,

I submitted a patch a few months ago, to generate OpenPGP headers, that
seems like it was a bit faulty. When trying it out today, it didn't seem
to work properly, so I debugged it and made a few changes attached
below.

My appologies for not testing the patch thoroughly enough. I hope
everything is fixed now.

>From 1053988e26dd614aeb4490ae853d2c1c502cc493 Mon Sep 17 00:00:00 2001
From: Philip K <[email-masked]>
Date: Tue, 18 Aug 2020 15:12:51 +0200
Subject: [PATCH] Fix issues with OpenPGP header

* doc/misc/message.texi (OpenPGP Header): Mention correct hook
* lisp/gnus/message.el (message-openpgp-header): Improve customize type
(message-add-openpgp-header): Insert header into correct buffer
---
 doc/misc/message.texi |  2 +-
 lisp/gnus/message.el  | 63 ++++++++++++++++++++++---------------------
 2 files changed, 34 insertions(+), 31 deletions(-)

diff --git a/doc/misc/message.texi b/doc/misc/message.texi
index 204a6386e0..55b166eb8b 100644
--- a/doc/misc/message.texi
+++ b/doc/misc/message.texi
@@ -1265,7 +1265,7 @@ OpenPGP Header
 To use this in Message, say:
 
 @lisp
-(add-hook 'message-send-hook 'message-add-openpgp-header)
+(add-hook 'message-header-setup-hook 'message-add-openpgp-header)
 @end lisp
 
 @noindent
diff --git a/lisp/gnus/message.el b/lisp/gnus/message.el
index ab625be9e3..07ff489038 100644
--- a/lisp/gnus/message.el
+++ b/lisp/gnus/message.el
@@ -2751,16 +2751,17 @@ message-openpgp-header
 or `message-openpgp-header' is itself nil, the OpenPGP header
 will not be inserted."
   :type '(choice
-	  (const nil :tag "Don't add OpenPGP header")
-	  (list (choice (string :tag "ID")
-			(const nil :tag "No ID"))
+	  (const :tag "Don't add OpenPGP header" nil)
+	  (list :tag "Use OpenPGP header"
+		(choice (string :tag "ID")
+			(const :tag "No ID" nil))
 		(choice (string :tag "Key")
-			(const nil :tag "No Key"))
-		(choice (other nil :tag "None")
-			(const "unprotected" :tag "Unprotected")
-			(const "sign" :tag "Sign")
-			(const "encrypt" :tag "Encrypt")
-			(const "signencrypt" :tag "Sign and Encrypt"))))
+			(const :tag "No Key" nil))
+		(choice (other :tag "None" nil)
+			(const :tag "Unprotected" "unprotected")
+			(const :tag "Sign" "sign")
+			(const :tag "Encrypt" "encrypt")
+			(const :tag "Sign and Encrypt" "signencrypt"))))
   :version "28.1")
 
 (defun message-add-openpgp-header ()
@@ -2768,32 +2769,34 @@ message-add-openpgp-header
 
 Header will be constructed as specified in `message-openpgp-header'.
 
-Consider adding this function to `message-send-hook'."
+Consider adding this function to `message-header-setup-hook'"
   ;; See https://tools.ietf.org/html/draft-josefsson-openpgp-mailnews-header
   (when (and message-openpgp-header
 	     (or (nth 0 message-openpgp-header)
 		 (nth 1 message-openpgp-header)
 		 (nth 2 message-openpgp-header)))
-    (with-temp-buffer
-      (insert "OpenPGP: ")
-      ;; add ID
-      (let (need-sep)
-	(when (nth 0 message-openpgp-header)
-	  (insert "id=" (nth 0 message-openpgp-header))
-	  (setq need-sep t))
-	;; add URL
-	(when (nth 1 message-openpgp-header)
-	  (when need-sep (insert "; "))
-	  (if (string-match-p ";")
-	      (insert "url=\"" (nth 1 message-openpgp-header) "\"")
-	    (insert "url=\"" (nth 1 message-openpgp-header) "\""))
-	  (setq need-sep t))
-	;; add preference
-	(when (nth 2 message-openpgp-header)
-	  (when need-sep (insert "; "))
-	  (insert "preference=" (nth 2 message-openpgp-header))))
-      ;; insert header
-      (message-add-header (buffer-string)))))
+    (message-add-header
+     (with-temp-buffer
+       (insert "OpenPGP: ")
+       ;; add ID
+       (let (need-sep)
+	 (when (nth 0 message-openpgp-header)
+	   (insert "id=" (nth 0 message-openpgp-header))
+	   (setq need-sep t))
+	 ;; add URL
+	 (when (nth 1 message-openpgp-header)
+	   (when need-sep (insert "; "))
+	   (if (string-match-p ";")
+	       (insert "url=\"" (nth 1 message-openpgp-header) "\"")
+	     (insert "url=\"" (nth 1 message-openpgp-header) "\""))
+	   (setq need-sep t))
+	 ;; add preference
+	 (when (nth 2 message-openpgp-header)
+	   (when need-sep (insert "; "))
+	   (insert "preference=" (nth 2 message-openpgp-header))))
+       ;; insert header
+       (buffer-string)))
+    (message-sort-headers)))
 
 
 
-- 
2.26.2