]> git.notmuchmail.org Git - notmuch/blob - notmuch-reply.c
emacs: add a tag jump menu
[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 (GMimeMessage *message)
29 {
30     GMimeStream *stream_stdout = NULL;
31
32     stream_stdout = g_mime_stream_file_new (stdout);
33     if (stream_stdout) {
34         g_mime_stream_file_set_owner (GMIME_STREAM_FILE (stream_stdout), FALSE);
35         /* Output RFC 2822 formatted (and RFC 2047 encoded) headers. */
36         g_mime_object_write_to_stream (GMIME_OBJECT(message), stream_stdout);
37         g_object_unref(stream_stdout);
38     }
39 }
40
41 static void
42 format_part_reply (mime_node_t *node)
43 {
44     int i;
45
46     if (node->envelope_file) {
47         printf ("On %s, %s wrote:\n",
48                 notmuch_message_get_header (node->envelope_file, "date"),
49                 notmuch_message_get_header (node->envelope_file, "from"));
50     } else if (GMIME_IS_MESSAGE (node->part)) {
51         GMimeMessage *message = GMIME_MESSAGE (node->part);
52         InternetAddressList *recipients;
53         const char *recipients_string;
54
55         printf ("> From: %s\n", g_mime_message_get_sender (message));
56         recipients = g_mime_message_get_recipients (message, GMIME_RECIPIENT_TYPE_TO);
57         recipients_string = internet_address_list_to_string (recipients, 0);
58         if (recipients_string)
59             printf ("> To: %s\n",
60                     recipients_string);
61         recipients = g_mime_message_get_recipients (message, GMIME_RECIPIENT_TYPE_CC);
62         recipients_string = internet_address_list_to_string (recipients, 0);
63         if (recipients_string)
64             printf ("> Cc: %s\n",
65                     recipients_string);
66         printf ("> Subject: %s\n", g_mime_message_get_subject (message));
67         printf ("> Date: %s\n", g_mime_message_get_date_as_string (message));
68         printf (">\n");
69     } else if (GMIME_IS_PART (node->part)) {
70         GMimeContentType *content_type = g_mime_object_get_content_type (node->part);
71         GMimeContentDisposition *disposition = g_mime_object_get_content_disposition (node->part);
72
73         if (g_mime_content_type_is_type (content_type, "application", "pgp-encrypted") ||
74             g_mime_content_type_is_type (content_type, "application", "pgp-signature")) {
75             /* Ignore PGP/MIME cruft parts */
76         } else if (g_mime_content_type_is_type (content_type, "text", "*") &&
77                    !g_mime_content_type_is_type (content_type, "text", "html")) {
78             GMimeStream *stream_stdout = g_mime_stream_file_new (stdout);
79             g_mime_stream_file_set_owner (GMIME_STREAM_FILE (stream_stdout), FALSE);
80             show_text_part_content (node->part, stream_stdout, NOTMUCH_SHOW_TEXT_PART_REPLY);
81             g_object_unref(stream_stdout);
82         } else if (disposition &&
83                    strcasecmp (g_mime_content_disposition_get_disposition (disposition),
84                                GMIME_DISPOSITION_ATTACHMENT) == 0) {
85             const char *filename = g_mime_part_get_filename (GMIME_PART (node->part));
86             printf ("Attachment: %s (%s)\n", filename,
87                     g_mime_content_type_to_string (content_type));
88         } else {
89             printf ("Non-text part: %s\n",
90                     g_mime_content_type_to_string (content_type));
91         }
92     }
93
94     for (i = 0; i < node->nchildren; i++)
95         format_part_reply (mime_node_child (node, i));
96 }
97
98 typedef enum {
99     USER_ADDRESS_IN_STRING,
100     STRING_IN_USER_ADDRESS,
101     STRING_IS_USER_ADDRESS,
102 } address_match_t;
103
104 /* Match given string against given address according to mode. */
105 static notmuch_bool_t
106 match_address (const char *str, const char *address, address_match_t mode)
107 {
108     switch (mode) {
109     case USER_ADDRESS_IN_STRING:
110         return strcasestr (str, address) != NULL;
111     case STRING_IN_USER_ADDRESS:
112         return strcasestr (address, str) != NULL;
113     case STRING_IS_USER_ADDRESS:
114         return strcasecmp (address, str) == 0;
115     }
116
117     return FALSE;
118 }
119
120 /* Match given string against user's configured "primary" and "other"
121  * addresses according to mode. */
122 static const char *
123 address_match (const char *str, notmuch_config_t *config, address_match_t mode)
124 {
125     const char *primary;
126     const char **other;
127     size_t i, other_len;
128
129     if (!str || *str == '\0')
130         return NULL;
131
132     primary = notmuch_config_get_user_primary_email (config);
133     if (match_address (str, primary, mode))
134         return primary;
135
136     other = notmuch_config_get_user_other_email (config, &other_len);
137     for (i = 0; i < other_len; i++) {
138         if (match_address (str, other[i], mode))
139             return other[i];
140     }
141
142     return NULL;
143 }
144
145 /* Does the given string contain an address configured as one of the
146  * user's "primary" or "other" addresses. If so, return the matching
147  * address, NULL otherwise. */
148 static const char *
149 user_address_in_string (const char *str, notmuch_config_t *config)
150 {
151     return address_match (str, config, USER_ADDRESS_IN_STRING);
152 }
153
154 /* Do any of the addresses configured as one of the user's "primary"
155  * or "other" addresses contain the given string. If so, return the
156  * matching address, NULL otherwise. */
157 static const char *
158 string_in_user_address (const char *str, notmuch_config_t *config)
159 {
160     return address_match (str, config, STRING_IN_USER_ADDRESS);
161 }
162
163 /* Is the given address configured as one of the user's "primary" or
164  * "other" addresses. */
165 static notmuch_bool_t
166 address_is_users (const char *address, notmuch_config_t *config)
167 {
168     return address_match (address, config, STRING_IS_USER_ADDRESS) != NULL;
169 }
170
171 /* Scan addresses in 'list'.
172  *
173  * If 'message' is non-NULL, then for each address in 'list' that is
174  * not configured as one of the user's addresses in 'config', add that
175  * address to 'message' as an address of 'type'.
176  *
177  * If 'user_from' is non-NULL and *user_from is NULL, *user_from will
178  * be set to the first address encountered in 'list' that is the
179  * user's address.
180  *
181  * Return the number of addresses added to 'message'. (If 'message' is
182  * NULL, the function returns 0 by definition.)
183  */
184 static unsigned int
185 scan_address_list (InternetAddressList *list,
186                    notmuch_config_t *config,
187                    GMimeMessage *message,
188                    GMimeRecipientType type,
189                    const char **user_from)
190 {
191     InternetAddress *address;
192     int i;
193     unsigned int n = 0;
194
195     if (list == NULL)
196         return 0;
197
198     for (i = 0; i < internet_address_list_length (list); i++) {
199         address = internet_address_list_get_address (list, i);
200         if (INTERNET_ADDRESS_IS_GROUP (address)) {
201             InternetAddressGroup *group;
202             InternetAddressList *group_list;
203
204             group = INTERNET_ADDRESS_GROUP (address);
205             group_list = internet_address_group_get_members (group);
206             n += scan_address_list (group_list, config, message, type, user_from);
207         } else {
208             InternetAddressMailbox *mailbox;
209             const char *name;
210             const char *addr;
211
212             mailbox = INTERNET_ADDRESS_MAILBOX (address);
213
214             name = internet_address_get_name (address);
215             addr = internet_address_mailbox_get_addr (mailbox);
216
217             if (address_is_users (addr, config)) {
218                 if (user_from && *user_from == NULL)
219                     *user_from = addr;
220             } else if (message) {
221                 g_mime_message_add_recipient (message, type, name, addr);
222                 n++;
223             }
224         }
225     }
226
227     return n;
228 }
229
230 /* Does the address in the Reply-To header of 'message' already appear
231  * in either the 'To' or 'Cc' header of the message?
232  */
233 static notmuch_bool_t
234 reply_to_header_is_redundant (GMimeMessage *message,
235                               InternetAddressList *reply_to_list)
236 {
237     const char *addr, *reply_to;
238     InternetAddress *address;
239     InternetAddressMailbox *mailbox;
240     InternetAddressList *recipients;
241     notmuch_bool_t ret = FALSE;
242     int i;
243
244     if (reply_to_list == NULL ||
245         internet_address_list_length (reply_to_list) != 1)
246         return 0;
247
248     address = internet_address_list_get_address (reply_to_list, 0);
249     if (INTERNET_ADDRESS_IS_GROUP (address))
250         return 0;
251
252     mailbox = INTERNET_ADDRESS_MAILBOX (address);
253     reply_to = internet_address_mailbox_get_addr (mailbox);
254
255     recipients = g_mime_message_get_all_recipients (message);
256
257     for (i = 0; i < internet_address_list_length (recipients); i++) {
258         address = internet_address_list_get_address (recipients, i);
259         if (INTERNET_ADDRESS_IS_GROUP (address))
260             continue;
261
262         mailbox = INTERNET_ADDRESS_MAILBOX (address);
263         addr = internet_address_mailbox_get_addr (mailbox);
264         if (strcmp (addr, reply_to) == 0) {
265             ret = TRUE;
266             break;
267         }
268     }
269
270     g_object_unref (G_OBJECT (recipients));
271
272     return ret;
273 }
274
275 static InternetAddressList *get_sender(GMimeMessage *message)
276 {
277     const char *reply_to;
278
279     reply_to = g_mime_message_get_reply_to (message);
280     if (reply_to && *reply_to) {
281         InternetAddressList *reply_to_list;
282
283         /*
284          * Some mailing lists munge the Reply-To header despite it
285          * being A Bad Thing, see
286          * http://marc.merlins.org/netrants/reply-to-harmful.html
287          *
288          * The munging is easy to detect, because it results in a
289          * redundant reply-to header, (with an address that already
290          * exists in either To or Cc). So in this case, we ignore the
291          * Reply-To field and use the From header. This ensures the
292          * original sender will get the reply even if not subscribed
293          * to the list. Note that the address in the Reply-To header
294          * will always appear in the reply if reply_all is true.
295          */
296         reply_to_list = internet_address_list_parse_string (reply_to);
297         if (! reply_to_header_is_redundant (message, reply_to_list))
298             return reply_to_list;
299
300         g_object_unref (G_OBJECT (reply_to_list));
301     }
302
303     return internet_address_list_parse_string (
304         g_mime_message_get_sender (message));
305 }
306
307 static InternetAddressList *get_to(GMimeMessage *message)
308 {
309     return g_mime_message_get_recipients (message, GMIME_RECIPIENT_TYPE_TO);
310 }
311
312 static InternetAddressList *get_cc(GMimeMessage *message)
313 {
314     return g_mime_message_get_recipients (message, GMIME_RECIPIENT_TYPE_CC);
315 }
316
317 static InternetAddressList *get_bcc(GMimeMessage *message)
318 {
319     return g_mime_message_get_recipients (message, GMIME_RECIPIENT_TYPE_BCC);
320 }
321
322 /* Augment the recipients of 'reply' from the "Reply-to:", "From:",
323  * "To:", "Cc:", and "Bcc:" headers of 'message'.
324  *
325  * If 'reply_all' is true, use sender and all recipients, otherwise
326  * scan the headers for the first that contains something other than
327  * the user's addresses and add the recipients from this header
328  * (typically this would be reply-to-sender, but also handles reply to
329  * user's own message in a sensible way).
330  *
331  * If any of the user's addresses were found in these headers, the
332  * first of these returned, otherwise NULL is returned.
333  */
334 static const char *
335 add_recipients_from_message (GMimeMessage *reply,
336                              notmuch_config_t *config,
337                              GMimeMessage *message,
338                              notmuch_bool_t reply_all)
339 {
340     struct {
341         InternetAddressList * (*get_header)(GMimeMessage *message);
342         GMimeRecipientType recipient_type;
343     } reply_to_map[] = {
344         { get_sender,   GMIME_RECIPIENT_TYPE_TO },
345         { get_to,       GMIME_RECIPIENT_TYPE_TO },
346         { get_cc,       GMIME_RECIPIENT_TYPE_CC },
347         { get_bcc,      GMIME_RECIPIENT_TYPE_BCC },
348     };
349     const char *from_addr = NULL;
350     unsigned int i;
351     unsigned int n = 0;
352
353     for (i = 0; i < ARRAY_SIZE (reply_to_map); i++) {
354         InternetAddressList *recipients;
355
356         recipients = reply_to_map[i].get_header (message);
357
358         n += scan_address_list (recipients, config, reply,
359                                 reply_to_map[i].recipient_type, &from_addr);
360
361         if (!reply_all && n) {
362             /* Stop adding new recipients in reply-to-sender mode if
363              * we have added some recipient(s) above.
364              *
365              * This also handles the case of user replying to his own
366              * message, where reply-to/from is not a recipient. In
367              * this case there may be more than one recipient even if
368              * not replying to all.
369              */
370             reply = NULL;
371
372             /* From address and some recipients are enough, bail out. */
373             if (from_addr)
374                 break;
375         }
376     }
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_config_t *config, 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, config);
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_config_t *config, 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, config);
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 special in our get_header function and is
468  * 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_config_t *config,
474                                 notmuch_message_t *message)
475 {
476     const char *received, *addr;
477     char *sanitized;
478
479     received = notmuch_message_get_header (message, "received");
480     if (! received)
481         return NULL;
482
483     sanitized = sanitize_string (NULL, received);
484     if (! sanitized)
485         return NULL;
486
487     addr = guess_from_in_received_for (config, sanitized);
488     if (! addr)
489         addr = guess_from_in_received_by (config, sanitized);
490
491     talloc_free (sanitized);
492
493     return addr;
494 }
495
496 /*
497  * Try to find user's email address in one of the extra To-like
498  * headers: Envelope-To, X-Original-To, and Delivered-To (searched in
499  * that order).
500  *
501  * Return the address that was found, if any, and NULL otherwise.
502  */
503 static const char *
504 get_from_in_to_headers (notmuch_config_t *config, notmuch_message_t *message)
505 {
506     size_t i;
507     const char *tohdr, *addr;
508     const char *to_headers[] = {
509         "Envelope-to",
510         "X-Original-To",
511         "Delivered-To",
512     };
513
514     for (i = 0; i < ARRAY_SIZE (to_headers); i++) {
515         tohdr = notmuch_message_get_header (message, to_headers[i]);
516
517         /* Note: tohdr potentially contains a list of email addresses. */
518         addr = user_address_in_string (tohdr, config);
519         if (addr)
520             return addr;
521     }
522
523     return NULL;
524 }
525
526 static GMimeMessage *
527 create_reply_message(void *ctx,
528                      notmuch_config_t *config,
529                      notmuch_message_t *message,
530                      GMimeMessage *mime_message,
531                      notmuch_bool_t reply_all,
532                      notmuch_bool_t limited)
533 {
534     const char *subject, *from_addr = NULL;
535     const char *in_reply_to, *orig_references, *references;
536
537     /*
538      * Use the below header order for limited headers, "pretty" order
539      * otherwise.
540      */
541     GMimeMessage *reply = g_mime_message_new (limited ? 0 : 1);
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);
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);
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);
599
600     subject = notmuch_message_get_header (message, "subject");
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);
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 do_reply(notmuch_config_t *config,
618                     notmuch_query_t *query,
619                     notmuch_show_params_t *params,
620                     int format,
621                     notmuch_bool_t reply_all)
622 {
623     GMimeMessage *reply;
624     mime_node_t *node;
625     notmuch_messages_t *messages;
626     notmuch_message_t *message;
627     notmuch_status_t status;
628     struct sprinter *sp = NULL;
629
630     if (format == FORMAT_JSON || format == FORMAT_SEXP) {
631         unsigned count;
632
633         status = notmuch_query_count_messages_st (query, &count);
634         if (print_status_query ("notmuch reply", query, status))
635             return 1;
636
637         if (count != 1) {
638             fprintf (stderr, "Error: search term did not match precisely one message (matched %d messages).\n", count);
639             return 1;
640         }
641
642         if (format == FORMAT_JSON)
643             sp = sprinter_json_create (config, stdout);
644         else
645             sp = sprinter_sexp_create (config, stdout);
646     }
647
648     status = notmuch_query_search_messages_st (query, &messages);
649     if (print_status_query ("notmuch reply", query, status))
650         return 1;
651
652     for (;
653          notmuch_messages_valid (messages);
654          notmuch_messages_move_to_next (messages))
655     {
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             format_headers_sprinter (sp, reply, TRUE);
673
674             /* Start the original */
675             sp->map_key (sp, "original");
676             format_part_sprinter (config, sp, node, TRUE, TRUE, FALSE);
677
678             /* End */
679             sp->end (sp);
680         } else {
681             show_reply_headers (reply);
682             if (format == FORMAT_DEFAULT)
683                 format_part_reply (node);
684         }
685
686         g_object_unref (G_OBJECT (reply));
687         talloc_free (node);
688
689         notmuch_message_destroy (message);
690     }
691
692     return 0;
693 }
694
695 int
696 notmuch_reply_command (notmuch_config_t *config, int argc, char *argv[])
697 {
698     notmuch_database_t *notmuch;
699     notmuch_query_t *query;
700     char *query_string;
701     int opt_index;
702     notmuch_show_params_t params = {
703         .part = -1,
704         .crypto = {
705             .verify = FALSE,
706             .decrypt = FALSE,
707             .gpgpath = NULL
708         }
709     };
710     int format = FORMAT_DEFAULT;
711     int reply_all = TRUE;
712
713     notmuch_opt_desc_t options[] = {
714         { NOTMUCH_OPT_KEYWORD, &format, "format", 'f',
715           (notmuch_keyword_t []){ { "default", FORMAT_DEFAULT },
716                                   { "json", FORMAT_JSON },
717                                   { "sexp", FORMAT_SEXP },
718                                   { "headers-only", FORMAT_HEADERS_ONLY },
719                                   { 0, 0 } } },
720         { NOTMUCH_OPT_INT, &notmuch_format_version, "format-version", 0, 0 },
721         { NOTMUCH_OPT_KEYWORD, &reply_all, "reply-to", 'r',
722           (notmuch_keyword_t []){ { "all", TRUE },
723                                   { "sender", FALSE },
724                                   { 0, 0 } } },
725         { NOTMUCH_OPT_BOOLEAN, &params.crypto.decrypt, "decrypt", 'd', 0 },
726         { NOTMUCH_OPT_INHERIT, (void *) &notmuch_shared_options, NULL, 0, 0 },
727         { 0, 0, 0, 0, 0 }
728     };
729
730     opt_index = parse_arguments (argc, argv, options, 1);
731     if (opt_index < 0)
732         return EXIT_FAILURE;
733
734     notmuch_process_shared_options (argv[0]);
735
736     notmuch_exit_if_unsupported_format ();
737
738     query_string = query_string_from_args (config, argc-opt_index, argv+opt_index);
739     if (query_string == NULL) {
740         fprintf (stderr, "Out of memory\n");
741         return EXIT_FAILURE;
742     }
743
744     if (*query_string == '\0') {
745         fprintf (stderr, "Error: notmuch reply requires at least one search term.\n");
746         return EXIT_FAILURE;
747     }
748
749     params.crypto.gpgpath = notmuch_config_get_crypto_gpg_path (config);
750
751     if (notmuch_database_open (notmuch_config_get_database_path (config),
752                                NOTMUCH_DATABASE_MODE_READ_ONLY, &notmuch))
753         return EXIT_FAILURE;
754
755     notmuch_exit_if_unmatched_db_uuid (notmuch);
756
757     query = notmuch_query_create (notmuch, query_string);
758     if (query == NULL) {
759         fprintf (stderr, "Out of memory\n");
760         return EXIT_FAILURE;
761     }
762
763     if (do_reply (config, query, &params, format, reply_all) != 0)
764         return EXIT_FAILURE;
765
766     notmuch_crypto_cleanup (&params.crypto);
767     notmuch_query_destroy (query);
768     notmuch_database_destroy (notmuch);
769
770     return EXIT_SUCCESS;
771 }