--- /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 2C4A06DE092C\r
+ for <notmuch@notmuchmail.org>; Fri, 8 Jul 2016 03:15:14 -0700 (PDT)\r
+X-Virus-Scanned: Debian amavisd-new at cworth.org\r
+X-Spam-Flag: NO\r
+X-Spam-Score: 0.018\r
+X-Spam-Level: \r
+X-Spam-Status: No, score=0.018 tagged_above=-999 required=5 tests=[AWL=0.018]\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 VQHSFU_TcCPl for <notmuch@notmuchmail.org>;\r
+ Fri, 8 Jul 2016 03:15:06 -0700 (PDT)\r
+Received: from che.mayfirst.org (che.mayfirst.org [162.247.75.118])\r
+ by arlo.cworth.org (Postfix) with ESMTP id 6771C6DE092D\r
+ for <notmuch@notmuchmail.org>; Fri, 8 Jul 2016 03:13:18 -0700 (PDT)\r
+Received: from fifthhorseman.net (unknown [88.128.80.54])\r
+ by che.mayfirst.org (Postfix) with ESMTPSA id 1844AF98B\r
+ for <notmuch@notmuchmail.org>; Fri, 8 Jul 2016 06:13:17 -0400 (EDT)\r
+Received: by fifthhorseman.net (Postfix, from userid 1000)\r
+ id B857D214EB; 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 11/16] add --try-decrypt to notmuch insert\r
+Date: Fri, 8 Jul 2016 11:27:22 +0200\r
+Message-Id: <1467970047-8013-12-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:15:14 -0000\r
+\r
+allow an incoming message to be delivered while indexing the\r
+cleartext.\r
+\r
+This requires the secret keys for the message to be available. For\r
+the moment, the most functional approach is to ensure that gpg-agent\r
+is running and knows about any secret keys that might be useful to\r
+decrypt incoming mail.\r
+\r
+Any additional recommendations for how to phrase the caveat for this\r
+option are welcome.\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-insert.rst | 11 +++++++++++\r
+ notmuch-insert.c | 32 +++++++++++++++++++++++++++++---\r
+ 3 files changed, 41 insertions(+), 4 deletions(-)\r
+\r
+diff --git a/completion/notmuch-completion.bash b/completion/notmuch-completion.bash\r
+index 78047b5..1e4b2cc 100644\r
+--- a/completion/notmuch-completion.bash\r
++++ b/completion/notmuch-completion.bash\r
+@@ -224,7 +224,7 @@ _notmuch_insert()\r
+ ! $split &&\r
+ case "${cur}" in\r
+ --*)\r
+- local options="--create-folder --folder= --keep --no-hooks ${_notmuch_shared_options}"\r
++ local options="--create-folder --folder= --keep --no-hooks --try-decrypt ${_notmuch_shared_options}"\r
+ compopt -o nospace\r
+ COMPREPLY=( $(compgen -W "$options" -- ${cur}) )\r
+ return\r
+diff --git a/doc/man1/notmuch-insert.rst b/doc/man1/notmuch-insert.rst\r
+index 2c9c0d0..9c76b30 100644\r
+--- a/doc/man1/notmuch-insert.rst\r
++++ b/doc/man1/notmuch-insert.rst\r
+@@ -50,6 +50,17 @@ Supported options for **insert** include\r
+ ``--no-hooks``\r
+ Prevent hooks from being run.\r
+ \r
++ ``--try-decrypt``\r
++\r
++ If the message is encrypted, try to decrypt the message while\r
++ indexing. If decryption is successful, index the cleartext\r
++ itself. The message is stored to disk in its original form\r
++ (ciphertext). 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
+ EXIT STATUS\r
+ ===========\r
+ \r
+diff --git a/notmuch-insert.c b/notmuch-insert.c\r
+index 131f09e..eec0eb5 100644\r
+--- a/notmuch-insert.c\r
++++ b/notmuch-insert.c\r
+@@ -378,12 +378,13 @@ FAIL:\r
+ */\r
+ static notmuch_status_t\r
+ add_file (notmuch_database_t *notmuch, const char *path, tag_op_list_t *tag_ops,\r
+- notmuch_bool_t synchronize_flags, notmuch_bool_t keep)\r
++ notmuch_bool_t synchronize_flags, notmuch_bool_t keep,\r
++ notmuch_indexopts_t *indexopts)\r
+ {\r
+ notmuch_message_t *message;\r
+ notmuch_status_t status;\r
+ \r
+- status = notmuch_database_add_message (notmuch, path, &message);\r
++ status = notmuch_database_add_message_with_indexopts (notmuch, path, indexopts, &message);\r
+ if (status == NOTMUCH_STATUS_SUCCESS) {\r
+ status = tag_op_list_apply (message, tag_ops, 0);\r
+ if (status) {\r
+@@ -455,17 +456,20 @@ notmuch_insert_command (notmuch_config_t *config, int argc, char *argv[])\r
+ notmuch_bool_t create_folder = FALSE;\r
+ notmuch_bool_t keep = FALSE;\r
+ notmuch_bool_t no_hooks = FALSE;\r
++ notmuch_bool_t try_decrypt = FALSE;\r
+ notmuch_bool_t synchronize_flags;\r
+ const char *maildir;\r
+ char *newpath;\r
+ int opt_index;\r
+ unsigned int i;\r
++ notmuch_indexopts_t *indexopts;\r
+ \r
+ notmuch_opt_desc_t options[] = {\r
+ { NOTMUCH_OPT_STRING, &folder, "folder", 0, 0 },\r
+ { NOTMUCH_OPT_BOOLEAN, &create_folder, "create-folder", 0, 0 },\r
+ { NOTMUCH_OPT_BOOLEAN, &keep, "keep", 0, 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
+ { NOTMUCH_OPT_END, 0, 0, 0, 0 }\r
+ };\r
+@@ -545,8 +549,29 @@ notmuch_insert_command (notmuch_config_t *config, int argc, char *argv[])\r
+ return EXIT_FAILURE;\r
+ }\r
+ \r
++ indexopts = notmuch_indexopts_create ();\r
++ if (!indexopts) {\r
++ fprintf (stderr, "Error: could not create index options.\n");\r
++ return EXIT_FAILURE;\r
++ }\r
++ status = notmuch_indexopts_set_try_decrypt (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 (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 (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
+ /* Index the message. */\r
+- status = add_file (notmuch, newpath, tag_ops, synchronize_flags, keep);\r
++ status = add_file (notmuch, newpath, tag_ops, synchronize_flags, keep, indexopts);\r
+ \r
+ /* Commit changes. */\r
+ close_status = notmuch_database_destroy (notmuch);\r
+@@ -577,5 +602,6 @@ notmuch_insert_command (notmuch_config_t *config, int argc, char *argv[])\r
+ notmuch_run_hook (db_path, "post-insert");\r
+ }\r
+ \r
++ notmuch_indexopts_destroy (indexopts);\r
+ return status ? EXIT_FAILURE : EXIT_SUCCESS;\r
+ }\r
+-- \r
+2.8.1\r
+\r