]> git.notmuchmail.org Git - notmuch/blob - notmuch-show.c
test: Link to compat files when building program during "make test"
[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
462     content_type = g_mime_object_get_content_type (GMIME_OBJECT (part));
463
464     if (! first)
465         fputs (", ", stdout);
466
467     printf ("{\"id\": %d, \"content-type\": %s",
468             *part_count,
469             json_quote_str (ctx, g_mime_content_type_to_string (content_type)));
470
471     disposition = g_mime_object_get_content_disposition (part);
472     if (disposition &&
473         strcmp (disposition->disposition, GMIME_DISPOSITION_ATTACHMENT) == 0)
474     {
475         const char *filename = g_mime_part_get_filename (GMIME_PART (part));
476
477         printf (", \"filename\": %s", json_quote_str (ctx, filename));
478     }
479
480     if (g_mime_content_type_is_type (content_type, "text", "*") &&
481         !g_mime_content_type_is_type (content_type, "text", "html"))
482     {
483         show_part_content (part, stream_memory);
484         part_content = g_mime_stream_mem_get_byte_array (GMIME_STREAM_MEM (stream_memory));
485
486         printf (", \"content\": %s", json_quote_chararray (ctx, (char *) part_content->data, part_content->len));
487     }
488     else if (g_mime_content_type_is_type (content_type, "multipart", "*"))
489     {
490         printf (", \"content\": [");
491     }
492
493     talloc_free (ctx);
494     if (stream_memory)
495         g_object_unref (stream_memory);
496 }
497
498 static void
499 format_part_end_json (GMimeObject *part)
500 {
501     GMimeContentType *content_type;
502
503     content_type = g_mime_object_get_content_type (GMIME_OBJECT (part));
504
505     if (g_mime_content_type_is_type (content_type, "multipart", "*"))
506         printf ("]");
507
508     printf ("}");
509 }
510
511 static void
512 show_message (void *ctx, const show_format_t *format, notmuch_message_t *message, int indent)
513 {
514     fputs (format->message_start, stdout);
515     if (format->message)
516         format->message(ctx, message, indent);
517
518     fputs (format->header_start, stdout);
519     if (format->header)
520         format->header(ctx, message);
521     fputs (format->header_end, stdout);
522
523     fputs (format->body_start, stdout);
524     if (format->part)
525         show_message_body (notmuch_message_get_filename (message),
526                            format->part, format->part_end);
527     fputs (format->body_end, stdout);
528
529     fputs (format->message_end, stdout);
530 }
531
532
533 static void
534 show_messages (void *ctx, const show_format_t *format, notmuch_messages_t *messages, int indent,
535                notmuch_bool_t entire_thread)
536 {
537     notmuch_message_t *message;
538     notmuch_bool_t match;
539     int first_set = 1;
540     int next_indent;
541
542     fputs (format->message_set_start, stdout);
543
544     for (;
545          notmuch_messages_valid (messages);
546          notmuch_messages_move_to_next (messages))
547     {
548         if (!first_set)
549             fputs (format->message_set_sep, stdout);
550         first_set = 0;
551
552         fputs (format->message_set_start, stdout);
553
554         message = notmuch_messages_get (messages);
555
556         match = notmuch_message_get_flag (message, NOTMUCH_MESSAGE_FLAG_MATCH);
557
558         next_indent = indent;
559
560         if (match || entire_thread) {
561             show_message (ctx, format, message, indent);
562             next_indent = indent + 1;
563
564             fputs (format->message_set_sep, stdout);
565         }
566
567         show_messages (ctx, format, notmuch_message_get_replies (message),
568                        next_indent, entire_thread);
569
570         notmuch_message_destroy (message);
571
572         fputs (format->message_set_end, stdout);
573     }
574
575     fputs (format->message_set_end, stdout);
576 }
577
578 /* Support for --format=raw */
579 static int
580 do_show_raw (unused(void *ctx), notmuch_query_t *query)
581 {
582     notmuch_messages_t *messages;
583     notmuch_message_t *message;
584     const char *filename;
585     FILE *file;
586     size_t size;
587     char buf[4096];
588
589     if (notmuch_query_count_messages (query) != 1) {
590         fprintf (stderr, "Error: search term did not match precisely one message.\n");
591         return 1;
592     }
593
594     messages = notmuch_query_search_messages (query);
595     message = notmuch_messages_get (messages);
596
597     if (message == NULL) {
598         fprintf (stderr, "Error: Cannot find matching message.\n");
599         return 1;
600     }
601
602     filename = notmuch_message_get_filename (message);
603     if (filename == NULL) {
604         fprintf (stderr, "Error: Cannot message filename.\n");
605         return 1;
606     }
607
608     file = fopen (filename, "r");
609     if (file == NULL) {
610         fprintf (stderr, "Error: Cannot open file %s: %s\n", filename, strerror (errno));
611         return 1;
612     }
613
614     while (!feof (file)) {
615         size = fread (buf, 1, sizeof (buf), file);
616         fwrite (buf, size, 1, stdout);
617     }
618
619     fclose (file);
620
621     return 0;
622 }
623
624 /* Support for --format=text|json|mbox */
625 static int
626 do_show (void *ctx,
627          notmuch_query_t *query,
628          const show_format_t *format,
629          int entire_thread)
630 {
631     notmuch_threads_t *threads;
632     notmuch_thread_t *thread;
633     notmuch_messages_t *messages;
634     int first_toplevel = 1;
635
636     fputs (format->message_set_start, stdout);
637
638     for (threads = notmuch_query_search_threads (query);
639          notmuch_threads_valid (threads);
640          notmuch_threads_move_to_next (threads))
641     {
642         thread = notmuch_threads_get (threads);
643
644         messages = notmuch_thread_get_toplevel_messages (thread);
645
646         if (messages == NULL)
647             INTERNAL_ERROR ("Thread %s has no toplevel messages.\n",
648                             notmuch_thread_get_thread_id (thread));
649
650         if (!first_toplevel)
651             fputs (format->message_set_sep, stdout);
652         first_toplevel = 0;
653
654         show_messages (ctx, format, messages, 0, entire_thread);
655
656         notmuch_thread_destroy (thread);
657
658     }
659
660     fputs (format->message_set_end, stdout);
661
662     return 0;
663 }
664
665 int
666 notmuch_show_command (void *ctx, unused (int argc), unused (char *argv[]))
667 {
668     notmuch_config_t *config;
669     notmuch_database_t *notmuch;
670     notmuch_query_t *query;
671     char *query_string;
672     char *opt;
673     const show_format_t *format = &format_text;
674     int entire_thread = 0;
675     int i;
676     int raw = 0;
677
678     for (i = 0; i < argc && argv[i][0] == '-'; i++) {
679         if (strcmp (argv[i], "--") == 0) {
680             i++;
681             break;
682         }
683         if (STRNCMP_LITERAL (argv[i], "--format=") == 0) {
684             opt = argv[i] + sizeof ("--format=") - 1;
685             if (strcmp (opt, "text") == 0) {
686                 format = &format_text;
687             } else if (strcmp (opt, "json") == 0) {
688                 format = &format_json;
689                 entire_thread = 1;
690             } else if (strcmp (opt, "mbox") == 0) {
691                 format = &format_mbox;
692             } else if (strcmp (opt, "raw") == 0) {
693                 raw = 1;
694             } else {
695                 fprintf (stderr, "Invalid value for --format: %s\n", opt);
696                 return 1;
697             }
698         } else if (STRNCMP_LITERAL (argv[i], "--entire-thread") == 0) {
699             entire_thread = 1;
700         } else {
701             fprintf (stderr, "Unrecognized option: %s\n", argv[i]);
702             return 1;
703         }
704     }
705
706     argc -= i;
707     argv += i;
708
709     config = notmuch_config_open (ctx, NULL, NULL);
710     if (config == NULL)
711         return 1;
712
713     query_string = query_string_from_args (ctx, argc, argv);
714     if (query_string == NULL) {
715         fprintf (stderr, "Out of memory\n");
716         return 1;
717     }
718
719     if (*query_string == '\0') {
720         fprintf (stderr, "Error: notmuch show requires at least one search term.\n");
721         return 1;
722     }
723
724     notmuch = notmuch_database_open (notmuch_config_get_database_path (config),
725                                      NOTMUCH_DATABASE_MODE_READ_ONLY);
726     if (notmuch == NULL)
727         return 1;
728
729     query = notmuch_query_create (notmuch, query_string);
730     if (query == NULL) {
731         fprintf (stderr, "Out of memory\n");
732         return 1;
733     }
734
735     if (raw)
736         return do_show_raw (ctx, query);
737     else
738         return do_show (ctx, query, format, entire_thread);
739
740     notmuch_query_destroy (query);
741     notmuch_database_close (notmuch);
742
743     return 0;
744 }
745
746 int
747 notmuch_part_command (void *ctx, unused (int argc), unused (char *argv[]))
748 {
749         notmuch_config_t *config;
750         notmuch_database_t *notmuch;
751         notmuch_query_t *query;
752         notmuch_messages_t *messages;
753         notmuch_message_t *message;
754         char *query_string;
755         int i;
756         int part = 0;
757
758         for (i = 0; i < argc && argv[i][0] == '-'; i++) {
759                 if (strcmp (argv[i], "--") == 0) {
760                         i++;
761                         break;
762                 }
763                 if (STRNCMP_LITERAL (argv[i], "--part=") == 0) {
764                         part = atoi(argv[i] + sizeof ("--part=") - 1);
765                 } else {
766                         fprintf (stderr, "Unrecognized option: %s\n", argv[i]);
767                         return 1;
768                 }
769         }
770
771         argc -= i;
772         argv += i;
773
774         config = notmuch_config_open (ctx, NULL, NULL);
775         if (config == NULL)
776                 return 1;
777
778         query_string = query_string_from_args (ctx, argc, argv);
779         if (query_string == NULL) {
780                 fprintf (stderr, "Out of memory\n");
781                 return 1;
782         }
783
784         if (*query_string == '\0') {
785                 fprintf (stderr, "Error: notmuch part requires at least one search term.\n");
786                 return 1;
787         }
788
789         notmuch = notmuch_database_open (notmuch_config_get_database_path (config),
790                                          NOTMUCH_DATABASE_MODE_READ_ONLY);
791         if (notmuch == NULL)
792                 return 1;
793
794         query = notmuch_query_create (notmuch, query_string);
795         if (query == NULL) {
796                 fprintf (stderr, "Out of memory\n");
797                 return 1;
798         }
799
800         if (notmuch_query_count_messages (query) != 1) {
801                 fprintf (stderr, "Error: search term did not match precisely one message.\n");
802                 return 1;
803         }
804
805         messages = notmuch_query_search_messages (query);
806         message = notmuch_messages_get (messages);
807
808         if (message == NULL) {
809                 fprintf (stderr, "Error: cannot find matching message.\n");
810                 return 1;
811         }
812
813         show_one_part (notmuch_message_get_filename (message), part);
814
815         notmuch_query_destroy (query);
816         notmuch_database_close (notmuch);
817
818         return 0;
819 }