]> git.notmuchmail.org Git - notmuch/blob - notmuch-show.c
7220dd973803a624e12eb978a9b013c79fbe59ff
[notmuch] / notmuch-show.c
1 /* notmuch - Not much of an email program, (just index and search)
2  *
3  * Copyright © 2009 Carl Worth
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program.  If not, see http://www.gnu.org/licenses/ .
17  *
18  * Author: Carl Worth <cworth@cworth.org>
19  */
20
21 #include "notmuch-client.h"
22
23 typedef struct show_format {
24     const char *message_set_start;
25     const char *message_start;
26     void (*message) (const void *ctx,
27                      notmuch_message_t *message,
28                      int indent);
29     const char *header_start;
30     void (*header) (const void *ctx,
31                     notmuch_message_t *message);
32     const char *header_end;
33     const char *body_start;
34     void (*part) (GMimeObject *part,
35                   int *part_count, int first);
36     void (*part_end) (GMimeObject *part);
37     const char *body_end;
38     const char *message_end;
39     const char *message_set_sep;
40     const char *message_set_end;
41 } show_format_t;
42
43 static void
44 format_message_text (unused (const void *ctx),
45                      notmuch_message_t *message,
46                      int indent);
47 static void
48 format_headers_text (const void *ctx,
49                      notmuch_message_t *message);
50
51 static void
52 format_part_text (GMimeObject *part,
53                   int *part_count,
54                   int first);
55
56 static void
57 format_part_end_text (GMimeObject *part);
58
59 static const show_format_t format_text = {
60     "",
61         "\fmessage{ ", format_message_text,
62             "\fheader{\n", format_headers_text, "\fheader}\n",
63             "\fbody{\n", format_part_text, format_part_end_text, "\fbody}\n",
64         "\fmessage}\n", "",
65     ""
66 };
67
68 static void
69 format_message_json (const void *ctx,
70                      notmuch_message_t *message,
71                      unused (int indent));
72 static void
73 format_headers_json (const void *ctx,
74                      notmuch_message_t *message);
75
76 static void
77 format_part_json (GMimeObject *part,
78                   int *part_count,
79                   int first);
80
81 static void
82 format_part_end_json (GMimeObject *part);
83
84 static const show_format_t format_json = {
85     "[",
86         "{", format_message_json,
87             ", \"headers\": {", format_headers_json, "}",
88             ", \"body\": [", format_part_json, format_part_end_json, "]",
89         "}", ", ",
90     "]"
91 };
92
93 static void
94 format_message_mbox (const void *ctx,
95                      notmuch_message_t *message,
96                      unused (int indent));
97
98 static const show_format_t format_mbox = {
99     "",
100         "", format_message_mbox,
101             "", NULL, "",
102             "", NULL, NULL, "",
103         "", "",
104     ""
105 };
106
107 static const char *
108 _get_tags_as_string (const void *ctx, notmuch_message_t *message)
109 {
110     notmuch_tags_t *tags;
111     int first = 1;
112     const char *tag;
113     char *result;
114
115     result = talloc_strdup (ctx, "");
116     if (result == NULL)
117         return NULL;
118
119     for (tags = notmuch_message_get_tags (message);
120          notmuch_tags_valid (tags);
121          notmuch_tags_move_to_next (tags))
122     {
123         tag = notmuch_tags_get (tags);
124
125         result = talloc_asprintf_append (result, "%s%s",
126                                          first ? "" : " ", tag);
127         first = 0;
128     }
129
130     return result;
131 }
132
133 /* Get a nice, single-line summary of message. */
134 static const char *
135 _get_one_line_summary (const void *ctx, notmuch_message_t *message)
136 {
137     const char *from;
138     time_t date;
139     const char *relative_date;
140     const char *tags;
141
142     from = notmuch_message_get_header (message, "from");
143
144     date = notmuch_message_get_date (message);
145     relative_date = notmuch_time_relative_date (ctx, date);
146
147     tags = _get_tags_as_string (ctx, message);
148
149     return talloc_asprintf (ctx, "%s (%s) (%s)",
150                             from, relative_date, tags);
151 }
152
153 static void
154 format_message_text (unused (const void *ctx), notmuch_message_t *message, int indent)
155 {
156     printf ("id:%s depth:%d match:%d filename:%s\n",
157             notmuch_message_get_message_id (message),
158             indent,
159             notmuch_message_get_flag (message, NOTMUCH_MESSAGE_FLAG_MATCH),
160             notmuch_message_get_filename (message));
161 }
162
163 static void
164 format_message_json (const void *ctx, notmuch_message_t *message, unused (int indent))
165 {
166     notmuch_tags_t *tags;
167     int first = 1;
168     void *ctx_quote = talloc_new (ctx);
169     time_t date;
170     const char *relative_date;
171
172     date = notmuch_message_get_date (message);
173     relative_date = notmuch_time_relative_date (ctx, date);
174
175     printf ("\"id\": %s, \"match\": %s, \"filename\": %s, \"timestamp\": %ld, \"date_relative\": \"%s\", \"tags\": [",
176             json_quote_str (ctx_quote, notmuch_message_get_message_id (message)),
177             notmuch_message_get_flag (message, NOTMUCH_MESSAGE_FLAG_MATCH) ? "true" : "false",
178             json_quote_str (ctx_quote, notmuch_message_get_filename (message)),
179             date, relative_date);
180
181     for (tags = notmuch_message_get_tags (message);
182          notmuch_tags_valid (tags);
183          notmuch_tags_move_to_next (tags))
184     {
185          printf("%s%s", first ? "" : ",",
186                json_quote_str (ctx_quote, notmuch_tags_get (tags)));
187          first = 0;
188     }
189     printf("]");
190     talloc_free (ctx_quote);
191 }
192
193 /* Extract just the email address from the contents of a From:
194  * header. */
195 static const char *
196 _extract_email_address (const void *ctx, const char *from)
197 {
198     InternetAddressList *addresses;
199     InternetAddress *address;
200     InternetAddressMailbox *mailbox;
201     const char *email = "MAILER-DAEMON";
202
203     addresses = internet_address_list_parse_string (from);
204
205     /* Bail if there is no address here. */
206     if (addresses == NULL || internet_address_list_length (addresses) < 1)
207         goto DONE;
208
209     /* Otherwise, just use the first address. */
210     address = internet_address_list_get_address (addresses, 0);
211
212     /* The From header should never contain an address group rather
213      * than a mailbox. So bail if it does. */
214     if (! INTERNET_ADDRESS_IS_MAILBOX (address))
215         goto DONE;
216
217     mailbox = INTERNET_ADDRESS_MAILBOX (address);
218     email = internet_address_mailbox_get_addr (mailbox);
219     email = talloc_strdup (ctx, email);
220
221   DONE:
222     /* XXX: How to free addresses here? */
223     return email;
224    }
225
226 /* Return 1 if 'line' is an mbox From_ line---that is, a line
227  * beginning with zero or more '>' characters followed by the
228  * characters 'F', 'r', 'o', 'm', and space.
229  *
230  * Any characters at all may appear after that in the line.
231  */
232 static int
233 _is_from_line (const char *line)
234 {
235     const char *s = line;
236
237     if (line == NULL)
238         return 0;
239
240     while (*s == '>')
241         s++;
242
243     if (STRNCMP_LITERAL (s, "From ") == 0)
244         return 1;
245     else
246         return 0;
247 }
248
249 /* Print a message in "mboxrd" format as documented, for example,
250  * here:
251  *
252  * http://qmail.org/qmail-manual-html/man5/mbox.html
253  */
254 static void
255 format_message_mbox (const void *ctx,
256                      notmuch_message_t *message,
257                      unused (int indent))
258 {
259     const char *filename;
260     FILE *file;
261     const char *from;
262
263     time_t date;
264     struct tm date_gmtime;
265     char date_asctime[26];
266
267     char *line = NULL;
268     size_t line_size;
269     ssize_t line_len;
270
271     filename = notmuch_message_get_filename (message);
272     file = fopen (filename, "r");
273     if (file == NULL) {
274         fprintf (stderr, "Failed to open %s: %s\n",
275                  filename, strerror (errno));
276         return;
277     }
278
279     from = notmuch_message_get_header (message, "from");
280     from = _extract_email_address (ctx, from);
281
282     date = notmuch_message_get_date (message);
283     gmtime_r (&date, &date_gmtime);
284     asctime_r (&date_gmtime, date_asctime);
285
286     printf ("From %s %s", from, date_asctime);
287
288     while ((line_len = getline (&line, &line_size, file)) != -1 ) {
289         if (_is_from_line (line))
290             putchar ('>');
291         printf ("%s", line);
292     }
293
294     printf ("\n");
295
296     fclose (file);
297 }
298
299 static void
300 format_headers_text (const void *ctx, notmuch_message_t *message)
301 {
302     const char *headers[] = {
303         "Subject", "From", "To", "Cc", "Bcc", "Date"
304     };
305     const char *name, *value;
306     unsigned int i;
307
308     printf ("%s\n", _get_one_line_summary (ctx, message));
309
310     for (i = 0; i < ARRAY_SIZE (headers); i++) {
311         name = headers[i];
312         value = notmuch_message_get_header (message, name);
313         if (value && strlen (value))
314             printf ("%s: %s\n", name, value);
315     }
316 }
317
318 static void
319 format_headers_json (const void *ctx, notmuch_message_t *message)
320 {
321     const char *headers[] = {
322         "Subject", "From", "To", "Cc", "Bcc", "Date"
323     };
324     const char *name, *value;
325     unsigned int i;
326     int first_header = 1;
327     void *ctx_quote = talloc_new (ctx);
328
329     for (i = 0; i < ARRAY_SIZE (headers); i++) {
330         name = headers[i];
331         value = notmuch_message_get_header (message, name);
332         if (value)
333         {
334             if (!first_header)
335                 fputs (", ", stdout);
336             first_header = 0;
337
338             printf ("%s: %s",
339                     json_quote_str (ctx_quote, name),
340                     json_quote_str (ctx_quote, value));
341         }
342     }
343
344     talloc_free (ctx_quote);
345 }
346
347 static void
348 show_part_content (GMimeObject *part, GMimeStream *stream_out)
349 {
350     GMimeStream *stream_filter = NULL;
351     GMimeDataWrapper *wrapper;
352     const char *charset;
353
354     charset = g_mime_object_get_content_type_parameter (part, "charset");
355
356     if (stream_out) {
357         stream_filter = g_mime_stream_filter_new (stream_out);
358         g_mime_stream_filter_add(GMIME_STREAM_FILTER (stream_filter),
359                                  g_mime_filter_crlf_new (FALSE, FALSE));
360         if (charset) {
361             GMimeFilter *charset_filter;
362             charset_filter = g_mime_filter_charset_new (charset, "UTF-8");
363             /* This result can be NULL for things like "unknown-8bit".
364              * Don't set a NULL filter as that makes GMime print
365              * annoying assertion-failure messages on stderr. */
366             if (charset_filter)
367                 g_mime_stream_filter_add (GMIME_STREAM_FILTER (stream_filter),
368                                           charset_filter);
369         }
370     }
371
372     wrapper = g_mime_part_get_content_object (GMIME_PART (part));
373     if (wrapper && stream_filter)
374         g_mime_data_wrapper_write_to_stream (wrapper, stream_filter);
375     if (stream_filter)
376         g_object_unref(stream_filter);
377 }
378
379 static void
380 format_part_text (GMimeObject *part, int *part_count, unused (int first))
381 {
382     GMimeContentDisposition *disposition;
383     GMimeContentType *content_type;
384
385     disposition = g_mime_object_get_content_disposition (part);
386     if (disposition &&
387         strcmp (disposition->disposition, GMIME_DISPOSITION_ATTACHMENT) == 0)
388     {
389         const char *filename = g_mime_part_get_filename (GMIME_PART (part));
390         content_type = g_mime_object_get_content_type (GMIME_OBJECT (part));
391
392         printf ("\fattachment{ ID: %d, Content-type: %s\n",
393                 *part_count,
394                 g_mime_content_type_to_string (content_type));
395         printf ("Attachment: %s (%s)\n", filename,
396                 g_mime_content_type_to_string (content_type));
397
398         if (g_mime_content_type_is_type (content_type, "text", "*") &&
399             !g_mime_content_type_is_type (content_type, "text", "html"))
400         {
401             GMimeStream *stream_stdout = g_mime_stream_file_new (stdout);
402             g_mime_stream_file_set_owner (GMIME_STREAM_FILE (stream_stdout), FALSE);
403             show_part_content (part, stream_stdout);
404             g_object_unref(stream_stdout);
405         }
406
407         return;
408     }
409
410     content_type = g_mime_object_get_content_type (GMIME_OBJECT (part));
411
412     printf ("\fpart{ ID: %d, Content-type: %s\n",
413             *part_count,
414             g_mime_content_type_to_string (content_type));
415
416     if (g_mime_content_type_is_type (content_type, "text", "*") &&
417         !g_mime_content_type_is_type (content_type, "text", "html"))
418     {
419         GMimeStream *stream_stdout = g_mime_stream_file_new (stdout);
420         g_mime_stream_file_set_owner (GMIME_STREAM_FILE (stream_stdout), FALSE);
421         show_part_content (part, stream_stdout);
422         g_object_unref(stream_stdout);
423     }
424     else if (g_mime_content_type_is_type (content_type, "multipart", "*"))
425     {
426         /* Do nothing for multipart since its content will be printed
427          * when recursing. */
428     }
429     else
430     {
431         printf ("Non-text part: %s\n",
432                 g_mime_content_type_to_string (content_type));
433     }
434 }
435
436 static void
437 format_part_end_text (GMimeObject *part)
438 {
439     GMimeContentDisposition *disposition;
440
441     disposition = g_mime_object_get_content_disposition (part);
442     if (disposition &&
443         strcmp (disposition->disposition, GMIME_DISPOSITION_ATTACHMENT) == 0)
444     {
445         printf ("\fattachment}\n");
446     }
447     else
448     {
449         printf ("\fpart}\n");
450     }
451 }
452
453 static void
454 format_part_json (GMimeObject *part, int *part_count, int first)
455 {
456     GMimeContentType *content_type;
457     GMimeContentDisposition *disposition;
458     void *ctx = talloc_new (NULL);
459     GMimeStream *stream_memory = g_mime_stream_mem_new ();
460     GByteArray *part_content;
461     const char *cid;
462
463     content_type = g_mime_object_get_content_type (GMIME_OBJECT (part));
464
465     if (! first)
466         fputs (", ", stdout);
467
468     printf ("{\"id\": %d, \"content-type\": %s",
469             *part_count,
470             json_quote_str (ctx, g_mime_content_type_to_string (content_type)));
471
472     cid = g_mime_object_get_content_id (part);
473     if (cid != NULL)
474             printf(", \"content-id\": %s",
475                    json_quote_str (ctx, cid));
476
477     disposition = g_mime_object_get_content_disposition (part);
478     if (disposition &&
479         strcmp (disposition->disposition, GMIME_DISPOSITION_ATTACHMENT) == 0)
480     {
481         const char *filename = g_mime_part_get_filename (GMIME_PART (part));
482
483         printf (", \"filename\": %s", json_quote_str (ctx, filename));
484     }
485
486     if (g_mime_content_type_is_type (content_type, "text", "*") &&
487         !g_mime_content_type_is_type (content_type, "text", "html"))
488     {
489         show_part_content (part, stream_memory);
490         part_content = g_mime_stream_mem_get_byte_array (GMIME_STREAM_MEM (stream_memory));
491
492         printf (", \"content\": %s", json_quote_chararray (ctx, (char *) part_content->data, part_content->len));
493     }
494     else if (g_mime_content_type_is_type (content_type, "multipart", "*"))
495     {
496         printf (", \"content\": [");
497     }
498
499     talloc_free (ctx);
500     if (stream_memory)
501         g_object_unref (stream_memory);
502 }
503
504 static void
505 format_part_end_json (GMimeObject *part)
506 {
507     GMimeContentType *content_type;
508
509     content_type = g_mime_object_get_content_type (GMIME_OBJECT (part));
510
511     if (g_mime_content_type_is_type (content_type, "multipart", "*"))
512         printf ("]");
513
514     printf ("}");
515 }
516
517 static void
518 show_message (void *ctx, const show_format_t *format, notmuch_message_t *message, int indent)
519 {
520     fputs (format->message_start, stdout);
521     if (format->message)
522         format->message(ctx, message, indent);
523
524     fputs (format->header_start, stdout);
525     if (format->header)
526         format->header(ctx, message);
527     fputs (format->header_end, stdout);
528
529     fputs (format->body_start, stdout);
530     if (format->part)
531         show_message_body (notmuch_message_get_filename (message),
532                            format->part, format->part_end);
533     fputs (format->body_end, stdout);
534
535     fputs (format->message_end, stdout);
536 }
537
538
539 static void
540 show_messages (void *ctx, const show_format_t *format, notmuch_messages_t *messages, int indent,
541                notmuch_bool_t entire_thread)
542 {
543     notmuch_message_t *message;
544     notmuch_bool_t match;
545     int first_set = 1;
546     int next_indent;
547
548     fputs (format->message_set_start, stdout);
549
550     for (;
551          notmuch_messages_valid (messages);
552          notmuch_messages_move_to_next (messages))
553     {
554         if (!first_set)
555             fputs (format->message_set_sep, stdout);
556         first_set = 0;
557
558         fputs (format->message_set_start, stdout);
559
560         message = notmuch_messages_get (messages);
561
562         match = notmuch_message_get_flag (message, NOTMUCH_MESSAGE_FLAG_MATCH);
563
564         next_indent = indent;
565
566         if (match || entire_thread) {
567             show_message (ctx, format, message, indent);
568             next_indent = indent + 1;
569
570             fputs (format->message_set_sep, stdout);
571         }
572
573         show_messages (ctx, format, notmuch_message_get_replies (message),
574                        next_indent, entire_thread);
575
576         notmuch_message_destroy (message);
577
578         fputs (format->message_set_end, stdout);
579     }
580
581     fputs (format->message_set_end, stdout);
582 }
583
584 /* Support for --format=raw */
585 static int
586 do_show_raw (unused(void *ctx), notmuch_query_t *query)
587 {
588     notmuch_messages_t *messages;
589     notmuch_message_t *message;
590     const char *filename;
591     FILE *file;
592     size_t size;
593     char buf[4096];
594
595     if (notmuch_query_count_messages (query) != 1) {
596         fprintf (stderr, "Error: search term did not match precisely one message.\n");
597         return 1;
598     }
599
600     messages = notmuch_query_search_messages (query);
601     message = notmuch_messages_get (messages);
602
603     if (message == NULL) {
604         fprintf (stderr, "Error: Cannot find matching message.\n");
605         return 1;
606     }
607
608     filename = notmuch_message_get_filename (message);
609     if (filename == NULL) {
610         fprintf (stderr, "Error: Cannot message filename.\n");
611         return 1;
612     }
613
614     file = fopen (filename, "r");
615     if (file == NULL) {
616         fprintf (stderr, "Error: Cannot open file %s: %s\n", filename, strerror (errno));
617         return 1;
618     }
619
620     while (!feof (file)) {
621         size = fread (buf, 1, sizeof (buf), file);
622         fwrite (buf, size, 1, stdout);
623     }
624
625     fclose (file);
626
627     return 0;
628 }
629
630 /* Support for --format=text|json|mbox */
631 static int
632 do_show (void *ctx,
633          notmuch_query_t *query,
634          const show_format_t *format,
635          int entire_thread)
636 {
637     notmuch_threads_t *threads;
638     notmuch_thread_t *thread;
639     notmuch_messages_t *messages;
640     int first_toplevel = 1;
641
642     fputs (format->message_set_start, stdout);
643
644     for (threads = notmuch_query_search_threads (query);
645          notmuch_threads_valid (threads);
646          notmuch_threads_move_to_next (threads))
647     {
648         thread = notmuch_threads_get (threads);
649
650         messages = notmuch_thread_get_toplevel_messages (thread);
651
652         if (messages == NULL)
653             INTERNAL_ERROR ("Thread %s has no toplevel messages.\n",
654                             notmuch_thread_get_thread_id (thread));
655
656         if (!first_toplevel)
657             fputs (format->message_set_sep, stdout);
658         first_toplevel = 0;
659
660         show_messages (ctx, format, messages, 0, entire_thread);
661
662         notmuch_thread_destroy (thread);
663
664     }
665
666     fputs (format->message_set_end, stdout);
667
668     return 0;
669 }
670
671 int
672 notmuch_show_command (void *ctx, unused (int argc), unused (char *argv[]))
673 {
674     notmuch_config_t *config;
675     notmuch_database_t *notmuch;
676     notmuch_query_t *query;
677     char *query_string;
678     char *opt;
679     const show_format_t *format = &format_text;
680     int entire_thread = 0;
681     int i;
682     int raw = 0;
683
684     for (i = 0; i < argc && argv[i][0] == '-'; i++) {
685         if (strcmp (argv[i], "--") == 0) {
686             i++;
687             break;
688         }
689         if (STRNCMP_LITERAL (argv[i], "--format=") == 0) {
690             opt = argv[i] + sizeof ("--format=") - 1;
691             if (strcmp (opt, "text") == 0) {
692                 format = &format_text;
693             } else if (strcmp (opt, "json") == 0) {
694                 format = &format_json;
695                 entire_thread = 1;
696             } else if (strcmp (opt, "mbox") == 0) {
697                 format = &format_mbox;
698             } else if (strcmp (opt, "raw") == 0) {
699                 raw = 1;
700             } else {
701                 fprintf (stderr, "Invalid value for --format: %s\n", opt);
702                 return 1;
703             }
704         } else if (STRNCMP_LITERAL (argv[i], "--entire-thread") == 0) {
705             entire_thread = 1;
706         } else {
707             fprintf (stderr, "Unrecognized option: %s\n", argv[i]);
708             return 1;
709         }
710     }
711
712     argc -= i;
713     argv += i;
714
715     config = notmuch_config_open (ctx, NULL, NULL);
716     if (config == NULL)
717         return 1;
718
719     query_string = query_string_from_args (ctx, argc, argv);
720     if (query_string == NULL) {
721         fprintf (stderr, "Out of memory\n");
722         return 1;
723     }
724
725     if (*query_string == '\0') {
726         fprintf (stderr, "Error: notmuch show requires at least one search term.\n");
727         return 1;
728     }
729
730     notmuch = notmuch_database_open (notmuch_config_get_database_path (config),
731                                      NOTMUCH_DATABASE_MODE_READ_ONLY);
732     if (notmuch == NULL)
733         return 1;
734
735     query = notmuch_query_create (notmuch, query_string);
736     if (query == NULL) {
737         fprintf (stderr, "Out of memory\n");
738         return 1;
739     }
740
741     if (raw)
742         return do_show_raw (ctx, query);
743     else
744         return do_show (ctx, query, format, entire_thread);
745
746     notmuch_query_destroy (query);
747     notmuch_database_close (notmuch);
748
749     return 0;
750 }
751
752 int
753 notmuch_part_command (void *ctx, unused (int argc), unused (char *argv[]))
754 {
755         notmuch_config_t *config;
756         notmuch_database_t *notmuch;
757         notmuch_query_t *query;
758         notmuch_messages_t *messages;
759         notmuch_message_t *message;
760         char *query_string;
761         int i;
762         int part = 0;
763
764         for (i = 0; i < argc && argv[i][0] == '-'; i++) {
765                 if (strcmp (argv[i], "--") == 0) {
766                         i++;
767                         break;
768                 }
769                 if (STRNCMP_LITERAL (argv[i], "--part=") == 0) {
770                         part = atoi(argv[i] + sizeof ("--part=") - 1);
771                 } else {
772                         fprintf (stderr, "Unrecognized option: %s\n", argv[i]);
773                         return 1;
774                 }
775         }
776
777         argc -= i;
778         argv += i;
779
780         config = notmuch_config_open (ctx, NULL, NULL);
781         if (config == NULL)
782                 return 1;
783
784         query_string = query_string_from_args (ctx, argc, argv);
785         if (query_string == NULL) {
786                 fprintf (stderr, "Out of memory\n");
787                 return 1;
788         }
789
790         if (*query_string == '\0') {
791                 fprintf (stderr, "Error: notmuch part requires at least one search term.\n");
792                 return 1;
793         }
794
795         notmuch = notmuch_database_open (notmuch_config_get_database_path (config),
796                                          NOTMUCH_DATABASE_MODE_READ_ONLY);
797         if (notmuch == NULL)
798                 return 1;
799
800         query = notmuch_query_create (notmuch, query_string);
801         if (query == NULL) {
802                 fprintf (stderr, "Out of memory\n");
803                 return 1;
804         }
805
806         if (notmuch_query_count_messages (query) != 1) {
807                 fprintf (stderr, "Error: search term did not match precisely one message.\n");
808                 return 1;
809         }
810
811         messages = notmuch_query_search_messages (query);
812         message = notmuch_messages_get (messages);
813
814         if (message == NULL) {
815                 fprintf (stderr, "Error: cannot find matching message.\n");
816                 return 1;
817         }
818
819         show_one_part (notmuch_message_get_filename (message), part);
820
821         notmuch_query_destroy (query);
822         notmuch_database_close (notmuch);
823
824         return 0;
825 }