--- /dev/null
+Return-Path: <jrollins@finestructure.net>\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 50D31431FBD\r
+ for <notmuch@notmuchmail.org>; Wed, 23 May 2012 15:40:55 -0700 (PDT)\r
+X-Virus-Scanned: Debian amavisd-new at olra.theworths.org\r
+X-Spam-Flag: NO\r
+X-Spam-Score: -2.3\r
+X-Spam-Level: \r
+X-Spam-Status: No, score=-2.3 tagged_above=-999 required=5\r
+ tests=[RCVD_IN_DNSWL_MED=-2.3] 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 T+k5HAexTW9k for <notmuch@notmuchmail.org>;\r
+ Wed, 23 May 2012 15:40:53 -0700 (PDT)\r
+Received: from outgoing-mail.its.caltech.edu (outgoing-mail.its.caltech.edu\r
+ [131.215.239.19])\r
+ by olra.theworths.org (Postfix) with ESMTP id 7E3A6431FC3\r
+ for <notmuch@notmuchmail.org>; Wed, 23 May 2012 15:40:50 -0700 (PDT)\r
+Received: from fire-doxen.imss.caltech.edu (localhost [127.0.0.1])\r
+ by fire-doxen-postvirus (Postfix) with ESMTP id 9E6B6328081\r
+ for <notmuch@notmuchmail.org>; Wed, 23 May 2012 15:40:49 -0700 (PDT)\r
+X-Spam-Scanned: at Caltech-IMSS on fire-doxen by amavisd-new\r
+Received: from finestructure.net (unknown [137.151.175.56])\r
+ (Authenticated sender: jrollins)\r
+ by fire-doxen-submit (Postfix) with ESMTP id 8644432807B\r
+ for <notmuch@notmuchmail.org>; Wed, 23 May 2012 15:40:47 -0700 (PDT)\r
+Received: by finestructure.net (Postfix, from userid 1000)\r
+ id 0D1691F4; Wed, 23 May 2012 15:40:46 -0700 (PDT)\r
+From: Jameson Graef Rollins <jrollins@finestructure.net>\r
+To: Notmuch Mail <notmuch@notmuchmail.org>\r
+Subject: [PATCH v4 7/7] cli: use new notmuch_crypto_get_context in mime-node.c\r
+Date: Wed, 23 May 2012 15:40:43 -0700\r
+Message-Id: <1337812843-14986-8-git-send-email-jrollins@finestructure.net>\r
+X-Mailer: git-send-email 1.7.10\r
+In-Reply-To: <1337812843-14986-7-git-send-email-jrollins@finestructure.net>\r
+References: <1337812843-14986-1-git-send-email-jrollins@finestructure.net>\r
+ <1337812843-14986-2-git-send-email-jrollins@finestructure.net>\r
+ <1337812843-14986-3-git-send-email-jrollins@finestructure.net>\r
+ <1337812843-14986-4-git-send-email-jrollins@finestructure.net>\r
+ <1337812843-14986-5-git-send-email-jrollins@finestructure.net>\r
+ <1337812843-14986-6-git-send-email-jrollins@finestructure.net>\r
+ <1337812843-14986-7-git-send-email-jrollins@finestructure.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: Wed, 23 May 2012 22:40:55 -0000\r
+\r
+This has the affect of lazily creating the crypto contexts only when\r
+needed. This removes code duplication from notmuch-show and\r
+notmuch-reply, and should speed up these functions considerably if the\r
+crypto flags are provided but the messages don't have any\r
+cryptographic parts.\r
+---\r
+ mime-node.c | 20 ++++++++++++++------\r
+ notmuch-client.h | 3 ++-\r
+ notmuch-reply.c | 19 -------------------\r
+ notmuch-show.c | 23 -----------------------\r
+ 4 files changed, 16 insertions(+), 49 deletions(-)\r
+\r
+diff --git a/mime-node.c b/mime-node.c\r
+index 73e28c5..857c84c 100644\r
+--- a/mime-node.c\r
++++ b/mime-node.c\r
+@@ -150,6 +150,7 @@ _mime_node_create (mime_node_t *parent, GMimeObject *part)\r
+ {\r
+ mime_node_t *node = talloc_zero (parent, mime_node_t);\r
+ GError *err = NULL;\r
++ GMimeCryptoContext *cryptoctx = NULL;\r
+ \r
+ /* Set basic node properties */\r
+ node->part = part;\r
+@@ -182,8 +183,15 @@ _mime_node_create (mime_node_t *parent, GMimeObject *part)\r
+ return NULL;\r
+ }\r
+ \r
++ if ((GMIME_IS_MULTIPART_ENCRYPTED (part) && node->ctx->crypto->decrypt)\r
++ || (GMIME_IS_MULTIPART_SIGNED (part) && node->ctx->crypto->verify)) {\r
++ GMimeContentType *content_type = g_mime_object_get_content_type (part);\r
++ const char *protocol = g_mime_content_type_get_parameter (content_type, "protocol");\r
++ cryptoctx = notmuch_crypto_get_context (node->ctx->crypto, protocol);\r
++ }\r
++\r
+ /* Handle PGP/MIME parts */\r
+- if (GMIME_IS_MULTIPART_ENCRYPTED (part) && node->ctx->crypto->decrypt) {\r
++ if (GMIME_IS_MULTIPART_ENCRYPTED (part) && node->ctx->crypto->decrypt && cryptoctx) {\r
+ if (node->nchildren != 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
+@@ -196,10 +204,10 @@ _mime_node_create (mime_node_t *parent, GMimeObject *part)\r
+ #ifdef GMIME_ATLEAST_26\r
+ GMimeDecryptResult *decrypt_result = NULL;\r
+ node->decrypted_child = g_mime_multipart_encrypted_decrypt\r
+- (encrypteddata, node->ctx->crypto->gpgctx, &decrypt_result, &err);\r
++ (encrypteddata, cryptoctx, &decrypt_result, &err);\r
+ #else\r
+ node->decrypted_child = g_mime_multipart_encrypted_decrypt\r
+- (encrypteddata, node->ctx->crypto->gpgctx, &err);\r
++ (encrypteddata, cryptoctx, &err);\r
+ #endif\r
+ if (node->decrypted_child) {\r
+ node->decrypt_success = node->verify_attempted = TRUE;\r
+@@ -217,7 +225,7 @@ _mime_node_create (mime_node_t *parent, GMimeObject *part)\r
+ (err ? err->message : "no error explanation given"));\r
+ }\r
+ }\r
+- } else if (GMIME_IS_MULTIPART_SIGNED (part) && node->ctx->crypto->verify) {\r
++ } else if (GMIME_IS_MULTIPART_SIGNED (part) && node->ctx->crypto->verify && cryptoctx) {\r
+ if (node->nchildren != 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
+@@ -226,7 +234,7 @@ _mime_node_create (mime_node_t *parent, GMimeObject *part)\r
+ } else {\r
+ #ifdef GMIME_ATLEAST_26\r
+ node->sig_list = g_mime_multipart_signed_verify\r
+- (GMIME_MULTIPART_SIGNED (part), node->ctx->crypto->gpgctx, &err);\r
++ (GMIME_MULTIPART_SIGNED (part), cryptoctx, &err);\r
+ node->verify_attempted = TRUE;\r
+ \r
+ if (!node->sig_list)\r
+@@ -242,7 +250,7 @@ _mime_node_create (mime_node_t *parent, GMimeObject *part)\r
+ * In GMime 2.6, they're both non-const, so we'll be able\r
+ * to clean up this asymmetry. */\r
+ GMimeSignatureValidity *sig_validity = g_mime_multipart_signed_verify\r
+- (GMIME_MULTIPART_SIGNED (part), node->ctx->crypto->gpgctx, &err);\r
++ (GMIME_MULTIPART_SIGNED (part), cryptoctx, &err);\r
+ node->verify_attempted = TRUE;\r
+ node->sig_validity = sig_validity;\r
+ if (sig_validity) {\r
+diff --git a/notmuch-client.h b/notmuch-client.h\r
+index c501186..243b49c 100644\r
+--- a/notmuch-client.h\r
++++ b/notmuch-client.h\r
+@@ -352,7 +352,8 @@ struct mime_node {\r
+ /* Construct a new MIME node pointing to the root message part of\r
+ * message. If crypto->verify is true, signed child parts will be\r
+ * verified. If crypto->decrypt is true, encrypted child parts will be\r
+- * decrypted.\r
++ * decrypted. If crypto->gpgctx is NULL, it will be lazily\r
++ * initialized.\r
+ *\r
+ * Return value:\r
+ *\r
+diff --git a/notmuch-reply.c b/notmuch-reply.c\r
+index e4f293f..aecd173 100644\r
+--- a/notmuch-reply.c\r
++++ b/notmuch-reply.c\r
+@@ -710,25 +710,6 @@ notmuch_reply_command (void *ctx, int argc, char *argv[])\r
+ else\r
+ reply_format_func = notmuch_reply_format_default;\r
+ \r
+- if (params.crypto.decrypt) {\r
+-#ifdef GMIME_ATLEAST_26\r
+- /* TODO: GMimePasswordRequestFunc */\r
+- params.crypto.gpgctx = g_mime_gpg_context_new (NULL, "gpg");\r
+-#else\r
+- GMimeSession* session = g_object_new (g_mime_session_get_type(), NULL);\r
+- params.crypto.gpgctx = g_mime_gpg_context_new (session, "gpg");\r
+-#endif\r
+- if (params.crypto.gpgctx) {\r
+- g_mime_gpg_context_set_always_trust ((GMimeGpgContext*) params.crypto.gpgctx, FALSE);\r
+- } else {\r
+- params.crypto.decrypt = FALSE;\r
+- fprintf (stderr, "Failed to construct gpg context.\n");\r
+- }\r
+-#ifndef GMIME_ATLEAST_26\r
+- g_object_unref (session);\r
+-#endif\r
+- }\r
+-\r
+ config = notmuch_config_open (ctx, NULL, NULL);\r
+ if (config == NULL)\r
+ return 1;\r
+diff --git a/notmuch-show.c b/notmuch-show.c\r
+index 3c06792..8247f1d 100644\r
+--- a/notmuch-show.c\r
++++ b/notmuch-show.c\r
+@@ -1056,29 +1056,6 @@ notmuch_show_command (void *ctx, unused (int argc), unused (char *argv[]))\r
+ break;\r
+ }\r
+ \r
+- if (params.crypto.decrypt || params.crypto.verify) {\r
+-#ifdef GMIME_ATLEAST_26\r
+- /* TODO: GMimePasswordRequestFunc */\r
+- params.crypto.gpgctx = g_mime_gpg_context_new (NULL, "gpg");\r
+-#else\r
+- GMimeSession* session = g_object_new (g_mime_session_get_type(), NULL);\r
+- params.crypto.gpgctx = g_mime_gpg_context_new (session, "gpg");\r
+-#endif\r
+- if (params.crypto.gpgctx) {\r
+- g_mime_gpg_context_set_always_trust ((GMimeGpgContext*) params.crypto.gpgctx, FALSE);\r
+- } else {\r
+- /* If we fail to create the gpgctx set the verify and\r
+- * decrypt flags to FALSE so we don't try to do any\r
+- * further verification or decryption */\r
+- params.crypto.verify = FALSE;\r
+- params.crypto.decrypt = FALSE;\r
+- fprintf (stderr, "Failed to construct gpg context.\n");\r
+- }\r
+-#ifndef GMIME_ATLEAST_26\r
+- g_object_unref (session);\r
+-#endif\r
+- }\r
+-\r
+ config = notmuch_config_open (ctx, NULL, NULL);\r
+ if (config == NULL)\r
+ return 1;\r
+-- \r
+1.7.10\r
+\r