[PATCH 6/6] cli: lazily create the crypto gpg context only when needed
Jameson Graef Rollins
jrollins at finestructure.net
Wed May 16 14:55:59 PDT 2012
Move the creation of the crypto ctx into mime-node.c and create it
only when needed. This removes code duplication from notmuch-show and
notmuch-reply, and should speed up these functions considerably if the
crypto flags are provided but the messages don't have any
cryptographic parts.
---
mime-node.c | 25 +++++++++++++++++++++++++
notmuch-reply.c | 19 -------------------
notmuch-show.c | 23 -----------------------
3 files changed, 25 insertions(+), 42 deletions(-)
diff --git a/mime-node.c b/mime-node.c
index 8cdabc8..7278c74 100644
--- a/mime-node.c
+++ b/mime-node.c
@@ -182,6 +182,31 @@ _mime_node_create (mime_node_t *parent, GMimeObject *part)
return NULL;
}
+ /* Lazily create the gpgctx if it's needed and hasn't been initialized yet */
+ if ((GMIME_IS_MULTIPART_ENCRYPTED (part) || GMIME_IS_MULTIPART_SIGNED (part))
+ && (node->ctx->crypto.verify || node->ctx->crypto.decrypt)) {
+ if (!node->ctx->crypto.gpgctx) {
+#ifdef GMIME_ATLEAST_26
+ /* TODO: GMimePasswordRequestFunc */
+ node->ctx->crypto.gpgctx = g_mime_gpg_context_new (NULL, "gpg");
+#else
+ GMimeSession* session = g_object_new (g_mime_session_get_type(), NULL);
+ node->ctx->crypto.gpgctx = g_mime_gpg_context_new (session, "gpg");
+ g_object_unref (session);
+#endif
+ if (node->ctx->crypto.gpgctx) {
+ g_mime_gpg_context_set_always_trust ((GMimeGpgContext*) node->ctx->crypto.gpgctx, FALSE);
+ } else {
+ /* If we fail to create the gpgctx set the verify and
+ * decrypt flags to FALSE so we don't try to do any
+ * further verification or decryption */
+ node->ctx->crypto.verify = FALSE;
+ node->ctx->crypto.decrypt = FALSE;
+ fprintf (stderr, "Failed to construct gpg context.\n");
+ }
+ }
+ }
+
/* Handle PGP/MIME parts */
if (GMIME_IS_MULTIPART_ENCRYPTED (part) && node->ctx->crypto.decrypt) {
if (node->nchildren != 2) {
diff --git a/notmuch-reply.c b/notmuch-reply.c
index 997fdd1..a56cf9f 100644
--- a/notmuch-reply.c
+++ b/notmuch-reply.c
@@ -708,25 +708,6 @@ notmuch_reply_command (void *ctx, int argc, char *argv[])
else
reply_format_func = notmuch_reply_format_default;
- if (crypto.decrypt) {
-#ifdef GMIME_ATLEAST_26
- /* TODO: GMimePasswordRequestFunc */
- crypto.gpgctx = g_mime_gpg_context_new (NULL, "gpg");
-#else
- GMimeSession* session = g_object_new (g_mime_session_get_type(), NULL);
- crypto.gpgctx = g_mime_gpg_context_new (session, "gpg");
-#endif
- if (crypto.gpgctx) {
- g_mime_gpg_context_set_always_trust ((GMimeGpgContext*) crypto.gpgctx, FALSE);
- } else {
- crypto.decrypt = FALSE;
- fprintf (stderr, "Failed to construct gpg context.\n");
- }
-#ifndef GMIME_ATLEAST_26
- g_object_unref (session);
-#endif
- }
-
config = notmuch_config_open (ctx, NULL, NULL);
if (config == NULL)
return 1;
diff --git a/notmuch-show.c b/notmuch-show.c
index 99a10bd..0616f68 100644
--- a/notmuch-show.c
+++ b/notmuch-show.c
@@ -1058,29 +1058,6 @@ notmuch_show_command (void *ctx, unused (int argc), unused (char *argv[]))
break;
}
- if (params.crypto.decrypt || params.crypto.verify) {
-#ifdef GMIME_ATLEAST_26
- /* TODO: GMimePasswordRequestFunc */
- params.crypto.gpgctx = g_mime_gpg_context_new (NULL, "gpg");
-#else
- GMimeSession* session = g_object_new (g_mime_session_get_type(), NULL);
- params.crypto.gpgctx = g_mime_gpg_context_new (session, "gpg");
-#endif
- if (params.crypto.gpgctx) {
- g_mime_gpg_context_set_always_trust ((GMimeGpgContext*) params.crypto.gpgctx, FALSE);
- } else {
- /* If we fail to create the gpgctx set the verify and
- * decrypt flags to FALSE so we don't try to do any
- * further verification or decryption */
- params.crypto.verify = FALSE;
- params.crypto.decrypt = FALSE;
- fprintf (stderr, "Failed to construct gpg context.\n");
- }
-#ifndef GMIME_ATLEAST_26
- g_object_unref (session);
-#endif
- }
-
config = notmuch_config_open (ctx, NULL, NULL);
if (config == NULL)
return 1;
--
1.7.10
More information about the notmuch
mailing list