]> git.notmuchmail.org Git - notmuch/blobdiff - notmuch-show.c
show: Convert format_headers_json to use sprinter
[notmuch] / notmuch-show.c
index 8247f1d52c6c34d3bb1a3370c2531d26f88170f3..9852119f77338f3a8a2190b1ed838449f25d3f28 100644 (file)
 
 #include "notmuch-client.h"
 #include "gmime-filter-reply.h"
+#include "sprinter.h"
 
 static notmuch_status_t
-format_part_text (const void *ctx, mime_node_t *node,
+format_part_text (const void *ctx, sprinter_t *sp, mime_node_t *node,
                  int indent, const notmuch_show_params_t *params);
 
 static const notmuch_show_format_t format_text = {
+    .new_sprinter = sprinter_text_create,
     .part = format_part_text,
 };
 
 static notmuch_status_t
-format_part_json_entry (const void *ctx, mime_node_t *node,
+format_part_json_entry (const void *ctx, sprinter_t *sp, mime_node_t *node,
                        int indent, const notmuch_show_params_t *params);
 
 static const notmuch_show_format_t format_json = {
+    .new_sprinter = sprinter_json_create,
     .message_set_start = "[",
     .part = format_part_json_entry,
     .message_set_sep = ", ",
-    .message_set_end = "]"
+    .message_set_end = "]",
+    .null_message = "null"
 };
 
 static notmuch_status_t
-format_part_mbox (const void *ctx, mime_node_t *node,
+format_part_mbox (const void *ctx, sprinter_t *sp, mime_node_t *node,
                  int indent, const notmuch_show_params_t *params);
 
 static const notmuch_show_format_t format_mbox = {
+    .new_sprinter = sprinter_text_create,
     .part = format_part_mbox,
 };
 
 static notmuch_status_t
-format_part_raw (unused (const void *ctx), mime_node_t *node,
+format_part_raw (unused (const void *ctx), sprinter_t *sp, mime_node_t *node,
                 unused (int indent),
                 unused (const notmuch_show_params_t *params));
 
 static const notmuch_show_format_t format_raw = {
+    .new_sprinter = sprinter_text_create,
     .part = format_part_raw,
 };
 
@@ -193,48 +199,46 @@ _is_from_line (const char *line)
 }
 
 void
-format_headers_json (const void *ctx, GMimeMessage *message, notmuch_bool_t reply)
+format_headers_json (sprinter_t *sp, GMimeMessage *message,
+                    notmuch_bool_t reply)
 {
-    void *local = talloc_new (ctx);
     InternetAddressList *recipients;
     const char *recipients_string;
 
-    printf ("{%s: %s",
-           json_quote_str (local, "Subject"),
-           json_quote_str (local, g_mime_message_get_subject (message)));
-    printf (", %s: %s",
-           json_quote_str (local, "From"),
-           json_quote_str (local, g_mime_message_get_sender (message)));
+    sp->begin_map (sp);
+
+    sp->map_key (sp, "Subject");
+    sp->string (sp, g_mime_message_get_subject (message));
+
+    sp->map_key (sp, "From");
+    sp->string (sp, g_mime_message_get_sender (message));
+
     recipients = g_mime_message_get_recipients (message, GMIME_RECIPIENT_TYPE_TO);
     recipients_string = internet_address_list_to_string (recipients, 0);
-    if (recipients_string)
-       printf (", %s: %s",
-               json_quote_str (local, "To"),
-               json_quote_str (local, recipients_string));
+    if (recipients_string) {
+       sp->map_key (sp, "To");
+       sp->string (sp, recipients_string);
+    }
+
     recipients = g_mime_message_get_recipients (message, GMIME_RECIPIENT_TYPE_CC);
     recipients_string = internet_address_list_to_string (recipients, 0);
-    if (recipients_string)
-       printf (", %s: %s",
-               json_quote_str (local, "Cc"),
-               json_quote_str (local, recipients_string));
+    if (recipients_string) {
+       sp->map_key (sp, "Cc");
+       sp->string (sp, recipients_string);
+    }
 
     if (reply) {
-       printf (", %s: %s",
-               json_quote_str (local, "In-reply-to"),
-               json_quote_str (local, g_mime_object_get_header (GMIME_OBJECT (message), "In-reply-to")));
+       sp->map_key (sp, "In-reply-to");
+       sp->string (sp, g_mime_object_get_header (GMIME_OBJECT (message), "In-reply-to"));
 
-       printf (", %s: %s",
-               json_quote_str (local, "References"),
-               json_quote_str (local, g_mime_object_get_header (GMIME_OBJECT (message), "References")));
+       sp->map_key (sp, "References");
+       sp->string (sp, g_mime_object_get_header (GMIME_OBJECT (message), "References"));
     } else {
-       printf (", %s: %s",
-               json_quote_str (local, "Date"),
-               json_quote_str (local, g_mime_message_get_date_as_string (message)));
+       sp->map_key (sp, "Date");
+       sp->string (sp, g_mime_message_get_date_as_string (message));
     }
 
-    printf ("}");
-
-    talloc_free (local);
+    sp->end (sp);
 }
 
 /* Write a MIME text part out to the given stream.
@@ -465,7 +469,7 @@ format_part_sigstatus_json (mime_node_t *node)
 #endif
 
 static notmuch_status_t
-format_part_text (const void *ctx, mime_node_t *node,
+format_part_text (const void *ctx, sprinter_t *sp, mime_node_t *node,
                  int indent, const notmuch_show_params_t *params)
 {
     /* The disposition and content-type metadata are associated with
@@ -547,7 +551,7 @@ format_part_text (const void *ctx, mime_node_t *node,
     }
 
     for (i = 0; i < node->nchildren; i++)
-       format_part_text (ctx, mime_node_child (node, i), indent, params);
+       format_part_text (ctx, sp, mime_node_child (node, i), indent, params);
 
     if (GMIME_IS_MESSAGE (node->part))
        printf ("\fbody}\n");
@@ -558,7 +562,8 @@ format_part_text (const void *ctx, mime_node_t *node,
 }
 
 void
-format_part_json (const void *ctx, mime_node_t *node, notmuch_bool_t first)
+format_part_json (const void *ctx, sprinter_t *sp, mime_node_t *node,
+                 notmuch_bool_t first, notmuch_bool_t output_body)
 {
     /* Any changes to the JSON format should be reflected in the file
      * devel/schemata. */
@@ -568,12 +573,14 @@ format_part_json (const void *ctx, mime_node_t *node, notmuch_bool_t first)
        format_message_json (ctx, node->envelope_file);
 
        printf ("\"headers\": ");
-       format_headers_json (ctx, GMIME_MESSAGE (node->part), FALSE);
-
-       printf (", \"body\": [");
-       format_part_json (ctx, mime_node_child (node, 0), first);
+       format_headers_json (sp, GMIME_MESSAGE (node->part), FALSE);
 
-       printf ("]}");
+       if (output_body) {
+           printf (", \"body\": [");
+           format_part_json (ctx, sp, mime_node_child (node, 0), first, TRUE);
+           printf ("]");
+       }
+       printf ("}");
        return;
     }
 
@@ -642,7 +649,7 @@ format_part_json (const void *ctx, mime_node_t *node, notmuch_bool_t first)
     } else if (GMIME_IS_MESSAGE (node->part)) {
        printf (", \"content\": [{");
        printf ("\"headers\": ");
-       format_headers_json (local, GMIME_MESSAGE (node->part), FALSE);
+       format_headers_json (sp, GMIME_MESSAGE (node->part), FALSE);
 
        printf (", \"body\": [");
        terminator = "]}]";
@@ -651,16 +658,17 @@ format_part_json (const void *ctx, mime_node_t *node, notmuch_bool_t first)
     talloc_free (local);
 
     for (i = 0; i < node->nchildren; i++)
-       format_part_json (ctx, mime_node_child (node, i), i == 0);
+       format_part_json (ctx, sp, mime_node_child (node, i), i == 0, TRUE);
 
     printf ("%s}", terminator);
 }
 
 static notmuch_status_t
-format_part_json_entry (const void *ctx, mime_node_t *node, unused (int indent),
-                       unused (const notmuch_show_params_t *params))
+format_part_json_entry (const void *ctx, sprinter_t *sp,
+                       mime_node_t *node, unused (int indent),
+                       const notmuch_show_params_t *params)
 {
-    format_part_json (ctx, node, TRUE);
+    format_part_json (ctx, sp, node, TRUE, params->output_body);
 
     return NOTMUCH_STATUS_SUCCESS;
 }
@@ -671,7 +679,8 @@ format_part_json_entry (const void *ctx, mime_node_t *node, unused (int indent),
  * http://qmail.org/qmail-manual-html/man5/mbox.html
  */
 static notmuch_status_t
-format_part_mbox (const void *ctx, mime_node_t *node, unused (int indent),
+format_part_mbox (const void *ctx, unused (sprinter_t *sp), mime_node_t *node,
+                 unused (int indent),
                  unused (const notmuch_show_params_t *params))
 {
     notmuch_message_t *message = node->envelope_file;
@@ -722,8 +731,8 @@ format_part_mbox (const void *ctx, mime_node_t *node, unused (int indent),
 }
 
 static notmuch_status_t
-format_part_raw (unused (const void *ctx), mime_node_t *node,
-                unused (int indent),
+format_part_raw (unused (const void *ctx), unused (sprinter_t *sp),
+                mime_node_t *node, unused (int indent),
                 unused (const notmuch_show_params_t *params))
 {
     if (node->envelope_file) {
@@ -799,9 +808,19 @@ format_part_raw (unused (const void *ctx), mime_node_t *node,
     return NOTMUCH_STATUS_SUCCESS;
 }
 
+static notmuch_status_t
+show_null_message (const notmuch_show_format_t *format)
+{
+    /* Output a null message. Currently empty for all formats except Json */
+    if (format->null_message)
+       printf ("%s", format->null_message);
+    return NOTMUCH_STATUS_SUCCESS;
+}
+
 static notmuch_status_t
 show_message (void *ctx,
              const notmuch_show_format_t *format,
+             sprinter_t *sp,
              notmuch_message_t *message,
              int indent,
              notmuch_show_params_t *params)
@@ -815,7 +834,7 @@ show_message (void *ctx,
        goto DONE;
     part = mime_node_seek_dfs (root, (params->part < 0 ? 0 : params->part));
     if (part)
-       status = format->part (local, part, indent, params);
+       status = format->part (local, sp, part, indent, params);
   DONE:
     talloc_free (local);
     return status;
@@ -824,6 +843,7 @@ show_message (void *ctx,
 static notmuch_status_t
 show_messages (void *ctx,
               const notmuch_show_format_t *format,
+              sprinter_t *sp,
               notmuch_messages_t *messages,
               int indent,
               notmuch_show_params_t *params)
@@ -857,17 +877,19 @@ show_messages (void *ctx,
        next_indent = indent;
 
        if ((match && (!excluded || !params->omit_excluded)) || params->entire_thread) {
-           status = show_message (ctx, format, message, indent, params);
+           status = show_message (ctx, format, sp, message, indent, params);
            if (status && !res)
                res = status;
            next_indent = indent + 1;
-
-           if (!status && format->message_set_sep)
-               fputs (format->message_set_sep, stdout);
+       } else {
+           status = show_null_message (format);
        }
 
+       if (!status && format->message_set_sep)
+           fputs (format->message_set_sep, stdout);
+
        status = show_messages (ctx,
-                               format,
+                               format, sp,
                                notmuch_message_get_replies (message),
                                next_indent,
                                params);
@@ -891,6 +913,7 @@ static int
 do_show_single (void *ctx,
                notmuch_query_t *query,
                const notmuch_show_format_t *format,
+               sprinter_t *sp,
                notmuch_show_params_t *params)
 {
     notmuch_messages_t *messages;
@@ -911,7 +934,8 @@ do_show_single (void *ctx,
 
     notmuch_message_set_flag (message, NOTMUCH_MESSAGE_FLAG_MATCH, 1);
 
-    return show_message (ctx, format, message, 0, params) != NOTMUCH_STATUS_SUCCESS;
+    return show_message (ctx, format, sp, message, 0, params)
+       != NOTMUCH_STATUS_SUCCESS;
 }
 
 /* Formatted output of threads */
@@ -919,6 +943,7 @@ static int
 do_show (void *ctx,
         notmuch_query_t *query,
         const notmuch_show_format_t *format,
+        sprinter_t *sp,
         notmuch_show_params_t *params)
 {
     notmuch_threads_t *threads;
@@ -946,7 +971,7 @@ do_show (void *ctx,
            fputs (format->message_set_sep, stdout);
        first_toplevel = 0;
 
-       status = show_messages (ctx, format, messages, 0, params);
+       status = show_messages (ctx, format, sp, messages, 0, params);
        if (status && !res)
            res = status;
 
@@ -968,6 +993,12 @@ enum {
     NOTMUCH_FORMAT_RAW
 };
 
+enum {
+    ENTIRE_THREAD_DEFAULT,
+    ENTIRE_THREAD_TRUE,
+    ENTIRE_THREAD_FALSE,
+};
+
 /* The following is to allow future options to be added more easily */
 enum {
     EXCLUDE_TRUE,
@@ -983,9 +1014,11 @@ notmuch_show_command (void *ctx, unused (int argc), unused (char *argv[]))
     char *query_string;
     int opt_index, ret;
     const notmuch_show_format_t *format = &format_text;
+    sprinter_t *sprinter;
     notmuch_show_params_t params = {
        .part = -1,
        .omit_excluded = TRUE,
+       .output_body = TRUE,
        .crypto = {
            .verify = FALSE,
            .decrypt = FALSE
@@ -993,6 +1026,7 @@ notmuch_show_command (void *ctx, unused (int argc), unused (char *argv[]))
     };
     int format_sel = NOTMUCH_FORMAT_NOT_SPECIFIED;
     int exclude = EXCLUDE_TRUE;
+    int entire_thread = ENTIRE_THREAD_DEFAULT;
 
     notmuch_opt_desc_t options[] = {
        { NOTMUCH_OPT_KEYWORD, &format_sel, "format", 'f',
@@ -1001,14 +1035,19 @@ notmuch_show_command (void *ctx, unused (int argc), unused (char *argv[]))
                                  { "mbox", NOTMUCH_FORMAT_MBOX },
                                  { "raw", NOTMUCH_FORMAT_RAW },
                                  { 0, 0 } } },
-        { NOTMUCH_OPT_KEYWORD, &exclude, "exclude", 'x',
-          (notmuch_keyword_t []){ { "true", EXCLUDE_TRUE },
-                                  { "false", EXCLUDE_FALSE },
-                                  { 0, 0 } } },
+       { NOTMUCH_OPT_KEYWORD, &exclude, "exclude", 'x',
+         (notmuch_keyword_t []){ { "true", EXCLUDE_TRUE },
+                                 { "false", EXCLUDE_FALSE },
+                                 { 0, 0 } } },
+       { NOTMUCH_OPT_KEYWORD, &entire_thread, "entire-thread", 't',
+         (notmuch_keyword_t []){ { "true", ENTIRE_THREAD_TRUE },
+                                 { "false", ENTIRE_THREAD_FALSE },
+                                 { "", ENTIRE_THREAD_TRUE },
+                                 { 0, 0 } } },
        { NOTMUCH_OPT_INT, &params.part, "part", 'p', 0 },
-       { NOTMUCH_OPT_BOOLEAN, &params.entire_thread, "entire-thread", 't', 0 },
        { NOTMUCH_OPT_BOOLEAN, &params.crypto.decrypt, "decrypt", 'd', 0 },
        { NOTMUCH_OPT_BOOLEAN, &params.crypto.verify, "verify", 'v', 0 },
+       { NOTMUCH_OPT_BOOLEAN, &params.output_body, "body", 'b', 0 },
        { 0, 0, 0, 0, 0 }
     };
 
@@ -1033,7 +1072,6 @@ notmuch_show_command (void *ctx, unused (int argc), unused (char *argv[]))
     switch (format_sel) {
     case NOTMUCH_FORMAT_JSON:
        format = &format_json;
-       params.entire_thread = TRUE;
        break;
     case NOTMUCH_FORMAT_TEXT:
        format = &format_text;
@@ -1056,6 +1094,29 @@ notmuch_show_command (void *ctx, unused (int argc), unused (char *argv[]))
        break;
     }
 
+    /* Default is entire-thread = FALSE except for format=json. */
+    if (entire_thread == ENTIRE_THREAD_DEFAULT) {
+       if (format == &format_json)
+           entire_thread = ENTIRE_THREAD_TRUE;
+       else
+           entire_thread = ENTIRE_THREAD_FALSE;
+    }
+
+    if (!params.output_body) {
+       if (params.part > 0) {
+           fprintf (stderr, "Warning: --body=false is incompatible with --part > 0. Disabling.\n");
+           params.output_body = TRUE;
+       } else {
+           if (format != &format_json)
+               fprintf (stderr, "Warning: --body=false only implemented for format=json\n");
+       }
+    }
+
+    if (entire_thread == ENTIRE_THREAD_TRUE)
+       params.entire_thread = TRUE;
+    else
+       params.entire_thread = FALSE;
+
     config = notmuch_config_open (ctx, NULL, NULL);
     if (config == NULL)
        return 1;
@@ -1081,9 +1142,12 @@ notmuch_show_command (void *ctx, unused (int argc), unused (char *argv[]))
        return 1;
     }
 
+    /* Create structure printer. */
+    sprinter = format->new_sprinter(ctx, stdout);
+
     /* If a single message is requested we do not use search_excludes. */
     if (params.part >= 0)
-       ret = do_show_single (ctx, query, format, &params);
+       ret = do_show_single (ctx, query, format, sprinter, &params);
     else {
        /* We always apply set the exclude flag. The
         * exclude=true|false option controls whether or not we return
@@ -1102,7 +1166,7 @@ notmuch_show_command (void *ctx, unused (int argc), unused (char *argv[]))
            params.omit_excluded = FALSE;
        }
 
-       ret = do_show (ctx, query, format, &params);
+       ret = do_show (ctx, query, format, sprinter, &params);
     }
 
     notmuch_crypto_cleanup (&params.crypto);