[PATCH v5.2 3/7] reply: Add a JSON reply format.
authorAdam Wolfe Gordon <awg+notmuch@xvx.ca>
Thu, 16 Feb 2012 03:12:33 +0000 (20:12 +1700)
committerW. Trevor King <wking@tremily.us>
Fri, 7 Nov 2014 17:44:35 +0000 (09:44 -0800)
29/4d926729207b794a0ff58149e34d5103e137fc [new file with mode: 0644]

diff --git a/29/4d926729207b794a0ff58149e34d5103e137fc b/29/4d926729207b794a0ff58149e34d5103e137fc
new file mode 100644 (file)
index 0000000..2ff5403
--- /dev/null
@@ -0,0 +1,258 @@
+Return-Path: <awg@lagos.xvx.ca>\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 02406421195\r
+       for <notmuch@notmuchmail.org>; Wed, 15 Feb 2012 19:12:53 -0800 (PST)\r
+X-Virus-Scanned: Debian amavisd-new at olra.theworths.org\r
+X-Spam-Flag: NO\r
+X-Spam-Score: 0\r
+X-Spam-Level: \r
+X-Spam-Status: No, score=0 tagged_above=-999 required=5\r
+       tests=[RCVD_IN_DNSWL_NONE=-0.0001] 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 kWV6SMnJZa4Y for <notmuch@notmuchmail.org>;\r
+       Wed, 15 Feb 2012 19:12:51 -0800 (PST)\r
+Received: from idcmail-mo1so.shaw.ca (idcmail-mo1so.shaw.ca [24.71.223.10])\r
+       by olra.theworths.org (Postfix) with ESMTP id 54CC4429E43\r
+       for <notmuch@notmuchmail.org>; Wed, 15 Feb 2012 19:12:47 -0800 (PST)\r
+Received: from pd4ml1so-ssvc.prod.shaw.ca ([10.0.141.141])\r
+       by pd3mo1so-svcs.prod.shaw.ca with ESMTP; 15 Feb 2012 20:12:46 -0700\r
+X-Cloudmark-SP-Filtered: true\r
+X-Cloudmark-SP-Result: v=1.1 cv=zu4PKjlF+9DoPrG/TPTQ4CC8dREy1gI1uGhqrRhVwqE=\r
+       c=1 sm=1\r
+       a=9U844qFRHGkA:10 a=BLceEmwcHowA:10 a=yQp6g8lIsgqumF79BAsFDg==:17\r
+       a=zj4l01KfgLfMms7IjnsA:9 a=Dx_uIxvhEoZ4WgtTZTMA:7 a=cQ6rN1uShYHculXO:21\r
+       a=epQYAQWoZu4M9vRf:21 a=HpAAvcLHHh0Zw7uRqdWCyQ==:117\r
+Received: from unknown (HELO lagos.xvx.ca) ([96.52.216.56])\r
+       by pd4ml1so-dmz.prod.shaw.ca with ESMTP; 15 Feb 2012 20:12:46 -0700\r
+Received: by lagos.xvx.ca (Postfix, from userid 1000)\r
+       id D147B8004C3A; Wed, 15 Feb 2012 20:12:45 -0700 (MST)\r
+From: Adam Wolfe Gordon <awg+notmuch@xvx.ca>\r
+To: notmuch@notmuchmail.org\r
+Subject: [PATCH v5.2 3/7] reply: Add a JSON reply format.\r
+Date: Wed, 15 Feb 2012 20:12:33 -0700\r
+Message-Id: <1329361957-28493-4-git-send-email-awg+notmuch@xvx.ca>\r
+X-Mailer: git-send-email 1.7.5.4\r
+In-Reply-To: <1329361957-28493-1-git-send-email-awg+notmuch@xvx.ca>\r
+References: <1329361957-28493-1-git-send-email-awg+notmuch@xvx.ca>\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: Thu, 16 Feb 2012 03:12:53 -0000\r
+\r
+This new JSON format for replies includes headers generated for a\r
+reply message as well as the headers of the original message.  Using\r
+this data, a client can intelligently create a reply. For example, the\r
+emacs client will be able to create replies with quoted HTML parts by\r
+parsing the HTML parts.\r
+\r
+Reply now enforces that only one message is returned, as the semantics\r
+of replying to multiple messages are not well-defined.\r
+---\r
+ notmuch-client.h |    3 +\r
+ notmuch-reply.c  |  121 ++++++++++++++++++++++++++++++++++++++++++++++-------\r
+ notmuch-show.c   |    2 +-\r
+ 3 files changed, 109 insertions(+), 17 deletions(-)\r
+\r
+diff --git a/notmuch-client.h b/notmuch-client.h\r
+index 60828aa..d28ea07 100644\r
+--- a/notmuch-client.h\r
++++ b/notmuch-client.h\r
+@@ -344,6 +344,9 @@ typedef struct mime_node {\r
+     int next_part_num;\r
+ } mime_node_t;\r
+ \r
++void\r
++format_part_json (const void *ctx, mime_node_t *node, notmuch_bool_t first);\r
++\r
+ /* Construct a new MIME node pointing to the root message part of\r
+  * message.  If cryptoctx is non-NULL, it will be used to verify\r
+  * signatures on any child parts.  If decrypt is true, then cryptoctx\r
+diff --git a/notmuch-reply.c b/notmuch-reply.c\r
+index 8e56245..6670288 100644\r
+--- a/notmuch-reply.c\r
++++ b/notmuch-reply.c\r
+@@ -572,30 +572,115 @@ notmuch_reply_format_default(void *ctx,\r
+     notmuch_message_t *message;\r
+     const notmuch_show_format_t *format = &format_reply;\r
+ \r
+-    for (messages = notmuch_query_search_messages (query);\r
+-       notmuch_messages_valid (messages);\r
+-       notmuch_messages_move_to_next (messages))\r
+-    {\r
+-      message = notmuch_messages_get (messages);\r
++    if (notmuch_query_count_messages (query) != 1) {\r
++      fprintf (stderr, "Error: search term did not match precisely one message.\n");\r
++      return 1;\r
++    }\r
+ \r
+-      reply = create_reply_message (ctx, config, message, reply_all);\r
++    messages = notmuch_query_search_messages (query);\r
++    message = notmuch_messages_get (messages);\r
+ \r
+-      if (!reply)\r
+-          continue;\r
++    reply = create_reply_message (ctx, config, message, reply_all);\r
+ \r
+-      show_reply_headers (reply);\r
++    if (!reply)\r
++      return 1;\r
+ \r
+-      g_object_unref (G_OBJECT (reply));\r
+-      reply = NULL;\r
++    show_reply_headers (reply);\r
+ \r
+-      printf ("On %s, %s wrote:\n",\r
+-              notmuch_message_get_header (message, "date"),\r
+-              notmuch_message_get_header (message, "from"));\r
++    g_object_unref (G_OBJECT (reply));\r
++    reply = NULL;\r
+ \r
+-      show_message_body (message, format, params);\r
++    printf ("On %s, %s wrote:\n",\r
++          notmuch_message_get_header (message, "date"),\r
++          notmuch_message_get_header (message, "from"));\r
+ \r
+-      notmuch_message_destroy (message);\r
++    show_message_body (message, format, params);\r
++\r
++    notmuch_message_destroy (message);\r
++\r
++    return 0;\r
++}\r
++\r
++static void\r
++format_reply_headers_json (const void *ctx, GMimeMessage *message)\r
++{\r
++    void *local = talloc_new (ctx);\r
++    InternetAddressList *recipients;\r
++    const char *recipients_string;\r
++\r
++    printf ("{%s: %s",\r
++          json_quote_str (local, "Subject"),\r
++          json_quote_str (local, g_mime_message_get_subject (message)));\r
++    printf (", %s: %s",\r
++          json_quote_str (local, "From"),\r
++          json_quote_str (local, g_mime_message_get_sender (message)));\r
++    recipients = g_mime_message_get_recipients (message, GMIME_RECIPIENT_TYPE_TO);\r
++    recipients_string = internet_address_list_to_string (recipients, 0);\r
++    if (recipients_string)\r
++      printf (", %s: %s",\r
++              json_quote_str (local, "To"),\r
++              json_quote_str (local, recipients_string));\r
++    recipients = g_mime_message_get_recipients (message, GMIME_RECIPIENT_TYPE_CC);\r
++    recipients_string = internet_address_list_to_string (recipients, 0);\r
++    if (recipients_string)\r
++      printf (", %s: %s",\r
++              json_quote_str (local, "Cc"),\r
++              json_quote_str (local, recipients_string));\r
++\r
++    printf (", %s: %s",\r
++          json_quote_str (local, "In-reply-to"),\r
++          json_quote_str (local, g_mime_object_get_header (GMIME_OBJECT (message), "In-reply-to")));\r
++\r
++    printf (", %s: %s",\r
++          json_quote_str (local, "References"),\r
++          json_quote_str (local, g_mime_object_get_header (GMIME_OBJECT (message), "References")));\r
++\r
++    talloc_free (local);\r
++}\r
++\r
++static int\r
++notmuch_reply_format_json(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
++{\r
++    GMimeMessage *reply;\r
++    notmuch_messages_t *messages;\r
++    notmuch_message_t *message;\r
++    mime_node_t *node;\r
++\r
++    if (notmuch_query_count_messages (query) != 1) {\r
++      fprintf (stderr, "Error: search term did not match precisely one message.\n");\r
++      return 1;\r
+     }\r
++\r
++    messages = notmuch_query_search_messages (query);\r
++    message = notmuch_messages_get (messages);\r
++    if (mime_node_open (ctx, message, params->cryptoctx, params->decrypt,\r
++                      &node) != NOTMUCH_STATUS_SUCCESS)\r
++      return 1;\r
++\r
++    reply = create_reply_message (ctx, config, message, reply_all);\r
++    if (!reply)\r
++      return 1;\r
++\r
++    /* The headers of the reply message we've created */\r
++    printf ("{\"reply-headers\": ");\r
++    format_reply_headers_json (ctx, reply);\r
++    printf ("}");\r
++    g_object_unref (G_OBJECT (reply));\r
++    reply = NULL;\r
++\r
++    /* Start the original */\r
++    printf (", \"original\": ");\r
++\r
++    format_part_json (ctx, node, TRUE);\r
++\r
++    /* End */\r
++    printf ("}\n");\r
++    notmuch_message_destroy (message);\r
++\r
+     return 0;\r
+ }\r
+ \r
+@@ -661,6 +746,7 @@ notmuch_reply_format_headers_only(void *ctx,\r
+ \r
+ enum {\r
+     FORMAT_DEFAULT,\r
++    FORMAT_JSON,\r
+     FORMAT_HEADERS_ONLY,\r
+ };\r
+ \r
+@@ -680,6 +766,7 @@ notmuch_reply_command (void *ctx, int argc, char *argv[])\r
+     notmuch_opt_desc_t options[] = {\r
+       { NOTMUCH_OPT_KEYWORD, &format, "format", 'f',\r
+         (notmuch_keyword_t []){ { "default", FORMAT_DEFAULT },\r
++                                { "json", FORMAT_JSON },\r
+                                 { "headers-only", FORMAT_HEADERS_ONLY },\r
+                                 { 0, 0 } } },\r
+       { NOTMUCH_OPT_KEYWORD, &reply_all, "reply-to", 'r',\r
+@@ -698,6 +785,8 @@ notmuch_reply_command (void *ctx, int argc, char *argv[])\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_json;\r
+     else\r
+       reply_format_func = notmuch_reply_format_default;\r
+ \r
+diff --git a/notmuch-show.c b/notmuch-show.c\r
+index 6a171a4..c570a16 100644\r
+--- a/notmuch-show.c\r
++++ b/notmuch-show.c\r
+@@ -652,7 +652,7 @@ format_part_text (const void *ctx, mime_node_t *node,\r
+     printf ("\f%s}\n", part_type);\r
+ }\r
+ \r
+-static void\r
++void\r
+ format_part_json (const void *ctx, mime_node_t *node, notmuch_bool_t first)\r
+ {\r
+     /* Any changes to the JSON format should be reflected in the file\r
+-- \r
+1.7.5.4\r
+\r