Re: [PATCH v4 2/4] reply: Add a JSON reply format.
authorDmitry Kurochkin <dmitry.kurochkin@gmail.com>
Thu, 9 Feb 2012 07:22:32 +0000 (11:22 +0400)
committerW. Trevor King <wking@tremily.us>
Fri, 7 Nov 2014 17:44:19 +0000 (09:44 -0800)
f9/7a1ed41fbc763febc14594abc651ade7eb2a24 [new file with mode: 0644]

diff --git a/f9/7a1ed41fbc763febc14594abc651ade7eb2a24 b/f9/7a1ed41fbc763febc14594abc651ade7eb2a24
new file mode 100644 (file)
index 0000000..d164195
--- /dev/null
@@ -0,0 +1,375 @@
+Return-Path: <dmitry.kurochkin@gmail.com>\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 A5E87431FAF\r
+       for <notmuch@notmuchmail.org>; Wed,  8 Feb 2012 23:23:55 -0800 (PST)\r
+X-Virus-Scanned: Debian amavisd-new at olra.theworths.org\r
+X-Spam-Flag: NO\r
+X-Spam-Score: -0.799\r
+X-Spam-Level: \r
+X-Spam-Status: No, score=-0.799 tagged_above=-999 required=5\r
+       tests=[DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1,\r
+       FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_LOW=-0.7] 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 QlULvsRxGJwX for <notmuch@notmuchmail.org>;\r
+       Wed,  8 Feb 2012 23:23:54 -0800 (PST)\r
+Received: from mail-bk0-f53.google.com (mail-bk0-f53.google.com\r
+       [209.85.214.53]) (using TLSv1 with cipher RC4-SHA (128/128 bits))\r
+       (No client certificate requested)\r
+       by olra.theworths.org (Postfix) with ESMTPS id 9CBB8431FAE\r
+       for <notmuch@notmuchmail.org>; Wed,  8 Feb 2012 23:23:53 -0800 (PST)\r
+Received: by bkcjk7 with SMTP id jk7so1394268bkc.26\r
+       for <notmuch@notmuchmail.org>; Wed, 08 Feb 2012 23:23:51 -0800 (PST)\r
+DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma;\r
+       h=from:to:subject:in-reply-to:references:user-agent:date:message-id\r
+       :mime-version:content-type;\r
+       bh=kRH2IQGYR5iDiR0vFPu0KIM0NntkfjvZO64h9BpBCuY=;\r
+       b=VhwGzH7Tz8cSwfk8+G1orNHxTlgOXadm14vsr0ljeunvx85fsMmE/ZshsZuyreEbu2\r
+       h+16RSuCNKUHA2LeCHCYX89bj67ztD9b0VyXi7SxJR1SEsk3XX8p5Y4RVvZ6j9/g7yZ3\r
+       rIMWLqYgC/vFswR+IWupldRy282YPEGv+uVQM=\r
+Received: by 10.204.154.140 with SMTP id o12mr237212bkw.102.1328772231883;\r
+       Wed, 08 Feb 2012 23:23:51 -0800 (PST)\r
+Received: from localhost ([91.144.186.21])\r
+       by mx.google.com with ESMTPS id cz3sm4928647bkb.3.2012.02.08.23.23.50\r
+       (version=TLSv1/SSLv3 cipher=OTHER);\r
+       Wed, 08 Feb 2012 23:23:50 -0800 (PST)\r
+From: Dmitry Kurochkin <dmitry.kurochkin@gmail.com>\r
+To: Adam Wolfe Gordon <awg+notmuch@xvx.ca>, notmuch@notmuchmail.org\r
+Subject: Re: [PATCH v4 2/4] reply: Add a JSON reply format.\r
+In-Reply-To: <1328746916-25447-3-git-send-email-awg+notmuch@xvx.ca>\r
+References: <1326995217-27423-1-git-send-email-awg+notmuch@xvx.ca>\r
+       <1328746916-25447-1-git-send-email-awg+notmuch@xvx.ca>\r
+       <1328746916-25447-3-git-send-email-awg+notmuch@xvx.ca>\r
+User-Agent: Notmuch/0.11.1+167~g6e72434 (http://notmuchmail.org) Emacs/23.3.1\r
+       (x86_64-pc-linux-gnu)\r
+Date: Thu, 09 Feb 2012 11:22:32 +0400\r
+Message-ID: <87mx8sa2tj.fsf@gmail.com>\r
+MIME-Version: 1.0\r
+Content-Type: text/plain; charset=us-ascii\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, 09 Feb 2012 07:23:55 -0000\r
+\r
+Hi Adam.\r
+\r
+On Wed,  8 Feb 2012 17:21:54 -0700, Adam Wolfe Gordon <awg+notmuch@xvx.ca> wrote:\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 using w3m.\r
+> \r
+> Reply now enforces that only one message is returned, as the semantics\r
+> of replying to multiple messages are not wel-defined.\r
+> \r
+\r
+s/wel/well/\r
+\r
+> Show is modified such that --format=json no longer implies\r
+> --entire-thread, as MUAs will use --format=json when constructing\r
+> replies. The man page is updated to reflect this change.\r
+\r
+I did not look into details.  But I am surprised that user needs to call\r
+notmuch show --format=json to make reply.  I would expect notmuch reply\r
+to provide all required info (except for bodies).\r
+\r
+Anyway, I think you should put this change in a separate patch.\r
+\r
+Also, we clearly need a NEWS entry for it and user-customizable Emacs\r
+variable changes.  Though it can be done after this series is pushed, I\r
+guess.\r
+\r
+Thank you for this work, it is much appreciated.\r
+\r
+Regards,\r
+  Dmitry\r
+\r
+> ---\r
+>  emacs/notmuch-query.el  |    2 +-\r
+>  man/man1/notmuch-show.1 |    6 +--\r
+>  notmuch-reply.c         |  167 +++++++++++++++++++++++++++++++++++------------\r
+>  notmuch-show.c          |    1 -\r
+>  4 files changed, 126 insertions(+), 50 deletions(-)\r
+> \r
+> diff --git a/emacs/notmuch-query.el b/emacs/notmuch-query.el\r
+> index d66baea..cdf2d6b 100644\r
+> --- a/emacs/notmuch-query.el\r
+> +++ b/emacs/notmuch-query.el\r
+> @@ -29,7 +29,7 @@ A thread is a forest or list of trees. A tree is a two element\r
+>  list where the first element is a message, and the second element\r
+>  is a possibly empty forest of replies.\r
+>  "\r
+> -  (let  ((args '("show" "--format=json"))\r
+> +  (let  ((args '("show" "--format=json" "--entire-thread"))\r
+>       (json-object-type 'plist)\r
+>       (json-array-type 'list)\r
+>       (json-false 'nil))\r
+> diff --git a/man/man1/notmuch-show.1 b/man/man1/notmuch-show.1\r
+> index b2301d8..c86b9db 100644\r
+> --- a/man/man1/notmuch-show.1\r
+> +++ b/man/man1/notmuch-show.1\r
+> @@ -55,11 +55,7 @@ be nested.\r
+>  The output is formatted with Javascript Object Notation (JSON). This\r
+>  format is more robust than the text format for automated\r
+>  processing. The nested structure of multipart MIME messages is\r
+> -reflected in nested JSON output. JSON output always includes all\r
+> -messages in a matching thread; in effect\r
+> -.B \-\-format=json\r
+> -implies\r
+> -.B \-\-entire\-thread\r
+> +reflected in nested JSON output.\r
+>  \r
+>  .RE\r
+>  .RS 4\r
+> diff --git a/notmuch-reply.c b/notmuch-reply.c\r
+> index f55b1d2..bfbc307 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
+> +    reply = create_reply_message (ctx, config, message, reply_all);\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
+> +    if (!reply)\r
+> +        continue;\r
+>  \r
+>      show_reply_headers (reply);\r
+>  \r
+> @@ -584,6 +599,68 @@ 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
+> +                      unused (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
+> +\r
+> +    const char *reply_headers[] = {"from", "to", "subject", "in-reply-to", "references"};\r
+> +    const char *orig_headers[] = {"from", "to", "cc", "subject", "date", "in-reply-to", "references", "message-id"};\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
+> +\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\": { ");\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
+> +    for (hidx = 0; hidx < ARRAY_SIZE (orig_headers); hidx++) {\r
+> +    if (hidx)\r
+> +        printf (", ");\r
+> +\r
+> +    printf ("%s: %s", json_quote_str (ctx, orig_headers[hidx]),\r
+> +            json_quote_str (ctx, notmuch_message_get_header (message, orig_headers[hidx])));\r
+> +    }\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 +723,7 @@ notmuch_reply_format_headers_only(void *ctx,\r
+>  \r
+>  enum {\r
+>      FORMAT_DEFAULT,\r
+> +    FORMAT_JSON,\r
+>      FORMAT_HEADERS_ONLY,\r
+>  };\r
+>  \r
+> @@ -666,6 +744,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
+> @@ -684,6 +763,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 dec799c..344d08c 100644\r
+> --- a/notmuch-show.c\r
+> +++ b/notmuch-show.c\r
+> @@ -1082,7 +1082,6 @@ notmuch_show_command (void *ctx, unused (int argc), unused (char *argv[]))\r
+>              format = &format_text;\r
+>          } else if (strcmp (opt, "json") == 0) {\r
+>              format = &format_json;\r
+> -            params.entire_thread = 1;\r
+>          } else if (strcmp (opt, "mbox") == 0) {\r
+>              format = &format_mbox;\r
+>              mbox = 1;\r
+> -- \r
+> 1.7.5.4\r
+> \r
+> _______________________________________________\r
+> notmuch mailing list\r
+> notmuch@notmuchmail.org\r
+> http://notmuchmail.org/mailman/listinfo/notmuch\r