--- /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 6F9926DE0A63\r
+ for <notmuch@notmuchmail.org>; Fri, 8 Jul 2016 03:14:16 -0700 (PDT)\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 tests=[AWL=0.036]\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 ttruo4f05ncQ for <notmuch@notmuchmail.org>;\r
+ Fri, 8 Jul 2016 03:14:08 -0700 (PDT)\r
+Received: from che.mayfirst.org (che.mayfirst.org [162.247.75.118])\r
+ by arlo.cworth.org (Postfix) with ESMTP id 09D376DE091C\r
+ for <notmuch@notmuchmail.org>; Fri, 8 Jul 2016 03:13:16 -0700 (PDT)\r
+Received: from fifthhorseman.net (unknown [88.128.80.54])\r
+ by che.mayfirst.org (Postfix) with ESMTPSA id 488CCF98B\r
+ for <notmuch@notmuchmail.org>; Fri, 8 Jul 2016 06:13:15 -0400 (EDT)\r
+Received: by fifthhorseman.net (Postfix, from userid 1000)\r
+ id BCBFE21575; Fri, 8 Jul 2016 11:27:34 +0200 (CEST)\r
+From: Daniel Kahn Gillmor <dkg@fifthhorseman.net>\r
+To: Notmuch Mail <notmuch@notmuchmail.org>\r
+Subject: [PATCH v4 12/16] add --try-decrypt to notmuch new\r
+Date: Fri, 8 Jul 2016 11:27:23 +0200\r
+Message-Id: <1467970047-8013-13-git-send-email-dkg@fifthhorseman.net>\r
+X-Mailer: git-send-email 2.8.1\r
+In-Reply-To: <1467970047-8013-1-git-send-email-dkg@fifthhorseman.net>\r
+References: <1467970047-8013-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: Fri, 08 Jul 2016 10:14:16 -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 1e4b2cc..a6a5a60 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 c55dea7..e495557 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
+@@ -930,6 +932,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
+@@ -943,6 +946,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
+@@ -951,6 +955,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
+@@ -1068,6 +1073,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
+@@ -1151,5 +1178,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.8.1\r
+\r