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