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