+ return 0;
+}
+
+enum {
+ FORMAT_DEFAULT,
+ FORMAT_HEADERS_ONLY,
+};
+
+int
+notmuch_reply_command (void *ctx, int argc, char *argv[])
+{
+ notmuch_config_t *config;
+ notmuch_database_t *notmuch;
+ notmuch_query_t *query;
+ char *query_string;
+ int opt_index, ret = 0;
+ int (*reply_format_func)(void *ctx, notmuch_config_t *config, notmuch_query_t *query, notmuch_show_params_t *params, notmuch_bool_t reply_all);
+ notmuch_show_params_t params = { .part = -1 };
+ int format = FORMAT_DEFAULT;
+ int reply_all = TRUE;
+
+ notmuch_opt_desc_t options[] = {
+ { NOTMUCH_OPT_KEYWORD, &format, "format", 'f',
+ (notmuch_keyword_t []){ { "default", FORMAT_DEFAULT },
+ { "headers-only", FORMAT_HEADERS_ONLY },
+ { 0, 0 } } },
+ { NOTMUCH_OPT_KEYWORD, &reply_all, "reply-to", 'r',
+ (notmuch_keyword_t []){ { "all", TRUE },
+ { "sender", FALSE },
+ { 0, 0 } } },
+ { NOTMUCH_OPT_BOOLEAN, ¶ms.decrypt, "decrypt", 'd', 0 },
+ { 0, 0, 0, 0, 0 }
+ };
+
+ opt_index = parse_arguments (argc, argv, options, 1);
+ if (opt_index < 0) {
+ /* diagnostics already printed */
+ return 1;
+ }
+
+ if (format == FORMAT_HEADERS_ONLY)
+ reply_format_func = notmuch_reply_format_headers_only;
+ else
+ reply_format_func = notmuch_reply_format_default;
+
+ if (params.decrypt) {
+#ifdef GMIME_ATLEAST_26
+ /* TODO: GMimePasswordRequestFunc */
+ params.cryptoctx = g_mime_gpg_context_new (NULL, "gpg");
+#else
+ GMimeSession* session = g_object_new (g_mime_session_get_type(), NULL);
+ params.cryptoctx = g_mime_gpg_context_new (session, "gpg");
+#endif
+ if (params.cryptoctx) {
+ g_mime_gpg_context_set_always_trust ((GMimeGpgContext*) params.cryptoctx, FALSE);
+ } else {
+ params.decrypt = FALSE;
+ fprintf (stderr, "Failed to construct gpg context.\n");
+ }
+#ifndef GMIME_ATLEAST_26
+ g_object_unref (session);
+#endif
+ }
+
+ config = notmuch_config_open (ctx, NULL, NULL);
+ if (config == NULL)
+ return 1;
+
+ query_string = query_string_from_args (ctx, argc-opt_index, argv+opt_index);
+ if (query_string == NULL) {
+ fprintf (stderr, "Out of memory\n");
+ return 1;
+ }
+
+ if (*query_string == '\0') {
+ fprintf (stderr, "Error: notmuch reply requires at least one search term.\n");
+ return 1;
+ }
+
+ notmuch = notmuch_database_open (notmuch_config_get_database_path (config),
+ NOTMUCH_DATABASE_MODE_READ_ONLY);
+ if (notmuch == NULL)
+ return 1;
+
+ query = notmuch_query_create (notmuch, query_string);
+ if (query == NULL) {
+ fprintf (stderr, "Out of memory\n");
+ return 1;
+ }
+
+ if (reply_format_func (ctx, config, query, ¶ms, reply_all) != 0)
+ return 1;