10

How can you generate a dynamic "Reply-To:" (and "From:") header in emacs/gnus based on Message-ID of the created message? I would like to use external (perl) script to generate a dynamic +detail part based on the "Messaged-ID:" header.

user+detail@example.net

I have managed to create a header with content generated by my external script. The script gets usenet group name as command line parameter. I would like to pass it the message-id value too.

My current code
~/.emacs :

'(gnus-posting-styles ("^pl\\.test$" ("Reply-To" message-make-reply-to)))

~/.gnus

(defun message-make-reply-to()
  (my-script ".../reply-to.pl" (message-fetch-field "Message-Id")))

(defun my-script(path &optional param) ....

The problem: the script does not receive message-id as its parameter (my-script gets correctly explicitly set parameter)

AnFi
  • 10,493
  • 3
  • 23
  • 47
  • 1
    Why don't you handle the task entirely from within Emacs/Gnus? And which message-id are you talking about, the one from the new reply or the one from the message being replied to? – schaueho Jun 29 '14 at 13:53
  • 1
    @schaueho 1) If you tell how to handle it in Emacs/Gnus then I should be able to add perl part 2) I want new new message-id (it is generated by my Emacs/Gnus customizations) – AnFi Jun 29 '14 at 15:08
  • 2
    The process you have in mind is still too unclear to me to give a complete answer but I believe you cannot get away without coding a small routine in Emacs Lisp. Hence some pointers: Fetching the message id from the new reply would be possible with `(message-fetch-field "Message-Id")`, generating the Reply-To field could be done by using `message-goto-reply-to` and then messing around with the line content or by `(message-replace-header "Reply-To" "my value")`. – schaueho Jun 30 '14 at 07:33
  • It does not seem to work. I use the following to execute scripts: `defun my-script(path &optional param) ...`. The dollowing in lisp function used to generated X-Reply-To have not passed Message-Id: to the script (my-script ".../script" (message-fetch-field "Message-Id")) – AnFi Jul 05 '14 at 08:08
  • If you add some code to your question, it would be easier to give help. – schaueho Jul 06 '14 at 13:45
  • You might need to set `message-generate-headers-first`, cg. [the relevant message documentation](https://www.gnu.org/software/emacs/manual/html_node/message/Message-Headers.html) – schaueho Jul 09 '14 at 10:11
  • `'(message-generate-headers-first t)` have not fixed "unset script param" problem. Message-Id is on both message-required-news-headers and message-required-mail-headers. P.S. I use custom lisp function to generate Message-Id. – AnFi Jul 09 '14 at 12:08
  • 1
    You probably need to ensure that the Message-ID is generated first, this might not be the case depending on various variables. I also don't know when your custom lisp function kicks in. – schaueho Jul 10 '14 at 11:50
  • @schaueho I have defined `message-make-message-id` lisp function to generate message-id – AnFi Jul 10 '14 at 12:23
  • I deleted my old answer (sorry for the misunderstanding) and added a new one. All the credit should really go to @schaueho, whose comments greatly helped to put together this answer. Please let me know if it worked. – tarleb Jul 04 '15 at 19:10

1 Answers1

4
;; Make sure the Message-ID header is present in newly created messages
(setq message-generate-headers-first '(Message-ID))

;; Prevent emacs from resetting the Message-ID before the message is sent.
(setq message-deletable-headers
      (remove 'Message-ID message-deletable-headers))

(setq gnus-posting-styles
      '(("^pl\\.test$"
         ("Reply-To" '(message-make-reply-to)))))

Note the additional quote and parentheses around message-make-reply-to. The explanation for this is that the function is run at different times, depending on whether it's given as a symbol or as a quoted s-expression.

  • If given as symbol, it is run when a lambda function is added to message-setup-hook. That happens in a message-mode-hook, i.e. right after the new buffer is created and switched into message-mode. The cause for this is some wild quoting/unquoting of values during creation of the lambda function.
  • If given as a quoted sexpr, evaluation is delayed until after the buffer is filled with initial values. It is close to the last code which is run on message setup.

Alternative Solution (without gnus-posting-styles)

In cases where the new header should be added to every new message, the Reply-To header can also be set using the message-header-setup-hook. A custom hook needs to be defined to add the header for each new message.

(defun reply-to-message-header-setup-hook ()
  (let* ((msg-id (message-fetch-field "Message-ID"))
         (reply-to (my-script ".../reply-to-pl" msg-id)))
    (message-add-header (concat "Reply-To: " reply-to))))

;; Call the hook every time a new message is created
(add-hook 'message-header-setup-hook 'reply-to-message-header-setup-hook)

;; Make sure the Message-ID header is present in newly created messages
(setq message-generate-headers-first '(Message-ID))
tarleb
  • 19,863
  • 4
  • 51
  • 80
  • It works after some modifications: `(defun reply-to-message-header-setup-hook () (message-add-header (concat "Reply-To: " (my-script ".../reply-to-pl" (list (message-fetch-field "Message-ID"))))))`. I wanted it to be selected only for some posts/messages by gnus-posting-styles but I accept the answer as the best offered. – AnFi Jul 16 '15 at 04:15
  • 1
    @AndrzejA.Filip After re-reading the respective gnus code, I found the answer to be much much simpler than what I was proposing: `(setq gnus-posting-styles ("^pl\\.test$" (Reply-To '(message-make-reply-to))))` (note the extra quote/parens) should work as long as the `message-generate-headers-first` value is set as above. I'll edit the answer to include this. – tarleb Jul 17 '15 at 20:00
  • 1
    **There is a problem**: The script gets Message-Id as presented in message compose buffer **BUT** the message is send with another Message-Id. – AnFi Jul 18 '15 at 08:10
  • @AndrzejA.Filip We have to remove Message-ID from message-deletable-headers to prevent it being reset when the message is sent: `(setq message-deletable-headers (remove 'Message-ID message-deletable-headers))` – tarleb Jul 18 '15 at 08:54
  • @AndrzejA.Filip Great! Thanks for being patient, debugging this with you was fun. – tarleb Jul 18 '15 at 19:26