+ return 0;
+}
+
+int
+notmuch_reply_command (void *ctx, int argc, char *argv[])
+{
+ notmuch_config_t *config;
+ notmuch_database_t *notmuch;
+ notmuch_query_t *query;
+ char *opt, *query_string;
+ int i, ret = 0;
+ int (*reply_format_func)(void *ctx, notmuch_config_t *config, notmuch_query_t *query, notmuch_show_params_t *params);
+ notmuch_show_params_t params;
+
+ reply_format_func = notmuch_reply_format_default;
+ params.part = -1;
+ params.cryptoctx = NULL;
+
+ for (i = 0; i < argc && argv[i][0] == '-'; i++) {
+ if (strcmp (argv[i], "--") == 0) {
+ i++;
+ break;
+ }
+ if (STRNCMP_LITERAL (argv[i], "--format=") == 0) {
+ opt = argv[i] + sizeof ("--format=") - 1;
+ if (strcmp (opt, "default") == 0) {
+ reply_format_func = notmuch_reply_format_default;
+ } else if (strcmp (opt, "headers-only") == 0) {
+ reply_format_func = notmuch_reply_format_headers_only;
+ } else {
+ fprintf (stderr, "Invalid value for --format: %s\n", opt);
+ return 1;
+ }
+ } else if ((STRNCMP_LITERAL (argv[i], "--decrypt") == 0)) {
+ if (params.cryptoctx == NULL) {
+ GMimeSession* session = g_object_new(g_mime_session_get_type(), NULL);
+ if (NULL == (params.cryptoctx = g_mime_gpg_context_new(session, "gpg")))
+ fprintf (stderr, "Failed to construct gpg context.\n");
+ else
+ g_mime_gpg_context_set_always_trust((GMimeGpgContext*)params.cryptoctx, FALSE);
+ g_object_unref (session);
+ session = NULL;
+ }
+ } else {
+ fprintf (stderr, "Unrecognized option: %s\n", argv[i]);
+ return 1;
+ }
+ }
+
+ argc -= i;
+ argv += i;
+
+ config = notmuch_config_open (ctx, NULL, NULL);
+ if (config == NULL)
+ return 1;
+
+ query_string = query_string_from_args (ctx, argc, argv);
+ 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) != 0)
+ return 1;