From f699e1f14451d0416be1c8cbdc67499d5e9bfb1e Mon Sep 17 00:00:00 2001 From: Shea Levy Date: Wed, 10 Aug 2016 16:55:16 +2000 Subject: [PATCH] [PATCH 2/4] notmuch-reply: respect users.other_name in From --- 90/02b5bb20fff90757715070c134e45355ea7289 | 414 ++++++++++++++++++++++ 1 file changed, 414 insertions(+) create mode 100644 90/02b5bb20fff90757715070c134e45355ea7289 diff --git a/90/02b5bb20fff90757715070c134e45355ea7289 b/90/02b5bb20fff90757715070c134e45355ea7289 new file mode 100644 index 000000000..a02ab8fb2 --- /dev/null +++ b/90/02b5bb20fff90757715070c134e45355ea7289 @@ -0,0 +1,414 @@ +Return-Path: +X-Original-To: notmuch@notmuchmail.org +Delivered-To: notmuch@notmuchmail.org +Received: from localhost (localhost [127.0.0.1]) + by arlo.cworth.org (Postfix) with ESMTP id 048916DE091B + for ; Tue, 9 Aug 2016 14:05:53 -0700 (PDT) +X-Virus-Scanned: Debian amavisd-new at cworth.org +X-Spam-Flag: NO +X-Spam-Score: -0.006 +X-Spam-Level: +X-Spam-Status: No, score=-0.006 tagged_above=-999 required=5 + tests=[AWL=-0.006, RCVD_IN_DNSWL_NONE=-0.0001] autolearn=disabled +Received: from arlo.cworth.org ([127.0.0.1]) + by localhost (arlo.cworth.org [127.0.0.1]) (amavisd-new, port 10024) + with ESMTP id DnX_K33TDuaw for ; + Tue, 9 Aug 2016 14:05:45 -0700 (PDT) +Received: from smtprelay.hostedemail.com (smtprelay0047.hostedemail.com + [216.40.44.47]) + by arlo.cworth.org (Postfix) with ESMTPS id DE8A86DE0362 + for ; Tue, 9 Aug 2016 14:05:44 -0700 (PDT) +Received: from smtprelay.hostedemail.com (10.5.19.251.rfc1918.com + [10.5.19.251]) + by smtpgrave03.hostedemail.com (Postfix) with ESMTP id 69C1128F2FB + for ; Tue, 9 Aug 2016 20:56:42 +0000 (UTC) +Received: from filter.hostedemail.com (unknown [216.40.38.60]) + by smtprelay01.hostedemail.com (Postfix) with ESMTP id 19F2C23421 + for ; Tue, 9 Aug 2016 20:56:41 +0000 (UTC) +X-Session-Marker: 7368656140736865616C6576792E636F6D +X-Spam-Summary: 10, 1, 0, , d41d8cd98f00b204, shea@shealevy.com, :, + RULES_HIT:1:41:69:355:379:541:800:960:966:973:988:989:1260:1321:1345:1359:1381:1437:1730:1747:1777:1792:2196:2198:2199:2200:2393:2559:2562:2639:2693:2731:2737:2898:3138:3139:3140:3141:3142:3354:3608:3865:3866:3867:3868:3870:3871:3872:3874:4250:4321:4385:4605:5007:6119:6248:6261:7903:7904:7974:8660:8957:9010:10848:11026:11473:11658:11914:12043:12291:12294:12296:12438:12517:12519:12555:12683:13148:13230:14110:14394:21063:21080:21324:21433:30003:30022:30029:30034:30051:30054:30070, 0, RBL:none, CacheIP:none, Bayesian:0.5, 0.5, 0.5, Netcheck:none, + DomainCache:0, MSF:not bulk, SPF:fn, MSBL:0, DNSBL:none, Custom_rules:0:1:0, + LFtime:18, LUA_SUMMARY:none +X-HE-Tag: loaf36_1d227d9b5a82d +X-Filterd-Recvd-Size: 14326 +Received: from shlevy-laptop.local.tld (unknown [184.209.6.164]) + (Authenticated sender: shea@shealevy.com) + by omf08.hostedemail.com (Postfix) with ESMTPA + for ; Tue, 9 Aug 2016 20:56:39 +0000 (UTC) +From: Shea Levy +To: notmuch@notmuchmail.org +Subject: [PATCH 2/4] notmuch-reply: respect users.other_name in From +Date: Tue, 9 Aug 2016 16:55:16 -0400 +Message-Id: <1470776118-5070-3-git-send-email-shea@shealevy.com> +X-Mailer: git-send-email 2.7.4 +In-Reply-To: <1470776118-5070-1-git-send-email-shea@shealevy.com> +References: <1470776118-5070-1-git-send-email-shea@shealevy.com> +X-BeenThere: notmuch@notmuchmail.org +X-Mailman-Version: 2.1.20 +Precedence: list +List-Id: "Use and development of the notmuch mail system." + +List-Unsubscribe: , + +List-Archive: +List-Post: +List-Help: +List-Subscribe: , + +X-List-Received-Date: Tue, 09 Aug 2016 21:05:53 -0000 + +--- + notmuch-reply.c | 129 ++++++++++++++++++++++++++++++++++++++++---------------- + 1 file changed, 92 insertions(+), 37 deletions(-) + +diff --git a/notmuch-reply.c b/notmuch-reply.c +index 4951373..1c205f9 100644 +--- a/notmuch-reply.c ++++ b/notmuch-reply.c +@@ -118,25 +118,41 @@ match_address (const char *str, const char *address, address_match_t mode) + } + + /* Match given string against user's configured "primary" and "other" +- * addresses according to mode. */ ++ * addresses according to mode. ++ * ++ * If 'user_from_name' is non-NULL and the address is found, ++ * *user_from_name is set to the corresponding name. ++ */ + static const char * +-address_match (const char *str, notmuch_config_t *config, address_match_t mode) ++address_match (const char *str, notmuch_config_t *config, const char **user_from_name, address_match_t mode) + { + const char *primary; + const char **other; +- size_t i, other_len; ++ const char **other_name; ++ size_t i, other_len, other_name_len; + + if (!str || *str == '\0') + return NULL; + + primary = notmuch_config_get_user_primary_email (config); +- if (match_address (str, primary, mode)) ++ if (match_address (str, primary, mode)) { ++ if (user_from_name != NULL) ++ *user_from_name = notmuch_config_get_user_name (config); + return primary; ++ } + + other = notmuch_config_get_user_other_email (config, &other_len); + for (i = 0; i < other_len; i++) { +- if (match_address (str, other[i], mode)) ++ if (match_address (str, other[i], mode)) { ++ if (user_from_name != NULL) { ++ other_name = notmuch_config_get_user_other_name (config, &other_name_len); ++ if (i < other_name_len) ++ *user_from_name = other_name[i]; ++ else ++ *user_from_name = NULL; ++ } + return other[i]; ++ } + } + + return NULL; +@@ -144,28 +160,41 @@ address_match (const char *str, notmuch_config_t *config, address_match_t mode) + + /* Does the given string contain an address configured as one of the + * user's "primary" or "other" addresses. If so, return the matching +- * address, NULL otherwise. */ ++ * address, NULL otherwise. ++ * ++ * If 'user_from_name' is non-NULL and the address is the user's, ++ * *user_from_name is set to the corresponding name. ++ */ + static const char * +-user_address_in_string (const char *str, notmuch_config_t *config) ++user_address_in_string (const char *str, notmuch_config_t *config, const char **user_from_name) + { +- return address_match (str, config, USER_ADDRESS_IN_STRING); ++ return address_match (str, config, user_from_name, USER_ADDRESS_IN_STRING); + } + + /* Do any of the addresses configured as one of the user's "primary" + * or "other" addresses contain the given string. If so, return the +- * matching address, NULL otherwise. */ ++ * matching address, NULL otherwise. ++ * ++ * If 'user_from_name' is non-NULL and the address is the user's, ++ * *user_from_name is set to the corresponding name. ++ */ + static const char * +-string_in_user_address (const char *str, notmuch_config_t *config) ++string_in_user_address (const char *str, notmuch_config_t *config, const char **user_from_name) + { +- return address_match (str, config, STRING_IN_USER_ADDRESS); ++ return address_match (str, config, user_from_name, STRING_IN_USER_ADDRESS); + } + + /* Is the given address configured as one of the user's "primary" or +- * "other" addresses. */ ++ * "other" addresses. ++ * ++ * If 'user_from_name' is non-NULL and the address is the user's, ++ * *user_from_name is set to the corresponding name. ++ * ++ */ + static notmuch_bool_t +-address_is_users (const char *address, notmuch_config_t *config) ++address_is_users (const char *address, notmuch_config_t *config, const char **user_from_name) + { +- return address_match (address, config, STRING_IS_USER_ADDRESS) != NULL; ++ return address_match (address, config, user_from_name, STRING_IS_USER_ADDRESS) != NULL; + } + + /* Scan addresses in 'list'. +@@ -178,6 +207,9 @@ address_is_users (const char *address, notmuch_config_t *config) + * be set to the first address encountered in 'list' that is the + * user's address. + * ++ * If 'user_from_name' is non-NULL and *user_from is set by this call, ++ * *user_from_name will be set to the name corresponding to *user_from. ++ * + * Return the number of addresses added to 'message'. (If 'message' is + * NULL, the function returns 0 by definition.) + */ +@@ -186,7 +218,8 @@ scan_address_list (InternetAddressList *list, + notmuch_config_t *config, + GMimeMessage *message, + GMimeRecipientType type, +- const char **user_from) ++ const char **user_from, ++ const char **user_from_name) + { + InternetAddress *address; + int i; +@@ -203,7 +236,7 @@ scan_address_list (InternetAddressList *list, + if (group_list == NULL) + continue; + +- n += scan_address_list (group_list, config, message, type, user_from); ++ n += scan_address_list (group_list, config, message, type, user_from, user_from_name); + } else { + InternetAddressMailbox *mailbox; + const char *name; +@@ -214,7 +247,7 @@ scan_address_list (InternetAddressList *list, + name = internet_address_get_name (address); + addr = internet_address_mailbox_get_addr (mailbox); + +- if (address_is_users (addr, config)) { ++ if (address_is_users (addr, config, user_from_name)) { + if (user_from && *user_from == NULL) + *user_from = addr; + } else if (message) { +@@ -238,7 +271,8 @@ scan_address_string (const char *recipients, + notmuch_config_t *config, + GMimeMessage *message, + GMimeRecipientType type, +- const char **user_from) ++ const char **user_from, ++ const char **user_from_name) + { + InternetAddressList *list; + +@@ -249,7 +283,7 @@ scan_address_string (const char *recipients, + if (list == NULL) + return 0; + +- return scan_address_list (list, config, message, type, user_from); ++ return scan_address_list (list, config, message, type, user_from, user_from_name); + } + + /* Does the address in the Reply-To header of 'message' already appear +@@ -300,6 +334,10 @@ reply_to_header_is_redundant (notmuch_message_t *message) + * (typically this would be reply-to-sender, but also handles reply to + * user's own message in a sensible way). + * ++ * If 'from_name' is non-NULL and any of the user's addresses were found ++ * in these headers and the user specified an alternate name for that ++ * address, it will be set to the corresponding value. ++ * + * If any of the user's addresses were found in these headers, the + * first of these returned, otherwise NULL is returned. + */ +@@ -307,7 +345,8 @@ static const char * + add_recipients_from_message (GMimeMessage *reply, + notmuch_config_t *config, + notmuch_message_t *message, +- notmuch_bool_t reply_all) ++ notmuch_bool_t reply_all, ++ const char **from_name) + { + struct { + const char *header; +@@ -349,7 +388,7 @@ add_recipients_from_message (GMimeMessage *reply, + reply_to_map[i].fallback); + + n += scan_address_string (recipients, config, reply, +- reply_to_map[i].recipient_type, &from_addr); ++ reply_to_map[i].recipient_type, &from_addr, from_name); + + if (!reply_all && n) { + /* Stop adding new recipients in reply-to-sender mode if +@@ -375,10 +414,13 @@ add_recipients_from_message (GMimeMessage *reply, + * Look for the user's address in " for " in the + * received headers. + * ++ * If 'user_from_name' is non-NULL and the address is found, ++ * *user_from_name is set to the corresponding name. ++ * + * Return the address that was found, if any, and NULL otherwise. + */ + static const char * +-guess_from_in_received_for (notmuch_config_t *config, const char *received) ++guess_from_in_received_for (notmuch_config_t *config, const char *received, const char **user_from_name) + { + const char *ptr; + +@@ -386,7 +428,7 @@ guess_from_in_received_for (notmuch_config_t *config, const char *received) + if (! ptr) + return NULL; + +- return user_address_in_string (ptr, config); ++ return user_address_in_string (ptr, config, user_from_name); + } + + /* +@@ -399,10 +441,13 @@ guess_from_in_received_for (notmuch_config_t *config, const char *received) + * - and then assume that the first whitespace delimited token that + * follows is the receiving system in this step of the receive chain. + * ++ * If 'user_from_name' is non-NULL and the address is found, ++ * *user_from_name is set to the corresponding name. ++ * + * Return the address that was found, if any, and NULL otherwise. + */ + static const char * +-guess_from_in_received_by (notmuch_config_t *config, const char *received) ++guess_from_in_received_by (notmuch_config_t *config, const char *received, const char **user_from_name) + { + const char *addr; + const char *by = received; +@@ -440,7 +485,7 @@ guess_from_in_received_by (notmuch_config_t *config, const char *received) + */ + *(tld - 1) = '.'; + +- addr = string_in_user_address (domain, config); ++ addr = string_in_user_address (domain, config, user_from_name); + if (addr) { + free (mta); + return addr; +@@ -460,11 +505,15 @@ guess_from_in_received_by (notmuch_config_t *config, const char *received) + * The Received: header is special in our get_header function and is + * always concatenated. + * ++ * If 'user_from_name' is non-NULL and the address is found, ++ * *user_from_name is set to the corresponding name. ++ * + * Return the address that was found, if any, and NULL otherwise. + */ + static const char * + guess_from_in_received_headers (notmuch_config_t *config, +- notmuch_message_t *message) ++ notmuch_message_t *message, ++ const char **user_from_name) + { + const char *received, *addr; + char *sanitized; +@@ -477,9 +526,9 @@ guess_from_in_received_headers (notmuch_config_t *config, + if (! sanitized) + return NULL; + +- addr = guess_from_in_received_for (config, sanitized); ++ addr = guess_from_in_received_for (config, sanitized, user_from_name); + if (! addr) +- addr = guess_from_in_received_by (config, sanitized); ++ addr = guess_from_in_received_by (config, sanitized, user_from_name); + + talloc_free (sanitized); + +@@ -491,10 +540,13 @@ guess_from_in_received_headers (notmuch_config_t *config, + * headers: Envelope-To, X-Original-To, and Delivered-To (searched in + * that order). + * ++ * If 'user_from_name' is non-NULL and the address is found, ++ * *user_from_name is set to the corresponding name. ++ * + * Return the address that was found, if any, and NULL otherwise. + */ + static const char * +-get_from_in_to_headers (notmuch_config_t *config, notmuch_message_t *message) ++get_from_in_to_headers (notmuch_config_t *config, notmuch_message_t *message, const char **user_from_name) + { + size_t i; + const char *tohdr, *addr; +@@ -508,7 +560,7 @@ get_from_in_to_headers (notmuch_config_t *config, notmuch_message_t *message) + tohdr = notmuch_message_get_header (message, to_headers[i]); + + /* Note: tohdr potentially contains a list of email addresses. */ +- addr = user_address_in_string (tohdr, config); ++ addr = user_address_in_string (tohdr, config, user_from_name); + if (addr) + return addr; + } +@@ -523,7 +575,7 @@ create_reply_message(void *ctx, + notmuch_bool_t reply_all) + { + const char *subject, *from_addr = NULL; +- const char *in_reply_to, *orig_references, *references; ++ const char *in_reply_to, *orig_references, *references, *from_name; + + /* The 1 means we want headers in a "pretty" order. */ + GMimeMessage *reply = g_mime_message_new (1); +@@ -540,7 +592,7 @@ create_reply_message(void *ctx, + } + + from_addr = add_recipients_from_message (reply, config, +- message, reply_all); ++ message, reply_all, &from_name); + + /* + * Sadly, there is no standard way to find out to which email +@@ -555,7 +607,7 @@ create_reply_message(void *ctx, + * Delivered-To: headers. + */ + if (from_addr == NULL) +- from_addr = get_from_in_to_headers (config, message); ++ from_addr = get_from_in_to_headers (config, message, &from_name); + + /* + * Check for a (for ) clause in Received: headers, +@@ -563,14 +615,17 @@ create_reply_message(void *ctx, + * of Received: headers + */ + if (from_addr == NULL) +- from_addr = guess_from_in_received_headers (config, message); ++ from_addr = guess_from_in_received_headers (config, message, &from_name); + + /* Default to user's primary address. */ +- if (from_addr == NULL) ++ if (from_addr == NULL) { + from_addr = notmuch_config_get_user_primary_email (config); ++ from_name = notmuch_config_get_user_name (config); ++ } else if (from_name == NULL || from_name[0] == '\0') ++ from_name = notmuch_config_get_user_name (config); + + from_addr = talloc_asprintf (ctx, "%s <%s>", +- notmuch_config_get_user_name (config), ++ from_name, + from_addr); + g_mime_object_set_header (GMIME_OBJECT (reply), + "From", from_addr); +@@ -751,7 +806,7 @@ notmuch_reply_format_headers_only(void *ctx, + g_mime_object_set_header (GMIME_OBJECT (reply), + "References", references); + +- (void)add_recipients_from_message (reply, config, message, reply_all); ++ (void)add_recipients_from_message (reply, config, message, reply_all, NULL); + + reply_headers = g_mime_object_to_string (GMIME_OBJECT (reply)); + printf ("%s", reply_headers); +-- +2.7.4 + -- 2.26.2