From 392145423abe2b5dc8917571979675610e3e61f9 Mon Sep 17 00:00:00 2001 From: Adam Wolfe Gordon Date: Wed, 15 Feb 2012 22:00:25 +1700 Subject: [PATCH] [PATCH v5 2/4] reply: Add a JSON reply format. --- d5/d2886e8f66a809c2a5d5176a3bb28acc285d16 | 316 ++++++++++++++++++++++ 1 file changed, 316 insertions(+) create mode 100644 d5/d2886e8f66a809c2a5d5176a3bb28acc285d16 diff --git a/d5/d2886e8f66a809c2a5d5176a3bb28acc285d16 b/d5/d2886e8f66a809c2a5d5176a3bb28acc285d16 new file mode 100644 index 000000000..9573726da --- /dev/null +++ b/d5/d2886e8f66a809c2a5d5176a3bb28acc285d16 @@ -0,0 +1,316 @@ +Return-Path: +X-Original-To: notmuch@notmuchmail.org +Delivered-To: notmuch@notmuchmail.org +Received: from localhost (localhost [127.0.0.1]) + by olra.theworths.org (Postfix) with ESMTP id D6B3D431FBC + for ; Tue, 14 Feb 2012 21:00:55 -0800 (PST) +X-Virus-Scanned: Debian amavisd-new at olra.theworths.org +X-Spam-Flag: NO +X-Spam-Score: 0 +X-Spam-Level: +X-Spam-Status: No, score=0 tagged_above=-999 required=5 + tests=[RCVD_IN_DNSWL_NONE=-0.0001] autolearn=disabled +Received: from olra.theworths.org ([127.0.0.1]) + by localhost (olra.theworths.org [127.0.0.1]) (amavisd-new, port 10024) + with ESMTP id dE27qsGwuD1K for ; + Tue, 14 Feb 2012 21:00:51 -0800 (PST) +Received: from idcmail-mo2no.shaw.ca (idcmail-mo2no.shaw.ca [64.59.134.9]) + by olra.theworths.org (Postfix) with ESMTP id E41AF429E30 + for ; Tue, 14 Feb 2012 21:00:46 -0800 (PST) +Received: from lb7f8hsrpno-svcs.dcs.int.inet (HELO pd7ml3no-ssvc.prod.shaw.ca) + ([10.0.144.222]) + by pd7mo1no-svcs.prod.shaw.ca with ESMTP; 14 Feb 2012 22:00:46 -0700 +X-Cloudmark-SP-Filtered: true +X-Cloudmark-SP-Result: v=1.1 cv=aDUJ/pRHNXkohnfhaDKKve0FfU8uPxX8npdo6G126bI= + c=1 sm=1 + a=qo7gft7jEYsA:10 a=BLceEmwcHowA:10 a=yQp6g8lIsgqumF79BAsFDg==:17 + a=zbVpAq9wq4nIC2Wvmr0A:9 a=v8grYg2SqlAI95iRv-gA:7 + a=HpAAvcLHHh0Zw7uRqdWCyQ==:117 +Received: from unknown (HELO lagos.xvx.ca) ([96.52.216.56]) + by pd7ml3no-dmz.prod.shaw.ca with ESMTP; 14 Feb 2012 22:00:46 -0700 +Received: by lagos.xvx.ca (Postfix, from userid 1000) + id F3BCE8004EBA; Tue, 14 Feb 2012 22:00:45 -0700 (MST) +From: Adam Wolfe Gordon +To: notmuch@notmuchmail.org +Subject: [PATCH v5 2/4] reply: Add a JSON reply format. +Date: Tue, 14 Feb 2012 22:00:25 -0700 +Message-Id: <1329282027-29457-3-git-send-email-awg+notmuch@xvx.ca> +X-Mailer: git-send-email 1.7.5.4 +In-Reply-To: <1329282027-29457-1-git-send-email-awg+notmuch@xvx.ca> +References: <1329282027-29457-1-git-send-email-awg+notmuch@xvx.ca> +X-BeenThere: notmuch@notmuchmail.org +X-Mailman-Version: 2.1.13 +Precedence: list +List-Id: "Use and development of the notmuch mail system." + +List-Unsubscribe: , + +List-Archive: +List-Post: +List-Help: +List-Subscribe: , + +X-List-Received-Date: Wed, 15 Feb 2012 05:00:56 -0000 + +This new JSON format for replies includes headers generated for a +reply message as well as the headers of the original message. Using +this data, a client can intelligently create a reply. For example, the +emacs client will be able to create replies with quoted HTML parts by +parsing the HTML parts. + +Reply now enforces that only one message is returned, as the semantics +of replying to multiple messages are not well-defined. +--- + notmuch-client.h | 3 + + notmuch-reply.c | 164 ++++++++++++++++++++++++++++++++++++++++-------------- + notmuch-show.c | 2 +- + 3 files changed, 125 insertions(+), 44 deletions(-) + +diff --git a/notmuch-client.h b/notmuch-client.h +index 60828aa..d28ea07 100644 +--- a/notmuch-client.h ++++ b/notmuch-client.h +@@ -344,6 +344,9 @@ typedef struct mime_node { + int next_part_num; + } mime_node_t; + ++void ++format_part_json (const void *ctx, mime_node_t *node, notmuch_bool_t first); ++ + /* Construct a new MIME node pointing to the root message part of + * message. If cryptoctx is non-NULL, it will be used to verify + * signatures on any child parts. If decrypt is true, then cryptoctx +diff --git a/notmuch-reply.c b/notmuch-reply.c +index 6b244e6..979ad86 100644 +--- a/notmuch-reply.c ++++ b/notmuch-reply.c +@@ -505,6 +505,61 @@ guess_from_received_header (notmuch_config_t *config, notmuch_message_t *message + return NULL; + } + ++static GMimeMessage * ++create_reply_message(void *ctx, ++ notmuch_config_t *config, ++ notmuch_message_t *message, ++ notmuch_bool_t reply_all) ++{ ++ const char *subject, *from_addr = NULL; ++ const char *in_reply_to, *orig_references, *references; ++ ++ /* The 1 means we want headers in a "pretty" order. */ ++ GMimeMessage *reply = g_mime_message_new (1); ++ if (reply == NULL) { ++ fprintf (stderr, "Out of memory\n"); ++ return NULL; ++ } ++ ++ subject = notmuch_message_get_header (message, "subject"); ++ if (subject) { ++ if (strncasecmp (subject, "Re:", 3)) ++ subject = talloc_asprintf (ctx, "Re: %s", subject); ++ g_mime_message_set_subject (reply, subject); ++ } ++ ++ from_addr = add_recipients_from_message (reply, config, ++ message, reply_all); ++ ++ if (from_addr == NULL) ++ from_addr = guess_from_received_header (config, message); ++ ++ if (from_addr == NULL) ++ from_addr = notmuch_config_get_user_primary_email (config); ++ ++ from_addr = talloc_asprintf (ctx, "%s <%s>", ++ notmuch_config_get_user_name (config), ++ from_addr); ++ g_mime_object_set_header (GMIME_OBJECT (reply), ++ "From", from_addr); ++ ++ in_reply_to = talloc_asprintf (ctx, "<%s>", ++ notmuch_message_get_message_id (message)); ++ ++ g_mime_object_set_header (GMIME_OBJECT (reply), ++ "In-Reply-To", in_reply_to); ++ ++ orig_references = notmuch_message_get_header (message, "references"); ++ references = talloc_asprintf (ctx, "%s%s%s", ++ orig_references ? orig_references : "", ++ orig_references ? " " : "", ++ in_reply_to); ++ g_mime_object_set_header (GMIME_OBJECT (reply), ++ "References", references); ++ ++ return reply; ++} ++ + static int + notmuch_reply_format_default(void *ctx, + notmuch_config_t *config, +@@ -515,8 +570,6 @@ notmuch_reply_format_default(void *ctx, + GMimeMessage *reply; + notmuch_messages_t *messages; + notmuch_message_t *message; +- const char *subject, *from_addr = NULL; +- const char *in_reply_to, *orig_references, *references; + const notmuch_show_format_t *format = &format_reply; + + for (messages = notmuch_query_search_messages (query); +@@ -525,48 +578,10 @@ notmuch_reply_format_default(void *ctx, + { + message = notmuch_messages_get (messages); + +- /* The 1 means we want headers in a "pretty" order. */ +- reply = g_mime_message_new (1); +- if (reply == NULL) { +- fprintf (stderr, "Out of memory\n"); +- return 1; +- } +- +- subject = notmuch_message_get_header (message, "subject"); +- if (subject) { +- if (strncasecmp (subject, "Re:", 3)) +- subject = talloc_asprintf (ctx, "Re: %s", subject); +- g_mime_message_set_subject (reply, subject); +- } +- +- from_addr = add_recipients_from_message (reply, config, message, +- reply_all); +- +- if (from_addr == NULL) +- from_addr = guess_from_received_header (config, message); +- +- if (from_addr == NULL) +- from_addr = notmuch_config_get_user_primary_email (config); +- +- from_addr = talloc_asprintf (ctx, "%s <%s>", +- notmuch_config_get_user_name (config), +- from_addr); +- g_mime_object_set_header (GMIME_OBJECT (reply), +- "From", from_addr); +- +- in_reply_to = talloc_asprintf (ctx, "<%s>", +- notmuch_message_get_message_id (message)); +- +- g_mime_object_set_header (GMIME_OBJECT (reply), +- "In-Reply-To", in_reply_to); ++ reply = create_reply_message (ctx, config, message, reply_all); + +- orig_references = notmuch_message_get_header (message, "references"); +- references = talloc_asprintf (ctx, "%s%s%s", +- orig_references ? orig_references : "", +- orig_references ? " " : "", +- in_reply_to); +- g_mime_object_set_header (GMIME_OBJECT (reply), +- "References", references); ++ if (!reply) ++ continue; + + show_reply_headers (reply); + +@@ -584,6 +599,65 @@ notmuch_reply_format_default(void *ctx, + return 0; + } + ++static int ++notmuch_reply_format_json(void *ctx, ++ notmuch_config_t *config, ++ notmuch_query_t *query, ++ notmuch_show_params_t *params, ++ notmuch_bool_t reply_all) ++{ ++ GMimeMessage *reply; ++ notmuch_messages_t *messages; ++ notmuch_message_t *message; ++ mime_node_t *node; ++ ++ const char *reply_headers[] = {"from", "to", "subject", "in-reply-to", "references"}; ++ unsigned int hidx; ++ ++ if (notmuch_query_count_messages (query) != 1) { ++ fprintf (stderr, "Error: search term did not match precisely one message.\n"); ++ return 1; ++ } ++ ++ messages = notmuch_query_search_messages (query); ++ message = notmuch_messages_get (messages); ++ if (mime_node_open (ctx, message, params->cryptoctx, params->decrypt, ++ &node) != NOTMUCH_STATUS_SUCCESS) ++ return 1; ++ ++ reply = create_reply_message (ctx, config, message, reply_all); ++ if (!reply) ++ return 1; ++ ++ /* Start a reply object */ ++ printf ("{\"reply-headers\": {"); ++ ++ for (hidx = 0; hidx < ARRAY_SIZE (reply_headers); hidx++) { ++ if (hidx) ++ printf (", "); ++ ++ printf ("%s: %s", json_quote_str (ctx, reply_headers[hidx]), ++ json_quote_str (ctx, g_mime_object_get_header (GMIME_OBJECT (reply), reply_headers[hidx]))); ++ } ++ ++ g_object_unref (G_OBJECT (reply)); ++ reply = NULL; ++ ++ /* Done the headers for the reply, which has no body */ ++ printf ("}"); ++ ++ /* Start the original */ ++ printf (", \"original\": "); ++ ++ format_part_json (ctx, node, TRUE); ++ ++ /* End */ ++ printf ("}\n"); ++ notmuch_message_destroy (message); ++ ++ return 0; ++} ++ + /* This format is currently tuned for a git send-email --notmuch hook */ + static int + notmuch_reply_format_headers_only(void *ctx, +@@ -646,6 +720,7 @@ notmuch_reply_format_headers_only(void *ctx, + + enum { + FORMAT_DEFAULT, ++ FORMAT_JSON, + FORMAT_HEADERS_ONLY, + }; + +@@ -665,6 +740,7 @@ notmuch_reply_command (void *ctx, int argc, char *argv[]) + notmuch_opt_desc_t options[] = { + { NOTMUCH_OPT_KEYWORD, &format, "format", 'f', + (notmuch_keyword_t []){ { "default", FORMAT_DEFAULT }, ++ { "json", FORMAT_JSON }, + { "headers-only", FORMAT_HEADERS_ONLY }, + { 0, 0 } } }, + { NOTMUCH_OPT_KEYWORD, &reply_all, "reply-to", 'r', +@@ -683,6 +759,8 @@ notmuch_reply_command (void *ctx, int argc, char *argv[]) + + if (format == FORMAT_HEADERS_ONLY) + reply_format_func = notmuch_reply_format_headers_only; ++ else if (format == FORMAT_JSON) ++ reply_format_func = notmuch_reply_format_json; + else + reply_format_func = notmuch_reply_format_default; + +diff --git a/notmuch-show.c b/notmuch-show.c +index 6a171a4..c570a16 100644 +--- a/notmuch-show.c ++++ b/notmuch-show.c +@@ -652,7 +652,7 @@ format_part_text (const void *ctx, mime_node_t *node, + printf ("\f%s}\n", part_type); + } + +-static void ++void + format_part_json (const void *ctx, mime_node_t *node, notmuch_bool_t first) + { + /* Any changes to the JSON format should be reflected in the file +-- +1.7.5.4 + -- 2.26.2