+ if (GMIME_IS_MESSAGE (node->part)) {
+ GMimeMessage *message = GMIME_MESSAGE (node->part);
+ InternetAddressList *recipients;
+ const char *recipients_string;
+
+ printf ("\fheader{\n");
+ if (node->envelope_file)
+ printf ("%s\n", _get_one_line_summary (ctx, node->envelope_file));
+ printf ("Subject: %s\n", g_mime_message_get_subject (message));
+ printf ("From: %s\n", 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 ("To: %s\n", 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 ("Cc: %s\n", recipients_string);
+ printf ("Date: %s\n", g_mime_message_get_date_as_string (message));
+ printf ("\fheader}\n");
+
+ printf ("\fbody{\n");
+ }
+
+ if (leaf) {
+ if (g_mime_content_type_is_type (content_type, "text", "*") &&
+ !g_mime_content_type_is_type (content_type, "text", "html"))
+ {
+ GMimeStream *stream_stdout = g_mime_stream_file_new (stdout);
+ g_mime_stream_file_set_owner (GMIME_STREAM_FILE (stream_stdout), FALSE);
+ show_text_part_content (node->part, stream_stdout, 0);
+ g_object_unref(stream_stdout);
+ } else {
+ printf ("Non-text part: %s\n",
+ g_mime_content_type_to_string (content_type));
+ }
+ }
+
+ for (i = 0; i < node->nchildren; i++)
+ format_part_text (ctx, mime_node_child (node, i), indent, params);
+
+ if (GMIME_IS_MESSAGE (node->part))
+ printf ("\fbody}\n");
+
+ printf ("\f%s}\n", part_type);
+
+ return NOTMUCH_STATUS_SUCCESS;
+}
+
+void
+format_part_json (const void *ctx, 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. */
+
+ if (node->envelope_file) {
+ printf ("{");
+ format_message_json (ctx, node->envelope_file);
+
+ printf ("\"headers\": ");
+ format_headers_json (ctx, GMIME_MESSAGE (node->part), FALSE);
+
+ if (output_body) {
+ printf (", \"body\": [");
+ format_part_json (ctx, mime_node_child (node, 0), first, TRUE);
+ printf ("]");
+ }
+ printf ("}");
+ return;
+ }
+
+ void *local = talloc_new (ctx);
+ /* The disposition and content-type metadata are associated with
+ * the envelope for message parts */
+ GMimeObject *meta = node->envelope_part ?
+ GMIME_OBJECT (node->envelope_part) : node->part;
+ GMimeContentType *content_type = g_mime_object_get_content_type (meta);
+ const char *cid = g_mime_object_get_content_id (meta);
+ const char *filename = GMIME_IS_PART (node->part) ?
+ g_mime_part_get_filename (GMIME_PART (node->part)) : NULL;
+ const char *terminator = "";
+ int i;
+
+ if (!first)
+ printf (", ");
+
+ printf ("{\"id\": %d", node->part_num);
+
+ if (node->decrypt_attempted)
+ printf (", \"encstatus\": [{\"status\": \"%s\"}]",
+ node->decrypt_success ? "good" : "bad");
+
+ if (node->verify_attempted) {
+ printf (", \"sigstatus\": ");
+ format_part_sigstatus_json (node);
+ }
+
+ printf (", \"content-type\": %s",
+ json_quote_str (local, g_mime_content_type_to_string (content_type)));
+
+ if (cid)
+ printf (", \"content-id\": %s", json_quote_str (local, cid));
+
+ if (filename)
+ printf (", \"filename\": %s", json_quote_str (local, filename));
+
+ if (GMIME_IS_PART (node->part)) {