+ printf (", \"content\": [");
+ }
+
+ talloc_free (ctx);
+ if (stream_memory)
+ g_object_unref (stream_memory);
+}
+
+static void
+format_part_end_json (GMimeObject *part)
+{
+ GMimeContentType *content_type;
+
+ content_type = g_mime_object_get_content_type (GMIME_OBJECT (part));
+
+ if (g_mime_content_type_is_type (content_type, "multipart", "*") ||
+ g_mime_content_type_is_type (content_type, "message", "rfc822"))
+ printf ("]");
+
+ printf ("}");
+}
+
+static void
+format_part_content_raw (GMimeObject *part)
+{
+ GMimeStream *stream_stdout;
+ GMimeStream *stream_filter = NULL;
+ GMimeDataWrapper *wrapper;
+
+ stream_stdout = g_mime_stream_file_new (stdout);
+ g_mime_stream_file_set_owner (GMIME_STREAM_FILE (stream_stdout), FALSE);
+
+ stream_filter = g_mime_stream_filter_new (stream_stdout);
+
+ wrapper = g_mime_part_get_content_object (GMIME_PART (part));
+
+ if (wrapper && stream_filter)
+ g_mime_data_wrapper_write_to_stream (wrapper, stream_filter);
+
+ if (stream_filter)
+ g_object_unref (stream_filter);
+
+ if (stream_stdout)
+ g_object_unref(stream_stdout);
+}
+
+static void
+show_message (void *ctx,
+ const notmuch_show_format_t *format,
+ notmuch_message_t *message,
+ int indent,
+ notmuch_show_params_t *params)
+{
+ if (params->part <= 0) {
+ fputs (format->message_start, stdout);
+ if (format->message)
+ format->message(ctx, message, indent);
+
+ fputs (format->header_start, stdout);
+ if (format->header)
+ format->header(ctx, message);
+ fputs (format->header_end, stdout);
+
+ fputs (format->body_start, stdout);
+ }
+
+ if (format->part_content)
+ show_message_body (notmuch_message_get_filename (message),
+ format, params);
+
+ if (params->part <= 0) {
+ fputs (format->body_end, stdout);
+
+ fputs (format->message_end, stdout);
+ }
+}
+
+static void
+show_messages (void *ctx,
+ const notmuch_show_format_t *format,
+ notmuch_messages_t *messages,
+ int indent,
+ notmuch_show_params_t *params)
+{
+ notmuch_message_t *message;
+ notmuch_bool_t match;
+ int first_set = 1;
+ int next_indent;
+
+ fputs (format->message_set_start, stdout);
+
+ for (;
+ notmuch_messages_valid (messages);
+ notmuch_messages_move_to_next (messages))
+ {
+ if (!first_set)
+ fputs (format->message_set_sep, stdout);
+ first_set = 0;
+
+ fputs (format->message_set_start, stdout);
+
+ message = notmuch_messages_get (messages);
+
+ match = notmuch_message_get_flag (message, NOTMUCH_MESSAGE_FLAG_MATCH);
+
+ next_indent = indent;
+
+ if (match || params->entire_thread) {
+ show_message (ctx, format, message, indent, params);
+ next_indent = indent + 1;
+
+ fputs (format->message_set_sep, stdout);
+ }
+
+ show_messages (ctx,
+ format,
+ notmuch_message_get_replies (message),
+ next_indent,
+ params);
+
+ notmuch_message_destroy (message);
+
+ fputs (format->message_set_end, stdout);
+ }
+
+ fputs (format->message_set_end, stdout);
+}
+
+/* Formatted output of single message */
+static int
+do_show_single (void *ctx,
+ notmuch_query_t *query,
+ const notmuch_show_format_t *format,
+ notmuch_show_params_t *params)
+{
+ notmuch_messages_t *messages;
+ notmuch_message_t *message;
+
+ 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 (message == NULL) {
+ fprintf (stderr, "Error: Cannot find matching message.\n");
+ return 1;
+ }
+
+ notmuch_message_set_flag (message, NOTMUCH_MESSAGE_FLAG_MATCH, 1);
+
+ /* Special case for --format=raw of full single message, just cat out file */
+ if (params->raw && 0 == params->part) {
+
+ const char *filename;
+ FILE *file;
+ size_t size;
+ char buf[4096];
+
+ filename = notmuch_message_get_filename (message);
+ if (filename == NULL) {
+ fprintf (stderr, "Error: Cannot message filename.\n");
+ return 1;
+ }
+
+ file = fopen (filename, "r");
+ if (file == NULL) {
+ fprintf (stderr, "Error: Cannot open file %s: %s\n", filename, strerror (errno));
+ return 1;
+ }
+
+ while (!feof (file)) {
+ size = fread (buf, 1, sizeof (buf), file);
+ fwrite (buf, size, 1, stdout);
+ }
+
+ fclose (file);
+
+ } else {
+
+ show_message (ctx, format, message, 0, params);
+
+ }
+
+ return 0;
+}
+
+/* Formatted output of threads */
+static int
+do_show (void *ctx,
+ notmuch_query_t *query,
+ const notmuch_show_format_t *format,
+ notmuch_show_params_t *params)
+{
+ notmuch_threads_t *threads;
+ notmuch_thread_t *thread;
+ notmuch_messages_t *messages;
+ int first_toplevel = 1;
+
+ fputs (format->message_set_start, stdout);
+
+ for (threads = notmuch_query_search_threads (query);
+ notmuch_threads_valid (threads);
+ notmuch_threads_move_to_next (threads))
+ {
+ thread = notmuch_threads_get (threads);
+
+ messages = notmuch_thread_get_toplevel_messages (thread);
+
+ if (messages == NULL)
+ INTERNAL_ERROR ("Thread %s has no toplevel messages.\n",
+ notmuch_thread_get_thread_id (thread));
+
+ if (!first_toplevel)
+ fputs (format->message_set_sep, stdout);
+ first_toplevel = 0;
+
+ show_messages (ctx, format, messages, 0, params);
+
+ notmuch_thread_destroy (thread);
+