--- /dev/null
+Return-Path:\r
+ <return-gsfbd9y6t2m24wu4hrg33p5p5n@temporary-address.scs.stanford.edu>\r
+X-Original-To: notmuch@notmuchmail.org\r
+Delivered-To: notmuch@notmuchmail.org\r
+Received: from localhost (localhost [127.0.0.1])\r
+ by olra.theworths.org (Postfix) with ESMTP id 17141431FBF\r
+ for <notmuch@notmuchmail.org>; Tue, 8 Apr 2014 02:20:08 -0700 (PDT)\r
+X-Virus-Scanned: Debian amavisd-new at olra.theworths.org\r
+X-Spam-Flag: NO\r
+X-Spam-Score: -2.3\r
+X-Spam-Level: \r
+X-Spam-Status: No, score=-2.3 tagged_above=-999 required=5\r
+ tests=[RCVD_IN_DNSWL_MED=-2.3] autolearn=disabled\r
+Received: from olra.theworths.org ([127.0.0.1])\r
+ by localhost (olra.theworths.org [127.0.0.1]) (amavisd-new, port 10024)\r
+ with ESMTP id LWQYCprcxFgU for <notmuch@notmuchmail.org>;\r
+ Tue, 8 Apr 2014 02:19:56 -0700 (PDT)\r
+Received: from market.scs.stanford.edu (market.scs.stanford.edu [171.66.3.10])\r
+ (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits))\r
+ (No client certificate requested)\r
+ by olra.theworths.org (Postfix) with ESMTPS id 2B226431FBC\r
+ for <notmuch@notmuchmail.org>; Tue, 8 Apr 2014 02:19:56 -0700 (PDT)\r
+Received: from market.scs.stanford.edu (localhost.scs.stanford.edu\r
+ [127.0.0.1]) by market.scs.stanford.edu (8.14.7/8.14.7) with ESMTP id\r
+ s389Jlde001749; Tue, 8 Apr 2014 02:19:47 -0700 (PDT)\r
+Received: (from dm@localhost)\r
+ by market.scs.stanford.edu (8.14.7/8.14.7/Submit) id s389JkH1002190;\r
+ Tue, 8 Apr 2014 02:19:46 -0700 (PDT)\r
+X-Authentication-Warning: market.scs.stanford.edu: dm set sender to\r
+ return-gsfbd9y6t2m24wu4hrg33p5p5n@ta.scs.stanford.edu using -f\r
+From: David Mazieres <dm-list-email-notmuch@scs.stanford.edu>\r
+To: Ian Kelling <ian@iankelling.org>, notmuch@notmuchmail.org\r
+Subject: Re: gnus-alias integration bug\r
+In-Reply-To: <87vbuk8xo5.fsf@treetowl.lan>\r
+References: <87vbuk8xo5.fsf@treetowl.lan>\r
+Date: Tue, 08 Apr 2014 11:19:46 +0200\r
+Message-ID: <8738hogoul.fsf@ta.scs.stanford.edu>\r
+MIME-Version: 1.0\r
+Content-Type: text/plain\r
+X-BeenThere: notmuch@notmuchmail.org\r
+X-Mailman-Version: 2.1.13\r
+Precedence: list\r
+Reply-To: David Mazieres expires 2014-07-07 CEST\r
+ <mazieres-ta6a6dtvd22xr33tw7tutuhifs@temporary-address.scs.stanford.edu>\r
+List-Id: "Use and development of the notmuch mail system."\r
+ <notmuch.notmuchmail.org>\r
+List-Unsubscribe: <http://notmuchmail.org/mailman/options/notmuch>,\r
+ <mailto:notmuch-request@notmuchmail.org?subject=unsubscribe>\r
+List-Archive: <http://notmuchmail.org/pipermail/notmuch>\r
+List-Post: <mailto:notmuch@notmuchmail.org>\r
+List-Help: <mailto:notmuch-request@notmuchmail.org?subject=help>\r
+List-Subscribe: <http://notmuchmail.org/mailman/listinfo/notmuch>,\r
+ <mailto:notmuch-request@notmuchmail.org?subject=subscribe>\r
+X-List-Received-Date: Tue, 08 Apr 2014 09:20:08 -0000\r
+\r
+Ian Kelling <ian@iankelling.org> writes:\r
+\r
+> gnus-alias allows you to set your identity based on the headers in the\r
+> message you are replying. This is an essential feature in a mail client\r
+> for me, I want to automatically reply as the person an email was sent to\r
+> for certain addresses. This does not work in notmuch.\r
+\r
+I need this feature, too. There is some support for the same\r
+functionality built into notmuch. The big problem for me is that\r
+.notmuch-config does not support wildcards (e.g., *@mydomain.com or\r
+user-*@mydomain.com). Compounding this issue is that, other than\r
+notmuch reply's internals, notmuch pretty much suppresses the\r
+Delivered-To header, which is crucial for knowing in a non-heuristic way\r
+where an email was actually delivered to. I want to compute my From:\r
+header based on the Delivered-To, so I created a\r
+mail-delivered-to-reply-from alist that maps one to the other.\r
+\r
+It's pretty horrifying what I had to do to get this to work, but it\r
+looks something like the following. (Apologies for any mistakes, I had\r
+to strip out additional complexity I have for other things in there, so\r
+it's not verbatim the code that I'm using, but it's pretty close.)\r
+\r
+If someone knows a better way of doing this with stock notmuch, please\r
+let me know. I know it's simpler to fix notmuch-reply, but I really\r
+don't want to start depending on a custom-forked version on the notmuch\r
+binary, as I read my mail from multiple operating systems and I'm sure\r
+I'd forget to update one of them.\r
+\r
+David\r
+\r
+\r
+;;; Regular expression matching all of my own email addresses. Note\r
+;;; this is also used by mail-dont-reply-to (which ships with emacs).\r
+(setq mail-dont-reply-to-names\r
+ (concat "REXEXP MATCHING ALL YOUR EMAIL ALIASES"))\r
+\r
+;;; Alist mapping Delivered-To addresses to the address that should be\r
+;;; in the From header of replies.\r
+(setq mail-delivered-to-reply-from\r
+ '(("delivered-to-\\(.*\\)@address.com"\r
+ . "Name <send-from-\\1@address.com>")\r
+ ;; ...\r
+ ))\r
+\r
+(defun compute-from (email)\r
+ (let ((case-fold-search t)\r
+ (subst (assoc-default email mail-delivered-to-reply-from\r
+ #'string-match)))\r
+ (and subst\r
+ (concat (notmuch-user-name) " <"\r
+ (replace-match subst t nil email) ">"))))\r
+\r
+(defun get-delivered-to-from-path (path)\r
+ (with-temp-buffer\r
+ ; It's weird to use sed, but there's also no reason to read a huge\r
+ ; file into a buffer right here when we reply to messags with\r
+ ; attachments. (notmuch show doesn't support --body=false in\r
+ ; --format=raw mode, and the other modes all get rid of Delivered-To.)\r
+ (call-process "sed" nil t t "-ne"\r
+ "/^Delivered-To: */{s///p;q;}; /^$/q" path)\r
+ (goto-char (point-max))\r
+ (or (bobp) (delete-char -1))\r
+ (and (not (bobp)) (buffer-string))))\r
+\r
+(defun get-msg-pathnames (query)\r
+ (notmuch-call-notmuch-sexp\r
+ "search" "--format=sexp" "--format-version=1"\r
+ "--output=files" "--" query))\r
+\r
+(defun compute-reply-from (pl)\r
+ (let* ((orig (plist-get pl :original))\r
+ (path (plist-get orig :filename))\r
+ (orig-from (plist-get (plist-get orig :headers) :From))\r
+ (addr\r
+ (cond\r
+ ((get-delivered-to-from-path path))\r
+ ;; in case of maildir.synchronize_flags=true, the pathname\r
+ ;; might already be incorrect, so try getting it again\r
+ ((and (setq path (car (get-msg-pathnames query-string)))\r
+ (get-delivered-to-from-path path)))\r
+ ((string-match mail-dont-reply-to-names\r
+ (setq orig-from (car (rfc822-addresses orig-from))))\r
+ orig-from)\r
+ )))\r
+ (and addr (compute-from addr))))\r
+\r
+;;; Somehow no plist-del function in emacs\r
+(defun my-plist-del (plist0 prop)\r
+ (if (eq prop (car plist0))\r
+ (cddr plist0)\r
+ (let ((p (cdr plist0)))\r
+ (while (and p (not (eq prop (cadr p))))\r
+ (setq p (cddr p)))\r
+ (if p (setcdr p (cdddr p)))\r
+ plist0)))\r
+\r
+(defun fix-reply-sexp (pl)\r
+ (let* ((rh (plist-get pl :reply-headers))\r
+ (from (compute-reply-from pl))\r
+ h)\r
+ (if from (setq rh (plist-put rh :From from)))\r
+ (and (setq h (plist-get rh :To))\r
+ (setq h (mail-dont-reply-to h))\r
+ (not (equal h ""))\r
+ (setq rh (plist-put rh :To h)))\r
+ (and (setq h (plist-get rh :Cc))\r
+ (setq h (mail-dont-reply-to h))\r
+ (if (equal h "")\r
+ (setq rh (my-plist-del rh :Cc))\r
+ (setq rh (plist-put rh :Cc h))))\r
+ (plist-put pl :reply-headers rh)))\r
+(defadvice notmuch-call-notmuch-sexp (after fix-reply activate)\r
+ "Fix From/To/CC headers in reply"\r
+ (if (equal (ad-get-arg 0) "reply")\r
+ (setq ad-return-value (fix-reply-sexp ad-return-value))))\r