Return-Path: X-Original-To: notmuch@notmuchmail.org Delivered-To: notmuch@notmuchmail.org Received: from localhost (localhost [127.0.0.1]) by olra.theworths.org (Postfix) with ESMTP id 17141431FBF for ; Tue, 8 Apr 2014 02:20:08 -0700 (PDT) X-Virus-Scanned: Debian amavisd-new at olra.theworths.org X-Spam-Flag: NO X-Spam-Score: -2.3 X-Spam-Level: X-Spam-Status: No, score=-2.3 tagged_above=-999 required=5 tests=[RCVD_IN_DNSWL_MED=-2.3] autolearn=disabled Received: from olra.theworths.org ([127.0.0.1]) by localhost (olra.theworths.org [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id LWQYCprcxFgU for ; Tue, 8 Apr 2014 02:19:56 -0700 (PDT) Received: from market.scs.stanford.edu (market.scs.stanford.edu [171.66.3.10]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (No client certificate requested) by olra.theworths.org (Postfix) with ESMTPS id 2B226431FBC for ; Tue, 8 Apr 2014 02:19:56 -0700 (PDT) Received: from market.scs.stanford.edu (localhost.scs.stanford.edu [127.0.0.1]) by market.scs.stanford.edu (8.14.7/8.14.7) with ESMTP id s389Jlde001749; Tue, 8 Apr 2014 02:19:47 -0700 (PDT) Received: (from dm@localhost) by market.scs.stanford.edu (8.14.7/8.14.7/Submit) id s389JkH1002190; Tue, 8 Apr 2014 02:19:46 -0700 (PDT) X-Authentication-Warning: market.scs.stanford.edu: dm set sender to return-gsfbd9y6t2m24wu4hrg33p5p5n@ta.scs.stanford.edu using -f From: David Mazieres To: Ian Kelling , notmuch@notmuchmail.org Subject: Re: gnus-alias integration bug In-Reply-To: <87vbuk8xo5.fsf@treetowl.lan> References: <87vbuk8xo5.fsf@treetowl.lan> Date: Tue, 08 Apr 2014 11:19:46 +0200 Message-ID: <8738hogoul.fsf@ta.scs.stanford.edu> MIME-Version: 1.0 Content-Type: text/plain X-BeenThere: notmuch@notmuchmail.org X-Mailman-Version: 2.1.13 Precedence: list Reply-To: David Mazieres expires 2014-07-07 CEST List-Id: "Use and development of the notmuch mail system." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 08 Apr 2014 09:20:08 -0000 Ian Kelling 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 ") ;; ... )) (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))))