How to generate dynamic "Reply-To:" based on "Message-ID:"? [+detail]
Asked Answered
R

1

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.

[email protected]

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)

Rectory answered 9/3, 2013 at 12:18 Comment(10)
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?Extravasate
@Extravasate 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)Rectory
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").Extravasate
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"))Rectory
If you add some code to your question, it would be easier to give help.Extravasate
You might need to set message-generate-headers-first, cg. the relevant message documentationExtravasate
'(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.Rectory
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.Extravasate
@Extravasate I have defined message-make-message-id lisp function to generate message-idRectory
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.Sheepwalk
S
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))
Sheepwalk answered 26/6, 2015 at 11:43 Comment(5)
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.Rectory
@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.Sheepwalk
There is a problem: The script gets Message-Id as presented in message compose buffer BUT the message is send with another Message-Id.Rectory
@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))Sheepwalk
@AndrzejA.Filip Great! Thanks for being patient, debugging this with you was fun.Sheepwalk

© 2022 - 2024 — McMap. All rights reserved.