--- /dev/null
+Return-Path: <jani@nikula.org>\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 73033431FC9\r
+ for <notmuch@notmuchmail.org>; Fri, 11 May 2012 07:33:18 -0700 (PDT)\r
+X-Virus-Scanned: Debian amavisd-new at olra.theworths.org\r
+X-Amavis-Alert: BAD HEADER SECTION, Duplicate header field: "References"\r
+X-Spam-Flag: NO\r
+X-Spam-Score: -0.7\r
+X-Spam-Level: \r
+X-Spam-Status: No, score=-0.7 tagged_above=-999 required=5\r
+ tests=[RCVD_IN_DNSWL_LOW=-0.7] 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 NJF0SZl36TxS for <notmuch@notmuchmail.org>;\r
+ Fri, 11 May 2012 07:33:15 -0700 (PDT)\r
+Received: from mail-lb0-f181.google.com (mail-lb0-f181.google.com\r
+ [209.85.217.181]) (using TLSv1 with cipher RC4-SHA (128/128 bits))\r
+ (No client certificate requested)\r
+ by olra.theworths.org (Postfix) with ESMTPS id 42CC9431FC2\r
+ for <notmuch@notmuchmail.org>; Fri, 11 May 2012 07:33:13 -0700 (PDT)\r
+Received: by mail-lb0-f181.google.com with SMTP id gk8so2103376lbb.26\r
+ for <notmuch@notmuchmail.org>; Fri, 11 May 2012 07:33:12 -0700 (PDT)\r
+X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;\r
+ d=google.com; s=20120113;\r
+ h=from:to:subject:date:message-id:x-mailer:in-reply-to:references\r
+ :in-reply-to:references:x-gm-message-state;\r
+ bh=uf3J/2BNiH0c7w0Hf/QKI0Ah/QbJJ/7UU0XhTtHCRqc=;\r
+ b=hpnjEdEpqqYDdTZ4fja/fNAHnkaRb3uCMStFcVGxflHYsyodW4jswmcWGKNFRmX4gt\r
+ GtbTFypMufKkSHxKNsBB15++AVvKEIX5/qFQOcifVafXlWFLNPnsOc0pqHeNVNHwrGW9\r
+ iyTejF0n0cIno3NBxfAZXWQRECSycE7MPjhiw7QlzVAOA1eUQD0I/FA/RYkqlqgsAAj6\r
+ cdzDOCrEDVmw7SG3A3BV/LpSW5c1VRRwDBhRQ5blWO4xTsFadIkiH4Vk5y0RPOg3Vcsj\r
+ K3Z58fGmt9THmSWnCQBG4VXq8Adq7ByEX86CTopKU37apPseBEY5ESiJhA3IqC86PQpk\r
+ SWAg==\r
+Received: by 10.152.129.74 with SMTP id nu10mr8383309lab.50.1336746792803;\r
+ Fri, 11 May 2012 07:33:12 -0700 (PDT)\r
+Received: from localhost (dsl-hkibrasgw4-fe50dc00-68.dhcp.inet.fi.\r
+ [80.220.80.68])\r
+ by mx.google.com with ESMTPS id mo3sm9617691lab.2.2012.05.11.07.33.10\r
+ (version=SSLv3 cipher=OTHER); Fri, 11 May 2012 07:33:11 -0700 (PDT)\r
+From: Jani Nikula <jani@nikula.org>\r
+To: notmuch@notmuchmail.org\r
+Subject: [PATCH 2/2] cli: clean up user address matching code in\r
+ guess_from_received_header()\r
+Date: Fri, 11 May 2012 17:33:05 +0300\r
+Message-Id:\r
+ <cd73ec6368b3743b0d84ed1696bfa00960124645.1336746187.git.jani@nikula.org>\r
+X-Mailer: git-send-email 1.7.9.5\r
+In-Reply-To:\r
+ <bae496e6100a4e4d4a598dcf5c73d67b178f4128.1336746187.git.jani@nikula.org>\r
+References:\r
+ <bae496e6100a4e4d4a598dcf5c73d67b178f4128.1336746187.git.jani@nikula.org>\r
+In-Reply-To:\r
+ <bae496e6100a4e4d4a598dcf5c73d67b178f4128.1336746187.git.jani@nikula.org>\r
+References:\r
+ <bae496e6100a4e4d4a598dcf5c73d67b178f4128.1336746187.git.jani@nikula.org>\r
+X-Gm-Message-State:\r
+ ALoCoQkR1OgnBJS9MNXUIVUFE3MYLJUenDenR2hY8Gycxf5pNBVw8C4fLxKfO2k5fy5jXFf4WkLf\r
+X-BeenThere: notmuch@notmuchmail.org\r
+X-Mailman-Version: 2.1.13\r
+Precedence: list\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: Fri, 11 May 2012 14:33:19 -0000\r
+\r
+Get rid of user address matching code duplication in\r
+guess_from_received_header() by using the new address matching\r
+helpers.\r
+\r
+No functional changes.\r
+\r
+Signed-off-by: Jani Nikula <jani@nikula.org>\r
+---\r
+ notmuch-reply.c | 64 +++++++++++++++++--------------------------------------\r
+ 1 file changed, 19 insertions(+), 45 deletions(-)\r
+\r
+diff --git a/notmuch-reply.c b/notmuch-reply.c\r
+index 0c82755..51cb6de 100644\r
+--- a/notmuch-reply.c\r
++++ b/notmuch-reply.c\r
+@@ -377,20 +377,15 @@ add_recipients_from_message (GMimeMessage *reply,\r
+ static const char *\r
+ guess_from_received_header (notmuch_config_t *config, notmuch_message_t *message)\r
+ {\r
+- const char *received,*primary,*by;\r
+- const char **other;\r
+- char *tohdr;\r
++ const char *addr, *received, *by;\r
+ char *mta,*ptr,*token;\r
+ char *domain=NULL;\r
+ char *tld=NULL;\r
+ const char *delim=". \t";\r
+- size_t i,j,other_len;\r
++ size_t i;\r
+ \r
+ const char *to_headers[] = {"Envelope-to", "X-Original-To"};\r
+ \r
+- primary = notmuch_config_get_user_primary_email (config);\r
+- other = notmuch_config_get_user_other_email (config, &other_len);\r
+-\r
+ /* sadly, there is no standard way to find out to which email\r
+ * address a mail was delivered - what is in the headers depends\r
+ * on the MTAs used along the way. So we are trying a number of\r
+@@ -405,23 +400,13 @@ guess_from_received_header (notmuch_config_t *config, notmuch_message_t *message\r
+ * 'by' part of Received headers\r
+ * If none of these work, we give up and return NULL\r
+ */\r
+- for (i = 0; i < sizeof(to_headers)/sizeof(*to_headers); i++) {\r
+- tohdr = xstrdup(notmuch_message_get_header (message, to_headers[i]));\r
+- if (tohdr && *tohdr) {\r
+- /* tohdr is potentialy a list of email addresses, so here we\r
+- * check if one of the email addresses is a substring of tohdr\r
+- */\r
+- if (strcasestr(tohdr, primary)) {\r
+- free(tohdr);\r
+- return primary;\r
+- }\r
+- for (j = 0; j < other_len; j++)\r
+- if (strcasestr (tohdr, other[j])) {\r
+- free(tohdr);\r
+- return other[j];\r
+- }\r
+- free(tohdr);\r
+- }\r
++ for (i = 0; i < ARRAY_SIZE (to_headers); i++) {\r
++ const char *tohdr = notmuch_message_get_header (message, to_headers[i]);\r
++\r
++ /* Note: tohdr potentially contains a list of email addresses. */\r
++ addr = user_address_in_string (tohdr, config);\r
++ if (addr)\r
++ return addr;\r
+ }\r
+ \r
+ /* We get the concatenated Received: headers and search from the\r
+@@ -439,19 +424,12 @@ guess_from_received_header (notmuch_config_t *config, notmuch_message_t *message\r
+ * header\r
+ */\r
+ ptr = strstr (received, " for ");\r
+- if (ptr) {\r
+- /* the text following is potentialy a list of email addresses,\r
+- * so again we check if one of the email addresses is a\r
+- * substring of ptr\r
+- */\r
+- if (strcasestr(ptr, primary)) {\r
+- return primary;\r
+- }\r
+- for (i = 0; i < other_len; i++)\r
+- if (strcasestr (ptr, other[i])) {\r
+- return other[i];\r
+- }\r
+- }\r
++\r
++ /* Note: ptr potentially contains a list of email addresses. */\r
++ addr = user_address_in_string (ptr, config);\r
++ if (addr)\r
++ return addr;\r
++\r
+ /* Finally, we parse all the " by MTA ..." headers to guess the\r
+ * email address that this was originally delivered to.\r
+ * We extract just the MTA here by removing leading whitespace and\r
+@@ -492,15 +470,11 @@ guess_from_received_header (notmuch_config_t *config, notmuch_message_t *message\r
+ */\r
+ *(tld-1) = '.';\r
+ \r
+- if (strcasestr(primary, domain)) {\r
+- free(mta);\r
+- return primary;\r
++ addr = string_in_user_address (domain, config);\r
++ if (addr) {\r
++ free (mta);\r
++ return addr;\r
+ }\r
+- for (i = 0; i < other_len; i++)\r
+- if (strcasestr (other[i],domain)) {\r
+- free(mta);\r
+- return other[i];\r
+- }\r
+ }\r
+ free (mta);\r
+ }\r
+-- \r
+1.7.9.5\r
+\r