Missing headers when forwarding html message as RFC822
[notmuch-archives.git] / 5d / f4d8f2977afc048a57c2151c362235c0d7c00c
1 Return-Path: <jani@nikula.org>\r
2 X-Original-To: notmuch@notmuchmail.org\r
3 Delivered-To: notmuch@notmuchmail.org\r
4 Received: from localhost (localhost [127.0.0.1])\r
5         by olra.theworths.org (Postfix) with ESMTP id 87EF0431FB6\r
6         for <notmuch@notmuchmail.org>; Sat, 12 May 2012 10:23:24 -0700 (PDT)\r
7 X-Virus-Scanned: Debian amavisd-new at olra.theworths.org\r
8 X-Spam-Flag: NO\r
9 X-Spam-Score: -0.7\r
10 X-Spam-Level: \r
11 X-Spam-Status: No, score=-0.7 tagged_above=-999 required=5\r
12         tests=[RCVD_IN_DNSWL_LOW=-0.7] autolearn=disabled\r
13 Received: from olra.theworths.org ([127.0.0.1])\r
14         by localhost (olra.theworths.org [127.0.0.1]) (amavisd-new, port 10024)\r
15         with ESMTP id 7Lisn6X46AHH for <notmuch@notmuchmail.org>;\r
16         Sat, 12 May 2012 10:23:23 -0700 (PDT)\r
17 Received: from mail-lb0-f181.google.com (mail-lb0-f181.google.com\r
18         [209.85.217.181]) (using TLSv1 with cipher RC4-SHA (128/128 bits))\r
19         (No client certificate requested)\r
20         by olra.theworths.org (Postfix) with ESMTPS id 1BA66431FAE\r
21         for <notmuch@notmuchmail.org>; Sat, 12 May 2012 10:23:22 -0700 (PDT)\r
22 Received: by lbbgk8 with SMTP id gk8so2797909lbb.26\r
23         for <notmuch@notmuchmail.org>; Sat, 12 May 2012 10:23:20 -0700 (PDT)\r
24 X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;\r
25         d=google.com; s=20120113;\r
26         h=from:to:cc:subject:in-reply-to:references:user-agent:date\r
27         :message-id:mime-version:content-type:x-gm-message-state;\r
28         bh=Mm7vU5wOMu/hoWZTzXF8/5XrT2vHlc2iTkKtmFNi5Jw=;\r
29         b=FwD2JnXEPgP/Nv7TCAq2hWw86aug0RULkPw+C6ajsYdXwFo0+/gDZVuWjSB+zGUqEG\r
30         oe87wze+dT5z9w8/TvTLAnLFssoZo0swmxON0d/PpGR/2Z9SeN3uiHgzu6JGjmMMdJqV\r
31         1r+wLR65k5VV2f5jjFdJ/q6NYW5euige/F5lJPl/WC+bOGvMLMP7DUzrHznla+fuLx5a\r
32         E1/uhD0l2eSkyPW2a6Zwi5y/f9msoOBLUhTq2Wk0ZCJa5xgU1hFiGNqOxiMkpns20ECb\r
33         9WnL/R7iSIBQ9a5bPWiDr6+bU7Q2I7/9IMi2ehvAGLR3lvm23ty3SXWpiTM+zt+KYBfr\r
34         eoNg==\r
35 Received: by 10.112.98.137 with SMTP id ei9mr973937lbb.102.1336843400107;\r
36         Sat, 12 May 2012 10:23:20 -0700 (PDT)\r
37 Received: from localhost (dsl-hkibrasgw4-fe50dc00-68.dhcp.inet.fi.\r
38         [80.220.80.68])\r
39         by mx.google.com with ESMTPS id i6sm16636115lbg.2.2012.05.12.10.23.17\r
40         (version=SSLv3 cipher=OTHER); Sat, 12 May 2012 10:23:18 -0700 (PDT)\r
41 From: Jani Nikula <jani@nikula.org>\r
42 To: Austin Clements <amdragon@MIT.EDU>\r
43 Subject: Re: [PATCH 2/2] cli: clean up user address matching code in\r
44         guess_from_received_header()\r
45 In-Reply-To: <20120512154746.GK11804@mit.edu>\r
46 References:\r
47  <bae496e6100a4e4d4a598dcf5c73d67b178f4128.1336746187.git.jani@nikula.org>\r
48         <cd73ec6368b3743b0d84ed1696bfa00960124645.1336746187.git.jani@nikula.org>\r
49         <20120512154746.GK11804@mit.edu>\r
50 User-Agent: Notmuch/0.12+169~g45438b0 (http://notmuchmail.org) Emacs/23.3.1\r
51         (i686-pc-linux-gnu)\r
52 Date: Sat, 12 May 2012 20:23:16 +0300\r
53 Message-ID: <87k40h1fgb.fsf@nikula.org>\r
54 MIME-Version: 1.0\r
55 Content-Type: text/plain; charset=us-ascii\r
56 X-Gm-Message-State:\r
57  ALoCoQnNgvbFzQ1bMidIifUj3dpwUmPRQlG1ShYbb5Pvt66IiFoKEJlxX2xkzfUqzpfd7WFJwkP0\r
58 Cc: notmuch@notmuchmail.org\r
59 X-BeenThere: notmuch@notmuchmail.org\r
60 X-Mailman-Version: 2.1.13\r
61 Precedence: list\r
62 List-Id: "Use and development of the notmuch mail system."\r
63         <notmuch.notmuchmail.org>\r
64 List-Unsubscribe: <http://notmuchmail.org/mailman/options/notmuch>,\r
65         <mailto:notmuch-request@notmuchmail.org?subject=unsubscribe>\r
66 List-Archive: <http://notmuchmail.org/pipermail/notmuch>\r
67 List-Post: <mailto:notmuch@notmuchmail.org>\r
68 List-Help: <mailto:notmuch-request@notmuchmail.org?subject=help>\r
69 List-Subscribe: <http://notmuchmail.org/mailman/listinfo/notmuch>,\r
70         <mailto:notmuch-request@notmuchmail.org?subject=subscribe>\r
71 X-List-Received-Date: Sat, 12 May 2012 17:23:24 -0000\r
72 \r
73 On Sat, 12 May 2012, Austin Clements <amdragon@MIT.EDU> wrote:\r
74 > Series LGTM.  This is a nice cleanup.\r
75 \r
76 Thanks for the review.\r
77 \r
78 > My one comment is that it seems like we should be stricter about\r
79 > matching email address lists, since a naive substring match could\r
80 > yield strange results.  That's not something that should be changed in\r
81 > this patch, though.\r
82 \r
83 Agreed. Especially the Received: header matching should be scrutinized,\r
84 as *all* the Received: headers in a message are merged into one by\r
85 notmuch_message_file_get_header(). But that should be another patch,\r
86 another time.\r
87 \r
88 > Does the context of this patch conflict with\r
89 > id:"21a946917c5c8dd63295b7c87b7c2d1ebcb6e71e.1336746160.git.jani@nikula.org"\r
90 > ?  It seems like both have context that's changed by the other patch.\r
91 \r
92 Ooops, you're right. The two series do *merge* on top of each other in\r
93 either order, but as patches they apply only on master. I wanted to keep\r
94 the series separate. Once either gets pushed, I'll rebase the other.\r
95 \r
96 BR,\r
97 Jani.\r
98 \r
99 >\r
100 > Quoth Jani Nikula on May 11 at  5:33 pm:\r
101 >> Get rid of user address matching code duplication in\r
102 >> guess_from_received_header() by using the new address matching\r
103 >> helpers.\r
104 >> \r
105 >> No functional changes.\r
106 >> \r
107 >> Signed-off-by: Jani Nikula <jani@nikula.org>\r
108 >> ---\r
109 >>  notmuch-reply.c |   64 +++++++++++++++++--------------------------------------\r
110 >>  1 file changed, 19 insertions(+), 45 deletions(-)\r
111 >> \r
112 >> diff --git a/notmuch-reply.c b/notmuch-reply.c\r
113 >> index 0c82755..51cb6de 100644\r
114 >> --- a/notmuch-reply.c\r
115 >> +++ b/notmuch-reply.c\r
116 >> @@ -377,20 +377,15 @@ add_recipients_from_message (GMimeMessage *reply,\r
117 >>  static const char *\r
118 >>  guess_from_received_header (notmuch_config_t *config, notmuch_message_t *message)\r
119 >>  {\r
120 >> -    const char *received,*primary,*by;\r
121 >> -    const char **other;\r
122 >> -    char *tohdr;\r
123 >> +    const char *addr, *received, *by;\r
124 >>      char *mta,*ptr,*token;\r
125 >>      char *domain=NULL;\r
126 >>      char *tld=NULL;\r
127 >>      const char *delim=". \t";\r
128 >> -    size_t i,j,other_len;\r
129 >> +    size_t i;\r
130 >>  \r
131 >>      const char *to_headers[] = {"Envelope-to", "X-Original-To"};\r
132 >>  \r
133 >> -    primary = notmuch_config_get_user_primary_email (config);\r
134 >> -    other = notmuch_config_get_user_other_email (config, &other_len);\r
135 >> -\r
136 >>      /* sadly, there is no standard way to find out to which email\r
137 >>       * address a mail was delivered - what is in the headers depends\r
138 >>       * on the MTAs used along the way. So we are trying a number of\r
139 >> @@ -405,23 +400,13 @@ guess_from_received_header (notmuch_config_t *config, notmuch_message_t *message\r
140 >>       *    'by' part of Received headers\r
141 >>       * If none of these work, we give up and return NULL\r
142 >>       */\r
143 >> -    for (i = 0; i < sizeof(to_headers)/sizeof(*to_headers); i++) {\r
144 >> -    tohdr = xstrdup(notmuch_message_get_header (message, to_headers[i]));\r
145 >> -    if (tohdr && *tohdr) {\r
146 >> -        /* tohdr is potentialy a list of email addresses, so here we\r
147 >> -         * check if one of the email addresses is a substring of tohdr\r
148 >> -         */\r
149 >> -        if (strcasestr(tohdr, primary)) {\r
150 >> -            free(tohdr);\r
151 >> -            return primary;\r
152 >> -        }\r
153 >> -        for (j = 0; j < other_len; j++)\r
154 >> -            if (strcasestr (tohdr, other[j])) {\r
155 >> -                free(tohdr);\r
156 >> -                return other[j];\r
157 >> -            }\r
158 >> -        free(tohdr);\r
159 >> -    }\r
160 >> +    for (i = 0; i < ARRAY_SIZE (to_headers); i++) {\r
161 >> +    const char *tohdr = notmuch_message_get_header (message, to_headers[i]);\r
162 >> +\r
163 >> +    /* Note: tohdr potentially contains a list of email addresses. */\r
164 >> +    addr = user_address_in_string (tohdr, config);\r
165 >> +    if (addr)\r
166 >> +        return addr;\r
167 >>      }\r
168 >>  \r
169 >>      /* We get the concatenated Received: headers and search from the\r
170 >> @@ -439,19 +424,12 @@ guess_from_received_header (notmuch_config_t *config, notmuch_message_t *message\r
171 >>       * header\r
172 >>       */\r
173 >>      ptr = strstr (received, " for ");\r
174 >> -    if (ptr) {\r
175 >> -    /* the text following is potentialy a list of email addresses,\r
176 >> -     * so again we check if one of the email addresses is a\r
177 >> -     * substring of ptr\r
178 >> -     */\r
179 >> -    if (strcasestr(ptr, primary)) {\r
180 >> -        return primary;\r
181 >> -    }\r
182 >> -    for (i = 0; i < other_len; i++)\r
183 >> -        if (strcasestr (ptr, other[i])) {\r
184 >> -            return other[i];\r
185 >> -        }\r
186 >> -    }\r
187 >> +\r
188 >> +    /* Note: ptr potentially contains a list of email addresses. */\r
189 >> +    addr = user_address_in_string (ptr, config);\r
190 >> +    if (addr)\r
191 >> +    return addr;\r
192 >> +\r
193 >>      /* Finally, we parse all the " by MTA ..." headers to guess the\r
194 >>       * email address that this was originally delivered to.\r
195 >>       * We extract just the MTA here by removing leading whitespace and\r
196 >> @@ -492,15 +470,11 @@ guess_from_received_header (notmuch_config_t *config, notmuch_message_t *message\r
197 >>           */\r
198 >>          *(tld-1) = '.';\r
199 >>  \r
200 >> -        if (strcasestr(primary, domain)) {\r
201 >> -            free(mta);\r
202 >> -            return primary;\r
203 >> +        addr = string_in_user_address (domain, config);\r
204 >> +        if (addr) {\r
205 >> +            free (mta);\r
206 >> +            return addr;\r
207 >>          }\r
208 >> -        for (i = 0; i < other_len; i++)\r
209 >> -            if (strcasestr (other[i],domain)) {\r
210 >> -                free(mta);\r
211 >> -                return other[i];\r
212 >> -            }\r
213 >>      }\r
214 >>      free (mta);\r
215 >>      }\r