Re: [PATCH v2] Omit User-Agent: header by default
[notmuch-archives.git] / 13 / f2dcbb483ebfd5772d325be25e8fdeab0fd9b0
1 Return-Path: <BATV+3d395db9405134d2e00f+2417+infradead.org+hohndel@bombadil.srs.infradead.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 B3752407E8C\r
6         for <notmuch@notmuchmail.org>; Tue,  6 Apr 2010 12:45:59 -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: -2.8\r
10 X-Spam-Level: \r
11 X-Spam-Status: No, score=-2.8 tagged_above=-999 required=5\r
12         tests=[BAYES_05=-0.5, RCVD_IN_DNSWL_MED=-2.3] autolearn=ham\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 gNUVR3ry58ci for <notmuch@notmuchmail.org>;\r
16         Tue,  6 Apr 2010 12:45:58 -0700 (PDT)\r
17 Received: from bombadil.infradead.org (bombadil.infradead.org [18.85.46.34])\r
18         by olra.theworths.org (Postfix) with ESMTP id C9BC1407E88\r
19         for <notmuch@notmuchmail.org>; Tue,  6 Apr 2010 12:45:58 -0700 (PDT)\r
20 Received: from localhost ([::1] helo=localhost.localdomain)\r
21         by bombadil.infradead.org with esmtp (Exim 4.69 #1 (Red Hat Linux))\r
22         id 1NzEik-0004Gj-7I\r
23         for notmuch@notmuchmail.org; Tue, 06 Apr 2010 19:45:58 +0000\r
24 Received: by localhost.localdomain (Postfix, from userid 500)\r
25         id D9C34C007F; Tue,  6 Apr 2010 12:45:57 -0700 (PDT)\r
26 From: Dirk Hohndel <hohndel@infradead.org>\r
27 To: notmuch <notmuch@notmuchmail.org>\r
28 Subject: [PATCH 2/2] guess From address from Received headers \r
29 Date: Tue, 06 Apr 2010 12:45:57 -0700\r
30 Message-ID: <m3sk78xt3e.fsf@x200.gr8dns.org>\r
31 MIME-Version: 1.0\r
32 Content-Type: text/plain; charset=us-ascii\r
33 X-SRS-Rewrite: SMTP reverse-path rewritten from <hohndel@infradead.org> by\r
34         bombadil.infradead.org See http://www.infradead.org/rpr.html\r
35 X-BeenThere: notmuch@notmuchmail.org\r
36 X-Mailman-Version: 2.1.13\r
37 Precedence: list\r
38 List-Id: "Use and development of the notmuch mail system."\r
39         <notmuch.notmuchmail.org>\r
40 List-Unsubscribe: <http://notmuchmail.org/mailman/options/notmuch>,\r
41         <mailto:notmuch-request@notmuchmail.org?subject=unsubscribe>\r
42 List-Archive: <http://notmuchmail.org/pipermail/notmuch>\r
43 List-Post: <mailto:notmuch@notmuchmail.org>\r
44 List-Help: <mailto:notmuch-request@notmuchmail.org?subject=help>\r
45 List-Subscribe: <http://notmuchmail.org/mailman/listinfo/notmuch>,\r
46         <mailto:notmuch-request@notmuchmail.org?subject=subscribe>\r
47 X-List-Received-Date: Tue, 06 Apr 2010 19:45:59 -0000\r
48 \r
49 \r
50  When replying to a message notmuch tries to pick the correct From\r
51  address by looking which one of a user's configured email addresses\r
52  were included in To or Cc headers of the email that is being replied to.\r
53  If none of the users email addresses are in the To or Cc headers we now\r
54  try to guess from the first (chronologically, last) Received header\r
55  which domain this email was received in and therefore which of the\r
56  email addresses to use in a reply\r
57  If that fails we still use the primary email as From email\r
58 \r
59 \r
60 Signed-off-by: Dirk Hohndel <hohndel@infradead.org>\r
61 ---\r
62  notmuch-reply.c |   73 ++++++++++++++++++++++++++++++++++++++++++++++++++++--\r
63  1 files changed, 70 insertions(+), 3 deletions(-)\r
64 \r
65 diff --git a/notmuch-reply.c b/notmuch-reply.c\r
66 index 6c15536..3c5b253 100644\r
67 --- a/notmuch-reply.c\r
68 +++ b/notmuch-reply.c\r
69 @@ -282,6 +282,69 @@ add_recipients_from_message (GMimeMessage *reply,\r
70      return from_addr;\r
71  }\r
72  \r
73 +\r
74 +static const char *\r
75 +guess_from_received_header(notmuch_config_t *config, notmuch_message_t *message)\r
76 +{\r
77 +    const char *received,*primary;\r
78 +    char **other;\r
79 +    char *by,*mta,*ptr,*token;\r
80 +    char *domain=NULL;\r
81 +    char *tld=NULL;\r
82 +    const char *delim=". \t";\r
83 +    size_t i,other_len;\r
84 +\r
85 +    received = notmuch_message_get_header (message, "received");\r
86 +    by = strstr(received," by ");\r
87 +    if (by && *(by+4)) {\r
88 +       /* we know that there are 4 characters after by - either the 4th one\r
89 +        * is '\0' (broken header) or it is the first letter of the hostname \r
90 +        * that last received this email - which we'll use to guess the right\r
91 +        * from email address\r
92 +        */\r
93 +       mta = strdup(by+4);\r
94 +       if (mta == NULL)\r
95 +           return NULL;\r
96 +       /* After the MTA comes its IP address (or HELO response) in parenthesis.\r
97 +        * so let's terminate the string there\r
98 +        */\r
99 +       if ((ptr = strchr(mta,'(')) == NULL) {\r
100 +           free (mta);\r
101 +           return NULL;\r
102 +       }\r
103 +       *ptr = '\0';\r
104 +       /* Now extract the last two components of the MTA host name\r
105 +        * as domain and tld\r
106 +        */\r
107 +       token = mta;\r
108 +       while ((ptr = strsep(&token,delim)) != NULL) {\r
109 +           if (*ptr == '\0')\r
110 +               continue;\r
111 +           domain = tld;\r
112 +           tld = ptr;\r
113 +       }\r
114 +       if (domain) {\r
115 +           /* recombine domain and tld and look for it among the configured\r
116 +            * email addresses\r
117 +            */\r
118 +           *(tld-1) = '.';\r
119 +           primary = notmuch_config_get_user_primary_email (config);\r
120 +           if (strcasestr (primary, domain)) {\r
121 +               free(mta);\r
122 +               return primary;\r
123 +           }\r
124 +           other = notmuch_config_get_user_other_email (config, &other_len);\r
125 +           for (i = 0; i < other_len; i++)\r
126 +               if (strcasestr (other[i], domain)) {\r
127 +                   free(mta);\r
128 +                   return other[i];\r
129 +               }\r
130 +       }\r
131 +       free(mta);\r
132 +    }\r
133 +    return NULL;\r
134 +}\r
135 +\r
136  static int\r
137  notmuch_reply_format_default(void *ctx, notmuch_config_t *config, notmuch_query_t *query)\r
138  {\r
139 @@ -312,9 +375,13 @@ notmuch_reply_format_default(void *ctx, notmuch_config_t *config, notmuch_query_\r
140         g_mime_message_set_subject (reply, subject);\r
141  \r
142         from_addr = add_recipients_from_message (reply, config, message);\r
143 -\r
144 -       if (from_addr == NULL)\r
145 -           from_addr = notmuch_config_get_user_primary_email (config);\r
146 +       \r
147 +       if (from_addr == NULL) {\r
148 +           from_addr = guess_from_received_header (config, message);\r
149 +           if (from_addr == NULL) {\r
150 +               from_addr = notmuch_config_get_user_primary_email (config);\r
151 +           }\r
152 +       }\r
153  \r
154         from_addr = talloc_asprintf (ctx, "%s <%s>",\r
155                                      notmuch_config_get_user_name (config),\r
156 -- \r
157 1.6.6.1\r
158 \r
159 \r
160 -- \r
161 Dirk Hohndel\r
162 Intel Open Source Technology Center\r