]> git.notmuchmail.org Git - notmuch/blob - notmuch-show.c
add part_sep formatter to replace "first" argument to part format 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
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 /* Support for --format=raw */
569 static int
570 do_show_raw (unused(void *ctx), notmuch_query_t *query)
571 {
572     notmuch_messages_t *messages;
573     notmuch_message_t *message;
574     const char *filename;
575     FILE *file;
576     size_t size;
577     char buf[4096];
578
579     if (notmuch_query_count_messages (query) != 1) {
580         fprintf (stderr, "Error: search term did not match precisely one message.\n");
581         return 1;
582     }
583
584     messages = notmuch_query_search_messages (query);
585     message = notmuch_messages_get (messages);
586
587     if (message == NULL) {
588         fprintf (stderr, "Error: Cannot find matching message.\n");
589         return 1;
590     }
591
592     filename = notmuch_message_get_filename (message);
593     if (filename == NULL) {
594         fprintf (stderr, "Error: Cannot message filename.\n");
595         return 1;
596     }
597
598     file = fopen (filename, "r");
599     if (file == NULL) {
600         fprintf (stderr, "Error: Cannot open file %s: %s\n", filename, strerror (errno));
601         return 1;
602     }
603
604     while (!feof (file)) {
605         size = fread (buf, 1, sizeof (buf), file);
606         fwrite (buf, size, 1, stdout);
607     }
608
609     fclose (file);
610
611     return 0;
612 }
613
614 /* Support for --format=text|json|mbox */
615 static int
616 do_show (void *ctx,
617          notmuch_query_t *query,
618          const notmuch_show_format_t *format,
619          notmuch_show_params_t *params)
620 {
621     notmuch_threads_t *threads;
622     notmuch_thread_t *thread;
623     notmuch_messages_t *messages;
624     int first_toplevel = 1;
625
626     fputs (format->message_set_start, stdout);
627
628     for (threads = notmuch_query_search_threads (query);
629          notmuch_threads_valid (threads);
630          notmuch_threads_move_to_next (threads))
631     {
632         thread = notmuch_threads_get (threads);
633
634         messages = notmuch_thread_get_toplevel_messages (thread);
635
636         if (messages == NULL)
637             INTERNAL_ERROR ("Thread %s has no toplevel messages.\n",
638                             notmuch_thread_get_thread_id (thread));
639
640         if (!first_toplevel)
641             fputs (format->message_set_sep, stdout);
642         first_toplevel = 0;
643
644         show_messages (ctx, format, messages, 0, params);
645
646         notmuch_thread_destroy (thread);
647
648     }
649
650     fputs (format->message_set_end, stdout);
651
652     return 0;
653 }
654
655 int
656 notmuch_show_command (void *ctx, unused (int argc), unused (char *argv[]))
657 {
658     notmuch_config_t *config;
659     notmuch_database_t *notmuch;
660     notmuch_query_t *query;
661     char *query_string;
662     char *opt;
663     const notmuch_show_format_t *format = &format_text;
664     notmuch_show_params_t params;
665     int i;
666     int raw = 0;
667
668     params.entire_thread = 0;
669
670     for (i = 0; i < argc && argv[i][0] == '-'; i++) {
671         if (strcmp (argv[i], "--") == 0) {
672             i++;
673             break;
674         }
675         if (STRNCMP_LITERAL (argv[i], "--format=") == 0) {
676             opt = argv[i] + sizeof ("--format=") - 1;
677             if (strcmp (opt, "text") == 0) {
678                 format = &format_text;
679             } else if (strcmp (opt, "json") == 0) {
680                 format = &format_json;
681                 params.entire_thread = 1;
682             } else if (strcmp (opt, "mbox") == 0) {
683                 format = &format_mbox;
684             } else if (strcmp (opt, "raw") == 0) {
685                 raw = 1;
686             } else {
687                 fprintf (stderr, "Invalid value for --format: %s\n", opt);
688                 return 1;
689             }
690         } else if (STRNCMP_LITERAL (argv[i], "--entire-thread") == 0) {
691             params.entire_thread = 1;
692         } else {
693             fprintf (stderr, "Unrecognized option: %s\n", argv[i]);
694             return 1;
695         }
696     }
697
698     argc -= i;
699     argv += i;
700
701     config = notmuch_config_open (ctx, NULL, NULL);
702     if (config == NULL)
703         return 1;
704
705     query_string = query_string_from_args (ctx, argc, argv);
706     if (query_string == NULL) {
707         fprintf (stderr, "Out of memory\n");
708         return 1;
709     }
710
711     if (*query_string == '\0') {
712         fprintf (stderr, "Error: notmuch show requires at least one search term.\n");
713         return 1;
714     }
715
716     notmuch = notmuch_database_open (notmuch_config_get_database_path (config),
717                                      NOTMUCH_DATABASE_MODE_READ_ONLY);
718     if (notmuch == NULL)
719         return 1;
720
721     query = notmuch_query_create (notmuch, query_string);
722     if (query == NULL) {
723         fprintf (stderr, "Out of memory\n");
724         return 1;
725     }
726
727     if (raw)
728         return do_show_raw (ctx, query);
729     else
730         return do_show (ctx, query, format, &params);
731
732     notmuch_query_destroy (query);
733     notmuch_database_close (notmuch);
734
735     return 0;
736 }
737
738 int
739 notmuch_part_command (void *ctx, unused (int argc), unused (char *argv[]))
740 {
741         notmuch_config_t *config;
742         notmuch_database_t *notmuch;
743         notmuch_query_t *query;
744         notmuch_messages_t *messages;
745         notmuch_message_t *message;
746         char *query_string;
747         int i;
748         int part = 0;
749
750         for (i = 0; i < argc && argv[i][0] == '-'; i++) {
751                 if (strcmp (argv[i], "--") == 0) {
752                         i++;
753                         break;
754                 }
755                 if (STRNCMP_LITERAL (argv[i], "--part=") == 0) {
756                         part = atoi(argv[i] + sizeof ("--part=") - 1);
757                 } else {
758                         fprintf (stderr, "Unrecognized option: %s\n", argv[i]);
759                         return 1;
760                 }
761         }
762
763         argc -= i;
764         argv += i;
765
766         config = notmuch_config_open (ctx, NULL, NULL);
767         if (config == NULL)
768                 return 1;
769
770         query_string = query_string_from_args (ctx, argc, argv);
771         if (query_string == NULL) {
772                 fprintf (stderr, "Out of memory\n");
773                 return 1;
774         }
775
776         if (*query_string == '\0') {
777                 fprintf (stderr, "Error: notmuch part requires at least one search term.\n");
778                 return 1;
779         }
780
781         notmuch = notmuch_database_open (notmuch_config_get_database_path (config),
782                                          NOTMUCH_DATABASE_MODE_READ_ONLY);
783         if (notmuch == NULL)
784                 return 1;
785
786         query = notmuch_query_create (notmuch, query_string);
787         if (query == NULL) {
788                 fprintf (stderr, "Out of memory\n");
789                 return 1;
790         }
791
792         if (notmuch_query_count_messages (query) != 1) {
793                 fprintf (stderr, "Error: search term did not match precisely one message.\n");
794                 return 1;
795         }
796
797         messages = notmuch_query_search_messages (query);
798         message = notmuch_messages_get (messages);
799
800         if (message == NULL) {
801                 fprintf (stderr, "Error: cannot find matching message.\n");
802                 return 1;
803         }
804
805         show_one_part (notmuch_message_get_filename (message), part);
806
807         notmuch_query_destroy (query);
808         notmuch_database_close (notmuch);
809
810         return 0;
811 }