diff options
author | Carl Worth <cworth@cworth.org> | 2011-05-17 15:34:57 -0700 |
---|---|---|
committer | Carl Worth <cworth@cworth.org> | 2011-05-17 15:58:57 -0700 |
commit | 362ab047c264ae67ec3de041aec637979077db21 (patch) | |
tree | b1a14c2f8a8434500b6d67c999570107c266a905 /notmuch-show.c | |
parent | c51d5b3cdb5ca0816816e88ca6f7136a24e74eee (diff) |
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.
Diffstat (limited to 'notmuch-show.c')
-rw-r--r-- | notmuch-show.c | 72 |
1 files changed, 57 insertions, 15 deletions
diff --git a/notmuch-show.c b/notmuch-show.c index c877152..8f485ef 100644 --- a/notmuch-show.c +++ b/notmuch-show.c @@ -32,7 +32,8 @@ typedef struct show_format { const char *header_end; const char *body_start; void (*part) (GMimeObject *part, - int *part_count); + int *part_count, int first); + void (*part_end) (GMimeObject *part); const char *body_end; const char *message_end; const char *message_set_sep; @@ -46,14 +47,20 @@ format_message_text (unused (const void *ctx), static void format_headers_text (const void *ctx, notmuch_message_t *message); + static void 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 = { "", "\fmessage{ ", format_message_text, "\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", "", "" }; @@ -65,14 +72,20 @@ format_message_json (const void *ctx, static void format_headers_json (const void *ctx, notmuch_message_t *message); + static void 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 = { "[", "{", format_message_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, "", NULL, "", - "", NULL, "", + "", NULL, NULL, "", "", "", "" }; @@ -364,7 +377,7 @@ show_part_content (GMimeObject *part, GMimeStream *stream_out) } static void -format_part_text (GMimeObject *part, int *part_count) +format_part_text (GMimeObject *part, int *part_count, unused (int first)) { GMimeContentDisposition *disposition; GMimeContentType *content_type; @@ -391,8 +404,6 @@ format_part_text (GMimeObject *part, int *part_count) g_object_unref(stream_stdout); } - printf ("\fattachment}\n"); - return; } @@ -420,12 +431,27 @@ format_part_text (GMimeObject *part, int *part_count) printf ("Non-text part: %s\n", g_mime_content_type_to_string (content_type)); } +} - printf ("\fpart}\n"); +static void +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) +format_part_json (GMimeObject *part, int *part_count, int first) { GMimeContentType *content_type; 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)); - if (*part_count > 1) + if (! first) fputs (", ", stdout); printf ("{\"id\": %d, \"content-type\": %s", @@ -459,8 +485,10 @@ format_part_json (GMimeObject *part, int *part_count) printf (", \"content\": %s", json_quote_chararray (ctx, (char *) part_content->data, part_content->len)); } - - fputs ("}", stdout); + else if (g_mime_content_type_is_type (content_type, "multipart", "*")) + { + printf (", \"content\": ["); + } talloc_free (ctx); if (stream_memory) @@ -468,6 +496,19 @@ format_part_json (GMimeObject *part, int *part_count) } 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 show_message (void *ctx, const show_format_t *format, notmuch_message_t *message, int indent) { fputs (format->message_start, stdout); @@ -481,7 +522,8 @@ show_message (void *ctx, const show_format_t *format, notmuch_message_t *message fputs (format->body_start, stdout); 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->message_end, stdout); |