gnus-alias integration bug

David Mazieres dm-list-email-notmuch at scs.stanford.edu
Tue Apr 8 02:19:46 PDT 2014


Ian Kelling <ian at iankelling.org> writes:

> gnus-alias allows you to set your identity based on the headers in the
> message you are replying. This is an essential feature in a mail client
> for me, I want to automatically reply as the person an email was sent to
> for certain addresses. This does not work in notmuch.

I need this feature, too.  There is some support for the same
functionality built into notmuch.  The big problem for me is that
.notmuch-config does not support wildcards (e.g., *@mydomain.com or
user-*@mydomain.com).  Compounding this issue is that, other than
notmuch reply's internals, notmuch pretty much suppresses the
Delivered-To header, which is crucial for knowing in a non-heuristic way
where an email was actually delivered to.  I want to compute my From:
header based on the Delivered-To, so I created a
mail-delivered-to-reply-from alist that maps one to the other.

It's pretty horrifying what I had to do to get this to work, but it
looks something like the following.  (Apologies for any mistakes, I had
to strip out additional complexity I have for other things in there, so
it's not verbatim the code that I'm using, but it's pretty close.)

If someone knows a better way of doing this with stock notmuch, please
let me know.  I know it's simpler to fix notmuch-reply, but I really
don't want to start depending on a custom-forked version on the notmuch
binary, as I read my mail from multiple operating systems and I'm sure
I'd forget to update one of them.

David


;;; Regular expression matching all of my own email addresses.  Note
;;; this is also used by mail-dont-reply-to (which ships with emacs).
(setq mail-dont-reply-to-names
      (concat "REXEXP MATCHING ALL YOUR EMAIL ALIASES"))

;;; Alist mapping Delivered-To addresses to the address that should be
;;; in the From header of replies.
(setq mail-delivered-to-reply-from
      '(("delivered-to-\\(.*\\)@address.com"
         . "Name <send-from-\\1 at address.com>")
        ;; ...
        ))

(defun compute-from (email)
  (let ((case-fold-search t)
	(subst (assoc-default email mail-delivered-to-reply-from
			      #'string-match)))
    (and subst
	 (concat (notmuch-user-name) " <"
		 (replace-match subst t nil email) ">"))))

(defun get-delivered-to-from-path (path)
  (with-temp-buffer
    ; It's weird to use sed, but there's also no reason to read a huge
    ; file into a buffer right here when we reply to messags with
    ; attachments.  (notmuch show doesn't support --body=false in
    ; --format=raw mode, and the other modes all get rid of Delivered-To.)
    (call-process "sed" nil t t "-ne"
		  "/^Delivered-To: */{s///p;q;}; /^$/q" path)
    (goto-char (point-max))
    (or (bobp) (delete-char -1))
    (and (not (bobp)) (buffer-string))))

(defun get-msg-pathnames (query)
  (notmuch-call-notmuch-sexp
   "search" "--format=sexp" "--format-version=1"
   "--output=files" "--" query))

(defun compute-reply-from (pl)
  (let* ((orig (plist-get pl :original))
	 (path (plist-get orig :filename))
	 (orig-from (plist-get (plist-get orig :headers) :From))
	 (addr
	  (cond
	   ((get-delivered-to-from-path path))
           ;; in case of maildir.synchronize_flags=true, the pathname
           ;; might already be incorrect, so try getting it again
	   ((and (setq path (car (get-msg-pathnames query-string)))
		 (get-delivered-to-from-path path)))
	   ((string-match mail-dont-reply-to-names
			  (setq orig-from (car (rfc822-addresses orig-from))))
	    orig-from)
	   )))
    (and addr (compute-from addr))))

;;; Somehow no plist-del function in emacs
(defun my-plist-del (plist0 prop)
  (if (eq prop (car plist0))
      (cddr plist0)
    (let ((p (cdr plist0)))
      (while (and p (not (eq prop (cadr p))))
	(setq p (cddr p)))
      (if p (setcdr p (cdddr p)))
      plist0)))

(defun fix-reply-sexp (pl)
  (let* ((rh (plist-get pl :reply-headers))
	 (from (compute-reply-from pl))
	 h)
    (if from (setq rh (plist-put rh :From from)))
    (and (setq h (plist-get rh :To))
	 (setq h (mail-dont-reply-to h))
	 (not (equal h ""))
	 (setq rh (plist-put rh :To h)))
    (and (setq h (plist-get rh :Cc))
	 (setq h (mail-dont-reply-to h))
	 (if (equal h "")
	     (setq rh (my-plist-del rh :Cc))
	   (setq rh (plist-put rh :Cc h))))
    (plist-put pl :reply-headers rh)))
(defadvice notmuch-call-notmuch-sexp (after fix-reply activate)
  "Fix From/To/CC headers in reply"
  (if (equal (ad-get-arg 0) "reply")
      (setq ad-return-value (fix-reply-sexp ad-return-value))))


More information about the notmuch mailing list