]> git.notmuchmail.org Git - notmuch/blob - notmuch-reply.c
reply: Convert default reply format to self-recursive style
[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 http://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 "gmime-filter-headers.h"
25
26 static void
27 show_reply_headers (GMimeMessage *message)
28 {
29     GMimeStream *stream_stdout = NULL, *stream_filter = NULL;
30
31     stream_stdout = g_mime_stream_file_new (stdout);
32     if (stream_stdout) {
33         g_mime_stream_file_set_owner (GMIME_STREAM_FILE (stream_stdout), FALSE);
34         stream_filter = g_mime_stream_filter_new(stream_stdout);
35         if (stream_filter) {
36                 g_mime_stream_filter_add(GMIME_STREAM_FILTER(stream_filter),
37                                          g_mime_filter_headers_new());
38                 g_mime_object_write_to_stream(GMIME_OBJECT(message), stream_filter);
39                 g_object_unref(stream_filter);
40         }
41         g_object_unref(stream_stdout);
42     }
43 }
44
45 static void
46 format_part_reply (mime_node_t *node)
47 {
48     int i;
49
50     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                    strcmp (disposition->disposition, GMIME_DISPOSITION_ATTACHMENT) == 0) {
84             const char *filename = g_mime_part_get_filename (GMIME_PART (node->part));
85             printf ("Attachment: %s (%s)\n", filename,
86                     g_mime_content_type_to_string (content_type));
87         } else {
88             printf ("Non-text part: %s\n",
89                     g_mime_content_type_to_string (content_type));
90         }
91     }
92
93     for (i = 0; i < node->nchildren; i++)
94         format_part_reply (mime_node_child (node, i));
95 }
96
97 /* Is the given address configured as one of the user's "personal" or
98  * "other" addresses. */
99 static int
100 address_is_users (const char *address, notmuch_config_t *config)
101 {
102     const char *primary;
103     const char **other;
104     size_t i, other_len;
105
106     primary = notmuch_config_get_user_primary_email (config);
107     if (strcasecmp (primary, address) == 0)
108         return 1;
109
110     other = notmuch_config_get_user_other_email (config, &other_len);
111     for (i = 0; i < other_len; i++)
112         if (strcasecmp (other[i], address) == 0)
113             return 1;
114
115     return 0;
116 }
117
118 /* Scan addresses in 'list'.
119  *
120  * If 'message' is non-NULL, then for each address in 'list' that is
121  * not configured as one of the user's addresses in 'config', add that
122  * address to 'message' as an address of 'type'.
123  *
124  * If 'user_from' is non-NULL and *user_from is NULL, *user_from will
125  * be set to the first address encountered in 'list' that is the
126  * user's address.
127  *
128  * Return the number of addresses added to 'message'. (If 'message' is
129  * NULL, the function returns 0 by definition.)
130  */
131 static unsigned int
132 scan_address_list (InternetAddressList *list,
133                    notmuch_config_t *config,
134                    GMimeMessage *message,
135                    GMimeRecipientType type,
136                    const char **user_from)
137 {
138     InternetAddress *address;
139     int i;
140     unsigned int n = 0;
141
142     for (i = 0; i < internet_address_list_length (list); i++) {
143         address = internet_address_list_get_address (list, i);
144         if (INTERNET_ADDRESS_IS_GROUP (address)) {
145             InternetAddressGroup *group;
146             InternetAddressList *group_list;
147
148             group = INTERNET_ADDRESS_GROUP (address);
149             group_list = internet_address_group_get_members (group);
150             if (group_list == NULL)
151                 continue;
152
153             n += scan_address_list (group_list, config, message, type, user_from);
154         } else {
155             InternetAddressMailbox *mailbox;
156             const char *name;
157             const char *addr;
158
159             mailbox = INTERNET_ADDRESS_MAILBOX (address);
160
161             name = internet_address_get_name (address);
162             addr = internet_address_mailbox_get_addr (mailbox);
163
164             if (address_is_users (addr, config)) {
165                 if (user_from && *user_from == NULL)
166                     *user_from = addr;
167             } else if (message) {
168                 g_mime_message_add_recipient (message, type, name, addr);
169                 n++;
170             }
171         }
172     }
173
174     return n;
175 }
176
177 /* Scan addresses in 'recipients'.
178  *
179  * See the documentation of scan_address_list() above. This function
180  * does exactly the same, but converts 'recipients' to an
181  * InternetAddressList first.
182  */
183 static unsigned int
184 scan_address_string (const char *recipients,
185                      notmuch_config_t *config,
186                      GMimeMessage *message,
187                      GMimeRecipientType type,
188                      const char **user_from)
189 {
190     InternetAddressList *list;
191
192     if (recipients == NULL)
193         return 0;
194
195     list = internet_address_list_parse_string (recipients);
196     if (list == NULL)
197         return 0;
198
199     return scan_address_list (list, config, message, type, user_from);
200 }
201
202 /* Does the address in the Reply-To header of 'message' already appear
203  * in either the 'To' or 'Cc' header of the message?
204  */
205 static int
206 reply_to_header_is_redundant (notmuch_message_t *message)
207 {
208     const char *reply_to, *to, *cc, *addr;
209     InternetAddressList *list;
210     InternetAddress *address;
211     InternetAddressMailbox *mailbox;
212
213     reply_to = notmuch_message_get_header (message, "reply-to");
214     if (reply_to == NULL || *reply_to == '\0')
215         return 0;
216
217     list = internet_address_list_parse_string (reply_to);
218
219     if (internet_address_list_length (list) != 1)
220         return 0;
221
222     address = internet_address_list_get_address (list, 0);
223     if (INTERNET_ADDRESS_IS_GROUP (address))
224         return 0;
225
226     mailbox = INTERNET_ADDRESS_MAILBOX (address);
227     addr = internet_address_mailbox_get_addr (mailbox);
228
229     to = notmuch_message_get_header (message, "to");
230     cc = notmuch_message_get_header (message, "cc");
231
232     if ((to && strstr (to, addr) != 0) ||
233         (cc && strstr (cc, addr) != 0))
234     {
235         return 1;
236     }
237
238     return 0;
239 }
240
241 /* Augment the recipients of 'reply' from the "Reply-to:", "From:",
242  * "To:", "Cc:", and "Bcc:" headers of 'message'.
243  *
244  * If 'reply_all' is true, use sender and all recipients, otherwise
245  * scan the headers for the first that contains something other than
246  * the user's addresses and add the recipients from this header
247  * (typically this would be reply-to-sender, but also handles reply to
248  * user's own message in a sensible way).
249  *
250  * If any of the user's addresses were found in these headers, the
251  * first of these returned, otherwise NULL is returned.
252  */
253 static const char *
254 add_recipients_from_message (GMimeMessage *reply,
255                              notmuch_config_t *config,
256                              notmuch_message_t *message,
257                              notmuch_bool_t reply_all)
258 {
259     struct {
260         const char *header;
261         const char *fallback;
262         GMimeRecipientType recipient_type;
263     } reply_to_map[] = {
264         { "reply-to", "from", GMIME_RECIPIENT_TYPE_TO  },
265         { "to",         NULL, GMIME_RECIPIENT_TYPE_TO  },
266         { "cc",         NULL, GMIME_RECIPIENT_TYPE_CC  },
267         { "bcc",        NULL, GMIME_RECIPIENT_TYPE_BCC }
268     };
269     const char *from_addr = NULL;
270     unsigned int i;
271     unsigned int n = 0;
272
273     /* Some mailing lists munge the Reply-To header despite it being A Bad
274      * Thing, see http://www.unicom.com/pw/reply-to-harmful.html
275      *
276      * The munging is easy to detect, because it results in a
277      * redundant reply-to header, (with an address that already exists
278      * in either To or Cc). So in this case, we ignore the Reply-To
279      * field and use the From header. This ensures the original sender
280      * will get the reply even if not subscribed to the list. Note
281      * that the address in the Reply-To header will always appear in
282      * the reply.
283      */
284     if (reply_to_header_is_redundant (message)) {
285         reply_to_map[0].header = "from";
286         reply_to_map[0].fallback = NULL;
287     }
288
289     for (i = 0; i < ARRAY_SIZE (reply_to_map); i++) {
290         const char *recipients;
291
292         recipients = notmuch_message_get_header (message,
293                                                  reply_to_map[i].header);
294         if ((recipients == NULL || recipients[0] == '\0') && reply_to_map[i].fallback)
295             recipients = notmuch_message_get_header (message,
296                                                      reply_to_map[i].fallback);
297
298         n += scan_address_string (recipients, config, reply,
299                                   reply_to_map[i].recipient_type, &from_addr);
300
301         if (!reply_all && n) {
302             /* Stop adding new recipients in reply-to-sender mode if
303              * we have added some recipient(s) above.
304              *
305              * This also handles the case of user replying to his own
306              * message, where reply-to/from is not a recipient. In
307              * this case there may be more than one recipient even if
308              * not replying to all.
309              */
310             reply = NULL;
311
312             /* From address and some recipients are enough, bail out. */
313             if (from_addr)
314                 break;
315         }
316     }
317
318     return from_addr;
319 }
320
321 static const char *
322 guess_from_received_header (notmuch_config_t *config, notmuch_message_t *message)
323 {
324     const char *received,*primary,*by;
325     const char **other;
326     char *tohdr;
327     char *mta,*ptr,*token;
328     char *domain=NULL;
329     char *tld=NULL;
330     const char *delim=". \t";
331     size_t i,j,other_len;
332
333     const char *to_headers[] = {"Envelope-to", "X-Original-To"};
334
335     primary = notmuch_config_get_user_primary_email (config);
336     other = notmuch_config_get_user_other_email (config, &other_len);
337
338     /* sadly, there is no standard way to find out to which email
339      * address a mail was delivered - what is in the headers depends
340      * on the MTAs used along the way. So we are trying a number of
341      * heuristics which hopefully will answer this question.
342
343      * We only got here if none of the users email addresses are in
344      * the To: or Cc: header. From here we try the following in order:
345      * 1) check for an Envelope-to: header
346      * 2) check for an X-Original-To: header
347      * 3) check for a (for <email@add.res>) clause in Received: headers
348      * 4) check for the domain part of known email addresses in the
349      *    'by' part of Received headers
350      * If none of these work, we give up and return NULL
351      */
352     for (i = 0; i < sizeof(to_headers)/sizeof(*to_headers); i++) {
353         tohdr = xstrdup(notmuch_message_get_header (message, to_headers[i]));
354         if (tohdr && *tohdr) {
355             /* tohdr is potentialy a list of email addresses, so here we
356              * check if one of the email addresses is a substring of tohdr
357              */
358             if (strcasestr(tohdr, primary)) {
359                 free(tohdr);
360                 return primary;
361             }
362             for (j = 0; j < other_len; j++)
363                 if (strcasestr (tohdr, other[j])) {
364                     free(tohdr);
365                     return other[j];
366                 }
367             free(tohdr);
368         }
369     }
370
371     /* We get the concatenated Received: headers and search from the
372      * front (last Received: header added) and try to extract from
373      * them indications to which email address this message was
374      * delivered.
375      * The Received: header is special in our get_header function
376      * and is always concatenated.
377      */
378     received = notmuch_message_get_header (message, "received");
379     if (received == NULL)
380         return NULL;
381
382     /* First we look for a " for <email@add.res>" in the received
383      * header
384      */
385     ptr = strstr (received, " for ");
386     if (ptr) {
387         /* the text following is potentialy a list of email addresses,
388          * so again we check if one of the email addresses is a
389          * substring of ptr
390          */
391         if (strcasestr(ptr, primary)) {
392             return primary;
393         }
394         for (i = 0; i < other_len; i++)
395             if (strcasestr (ptr, other[i])) {
396                 return other[i];
397             }
398     }
399     /* Finally, we parse all the " by MTA ..." headers to guess the
400      * email address that this was originally delivered to.
401      * We extract just the MTA here by removing leading whitespace and
402      * assuming that the MTA name ends at the next whitespace.
403      * We test for *(by+4) to be non-'\0' to make sure there's
404      * something there at all - and then assume that the first
405      * whitespace delimited token that follows is the receiving
406      * system in this step of the receive chain
407      */
408     by = received;
409     while((by = strstr (by, " by ")) != NULL) {
410         by += 4;
411         if (*by == '\0')
412             break;
413         mta = xstrdup (by);
414         token = strtok(mta," \t");
415         if (token == NULL) {
416             free (mta);
417             break;
418         }
419         /* Now extract the last two components of the MTA host name
420          * as domain and tld.
421          */
422         domain = tld = NULL;
423         while ((ptr = strsep (&token, delim)) != NULL) {
424             if (*ptr == '\0')
425                 continue;
426             domain = tld;
427             tld = ptr;
428         }
429
430         if (domain) {
431             /* Recombine domain and tld and look for it among the configured
432              * email addresses.
433              * This time we have a known domain name and nothing else - so
434              * the test is the other way around: we check if this is a
435              * substring of one of the email addresses.
436              */
437             *(tld-1) = '.';
438
439             if (strcasestr(primary, domain)) {
440                 free(mta);
441                 return primary;
442             }
443             for (i = 0; i < other_len; i++)
444                 if (strcasestr (other[i],domain)) {
445                     free(mta);
446                     return other[i];
447                 }
448         }
449         free (mta);
450     }
451
452     return NULL;
453 }
454
455 static GMimeMessage *
456 create_reply_message(void *ctx,
457                      notmuch_config_t *config,
458                      notmuch_message_t *message,
459                      notmuch_bool_t reply_all)
460 {
461     const char *subject, *from_addr = NULL;
462     const char *in_reply_to, *orig_references, *references;
463
464     /* The 1 means we want headers in a "pretty" order. */
465     GMimeMessage *reply = g_mime_message_new (1);
466     if (reply == NULL) {
467         fprintf (stderr, "Out of memory\n");
468         return NULL;
469     }
470
471     subject = notmuch_message_get_header (message, "subject");
472     if (subject) {
473         if (strncasecmp (subject, "Re:", 3))
474             subject = talloc_asprintf (ctx, "Re: %s", subject);
475         g_mime_message_set_subject (reply, subject);
476     }
477
478     from_addr = add_recipients_from_message (reply, config,
479                                              message, reply_all);
480
481     if (from_addr == NULL)
482         from_addr = guess_from_received_header (config, message);
483
484     if (from_addr == NULL)
485         from_addr = notmuch_config_get_user_primary_email (config);
486
487     from_addr = talloc_asprintf (ctx, "%s <%s>",
488                                  notmuch_config_get_user_name (config),
489                                  from_addr);
490     g_mime_object_set_header (GMIME_OBJECT (reply),
491                               "From", from_addr);
492
493     in_reply_to = talloc_asprintf (ctx, "<%s>",
494                                    notmuch_message_get_message_id (message));
495
496     g_mime_object_set_header (GMIME_OBJECT (reply),
497                               "In-Reply-To", in_reply_to);
498
499     orig_references = notmuch_message_get_header (message, "references");
500     references = talloc_asprintf (ctx, "%s%s%s",
501                                   orig_references ? orig_references : "",
502                                   orig_references ? " " : "",
503                                   in_reply_to);
504     g_mime_object_set_header (GMIME_OBJECT (reply),
505                               "References", references);
506
507     return reply;
508 }
509
510 static int
511 notmuch_reply_format_default(void *ctx,
512                              notmuch_config_t *config,
513                              notmuch_query_t *query,
514                              notmuch_show_params_t *params,
515                              notmuch_bool_t reply_all)
516 {
517     GMimeMessage *reply;
518     notmuch_messages_t *messages;
519     notmuch_message_t *message;
520     mime_node_t *root;
521
522     for (messages = notmuch_query_search_messages (query);
523          notmuch_messages_valid (messages);
524          notmuch_messages_move_to_next (messages))
525     {
526         message = notmuch_messages_get (messages);
527
528         reply = create_reply_message (ctx, config, message, reply_all);
529
530         /* If reply creation failed, we're out of memory, so don't
531          * bother trying any more messages.
532          */
533         if (!reply) {
534             notmuch_message_destroy (message);
535             return 1;
536         }
537
538         show_reply_headers (reply);
539
540         g_object_unref (G_OBJECT (reply));
541         reply = NULL;
542
543         printf ("On %s, %s wrote:\n",
544                 notmuch_message_get_header (message, "date"),
545                 notmuch_message_get_header (message, "from"));
546
547         if (mime_node_open (ctx, message, params->cryptoctx, params->decrypt,
548                             &root) == NOTMUCH_STATUS_SUCCESS) {
549             format_part_reply (mime_node_child (root, 0));
550             talloc_free (root);
551         }
552
553         notmuch_message_destroy (message);
554     }
555     return 0;
556 }
557
558 static int
559 notmuch_reply_format_json(void *ctx,
560                           notmuch_config_t *config,
561                           notmuch_query_t *query,
562                           notmuch_show_params_t *params,
563                           notmuch_bool_t reply_all)
564 {
565     GMimeMessage *reply;
566     notmuch_messages_t *messages;
567     notmuch_message_t *message;
568     mime_node_t *node;
569
570     if (notmuch_query_count_messages (query) != 1) {
571         fprintf (stderr, "Error: search term did not match precisely one message.\n");
572         return 1;
573     }
574
575     messages = notmuch_query_search_messages (query);
576     message = notmuch_messages_get (messages);
577     if (mime_node_open (ctx, message, params->cryptoctx, params->decrypt,
578                         &node) != NOTMUCH_STATUS_SUCCESS)
579         return 1;
580
581     reply = create_reply_message (ctx, config, message, reply_all);
582     if (!reply)
583         return 1;
584
585     /* The headers of the reply message we've created */
586     printf ("{\"reply-headers\": ");
587     format_headers_json (ctx, reply, TRUE);
588     g_object_unref (G_OBJECT (reply));
589     reply = NULL;
590
591     /* Start the original */
592     printf (", \"original\": ");
593
594     format_part_json (ctx, node, TRUE);
595
596     /* End */
597     printf ("}\n");
598     notmuch_message_destroy (message);
599
600     return 0;
601 }
602
603 /* This format is currently tuned for a git send-email --notmuch hook */
604 static int
605 notmuch_reply_format_headers_only(void *ctx,
606                                   notmuch_config_t *config,
607                                   notmuch_query_t *query,
608                                   unused (notmuch_show_params_t *params),
609                                   notmuch_bool_t reply_all)
610 {
611     GMimeMessage *reply;
612     notmuch_messages_t *messages;
613     notmuch_message_t *message;
614     const char *in_reply_to, *orig_references, *references;
615     char *reply_headers;
616
617     for (messages = notmuch_query_search_messages (query);
618          notmuch_messages_valid (messages);
619          notmuch_messages_move_to_next (messages))
620     {
621         message = notmuch_messages_get (messages);
622
623         /* The 0 means we do not want headers in a "pretty" order. */
624         reply = g_mime_message_new (0);
625         if (reply == NULL) {
626             fprintf (stderr, "Out of memory\n");
627             return 1;
628         }
629
630         in_reply_to = talloc_asprintf (ctx, "<%s>",
631                              notmuch_message_get_message_id (message));
632
633         g_mime_object_set_header (GMIME_OBJECT (reply),
634                                   "In-Reply-To", in_reply_to);
635
636
637         orig_references = notmuch_message_get_header (message, "references");
638
639         /* We print In-Reply-To followed by References because git format-patch treats them
640          * specially.  Git does not interpret the other headers specially
641          */
642         references = talloc_asprintf (ctx, "%s%s%s",
643                                       orig_references ? orig_references : "",
644                                       orig_references ? " " : "",
645                                       in_reply_to);
646         g_mime_object_set_header (GMIME_OBJECT (reply),
647                                   "References", references);
648
649         (void)add_recipients_from_message (reply, config, message, reply_all);
650
651         reply_headers = g_mime_object_to_string (GMIME_OBJECT (reply));
652         printf ("%s", reply_headers);
653         free (reply_headers);
654
655         g_object_unref (G_OBJECT (reply));
656         reply = NULL;
657
658         notmuch_message_destroy (message);
659     }
660     return 0;
661 }
662
663 enum {
664     FORMAT_DEFAULT,
665     FORMAT_JSON,
666     FORMAT_HEADERS_ONLY,
667 };
668
669 int
670 notmuch_reply_command (void *ctx, int argc, char *argv[])
671 {
672     notmuch_config_t *config;
673     notmuch_database_t *notmuch;
674     notmuch_query_t *query;
675     char *query_string;
676     int opt_index, ret = 0;
677     int (*reply_format_func)(void *ctx, notmuch_config_t *config, notmuch_query_t *query, notmuch_show_params_t *params, notmuch_bool_t reply_all);
678     notmuch_show_params_t params = { .part = -1 };
679     int format = FORMAT_DEFAULT;
680     int reply_all = TRUE;
681
682     notmuch_opt_desc_t options[] = {
683         { NOTMUCH_OPT_KEYWORD, &format, "format", 'f',
684           (notmuch_keyword_t []){ { "default", FORMAT_DEFAULT },
685                                   { "json", FORMAT_JSON },
686                                   { "headers-only", FORMAT_HEADERS_ONLY },
687                                   { 0, 0 } } },
688         { NOTMUCH_OPT_KEYWORD, &reply_all, "reply-to", 'r',
689           (notmuch_keyword_t []){ { "all", TRUE },
690                                   { "sender", FALSE },
691                                   { 0, 0 } } },
692         { NOTMUCH_OPT_BOOLEAN, &params.decrypt, "decrypt", 'd', 0 },
693         { 0, 0, 0, 0, 0 }
694     };
695
696     opt_index = parse_arguments (argc, argv, options, 1);
697     if (opt_index < 0) {
698         /* diagnostics already printed */
699         return 1;
700     }
701
702     if (format == FORMAT_HEADERS_ONLY)
703         reply_format_func = notmuch_reply_format_headers_only;
704     else if (format == FORMAT_JSON)
705         reply_format_func = notmuch_reply_format_json;
706     else
707         reply_format_func = notmuch_reply_format_default;
708
709     if (params.decrypt) {
710 #ifdef GMIME_ATLEAST_26
711         /* TODO: GMimePasswordRequestFunc */
712         params.cryptoctx = g_mime_gpg_context_new (NULL, "gpg");
713 #else
714         GMimeSession* session = g_object_new (g_mime_session_get_type(), NULL);
715         params.cryptoctx = g_mime_gpg_context_new (session, "gpg");
716 #endif
717         if (params.cryptoctx) {
718             g_mime_gpg_context_set_always_trust ((GMimeGpgContext*) params.cryptoctx, FALSE);
719         } else {
720             params.decrypt = FALSE;
721             fprintf (stderr, "Failed to construct gpg context.\n");
722         }
723 #ifndef GMIME_ATLEAST_26
724         g_object_unref (session);
725 #endif
726     }
727
728     config = notmuch_config_open (ctx, NULL, NULL);
729     if (config == NULL)
730         return 1;
731
732     query_string = query_string_from_args (ctx, argc-opt_index, argv+opt_index);
733     if (query_string == NULL) {
734         fprintf (stderr, "Out of memory\n");
735         return 1;
736     }
737
738     if (*query_string == '\0') {
739         fprintf (stderr, "Error: notmuch reply requires at least one search term.\n");
740         return 1;
741     }
742
743     notmuch = notmuch_database_open (notmuch_config_get_database_path (config),
744                                      NOTMUCH_DATABASE_MODE_READ_ONLY);
745     if (notmuch == NULL)
746         return 1;
747
748     query = notmuch_query_create (notmuch, query_string);
749     if (query == NULL) {
750         fprintf (stderr, "Out of memory\n");
751         return 1;
752     }
753
754     if (reply_format_func (ctx, config, query, &params, reply_all) != 0)
755         return 1;
756
757     notmuch_query_destroy (query);
758     notmuch_database_close (notmuch);
759
760     if (params.cryptoctx)
761         g_object_unref(params.cryptoctx);
762
763     return ret;
764 }