+ orig_references = notmuch_message_get_header (message, "references");
+
+ /* We print In-Reply-To followed by References because git format-patch treats them
+ * specially. Git does not interpret the other headers specially
+ */
+ references = talloc_asprintf (ctx, "%s%s%s",
+ orig_references ? orig_references : "",
+ orig_references ? " " : "",
+ in_reply_to);
+ g_mime_object_set_header (GMIME_OBJECT (reply),
+ "References", references);
+
+ (void)add_recipients_from_message (reply, config, message);
+
+ g_mime_object_set_header (GMIME_OBJECT (reply), "Bcc",
+ notmuch_config_get_user_primary_email (config));
+
+ reply_headers = g_mime_object_to_string (GMIME_OBJECT (reply));
+ printf ("%s", reply_headers);
+ free (reply_headers);
+
+ g_object_unref (G_OBJECT (reply));
+ reply = NULL;
+
+ notmuch_message_destroy (message);
+ }
+ 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);
+
+ reply_format_func = notmuch_reply_format_default;
+
+ 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 {
+ 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;
+ }