]> git.notmuchmail.org Git - notmuch/blob - notmuch-reply.c
emacs: Add new option notmuch-search-hide-excluded
[notmuch] / notmuch-reply.c
1 /* notmuch - Not much of an email program, (just index and search)
2  *
3  * Copyright © 2009 Carl Worth
4  * Copyright © 2009 Keith Packard
5  *
6  * This program is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program.  If not, see https://www.gnu.org/licenses/ .
18  *
19  * Authors: Carl Worth <cworth@cworth.org>
20  *          Keith Packard <keithp@keithp.com>
21  */
22
23 #include "notmuch-client.h"
24 #include "string-util.h"
25 #include "sprinter.h"
26
27 static void
28 show_reply_headers (GMimeStream *stream, GMimeMessage *message)
29 {
30     /* Output RFC 2822 formatted (and RFC 2047 encoded) headers. */
31     if (g_mime_object_write_to_stream (GMIME_OBJECT (message), NULL, stream) < 0) {
32         INTERNAL_ERROR ("failed to write headers to stdout\n");
33     }
34 }
35
36 static void
37 format_part_reply (GMimeStream *stream, mime_node_t *node)
38 {
39     int i;
40
41     if (node->envelope_file) {
42         g_mime_stream_printf (stream, "On %s, %s wrote:\n",
43                               notmuch_message_get_header (node->envelope_file, "date"),
44                               notmuch_message_get_header (node->envelope_file, "from"));
45     } else if (GMIME_IS_MESSAGE (node->part)) {
46         GMimeMessage *message = GMIME_MESSAGE (node->part);
47         char *recipients_string;
48
49         g_mime_stream_printf (stream, "> From: %s\n", g_mime_message_get_from_string (message));
50         recipients_string = g_mime_message_get_address_string (message, GMIME_ADDRESS_TYPE_TO);
51         if (recipients_string)
52             g_mime_stream_printf (stream, "> To: %s\n",
53                                   recipients_string);
54         g_free (recipients_string);
55         recipients_string = g_mime_message_get_address_string (message, GMIME_ADDRESS_TYPE_CC);
56         if (recipients_string)
57             g_mime_stream_printf (stream, "> Cc: %s\n",
58                                   recipients_string);
59         g_free (recipients_string);
60         g_mime_stream_printf (stream, "> Subject: %s\n", g_mime_message_get_subject (message));
61         g_mime_stream_printf (stream, "> Date: %s\n", g_mime_message_get_date_string (node, message));
62         g_mime_stream_printf (stream, ">\n");
63     } else if (GMIME_IS_PART (node->part)) {
64         GMimeContentType *content_type = g_mime_object_get_content_type (node->part);
65         GMimeContentDisposition *disposition = g_mime_object_get_content_disposition (node->part);
66
67         if (g_mime_content_type_is_type (content_type, "application", "pgp-encrypted") ||
68             g_mime_content_type_is_type (content_type, "application", "pgp-signature") ||
69             g_mime_content_type_is_type (content_type, "application", "pkcs7-mime")) {
70             /* Ignore PGP/MIME and S/MIME cruft parts */
71         } else if (g_mime_content_type_is_type (content_type, "text", "*") &&
72                    ! g_mime_content_type_is_type (content_type, "text", "html")) {
73             show_text_part_content (node->part, stream, NOTMUCH_SHOW_TEXT_PART_REPLY);
74         } else if (disposition &&
75                    strcasecmp (g_mime_content_disposition_get_disposition (disposition),
76                                GMIME_DISPOSITION_ATTACHMENT) == 0) {
77             const char *filename = g_mime_part_get_filename (GMIME_PART (node->part));
78             g_mime_stream_printf (stream, "Attachment: %s (%s)\n", filename,
79                                   g_mime_content_type_get_mime_type (content_type));
80         } else {
81             g_mime_stream_printf (stream, "Non-text part: %s\n",
82                                   g_mime_content_type_get_mime_type (content_type));
83         }
84     }
85
86     for (i = 0; i < node->nchildren; i++)
87         format_part_reply (stream, mime_node_child (node, i));
88 }
89
90 typedef enum {
91     USER_ADDRESS_IN_STRING,
92     STRING_IN_USER_ADDRESS,
93     STRING_IS_USER_ADDRESS,
94 } address_match_t;
95
96 /* Match given string against given address according to mode. */
97 static bool
98 match_address (const char *str, const char *address, address_match_t mode)
99 {
100     switch (mode) {
101     case USER_ADDRESS_IN_STRING:
102         return strcasestr (str, address) != NULL;
103     case STRING_IN_USER_ADDRESS:
104         return strcasestr (address, str) != NULL;
105     case STRING_IS_USER_ADDRESS:
106         return strcasecmp (address, str) == 0;
107     }
108
109     return false;
110 }
111
112 /* Match given string against user's configured "primary" and "other"
113  * addresses according to mode. */
114 static const char *
115 address_match (const char *str, notmuch_database_t *notmuch, address_match_t mode)
116 {
117     const char *primary;
118     notmuch_config_values_t *other = NULL;
119
120     if (! str || *str == '\0')
121         return NULL;
122
123     primary = notmuch_config_get (notmuch, NOTMUCH_CONFIG_PRIMARY_EMAIL);
124     if (match_address (str, primary, mode))
125         return primary;
126
127     for (other = notmuch_config_get_values (notmuch, NOTMUCH_CONFIG_OTHER_EMAIL);
128          notmuch_config_values_valid (other);
129          notmuch_config_values_move_to_next (other)) {
130         const char *addr = notmuch_config_values_get (other);
131
132         if (match_address (str, addr, mode))
133             return addr;
134     }
135     return NULL;
136 }
137
138 /* Does the given string contain an address configured as one of the
139  * user's "primary" or "other" addresses. If so, return the matching
140  * address, NULL otherwise. */
141 static const char *
142 user_address_in_string (const char *str, notmuch_database_t *notmuch)
143 {
144     return address_match (str, notmuch, USER_ADDRESS_IN_STRING);
145 }
146
147 /* Do any of the addresses configured as one of the user's "primary"
148  * or "other" addresses contain the given string. If so, return the
149  * matching address, NULL otherwise. */
150 static const char *
151 string_in_user_address (const char *str, notmuch_database_t *notmuch)
152 {
153     return address_match (str, notmuch, STRING_IN_USER_ADDRESS);
154 }
155
156 /* Is the given address configured as one of the user's "primary" or
157  * "other" addresses. */
158 static bool
159 address_is_users (const char *address, notmuch_database_t *notmuch)
160 {
161     return address_match (address, notmuch, STRING_IS_USER_ADDRESS) != NULL;
162 }
163
164 /* Scan addresses in 'list'.
165  *
166  * If 'message' is non-NULL, then for each address in 'list' that is
167  * not configured as one of the user's addresses in 'config', add that
168  * address to 'message' as an address of 'type'.
169  *
170  * If 'user_from' is non-NULL and *user_from is NULL, *user_from will
171  * be set to the first address encountered in 'list' that is the
172  * user's address.
173  *
174  * Return the number of addresses added to 'message'. (If 'message' is
175  * NULL, the function returns 0 by definition.)
176  */
177 static unsigned int
178 scan_address_list (InternetAddressList *list,
179                    notmuch_database_t *notmuch,
180                    GMimeMessage *message,
181                    GMimeAddressType type,
182                    const char **user_from)
183 {
184     InternetAddress *address;
185     int i;
186     unsigned int n = 0;
187
188     if (list == NULL)
189         return 0;
190
191     for (i = 0; i < internet_address_list_length (list); i++) {
192         address = internet_address_list_get_address (list, i);
193         if (INTERNET_ADDRESS_IS_GROUP (address)) {
194             InternetAddressGroup *group;
195             InternetAddressList *group_list;
196
197             group = INTERNET_ADDRESS_GROUP (address);
198             group_list = internet_address_group_get_members (group);
199             n += scan_address_list (group_list, notmuch, message, type, user_from);
200         } else {
201             InternetAddressMailbox *mailbox;
202             const char *name;
203             const char *addr;
204
205             mailbox = INTERNET_ADDRESS_MAILBOX (address);
206
207             name = internet_address_get_name (address);
208             addr = internet_address_mailbox_get_addr (mailbox);
209
210             if (address_is_users (addr, notmuch)) {
211                 if (user_from && *user_from == NULL)
212                     *user_from = addr;
213             } else if (message) {
214                 g_mime_message_add_mailbox (message, type, name, addr);
215                 n++;
216             }
217         }
218     }
219
220     return n;
221 }
222
223 /* Does the address in the Reply-To header of 'message' already appear
224  * in either the 'To' or 'Cc' header of the message?
225  */
226 static bool
227 reply_to_header_is_redundant (GMimeMessage *message,
228                               InternetAddressList *reply_to_list)
229 {
230     const char *addr, *reply_to;
231     InternetAddress *address;
232     InternetAddressMailbox *mailbox;
233     InternetAddressList *recipients;
234     bool ret = false;
235     int i;
236
237     if (reply_to_list == NULL ||
238         internet_address_list_length (reply_to_list) != 1)
239         return 0;
240
241     address = internet_address_list_get_address (reply_to_list, 0);
242     if (INTERNET_ADDRESS_IS_GROUP (address))
243         return 0;
244
245     mailbox = INTERNET_ADDRESS_MAILBOX (address);
246     reply_to = internet_address_mailbox_get_addr (mailbox);
247
248     recipients = g_mime_message_get_all_recipients (message);
249
250     for (i = 0; i < internet_address_list_length (recipients); i++) {
251         address = internet_address_list_get_address (recipients, i);
252         if (INTERNET_ADDRESS_IS_GROUP (address))
253             continue;
254
255         mailbox = INTERNET_ADDRESS_MAILBOX (address);
256         addr = internet_address_mailbox_get_addr (mailbox);
257         if (strcmp (addr, reply_to) == 0) {
258             ret = true;
259             break;
260         }
261     }
262
263     g_object_unref (G_OBJECT (recipients));
264
265     return ret;
266 }
267
268 static InternetAddressList *
269 get_sender (GMimeMessage *message)
270 {
271     InternetAddressList *reply_to_list;
272
273     reply_to_list = g_mime_message_get_reply_to_list (message);
274     if (reply_to_list &&
275         internet_address_list_length (reply_to_list) > 0) {
276         /*
277          * Some mailing lists munge the Reply-To header despite it
278          * being A Bad Thing, see
279          * http://marc.merlins.org/netrants/reply-to-harmful.html
280          *
281          * The munging is easy to detect, because it results in a
282          * redundant reply-to header, (with an address that already
283          * exists in either To or Cc). So in this case, we ignore the
284          * Reply-To field and use the From header. This ensures the
285          * original sender will get the reply even if not subscribed
286          * to the list. Note that the address in the Reply-To header
287          * will always appear in the reply if reply_all is true.
288          */
289         if (! reply_to_header_is_redundant (message, reply_to_list))
290             return reply_to_list;
291     }
292
293     return g_mime_message_get_from (message);
294 }
295
296 static InternetAddressList *
297 get_to (GMimeMessage *message)
298 {
299     return g_mime_message_get_addresses (message, GMIME_ADDRESS_TYPE_TO);
300 }
301
302 static InternetAddressList *
303 get_cc (GMimeMessage *message)
304 {
305     return g_mime_message_get_addresses (message, GMIME_ADDRESS_TYPE_CC);
306 }
307
308 static InternetAddressList *
309 get_bcc (GMimeMessage *message)
310 {
311     return g_mime_message_get_addresses (message, GMIME_ADDRESS_TYPE_BCC);
312 }
313
314 /* Augment the recipients of 'reply' from the "Reply-to:", "From:",
315  * "To:", "Cc:", and "Bcc:" headers of 'message'.
316  *
317  * If 'reply_all' is true, use sender and all recipients, otherwise
318  * scan the headers for the first that contains something other than
319  * the user's addresses and add the recipients from this header
320  * (typically this would be reply-to-sender, but also handles reply to
321  * user's own message in a sensible way).
322  *
323  * If any of the user's addresses were found in these headers, the
324  * first of these returned, otherwise NULL is returned.
325  */
326 static const char *
327 add_recipients_from_message (GMimeMessage *reply,
328                              notmuch_database_t *notmuch,
329                              GMimeMessage *message,
330                              bool reply_all)
331 {
332     struct {
333         InternetAddressList * (*get_header)(GMimeMessage *message);
334         GMimeAddressType recipient_type;
335     } reply_to_map[] = {
336         { get_sender,   GMIME_ADDRESS_TYPE_TO },
337         { get_to,       GMIME_ADDRESS_TYPE_TO },
338         { get_cc,       GMIME_ADDRESS_TYPE_CC },
339         { get_bcc,      GMIME_ADDRESS_TYPE_BCC },
340     };
341     const char *from_addr = NULL;
342     unsigned int i;
343     unsigned int n = 0;
344
345     for (i = 0; i < ARRAY_SIZE (reply_to_map); i++) {
346         InternetAddressList *recipients;
347
348         recipients = reply_to_map[i].get_header (message);
349
350         n += scan_address_list (recipients, notmuch, reply,
351                                 reply_to_map[i].recipient_type, &from_addr);
352
353         if (! reply_all && n) {
354             /* Stop adding new recipients in reply-to-sender mode if
355              * we have added some recipient(s) above.
356              *
357              * This also handles the case of user replying to his own
358              * message, where reply-to/from is not a recipient. In
359              * this case there may be more than one recipient even if
360              * not replying to all.
361              */
362             reply = NULL;
363
364             /* From address and some recipients are enough, bail out. */
365             if (from_addr)
366                 break;
367         }
368     }
369
370     /* If no recipients were added but we found one of the user's
371      * addresses to use as a from address then the message is from the
372      * user to the user - add the discovered from address to the list
373      * of recipients so that the reply goes back to the user.
374      */
375     if (n == 0 && from_addr)
376         g_mime_message_add_mailbox (reply, GMIME_ADDRESS_TYPE_TO, NULL, from_addr);
377
378     return from_addr;
379 }
380
381 /*
382  * Look for the user's address in " for <email@add.res>" in the
383  * received headers.
384  *
385  * Return the address that was found, if any, and NULL otherwise.
386  */
387 static const char *
388 guess_from_in_received_for (notmuch_database_t *notmuch, const char *received)
389 {
390     const char *ptr;
391
392     ptr = strstr (received, " for ");
393     if (! ptr)
394         return NULL;
395
396     return user_address_in_string (ptr, notmuch);
397 }
398
399 /*
400  * Parse all the " by MTA ..." parts in received headers to guess the
401  * email address that this was originally delivered to.
402  *
403  * Extract just the MTA here by removing leading whitespace and
404  * assuming that the MTA name ends at the next whitespace. Test for
405  * *(by+4) to be non-'\0' to make sure there's something there at all
406  * - and then assume that the first whitespace delimited token that
407  * follows is the receiving system in this step of the receive chain.
408  *
409  * Return the address that was found, if any, and NULL otherwise.
410  */
411 static const char *
412 guess_from_in_received_by (notmuch_database_t *notmuch, const char *received)
413 {
414     const char *addr;
415     const char *by = received;
416     char *domain, *tld, *mta, *ptr, *token;
417
418     while ((by = strstr (by, " by ")) != NULL) {
419         by += 4;
420         if (*by == '\0')
421             break;
422         mta = xstrdup (by);
423         token = strtok (mta, " \t");
424         if (token == NULL) {
425             free (mta);
426             break;
427         }
428         /*
429          * Now extract the last two components of the MTA host name as
430          * domain and tld.
431          */
432         domain = tld = NULL;
433         while ((ptr = strsep (&token, ". \t")) != NULL) {
434             if (*ptr == '\0')
435                 continue;
436             domain = tld;
437             tld = ptr;
438         }
439
440         if (domain) {
441             /*
442              * Recombine domain and tld and look for it among the
443              * configured email addresses. This time we have a known
444              * domain name and nothing else - so the test is the other
445              * way around: we check if this is a substring of one of
446              * the email addresses.
447              */
448             *(tld - 1) = '.';
449
450             addr = string_in_user_address (domain, notmuch);
451             if (addr) {
452                 free (mta);
453                 return addr;
454             }
455         }
456         free (mta);
457     }
458
459     return NULL;
460 }
461
462 /*
463  * Get the concatenated Received: headers and search from the front
464  * (last Received: header added) and try to extract from them
465  * indications to which email address this message was delivered.
466  *
467  * The Received: header is among special ones in our get_header function
468  * and is always concatenated.
469  *
470  * Return the address that was found, if any, and NULL otherwise.
471  */
472 static const char *
473 guess_from_in_received_headers (notmuch_message_t *message)
474 {
475     const char *received, *addr;
476     char *sanitized;
477
478     notmuch_database_t *notmuch = notmuch_message_get_database (message);
479
480     received = notmuch_message_get_header (message, "received");
481     if (! received)
482         return NULL;
483
484     sanitized = sanitize_string (NULL, received);
485     if (! sanitized)
486         return NULL;
487
488     addr = guess_from_in_received_for (notmuch, sanitized);
489     if (! addr)
490         addr = guess_from_in_received_by (notmuch, sanitized);
491
492     talloc_free (sanitized);
493
494     return addr;
495 }
496
497 /*
498  * Try to find user's email address in one of the extra To-like
499  * headers: Envelope-To, X-Original-To, and Delivered-To (searched in
500  * that order).
501  *
502  * The Delivered-To: header is among special ones in our get_header
503  * function and is always concatenated.
504  *
505  * Return the address that was found, if any, and NULL otherwise.
506  */
507 static const char *
508 get_from_in_to_headers (notmuch_message_t *message)
509 {
510     size_t i;
511     const char *tohdr, *addr;
512     const char *to_headers[] = {
513         "Envelope-to",
514         "X-Original-To",
515         "Delivered-To",
516     };
517
518     notmuch_database_t *notmuch = notmuch_message_get_database (message);
519
520     for (i = 0; i < ARRAY_SIZE (to_headers); i++) {
521         tohdr = notmuch_message_get_header (message, to_headers[i]);
522
523         /* Note: tohdr potentially contains a list of email addresses. */
524         addr = user_address_in_string (tohdr, notmuch);
525         if (addr)
526             return addr;
527     }
528
529     return NULL;
530 }
531
532 static GMimeMessage *
533 create_reply_message (void *ctx,
534                       notmuch_message_t *message,
535                       GMimeMessage *mime_message,
536                       bool reply_all,
537                       bool limited)
538 {
539     const char *subject, *from_addr = NULL;
540     const char *in_reply_to, *orig_references, *references;
541     notmuch_database_t *notmuch = notmuch_message_get_database (message);
542     /*
543      * Use the below header order for limited headers, "pretty" order
544      * otherwise.
545      */
546     GMimeMessage *reply = g_mime_message_new (limited ? 0 : 1);
547
548     if (reply == NULL) {
549         fprintf (stderr, "Out of memory\n");
550         return NULL;
551     }
552
553     in_reply_to = talloc_asprintf (ctx, "<%s>",
554                                    notmuch_message_get_message_id (message));
555
556     g_mime_object_set_header (GMIME_OBJECT (reply), "In-Reply-To", in_reply_to, NULL);
557
558     orig_references = notmuch_message_get_header (message, "references");
559     if (orig_references && *orig_references)
560         references = talloc_asprintf (ctx, "%s %s", orig_references,
561                                       in_reply_to);
562     else
563         references = talloc_strdup (ctx, in_reply_to);
564
565     g_mime_object_set_header (GMIME_OBJECT (reply), "References", references, NULL);
566
567     from_addr = add_recipients_from_message (reply, notmuch,
568                                              mime_message, reply_all);
569
570     /* The above is all that is needed for limited headers. */
571     if (limited)
572         return reply;
573
574     /*
575      * Sadly, there is no standard way to find out to which email
576      * address a mail was delivered - what is in the headers depends
577      * on the MTAs used along the way.
578      *
579      * If none of the user's email addresses are in the To: or Cc:
580      * headers, we try a number of heuristics which hopefully will
581      * answer this question.
582      *
583      * First, check for Envelope-To:, X-Original-To:, and
584      * Delivered-To: headers.
585      */
586     if (from_addr == NULL)
587         from_addr = get_from_in_to_headers (message);
588
589     /*
590      * Check for a (for <email@add.res>) clause in Received: headers,
591      * and the domain part of known email addresses in the 'by' part
592      * of Received: headers
593      */
594     if (from_addr == NULL)
595         from_addr = guess_from_in_received_headers (message);
596
597     /* Default to user's primary address. */
598     if (from_addr == NULL)
599         from_addr = notmuch_config_get (notmuch, NOTMUCH_CONFIG_PRIMARY_EMAIL);
600
601     from_addr = talloc_asprintf (ctx, "%s <%s>",
602                                  notmuch_config_get (notmuch, NOTMUCH_CONFIG_USER_NAME),
603                                  from_addr);
604     g_mime_object_set_header (GMIME_OBJECT (reply), "From", from_addr, NULL);
605
606     subject = g_mime_message_get_subject (mime_message);
607     if (subject) {
608         if (strncasecmp (subject, "Re:", 3))
609             subject = talloc_asprintf (ctx, "Re: %s", subject);
610         g_mime_message_set_subject (reply, subject, NULL);
611     }
612
613     return reply;
614 }
615
616 enum {
617     FORMAT_DEFAULT,
618     FORMAT_JSON,
619     FORMAT_SEXP,
620     FORMAT_HEADERS_ONLY,
621 };
622
623 static int
624 do_reply (notmuch_database_t *notmuch,
625           notmuch_query_t *query,
626           notmuch_show_params_t *params,
627           int format,
628           bool reply_all)
629 {
630     GMimeMessage *reply;
631     mime_node_t *node;
632     notmuch_messages_t *messages;
633     notmuch_message_t *message;
634     notmuch_status_t status;
635     struct sprinter *sp = NULL;
636
637     if (format == FORMAT_JSON || format == FORMAT_SEXP) {
638         unsigned count;
639
640         status = notmuch_query_count_messages (query, &count);
641         if (print_status_query ("notmuch reply", query, status))
642             return 1;
643
644         if (count != 1) {
645             fprintf (stderr,
646                      "Error: search term did not match precisely one message (matched %u messages).\n",
647                      count);
648             return 1;
649         }
650
651         if (format == FORMAT_JSON)
652             sp = sprinter_json_create (notmuch, stdout);
653         else
654             sp = sprinter_sexp_create (notmuch, stdout);
655     }
656
657     status = notmuch_query_search_messages (query, &messages);
658     if (print_status_query ("notmuch reply", query, status))
659         return 1;
660
661     for (;
662          notmuch_messages_valid (messages);
663          notmuch_messages_move_to_next (messages)) {
664         message = notmuch_messages_get (messages);
665
666         if (mime_node_open (notmuch, message, params->duplicate, &params->crypto, &node))
667             return 1;
668
669         reply = create_reply_message (notmuch, message,
670                                       GMIME_MESSAGE (node->part), reply_all,
671                                       format == FORMAT_HEADERS_ONLY);
672         if (! reply)
673             return 1;
674
675         if (format == FORMAT_JSON || format == FORMAT_SEXP) {
676             sp->begin_map (sp);
677
678             /* The headers of the reply message we've created */
679             sp->map_key (sp, "reply-headers");
680             /* FIXME: send msg_crypto here to avoid killing the
681              * subject line on reply to encrypted messages! */
682             format_headers_sprinter (sp, reply, true, NULL);
683
684             /* Start the original */
685             sp->map_key (sp, "original");
686             format_part_sprinter (notmuch, sp, node, params->duplicate, true, false);
687
688             /* End */
689             sp->end (sp);
690         } else {
691             GMimeStream *stream_stdout = stream_stdout = g_mime_stream_stdout_new ();
692             if (stream_stdout) {
693                 show_reply_headers (stream_stdout, reply);
694                 if (format == FORMAT_DEFAULT)
695                     format_part_reply (stream_stdout, node);
696             }
697             g_mime_stream_flush (stream_stdout);
698             g_object_unref (stream_stdout);
699         }
700
701         g_object_unref (G_OBJECT (reply));
702         talloc_free (node);
703
704         notmuch_message_destroy (message);
705     }
706
707     return 0;
708 }
709
710 int
711 notmuch_reply_command (notmuch_database_t *notmuch, int argc, char *argv[])
712 {
713     notmuch_query_t *query;
714     char *query_string;
715     int opt_index;
716     notmuch_show_params_t params = {
717         .part = -1,
718         .duplicate = 0,
719         .crypto = { .decrypt = NOTMUCH_DECRYPT_AUTO },
720     };
721     int format = FORMAT_DEFAULT;
722     int reply_all = true;
723     notmuch_status_t status;
724
725     notmuch_opt_desc_t options[] = {
726         { .opt_keyword = &format, .name = "format", .keywords =
727               (notmuch_keyword_t []){ { "default", FORMAT_DEFAULT },
728                                       { "json", FORMAT_JSON },
729                                       { "sexp", FORMAT_SEXP },
730                                       { "headers-only", FORMAT_HEADERS_ONLY },
731                                       { 0, 0 } } },
732         { .opt_int = &notmuch_format_version, .name = "format-version" },
733         { .opt_keyword = &reply_all, .name = "reply-to", .keywords =
734               (notmuch_keyword_t []){ { "all", true },
735                                       { "sender", false },
736                                       { 0, 0 } } },
737         { .opt_keyword = (int *) (&params.crypto.decrypt), .name = "decrypt",
738           .keyword_no_arg_value = "true", .keywords =
739               (notmuch_keyword_t []){ { "false", NOTMUCH_DECRYPT_FALSE },
740                                       { "auto", NOTMUCH_DECRYPT_AUTO },
741                                       { "true", NOTMUCH_DECRYPT_NOSTASH },
742                                       { 0, 0 } } },
743         { .opt_int = &params.duplicate, .name = "duplicate" },
744         { .opt_inherit = notmuch_shared_options },
745         { }
746     };
747
748     opt_index = parse_arguments (argc, argv, options, 1);
749     if (opt_index < 0)
750         return EXIT_FAILURE;
751
752     notmuch_process_shared_options (notmuch, argv[0]);
753
754     notmuch_exit_if_unsupported_format ();
755
756     query_string = query_string_from_args (notmuch, argc - opt_index, argv + opt_index);
757     if (query_string == NULL) {
758         fprintf (stderr, "Out of memory\n");
759         return EXIT_FAILURE;
760     }
761
762     if (*query_string == '\0') {
763         fprintf (stderr, "Error: notmuch reply requires at least one search term.\n");
764         return EXIT_FAILURE;
765     }
766
767     status = notmuch_query_create_with_syntax (notmuch, query_string,
768                                                shared_option_query_syntax (),
769                                                &query);
770     if (print_status_database ("notmuch reply", notmuch, status))
771         return EXIT_FAILURE;
772
773     if (do_reply (notmuch, query, &params, format, reply_all) != 0)
774         return EXIT_FAILURE;
775
776     _notmuch_crypto_cleanup (&params.crypto);
777     notmuch_query_destroy (query);
778     notmuch_database_destroy (notmuch);
779
780     return EXIT_SUCCESS;
781 }