[PATCH v2 3/6] cli: add support for replying just to the sender in "notmuch reply"
authorJani Nikula <jani@nikula.org>
Sun, 8 Jan 2012 21:48:32 +0000 (23:48 +0200)
committerW. Trevor King <wking@tremily.us>
Fri, 7 Nov 2014 17:41:46 +0000 (09:41 -0800)
2c/1c4cd8a334b76491d35a3d0d37df86e5494dbc [new file with mode: 0644]

diff --git a/2c/1c4cd8a334b76491d35a3d0d37df86e5494dbc b/2c/1c4cd8a334b76491d35a3d0d37df86e5494dbc
new file mode 100644 (file)
index 0000000..e68d80b
--- /dev/null
@@ -0,0 +1,343 @@
+Return-Path: <jani@nikula.org>\r
+X-Original-To: notmuch@notmuchmail.org\r
+Delivered-To: notmuch@notmuchmail.org\r
+Received: from localhost (localhost [127.0.0.1])\r
+       by olra.theworths.org (Postfix) with ESMTP id 0F5344150EF\r
+       for <notmuch@notmuchmail.org>; Sun,  8 Jan 2012 13:49:00 -0800 (PST)\r
+X-Virus-Scanned: Debian amavisd-new at olra.theworths.org\r
+X-Amavis-Alert: BAD HEADER SECTION, Duplicate header field: "References"\r
+X-Spam-Flag: NO\r
+X-Spam-Score: -0.7\r
+X-Spam-Level: \r
+X-Spam-Status: No, score=-0.7 tagged_above=-999 required=5\r
+       tests=[RCVD_IN_DNSWL_LOW=-0.7] autolearn=disabled\r
+Received: from olra.theworths.org ([127.0.0.1])\r
+       by localhost (olra.theworths.org [127.0.0.1]) (amavisd-new, port 10024)\r
+       with ESMTP id HXpb9YYF4SQN for <notmuch@notmuchmail.org>;\r
+       Sun,  8 Jan 2012 13:48:57 -0800 (PST)\r
+Received: from mail-ee0-f53.google.com (mail-ee0-f53.google.com\r
+ [74.125.83.53])       (using TLSv1 with cipher RC4-SHA (128/128 bits))        (No client\r
+ certificate requested)        by olra.theworths.org (Postfix) with ESMTPS id\r
+ 94DB34150EC   for <notmuch@notmuchmail.org>; Sun,  8 Jan 2012 13:48:55 -0800\r
+ (PST)\r
+Received: by mail-ee0-f53.google.com with SMTP id d41so2496075eek.26\r
+       for <notmuch@notmuchmail.org>; Sun, 08 Jan 2012 13:48:55 -0800 (PST)\r
+Received: by 10.14.14.167 with SMTP id d39mr1029406eed.3.1326059335180;\r
+       Sun, 08 Jan 2012 13:48:55 -0800 (PST)\r
+Received: from localhost (dsl-hkibrasgw4-fe5cdc00-23.dhcp.inet.fi.\r
+       [80.220.92.23])\r
+       by mx.google.com with ESMTPS id e12sm131493743eea.5.2012.01.08.13.48.53\r
+       (version=SSLv3 cipher=OTHER); Sun, 08 Jan 2012 13:48:54 -0800 (PST)\r
+From: Jani Nikula <jani@nikula.org>\r
+To: notmuch@notmuchmail.org\r
+Subject: [PATCH v2 3/6] cli: add support for replying just to the sender in\r
+       "notmuch reply"\r
+Date: Sun,  8 Jan 2012 23:48:32 +0200\r
+Message-Id:\r
+ <6e37e058190d7c06519de3e67569fbee0be45461.1326058946.git.jani@nikula.org>\r
+X-Mailer: git-send-email 1.7.5.4\r
+In-Reply-To: <cover.1326058946.git.jani@nikula.org>\r
+References: <cover.1326058946.git.jani@nikula.org>\r
+In-Reply-To: <cover.1326058946.git.jani@nikula.org>\r
+References: <cover.1325794371.git.jani@nikula.org>\r
+       <cover.1326058946.git.jani@nikula.org>\r
+X-BeenThere: notmuch@notmuchmail.org\r
+X-Mailman-Version: 2.1.13\r
+Precedence: list\r
+List-Id: "Use and development of the notmuch mail system."\r
+       <notmuch.notmuchmail.org>\r
+List-Unsubscribe: <http://notmuchmail.org/mailman/options/notmuch>,\r
+       <mailto:notmuch-request@notmuchmail.org?subject=unsubscribe>\r
+List-Archive: <http://notmuchmail.org/pipermail/notmuch>\r
+List-Post: <mailto:notmuch@notmuchmail.org>\r
+List-Help: <mailto:notmuch-request@notmuchmail.org?subject=help>\r
+List-Subscribe: <http://notmuchmail.org/mailman/listinfo/notmuch>,\r
+       <mailto:notmuch-request@notmuchmail.org?subject=subscribe>\r
+X-List-Received-Date: Sun, 08 Jan 2012 21:49:00 -0000\r
+\r
+Add new option --reply-to=(all|sender) to "notmuch reply" to select whether\r
+to reply to all (sender and all recipients), or just sender. Reply to all\r
+remains the default.\r
+\r
+Credits to Mark Walters <markwalters1009@gmail.com> for his similar earlier\r
+work where I picked up the basic idea of handling reply-to-sender in\r
+add_recipients_from_message(). All bugs are mine, though.\r
+\r
+Signed-off-by: Jani Nikula <jani@nikula.org>\r
+\r
+---\r
+\r
+Settled on --reply-to=(all|sender) per Carl's earlier suggestion\r
+(id:87pqn5cg4g.fsf@yoom.home.cworth.org) and David's approval on IRC.\r
+---\r
+ man/man1/notmuch-reply.1 |   28 +++++++++++++---\r
+ notmuch-reply.c          |   78 ++++++++++++++++++++++++++++++++++++----------\r
+ 2 files changed, 84 insertions(+), 22 deletions(-)\r
+\r
+diff --git a/man/man1/notmuch-reply.1 b/man/man1/notmuch-reply.1\r
+index 099d808..21afa35 100644\r
+--- a/man/man1/notmuch-reply.1\r
++++ b/man/man1/notmuch-reply.1\r
+@@ -14,11 +14,13 @@ Constructs a reply template for a set of messages.\r
+ To make replying to email easier,\r
+ .B notmuch reply\r
+ takes an existing set of messages and constructs a suitable mail\r
+-template. The Reply-to header (if any, otherwise From:) is used for\r
+-the To: address. Vales from the To: and Cc: headers are copied, but\r
+-not including any of the current user's email addresses (as configured\r
+-in primary_mail or other_email in the .notmuch\-config file) in the\r
+-recipient list\r
++template. The Reply-to: header (if any, otherwise From:) is used for\r
++the To: address. Unless\r
++.BR \-\-reply-to=sender\r
++is specified, values from the To: and Cc: headers are copied, but not\r
++including any of the current user's email addresses (as configured in\r
++primary_mail or other_email in the .notmuch\-config file) in the\r
++recipient list.\r
\r
+ It also builds a suitable new subject, including Re: at the front (if\r
+ not already present), and adding the message IDs of the messages being\r
+@@ -45,6 +47,22 @@ Includes subject and quoted message body.\r
+ Only produces In\-Reply\-To, References, To, Cc, and Bcc headers.\r
+ .RE\r
+ .RE\r
++.RS\r
++.TP 4\r
++.BR \-\-reply\-to= ( all | sender )\r
++.RS\r
++.TP 4\r
++.BR all " (default)"\r
++Replies to all addresses.\r
++.TP 4\r
++.BR sender\r
++Replies only to the sender. If Reply-to: header (if any, otherwise\r
++From:) is any of the current user's configured email addresses\r
++(replying to user's own message), try To:, Cc:, and Bcc: headers in\r
++this order, and use the addresses in the first that contains something\r
++other than only the user's addresses for the To: address.\r
++.RE\r
++.RE\r
\r
+ See \fBnotmuch-search-terms\fR(7)\r
+ for details of the supported syntax for <search-terms>.\r
+diff --git a/notmuch-reply.c b/notmuch-reply.c\r
+index 000f6da..71946b3 100644\r
+--- a/notmuch-reply.c\r
++++ b/notmuch-reply.c\r
+@@ -170,7 +170,7 @@ address_is_users (const char *address, notmuch_config_t *config)\r
\r
+ /* For each address in 'list' that is not configured as one of the\r
+  * user's addresses in 'config', add that address to 'message' as an\r
+- * address of 'type'.\r
++ * address of 'type', if 'add' is true.\r
+  *\r
+  * The first address encountered that *is* the user's address will be\r
+  * returned, (otherwise NULL is returned).\r
+@@ -179,7 +179,8 @@ static const char *\r
+ add_recipients_for_address_list (GMimeMessage *message,\r
+                                notmuch_config_t *config,\r
+                                GMimeRecipientType type,\r
+-                               InternetAddressList *list)\r
++                               InternetAddressList *list,\r
++                               notmuch_bool_t add)\r
+ {\r
+     InternetAddress *address;\r
+     int i;\r
+@@ -197,7 +198,7 @@ add_recipients_for_address_list (GMimeMessage *message,\r
+               continue;\r
\r
+           add_recipients_for_address_list (message, config,\r
+-                                           type, group_list);\r
++                                           type, group_list, add);\r
+       } else {\r
+           InternetAddressMailbox *mailbox;\r
+           const char *name;\r
+@@ -211,7 +212,7 @@ add_recipients_for_address_list (GMimeMessage *message,\r
+           if (address_is_users (addr, config)) {\r
+               if (ret == NULL)\r
+                   ret = addr;\r
+-          } else {\r
++          } else if (add) {\r
+               g_mime_message_add_recipient (message, type, name, addr);\r
+           }\r
+       }\r
+@@ -222,7 +223,7 @@ add_recipients_for_address_list (GMimeMessage *message,\r
\r
+ /* For each address in 'recipients' that is not configured as one of\r
+  * the user's addresses in 'config', add that address to 'message' as\r
+- * an address of 'type'.\r
++ * an address of 'type', if 'add' is true.\r
+  *\r
+  * The first address encountered that *is* the user's address will be\r
+  * returned, (otherwise NULL is returned).\r
+@@ -231,7 +232,8 @@ static const char *\r
+ add_recipients_for_string (GMimeMessage *message,\r
+                          notmuch_config_t *config,\r
+                          GMimeRecipientType type,\r
+-                         const char *recipients)\r
++                         const char *recipients,\r
++                         notmuch_bool_t add)\r
+ {\r
+     InternetAddressList *list;\r
\r
+@@ -242,7 +244,7 @@ add_recipients_for_string (GMimeMessage *message,\r
+     if (list == NULL)\r
+       return NULL;\r
\r
+-    return add_recipients_for_address_list (message, config, type, list);\r
++    return add_recipients_for_address_list (message, config, type, list, add);\r
+ }\r
\r
+ /* Does the address in the Reply-To header of 'message' already appear\r
+@@ -286,13 +288,19 @@ reply_to_header_is_redundant (notmuch_message_t *message)\r
\r
+ /* Augments the recipients of reply from the headers of message.\r
+  *\r
++ * If 'reply_all' is true, use sender and all recipients, otherwise use the\r
++ * first header that contains something other than the user's addresses\r
++ * (typically this would be reply-to-sender, but also handles reply to user's\r
++ * own message in a sensible way).\r
++ *\r
+  * If any of the user's addresses were found in these headers, the first\r
+  * of these returned, otherwise NULL is returned.\r
+  */\r
+ static const char *\r
+ add_recipients_from_message (GMimeMessage *reply,\r
+                            notmuch_config_t *config,\r
+-                           notmuch_message_t *message)\r
++                           notmuch_message_t *message,\r
++                           notmuch_bool_t reply_all)\r
+ {\r
+     struct {\r
+       const char *header;\r
+@@ -305,6 +313,7 @@ add_recipients_from_message (GMimeMessage *reply,\r
+       { "bcc",        NULL, GMIME_RECIPIENT_TYPE_BCC }\r
+     };\r
+     const char *from_addr = NULL;\r
++    notmuch_bool_t add_recipients = TRUE;\r
+     unsigned int i;\r
\r
+     /* Some mailing lists munge the Reply-To header despite it being A Bad\r
+@@ -325,6 +334,7 @@ add_recipients_from_message (GMimeMessage *reply,\r
\r
+     for (i = 0; i < ARRAY_SIZE (reply_to_map); i++) {\r
+       const char *addr, *recipients;\r
++      GMimeRecipientType recipient_type;\r
\r
+       recipients = notmuch_message_get_header (message,\r
+                                                reply_to_map[i].header);\r
+@@ -332,11 +342,37 @@ add_recipients_from_message (GMimeMessage *reply,\r
+           recipients = notmuch_message_get_header (message,\r
+                                                    reply_to_map[i].fallback);\r
\r
+-      addr = add_recipients_for_string (reply, config,\r
+-                                        reply_to_map[i].recipient_type,\r
+-                                        recipients);\r
++      /* Force recipient type in reply-to-sender mode just in case replying to\r
++       * user's own message finds recipients in Cc/Bcc fields only.\r
++       */\r
++      if (reply_all)\r
++          recipient_type = reply_to_map[i].recipient_type;\r
++      else\r
++          recipient_type = GMIME_RECIPIENT_TYPE_TO;\r
++\r
++      addr = add_recipients_for_string (reply, config, recipient_type,\r
++                                        recipients, add_recipients);\r
++\r
+       if (from_addr == NULL)\r
+           from_addr = addr;\r
++\r
++      if (!reply_all) {\r
++          /* Stop adding new recipients in reply-to-sender mode if we have\r
++           * added some recipient(s) above.\r
++           *\r
++           * This also handles the case of user replying to his own message,\r
++           * where reply-to/from is not a recipient. In this case there may be\r
++           * more than one recipient even if not replying to all.\r
++           */\r
++          InternetAddressList *list;\r
++          list = g_mime_message_get_recipients (reply, recipient_type);\r
++          if (list && internet_address_list_length (list))\r
++              add_recipients = FALSE;\r
++\r
++          /* Check if we've got a from address and all recipients we need. */\r
++          if (!add_recipients && from_addr)\r
++              break;\r
++      }\r
+     }\r
\r
+     return from_addr;\r
+@@ -480,7 +516,8 @@ static int\r
+ notmuch_reply_format_default(void *ctx,\r
+                            notmuch_config_t *config,\r
+                            notmuch_query_t *query,\r
+-                           notmuch_show_params_t *params)\r
++                           notmuch_show_params_t *params,\r
++                           notmuch_bool_t reply_all)\r
+ {\r
+     GMimeMessage *reply;\r
+     notmuch_messages_t *messages;\r
+@@ -509,7 +546,8 @@ notmuch_reply_format_default(void *ctx,\r
+           g_mime_message_set_subject (reply, subject);\r
+       }\r
\r
+-      from_addr = add_recipients_from_message (reply, config, message);\r
++      from_addr = add_recipients_from_message (reply, config, message,\r
++                                               reply_all);\r
\r
+       if (from_addr == NULL)\r
+           from_addr = guess_from_received_header (config, message);\r
+@@ -558,7 +596,8 @@ static int\r
+ notmuch_reply_format_headers_only(void *ctx,\r
+                                 notmuch_config_t *config,\r
+                                 notmuch_query_t *query,\r
+-                                unused (notmuch_show_params_t *params))\r
++                                unused (notmuch_show_params_t *params),\r
++                                notmuch_bool_t reply_all)\r
+ {\r
+     GMimeMessage *reply;\r
+     notmuch_messages_t *messages;\r
+@@ -598,7 +637,7 @@ notmuch_reply_format_headers_only(void *ctx,\r
+       g_mime_object_set_header (GMIME_OBJECT (reply),\r
+                                 "References", references);\r
\r
+-      (void)add_recipients_from_message (reply, config, message);\r
++      (void)add_recipients_from_message (reply, config, message, reply_all);\r
\r
+       reply_headers = g_mime_object_to_string (GMIME_OBJECT (reply));\r
+       printf ("%s", reply_headers);\r
+@@ -625,9 +664,10 @@ notmuch_reply_command (void *ctx, int argc, char *argv[])\r
+     notmuch_query_t *query;\r
+     char *query_string;\r
+     int opt_index, ret = 0;\r
+-    int (*reply_format_func)(void *ctx, notmuch_config_t *config, notmuch_query_t *query, notmuch_show_params_t *params);\r
++    int (*reply_format_func)(void *ctx, notmuch_config_t *config, notmuch_query_t *query, notmuch_show_params_t *params, notmuch_bool_t reply_all);\r
+     notmuch_show_params_t params = { .part = -1 };\r
+     int format = FORMAT_DEFAULT;\r
++    int reply_all = TRUE;\r
+     notmuch_bool_t decrypt = FALSE;\r
\r
+     notmuch_opt_desc_t options[] = {\r
+@@ -635,6 +675,10 @@ notmuch_reply_command (void *ctx, int argc, char *argv[])\r
+         (notmuch_keyword_t []){ { "default", FORMAT_DEFAULT },\r
+                                 { "headers-only", FORMAT_HEADERS_ONLY },\r
+                                 { 0, 0 } } },\r
++      { NOTMUCH_OPT_KEYWORD, &reply_all, "reply-to", 'r',\r
++        (notmuch_keyword_t []){ { "all", TRUE },\r
++                                { "sender", FALSE },\r
++                                { 0, 0 } } },\r
+       { NOTMUCH_OPT_BOOLEAN, &decrypt, "decrypt", 'd', 0 },\r
+       { 0, 0, 0, 0, 0 }\r
+     };\r
+@@ -688,7 +732,7 @@ notmuch_reply_command (void *ctx, int argc, char *argv[])\r
+       return 1;\r
+     }\r
\r
+-    if (reply_format_func (ctx, config, query, &params) != 0)\r
++    if (reply_format_func (ctx, config, query, &params, reply_all) != 0)\r
+       return 1;\r
\r
+     notmuch_query_destroy (query);\r
+-- \r
+1.7.5.4\r
+\r