[PATCH 1/7] cli/reply: push notmuch reply format abstraction lower in the stack
authorJani Nikula <jani@nikula.org>
Sat, 18 Jun 2016 21:31:27 +0000 (00:31 +0300)
committerW. Trevor King <wking@tremily.us>
Sat, 20 Aug 2016 23:22:06 +0000 (16:22 -0700)
3f/2bbfa0281e2bb99e7606866dd703963cffeeff [new file with mode: 0644]

diff --git a/3f/2bbfa0281e2bb99e7606866dd703963cffeeff b/3f/2bbfa0281e2bb99e7606866dd703963cffeeff
new file mode 100644 (file)
index 0000000..50275cd
--- /dev/null
@@ -0,0 +1,420 @@
+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 arlo.cworth.org (Postfix) with ESMTP id 91C8F6DE034D\r
+ for <notmuch@notmuchmail.org>; Sat, 18 Jun 2016 14:33:09 -0700 (PDT)\r
+X-Virus-Scanned: Debian amavisd-new at cworth.org\r
+X-Amavis-Alert: BAD HEADER SECTION, Duplicate header field: "References"\r
+X-Spam-Flag: NO\r
+X-Spam-Score: -0.563\r
+X-Spam-Level: \r
+X-Spam-Status: No, score=-0.563 tagged_above=-999 required=5 tests=[AWL=0.157,\r
+  DKIM_SIGNED=0.1, DKIM_VALID=-0.1, RCVD_IN_DNSWL_LOW=-0.7,\r
+ RCVD_IN_MSPIKE_H3=-0.01, RCVD_IN_MSPIKE_WL=-0.01] autolearn=disabled\r
+Received: from arlo.cworth.org ([127.0.0.1])\r
+ by localhost (arlo.cworth.org [127.0.0.1]) (amavisd-new, port 10024)\r
+ with ESMTP id B5KL1YfQwBXU for <notmuch@notmuchmail.org>;\r
+ Sat, 18 Jun 2016 14:33:01 -0700 (PDT)\r
+Received: from mail-wm0-f67.google.com (mail-wm0-f67.google.com\r
+ [74.125.82.67]) by arlo.cworth.org (Postfix) with ESMTPS id 005FA6DE00DF for\r
+ <notmuch@notmuchmail.org>; Sat, 18 Jun 2016 14:33:00 -0700 (PDT)\r
+Received: by mail-wm0-f67.google.com with SMTP id c82so2550855wme.3\r
+ for <notmuch@notmuchmail.org>; Sat, 18 Jun 2016 14:33:00 -0700 (PDT)\r
+DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;\r
+ d=nikula-org.20150623.gappssmtp.com; s=20150623;\r
+ h=from:to:cc:subject:date:message-id:in-reply-to:references\r
+ :in-reply-to:references;\r
+ bh=0gTT1Cs2x5flbIL5lOKo8lgQoRdqG9bi3NSycpTh6/I=;\r
+ b=j9HBsAfAVrT8C/54OyYk7Ku7rQccosUyqQd9q53bpKhWhCrbXEE80dPwvYvyGCQuhV\r
+ sc0puSwUt7UkS/11+3ASnnvyvscCMkpAP0VEw85bLoBUP8CHQRqoxRqV0uD68TnnJ3/P\r
+ GWoOJOJHF73sKq63YFP+bl5pYM//f698C4PyIjg+3oOPkwJ+CnkS1pNowL2SZTMk3OAh\r
+ Sd6CY/F99lpL+yY19WtopjzmgEJl5eASO2sYi3kcJnJ9tkwfYONCQYpDeGdkZEWgF18a\r
+ KDiUGspWhx2yAusbROWhwLcLteDNfiLWVILOzOHblX8MiRMuXo1mMhQQ+EDsqTKMy1SN\r
+ ikHA==\r
+X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;\r
+ d=1e100.net; s=20130820;\r
+ h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to\r
+ :references:in-reply-to:references;\r
+ bh=0gTT1Cs2x5flbIL5lOKo8lgQoRdqG9bi3NSycpTh6/I=;\r
+ b=aUdlSkvBWNApBYdzGMqzqVGqeYkt3KAtV8b5IZwi2c9W7tmVlG7lSy8r87gBbBk5OG\r
+ Qrp0A9TODdH8EDkqScdSIwioHmP9FF9W7wEluipnX9I8X3QL+Pbih+jncCKRFxJhVgrD\r
+ Vmguo/He/jHuPOU3POk7zDv92YAOqglipJ5OL0sGIdUDMxxsqC/J6X41cTZuiLLes/wk\r
+ AtQlzRww2pwLijGwngacJjJ27WjDWtv3p25PN+tJr9ETRpgIIjZY5W2wJqvVrJC/YDNH\r
+ qpc8G3BqDmPuHnwdelXmTKQ9AFAHCJcbN4rLq0nObQFG95P4Yb/lsU2L0++3mSXSxjbC\r
+ Xkwg==\r
+X-Gm-Message-State:\r
+ ALyK8tLhcSZxh4e9pcmNntkPJJ0vAWeYSDuwzBzLq1lRIcZFrx/amniscLYsnOBnOtKQ8w==\r
+X-Received: by 10.28.135.137 with SMTP id j131mr4251433wmd.47.1466285579405;\r
+ Sat, 18 Jun 2016 14:32:59 -0700 (PDT)\r
+Received: from localhost (mobile-access-bcee4e-81.dhcp.inet.fi.\r
+ [188.238.78.81])\r
+ by smtp.gmail.com with ESMTPSA id j4sm54730882wjg.20.2016.06.18.14.32.58\r
+ (version=TLSv1/SSLv3 cipher=OTHER);\r
+ Sat, 18 Jun 2016 14:32:58 -0700 (PDT)\r
+From: Jani Nikula <jani@nikula.org>\r
+To: notmuch@notmuchmail.org\r
+Cc: jani@nikula.org,\r
+       Daniel Kahn Gillmor <dkg@fifthhorseman.net>\r
+Subject: [PATCH 1/7] cli/reply: push notmuch reply format abstraction lower in\r
+ the stack\r
+Date: Sun, 19 Jun 2016 00:31:27 +0300\r
+Message-Id:\r
+ <c3a26221dc40ec3e685680435361909a95abcb0f.1466284726.git.jani@nikula.org>\r
+X-Mailer: git-send-email 2.1.4\r
+In-Reply-To: <cover.1466284726.git.jani@nikula.org>\r
+References: <cover.1466284726.git.jani@nikula.org>\r
+In-Reply-To: <cover.1466284726.git.jani@nikula.org>\r
+References: <cover.1466284726.git.jani@nikula.org>\r
+X-BeenThere: notmuch@notmuchmail.org\r
+X-Mailman-Version: 2.1.20\r
+Precedence: list\r
+List-Id: "Use and development of the notmuch mail system."\r
+ <notmuch.notmuchmail.org>\r
+List-Unsubscribe: <https://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: <https://notmuchmail.org/mailman/listinfo/notmuch>,\r
+ <mailto:notmuch-request@notmuchmail.org?subject=subscribe>\r
+X-List-Received-Date: Sat, 18 Jun 2016 21:33:09 -0000\r
+\r
+There's quite a bit of duplication, and some consequent deviation,\r
+between the various notmuch reply format code paths. Perform the query\r
+and message iteration in common code, and make the format specific\r
+functions operate on single messages.\r
+\r
+There should be no functional changes.\r
+---\r
+ notmuch-reply.c | 216 ++++++++++++++++++++++++++------------------------------\r
+ 1 file changed, 101 insertions(+), 115 deletions(-)\r
+\r
+diff --git a/notmuch-reply.c b/notmuch-reply.c\r
+index 49513732e620..847e306f94d2 100644\r
+--- a/notmuch-reply.c\r
++++ b/notmuch-reply.c\r
+@@ -598,82 +598,42 @@ create_reply_message(void *ctx,\r
+ static int\r
+ notmuch_reply_format_default(void *ctx,\r
+                            notmuch_config_t *config,\r
+-                           notmuch_query_t *query,\r
++                           notmuch_message_t *message,\r
+                            notmuch_show_params_t *params,\r
+                            notmuch_bool_t reply_all,\r
+                            unused (sprinter_t *sp))\r
+ {\r
+     GMimeMessage *reply;\r
+-    notmuch_messages_t *messages;\r
+-    notmuch_message_t *message;\r
+     mime_node_t *root;\r
+-    notmuch_status_t status;\r
\r
+-    status = notmuch_query_search_messages_st (query, &messages);\r
+-    if (print_status_query ("notmuch reply", query, status))\r
++    reply = create_reply_message (ctx, config, message, reply_all);\r
++    if (!reply)\r
+       return 1;\r
\r
+-    for (;\r
+-       notmuch_messages_valid (messages);\r
+-       notmuch_messages_move_to_next (messages))\r
+-    {\r
+-      message = notmuch_messages_get (messages);\r
++    show_reply_headers (reply);\r
\r
+-      reply = create_reply_message (ctx, config, message, reply_all);\r
+-\r
+-      /* If reply creation failed, we're out of memory, so don't\r
+-       * bother trying any more messages.\r
+-       */\r
+-      if (!reply) {\r
+-          notmuch_message_destroy (message);\r
+-          return 1;\r
+-      }\r
+-\r
+-      show_reply_headers (reply);\r
+-\r
+-      g_object_unref (G_OBJECT (reply));\r
+-      reply = NULL;\r
+-\r
+-      if (mime_node_open (ctx, message, &(params->crypto), &root) == NOTMUCH_STATUS_SUCCESS) {\r
+-          format_part_reply (root);\r
+-          talloc_free (root);\r
+-      }\r
++    g_object_unref (G_OBJECT (reply));\r
\r
+-      notmuch_message_destroy (message);\r
++    if (mime_node_open (ctx, message, &params->crypto, &root) == NOTMUCH_STATUS_SUCCESS) {\r
++      format_part_reply (root);\r
++      talloc_free (root);\r
+     }\r
++\r
+     return 0;\r
+ }\r
\r
+ static int\r
+ notmuch_reply_format_sprinter(void *ctx,\r
+                             notmuch_config_t *config,\r
+-                            notmuch_query_t *query,\r
++                            notmuch_message_t *message,\r
+                             notmuch_show_params_t *params,\r
+                             notmuch_bool_t reply_all,\r
+                             sprinter_t *sp)\r
+ {\r
+     GMimeMessage *reply;\r
+-    notmuch_messages_t *messages;\r
+-    notmuch_message_t *message;\r
+     mime_node_t *node;\r
+-    unsigned count;\r
+-    notmuch_status_t status;\r
+-\r
+-    status = notmuch_query_count_messages_st (query, &count);\r
+-    if (print_status_query ("notmuch reply", query, status))\r
+-      return 1;\r
+-\r
+-    if (count != 1) {\r
+-      fprintf (stderr, "Error: search term did not match precisely one message (matched %d messages).\n", count);\r
+-      return 1;\r
+-    }\r
\r
+-    status = notmuch_query_search_messages_st (query, &messages);\r
+-    if (print_status_query ("notmuch reply", query, status))\r
+-      return 1;\r
+-\r
+-    message = notmuch_messages_get (messages);\r
+-    if (mime_node_open (ctx, message, &(params->crypto), &node) != NOTMUCH_STATUS_SUCCESS)\r
++    if (mime_node_open (ctx, message, &params->crypto, &node) != NOTMUCH_STATUS_SUCCESS)\r
+       return 1;\r
\r
+     reply = create_reply_message (ctx, config, message, reply_all);\r
+@@ -686,7 +646,6 @@ notmuch_reply_format_sprinter(void *ctx,\r
+     sp->map_key (sp, "reply-headers");\r
+     format_headers_sprinter (sp, reply, TRUE);\r
+     g_object_unref (G_OBJECT (reply));\r
+-    reply = NULL;\r
\r
+     /* Start the original */\r
+     sp->map_key (sp, "original");\r
+@@ -694,7 +653,6 @@ notmuch_reply_format_sprinter(void *ctx,\r
\r
+     /* End */\r
+     sp->end (sp);\r
+-    notmuch_message_destroy (message);\r
\r
+     return 0;\r
+ }\r
+@@ -703,65 +661,48 @@ notmuch_reply_format_sprinter(void *ctx,\r
+ static int\r
+ notmuch_reply_format_headers_only(void *ctx,\r
+                                 notmuch_config_t *config,\r
+-                                notmuch_query_t *query,\r
++                                notmuch_message_t *message,\r
+                                 unused (notmuch_show_params_t *params),\r
+                                 notmuch_bool_t reply_all,\r
+                                 unused (sprinter_t *sp))\r
+ {\r
+     GMimeMessage *reply;\r
+-    notmuch_messages_t *messages;\r
+-    notmuch_message_t *message;\r
+     const char *in_reply_to, *orig_references, *references;\r
+     char *reply_headers;\r
+-    notmuch_status_t status;\r
\r
+-    status = notmuch_query_search_messages_st (query, &messages);\r
+-    if (print_status_query ("notmuch reply", query, status))\r
++    /* The 0 means we do not want headers in a "pretty" order. */\r
++    reply = g_mime_message_new (0);\r
++    if (reply == NULL) {\r
++      fprintf (stderr, "Out of memory\n");\r
+       return 1;\r
++    }\r
\r
+-    for (;\r
+-       notmuch_messages_valid (messages);\r
+-       notmuch_messages_move_to_next (messages))\r
+-    {\r
+-      message = notmuch_messages_get (messages);\r
+-\r
+-      /* The 0 means we do not want headers in a "pretty" order. */\r
+-      reply = g_mime_message_new (0);\r
+-      if (reply == NULL) {\r
+-          fprintf (stderr, "Out of memory\n");\r
+-          return 1;\r
+-      }\r
+-\r
+-      in_reply_to = talloc_asprintf (ctx, "<%s>",\r
+-                           notmuch_message_get_message_id (message));\r
+-\r
+-        g_mime_object_set_header (GMIME_OBJECT (reply),\r
+-                                "In-Reply-To", in_reply_to);\r
++    in_reply_to = talloc_asprintf (ctx, "<%s>",\r
++                                 notmuch_message_get_message_id (message));\r
\r
++    g_mime_object_set_header (GMIME_OBJECT (reply), "In-Reply-To", in_reply_to);\r
\r
+-      orig_references = notmuch_message_get_header (message, "references");\r
++    orig_references = notmuch_message_get_header (message, "references");\r
\r
+-      /* We print In-Reply-To followed by References because git format-patch treats them\r
+-         * specially.  Git does not interpret the other headers specially\r
+-       */\r
+-      references = talloc_asprintf (ctx, "%s%s%s",\r
+-                                    orig_references ? orig_references : "",\r
+-                                    orig_references ? " " : "",\r
+-                                    in_reply_to);\r
+-      g_mime_object_set_header (GMIME_OBJECT (reply),\r
+-                                "References", references);\r
++    /*\r
++     * We print In-Reply-To followed by References because git\r
++     * format-patch treats them specially. Git does not interpret the\r
++     * other headers specially.\r
++     */\r
++    references = talloc_asprintf (ctx, "%s%s%s",\r
++                                orig_references ? orig_references : "",\r
++                                orig_references ? " " : "",\r
++                                in_reply_to);\r
++    g_mime_object_set_header (GMIME_OBJECT (reply), "References", references);\r
\r
+-      (void)add_recipients_from_message (reply, config, message, reply_all);\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
+-      free (reply_headers);\r
++    reply_headers = g_mime_object_to_string (GMIME_OBJECT (reply));\r
++    printf ("%s", reply_headers);\r
++    free (reply_headers);\r
\r
+-      g_object_unref (G_OBJECT (reply));\r
+-      reply = NULL;\r
++    g_object_unref (G_OBJECT (reply));\r
\r
+-      notmuch_message_destroy (message);\r
+-    }\r
+     return 0;\r
+ }\r
\r
+@@ -772,6 +713,70 @@ enum {\r
+     FORMAT_HEADERS_ONLY,\r
+ };\r
\r
++static int do_reply(notmuch_config_t *config,\r
++                  notmuch_query_t *query,\r
++                  notmuch_show_params_t *params,\r
++                  int format,\r
++                  notmuch_bool_t reply_all)\r
++{\r
++    notmuch_messages_t *messages;\r
++    notmuch_message_t *message;\r
++    notmuch_status_t status;\r
++    struct sprinter *sp = NULL;\r
++    int ret = 0;\r
++    int (*reply_format_func) (void *ctx,\r
++                            notmuch_config_t *config,\r
++                            notmuch_message_t *message,\r
++                            notmuch_show_params_t *params,\r
++                            notmuch_bool_t reply_all,\r
++                            struct sprinter *sp);\r
++\r
++    if (format == FORMAT_JSON || format == FORMAT_SEXP) {\r
++      unsigned count;\r
++\r
++      status = notmuch_query_count_messages_st (query, &count);\r
++      if (print_status_query ("notmuch reply", query, status))\r
++          return 1;\r
++\r
++      if (count != 1) {\r
++          fprintf (stderr, "Error: search term did not match precisely one message (matched %d messages).\n", count);\r
++          return 1;\r
++      }\r
++    }\r
++\r
++    if (format == FORMAT_HEADERS_ONLY) {\r
++      reply_format_func = notmuch_reply_format_headers_only;\r
++    } else if (format == FORMAT_JSON) {\r
++      reply_format_func = notmuch_reply_format_sprinter;\r
++      sp = sprinter_json_create (config, stdout);\r
++    } else if (format == FORMAT_SEXP) {\r
++      reply_format_func = notmuch_reply_format_sprinter;\r
++      sp = sprinter_sexp_create (config, stdout);\r
++    } else {\r
++      reply_format_func = notmuch_reply_format_default;\r
++    }\r
++\r
++    status = notmuch_query_search_messages_st (query, &messages);\r
++    if (print_status_query ("notmuch reply", query, status))\r
++      return 1;\r
++\r
++    for (;\r
++       notmuch_messages_valid (messages);\r
++       notmuch_messages_move_to_next (messages))\r
++    {\r
++      message = notmuch_messages_get (messages);\r
++\r
++      ret = reply_format_func(config, config, message, params, reply_all, sp);\r
++\r
++      notmuch_message_destroy (message);\r
++\r
++      if (ret)\r
++          break;\r
++    }\r
++\r
++    return ret;\r
++}\r
++\r
+ int\r
+ notmuch_reply_command (notmuch_config_t *config, int argc, char *argv[])\r
+ {\r
+@@ -779,12 +784,6 @@ notmuch_reply_command (notmuch_config_t *config, int argc, char *argv[])\r
+     notmuch_query_t *query;\r
+     char *query_string;\r
+     int opt_index;\r
+-    int (*reply_format_func) (void *ctx,\r
+-                            notmuch_config_t *config,\r
+-                            notmuch_query_t *query,\r
+-                            notmuch_show_params_t *params,\r
+-                            notmuch_bool_t reply_all,\r
+-                            struct sprinter *sp);\r
+     notmuch_show_params_t params = {\r
+       .part = -1,\r
+       .crypto = {\r
+@@ -795,7 +794,6 @@ notmuch_reply_command (notmuch_config_t *config, int argc, char *argv[])\r
+     };\r
+     int format = FORMAT_DEFAULT;\r
+     int reply_all = TRUE;\r
+-    struct sprinter *sp = NULL;\r
\r
+     notmuch_opt_desc_t options[] = {\r
+       { NOTMUCH_OPT_KEYWORD, &format, "format", 'f',\r
+@@ -820,18 +818,6 @@ notmuch_reply_command (notmuch_config_t *config, int argc, char *argv[])\r
\r
+     notmuch_process_shared_options (argv[0]);\r
\r
+-    if (format == FORMAT_HEADERS_ONLY) {\r
+-      reply_format_func = notmuch_reply_format_headers_only;\r
+-    } else if (format == FORMAT_JSON) {\r
+-      reply_format_func = notmuch_reply_format_sprinter;\r
+-      sp = sprinter_json_create (config, stdout);\r
+-    } else if (format == FORMAT_SEXP) {\r
+-      reply_format_func = notmuch_reply_format_sprinter;\r
+-      sp = sprinter_sexp_create (config, stdout);\r
+-    } else {\r
+-      reply_format_func = notmuch_reply_format_default;\r
+-    }\r
+-\r
+     notmuch_exit_if_unsupported_format ();\r
\r
+     query_string = query_string_from_args (config, argc-opt_index, argv+opt_index);\r
+@@ -859,7 +845,7 @@ notmuch_reply_command (notmuch_config_t *config, int argc, char *argv[])\r
+       return EXIT_FAILURE;\r
+     }\r
\r
+-    if (reply_format_func (config, config, query, &params, reply_all, sp) != 0)\r
++    if (do_reply (config, query, &params, format, reply_all) != 0)\r
+       return EXIT_FAILURE;\r
\r
+     notmuch_crypto_cleanup (&params.crypto);\r
+-- \r
+2.1.4\r
+\r