Re: [PATCH 2/4] Introduce a generic tree-like abstraction for MIME traversal.
authorJani Nikula <jani@nikula.org>
Tue, 29 Nov 2011 19:11:49 +0000 (21:11 +0200)
committerW. Trevor King <wking@tremily.us>
Fri, 7 Nov 2014 17:40:30 +0000 (09:40 -0800)
e3/4dd8c5ec97bf27c7c2efe2dd24c778eb9e9bf3 [new file with mode: 0644]

diff --git a/e3/4dd8c5ec97bf27c7c2efe2dd24c778eb9e9bf3 b/e3/4dd8c5ec97bf27c7c2efe2dd24c778eb9e9bf3
new file mode 100644 (file)
index 0000000..79d4bde
--- /dev/null
@@ -0,0 +1,454 @@
+Return-Path: <jani@nikula.org>\r
+X-Original-To: notmuch@notmuchmail.org\r
+Delivered-To: notmuch@notmuchmail.org\r
+Received: from localhost (localhost [127.0.0.1])\r
+       by olra.theworths.org (Postfix) with ESMTP id 63CC9429E25\r
+       for <notmuch@notmuchmail.org>; Tue, 29 Nov 2011 11:12:00 -0800 (PST)\r
+X-Virus-Scanned: Debian amavisd-new at olra.theworths.org\r
+X-Spam-Flag: NO\r
+X-Spam-Score: -0.7\r
+X-Spam-Level: \r
+X-Spam-Status: No, score=-0.7 tagged_above=-999 required=5\r
+       tests=[RCVD_IN_DNSWL_LOW=-0.7] autolearn=disabled\r
+Received: from olra.theworths.org ([127.0.0.1])\r
+       by localhost (olra.theworths.org [127.0.0.1]) (amavisd-new, port 10024)\r
+       with ESMTP id wTtF7DwC5tuP for <notmuch@notmuchmail.org>;\r
+       Tue, 29 Nov 2011 11:11:59 -0800 (PST)\r
+Received: from mail-bw0-f53.google.com (mail-bw0-f53.google.com\r
+       [209.85.214.53]) (using TLSv1 with cipher RC4-SHA (128/128 bits))\r
+       (No client certificate requested)\r
+       by olra.theworths.org (Postfix) with ESMTPS id 9F3AE431FB6\r
+       for <notmuch@notmuchmail.org>; Tue, 29 Nov 2011 11:11:58 -0800 (PST)\r
+Received: by bkaq10 with SMTP id q10so11429066bka.26\r
+       for <notmuch@notmuchmail.org>; Tue, 29 Nov 2011 11:11:55 -0800 (PST)\r
+Received: by 10.204.133.197 with SMTP id g5mr50160418bkt.43.1322593915613;\r
+       Tue, 29 Nov 2011 11:11:55 -0800 (PST)\r
+Received: from localhost (dsl-hkibrasgw4-fe5cdc00-23.dhcp.inet.fi.\r
+       [80.220.92.23])\r
+       by mx.google.com with ESMTPS id iu9sm5294958bkc.0.2011.11.29.11.11.52\r
+       (version=SSLv3 cipher=OTHER); Tue, 29 Nov 2011 11:11:53 -0800 (PST)\r
+From: Jani Nikula <jani@nikula.org>\r
+To: Austin Clements <amdragon@MIT.EDU>, notmuch@notmuchmail.org\r
+Subject: Re: [PATCH 2/4] Introduce a generic tree-like abstraction for MIME\r
+       traversal.\r
+In-Reply-To: <1322446871-14986-3-git-send-email-amdragon@mit.edu>\r
+References: <1322446871-14986-1-git-send-email-amdragon@mit.edu>\r
+       <1322446871-14986-3-git-send-email-amdragon@mit.edu>\r
+User-Agent: Notmuch/0.10+51~gef3ae74 (http://notmuchmail.org) Emacs/23.3.1\r
+       (i686-pc-linux-gnu)\r
+Date: Tue, 29 Nov 2011 21:11:49 +0200\r
+Message-ID: <8739d6u4ju.fsf@nikula.org>\r
+MIME-Version: 1.0\r
+Content-Type: text/plain; charset=utf-8\r
+Content-Transfer-Encoding: quoted-printable\r
+Cc: dkg@fifthhorseman.net\r
+X-BeenThere: notmuch@notmuchmail.org\r
+X-Mailman-Version: 2.1.13\r
+Precedence: list\r
+List-Id: "Use and development of the notmuch mail system."\r
+       <notmuch.notmuchmail.org>\r
+List-Unsubscribe: <http://notmuchmail.org/mailman/options/notmuch>,\r
+       <mailto:notmuch-request@notmuchmail.org?subject=unsubscribe>\r
+List-Archive: <http://notmuchmail.org/pipermail/notmuch>\r
+List-Post: <mailto:notmuch@notmuchmail.org>\r
+List-Help: <mailto:notmuch-request@notmuchmail.org?subject=help>\r
+List-Subscribe: <http://notmuchmail.org/mailman/listinfo/notmuch>,\r
+       <mailto:notmuch-request@notmuchmail.org?subject=subscribe>\r
+X-List-Received-Date: Tue, 29 Nov 2011 19:12:00 -0000\r
+\r
+\r
+Hi, generally looks good to me, but please find a few comments below.\r
+\r
+BR,\r
+Jani.\r
+\r
+On Sun, 27 Nov 2011 21:21:09 -0500, Austin Clements <amdragon@MIT.EDU> wrot=\r
+e:\r
+> This wraps all of the complex MIME part handling in a single, simple\r
+> function that gets part N from *any* MIME object, so traversing a MIME\r
+> part tree becomes a two-line for loop.  Furthermore, the MIME node\r
+> structure provides easy access to envelopes for message parts as well\r
+> as cryptographic information.\r
+>=20\r
+> This code is directly derived from the current show_message_body code\r
+> (much of it is identical), but the control relation is inverted:\r
+> instead of show_message_body controlling the traversal of the MIME\r
+> structure and invoking callbacks, the caller controls the traversal of\r
+> the MIME structure.\r
+> ---\r
+>  Makefile.local   |    1 +\r
+>  mime-node.c      |  234 ++++++++++++++++++++++++++++++++++++++++++++++++=\r
+++++++\r
+>  notmuch-client.h |   80 ++++++++++++++++++\r
+>  3 files changed, 315 insertions(+), 0 deletions(-)\r
+>  create mode 100644 mime-node.c\r
+>=20\r
+> diff --git a/Makefile.local b/Makefile.local\r
+> index c94402b..c46ed26 100644\r
+> --- a/Makefile.local\r
+> +++ b/Makefile.local\r
+> @@ -312,6 +312,7 @@ notmuch_client_srcs =3D          \\r
+>      notmuch-time.c          \\r
+>      query-string.c          \\r
+>      show-message.c          \\r
+> +    mime-node.c             \\r
+>      json.c\r
+>=20=20\r
+>  notmuch_client_modules =3D $(notmuch_client_srcs:.c=3D.o)\r
+> diff --git a/mime-node.c b/mime-node.c\r
+> new file mode 100644\r
+> index 0000000..942738b\r
+> --- /dev/null\r
+> +++ b/mime-node.c\r
+> @@ -0,0 +1,234 @@\r
+> +/* notmuch - Not much of an email program, (just index and search)\r
+> + *\r
+> + * Copyright =C2=A9 2009 Carl Worth\r
+> + * Copyright =C2=A9 2009 Keith Packard\r
+> + *\r
+> + * This program is free software: you can redistribute it and/or modify\r
+> + * it under the terms of the GNU General Public License as published by\r
+> + * the Free Software Foundation, either version 3 of the License, or\r
+> + * (at your option) any later version.\r
+> + *\r
+> + * This program is distributed in the hope that it will be useful,\r
+> + * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
+> + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
+> + * GNU General Public License for more details.\r
+> + *\r
+> + * You should have received a copy of the GNU General Public License\r
+> + * along with this program.  If not, see http://www.gnu.org/licenses/ .\r
+> + *\r
+> + * Authors: Carl Worth <cworth@cworth.org>\r
+> + *          Keith Packard <keithp@keithp.com>\r
+> + *          Austin Clements <aclements@csail.mit.edu>\r
+> + */\r
+> +\r
+> +#include "notmuch-client.h"\r
+> +\r
+> +/* Context that gets inherited from the root node. */\r
+> +typedef struct mime_node_context {\r
+> +    /* Per-message resources.  These are allocated internally and must\r
+> +     * be destroyed. */\r
+> +    FILE *file;\r
+> +    GMimeStream *stream;\r
+> +    GMimeParser *parser;\r
+> +    GMimeMessage *mime_message;\r
+> +=20=20=20=20\r
+\r
+Leftover indentation spaces above.\r
+\r
+> +    /* Context provided by the caller. */\r
+> +    GMimeCipherContext *cryptoctx;\r
+> +    notmuch_bool_t decrypt;\r
+> +} mime_node_context_t;\r
+> +\r
+> +static int\r
+> +_mime_node_context_free (mime_node_context_t *res)\r
+> +{\r
+> +    if (res->mime_message)\r
+> +    g_object_unref (res->mime_message);\r
+> +\r
+> +    if (res->parser)\r
+> +    g_object_unref (res->parser);\r
+> +\r
+> +    if (res->stream)\r
+> +    g_object_unref (res->stream);\r
+> +\r
+> +    if (res->file)\r
+> +    fclose (res->file);\r
+> +\r
+> +    return 0;\r
+> +}\r
+> +\r
+> +notmuch_status_t\r
+> +mime_node_open (const void *ctx, notmuch_message_t *message,\r
+> +            GMimeCipherContext* cryptoctx, notmuch_bool_t decrypt,\r
+\r
+The style here seems to be * next to the variable name, not type.\r
+\r
+> +            mime_node_t **root_out)\r
+> +{\r
+> +    const char *filename =3D notmuch_message_get_filename (message);\r
+> +    mime_node_context_t *mctx;\r
+> +    mime_node_t *root =3D NULL;\r
+\r
+No need to initialize as it's initialized right away below.\r
+\r
+> +    notmuch_status_t status;\r
+> +\r
+> +    root =3D talloc_zero (ctx, mime_node_t);\r
+> +    if (root =3D=3D NULL) {\r
+> +    fprintf (stderr, "Out of memory.\n");\r
+> +    status =3D NOTMUCH_STATUS_OUT_OF_MEMORY;\r
+> +    goto DONE;\r
+> +    }\r
+> +\r
+> +    /* Create the tree-wide context */\r
+> +    mctx =3D talloc_zero (root, mime_node_context_t);\r
+> +    if (mctx =3D=3D NULL) {\r
+> +    fprintf (stderr, "Out of memory.\n");\r
+> +    status =3D NOTMUCH_STATUS_OUT_OF_MEMORY;\r
+> +    goto DONE;\r
+> +    }\r
+> +    talloc_set_destructor (mctx, _mime_node_context_free);\r
+> +\r
+> +    mctx->file =3D fopen (filename, "r");\r
+> +    if (! mctx->file) {\r
+> +    fprintf (stderr, "Error opening %s: %s\n", filename, strerror (errno));\r
+> +    status =3D NOTMUCH_STATUS_FILE_ERROR;\r
+> +    goto DONE;\r
+> +    }\r
+> +\r
+> +    mctx->stream =3D g_mime_stream_file_new (mctx->file);\r
+\r
+AFAICT the GMimeStreamFile object owns the FILE * pointer now, and\r
+closes it later. Calling fclose() on it in _mime_node_context_free()\r
+would be undefined behaviour. But please don't just take my word for it.\r
+\r
+> +    g_mime_stream_file_set_owner (GMIME_STREAM_FILE (mctx->stream), FALS=\r
+E);\r
+> +\r
+> +    mctx->parser =3D g_mime_parser_new_with_stream (mctx->stream);\r
+> +\r
+> +    mctx->mime_message =3D g_mime_parser_construct_message (mctx->parser=\r
+);\r
+> +\r
+> +    mctx->cryptoctx =3D cryptoctx;\r
+> +    mctx->decrypt =3D decrypt;\r
+> +\r
+> +    /* Create the root node */\r
+> +    root->part =3D GMIME_OBJECT (mctx->mime_message);\r
+> +    root->envelope_file =3D message;\r
+> +    root->children =3D 1;\r
+> +    root->ctx =3D mctx;\r
+> +\r
+> +    *root_out =3D root;\r
+> +    return NOTMUCH_STATUS_SUCCESS;\r
+> +\r
+> +DONE:\r
+> +    talloc_free (root);\r
+> +    return status;\r
+> +}\r
+> +\r
+> +static int\r
+> +_signature_validity_free (GMimeSignatureValidity **proxy)\r
+> +{\r
+> +    g_mime_signature_validity_free (*proxy);\r
+> +    return 0;\r
+> +}\r
+> +\r
+> +static mime_node_t *\r
+> +_mime_node_create (const mime_node_t *parent, GMimeObject *part)\r
+> +{\r
+> +    mime_node_t *out =3D talloc_zero (parent, mime_node_t);\r
+> +    GError *err =3D NULL;\r
+> +\r
+> +    /* Set basic node properties */\r
+> +    out->part =3D part;\r
+> +    out->ctx =3D parent->ctx;\r
+> +    if (!talloc_reference (out, out->ctx)) {\r
+> +    fprintf (stderr, "Out of memory.\n");\r
+> +    talloc_free (out);\r
+> +    return NULL;\r
+> +    }\r
+> +\r
+> +    /* Deal with the different types of parts */\r
+> +    if (GMIME_IS_PART (part)) {\r
+> +    out->children =3D 0;\r
+> +    } else if (GMIME_IS_MULTIPART (part)) {\r
+> +    out->children =3D g_mime_multipart_get_count (GMIME_MULTIPART (part));\r
+> +    } else if (GMIME_IS_MESSAGE_PART (part)) {\r
+> +    /* Promote part to an envelope and open it */\r
+> +    GMimeMessagePart *message_part =3D GMIME_MESSAGE_PART (part);\r
+> +    GMimeMessage *message =3D g_mime_message_part_get_message (message_part=\r
+);\r
+> +    out->envelope_part =3D message_part;\r
+> +    out->part =3D GMIME_OBJECT (message);\r
+> +    out->children =3D 1;\r
+> +    } else {\r
+> +    fprintf (stderr, "Warning: Unknown mime part type: %s.\n",\r
+> +             g_type_name (G_OBJECT_TYPE (part)));\r
+> +    talloc_free (out);\r
+> +    return NULL;\r
+> +    }\r
+> +\r
+> +    /* Handle PGP/MIME parts */\r
+> +    if (GMIME_IS_MULTIPART_ENCRYPTED (part) && out->ctx->decrypt) {\r
+> +    if (out->children !=3D 2) {\r
+> +        /* this violates RFC 3156 section 4, so we won't bother with it. */\r
+> +        fprintf (stderr, "Error: %d part(s) for a multipart/encrypted "\r
+> +                 "message (should be exactly 2)\n",\r
+> +                 out->children);\r
+> +    } else {\r
+> +        out->is_encrypted =3D TRUE;\r
+> +        GMimeMultipartEncrypted *encrypteddata =3D\r
+> +            GMIME_MULTIPART_ENCRYPTED (part);\r
+> +        out->decrypted_child =3D g_mime_multipart_encrypted_decrypt\r
+> +            (encrypteddata, out->ctx->cryptoctx, &err);\r
+> +        if (out->decrypted_child) {\r
+> +            out->decrypt_success =3D TRUE;\r
+> +            out->is_signed =3D TRUE;\r
+> +            out->sig_validity =3D g_mime_multipart_encrypted_get_signature_validit=\r
+y (encrypteddata);\r
+> +        } else {\r
+> +            fprintf (stderr, "Failed to decrypt part: %s\n",\r
+> +                     (err ? err->message : "no error explanation given"));\r
+> +        }\r
+> +    }\r
+> +    } else if (GMIME_IS_MULTIPART_SIGNED (part) && out->ctx->cryptoctx) {\r
+> +    if (out->children !=3D 2) {\r
+> +        /* this violates RFC 3156 section 5, so we won't bother with it. */\r
+> +        fprintf (stderr, "Error: %d part(s) for a multipart/signed message "\r
+> +                 "(should be exactly 2)\n",\r
+> +                 out->children);\r
+> +    } else {\r
+> +        out->is_signed =3D TRUE;\r
+> +        /* For some reason the GMimeSignatureValidity returned\r
+> +         * here is not a const (inconsistent with that\r
+> +         * returned by\r
+> +         * g_mime_multipart_encrypted_get_signature_validity,\r
+> +         * and therefore needs to be properly disposed of.\r
+> +         * Hopefully the API will become more consistent. */\r
+> +        GMimeSignatureValidity *sig_validity =3D g_mime_multipart_signed_ve=\r
+rify\r
+> +            (GMIME_MULTIPART_SIGNED (part), out->ctx->cryptoctx, &err);\r
+> +        out->sig_validity =3D sig_validity;\r
+> +        if (sig_validity) {\r
+> +            GMimeSignatureValidity **proxy =3D\r
+> +                talloc (out, GMimeSignatureValidity *);\r
+> +            *proxy =3D sig_validity;\r
+> +            talloc_set_destructor (proxy, _signature_validity_free);\r
+> +        }\r
+> +    }\r
+> +    }\r
+> +\r
+> +    if (out->is_signed && !out->sig_validity)\r
+> +    fprintf (stderr, "Failed to verify signed part: %s\n",\r
+> +             (err ? err->message : "no error explanation given"));\r
+> +\r
+> +    if (err)\r
+> +    g_error_free (err);\r
+> +\r
+> +    return out;\r
+> +}\r
+> +\r
+> +mime_node_t *\r
+> +mime_node_child (const mime_node_t *parent, int child)\r
+> +{\r
+> +    if (!parent || child < 0 || child >=3D parent->children)\r
+> +    return NULL;\r
+> +\r
+> +    if (GMIME_IS_MULTIPART (parent->part)) {\r
+> +    GMimeMultipart *multipart =3D GMIME_MULTIPART (parent->part);\r
+> +    if (child =3D=3D 1 && parent->decrypted_child)\r
+> +        return _mime_node_create (parent, parent->decrypted_child);\r
+> +    return _mime_node_create (parent, g_mime_multipart_get_part (multipart,=\r
+ child));\r
+> +    } else if (GMIME_IS_MESSAGE (parent->part)) {\r
+> +    GMimeMessage *message =3D GMIME_MESSAGE (parent->part);\r
+> +    GMimeObject *child =3D g_mime_message_get_mime_part (message);\r
+> +    return _mime_node_create (parent, child);\r
+> +    } else {\r
+> +    /* This should have been caught by message_part_create */\r
+> +    INTERNAL_ERROR ("Unexpected GMimeObject type: %s",\r
+> +                    g_type_name (G_OBJECT_TYPE (parent->part)));\r
+> +    }\r
+> +}\r
+> diff --git a/notmuch-client.h b/notmuch-client.h\r
+> index d7fb6ee..58bd21c 100644\r
+> --- a/notmuch-client.h\r
+> +++ b/notmuch-client.h\r
+> @@ -238,4 +238,84 @@ notmuch_config_set_maildir_synchronize_flags (notmuc=\r
+h_config_t *config,\r
+>  notmuch_bool_t\r
+>  debugger_is_active (void);\r
+>=20=20\r
+> +/* mime-node.c */\r
+> +\r
+> +/* mime_node_t represents a single node in a MIME tree.  A MIME tree\r
+> + * abstracts the different ways of traversing different types of MIME\r
+> + * parts, allowing a MIME message to be viewed as a generic tree of\r
+> + * parts.  Message-type parts have one child, multipart-type parts\r
+> + * have multiple children, and leaf parts have zero children.\r
+> + */\r
+> +typedef struct mime_node {\r
+> +    /* The MIME object of this part.  This will be a GMimeMessage,\r
+> +     * GMimePart, GMimeMultipart, or a subclass of one of these.\r
+> +     *\r
+> +     * This will never be a GMimeMessagePart because GMimeMessagePart\r
+> +     * is structurally redundant with GMimeMessage.  If this part is a\r
+> +     * message (that is, 'part' is a GMimeMessage), then either\r
+> +     * envelope_file will be set to a notmuch_message_t (for top-level\r
+> +     * messages) or envelope_part will be set to a GMimeMessagePart\r
+> +     * (for embedded message parts).\r
+> +     */\r
+> +    GMimeObject *part;\r
+> +\r
+> +    /* If part is a GMimeMessage, these record the envelope of the\r
+> +     * message: either a notmuch_message_t representing a top-level\r
+> +     * message, or a GMimeMessagePart representing a MIME part\r
+> +     * containing a message.\r
+> +     */\r
+> +    notmuch_message_t *envelope_file;\r
+> +    GMimeMessagePart *envelope_part;\r
+> +\r
+> +    /* The number of children of this part. */\r
+> +    int children;\r
+> +\r
+> +    /* True if this is the container for an encrypted or signed part.\r
+> +     * This does *not* mean that decryption or signature verification\r
+> +     * succeeded. */\r
+> +    notmuch_bool_t is_encrypted, is_signed;\r
+> +    /* True if decryption of this part's child succeeded.  In this\r
+> +     * case, the decrypted part is substituted for the second child of\r
+> +     * this part (which would usually be the encrypted data). */\r
+> +    notmuch_bool_t decrypt_success;\r
+> +    /* For signed or encrypted containers, the validity of the\r
+> +     * signature.  May be NULL if signature verification failed. */\r
+> +    const GMimeSignatureValidity *sig_validity;\r
+> +\r
+> +    /* Internal: Context inherited from the root iterator. */\r
+> +    struct mime_node_context *ctx;\r
+> +\r
+> +    /* Internal: For successfully decrypted multipart parts, the\r
+> +     * decrypted part to substitute for the second child. */\r
+> +    GMimeObject *decrypted_child;\r
+> +} mime_node_t;\r
+> +\r
+> +/* Construct a new MIME node pointing to the root message part of\r
+> + * message.  If cryptoctx is non-NULL, it will be used to verify\r
+> + * signatures on any child parts.  If decrypt is true, then cryptoctx\r
+> + * will additionally be used to decrypt any encrypted child parts.\r
+> + *\r
+> + * Return value:\r
+> + *\r
+> + * NOTMUCH_STATUS_SUCCESS: Root node is returned in *node_out.\r
+> + *\r
+> + * NOTMUCH_STATUS_FILE_ERROR: Failed to open message file.\r
+> + *\r
+> + * NOTMUCH_STATUS_OUT_OF_MEMORY: Out of memory.\r
+> + */\r
+> +notmuch_status_t\r
+> +mime_node_open (const void *ctx, notmuch_message_t *message,\r
+> +            GMimeCipherContext* cryptoctx, notmuch_bool_t decrypt,\r
+> +            mime_node_t **node_out);\r
+> +\r
+> +/* Return a new MIME node for the requested child part of parent.\r
+> + * parent will be used as the talloc context for the returned child\r
+> + * node.\r
+> + *\r
+> + * In case of any failure, this function returns NULL, (after printing\r
+> + * an error message on stderr).\r
+> + */\r
+> +mime_node_t *\r
+> +mime_node_child (const mime_node_t *parent, int child);\r
+> +\r
+>  #endif\r
+> --=20\r
+> 1.7.5.4\r
+>=20\r
+> _______________________________________________\r
+> notmuch mailing list\r
+> notmuch@notmuchmail.org\r
+> http://notmuchmail.org/mailman/listinfo/notmuch\r