Re: [PATCH 2/2] cli: clean up user address matching code in guess_from_received_header()
authorJani Nikula <jani@nikula.org>
Sat, 12 May 2012 17:23:16 +0000 (20:23 +0300)
committerW. Trevor King <wking@tremily.us>
Fri, 7 Nov 2014 17:47:03 +0000 (09:47 -0800)
5d/f4d8f2977afc048a57c2151c362235c0d7c00c [new file with mode: 0644]

diff --git a/5d/f4d8f2977afc048a57c2151c362235c0d7c00c b/5d/f4d8f2977afc048a57c2151c362235c0d7c00c
new file mode 100644 (file)
index 0000000..11fcd49
--- /dev/null
@@ -0,0 +1,215 @@
+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 87EF0431FB6\r
+       for <notmuch@notmuchmail.org>; Sat, 12 May 2012 10:23:24 -0700 (PDT)\r
+X-Virus-Scanned: Debian amavisd-new at olra.theworths.org\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 7Lisn6X46AHH for <notmuch@notmuchmail.org>;\r
+       Sat, 12 May 2012 10:23:23 -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 1BA66431FAE\r
+       for <notmuch@notmuchmail.org>; Sat, 12 May 2012 10:23:22 -0700 (PDT)\r
+Received: by lbbgk8 with SMTP id gk8so2797909lbb.26\r
+       for <notmuch@notmuchmail.org>; Sat, 12 May 2012 10:23:20 -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:cc:subject:in-reply-to:references:user-agent:date\r
+       :message-id:mime-version:content-type:x-gm-message-state;\r
+       bh=Mm7vU5wOMu/hoWZTzXF8/5XrT2vHlc2iTkKtmFNi5Jw=;\r
+       b=FwD2JnXEPgP/Nv7TCAq2hWw86aug0RULkPw+C6ajsYdXwFo0+/gDZVuWjSB+zGUqEG\r
+       oe87wze+dT5z9w8/TvTLAnLFssoZo0swmxON0d/PpGR/2Z9SeN3uiHgzu6JGjmMMdJqV\r
+       1r+wLR65k5VV2f5jjFdJ/q6NYW5euige/F5lJPl/WC+bOGvMLMP7DUzrHznla+fuLx5a\r
+       E1/uhD0l2eSkyPW2a6Zwi5y/f9msoOBLUhTq2Wk0ZCJa5xgU1hFiGNqOxiMkpns20ECb\r
+       9WnL/R7iSIBQ9a5bPWiDr6+bU7Q2I7/9IMi2ehvAGLR3lvm23ty3SXWpiTM+zt+KYBfr\r
+       eoNg==\r
+Received: by 10.112.98.137 with SMTP id ei9mr973937lbb.102.1336843400107;\r
+       Sat, 12 May 2012 10:23:20 -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 i6sm16636115lbg.2.2012.05.12.10.23.17\r
+       (version=SSLv3 cipher=OTHER); Sat, 12 May 2012 10:23:18 -0700 (PDT)\r
+From: Jani Nikula <jani@nikula.org>\r
+To: Austin Clements <amdragon@MIT.EDU>\r
+Subject: Re: [PATCH 2/2] cli: clean up user address matching code in\r
+       guess_from_received_header()\r
+In-Reply-To: <20120512154746.GK11804@mit.edu>\r
+References:\r
+ <bae496e6100a4e4d4a598dcf5c73d67b178f4128.1336746187.git.jani@nikula.org>\r
+       <cd73ec6368b3743b0d84ed1696bfa00960124645.1336746187.git.jani@nikula.org>\r
+       <20120512154746.GK11804@mit.edu>\r
+User-Agent: Notmuch/0.12+169~g45438b0 (http://notmuchmail.org) Emacs/23.3.1\r
+       (i686-pc-linux-gnu)\r
+Date: Sat, 12 May 2012 20:23:16 +0300\r
+Message-ID: <87k40h1fgb.fsf@nikula.org>\r
+MIME-Version: 1.0\r
+Content-Type: text/plain; charset=us-ascii\r
+X-Gm-Message-State:\r
+ ALoCoQnNgvbFzQ1bMidIifUj3dpwUmPRQlG1ShYbb5Pvt66IiFoKEJlxX2xkzfUqzpfd7WFJwkP0\r
+Cc: notmuch@notmuchmail.org\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: Sat, 12 May 2012 17:23:24 -0000\r
+\r
+On Sat, 12 May 2012, Austin Clements <amdragon@MIT.EDU> wrote:\r
+> Series LGTM.  This is a nice cleanup.\r
+\r
+Thanks for the review.\r
+\r
+> My one comment is that it seems like we should be stricter about\r
+> matching email address lists, since a naive substring match could\r
+> yield strange results.  That's not something that should be changed in\r
+> this patch, though.\r
+\r
+Agreed. Especially the Received: header matching should be scrutinized,\r
+as *all* the Received: headers in a message are merged into one by\r
+notmuch_message_file_get_header(). But that should be another patch,\r
+another time.\r
+\r
+> Does the context of this patch conflict with\r
+> id:"21a946917c5c8dd63295b7c87b7c2d1ebcb6e71e.1336746160.git.jani@nikula.org"\r
+> ?  It seems like both have context that's changed by the other patch.\r
+\r
+Ooops, you're right. The two series do *merge* on top of each other in\r
+either order, but as patches they apply only on master. I wanted to keep\r
+the series separate. Once either gets pushed, I'll rebase the other.\r
+\r
+BR,\r
+Jani.\r
+\r
+>\r
+> Quoth Jani Nikula on May 11 at  5:33 pm:\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