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