--- /dev/null
+Return-Path: <dkg@fifthhorseman.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 arlo.cworth.org (Postfix) with ESMTP id 897B06DE1902\r
+ for <notmuch@notmuchmail.org>; Wed, 9 Dec 2015 19:40:07 -0800 (PST)\r
+X-Virus-Scanned: Debian amavisd-new at cworth.org\r
+X-Spam-Flag: NO\r
+X-Spam-Score: -0.036\r
+X-Spam-Level: \r
+X-Spam-Status: No, score=-0.036 tagged_above=-999 required=5\r
+ tests=[AWL=-0.036] autolearn=disabled\r
+Received: from arlo.cworth.org ([127.0.0.1])\r
+ by localhost (arlo.cworth.org [127.0.0.1]) (amavisd-new, port 10024)\r
+ with ESMTP id hYFM1wP90l94 for <notmuch@notmuchmail.org>;\r
+ Wed, 9 Dec 2015 19:40:05 -0800 (PST)\r
+Received: from che.mayfirst.org (che.mayfirst.org [209.234.253.108])\r
+ by arlo.cworth.org (Postfix) with ESMTP id DC33B6DE0FF1\r
+ for <notmuch@notmuchmail.org>; Wed, 9 Dec 2015 19:40:04 -0800 (PST)\r
+Received: from fifthhorseman.net (unknown [38.109.115.130])\r
+ by che.mayfirst.org (Postfix) with ESMTPSA id EA284F984\r
+ for <notmuch@notmuchmail.org>; Wed, 9 Dec 2015 22:40:03 -0500 (EST)\r
+Received: by fifthhorseman.net (Postfix, from userid 1000)\r
+ id A7A5220C13; Wed, 9 Dec 2015 22:40:03 -0500 (EST)\r
+From: Daniel Kahn Gillmor <dkg@fifthhorseman.net>\r
+To: Notmuch Mail <notmuch@notmuchmail.org>\r
+Subject: [PATCH 2/9] Add a lazily-initialized crypto context to\r
+ notmuch_database_t\r
+Date: Wed, 9 Dec 2015 22:39:39 -0500\r
+Message-Id: <1449718786-28000-3-git-send-email-dkg@fifthhorseman.net>\r
+X-Mailer: git-send-email 2.6.2\r
+In-Reply-To: <1449718786-28000-1-git-send-email-dkg@fifthhorseman.net>\r
+References: <1449718786-28000-1-git-send-email-dkg@fifthhorseman.net>\r
+X-BeenThere: notmuch@notmuchmail.org\r
+X-Mailman-Version: 2.1.20\r
+Precedence: list\r
+List-Id: "Use and development of the notmuch mail system."\r
+ <notmuch.notmuchmail.org>\r
+List-Unsubscribe: <https://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: <https://notmuchmail.org/mailman/listinfo/notmuch>,\r
+ <mailto:notmuch-request@notmuchmail.org?subject=subscribe>\r
+X-List-Received-Date: Thu, 10 Dec 2015 03:40:08 -0000\r
+\r
+This is in large part a duplicate of parts of crypto.c, but that code\r
+is in the client (outside the library), and we don't want to entangle\r
+the libgmime API with the libnotmuch API.\r
+\r
+I welcome better proposals for how to share this code explicitly\r
+between the library and the client.\r
+---\r
+ lib/database-private.h | 1 +\r
+ lib/database.cc | 42 ++++++++++++++++++++++++++++++++++++++++++\r
+ lib/notmuch-private.h | 8 ++++++++\r
+ 3 files changed, 51 insertions(+)\r
+\r
+diff --git a/lib/database-private.h b/lib/database-private.h\r
+index 3fb10f7..1bf76c5 100644\r
+--- a/lib/database-private.h\r
++++ b/lib/database-private.h\r
+@@ -171,6 +171,7 @@ struct _notmuch_database {\r
+ * notmuch_database_new_revision. */\r
+ unsigned long revision;\r
+ const char *uuid;\r
++ GMimeCryptoContext *gpg_crypto_ctx;\r
+ \r
+ Xapian::QueryParser *query_parser;\r
+ Xapian::TermGenerator *term_gen;\r
+diff --git a/lib/database.cc b/lib/database.cc\r
+index 3b342f1..13b0bad 100644\r
+--- a/lib/database.cc\r
++++ b/lib/database.cc\r
+@@ -995,6 +995,8 @@ notmuch_database_open_verbose (const char *path,\r
+ notmuch->uuid = talloc_strdup (\r
+ notmuch, notmuch->xapian_db->get_uuid ().c_str ());\r
+ \r
++ notmuch->gpg_crypto_ctx = NULL;\r
++ \r
+ notmuch->query_parser = new Xapian::QueryParser;\r
+ notmuch->term_gen = new Xapian::TermGenerator;\r
+ notmuch->term_gen->set_stemmer (Xapian::Stem ("english"));\r
+@@ -1090,6 +1092,11 @@ notmuch_database_close (notmuch_database_t *notmuch)\r
+ delete notmuch->last_mod_range_processor;\r
+ notmuch->last_mod_range_processor = NULL;\r
+ \r
++ if (notmuch->gpg_crypto_ctx) {\r
++ g_object_unref (notmuch->gpg_crypto_ctx);\r
++ notmuch->gpg_crypto_ctx = NULL;\r
++ }\r
++ \r
+ return status;\r
+ }\r
+ \r
+@@ -2386,6 +2393,41 @@ _notmuch_database_link_message (notmuch_database_t *notmuch,\r
+ return status;\r
+ }\r
+ \r
++notmuch_private_status_t\r
++_notmuch_database_get_crypto_for_protocol (notmuch_database_t *notmuch,\r
++ const char *protocol,\r
++ GMimeCryptoContext **crypto_ctx)\r
++{\r
++ if (! protocol)\r
++ return NOTMUCH_PRIVATE_STATUS_MALFORMED_CRYPTO_PROTOCOL;\r
++ \r
++ /* As per RFC 1847 section 2.1: "the [protocol] value token is\r
++ * comprised of the type and sub-type tokens of the Content-Type".\r
++ * As per RFC 1521 section 2: "Content-Type values, subtypes, and\r
++ * parameter names as defined in this document are\r
++ * case-insensitive." Thus, we use strcasecmp for the protocol.\r
++ */\r
++ if (strcasecmp (protocol, "application/pgp-signature") == 0 ||\r
++ strcasecmp (protocol, "application/pgp-encrypted") == 0) {\r
++ if (! notmuch->gpg_crypto_ctx) {\r
++ /* FIXME: how do we allow for configuring the gpg binary\r
++ * here? how would this config get into the library? Is\r
++ * this an option we can set on the database object? Or\r
++ * elsewhere? */\r
++ notmuch->gpg_crypto_ctx = g_mime_gpg_context_new (NULL, "gpg");\r
++ if (! notmuch->gpg_crypto_ctx)\r
++ return NOTMUCH_PRIVATE_STATUS_FAILED_CRYPTO_CONTEXT_CREATION;\r
++\r
++ g_mime_gpg_context_set_use_agent ((GMimeGpgContext *) notmuch->gpg_crypto_ctx, TRUE);\r
++ g_mime_gpg_context_set_always_trust ((GMimeGpgContext *) notmuch->gpg_crypto_ctx, FALSE);\r
++ }\r
++ *crypto_ctx = notmuch->gpg_crypto_ctx;\r
++ return NOTMUCH_PRIVATE_STATUS_SUCCESS;\r
++ } else {\r
++ return NOTMUCH_PRIVATE_STATUS_UNKNOWN_CRYPTO_PROTOCOL;\r
++ }\r
++}\r
++\r
+ notmuch_status_t\r
+ notmuch_database_add_message (notmuch_database_t *notmuch,\r
+ const char *filename,\r
+diff --git a/lib/notmuch-private.h b/lib/notmuch-private.h\r
+index 5dd4770..f6fd36a 100644\r
+--- a/lib/notmuch-private.h\r
++++ b/lib/notmuch-private.h\r
+@@ -141,6 +141,9 @@ typedef enum _notmuch_private_status {\r
+ /* Then add our own private values. */\r
+ NOTMUCH_PRIVATE_STATUS_TERM_TOO_LONG = NOTMUCH_STATUS_LAST_STATUS,\r
+ NOTMUCH_PRIVATE_STATUS_NO_DOCUMENT_FOUND,\r
++ NOTMUCH_PRIVATE_STATUS_MALFORMED_CRYPTO_PROTOCOL,\r
++ NOTMUCH_PRIVATE_STATUS_FAILED_CRYPTO_CONTEXT_CREATION,\r
++ NOTMUCH_PRIVATE_STATUS_UNKNOWN_CRYPTO_PROTOCOL,\r
+ \r
+ NOTMUCH_PRIVATE_STATUS_LAST_STATUS\r
+ } notmuch_private_status_t;\r
+@@ -239,6 +242,11 @@ _notmuch_database_filename_to_direntry (void *ctx,\r
+ notmuch_find_flags_t flags,\r
+ char **direntry);\r
+ \r
++notmuch_private_status_t\r
++_notmuch_database_get_crypto_for_protocol (notmuch_database_t *notmuch,\r
++ const char *protocol,\r
++ GMimeCryptoContext **crypto_ctx);\r
++\r
+ /* directory.cc */\r
+ \r
+ notmuch_directory_t *\r
+-- \r
+2.6.2\r
+\r