[PATCH v2 11/14] cli/reply: return internet address list from get header funcs
[notmuch-archives.git] / a1 / c8e19152240f83eaf4352fb05e9b7205ae655e
1 Return-Path: <five9a2@gmail.com>\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 8FD34431FC0\r
6         for <notmuch@notmuchmail.org>; Sat, 28 Nov 2009 15:19:06 -0800 (PST)\r
7 X-Virus-Scanned: Debian amavisd-new at olra.theworths.org\r
8 Received: from olra.theworths.org ([127.0.0.1])\r
9         by localhost (olra.theworths.org [127.0.0.1]) (amavisd-new, port 10024)\r
10         with ESMTP id SWeJOEkynNik for <notmuch@notmuchmail.org>;\r
11         Sat, 28 Nov 2009 15:19:04 -0800 (PST)\r
12 Received: from mail-bw0-f224.google.com (mail-bw0-f224.google.com\r
13         [209.85.218.224])\r
14         by olra.theworths.org (Postfix) with ESMTP id 6880D431FAE\r
15         for <notmuch@notmuchmail.org>; Sat, 28 Nov 2009 15:19:04 -0800 (PST)\r
16 Received: by mail-bw0-f224.google.com with SMTP id 24so1833687bwz.30\r
17         for <notmuch@notmuchmail.org>; Sat, 28 Nov 2009 15:19:04 -0800 (PST)\r
18 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma;\r
19         h=domainkey-signature:received:received:sender:from:to:cc:subject\r
20         :date:message-id:x-mailer:in-reply-to:references;\r
21         bh=kO125PLjZtb5FvHOo+R/CUpNJ/nx/2inV3hKZNIFiiM=;\r
22         b=ks7U+74LRxbz4pU7f+pb7UxAHT7AhgOYtmSyKbEMRphlVqBu1bsM5wppV03OMlo3SV\r
23         vRoV5e9q/uFOVaYYwHtZERzmqWfxGSWFnhdL7qrDs8HdkxR6oixI1J7XdBhoPq9cg6vb\r
24         uYPbKHBSJ198qJ0HoEca5Pq8TegUwdf9fENWg=\r
25 DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma;\r
26         h=sender:from:to:cc:subject:date:message-id:x-mailer:in-reply-to\r
27         :references;\r
28         b=vXY5wtuiNlLQdZz9AUHDWdd0YyqkmoNLEOJkRsYx3HW2PR/hAq8zHq620FDKocQzyO\r
29         q93EHzYbA5ohDcJXq1UdlE0sIx9vhbQtaoDPn3RodgqJsutMOIKlJbhq9SJh8H5HvDdq\r
30         EdvsTOonlOB8bnl3/Qt8tAsbiGo6QBRLuUuds=\r
31 Received: by 10.204.34.71 with SMTP id k7mr2581476bkd.206.1259450344114;\r
32         Sat, 28 Nov 2009 15:19:04 -0800 (PST)\r
33 Received: from localhost.localdomain (vawpc43.ethz.ch [129.132.59.11])\r
34         by mx.google.com with ESMTPS id 15sm965320fxm.14.2009.11.28.15.19.02\r
35         (version=TLSv1/SSLv3 cipher=RC4-MD5);\r
36         Sat, 28 Nov 2009 15:19:03 -0800 (PST)\r
37 Sender: Jed Brown <five9a2@gmail.com>\r
38 From: Jed Brown <jed@59A2.org>\r
39 To: cworth@cworth.org\r
40 Date: Sun, 29 Nov 2009 00:19:36 +0100\r
41 Message-Id: <1259450376-24523-2-git-send-email-jed@59A2.org>\r
42 X-Mailer: git-send-email 1.6.5.3\r
43 In-Reply-To: <1259450376-24523-1-git-send-email-jed@59A2.org>\r
44 References: <87r5ripfy2.fsf@yoom.home.cworth.org>\r
45         <1259450376-24523-1-git-send-email-jed@59A2.org>\r
46 Cc: notmuch@notmuchmail.org\r
47 Subject: [notmuch] [PATCH 2/2] notmuch-reply.c: Handle munged `Reply-To'\r
48         headers.\r
49 X-BeenThere: notmuch@notmuchmail.org\r
50 X-Mailman-Version: 2.1.12\r
51 Precedence: list\r
52 List-Id: "Use and development of the notmuch mail system."\r
53         <notmuch.notmuchmail.org>\r
54 List-Unsubscribe: <http://notmuchmail.org/mailman/options/notmuch>,\r
55         <mailto:notmuch-request@notmuchmail.org?subject=unsubscribe>\r
56 List-Archive: <http://notmuchmail.org/pipermail/notmuch>\r
57 List-Post: <mailto:notmuch@notmuchmail.org>\r
58 List-Help: <mailto:notmuch-request@notmuchmail.org?subject=help>\r
59 List-Subscribe: <http://notmuchmail.org/mailman/listinfo/notmuch>,\r
60         <mailto:notmuch-request@notmuchmail.org?subject=subscribe>\r
61 X-List-Received-Date: Sat, 28 Nov 2009 23:19:07 -0000\r
62 \r
63 Some mailing lists engage in the evil practice of changing the Reply-To\r
64 header so that replies from all mailers go to the list by default, at\r
65 the expense of not responding to the person who actually sent the\r
66 message.  When this is detected, we reply to `From' and remove the\r
67 duplicate response to the mailing list.  Consider a reply to the\r
68 following message.\r
69 \r
70   From: Some User <some.user@example.com>\r
71   To: Sample users list <sample-users@sample.org>\r
72   Reply-To: Sample users list <sample-users@sample.org>\r
73 \r
74 Prior to this patch, `notmuch reply' produces\r
75 \r
76   To: Sample users list <sample-users@sample.org>,\r
77       Sample users list <sample-users@sample.org>\r
78 \r
79 and after the patch,\r
80 \r
81   To: Some User <some.user@example.com>,\r
82       Sample users list <sample-users@sample.org>\r
83 \r
84 Signed-off-by: Jed Brown <jed@59A2.org>\r
85 ---\r
86  notmuch-reply.c |   47 ++++++++++++++++++++++++++++++++++++++++++++++-\r
87  1 files changed, 46 insertions(+), 1 deletions(-)\r
88 \r
89 diff --git a/notmuch-reply.c b/notmuch-reply.c\r
90 index b91a830..9b9e9ab 100644\r
91 --- a/notmuch-reply.c\r
92 +++ b/notmuch-reply.c\r
93 @@ -182,6 +182,39 @@ add_recipients_for_string (GMimeMessage *message,\r
94      return add_recipients_for_address_list (message, config, type, list);\r
95  }\r
96  \r
97 +/* Some mailing lists munge the Reply-To header despite it being A Bad\r
98 + * Thing, see http://www.unicom.com/pw/reply-to-harmful.html\r
99 + *\r
100 + * This function detects such munging so that reasonable headers can be\r
101 + * generated anyway.  Returns 1 if munged, else 0.\r
102 + *\r
103 + * The current logic is fairly naive, Reply-To is diagnosed as munged if\r
104 + * it contains exactly one address, and this address is also present in\r
105 + * the To or Cc fields.\r
106 + */\r
107 +static int\r
108 +mailing_list_munged_reply_to (notmuch_message_t *message)\r
109 +{\r
110 +    const char *header, *addr;\r
111 +    InternetAddressList *list;\r
112 +    InternetAddress *address;\r
113 +    InternetAddressMailbox *mailbox;\r
114 +\r
115 +    header = notmuch_message_get_header (message, "reply-to");\r
116 +    list = internet_address_list_parse_string (header);\r
117 +    if (internet_address_list_length (list) != 1)\r
118 +       return 0;\r
119 +    address = internet_address_list_get_address (list, 0);\r
120 +    if (INTERNET_ADDRESS_IS_GROUP (address))\r
121 +       return 0;\r
122 +    mailbox = INTERNET_ADDRESS_MAILBOX (address);\r
123 +    addr = internet_address_mailbox_get_addr (mailbox);\r
124 +    /* Note that strcasestr() is a GNU extension, strstr() might be sufficient */\r
125 +    if (strcasestr (notmuch_message_get_header (message, "to"), addr) == 0 ||\r
126 +       strcasestr (notmuch_message_get_header (message, "cc"), addr) == 0)\r
127 +       return 1;\r
128 +    return 0; }\r
129 +\r
130  /* Augments the recipients of reply from the headers of message.\r
131   *\r
132   * If any of the user's addresses were found in these headers, the first\r
133 @@ -192,7 +225,7 @@ add_recipients_from_message (GMimeMessage *reply,\r
134                              notmuch_config_t *config,\r
135                              notmuch_message_t *message)\r
136  {\r
137 -    static const struct {\r
138 +    struct {\r
139         const char *header;\r
140         const char *fallback;\r
141         GMimeRecipientType recipient_type;\r
142 @@ -205,6 +238,18 @@ add_recipients_from_message (GMimeMessage *reply,\r
143      const char *from_addr = NULL;\r
144      unsigned int i;\r
145  \r
146 +    /* When we have detected Reply-To munging, we ignore the Reply-To\r
147 +     * field (because it appears in the To or Cc headers) and use the\r
148 +     * From header so that person will get pinged and will actually\r
149 +     * receive the response if not subscribed to the list.  Note that\r
150 +     * under no circumstances does this fail to reply to the address in\r
151 +     * the Reply-To header.\r
152 +     */\r
153 +    if (mailing_list_munged_reply_to (message)) {\r
154 +       reply_to_map[0].header = "from";\r
155 +       reply_to_map[0].fallback = NULL;\r
156 +    }\r
157 +\r
158      for (i = 0; i < ARRAY_SIZE (reply_to_map); i++) {\r
159         const char *addr, *recipients;\r
160  \r
161 -- \r
162 1.6.5.3\r
163 \r