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