Re: [PATCH 0/4] Allow specifying alternate names for addresses in other_email
[notmuch-archives.git] / c7 / 99a05c0dc059d8d7d080fdf66a0bb53f72903d
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 arlo.cworth.org (Postfix) with ESMTP id B37636DE33F3\r
6  for <notmuch@notmuchmail.org>; Sat, 13 Aug 2016 04:40:49 -0700 (PDT)\r
7 X-Virus-Scanned: Debian amavisd-new at cworth.org\r
8 X-Amavis-Alert: BAD HEADER SECTION, Duplicate header field: "References"\r
9 X-Spam-Flag: NO\r
10 X-Spam-Score: -0.574\r
11 X-Spam-Level: \r
12 X-Spam-Status: No, score=-0.574 tagged_above=-999 required=5 tests=[AWL=0.146,\r
13   DKIM_SIGNED=0.1, DKIM_VALID=-0.1, RCVD_IN_DNSWL_LOW=-0.7,\r
14  RCVD_IN_MSPIKE_H3=-0.01, RCVD_IN_MSPIKE_WL=-0.01] autolearn=disabled\r
15 Received: from arlo.cworth.org ([127.0.0.1])\r
16  by localhost (arlo.cworth.org [127.0.0.1]) (amavisd-new, port 10024)\r
17  with ESMTP id 5EBo5uqOBVAu for <notmuch@notmuchmail.org>;\r
18  Sat, 13 Aug 2016 04:40:41 -0700 (PDT)\r
19 Received: from mail-wm0-f65.google.com (mail-wm0-f65.google.com\r
20  [74.125.82.65]) by arlo.cworth.org (Postfix) with ESMTPS id 528266DE3961 for\r
21  <notmuch@notmuchmail.org>; Sat, 13 Aug 2016 04:39:17 -0700 (PDT)\r
22 Received: by mail-wm0-f65.google.com with SMTP id i5so2016939wmg.2\r
23  for <notmuch@notmuchmail.org>; Sat, 13 Aug 2016 04:39:17 -0700 (PDT)\r
24 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;\r
25  d=nikula-org.20150623.gappssmtp.com; s=20150623;\r
26  h=from:to:cc:subject:date:message-id:in-reply-to:references\r
27  :in-reply-to:references;\r
28  bh=pcw6+Tq9er4sa7ROzNemL06kbs8q3+z/oOQXlztCE88=;\r
29  b=sbzxgVh77OTRgCS2fy6ZyXpGO2DUrHQHBnwwr4b1KBfp4MwpPP7BB/eXow9SEP8vxn\r
30  PJwPPAr1J0iyI2IXJ8YyaXjmhq/kiocaXUhkJDXZIjWg8lG19Vva085CcZUll0JlrLTp\r
31  wvYMeg0jm/5Hm1IX28cAsM8XM32QP73XUzXqC7JZoOdC6ac4VGiCuG2JE34qbFK/acRs\r
32  qvviOsAB5L0Yvyfp3RIln1WBJ9oL0F6eG2y34iqyDip5IFVffDVrVJ8CsMK9F3fsSe46\r
33  DwVYzaWBHYvPoO46u8mIf3XfuEgHqo87X4IN8EWYZJc+CmiM1AYxQNZng/XPuRNWT59e\r
34  0luw==\r
35 X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;\r
36  d=1e100.net; s=20130820;\r
37  h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to\r
38  :references:in-reply-to:references;\r
39  bh=pcw6+Tq9er4sa7ROzNemL06kbs8q3+z/oOQXlztCE88=;\r
40  b=I7V5O1X7F9rDd2yS+wC0tLpkXHJoNu8RBUpQ4asiwFj+10vuQkeuDPEHX6N758LX9e\r
41  ifMq89VtoP0+6I5R3jLcDjb0wWuZkM23tT412tLAuMqUM1f/ZVreWiId6WXwEnIpS5nH\r
42  iALPmZDOSuWcDoOtOvKEF9jcHVSnK2JmeQHDpIHdwCzGuNZI2vHus50U4FtzOKRTWnBH\r
43  6uuvPbvdHy8uVYDV00H62/tCFfwzv1eHKwoITTl3Xc0qh5Y4CGWg71H1buo0gtuZSvDC\r
44  f0HZtJ/lvQHab9CMPZt3AthfRmrBr1no9QpHVj+Fa3/+0pW81/IZUpHc99sTOJwJmX+g\r
45  tG3Q==\r
46 X-Gm-Message-State:\r
47  AEkoous7h0BbGYXDUYsw0W9SHOyTapM/6VKnNRYewi43JZ217/hdrkOJILrQnCr2ZfOjRw==\r
48 X-Received: by 10.194.136.196 with SMTP id qc4mr23185541wjb.136.1471088355908;\r
49   Sat, 13 Aug 2016 04:39:15 -0700 (PDT)\r
50 Received: from localhost (mobile-access-bcee63-250.dhcp.inet.fi.\r
51  [188.238.99.250])\r
52  by smtp.gmail.com with ESMTPSA id a184sm6950592wmh.1.2016.08.13.04.39.15\r
53  (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128);\r
54  Sat, 13 Aug 2016 04:39:15 -0700 (PDT)\r
55 From: Jani Nikula <jani@nikula.org>\r
56 To: notmuch@notmuchmail.org\r
57 Cc: Daniel Kahn Gillmor <dkg@fifthhorseman.net>,\r
58         jani@nikula.org\r
59 Subject: [PATCH v2 09/14] cli/reply: use dedicated functions for reply to\r
60  mapping\r
61 Date: Sat, 13 Aug 2016 14:37:33 +0300\r
62 Message-Id:\r
63  <9a4c17307d2fe3865fd62b38cb9195c1eba31c14.1471088022.git.jani@nikula.org>\r
64 X-Mailer: git-send-email 2.1.4\r
65 In-Reply-To: <cover.1471088022.git.jani@nikula.org>\r
66 References: <cover.1471088022.git.jani@nikula.org>\r
67 In-Reply-To: <cover.1471088022.git.jani@nikula.org>\r
68 References: <cover.1471088022.git.jani@nikula.org>\r
69 X-BeenThere: notmuch@notmuchmail.org\r
70 X-Mailman-Version: 2.1.20\r
71 Precedence: list\r
72 List-Id: "Use and development of the notmuch mail system."\r
73  <notmuch.notmuchmail.org>\r
74 List-Unsubscribe: <https://notmuchmail.org/mailman/options/notmuch>,\r
75  <mailto:notmuch-request@notmuchmail.org?subject=unsubscribe>\r
76 List-Archive: <http://notmuchmail.org/pipermail/notmuch/>\r
77 List-Post: <mailto:notmuch@notmuchmail.org>\r
78 List-Help: <mailto:notmuch-request@notmuchmail.org?subject=help>\r
79 List-Subscribe: <https://notmuchmail.org/mailman/listinfo/notmuch>,\r
80  <mailto:notmuch-request@notmuchmail.org?subject=subscribe>\r
81 X-List-Received-Date: Sat, 13 Aug 2016 11:40:49 -0000\r
82 \r
83 The main motivation here is to move the special casing around\r
84 reply-to/from handling into a function of its own, clarifying the main\r
85 logic.\r
86 ---\r
87  notmuch-reply.c | 82 ++++++++++++++++++++++++++++++++++-----------------------\r
88  1 file changed, 49 insertions(+), 33 deletions(-)\r
89 \r
90 diff --git a/notmuch-reply.c b/notmuch-reply.c\r
91 index b380678e7204..9b78ea2c2b20 100644\r
92 --- a/notmuch-reply.c\r
93 +++ b/notmuch-reply.c\r
94 @@ -256,17 +256,13 @@ scan_address_string (const char *recipients,\r
95   * in either the 'To' or 'Cc' header of the message?\r
96   */\r
97  static int\r
98 -reply_to_header_is_redundant (notmuch_message_t *message)\r
99 +reply_to_header_is_redundant (notmuch_message_t *message, const char *reply_to)\r
100  {\r
101 -    const char *reply_to, *to, *cc, *addr;\r
102 +    const char *to, *cc, *addr;\r
103      InternetAddressList *list;\r
104      InternetAddress *address;\r
105      InternetAddressMailbox *mailbox;\r
106  \r
107 -    reply_to = notmuch_message_get_header (message, "reply-to");\r
108 -    if (reply_to == NULL || *reply_to == '\0')\r
109 -       return 0;\r
110 -\r
111      list = internet_address_list_parse_string (reply_to);\r
112  \r
113      if (internet_address_list_length (list) != 1)\r
114 @@ -291,6 +287,47 @@ reply_to_header_is_redundant (notmuch_message_t *message)\r
115      return 0;\r
116  }\r
117  \r
118 +static const char *get_sender(notmuch_message_t *message)\r
119 +{\r
120 +    const char *reply_to;\r
121 +\r
122 +    reply_to = notmuch_message_get_header (message, "reply-to");\r
123 +    if (reply_to && *reply_to) {\r
124 +        /*\r
125 +        * Some mailing lists munge the Reply-To header despite it\r
126 +        * being A Bad Thing, see\r
127 +        * http://marc.merlins.org/netrants/reply-to-harmful.html\r
128 +        *\r
129 +        * The munging is easy to detect, because it results in a\r
130 +        * redundant reply-to header, (with an address that already\r
131 +        * exists in either To or Cc). So in this case, we ignore the\r
132 +        * Reply-To field and use the From header. This ensures the\r
133 +        * original sender will get the reply even if not subscribed\r
134 +        * to the list. Note that the address in the Reply-To header\r
135 +        * will always appear in the reply if reply_all is true.\r
136 +        */\r
137 +       if (! reply_to_header_is_redundant (message, reply_to))\r
138 +           return reply_to;\r
139 +    }\r
140 +\r
141 +    return notmuch_message_get_header (message, "from");\r
142 +}\r
143 +\r
144 +static const char *get_to(notmuch_message_t *message)\r
145 +{\r
146 +    return notmuch_message_get_header (message, "to");\r
147 +}\r
148 +\r
149 +static const char *get_cc(notmuch_message_t *message)\r
150 +{\r
151 +    return notmuch_message_get_header (message, "cc");\r
152 +}\r
153 +\r
154 +static const char *get_bcc(notmuch_message_t *message)\r
155 +{\r
156 +    return notmuch_message_get_header (message, "bcc");\r
157 +}\r
158 +\r
159  /* Augment the recipients of 'reply' from the "Reply-to:", "From:",\r
160   * "To:", "Cc:", and "Bcc:" headers of 'message'.\r
161   *\r
162 @@ -310,43 +347,22 @@ add_recipients_from_message (GMimeMessage *reply,\r
163                              notmuch_bool_t reply_all)\r
164  {\r
165      struct {\r
166 -       const char *header;\r
167 -       const char *fallback;\r
168 +       const char * (*get_header)(notmuch_message_t *message);\r
169         GMimeRecipientType recipient_type;\r
170      } reply_to_map[] = {\r
171 -       { "reply-to", "from", GMIME_RECIPIENT_TYPE_TO  },\r
172 -       { "to",         NULL, GMIME_RECIPIENT_TYPE_TO  },\r
173 -       { "cc",         NULL, GMIME_RECIPIENT_TYPE_CC  },\r
174 -       { "bcc",        NULL, GMIME_RECIPIENT_TYPE_BCC }\r
175 +       { get_sender,   GMIME_RECIPIENT_TYPE_TO },\r
176 +       { get_to,       GMIME_RECIPIENT_TYPE_TO },\r
177 +       { get_cc,       GMIME_RECIPIENT_TYPE_CC },\r
178 +       { get_bcc,      GMIME_RECIPIENT_TYPE_BCC },\r
179      };\r
180      const char *from_addr = NULL;\r
181      unsigned int i;\r
182      unsigned int n = 0;\r
183  \r
184 -    /* Some mailing lists munge the Reply-To header despite it being A Bad\r
185 -     * Thing, see http://marc.merlins.org/netrants/reply-to-harmful.html\r
186 -     *\r
187 -     * The munging is easy to detect, because it results in a\r
188 -     * redundant reply-to header, (with an address that already exists\r
189 -     * in either To or Cc). So in this case, we ignore the Reply-To\r
190 -     * field and use the From header. This ensures the original sender\r
191 -     * will get the reply even if not subscribed to the list. Note\r
192 -     * that the address in the Reply-To header will always appear in\r
193 -     * the reply if reply_all is true.\r
194 -     */\r
195 -    if (reply_to_header_is_redundant (message)) {\r
196 -       reply_to_map[0].header = "from";\r
197 -       reply_to_map[0].fallback = NULL;\r
198 -    }\r
199 -\r
200      for (i = 0; i < ARRAY_SIZE (reply_to_map); i++) {\r
201         const char *recipients;\r
202  \r
203 -       recipients = notmuch_message_get_header (message,\r
204 -                                                reply_to_map[i].header);\r
205 -       if ((recipients == NULL || recipients[0] == '\0') && reply_to_map[i].fallback)\r
206 -           recipients = notmuch_message_get_header (message,\r
207 -                                                    reply_to_map[i].fallback);\r
208 +       recipients = reply_to_map[i].get_header (message);\r
209  \r
210         n += scan_address_string (recipients, config, reply,\r
211                                   reply_to_map[i].recipient_type, &from_addr);\r
212 -- \r
213 2.1.4\r
214 \r