]> git.notmuchmail.org Git - notmuch/blob - notmuch-reply.c
lib/cli: replace use of g_mime_message_get_sender
[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), 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         InternetAddressList *recipients;
48         char *recipients_string;
49
50         recipients = g_mime_message_get_recipients (message, GMIME_RECIPIENT_TYPE_TO);
51         recipients_string = internet_address_list_to_string (recipients, 0);
52         g_mime_stream_printf (stream, "> From: %s\n", g_mime_message_get_from_string (message));
53         if (recipients_string)
54             g_mime_stream_printf (stream, "> To: %s\n",
55                                   recipients_string);
56         g_free (recipients_string);
57         recipients = g_mime_message_get_recipients (message, GMIME_RECIPIENT_TYPE_CC);
58         recipients_string = internet_address_list_to_string (recipients, 0);
59         if (recipients_string)
60             g_mime_stream_printf (stream, "> Cc: %s\n",
61                                   recipients_string);
62         g_free (recipients_string);
63         g_mime_stream_printf (stream, "> Subject: %s\n", g_mime_message_get_subject (message));
64         g_mime_stream_printf (stream, "> Date: %s\n", g_mime_message_get_date_string (node, message));
65         g_mime_stream_printf (stream, ">\n");
66     } else if (GMIME_IS_PART (node->part)) {
67         GMimeContentType *content_type = g_mime_object_get_content_type (node->part);
68         GMimeContentDisposition *disposition = g_mime_object_get_content_disposition (node->part);
69
70         if (g_mime_content_type_is_type (content_type, "application", "pgp-encrypted") ||
71             g_mime_content_type_is_type (content_type, "application", "pgp-signature")) {
72             /* Ignore PGP/MIME cruft parts */
73         } else if (g_mime_content_type_is_type (content_type, "text", "*") &&
74                    !g_mime_content_type_is_type (content_type, "text", "html")) {
75             show_text_part_content (node->part, stream, NOTMUCH_SHOW_TEXT_PART_REPLY);
76         } else if (disposition &&
77                    strcasecmp (g_mime_content_disposition_get_disposition (disposition),
78                                GMIME_DISPOSITION_ATTACHMENT) == 0) {
79             const char *filename = g_mime_part_get_filename (GMIME_PART (node->part));
80             g_mime_stream_printf (stream, "Attachment: %s (%s)\n", filename,
81                                   g_mime_content_type_to_string (content_type));
82         } else {
83             g_mime_stream_printf (stream, "Non-text part: %s\n",
84                                   g_mime_content_type_to_string (content_type));
85         }
86     }
87
88     for (i = 0; i < node->nchildren; i++)
89         format_part_reply (stream, mime_node_child (node, i));
90 }
91
92 typedef enum {
93     USER_ADDRESS_IN_STRING,
94     STRING_IN_USER_ADDRESS,
95     STRING_IS_USER_ADDRESS,
96 } address_match_t;
97
98 /* Match given string against given address according to mode. */
99 static notmuch_bool_t
100 match_address (const char *str, const char *address, address_match_t mode)
101 {
102     switch (mode) {
103     case USER_ADDRESS_IN_STRING:
104         return strcasestr (str, address) != NULL;
105     case STRING_IN_USER_ADDRESS:
106         return strcasestr (address, str) != NULL;
107     case STRING_IS_USER_ADDRESS:
108         return strcasecmp (address, str) == 0;
109     }
110
111     return FALSE;
112 }
113
114 /* Match given string against user's configured "primary" and "other"
115  * addresses according to mode. */
116 static const char *
117 address_match (const char *str, notmuch_config_t *config, address_match_t mode)
118 {
119     const char *primary;
120     const char **other;
121     size_t i, other_len;
122
123     if (!str || *str == '\0')
124         return NULL;
125
126     primary = notmuch_config_get_user_primary_email (config);
127     if (match_address (str, primary, mode))
128         return primary;
129
130     other = notmuch_config_get_user_other_email (config, &other_len);
131     for (i = 0; i < other_len; i++) {
132         if (match_address (str, other[i], mode))
133             return other[i];
134     }
135
136     return NULL;
137 }
138
139 /* Does the given string contain an address configured as one of the
140  * user's "primary" or "other" addresses. If so, return the matching
141  * address, NULL otherwise. */
142 static const char *
143 user_address_in_string (const char *str, notmuch_config_t *config)
144 {
145     return address_match (str, config, USER_ADDRESS_IN_STRING);
146 }
147
148 /* Do any of the addresses configured as one of the user's "primary"
149  * or "other" addresses contain the given string. If so, return the
150  * matching address, NULL otherwise. */
151 static const char *
152 string_in_user_address (const char *str, notmuch_config_t *config)
153 {
154     return address_match (str, config, STRING_IN_USER_ADDRESS);
155 }
156
157 /* Is the given address configured as one of the user's "primary" or
158  * "other" addresses. */
159 static notmuch_bool_t
160 address_is_users (const char *address, notmuch_config_t *config)
161 {
162     return address_match (address, config, STRING_IS_USER_ADDRESS) != NULL;
163 }
164
165 /* Scan addresses in 'list'.
166  *
167  * If 'message' is non-NULL, then for each address in 'list' that is
168  * not configured as one of the user's addresses in 'config', add that
169  * address to 'message' as an address of 'type'.
170  *
171  * If 'user_from' is non-NULL and *user_from is NULL, *user_from will
172  * be set to the first address encountered in 'list' that is the
173  * user's address.
174  *
175  * Return the number of addresses added to 'message'. (If 'message' is
176  * NULL, the function returns 0 by definition.)
177  */
178 static unsigned int
179 scan_address_list (InternetAddressList *list,
180                    notmuch_config_t *config,
181                    GMimeMessage *message,
182                    GMimeRecipientType type,
183                    const char **user_from)
184 {
185     InternetAddress *address;
186     int i;
187     unsigned int n = 0;
188
189     if (list == NULL)
190         return 0;
191
192     for (i = 0; i < internet_address_list_length (list); i++) {
193         address = internet_address_list_get_address (list, i);
194         if (INTERNET_ADDRESS_IS_GROUP (address)) {
195             InternetAddressGroup *group;
196             InternetAddressList *group_list;
197
198             group = INTERNET_ADDRESS_GROUP (address);
199             group_list = internet_address_group_get_members (group);
200             n += scan_address_list (group_list, config, message, type, user_from);
201         } else {
202             InternetAddressMailbox *mailbox;
203             const char *name;
204             const char *addr;
205
206             mailbox = INTERNET_ADDRESS_MAILBOX (address);
207
208             name = internet_address_get_name (address);
209             addr = internet_address_mailbox_get_addr (mailbox);
210
211             if (address_is_users (addr, config)) {
212                 if (user_from && *user_from == NULL)
213                     *user_from = addr;
214             } else if (message) {
215                 g_mime_message_add_recipient (message, type, name, addr);
216                 n++;
217             }
218         }
219     }
220
221     return n;
222 }
223
224 /* Does the address in the Reply-To header of 'message' already appear
225  * in either the 'To' or 'Cc' header of the message?
226  */
227 static notmuch_bool_t
228 reply_to_header_is_redundant (GMimeMessage *message,
229                               InternetAddressList *reply_to_list)
230 {
231     const char *addr, *reply_to;
232     InternetAddress *address;
233     InternetAddressMailbox *mailbox;
234     InternetAddressList *recipients;
235     notmuch_bool_t ret = FALSE;
236     int i;
237
238     if (reply_to_list == NULL ||
239         internet_address_list_length (reply_to_list) != 1)
240         return 0;
241
242     address = internet_address_list_get_address (reply_to_list, 0);
243     if (INTERNET_ADDRESS_IS_GROUP (address))
244         return 0;
245
246     mailbox = INTERNET_ADDRESS_MAILBOX (address);
247     reply_to = internet_address_mailbox_get_addr (mailbox);
248
249     recipients = g_mime_message_get_all_recipients (message);
250
251     for (i = 0; i < internet_address_list_length (recipients); i++) {
252         address = internet_address_list_get_address (recipients, i);
253         if (INTERNET_ADDRESS_IS_GROUP (address))
254             continue;
255
256         mailbox = INTERNET_ADDRESS_MAILBOX (address);
257         addr = internet_address_mailbox_get_addr (mailbox);
258         if (strcmp (addr, reply_to) == 0) {
259             ret = TRUE;
260             break;
261         }
262     }
263
264     g_object_unref (G_OBJECT (recipients));
265
266     return ret;
267 }
268
269 static InternetAddressList *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         g_object_unref (G_OBJECT (reply_to_list));
293     }
294
295     return g_mime_message_get_from (message);
296 }
297
298 static InternetAddressList *get_to(GMimeMessage *message)
299 {
300     return g_mime_message_get_recipients (message, GMIME_RECIPIENT_TYPE_TO);
301 }
302
303 static InternetAddressList *get_cc(GMimeMessage *message)
304 {
305     return g_mime_message_get_recipients (message, GMIME_RECIPIENT_TYPE_CC);
306 }
307
308 static InternetAddressList *get_bcc(GMimeMessage *message)
309 {
310     return g_mime_message_get_recipients (message, GMIME_RECIPIENT_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                              notmuch_bool_t reply_all)
330 {
331
332     /* There is a memory leak here with gmime-2.6 because get_sender
333      * returns a newly allocated list, while the others return
334      * references to libgmime owned data. This leak will be fixed with
335      * the transition to gmime-3.0.
336      */
337     struct {
338         InternetAddressList * (*get_header)(GMimeMessage *message);
339         GMimeRecipientType recipient_type;
340     } reply_to_map[] = {
341         { get_sender,   GMIME_RECIPIENT_TYPE_TO },
342         { get_to,       GMIME_RECIPIENT_TYPE_TO },
343         { get_cc,       GMIME_RECIPIENT_TYPE_CC },
344         { get_bcc,      GMIME_RECIPIENT_TYPE_BCC },
345     };
346     const char *from_addr = NULL;
347     unsigned int i;
348     unsigned int n = 0;
349
350     for (i = 0; i < ARRAY_SIZE (reply_to_map); i++) {
351         InternetAddressList *recipients;
352
353         recipients = reply_to_map[i].get_header (message);
354
355         n += scan_address_list (recipients, config, reply,
356                                 reply_to_map[i].recipient_type, &from_addr);
357
358         if (!reply_all && n) {
359             /* Stop adding new recipients in reply-to-sender mode if
360              * we have added some recipient(s) above.
361              *
362              * This also handles the case of user replying to his own
363              * message, where reply-to/from is not a recipient. In
364              * this case there may be more than one recipient even if
365              * not replying to all.
366              */
367             reply = NULL;
368
369             /* From address and some recipients are enough, bail out. */
370             if (from_addr)
371                 break;
372         }
373     }
374
375     return from_addr;
376 }
377
378 /*
379  * Look for the user's address in " for <email@add.res>" in the
380  * received headers.
381  *
382  * Return the address that was found, if any, and NULL otherwise.
383  */
384 static const char *
385 guess_from_in_received_for (notmuch_config_t *config, const char *received)
386 {
387     const char *ptr;
388
389     ptr = strstr (received, " for ");
390     if (! ptr)
391         return NULL;
392
393     return user_address_in_string (ptr, config);
394 }
395
396 /*
397  * Parse all the " by MTA ..." parts in received headers to guess the
398  * email address that this was originally delivered to.
399  *
400  * Extract just the MTA here by removing leading whitespace and
401  * assuming that the MTA name ends at the next whitespace. Test for
402  * *(by+4) to be non-'\0' to make sure there's something there at all
403  * - and then assume that the first whitespace delimited token that
404  * follows is the receiving system in this step of the receive chain.
405  *
406  * Return the address that was found, if any, and NULL otherwise.
407  */
408 static const char *
409 guess_from_in_received_by (notmuch_config_t *config, const char *received)
410 {
411     const char *addr;
412     const char *by = received;
413     char *domain, *tld, *mta, *ptr, *token;
414
415     while ((by = strstr (by, " by ")) != NULL) {
416         by += 4;
417         if (*by == '\0')
418             break;
419         mta = xstrdup (by);
420         token = strtok(mta," \t");
421         if (token == NULL) {
422             free (mta);
423             break;
424         }
425         /*
426          * Now extract the last two components of the MTA host name as
427          * domain and tld.
428          */
429         domain = tld = NULL;
430         while ((ptr = strsep (&token, ". \t")) != NULL) {
431             if (*ptr == '\0')
432                 continue;
433             domain = tld;
434             tld = ptr;
435         }
436
437         if (domain) {
438             /*
439              * Recombine domain and tld and look for it among the
440              * configured email addresses. This time we have a known
441              * domain name and nothing else - so the test is the other
442              * way around: we check if this is a substring of one of
443              * the email addresses.
444              */
445             *(tld - 1) = '.';
446
447             addr = string_in_user_address (domain, config);
448             if (addr) {
449                 free (mta);
450                 return addr;
451             }
452         }
453         free (mta);
454     }
455
456     return NULL;
457 }
458
459 /*
460  * Get the concatenated Received: headers and search from the front
461  * (last Received: header added) and try to extract from them
462  * indications to which email address this message was delivered.
463  *
464  * The Received: header is special in our get_header function and is
465  * always concatenated.
466  *
467  * Return the address that was found, if any, and NULL otherwise.
468  */
469 static const char *
470 guess_from_in_received_headers (notmuch_config_t *config,
471                                 notmuch_message_t *message)
472 {
473     const char *received, *addr;
474     char *sanitized;
475
476     received = notmuch_message_get_header (message, "received");
477     if (! received)
478         return NULL;
479
480     sanitized = sanitize_string (NULL, received);
481     if (! sanitized)
482         return NULL;
483
484     addr = guess_from_in_received_for (config, sanitized);
485     if (! addr)
486         addr = guess_from_in_received_by (config, sanitized);
487
488     talloc_free (sanitized);
489
490     return addr;
491 }
492
493 /*
494  * Try to find user's email address in one of the extra To-like
495  * headers: Envelope-To, X-Original-To, and Delivered-To (searched in
496  * that order).
497  *
498  * Return the address that was found, if any, and NULL otherwise.
499  */
500 static const char *
501 get_from_in_to_headers (notmuch_config_t *config, notmuch_message_t *message)
502 {
503     size_t i;
504     const char *tohdr, *addr;
505     const char *to_headers[] = {
506         "Envelope-to",
507         "X-Original-To",
508         "Delivered-To",
509     };
510
511     for (i = 0; i < ARRAY_SIZE (to_headers); i++) {
512         tohdr = notmuch_message_get_header (message, to_headers[i]);
513
514         /* Note: tohdr potentially contains a list of email addresses. */
515         addr = user_address_in_string (tohdr, config);
516         if (addr)
517             return addr;
518     }
519
520     return NULL;
521 }
522
523 static GMimeMessage *
524 create_reply_message(void *ctx,
525                      notmuch_config_t *config,
526                      notmuch_message_t *message,
527                      GMimeMessage *mime_message,
528                      notmuch_bool_t reply_all,
529                      notmuch_bool_t limited)
530 {
531     const char *subject, *from_addr = NULL;
532     const char *in_reply_to, *orig_references, *references;
533
534     /*
535      * Use the below header order for limited headers, "pretty" order
536      * otherwise.
537      */
538     GMimeMessage *reply = g_mime_message_new (limited ? 0 : 1);
539     if (reply == NULL) {
540         fprintf (stderr, "Out of memory\n");
541         return NULL;
542     }
543
544     in_reply_to = talloc_asprintf (ctx, "<%s>",
545                                    notmuch_message_get_message_id (message));
546
547     g_mime_object_set_header (GMIME_OBJECT (reply), "In-Reply-To", in_reply_to);
548
549     orig_references = notmuch_message_get_header (message, "references");
550     if (orig_references && *orig_references)
551         references = talloc_asprintf (ctx, "%s %s", orig_references,
552                                       in_reply_to);
553     else
554         references = talloc_strdup (ctx, in_reply_to);
555
556     g_mime_object_set_header (GMIME_OBJECT (reply), "References", references);
557
558     from_addr = add_recipients_from_message (reply, config,
559                                              mime_message, reply_all);
560
561     /* The above is all that is needed for limited headers. */
562     if (limited)
563         return reply;
564
565     /*
566      * Sadly, there is no standard way to find out to which email
567      * address a mail was delivered - what is in the headers depends
568      * on the MTAs used along the way.
569      *
570      * If none of the user's email addresses are in the To: or Cc:
571      * headers, we try a number of heuristics which hopefully will
572      * answer this question.
573      *
574      * First, check for Envelope-To:, X-Original-To:, and
575      * Delivered-To: headers.
576      */
577     if (from_addr == NULL)
578         from_addr = get_from_in_to_headers (config, message);
579
580     /*
581      * Check for a (for <email@add.res>) clause in Received: headers,
582      * and the domain part of known email addresses in the 'by' part
583      * of Received: headers
584      */
585     if (from_addr == NULL)
586         from_addr = guess_from_in_received_headers (config, message);
587
588     /* Default to user's primary address. */
589     if (from_addr == NULL)
590         from_addr = notmuch_config_get_user_primary_email (config);
591
592     from_addr = talloc_asprintf (ctx, "%s <%s>",
593                                  notmuch_config_get_user_name (config),
594                                  from_addr);
595     g_mime_object_set_header (GMIME_OBJECT (reply), "From", from_addr);
596
597     subject = notmuch_message_get_header (message, "subject");
598     if (subject) {
599         if (strncasecmp (subject, "Re:", 3))
600             subject = talloc_asprintf (ctx, "Re: %s", subject);
601         g_mime_message_set_subject (reply, subject);
602     }
603
604     return reply;
605 }
606
607 enum {
608     FORMAT_DEFAULT,
609     FORMAT_JSON,
610     FORMAT_SEXP,
611     FORMAT_HEADERS_ONLY,
612 };
613
614 static int do_reply(notmuch_config_t *config,
615                     notmuch_query_t *query,
616                     notmuch_show_params_t *params,
617                     int format,
618                     notmuch_bool_t reply_all)
619 {
620     GMimeMessage *reply;
621     mime_node_t *node;
622     notmuch_messages_t *messages;
623     notmuch_message_t *message;
624     notmuch_status_t status;
625     struct sprinter *sp = NULL;
626
627     if (format == FORMAT_JSON || format == FORMAT_SEXP) {
628         unsigned count;
629
630         status = notmuch_query_count_messages (query, &count);
631         if (print_status_query ("notmuch reply", query, status))
632             return 1;
633
634         if (count != 1) {
635             fprintf (stderr, "Error: search term did not match precisely one message (matched %u messages).\n", count);
636             return 1;
637         }
638
639         if (format == FORMAT_JSON)
640             sp = sprinter_json_create (config, stdout);
641         else
642             sp = sprinter_sexp_create (config, stdout);
643     }
644
645     status = notmuch_query_search_messages (query, &messages);
646     if (print_status_query ("notmuch reply", query, status))
647         return 1;
648
649     for (;
650          notmuch_messages_valid (messages);
651          notmuch_messages_move_to_next (messages))
652     {
653         message = notmuch_messages_get (messages);
654
655         if (mime_node_open (config, message, &params->crypto, &node))
656             return 1;
657
658         reply = create_reply_message (config, config, message,
659                                       GMIME_MESSAGE (node->part), reply_all,
660                                       format == FORMAT_HEADERS_ONLY);
661         if (!reply)
662             return 1;
663
664         if (format == FORMAT_JSON || format == FORMAT_SEXP) {
665             sp->begin_map (sp);
666
667             /* The headers of the reply message we've created */
668             sp->map_key (sp, "reply-headers");
669             format_headers_sprinter (sp, reply, TRUE);
670
671             /* Start the original */
672             sp->map_key (sp, "original");
673             format_part_sprinter (config, sp, node, TRUE, TRUE, FALSE);
674
675             /* End */
676             sp->end (sp);
677         } else {
678             GMimeStream *stream_stdout = stream_stdout = g_mime_stream_stdout_new ();
679             if (stream_stdout) {
680                 show_reply_headers (stream_stdout, reply);
681                 if (format == FORMAT_DEFAULT)
682                     format_part_reply (stream_stdout, node);
683             }
684             g_mime_stream_flush (stream_stdout);
685             g_object_unref(stream_stdout);
686         }
687
688         g_object_unref (G_OBJECT (reply));
689         talloc_free (node);
690
691         notmuch_message_destroy (message);
692     }
693
694     return 0;
695 }
696
697 int
698 notmuch_reply_command (notmuch_config_t *config, int argc, char *argv[])
699 {
700     notmuch_database_t *notmuch;
701     notmuch_query_t *query;
702     char *query_string;
703     int opt_index;
704     notmuch_show_params_t params = {
705         .part = -1,
706     };
707     int format = FORMAT_DEFAULT;
708     int reply_all = TRUE;
709
710     notmuch_opt_desc_t options[] = {
711         { NOTMUCH_OPT_KEYWORD, &format, "format", 'f',
712           (notmuch_keyword_t []){ { "default", FORMAT_DEFAULT },
713                                   { "json", FORMAT_JSON },
714                                   { "sexp", FORMAT_SEXP },
715                                   { "headers-only", FORMAT_HEADERS_ONLY },
716                                   { 0, 0 } } },
717         { NOTMUCH_OPT_INT, &notmuch_format_version, "format-version", 0, 0 },
718         { NOTMUCH_OPT_KEYWORD, &reply_all, "reply-to", 'r',
719           (notmuch_keyword_t []){ { "all", TRUE },
720                                   { "sender", FALSE },
721                                   { 0, 0 } } },
722         { NOTMUCH_OPT_BOOLEAN, &params.crypto.decrypt, "decrypt", 'd', 0 },
723         { NOTMUCH_OPT_INHERIT, (void *) &notmuch_shared_options, NULL, 0, 0 },
724         { 0, 0, 0, 0, 0 }
725     };
726
727     opt_index = parse_arguments (argc, argv, options, 1);
728     if (opt_index < 0)
729         return EXIT_FAILURE;
730
731     notmuch_process_shared_options (argv[0]);
732
733     notmuch_exit_if_unsupported_format ();
734
735     query_string = query_string_from_args (config, argc-opt_index, argv+opt_index);
736     if (query_string == NULL) {
737         fprintf (stderr, "Out of memory\n");
738         return EXIT_FAILURE;
739     }
740
741     if (*query_string == '\0') {
742         fprintf (stderr, "Error: notmuch reply requires at least one search term.\n");
743         return EXIT_FAILURE;
744     }
745
746     params.crypto.gpgpath = notmuch_config_get_crypto_gpg_path (config);
747
748     if (notmuch_database_open (notmuch_config_get_database_path (config),
749                                NOTMUCH_DATABASE_MODE_READ_ONLY, &notmuch))
750         return EXIT_FAILURE;
751
752     notmuch_exit_if_unmatched_db_uuid (notmuch);
753
754     query = notmuch_query_create (notmuch, query_string);
755     if (query == NULL) {
756         fprintf (stderr, "Out of memory\n");
757         return EXIT_FAILURE;
758     }
759
760     if (do_reply (config, query, &params, format, reply_all) != 0)
761         return EXIT_FAILURE;
762
763     notmuch_crypto_cleanup (&params.crypto);
764     notmuch_query_destroy (query);
765     notmuch_database_destroy (notmuch);
766
767     return EXIT_SUCCESS;
768 }