Re: [PATCH 2/4] reply: Add a JSON reply format.
authorJani Nikula <jani@nikula.org>
Sat, 14 Jan 2012 20:58:55 +0000 (22:58 +0200)
committerW. Trevor King <wking@tremily.us>
Fri, 7 Nov 2014 17:42:09 +0000 (09:42 -0800)
41/7d17054650b713c45e063d76a4b1a2db2834e1 [new file with mode: 0644]

diff --git a/41/7d17054650b713c45e063d76a4b1a2db2834e1 b/41/7d17054650b713c45e063d76a4b1a2db2834e1
new file mode 100644 (file)
index 0000000..7bd9186
--- /dev/null
@@ -0,0 +1,488 @@
+Return-Path: <jani@nikula.org>\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 31586429E25\r
+       for <notmuch@notmuchmail.org>; Sat, 14 Jan 2012 12:59:07 -0800 (PST)\r
+X-Virus-Scanned: Debian amavisd-new at olra.theworths.org\r
+X-Spam-Flag: NO\r
+X-Spam-Score: -0.7\r
+X-Spam-Level: \r
+X-Spam-Status: No, score=-0.7 tagged_above=-999 required=5\r
+       tests=[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 xDU3DuOtJtmd for <notmuch@notmuchmail.org>;\r
+       Sat, 14 Jan 2012 12:59:06 -0800 (PST)\r
+Received: from mail-ey0-f181.google.com (mail-ey0-f181.google.com\r
+       [209.85.215.181]) (using TLSv1 with cipher RC4-SHA (128/128 bits))\r
+       (No client certificate requested)\r
+       by olra.theworths.org (Postfix) with ESMTPS id 9DB6E431FB6\r
+       for <notmuch@notmuchmail.org>; Sat, 14 Jan 2012 12:59:05 -0800 (PST)\r
+Received: by eaah10 with SMTP id h10so828123eaa.26\r
+       for <notmuch@notmuchmail.org>; Sat, 14 Jan 2012 12:59:04 -0800 (PST)\r
+Received: by 10.213.9.71 with SMTP id k7mr1032488ebk.71.1326574739515;\r
+       Sat, 14 Jan 2012 12:58:59 -0800 (PST)\r
+Received: from localhost (dsl-hkibrasgw4-fe5cdc00-23.dhcp.inet.fi.\r
+       [80.220.92.23])\r
+       by mx.google.com with ESMTPS id a60sm48763810eeb.4.2012.01.14.12.58.56\r
+       (version=SSLv3 cipher=OTHER); Sat, 14 Jan 2012 12:58:58 -0800 (PST)\r
+From: Jani Nikula <jani@nikula.org>\r
+To: Adam Wolfe Gordon <awg+notmuch@xvx.ca>, notmuch@notmuchmail.org,\r
+ awg@xvx.ca\r
+Subject: Re: [PATCH 2/4] reply: Add a JSON reply format.\r
+In-Reply-To: <1326009162-19524-3-git-send-email-awg+notmuch@xvx.ca>\r
+References: <1326009162-19524-1-git-send-email-awg+notmuch@xvx.ca>\r
+       <1326009162-19524-3-git-send-email-awg+notmuch@xvx.ca>\r
+User-Agent: Notmuch/0.11+59~g83cfe07 (http://notmuchmail.org) Emacs/23.3.1\r
+       (i686-pc-linux-gnu)\r
+Date: Sat, 14 Jan 2012 22:58:55 +0200\r
+Message-ID: <87k44uatm8.fsf@nikula.org>\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: Sat, 14 Jan 2012 20:59:07 -0000\r
+\r
+On Sun,  8 Jan 2012 00:52:40 -0700, Adam Wolfe Gordon <awg+notmuch@xvx.ca> wrote:\r
+> From: Adam Wolfe Gordon <awg@xvx.ca>\r
+> \r
+> This new JSON format for replies includes headers generated for a reply\r
+> message as well as the headers and all text parts of the original message.\r
+> Using this data, a client can intelligently create a reply. For example,\r
+> the emacs client will be able to create replies with quoted HTML parts by\r
+> parsing the HTML parts using w3m.\r
+\r
+Hi Adam, this is a drive-by-review on some things I spotted, but can't\r
+say I would've thought the whole thing through. I'm pretty ignorant\r
+about MIME parts etc. Please find comments inline.\r
+\r
+The reply-to-sender set was just merged. You'll have conflicts both here\r
+and in emacs code, but they should be trivial to sort out.\r
+\r
+\r
+BR,\r
+Jani.\r
+\r
+\r
+> ---\r
+>  notmuch-reply.c |  269 +++++++++++++++++++++++++++++++++++++++++++++++--------\r
+>  1 files changed, 230 insertions(+), 39 deletions(-)\r
+> \r
+> diff --git a/notmuch-reply.c b/notmuch-reply.c\r
+> index f8d5f64..82df396 100644\r
+> --- a/notmuch-reply.c\r
+> +++ b/notmuch-reply.c\r
+> @@ -30,6 +30,15 @@ reply_headers_message_part (GMimeMessage *message);\r
+>  static void\r
+>  reply_part_content (GMimeObject *part);\r
+>  \r
+> +static void\r
+> +reply_part_start_json (GMimeObject *part, int *part_count);\r
+> +\r
+> +static void\r
+> +reply_part_content_json (GMimeObject *part);\r
+> +\r
+> +static void\r
+> +reply_part_end_json (GMimeObject *part);\r
+> +\r
+\r
+I know there are existing forward declarations like this, but would it\r
+be much trouble to arrange the code so that they were not needed at all?\r
+\r
+>  static const notmuch_show_format_t format_reply = {\r
+>      "",\r
+>      "", NULL,\r
+> @@ -46,6 +55,22 @@ static const notmuch_show_format_t format_reply = {\r
+>      ""\r
+>  };\r
+>  \r
+> +static const notmuch_show_format_t format_json = {\r
+> +    "",\r
+> +    "", NULL,\r
+> +        "", NULL, NULL, "",\r
+> +        "",\r
+> +            reply_part_start_json,\r
+> +            NULL,\r
+> +            NULL,\r
+> +            reply_part_content_json,\r
+> +            reply_part_end_json,\r
+> +            "",\r
+> +        "",\r
+> +    "", "",\r
+> +    ""\r
+> +};\r
+> +\r
+>  static void\r
+>  show_reply_headers (GMimeMessage *message)\r
+>  {\r
+> @@ -147,6 +172,78 @@ reply_part_content (GMimeObject *part)\r
+>      }\r
+>  }\r
+>  \r
+> +static void\r
+> +reply_part_start_json (GMimeObject *part, unused(int *part_count))\r
+> +{\r
+> +    GMimeContentType *content_type = g_mime_object_get_content_type (GMIME_OBJECT (part));\r
+> +    GMimeContentDisposition *disposition = g_mime_object_get_content_disposition (part);\r
+> +\r
+> +    if (g_mime_content_type_is_type (content_type, "text", "*") &&\r
+> +    (!disposition ||\r
+> +     strcmp (disposition->disposition, GMIME_DISPOSITION_INLINE) == 0))\r
+> +    {\r
+> +    printf("{ ");\r
+> +    }\r
+> +}\r
+> +\r
+> +static void\r
+> +reply_part_end_json (GMimeObject *part)\r
+> +{\r
+> +    GMimeContentType *content_type = g_mime_object_get_content_type (GMIME_OBJECT (part));\r
+> +    GMimeContentDisposition *disposition = g_mime_object_get_content_disposition (part);\r
+> +\r
+> +    if (g_mime_content_type_is_type (content_type, "text", "*") &&\r
+> +    (!disposition ||\r
+> +     strcmp (disposition->disposition, GMIME_DISPOSITION_INLINE) == 0))\r
+> +    printf ("}, ");\r
+> +}\r
+\r
+The two functions above, while small, are almost identical. Please move\r
+the common stuff to a common helper, and you can have something like\r
+this:\r
+\r
+       if (the_common_function (part))\r
+               printf ("}, ");\r
+\r
+> +\r
+> +static void\r
+> +reply_part_content_json (GMimeObject *part)\r
+> +{\r
+> +    GMimeContentType *content_type = g_mime_object_get_content_type (GMIME_OBJECT (part));\r
+> +    GMimeContentDisposition *disposition = g_mime_object_get_content_disposition (part);\r
+> +\r
+> +    void *ctx = talloc_new (NULL);\r
+> +\r
+> +    /* We only care about inline text parts for reply purposes */\r
+> +    if (g_mime_content_type_is_type (content_type, "text", "*") &&\r
+> +    (!disposition ||\r
+> +     strcmp (disposition->disposition, GMIME_DISPOSITION_INLINE) == 0))\r
+\r
+Oh, you can use the common helper here too.\r
+\r
+> +    {\r
+> +    GMimeStream *stream_memory = NULL, *stream_filter = NULL;\r
+\r
+No need to initialize stream_memory here.\r
+\r
+> +    GMimeDataWrapper *wrapper;\r
+> +    GByteArray *part_content;\r
+> +    const char *charset;\r
+> +\r
+> +    printf("\"content-type\": %s, \"content\": ",\r
+> +           json_quote_str(ctx, g_mime_content_type_to_string(content_type)));\r
+\r
+The style in notmuch is to have a space before the opening brace in\r
+function calls. Check elsewhere also. I always forget that too. :)\r
+\r
+> +\r
+> +    charset = g_mime_object_get_content_type_parameter (part, "charset");\r
+\r
+AFAICT charset declaration and the above call could be moved inside "if\r
+(stream_memory)" below.\r
+\r
+> +    stream_memory = g_mime_stream_mem_new ();\r
+> +    if (stream_memory) {\r
+> +        stream_filter = g_mime_stream_filter_new(stream_memory);\r
+> +        if (charset) {\r
+> +            g_mime_stream_filter_add(GMIME_STREAM_FILTER(stream_filter),\r
+> +                                     g_mime_filter_charset_new(charset, "UTF-8"));\r
+> +        }\r
+> +    }\r
+> +    wrapper = g_mime_part_get_content_object (GMIME_PART (part));\r
+> +    if (wrapper && stream_filter)\r
+> +        g_mime_data_wrapper_write_to_stream (wrapper, stream_filter);\r
+> +    part_content = g_mime_stream_mem_get_byte_array (GMIME_STREAM_MEM (stream_memory));\r
+\r
+You only need charset, stream_memory and stream_filter if wrapper is\r
+true. You could perhaps include all that stuff within "if (wrapper)".\r
+\r
+> +\r
+> +    printf("%s", json_quote_chararray(ctx, (char *) part_content->data, part_content->len));\r
+> +\r
+> +    if (stream_filter)\r
+> +        g_object_unref(stream_filter);\r
+> +    if (stream_memory)\r
+> +        g_object_unref(stream_memory);\r
+> +    }\r
+> +\r
+> +    talloc_free (ctx);\r
+> +}\r
+> +\r
+>  /* Is the given address configured as one of the user's "personal" or\r
+>   * "other" addresses. */\r
+>  static int\r
+> @@ -476,6 +573,59 @@ 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
+> +{\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, message);\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
+If you want to, you could make review easier by first having a patch\r
+that abstracts the above in the original code, with no other\r
+modifications. Then you could introduce new functionality that utilizes\r
+it later. But this is no big deal in such a small abstraction.\r
+\r
+> +\r
+>  static int\r
+>  notmuch_reply_format_default(void *ctx,\r
+>                           notmuch_config_t *config,\r
+> @@ -485,8 +635,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
+> @@ -495,61 +643,102 @@ 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
+> +    reply = create_reply_message(ctx, config, message);\r
+\r
+You should check the return value.\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
+> +    show_reply_headers (reply);\r
+>  \r
+> -    from_addr = add_recipients_from_message (reply, config, message);\r
+> +    g_object_unref (G_OBJECT (reply));\r
+> +    reply = NULL;\r
+>  \r
+> -    if (from_addr == NULL)\r
+> -        from_addr = guess_from_received_header (config, message);\r
+> +    printf ("On %s, %s wrote:\n",\r
+> +            notmuch_message_get_header (message, "date"),\r
+> +            notmuch_message_get_header (message, "from"));\r
+>  \r
+> -    if (from_addr == NULL)\r
+> -        from_addr = notmuch_config_get_user_primary_email (config);\r
+> +    show_message_body (message, format, params);\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
+> +    notmuch_message_destroy (message);\r
+> +    }\r
+> +    return 0;\r
+> +}\r
+>  \r
+> -    in_reply_to = talloc_asprintf (ctx, "<%s>",\r
+> -                         notmuch_message_get_message_id (message));\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
+> +{\r
+> +    GMimeMessage *reply;\r
+> +    notmuch_messages_t *messages;\r
+> +    notmuch_message_t *message;\r
+> +    const notmuch_show_format_t *format = &format_json;\r
+>  \r
+> -    g_mime_object_set_header (GMIME_OBJECT (reply),\r
+> -                              "In-Reply-To", in_reply_to);\r
+> +    const char *reply_headers[] = {"from", "to", "subject", "in-reply-to", "references"};\r
+> +    const int n_reply_headers = 5;\r
+\r
+Drop this, and use ARRAY_SIZE (reply_headers) instead.\r
+\r
+> +    const char *orig_headers[] = {"from", "to", "cc", "subject", "date", "in-reply-to", "references"};\r
+> +    const int n_orig_headers = 7;\r
+\r
+Ditto.\r
+\r
+> +    int hidx;\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
+> +    /* Start array of reply objects */\r
+> +    printf ("[");\r
+>  \r
+> -    show_reply_headers (reply);\r
+> +    for (messages = notmuch_query_search_messages (query);\r
+> +     notmuch_messages_valid (messages);\r
+> +     notmuch_messages_move_to_next (messages))\r
+> +    {\r
+> +    /* Start a reply object */\r
+> +    printf ("{ \"reply\": { \"headers\": { ");\r
+> +\r
+> +    message = notmuch_messages_get (messages);\r
+> +\r
+> +    reply = create_reply_message(ctx, config, message);\r
+\r
+Check return value.\r
+\r
+> +\r
+> +    for (hidx = 0; hidx < n_reply_headers; hidx += 1)\r
+\r
+hidx++ is the pattern in such for loops.\r
+\r
+> +    {\r
+> +        if (hidx > 0) {\r
+> +            printf (", ");\r
+> +        }\r
+\r
+You could just compare "if (hidx)", and also drop the curly braces.\r
+\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
+> -    printf ("On %s, %s wrote:\n",\r
+> -            notmuch_message_get_header (message, "date"),\r
+> -            notmuch_message_get_header (message, "from"));\r
+> +    /* Done the headers for the reply, which has no body parts */\r
+> +    printf ("} }");\r
+> +\r
+> +    /* Start the original */\r
+> +    printf (", \"original\": { \"headers\": { ");\r
+> +\r
+> +    for (hidx = 0; hidx < n_orig_headers; hidx += 1)\r
+\r
+hidx++\r
+\r
+> +    {\r
+> +        if (hidx > 0) {\r
+> +            printf (", ");\r
+> +        }\r
+\r
+Same as above.\r
+\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 headers */\r
+> +    printf (" }, \"body\": [ ");\r
+>  \r
+> +    /* Show body parts */\r
+>      show_message_body (message, format, params);\r
+>  \r
+>      notmuch_message_destroy (message);\r
+> +\r
+> +    /* Done the original */\r
+> +    printf ("{} ] }");\r
+> +\r
+> +    /* End the reply object. */\r
+> +    printf (" }, ");\r
+>      }\r
+> +\r
+> +    /* End array of reply objects */\r
+> +    printf ("{} ]\n");\r
+> +\r
+>      return 0;\r
+>  }\r
+>  \r
+> @@ -640,6 +829,8 @@ notmuch_reply_command (void *ctx, int argc, char *argv[])\r
+>              reply_format_func = notmuch_reply_format_default;\r
+>          } else if (strcmp (opt, "headers-only") == 0) {\r
+>              reply_format_func = notmuch_reply_format_headers_only;\r
+> +        } else if (strcmp (opt, "json") == 0) {\r
+> +            reply_format_func = notmuch_reply_format_json;\r
+>          } else {\r
+>              fprintf (stderr, "Invalid value for --format: %s\n", opt);\r
+>              return 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