Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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)

like image 229
AnFi Avatar asked Mar 09 '13 12:03

AnFi


1 Answers

;; 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))
like image 157
tarleb Avatar answered Oct 08 '22 10:10

tarleb