notmuch show: Properly nest MIME parts within mulipart parts

Previously, notmuch show flattened all output, losing information
about the nesting of the MIME hierarchy. Now, the output is properly
nested, (both in the --format=text and --format=json output), so that
clients can analyze the original MIME structure.

Internally, this required splitting the final closing delimiter out of
the various show_part functions and putting it into a new
show_part_end function instead. Also, the show_part function now
accepts a new "first" argument that is set not only for the first MIME
part of a message, but also for each first MIME part within a series
of multipart parts. This "first" argument controls the omission of a
preceding comma when printing a part (for json).

Many thanks to David Edmondson <dme@dme.org> for originally
identifying the lack of nesting in the json output and submitting an
early implementation of this feature. Thanks as well to Jameson Graef
Rollins <jrollins@finestructure.net> for carefully shepherding David's
patches through a remarkably long review process, patiently explaining
them, and providing a cleaned up series that led to this final
implementation. Jameson also provided the new emacs code here.
This commit is contained in:
Carl Worth 2011-05-17 15:34:57 -07:00
parent c51d5b3cdb
commit 362ab047c2
8 changed files with 104 additions and 37 deletions

View file

@ -280,6 +280,15 @@ current buffer, if possible."
t) t)
nil))))) nil)))))
(defun notmuch-show-insert-part-multipart/* (msg part content-type nth depth declared-type)
(let ((inner-parts (plist-get part :content)))
(notmuch-show-insert-part-header nth declared-type content-type nil)
;; Show all of the parts.
(mapc (lambda (inner-part)
(notmuch-show-insert-bodypart msg inner-part depth))
inner-parts))
t)
(defun notmuch-show-insert-part-text/plain (msg part content-type nth depth declared-type) (defun notmuch-show-insert-part-text/plain (msg part content-type nth depth declared-type)
(let ((start (point))) (let ((start (point)))
;; If this text/plain part is not the first part in the message, ;; If this text/plain part is not the first part in the message,

View file

@ -133,7 +133,8 @@ query_string_from_args (void *ctx, int argc, char *argv[]);
notmuch_status_t notmuch_status_t
show_message_body (const char *filename, show_message_body (const char *filename,
void (*show_part) (GMimeObject *part, int *part_count)); void (*show_part) (GMimeObject *part, int *part_count, int first),
void (*show_part_end) (GMimeObject *part));
notmuch_status_t notmuch_status_t
show_one_part (const char *filename, int part); show_one_part (const char *filename, int part);

View file

@ -72,7 +72,7 @@ show_reply_headers (GMimeMessage *message)
} }
static void static void
reply_part (GMimeObject *part, int *part_count) reply_part (GMimeObject *part, int *part_count, unused (int first))
{ {
GMimeContentDisposition *disposition; GMimeContentDisposition *disposition;
GMimeContentType *content_type; GMimeContentType *content_type;
@ -505,7 +505,8 @@ notmuch_reply_format_default(void *ctx, notmuch_config_t *config, notmuch_query_
notmuch_message_get_header (message, "date"), notmuch_message_get_header (message, "date"),
notmuch_message_get_header (message, "from")); notmuch_message_get_header (message, "from"));
show_message_body (notmuch_message_get_filename (message), reply_part); show_message_body (notmuch_message_get_filename (message),
reply_part, NULL);
notmuch_message_destroy (message); notmuch_message_destroy (message);
} }

View file

@ -32,7 +32,8 @@ typedef struct show_format {
const char *header_end; const char *header_end;
const char *body_start; const char *body_start;
void (*part) (GMimeObject *part, void (*part) (GMimeObject *part,
int *part_count); int *part_count, int first);
void (*part_end) (GMimeObject *part);
const char *body_end; const char *body_end;
const char *message_end; const char *message_end;
const char *message_set_sep; const char *message_set_sep;
@ -46,14 +47,20 @@ format_message_text (unused (const void *ctx),
static void static void
format_headers_text (const void *ctx, format_headers_text (const void *ctx,
notmuch_message_t *message); notmuch_message_t *message);
static void static void
format_part_text (GMimeObject *part, format_part_text (GMimeObject *part,
int *part_count); int *part_count,
int first);
static void
format_part_end_text (GMimeObject *part);
static const show_format_t format_text = { static const show_format_t format_text = {
"", "",
"\fmessage{ ", format_message_text, "\fmessage{ ", format_message_text,
"\fheader{\n", format_headers_text, "\fheader}\n", "\fheader{\n", format_headers_text, "\fheader}\n",
"\fbody{\n", format_part_text, "\fbody}\n", "\fbody{\n", format_part_text, format_part_end_text, "\fbody}\n",
"\fmessage}\n", "", "\fmessage}\n", "",
"" ""
}; };
@ -65,14 +72,20 @@ format_message_json (const void *ctx,
static void static void
format_headers_json (const void *ctx, format_headers_json (const void *ctx,
notmuch_message_t *message); notmuch_message_t *message);
static void static void
format_part_json (GMimeObject *part, format_part_json (GMimeObject *part,
int *part_count); int *part_count,
int first);
static void
format_part_end_json (GMimeObject *part);
static const show_format_t format_json = { static const show_format_t format_json = {
"[", "[",
"{", format_message_json, "{", format_message_json,
", \"headers\": {", format_headers_json, "}", ", \"headers\": {", format_headers_json, "}",
", \"body\": [", format_part_json, "]", ", \"body\": [", format_part_json, format_part_end_json, "]",
"}", ", ", "}", ", ",
"]" "]"
}; };
@ -86,7 +99,7 @@ static const show_format_t format_mbox = {
"", "",
"", format_message_mbox, "", format_message_mbox,
"", NULL, "", "", NULL, "",
"", NULL, "", "", NULL, NULL, "",
"", "", "", "",
"" ""
}; };
@ -364,7 +377,7 @@ show_part_content (GMimeObject *part, GMimeStream *stream_out)
} }
static void static void
format_part_text (GMimeObject *part, int *part_count) format_part_text (GMimeObject *part, int *part_count, unused (int first))
{ {
GMimeContentDisposition *disposition; GMimeContentDisposition *disposition;
GMimeContentType *content_type; GMimeContentType *content_type;
@ -391,8 +404,6 @@ format_part_text (GMimeObject *part, int *part_count)
g_object_unref(stream_stdout); g_object_unref(stream_stdout);
} }
printf ("\fattachment}\n");
return; return;
} }
@ -420,12 +431,27 @@ format_part_text (GMimeObject *part, int *part_count)
printf ("Non-text part: %s\n", printf ("Non-text part: %s\n",
g_mime_content_type_to_string (content_type)); g_mime_content_type_to_string (content_type));
} }
printf ("\fpart}\n");
} }
static void static void
format_part_json (GMimeObject *part, int *part_count) format_part_end_text (GMimeObject *part)
{
GMimeContentDisposition *disposition;
disposition = g_mime_object_get_content_disposition (part);
if (disposition &&
strcmp (disposition->disposition, GMIME_DISPOSITION_ATTACHMENT) == 0)
{
printf ("\fattachment}\n");
}
else
{
printf ("\fpart}\n");
}
}
static void
format_part_json (GMimeObject *part, int *part_count, int first)
{ {
GMimeContentType *content_type; GMimeContentType *content_type;
GMimeContentDisposition *disposition; GMimeContentDisposition *disposition;
@ -435,7 +461,7 @@ format_part_json (GMimeObject *part, int *part_count)
content_type = g_mime_object_get_content_type (GMIME_OBJECT (part)); content_type = g_mime_object_get_content_type (GMIME_OBJECT (part));
if (*part_count > 1) if (! first)
fputs (", ", stdout); fputs (", ", stdout);
printf ("{\"id\": %d, \"content-type\": %s", printf ("{\"id\": %d, \"content-type\": %s",
@ -459,14 +485,29 @@ format_part_json (GMimeObject *part, int *part_count)
printf (", \"content\": %s", json_quote_chararray (ctx, (char *) part_content->data, part_content->len)); printf (", \"content\": %s", json_quote_chararray (ctx, (char *) part_content->data, part_content->len));
} }
else if (g_mime_content_type_is_type (content_type, "multipart", "*"))
fputs ("}", stdout); {
printf (", \"content\": [");
}
talloc_free (ctx); talloc_free (ctx);
if (stream_memory) if (stream_memory)
g_object_unref (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", "*"))
printf ("]");
printf ("}");
}
static void static void
show_message (void *ctx, const show_format_t *format, notmuch_message_t *message, int indent) show_message (void *ctx, const show_format_t *format, notmuch_message_t *message, int indent)
{ {
@ -481,7 +522,8 @@ show_message (void *ctx, const show_format_t *format, notmuch_message_t *message
fputs (format->body_start, stdout); fputs (format->body_start, stdout);
if (format->part) if (format->part)
show_message_body (notmuch_message_get_filename (message), format->part); show_message_body (notmuch_message_get_filename (message),
format->part, format->part_end);
fputs (format->body_end, stdout); fputs (format->body_end, stdout);
fputs (format->message_end, stdout); fputs (format->message_end, stdout);

View file

@ -260,7 +260,8 @@ decoded. Various components in the output,
will be delimited by easily-parsed markers. Each marker consists of a will be delimited by easily-parsed markers. Each marker consists of a
Control-L character (ASCII decimal 12), the name of the marker, and Control-L character (ASCII decimal 12), the name of the marker, and
then either an opening or closing brace, ('{' or '}'), to either open then either an opening or closing brace, ('{' or '}'), to either open
or close the component. or close the component. For a multipart MIME message, these parts will
be nested.
.RE .RE
.RS 4 .RS 4
.TP 4 .TP 4
@ -268,8 +269,9 @@ or close the component.
The output is formatted with Javascript Object Notation (JSON). This The output is formatted with Javascript Object Notation (JSON). This
format is more robust than the text format for automated format is more robust than the text format for automated
processing. JSON output always includes all messages in a matching processing. The nested structure of multipart MIME messages is
thread; in effect reflected in nested JSON output. JSON output always includes all
messages in a matching thread; in effect
.B \-\-format=json .B \-\-format=json
implies implies
.B \-\-entire\-thread .B \-\-entire\-thread

View file

@ -238,15 +238,17 @@ command_t commands[] = {
"\t\teasily-parsed markers. Each marker consists of a Control-L\n" "\t\teasily-parsed markers. Each marker consists of a Control-L\n"
"\t\tcharacter (ASCII decimal 12), the name of the marker, and\n" "\t\tcharacter (ASCII decimal 12), the name of the marker, and\n"
"\t\tthen either an opening or closing brace, '{' or '}' to\n" "\t\tthen either an opening or closing brace, '{' or '}' to\n"
"\t\teither open or close the component.\n" "\t\teither open or close the component. For a multipart MIME\n"
"\t\tmessage, these parts will be nested.\n"
"\n" "\n"
"\t\tjson\n" "\t\tjson\n"
"\n" "\n"
"\t\tThe output is formatted with Javascript Object Notation\n" "\t\tThe output is formatted with Javascript Object Notation\n"
"\t\t(JSON). This format is more robust than the text format\n" "\t\t(JSON). This format is more robust than the text format\n"
"\t\tfor automated processing. JSON output always includes all\n" "\t\tfor automated processing. The nested structure of multipart\n"
"\t\tmessages in a matching thread; in effect '--format=json'\n" "\t\tMIME messages is reflected in nested JSON output. JSON\n"
"\t\timplies '--entire-thread'\n" "\t\toutput always includes all messages in a matching thread;\n"
"\t\tin effect '--format=json' implies '--entire-thread'\n"
"\n" "\n"
"\t\tmbox\n" "\t\tmbox\n"
"\n" "\n"

View file

@ -23,20 +23,27 @@
#include "notmuch-client.h" #include "notmuch-client.h"
static void static void
show_message_part (GMimeObject *part, int *part_count, show_message_part (GMimeObject *part,
void (*show_part) (GMimeObject *part, int *part_count)) int *part_count,
void (*show_part) (GMimeObject *part, int *part_count, int first),
void (*show_part_end) (GMimeObject *part),
int first)
{ {
if (GMIME_IS_MULTIPART (part)) { if (GMIME_IS_MULTIPART (part)) {
GMimeMultipart *multipart = GMIME_MULTIPART (part); GMimeMultipart *multipart = GMIME_MULTIPART (part);
int i; int i;
*part_count = *part_count + 1; *part_count = *part_count + 1;
(*show_part) (part, part_count); (*show_part) (part, part_count, first);
for (i = 0; i < g_mime_multipart_get_count (multipart); i++) { for (i = 0; i < g_mime_multipart_get_count (multipart); i++) {
show_message_part (g_mime_multipart_get_part (multipart, i), show_message_part (g_mime_multipart_get_part (multipart, i),
part_count, show_part); part_count, show_part, show_part_end, i == 0);
} }
if (show_part_end)
(*show_part_end) (part);
return; return;
} }
@ -46,7 +53,7 @@ show_message_part (GMimeObject *part, int *part_count,
mime_message = g_mime_message_part_get_message (GMIME_MESSAGE_PART (part)); mime_message = g_mime_message_part_get_message (GMIME_MESSAGE_PART (part));
show_message_part (g_mime_message_get_mime_part (mime_message), show_message_part (g_mime_message_get_mime_part (mime_message),
part_count, show_part); part_count, show_part, show_part_end, first);
return; return;
} }
@ -59,12 +66,15 @@ show_message_part (GMimeObject *part, int *part_count,
*part_count = *part_count + 1; *part_count = *part_count + 1;
(*show_part) (part, part_count); (*show_part) (part, part_count, first);
if (show_part_end)
(*show_part_end) (part);
} }
notmuch_status_t notmuch_status_t
show_message_body (const char *filename, show_message_body (const char *filename,
void (*show_part) (GMimeObject *part, int *part_count)) void (*show_part) (GMimeObject *part, int *part_count, int first),
void (*show_part_end) (GMimeObject *part))
{ {
GMimeStream *stream = NULL; GMimeStream *stream = NULL;
GMimeParser *parser = NULL; GMimeParser *parser = NULL;
@ -88,7 +98,7 @@ show_message_body (const char *filename,
mime_message = g_mime_parser_construct_message (parser); mime_message = g_mime_parser_construct_message (parser);
show_message_part (g_mime_message_get_mime_part (mime_message), show_message_part (g_mime_message_get_mime_part (mime_message),
&part_count, show_part); &part_count, show_part, show_part_end, TRUE);
DONE: DONE:
if (mime_message) if (mime_message)

View file

@ -59,9 +59,7 @@ Date: Tue, 05 Jan 2001 15:43:57 -0000
header} header}
body{ body{
part{ ID: 1, Content-type: multipart/signed part{ ID: 1, Content-type: multipart/signed
part}
part{ ID: 2, Content-type: multipart/mixed part{ ID: 2, Content-type: multipart/mixed
part}
part{ ID: 3, Content-type: text/plain part{ ID: 3, Content-type: text/plain
This is an inline text part. This is an inline text part.
part} part}
@ -74,15 +72,17 @@ And this message is signed.
-Carl -Carl
part} part}
part}
part{ ID: 6, Content-type: application/pgp-signature part{ ID: 6, Content-type: application/pgp-signature
Non-text part: application/pgp-signature Non-text part: application/pgp-signature
part} part}
part}
body} body}
message}" message}"
test_begin_subtest "Show multipart MIME message (--format=json)" test_begin_subtest "Show multipart MIME message (--format=json)"
output=$(notmuch show --format=json 'id:87liy5ap00.fsf@yoom.home.cworth.org') output=$(notmuch show --format=json 'id:87liy5ap00.fsf@yoom.home.cworth.org')
test_expect_equal "$output" '[[[{"id": "87liy5ap00.fsf@yoom.home.cworth.org", "match": true, "filename": "/home/cworth/src/notmuch/test/tmp.multipart/mail/multipart", "timestamp": 978709437, "date_relative": "2001-01-05", "tags": ["attachment","inbox","unread"], "headers": {"Subject": "Multipart message", "From": "Carl Worth <cworth@cworth.org>", "To": "cworth@cworth.org", "Cc": "", "Bcc": "", "Date": "Tue, 05 Jan 2001 15:43:57 -0000"}, "body": [{"id": 1, "content-type": "multipart/signed"}, {"id": 2, "content-type": "multipart/mixed"}, {"id": 3, "content-type": "text/plain", "content": "This is an inline text part.\n"}, {"id": 4, "content-type": "text/plain", "filename": "attachment", "content": "This is a text attachment.\n"}, {"id": 5, "content-type": "text/plain", "content": "And this message is signed.\n\n-Carl\n"}, {"id": 6, "content-type": "application/pgp-signature"}]}, []]]]' test_expect_equal "$output" '[[[{"id": "87liy5ap00.fsf@yoom.home.cworth.org", "match": true, "filename": "/home/cworth/src/notmuch/test/tmp.multipart/mail/multipart", "timestamp": 978709437, "date_relative": "2001-01-05", "tags": ["attachment","inbox","unread"], "headers": {"Subject": "Multipart message", "From": "Carl Worth <cworth@cworth.org>", "To": "cworth@cworth.org", "Cc": "", "Bcc": "", "Date": "Tue, 05 Jan 2001 15:43:57 -0000"}, "body": [{"id": 1, "content-type": "multipart/signed", "content": [{"id": 2, "content-type": "multipart/mixed", "content": [{"id": 3, "content-type": "text/plain", "content": "This is an inline text part.\n"}, {"id": 4, "content-type": "text/plain", "filename": "attachment", "content": "This is a text attachment.\n"}, {"id": 5, "content-type": "text/plain", "content": "And this message is signed.\n\n-Carl\n"}]}, {"id": 6, "content-type": "application/pgp-signature"}]}]}, []]]]'
test_done test_done