--- /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 0605D6DE1AD2\r
+ for <notmuch@notmuchmail.org>; Sun, 31 Jan 2016 12:40:23 -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 96omg29d9WUE for <notmuch@notmuchmail.org>;\r
+ Sun, 31 Jan 2016 12:40:21 -0800 (PST)\r
+Received: from che.mayfirst.org (che.mayfirst.org [209.234.253.108])\r
+ by arlo.cworth.org (Postfix) with ESMTP id B1CF06DE1AD8\r
+ for <notmuch@notmuchmail.org>; Sun, 31 Jan 2016 12:40:09 -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 DCB90F9A1\r
+ for <notmuch@notmuchmail.org>; Sun, 31 Jan 2016 15:40:06 -0500 (EST)\r
+Received: by fifthhorseman.net (Postfix, from userid 1000)\r
+ id 57AA9210B5; 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 12/16] add --try-decrypt to notmuch new\r
+Date: Sun, 31 Jan 2016 15:39:57 -0500\r
+Message-Id: <1454272801-23623-13-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:23 -0000\r
+\r
+Try to decrypt any encrypted parts of newly-discovered messages while\r
+indexing them. The cleartext of any successfully-decrypted messages\r
+will be indexed, with tags applied in the same form as from notmuch\r
+insert --try-decrypt.\r
+\r
+If ~/.notmuch-config contains crypto.gpg_path, and gpg is needed for\r
+indexing, the configuration option will be used to find gpg.\r
+---\r
+ completion/notmuch-completion.bash | 2 +-\r
+ doc/man1/notmuch-new.rst | 10 ++++++++++\r
+ notmuch-new.c | 30 +++++++++++++++++++++++++++++-\r
+ 3 files changed, 40 insertions(+), 2 deletions(-)\r
+\r
+diff --git a/completion/notmuch-completion.bash b/completion/notmuch-completion.bash\r
+index 4bc9040..214f776 100644\r
+--- a/completion/notmuch-completion.bash\r
++++ b/completion/notmuch-completion.bash\r
+@@ -247,7 +247,7 @@ _notmuch_new()\r
+ \r
+ case "${cur}" in\r
+ -*)\r
+- local options="--no-hooks --quiet ${_notmuch_shared_options}"\r
++ local options="--no-hooks --try-decrypt --quiet ${_notmuch_shared_options}"\r
+ compopt -o nospace\r
+ COMPREPLY=( $(compgen -W "${options}" -- ${cur}) )\r
+ ;;\r
+diff --git a/doc/man1/notmuch-new.rst b/doc/man1/notmuch-new.rst\r
+index 787ed78..cf08021 100644\r
+--- a/doc/man1/notmuch-new.rst\r
++++ b/doc/man1/notmuch-new.rst\r
+@@ -43,6 +43,16 @@ Supported options for **new** include\r
+ ``--quiet``\r
+ Do not print progress or results.\r
+ \r
++ ``--try-decrypt``\r
++\r
++ For each message, if it is encrypted, try to decrypt it while\r
++ indexing. If decryption is successful, index the cleartext\r
++ itself. Be aware that the index is likely sufficient to\r
++ reconstruct the cleartext of the message itself, so please\r
++ ensure that the notmuch message index is adequately\r
++ protected. DO NOT USE THIS FLAG without considering the\r
++ security of your index.\r
++\r
+ SEE ALSO\r
+ ========\r
+ \r
+diff --git a/notmuch-new.c b/notmuch-new.c\r
+index e503776..3d5efd5 100644\r
+--- a/notmuch-new.c\r
++++ b/notmuch-new.c\r
+@@ -49,6 +49,7 @@ typedef struct {\r
+ size_t new_tags_length;\r
+ const char **new_ignore;\r
+ size_t new_ignore_length;\r
++ notmuch_indexopts_t *indexopts;\r
+ \r
+ int total_files;\r
+ int processed_files;\r
+@@ -260,7 +261,8 @@ add_file (notmuch_database_t *notmuch, const char *filename,\r
+ if (status)\r
+ goto DONE;\r
+ \r
+- status = notmuch_database_add_message (notmuch, filename, &message);\r
++ status = notmuch_database_add_message_with_indexopts (notmuch, filename,\r
++ state->indexopts, &message);\r
+ switch (status) {\r
+ /* Success. */\r
+ case NOTMUCH_STATUS_SUCCESS:\r
+@@ -929,6 +931,7 @@ notmuch_new_command (notmuch_config_t *config, int argc, char *argv[])\r
+ add_files_state_t add_files_state = {\r
+ .verbosity = VERBOSITY_NORMAL,\r
+ .debug = FALSE,\r
++ .indexopts = NULL,\r
+ .output_is_a_tty = isatty (fileno (stdout)),\r
+ };\r
+ struct timeval tv_start;\r
+@@ -942,6 +945,7 @@ notmuch_new_command (notmuch_config_t *config, int argc, char *argv[])\r
+ unsigned int i;\r
+ notmuch_bool_t timer_is_active = FALSE;\r
+ notmuch_bool_t no_hooks = FALSE;\r
++ notmuch_bool_t try_decrypt = FALSE;\r
+ notmuch_bool_t quiet = FALSE, verbose = FALSE;\r
+ notmuch_status_t status;\r
+ \r
+@@ -950,6 +954,7 @@ notmuch_new_command (notmuch_config_t *config, int argc, char *argv[])\r
+ { NOTMUCH_OPT_BOOLEAN, &verbose, "verbose", 'v', 0 },\r
+ { NOTMUCH_OPT_BOOLEAN, &add_files_state.debug, "debug", 'd', 0 },\r
+ { NOTMUCH_OPT_BOOLEAN, &no_hooks, "no-hooks", 'n', 0 },\r
++ { NOTMUCH_OPT_BOOLEAN, &try_decrypt, "try-decrypt", 0, 0 },\r
+ { NOTMUCH_OPT_INHERIT, (void *) ¬much_shared_options, NULL, 0, 0 },\r
+ { 0, 0, 0, 0, 0 }\r
+ };\r
+@@ -1067,6 +1072,28 @@ notmuch_new_command (notmuch_config_t *config, int argc, char *argv[])\r
+ if (notmuch == NULL)\r
+ return EXIT_FAILURE;\r
+ \r
++ add_files_state.indexopts = notmuch_indexopts_create ();\r
++ if (!add_files_state.indexopts) {\r
++ fprintf (stderr, "Error: could not create index options.\n");\r
++ return EXIT_FAILURE;\r
++ }\r
++ status = notmuch_indexopts_set_try_decrypt (add_files_state.indexopts, try_decrypt);\r
++ if (status != NOTMUCH_STATUS_SUCCESS) {\r
++ fprintf (stderr, "Error: Failed to set try_decrypt to %s. (%s)\n",\r
++ try_decrypt ? "True" : "False", notmuch_status_to_string (status));\r
++ notmuch_indexopts_destroy (add_files_state.indexopts);\r
++ return EXIT_FAILURE;\r
++ }\r
++ if (try_decrypt) {\r
++ const char* gpg_path = notmuch_config_get_crypto_gpg_path (config);\r
++ status = notmuch_indexopts_set_gpg_path (add_files_state.indexopts, gpg_path);\r
++ if (status)\r
++ fprintf (stderr, "Warning: failed to set database gpg_path to '%s' (%s)\n",\r
++ gpg_path ? gpg_path : "(NULL)",\r
++ notmuch_status_to_string (status));\r
++ }\r
++\r
++ \r
+ /* Set up our handler for SIGINT. We do this after having\r
+ * potentially done a database upgrade we this interrupt handler\r
+ * won't support. */\r
+@@ -1150,5 +1177,6 @@ notmuch_new_command (notmuch_config_t *config, int argc, char *argv[])\r
+ if (!no_hooks && !ret && !interrupted)\r
+ ret = notmuch_run_hook (db_path, "post-new");\r
+ \r
++ notmuch_indexopts_destroy (add_files_state.indexopts);\r
+ return ret || interrupted ? EXIT_FAILURE : EXIT_SUCCESS;\r
+ }\r
+-- \r
+2.7.0.rc3\r
+\r