From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from localhost (localhost [127.0.0.1]) by olra.theworths.org (Postfix) with ESMTP id C8D81429E3F for ; Fri, 23 Dec 2011 19:46:08 -0800 (PST) X-Virus-Scanned: Debian amavisd-new at olra.theworths.org X-Spam-Flag: NO X-Spam-Score: -0.7 X-Spam-Level: X-Spam-Status: No, score=-0.7 tagged_above=-999 required=5 tests=[RCVD_IN_DNSWL_LOW=-0.7] autolearn=disabled Received: from olra.theworths.org ([127.0.0.1]) by localhost (olra.theworths.org [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id BP65cdge-J1E for ; Fri, 23 Dec 2011 19:46:07 -0800 (PST) Received: from dmz-mailsec-scanner-2.mit.edu (DMZ-MAILSEC-SCANNER-2.MIT.EDU [18.9.25.13]) by olra.theworths.org (Postfix) with ESMTP id A3974429E30 for ; Fri, 23 Dec 2011 19:46:07 -0800 (PST) X-AuditID: 1209190d-b7f576d0000008c4-98-4ef54aff5c29 Received: from mailhub-auth-3.mit.edu ( [18.9.21.43]) by dmz-mailsec-scanner-2.mit.edu (Symantec Messaging Gateway) with SMTP id 07.33.02244.FFA45FE4; Fri, 23 Dec 2011 22:46:07 -0500 (EST) Received: from outgoing.mit.edu (OUTGOING-AUTH.MIT.EDU [18.7.22.103]) by mailhub-auth-3.mit.edu (8.13.8/8.9.2) with ESMTP id pBO3k7Wk024227; Fri, 23 Dec 2011 22:46:07 -0500 Received: from drake.mit.edu (c-76-21-105-205.hsd1.ca.comcast.net [76.21.105.205]) (authenticated bits=0) (User authenticated as amdragon@ATHENA.MIT.EDU) by outgoing.mit.edu (8.13.6/8.12.4) with ESMTP id pBO3k4UY017821 (version=TLSv1/SSLv3 cipher=AES256-SHA bits=256 verify=NOT); Fri, 23 Dec 2011 22:46:06 -0500 (EST) Received: from amthrax by drake.mit.edu with local (Exim 4.77) (envelope-from ) id 1ReIYe-0007IR-MD; Fri, 23 Dec 2011 22:46:04 -0500 From: Austin Clements To: notmuch@notmuchmail.org Subject: [PATCH v4 4/4] show: Rewrite show_message_body to use the MIME tree interface. Date: Fri, 23 Dec 2011 22:45:48 -0500 Message-Id: <1324698348-27620-5-git-send-email-amdragon@mit.edu> X-Mailer: git-send-email 1.7.7.3 In-Reply-To: <1324698348-27620-1-git-send-email-amdragon@mit.edu> References: <1323460468-4030-1-git-send-email-amdragon@mit.edu> <1324698348-27620-1-git-send-email-amdragon@mit.edu> X-Brightmail-Tracker: H4sIAAAAAAAAA+NgFtrIIsWRmVeSWpSXmKPExsUixCmqrfvf66ufwZO1XBbXb85kdmD0eLbq FnMAYxSXTUpqTmZZapG+XQJXxvmTH9gK7vlUzNj7ibWBcb9NFyMnh4SAicTck4uYIGwxiQv3 1rN1MXJxCAnsY5R4OvUrO4SzgVFi1cNrUJlzTBLb7s5ghHDmM0pcnvWfBaSfTUBDYtv+5Ywg toiAtMTOu7NZuxg5OJgF1CT+dKmAmMIC4RIb95qCVLAIqEosaNgDtplXwEHi8M+3zBBXKEic W32OHcTmFHCU2Lr1PSNIq5BAmUTzmaAJjPwLGBlWMcqm5Fbp5iZm5hSnJusWJyfm5aUW6Rrp 5WaW6KWmlG5iBIeMJO8OxncHlQ4xCnAwKvHwNi794ifEmlhWXJl7iFGSg0lJlPeq21c/Ib6k /JTKjMTijPii0pzU4kOMEhzMSiK8mklA5bwpiZVVqUX5MClpDhYlcV5VrXd+QgLpiSWp2amp BalFMFkZDg4lCd7pwNgQEixKTU+tSMvMKUFIM3FwggznARreDFLDW1yQmFucmQ6RP8WoKCXO mwWSEABJZJTmwfXCYvoVozjQK8K8PSBVPMB0ANf9CmgwE9DgGCOQq4tLEhFSUg2MBwJPW7m3 nO3tn7jC34lxgu0XbY9/XcyHJCuvCsX8fNcpcWdqRxxXZJTinZcXMw6+es7dzqm5NH7p8Qfs IWo6ex8o+mWIns7vmPB90fQTKx/nfLau0qntXeySLpq/paYnlu2kuWGN0erF/Hl/fpTwcYqX rtj9IGjSh1mS6qs8dI6vub31cbaYEktxRqKhFnNRcSIAxqUWm8QCAAA= X-BeenThere: notmuch@notmuchmail.org X-Mailman-Version: 2.1.13 Precedence: list List-Id: "Use and development of the notmuch mail system." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sat, 24 Dec 2011 03:46:09 -0000 This removes all of the MIME traversal logic from show_message_body and leaves only its interaction with the format callbacks. Besides isolating concerns, since traversal happens behind a trivial interface, there is now much less code duplication in show_message_part. Also, this uses mime_node_seek_dfs to start at the requested part, eliminating all of the logic about parts being selected or being in_zone (and reducing the "show state" to only a part counter). notmuch_show_params_t no longer needs to be passed through the recursion because the only two fields that mattered (related to crypto) are now handled by the MIME tree. The few remaining complexities in show_message_part highlight irregularities in the format callbacks with respect to top-level messages and embedded message parts. Since this is a rewrite, the diff is not very enlightening. It's easier to look at the old code and the new code side-by-side. --- show-message.c | 229 +++++++++++++------------------------------------------- 1 files changed, 52 insertions(+), 177 deletions(-) diff --git a/show-message.c b/show-message.c index 09fa607..d02e744 100644 --- a/show-message.c +++ b/show-message.c @@ -24,154 +24,54 @@ typedef struct show_message_state { int part_count; - int in_zone; } show_message_state_t; static void -show_message_part (GMimeObject *part, +show_message_part (mime_node_t *node, show_message_state_t *state, const notmuch_show_format_t *format, - notmuch_show_params_t *params, int first) { - GMimeObject *decryptedpart = NULL; - int selected; + /* Formatters expect the envelope for embedded message parts */ + GMimeObject *part = node->envelope_part ? + GMIME_OBJECT (node->envelope_part) : node->part; + int i; + + if (!first) + fputs (format->part_sep, stdout); + + /* Format this part */ + if (format->part_start) + format->part_start (part, &(state->part_count)); + + if (node->decrypt_attempted && format->part_encstatus) + format->part_encstatus (node->decrypt_success); + + if (node->sig_attempted && format->part_sigstatus) + format->part_sigstatus (node->sig_validity); + + format->part_content (part); + + if (node->envelope_part) { + fputs (format->header_start, stdout); + if (format->header_message_part) + format->header_message_part (GMIME_MESSAGE (node->part)); + fputs (format->header_end, stdout); + + fputs (format->body_start, stdout); + } + + /* Recurse over the children */ state->part_count += 1; + for (i = 0; i < node->nchildren; i++) + show_message_part (mime_node_child (node, i), state, format, i == 0); - if (! (GMIME_IS_PART (part) || GMIME_IS_MULTIPART (part) || GMIME_IS_MESSAGE_PART (part))) { - fprintf (stderr, "Warning: Not displaying unknown mime part: %s.\n", - g_type_name (G_OBJECT_TYPE (part))); - return; - } + /* Finish this part */ + if (node->envelope_part) + fputs (format->body_end, stdout); - selected = (params->part <= 0 || state->part_count == params->part); - - if (selected || state->in_zone) { - if (!first && (params->part <= 0 || state->in_zone)) - fputs (format->part_sep, stdout); - - if (format->part_start) - format->part_start (part, &(state->part_count)); - } - - /* handle PGP/MIME parts */ - if (GMIME_IS_MULTIPART (part) && params->cryptoctx) { - GMimeMultipart *multipart = GMIME_MULTIPART (part); - GError* err = NULL; - - if (GMIME_IS_MULTIPART_ENCRYPTED (part) && params->decrypt) - { - if ( g_mime_multipart_get_count (multipart) != 2 ) { - /* this violates RFC 3156 section 4, so we won't bother with it. */ - fprintf (stderr, - "Error: %d part(s) for a multipart/encrypted message (should be exactly 2)\n", - g_mime_multipart_get_count (multipart)); - } else { - GMimeMultipartEncrypted *encrypteddata = GMIME_MULTIPART_ENCRYPTED (part); - decryptedpart = g_mime_multipart_encrypted_decrypt (encrypteddata, params->cryptoctx, &err); - if (decryptedpart) { - if ((selected || state->in_zone) && format->part_encstatus) - format->part_encstatus (1); - const GMimeSignatureValidity *sigvalidity = g_mime_multipart_encrypted_get_signature_validity (encrypteddata); - if (!sigvalidity) - fprintf (stderr, "Failed to verify signed part: %s\n", (err ? err->message : "no error explanation given")); - if ((selected || state->in_zone) && format->part_sigstatus) - format->part_sigstatus (sigvalidity); - } else { - fprintf (stderr, "Failed to decrypt part: %s\n", (err ? err->message : "no error explanation given")); - if ((selected || state->in_zone) && format->part_encstatus) - format->part_encstatus (0); - } - } - } - else if (GMIME_IS_MULTIPART_SIGNED (part)) - { - if ( g_mime_multipart_get_count (multipart) != 2 ) { - /* this violates RFC 3156 section 5, so we won't bother with it. */ - fprintf (stderr, - "Error: %d part(s) for a multipart/signed message (should be exactly 2)\n", - g_mime_multipart_get_count (multipart)); - } else { - /* For some reason the GMimeSignatureValidity returned - * here is not a const (inconsistent with that - * returned by - * g_mime_multipart_encrypted_get_signature_validity, - * and therefore needs to be properly disposed of. - * Hopefully the API will become more consistent. */ - GMimeSignatureValidity *sigvalidity = g_mime_multipart_signed_verify (GMIME_MULTIPART_SIGNED (part), params->cryptoctx, &err); - if (!sigvalidity) { - fprintf (stderr, "Failed to verify signed part: %s\n", (err ? err->message : "no error explanation given")); - } - if ((selected || state->in_zone) && format->part_sigstatus) - format->part_sigstatus (sigvalidity); - if (sigvalidity) - g_mime_signature_validity_free (sigvalidity); - } - } - - if (err) - g_error_free (err); - } - /* end handle PGP/MIME parts */ - - if (selected || state->in_zone) - format->part_content (part); - - if (GMIME_IS_MULTIPART (part)) { - GMimeMultipart *multipart = GMIME_MULTIPART (part); - int i; - - if (selected) - state->in_zone = 1; - - if (decryptedpart) { - /* We emit the useless application/pgp-encrypted version - * part here only to keep the emitted output as consistent - * as possible between decrypted output and the - * unprocessed multipart/mime. For some strange reason, - * the actual encrypted data is the second part of the - * multipart. */ - show_message_part (g_mime_multipart_get_part (multipart, 0), state, format, params, TRUE); - show_message_part (decryptedpart, state, format, params, FALSE); - } else { - for (i = 0; i < g_mime_multipart_get_count (multipart); i++) { - show_message_part (g_mime_multipart_get_part (multipart, i), - state, format, params, i == 0); - } - } - - if (selected) - state->in_zone = 0; - - } else if (GMIME_IS_MESSAGE_PART (part)) { - GMimeMessage *mime_message = g_mime_message_part_get_message (GMIME_MESSAGE_PART (part)); - - if (selected) - state->in_zone = 1; - - if (selected || (!selected && state->in_zone)) { - fputs (format->header_start, stdout); - if (format->header_message_part) - format->header_message_part (mime_message); - fputs (format->header_end, stdout); - - fputs (format->body_start, stdout); - } - - show_message_part (g_mime_message_get_mime_part (mime_message), - state, format, params, TRUE); - - if (selected || (!selected && state->in_zone)) - fputs (format->body_end, stdout); - - if (selected) - state->in_zone = 0; - } - - if (selected || state->in_zone) { - if (format->part_end) - format->part_end (part); - } + if (format->part_end) + format->part_end (part); } notmuch_status_t @@ -179,49 +79,24 @@ show_message_body (notmuch_message_t *message, const notmuch_show_format_t *format, notmuch_show_params_t *params) { - GMimeStream *stream = NULL; - GMimeParser *parser = NULL; - GMimeMessage *mime_message = NULL; - notmuch_status_t ret = NOTMUCH_STATUS_SUCCESS; - const char *filename = notmuch_message_get_filename (message); - FILE *file = NULL; + notmuch_status_t ret; show_message_state_t state; + mime_node_t *root, *part; - state.part_count = 0; - state.in_zone = 0; + ret = mime_node_open (NULL, message, params->cryptoctx, params->decrypt, + &root); + if (ret) + return ret; - file = fopen (filename, "r"); - if (! file) { - fprintf (stderr, "Error opening %s: %s\n", filename, strerror (errno)); - ret = NOTMUCH_STATUS_FILE_ERROR; - goto DONE; - } + /* The caller of show_message_body has already handled the + * outermost envelope, so skip it. */ + state.part_count = MAX (params->part, 1); - stream = g_mime_stream_file_new (file); - g_mime_stream_file_set_owner (GMIME_STREAM_FILE (stream), FALSE); + part = mime_node_seek_dfs (root, state.part_count); + if (part) + show_message_part (part, &state, format, TRUE); - parser = g_mime_parser_new_with_stream (stream); + talloc_free (root); - mime_message = g_mime_parser_construct_message (parser); - - show_message_part (g_mime_message_get_mime_part (mime_message), - &state, - format, - params, - TRUE); - - DONE: - if (mime_message) - g_object_unref (mime_message); - - if (parser) - g_object_unref (parser); - - if (stream) - g_object_unref (stream); - - if (file) - fclose (file); - - return ret; + return NOTMUCH_STATUS_SUCCESS; } -- 1.7.7.3