[PATCH v7 04/10] reply: Add a JSON reply format.
authorAdam Wolfe Gordon <awg+notmuch@xvx.ca>
Mon, 12 Mar 2012 04:05:36 +0000 (22:05 +1800)
committerW. Trevor King <wking@tremily.us>
Fri, 7 Nov 2014 17:45:25 +0000 (09:45 -0800)
0d/9030501ab28ba2df95112870128209c92054c2 [new file with mode: 0644]

diff --git a/0d/9030501ab28ba2df95112870128209c92054c2 b/0d/9030501ab28ba2df95112870128209c92054c2
new file mode 100644 (file)
index 0000000..c734762
--- /dev/null
@@ -0,0 +1,282 @@
+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 A0B2D431FDB\r
+       for <notmuch@notmuchmail.org>; Sun, 11 Mar 2012 21:06:05 -0700 (PDT)\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 A+HpBoJxyXw1 for <notmuch@notmuchmail.org>;\r
+       Sun, 11 Mar 2012 21:06:02 -0700 (PDT)\r
+Received: from idcmail-mo2no.shaw.ca (idcmail-mo2no.shaw.ca [64.59.134.9])\r
+       by olra.theworths.org (Postfix) with ESMTP id 446DD431FC4\r
+       for <notmuch@notmuchmail.org>; Sun, 11 Mar 2012 21:06:01 -0700 (PDT)\r
+Received: from lb7f8hsrpno-svcs.dcs.int.inet (HELO pd5ml1no-ssvc.prod.shaw.ca)\r
+       ([10.0.144.222])\r
+       by pd6mo1no-svcs.prod.shaw.ca with ESMTP; 11 Mar 2012 22:06:00 -0600\r
+X-Cloudmark-SP-Filtered: true\r
+X-Cloudmark-SP-Result: v=1.1 cv=bD0CuDYpMgGTku+nVSbZuKP/9fNjspX1F8zuwcoBWhM=\r
+       c=1 sm=1\r
+       a=N6MTHTo5iFwA:10 a=BLceEmwcHowA:10 a=yQp6g8lIsgqumF79BAsFDg==:17\r
+       a=bz-zy3QpAAAA:8 a=n6QtOu-G53KnuZUlGJ0A:9 a=h7bP-iDue0dEkRzURMAA:7\r
+       a=NFWI-YhVVZsA:10 a=ahd4hhnNlpr_wjV6:21 a=RMfGCvDGy4T3Z2Ik:21\r
+       a=HpAAvcLHHh0Zw7uRqdWCyQ==:117\r
+Received: from unknown (HELO lagos.xvx.ca) ([96.52.216.56])\r
+       by pd5ml1no-dmz.prod.shaw.ca with ESMTP; 11 Mar 2012 22:06:00 -0600\r
+Received: by lagos.xvx.ca (Postfix, from userid 1000)\r
+       id A7FAD8004958; Sun, 11 Mar 2012 22:06:00 -0600 (MDT)\r
+From: Adam Wolfe Gordon <awg+notmuch@xvx.ca>\r
+To: notmuch@notmuchmail.org\r
+Subject: [PATCH v7 04/10] reply: Add a JSON reply format.\r
+Date: Sun, 11 Mar 2012 22:05:36 -0600\r
+Message-Id: <1331525142-30539-5-git-send-email-awg+notmuch@xvx.ca>\r
+X-Mailer: git-send-email 1.7.5.4\r
+In-Reply-To: <1331525142-30539-1-git-send-email-awg+notmuch@xvx.ca>\r
+References: <1331525142-30539-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: Mon, 12 Mar 2012 04:06:06 -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
+ notmuch-client.h |   14 ++++++++++----\r
+ notmuch-reply.c  |   49 +++++++++++++++++++++++++++++++++++++++++++++++++\r
+ notmuch-show.c   |   29 +++++++++++++++++++++--------\r
+ test/multipart   |    1 -\r
+ 4 files changed, 80 insertions(+), 13 deletions(-)\r
+\r
+diff --git a/notmuch-client.h b/notmuch-client.h\r
+index f4a62cc..270daff 100644\r
+--- a/notmuch-client.h\r
++++ b/notmuch-client.h\r
+@@ -62,13 +62,13 @@\r
+ #define STRINGIFY(s) STRINGIFY_(s)\r
+ #define STRINGIFY_(s) #s\r
\r
+-struct mime_node;\r
++typedef struct mime_node mime_node_t;\r
+ struct notmuch_show_params;\r
\r
+ typedef struct notmuch_show_format {\r
+     const char *message_set_start;\r
+     void (*part) (const void *ctx,\r
+-                struct mime_node *node, int indent,\r
++                mime_node_t *node, int indent,\r
+                 const struct notmuch_show_params *params);\r
+     const char *message_start;\r
+     void (*message) (const void *ctx,\r
+@@ -191,6 +191,12 @@ show_message_body (notmuch_message_t *message,\r
+ notmuch_status_t\r
+ show_one_part (const char *filename, int part);\r
\r
++void\r
++format_part_json (const void *ctx, mime_node_t *node, notmuch_bool_t first);\r
++\r
++void\r
++format_headers_json (const void *ctx, GMimeMessage *message, notmuch_bool_t reply);\r
++\r
+ char *\r
+ json_quote_chararray (const void *ctx, const char *str, const size_t len);\r
\r
+@@ -288,7 +294,7 @@ debugger_is_active (void);\r
+  * parts.  Message-type parts have one child, multipart-type parts\r
+  * have multiple children, and leaf parts have zero children.\r
+  */\r
+-typedef struct mime_node {\r
++struct mime_node {\r
+     /* The MIME object of this part.  This will be a GMimeMessage,\r
+      * GMimePart, GMimeMultipart, or a subclass of one of these.\r
+      *\r
+@@ -351,7 +357,7 @@ typedef struct mime_node {\r
+      * number to assign it (or -1 if unknown). */\r
+     int next_child;\r
+     int next_part_num;\r
+-} mime_node_t;\r
++};\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
+diff --git a/notmuch-reply.c b/notmuch-reply.c\r
+index f1478cc..e2b6c25 100644\r
+--- a/notmuch-reply.c\r
++++ b/notmuch-reply.c\r
+@@ -604,6 +604,51 @@ notmuch_reply_format_default(void *ctx,\r
+     return 0;\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_headers_json (ctx, reply, TRUE);\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
+ /* This format is currently tuned for a git send-email --notmuch hook */\r
+ static int\r
+ notmuch_reply_format_headers_only(void *ctx,\r
+@@ -666,6 +711,7 @@ notmuch_reply_format_headers_only(void *ctx,\r
\r
+ enum {\r
+     FORMAT_DEFAULT,\r
++    FORMAT_JSON,\r
+     FORMAT_HEADERS_ONLY,\r
+ };\r
\r
+@@ -685,6 +731,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
+@@ -703,6 +750,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 05d51b2..cb3a427 100644\r
+--- a/notmuch-show.c\r
++++ b/notmuch-show.c\r
+@@ -291,8 +291,8 @@ format_headers_message_part_text (GMimeMessage *message)\r
+     printf ("Date: %s\n", g_mime_message_get_date_as_string (message));\r
+ }\r
\r
+-static void\r
+-format_headers_json (const void *ctx, GMimeMessage *message)\r
++void\r
++format_headers_json (const void *ctx, GMimeMessage *message, notmuch_bool_t reply)\r
+ {\r
+     void *local = talloc_new (ctx);\r
+     InternetAddressList *recipients;\r
+@@ -316,9 +316,22 @@ format_headers_json (const void *ctx, GMimeMessage *message)\r
+       printf (", %s: %s",\r
+               json_quote_str (local, "Cc"),\r
+               json_quote_str (local, recipients_string));\r
+-    printf (", %s: %s}",\r
+-          json_quote_str (local, "Date"),\r
+-          json_quote_str (local, g_mime_message_get_date_as_string (message)));\r
++\r
++    if (reply) {\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
++    } else {\r
++      printf (", %s: %s",\r
++              json_quote_str (local, "Date"),\r
++              json_quote_str (local, g_mime_message_get_date_as_string (message)));\r
++    }\r
++\r
++    printf ("}");\r
\r
+     talloc_free (local);\r
+ }\r
+@@ -654,7 +667,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
+@@ -665,7 +678,7 @@ format_part_json (const void *ctx, mime_node_t *node, notmuch_bool_t first)\r
+       format_message_json (ctx, node->envelope_file);\r
\r
+       printf ("\"headers\": ");\r
+-      format_headers_json (ctx, GMIME_MESSAGE (node->part));\r
++      format_headers_json (ctx, GMIME_MESSAGE (node->part), FALSE);\r
\r
+       printf (", \"body\": [");\r
+       format_part_json (ctx, mime_node_child (node, 0), first);\r
+@@ -739,7 +752,7 @@ format_part_json (const void *ctx, mime_node_t *node, notmuch_bool_t first)\r
+     } else if (GMIME_IS_MESSAGE (node->part)) {\r
+       printf (", \"content\": [{");\r
+       printf ("\"headers\": ");\r
+-      format_headers_json (local, GMIME_MESSAGE (node->part));\r
++      format_headers_json (local, GMIME_MESSAGE (node->part), FALSE);\r
\r
+       printf (", \"body\": [");\r
+       terminator = "]}]";\r
+diff --git a/test/multipart b/test/multipart\r
+index 80d6e88..2383b9c 100755\r
+--- a/test/multipart\r
++++ b/test/multipart\r
+@@ -590,7 +590,6 @@ EOF\r
+ test_expect_equal_file OUTPUT EXPECTED\r
\r
+ test_begin_subtest "'notmuch reply' to a multipart message with json format"\r
+-test_subtest_known_broken\r
+ notmuch reply --format=json 'id:87liy5ap00.fsf@yoom.home.cworth.org' | notmuch_json_show_sanitize >OUTPUT\r
+ cat <<EOF >EXPECTED\r
+ {"reply-headers": {"Subject": "Re: Multipart message",\r
+-- \r
+1.7.5.4\r
+\r