--- /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 800D16DE1781\r
+ for <notmuch@notmuchmail.org>; Sun, 31 Jan 2016 12:40:12 -0800 (PST)\r
+X-Virus-Scanned: Debian amavisd-new at cworth.org\r
+X-Spam-Flag: NO\r
+X-Spam-Score: 0\r
+X-Spam-Level: \r
+X-Spam-Status: No, score=0 tagged_above=-999 required=5 tests=[none]\r
+ 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 qgCMc9u7yGpG for <notmuch@notmuchmail.org>;\r
+ Sun, 31 Jan 2016 12:40:10 -0800 (PST)\r
+Received: from che.mayfirst.org (che.mayfirst.org [209.234.253.108])\r
+ by arlo.cworth.org (Postfix) with ESMTP id E20866DE0022\r
+ for <notmuch@notmuchmail.org>; Sun, 31 Jan 2016 12:40:08 -0800 (PST)\r
+Received: from fifthhorseman.net (ip-64-134-185-108.public.wayport.net\r
+ [64.134.185.108])\r
+ by che.mayfirst.org (Postfix) with ESMTPSA id 77452F991\r
+ for <notmuch@notmuchmail.org>; Sun, 31 Jan 2016 15:40:05 -0500 (EST)\r
+Received: by fifthhorseman.net (Postfix, from userid 1000)\r
+ id 38F1720544; Sun, 31 Jan 2016 15:40:06 -0500 (EST)\r
+From: Daniel Kahn Gillmor <dkg@fifthhorseman.net>\r
+To: Notmuch Mail <notmuch@notmuchmail.org>\r
+Subject: [PATCH v3 04/16] Provide _notmuch_crypto_{set,get}_gpg_path\r
+Date: Sun, 31 Jan 2016 15:39:49 -0500\r
+Message-Id: <1454272801-23623-5-git-send-email-dkg@fifthhorseman.net>\r
+X-Mailer: git-send-email 2.7.0.rc3\r
+In-Reply-To: <1454272801-23623-1-git-send-email-dkg@fifthhorseman.net>\r
+References: <1454272801-23623-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: Sun, 31 Jan 2016 20:40:12 -0000\r
+\r
+Use functions to access the gpg_path for a _notmuch_crypto_t object.\r
+This lets us return sensible defaults based on the state of the user's\r
+machine.\r
+---\r
+ notmuch-reply.c | 13 ++++++++++---\r
+ notmuch-show.c | 12 ++++++++++--\r
+ util/crypto.c | 48 +++++++++++++++++++++++++++++++++++++++++++++++-\r
+ util/crypto.h | 8 +++++++-\r
+ 4 files changed, 74 insertions(+), 7 deletions(-)\r
+\r
+diff --git a/notmuch-reply.c b/notmuch-reply.c\r
+index eccfb32..793e6f9 100644\r
+--- a/notmuch-reply.c\r
++++ b/notmuch-reply.c\r
+@@ -790,13 +790,15 @@ notmuch_reply_command (notmuch_config_t *config, int argc, char *argv[])\r
+ .crypto = {\r
+ .verify = FALSE,\r
+ .decrypt = FALSE,\r
+- .gpgpath = NULL\r
++ .gpg_path = NULL\r
+ }\r
+ };\r
+ int format = FORMAT_DEFAULT;\r
+ int reply_all = TRUE;\r
+ struct sprinter *sp = NULL;\r
+-\r
++ notmuch_status_t status;\r
++ const char *gpg_path = NULL;\r
++ \r
+ notmuch_opt_desc_t options[] = {\r
+ { NOTMUCH_OPT_KEYWORD, &format, "format", 'f',\r
+ (notmuch_keyword_t []){ { "default", FORMAT_DEFAULT },\r
+@@ -845,7 +847,12 @@ notmuch_reply_command (notmuch_config_t *config, int argc, char *argv[])\r
+ return EXIT_FAILURE;\r
+ }\r
+ \r
+- params.crypto.gpgpath = notmuch_config_get_crypto_gpg_path (config);\r
++ gpg_path = notmuch_config_get_crypto_gpg_path (config);\r
++ status = _notmuch_crypto_set_gpg_path (&(params.crypto), gpg_path);\r
++ if (status != NOTMUCH_STATUS_SUCCESS) {\r
++ fprintf (stderr, "Error: could not set gpg_path to '%s'.\n", gpg_path);\r
++ return EXIT_FAILURE;\r
++ }\r
+ \r
+ if (notmuch_database_open (notmuch_config_get_database_path (config),\r
+ NOTMUCH_DATABASE_MODE_READ_ONLY, ¬much))\r
+diff --git a/notmuch-show.c b/notmuch-show.c\r
+index 3c91ece..096fd49 100644\r
+--- a/notmuch-show.c\r
++++ b/notmuch-show.c\r
+@@ -1006,13 +1006,15 @@ notmuch_show_command (notmuch_config_t *config, int argc, char *argv[])\r
+ .crypto = {\r
+ .verify = FALSE,\r
+ .decrypt = FALSE,\r
+- .gpgpath = NULL\r
++ .gpg_path = NULL\r
+ },\r
+ .include_html = FALSE\r
+ };\r
+ int format_sel = NOTMUCH_FORMAT_NOT_SPECIFIED;\r
+ int exclude = EXCLUDE_TRUE;\r
+ int entire_thread = ENTIRE_THREAD_DEFAULT;\r
++ notmuch_status_t status;\r
++ const char *gpg_path = NULL;\r
+ \r
+ notmuch_opt_desc_t options[] = {\r
+ { NOTMUCH_OPT_KEYWORD, &format_sel, "format", 'f',\r
+@@ -1130,7 +1132,13 @@ notmuch_show_command (notmuch_config_t *config, int argc, char *argv[])\r
+ return EXIT_FAILURE;\r
+ }\r
+ \r
+- params.crypto.gpgpath = notmuch_config_get_crypto_gpg_path (config);\r
++\r
++ gpg_path = notmuch_config_get_crypto_gpg_path (config);\r
++ status = _notmuch_crypto_set_gpg_path (&(params.crypto), gpg_path);\r
++ if (status != NOTMUCH_STATUS_SUCCESS) {\r
++ fprintf (stderr, "Error: could not set gpg_path to '%s'.\n", gpg_path);\r
++ return EXIT_FAILURE;\r
++ }\r
+ \r
+ if (notmuch_database_open (notmuch_config_get_database_path (config),\r
+ NOTMUCH_DATABASE_MODE_READ_ONLY, ¬much))\r
+diff --git a/util/crypto.c b/util/crypto.c\r
+index 151a352..c89ce8a 100644\r
+--- a/util/crypto.c\r
++++ b/util/crypto.c\r
+@@ -21,7 +21,9 @@\r
+ \r
+ #include "notmuch.h"\r
+ #include "crypto.h"\r
++#include "search-path.h"\r
+ #include <string.h>\r
++#include <talloc.h>\r
+ \r
+ #define ARRAY_SIZE(arr) (sizeof (arr) / sizeof (arr[0]))\r
+ \r
+@@ -38,7 +40,7 @@ get_gpg_context (_notmuch_crypto_t *crypto, GMimeCryptoContext **ctx)\r
+ }\r
+ \r
+ /* TODO: GMimePasswordRequestFunc */\r
+- crypto->gpgctx = g_mime_gpg_context_new (NULL, crypto->gpgpath ? crypto->gpgpath : "gpg");\r
++ crypto->gpgctx = g_mime_gpg_context_new (NULL, _notmuch_crypto_get_gpg_path(crypto));\r
+ if (! crypto->gpgctx) {\r
+ return NOTMUCH_STATUS_FAILED_CRYPTO_CONTEXT_CREATION;\r
+ }\r
+@@ -120,6 +122,47 @@ _notmuch_crypto_get_gmime_ctx_for_protocol (_notmuch_crypto_t *crypto,\r
+ return NOTMUCH_STATUS_UNKNOWN_CRYPTO_PROTOCOL;\r
+ }\r
+ \r
++const char*\r
++_notmuch_crypto_get_gpg_path (const _notmuch_crypto_t *crypto)\r
++{\r
++ if (crypto->gpg_path)\r
++ return crypto->gpg_path;\r
++\r
++#define try_gpg_path(z) if (test_for_executable(z)) return z\r
++ try_gpg_path("gpg2");\r
++ try_gpg_path("gpg");\r
++#undef try_gpg_path\r
++ return NULL;\r
++}\r
++\r
++notmuch_status_t\r
++_notmuch_crypto_set_gpg_path (_notmuch_crypto_t *crypto, const char* gpg_path)\r
++{\r
++ /* return success if this matches what is already configured */\r
++ if ((!gpg_path && !crypto->gpg_path) ||\r
++ (gpg_path && crypto->gpg_path && 0 == strcmp(gpg_path, crypto->gpg_path)))\r
++ return NOTMUCH_STATUS_SUCCESS;\r
++\r
++ if (!gpg_path && !test_for_executable(gpg_path))\r
++ return NOTMUCH_STATUS_FILE_ERROR;\r
++\r
++ /* clear any existing gpgctx, since things are changing */\r
++ if (crypto->gpgctx) {\r
++ g_object_unref (crypto->gpgctx);\r
++ crypto->gpgctx = NULL;\r
++ }\r
++\r
++ if (crypto->gpg_path) {\r
++ talloc_free(crypto->gpg_path);\r
++ crypto->gpg_path = NULL;\r
++ }\r
++\r
++ if (gpg_path)\r
++ crypto->gpg_path = talloc_strdup (NULL, gpg_path);\r
++\r
++ return NOTMUCH_STATUS_SUCCESS;\r
++}\r
++\r
+ void\r
+ _notmuch_crypto_cleanup (_notmuch_crypto_t *crypto)\r
+ {\r
+@@ -132,4 +175,7 @@ _notmuch_crypto_cleanup (_notmuch_crypto_t *crypto)\r
+ g_object_unref (crypto->pkcs7ctx);\r
+ crypto->pkcs7ctx = NULL;\r
+ }\r
++\r
++ talloc_free (crypto->gpg_path);\r
++ crypto->gpg_path = NULL;\r
+ }\r
+diff --git a/util/crypto.h b/util/crypto.h\r
+index 7cb0a39..70fc8ef 100644\r
+--- a/util/crypto.h\r
++++ b/util/crypto.h\r
+@@ -11,7 +11,7 @@ typedef struct _notmuch_crypto {\r
+ GMimeCryptoContext* pkcs7ctx;\r
+ notmuch_bool_t verify;\r
+ notmuch_bool_t decrypt;\r
+- const char *gpgpath;\r
++ char *gpg_path;\r
+ } _notmuch_crypto_t;\r
+ \r
+ \r
+@@ -20,6 +20,12 @@ _notmuch_crypto_get_gmime_ctx_for_protocol (_notmuch_crypto_t *crypto,\r
+ const char *protocol,\r
+ GMimeCryptoContext **ctx);\r
+ \r
++notmuch_status_t\r
++_notmuch_crypto_set_gpg_path (_notmuch_crypto_t *crypto, const char *gpg_path);\r
++\r
++const char *\r
++_notmuch_crypto_get_gpg_path (const _notmuch_crypto_t *crypto);\r
++\r
+ void\r
+ _notmuch_crypto_cleanup (_notmuch_crypto_t *crypto);\r
+ \r
+-- \r
+2.7.0.rc3\r
+\r