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