--- /dev/null
+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 D6B3D431FBC\r
+ for <notmuch@notmuchmail.org>; Tue, 14 Feb 2012 21:00:55 -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 dE27qsGwuD1K for <notmuch@notmuchmail.org>;\r
+ Tue, 14 Feb 2012 21:00:51 -0800 (PST)\r
+Received: from idcmail-mo2no.shaw.ca (idcmail-mo2no.shaw.ca [64.59.134.9])\r
+ by olra.theworths.org (Postfix) with ESMTP id E41AF429E30\r
+ for <notmuch@notmuchmail.org>; Tue, 14 Feb 2012 21:00:46 -0800 (PST)\r
+Received: from lb7f8hsrpno-svcs.dcs.int.inet (HELO pd7ml3no-ssvc.prod.shaw.ca)\r
+ ([10.0.144.222])\r
+ by pd7mo1no-svcs.prod.shaw.ca with ESMTP; 14 Feb 2012 22:00:46 -0700\r
+X-Cloudmark-SP-Filtered: true\r
+X-Cloudmark-SP-Result: v=1.1 cv=aDUJ/pRHNXkohnfhaDKKve0FfU8uPxX8npdo6G126bI=\r
+ c=1 sm=1\r
+ a=qo7gft7jEYsA:10 a=BLceEmwcHowA:10 a=yQp6g8lIsgqumF79BAsFDg==:17\r
+ a=zbVpAq9wq4nIC2Wvmr0A:9 a=v8grYg2SqlAI95iRv-gA:7\r
+ a=HpAAvcLHHh0Zw7uRqdWCyQ==:117\r
+Received: from unknown (HELO lagos.xvx.ca) ([96.52.216.56])\r
+ by pd7ml3no-dmz.prod.shaw.ca with ESMTP; 14 Feb 2012 22:00:46 -0700\r
+Received: by lagos.xvx.ca (Postfix, from userid 1000)\r
+ id F3BCE8004EBA; Tue, 14 Feb 2012 22:00:45 -0700 (MST)\r
+From: Adam Wolfe Gordon <awg+notmuch@xvx.ca>\r
+To: notmuch@notmuchmail.org\r
+Subject: [PATCH v5 2/4] reply: Add a JSON reply format.\r
+Date: Tue, 14 Feb 2012 22:00:25 -0700\r
+Message-Id: <1329282027-29457-3-git-send-email-awg+notmuch@xvx.ca>\r
+X-Mailer: git-send-email 1.7.5.4\r
+In-Reply-To: <1329282027-29457-1-git-send-email-awg+notmuch@xvx.ca>\r
+References: <1329282027-29457-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: Wed, 15 Feb 2012 05:00:56 -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 | 164 ++++++++++++++++++++++++++++++++++++++++--------------\r
+ notmuch-show.c | 2 +-\r
+ 3 files changed, 125 insertions(+), 44 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 6b244e6..979ad86 100644\r
+--- a/notmuch-reply.c\r
++++ b/notmuch-reply.c\r
+@@ -505,6 +505,61 @@ guess_from_received_header (notmuch_config_t *config, notmuch_message_t *message\r
+ return NULL;\r
+ }\r
+ \r
++static GMimeMessage *\r
++create_reply_message(void *ctx,\r
++ notmuch_config_t *config,\r
++ notmuch_message_t *message,\r
++ notmuch_bool_t reply_all)\r
++{\r
++ const char *subject, *from_addr = NULL;\r
++ const char *in_reply_to, *orig_references, *references;\r
++\r
++ /* The 1 means we want headers in a "pretty" order. */\r
++ GMimeMessage *reply = g_mime_message_new (1);\r
++ if (reply == NULL) {\r
++ fprintf (stderr, "Out of memory\n");\r
++ return NULL;\r
++ }\r
++\r
++ subject = notmuch_message_get_header (message, "subject");\r
++ if (subject) {\r
++ if (strncasecmp (subject, "Re:", 3))\r
++ subject = talloc_asprintf (ctx, "Re: %s", subject);\r
++ g_mime_message_set_subject (reply, subject);\r
++ }\r
++\r
++ from_addr = add_recipients_from_message (reply, config,\r
++ message, reply_all);\r
++\r
++ if (from_addr == NULL)\r
++ from_addr = guess_from_received_header (config, message);\r
++\r
++ if (from_addr == NULL)\r
++ from_addr = notmuch_config_get_user_primary_email (config);\r
++\r
++ from_addr = talloc_asprintf (ctx, "%s <%s>",\r
++ notmuch_config_get_user_name (config),\r
++ from_addr);\r
++ g_mime_object_set_header (GMIME_OBJECT (reply),\r
++ "From", from_addr);\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
++\r
++ orig_references = notmuch_message_get_header (message, "references");\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
++ return reply;\r
++}\r
++\r
+ static int\r
+ notmuch_reply_format_default(void *ctx,\r
+ notmuch_config_t *config,\r
+@@ -515,8 +570,6 @@ notmuch_reply_format_default(void *ctx,\r
+ GMimeMessage *reply;\r
+ notmuch_messages_t *messages;\r
+ notmuch_message_t *message;\r
+- const char *subject, *from_addr = NULL;\r
+- const char *in_reply_to, *orig_references, *references;\r
+ const notmuch_show_format_t *format = &format_reply;\r
+ \r
+ for (messages = notmuch_query_search_messages (query);\r
+@@ -525,48 +578,10 @@ notmuch_reply_format_default(void *ctx,\r
+ {\r
+ message = notmuch_messages_get (messages);\r
+ \r
+- /* The 1 means we want headers in a "pretty" order. */\r
+- reply = g_mime_message_new (1);\r
+- if (reply == NULL) {\r
+- fprintf (stderr, "Out of memory\n");\r
+- return 1;\r
+- }\r
+-\r
+- subject = notmuch_message_get_header (message, "subject");\r
+- if (subject) {\r
+- if (strncasecmp (subject, "Re:", 3))\r
+- subject = talloc_asprintf (ctx, "Re: %s", subject);\r
+- g_mime_message_set_subject (reply, subject);\r
+- }\r
+-\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
+-\r
+- if (from_addr == NULL)\r
+- from_addr = notmuch_config_get_user_primary_email (config);\r
+-\r
+- from_addr = talloc_asprintf (ctx, "%s <%s>",\r
+- notmuch_config_get_user_name (config),\r
+- from_addr);\r
+- g_mime_object_set_header (GMIME_OBJECT (reply),\r
+- "From", from_addr);\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
++ reply = create_reply_message (ctx, config, message, reply_all);\r
+ \r
+- orig_references = notmuch_message_get_header (message, "references");\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
++ if (!reply)\r
++ continue;\r
+ \r
+ show_reply_headers (reply);\r
+ \r
+@@ -584,6 +599,65 @@ 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
++ const char *reply_headers[] = {"from", "to", "subject", "in-reply-to", "references"};\r
++ unsigned int hidx;\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
++ /* Start a reply object */\r
++ printf ("{\"reply-headers\": {");\r
++\r
++ for (hidx = 0; hidx < ARRAY_SIZE (reply_headers); hidx++) {\r
++ if (hidx)\r
++ printf (", ");\r
++\r
++ printf ("%s: %s", json_quote_str (ctx, reply_headers[hidx]),\r
++ json_quote_str (ctx, g_mime_object_get_header (GMIME_OBJECT (reply), reply_headers[hidx])));\r
++ }\r
++\r
++ g_object_unref (G_OBJECT (reply));\r
++ reply = NULL;\r
++\r
++ /* Done the headers for the reply, which has no body */\r
++ printf ("}");\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
+@@ -646,6 +720,7 @@ notmuch_reply_format_headers_only(void *ctx,\r
+ \r
+ enum {\r
+ FORMAT_DEFAULT,\r
++ FORMAT_JSON,\r
+ FORMAT_HEADERS_ONLY,\r
+ };\r
+ \r
+@@ -665,6 +740,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
+@@ -683,6 +759,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