+ in_reply_to = talloc_asprintf (ctx, "<%s>",
+ notmuch_message_get_message_id (message));
+
+ g_mime_object_set_header (GMIME_OBJECT (reply), "In-Reply-To", in_reply_to, NULL);
+
+ orig_references = notmuch_message_get_header (message, "references");
+ if (orig_references && *orig_references)
+ references = talloc_asprintf (ctx, "%s %s", orig_references,
+ in_reply_to);
+ else
+ references = talloc_strdup (ctx, in_reply_to);
+
+ g_mime_object_set_header (GMIME_OBJECT (reply), "References", references, NULL);
+
+ from_addr = add_recipients_from_message (reply, config,
+ mime_message, reply_all);
+
+ /* The above is all that is needed for limited headers. */
+ if (limited)
+ return reply;
+
+ /*
+ * Sadly, there is no standard way to find out to which email
+ * address a mail was delivered - what is in the headers depends
+ * on the MTAs used along the way.
+ *
+ * If none of the user's email addresses are in the To: or Cc:
+ * headers, we try a number of heuristics which hopefully will
+ * answer this question.
+ *
+ * First, check for Envelope-To:, X-Original-To:, and
+ * Delivered-To: headers.
+ */
+ if (from_addr == NULL)
+ from_addr = get_from_in_to_headers (config, message);
+
+ /*
+ * Check for a (for <email@add.res>) clause in Received: headers,
+ * and the domain part of known email addresses in the 'by' part
+ * of Received: headers
+ */
+ if (from_addr == NULL)
+ from_addr = guess_from_in_received_headers (config, message);
+
+ /* Default to user's primary address. */
+ if (from_addr == NULL)
+ from_addr = notmuch_config_get_user_primary_email (config);
+
+ from_addr = talloc_asprintf (ctx, "%s <%s>",
+ notmuch_config_get_user_name (config),
+ from_addr);
+ g_mime_object_set_header (GMIME_OBJECT (reply), "From", from_addr, NULL);
+
+ subject = notmuch_message_get_header (message, "subject");
+ if (subject) {
+ if (strncasecmp (subject, "Re:", 3))
+ subject = talloc_asprintf (ctx, "Re: %s", subject);
+ g_mime_message_set_subject (reply, subject, NULL);
+ }
+
+ return reply;
+}
+
+enum {
+ FORMAT_DEFAULT,
+ FORMAT_JSON,
+ FORMAT_SEXP,
+ FORMAT_HEADERS_ONLY,
+};
+
+static int do_reply(notmuch_config_t *config,
+ notmuch_query_t *query,
+ notmuch_show_params_t *params,
+ int format,
+ bool reply_all)
+{
+ GMimeMessage *reply;
+ mime_node_t *node;
+ notmuch_messages_t *messages;
+ notmuch_message_t *message;
+ notmuch_status_t status;
+ struct sprinter *sp = NULL;
+
+ if (format == FORMAT_JSON || format == FORMAT_SEXP) {
+ unsigned count;
+
+ status = notmuch_query_count_messages (query, &count);
+ if (print_status_query ("notmuch reply", query, status))
+ return 1;
+
+ if (count != 1) {
+ fprintf (stderr, "Error: search term did not match precisely one message (matched %u messages).\n", count);
+ return 1;
+ }
+
+ if (format == FORMAT_JSON)
+ sp = sprinter_json_create (config, stdout);
+ else
+ sp = sprinter_sexp_create (config, stdout);
+ }
+
+ status = notmuch_query_search_messages (query, &messages);
+ if (print_status_query ("notmuch reply", query, status))
+ return 1;
+
+ for (;
+ notmuch_messages_valid (messages);
+ notmuch_messages_move_to_next (messages))