1 /* notmuch - Not much of an email program, (just index and search)
3 * Copyright © 2009 Carl Worth
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.
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.
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/ .
18 * Author: Carl Worth <cworth@cworth.org>
21 #include "notmuch-client.h"
24 format_message_text (unused (const void *ctx),
25 notmuch_message_t *message,
28 format_headers_text (const void *ctx,
29 notmuch_message_t *message);
32 format_headers_message_part_text (GMimeMessage *message);
35 format_part_start_text (GMimeObject *part,
39 format_part_content_text (GMimeObject *part);
42 format_part_end_text (GMimeObject *part);
44 static const notmuch_show_format_t format_text = {
46 "\fmessage{ ", format_message_text,
47 "\fheader{\n", format_headers_text, format_headers_message_part_text, "\fheader}\n",
49 format_part_start_text,
52 format_part_content_text,
61 format_message_json (const void *ctx,
62 notmuch_message_t *message,
65 format_headers_json (const void *ctx,
66 notmuch_message_t *message);
69 format_headers_message_part_json (GMimeMessage *message);
72 format_part_start_json (unused (GMimeObject *part),
76 format_part_encstatus_json (int status);
79 format_part_sigstatus_json (const GMimeSignatureValidity* validity);
82 format_part_content_json (GMimeObject *part);
85 format_part_end_json (GMimeObject *part);
87 static const notmuch_show_format_t format_json = {
89 "{", format_message_json,
90 "\"headers\": {", format_headers_json, format_headers_message_part_json, "}",
92 format_part_start_json,
93 format_part_encstatus_json,
94 format_part_sigstatus_json,
95 format_part_content_json,
104 format_message_mbox (const void *ctx,
105 notmuch_message_t *message,
106 unused (int indent));
108 static const notmuch_show_format_t format_mbox = {
110 "", format_message_mbox,
125 format_part_content_raw (GMimeObject *part);
127 static const notmuch_show_format_t format_raw = {
130 "", NULL, format_headers_message_part_text, "\n",
135 format_part_content_raw,
144 _get_tags_as_string (const void *ctx, notmuch_message_t *message)
146 notmuch_tags_t *tags;
151 result = talloc_strdup (ctx, "");
155 for (tags = notmuch_message_get_tags (message);
156 notmuch_tags_valid (tags);
157 notmuch_tags_move_to_next (tags))
159 tag = notmuch_tags_get (tags);
161 result = talloc_asprintf_append (result, "%s%s",
162 first ? "" : " ", tag);
169 /* Get a nice, single-line summary of message. */
171 _get_one_line_summary (const void *ctx, notmuch_message_t *message)
175 const char *relative_date;
178 from = notmuch_message_get_header (message, "from");
180 date = notmuch_message_get_date (message);
181 relative_date = notmuch_time_relative_date (ctx, date);
183 tags = _get_tags_as_string (ctx, message);
185 return talloc_asprintf (ctx, "%s (%s) (%s)",
186 from, relative_date, tags);
190 format_message_text (unused (const void *ctx), notmuch_message_t *message, int indent)
192 printf ("id:%s depth:%d match:%d filename:%s\n",
193 notmuch_message_get_message_id (message),
195 notmuch_message_get_flag (message, NOTMUCH_MESSAGE_FLAG_MATCH),
196 notmuch_message_get_filename (message));
200 format_message_json (const void *ctx, notmuch_message_t *message, unused (int indent))
202 notmuch_tags_t *tags;
204 void *ctx_quote = talloc_new (ctx);
206 const char *relative_date;
208 date = notmuch_message_get_date (message);
209 relative_date = notmuch_time_relative_date (ctx, date);
211 printf ("\"id\": %s, \"match\": %s, \"filename\": %s, \"timestamp\": %ld, \"date_relative\": \"%s\", \"tags\": [",
212 json_quote_str (ctx_quote, notmuch_message_get_message_id (message)),
213 notmuch_message_get_flag (message, NOTMUCH_MESSAGE_FLAG_MATCH) ? "true" : "false",
214 json_quote_str (ctx_quote, notmuch_message_get_filename (message)),
215 date, relative_date);
217 for (tags = notmuch_message_get_tags (message);
218 notmuch_tags_valid (tags);
219 notmuch_tags_move_to_next (tags))
221 printf("%s%s", first ? "" : ",",
222 json_quote_str (ctx_quote, notmuch_tags_get (tags)));
226 talloc_free (ctx_quote);
229 /* Extract just the email address from the contents of a From:
232 _extract_email_address (const void *ctx, const char *from)
234 InternetAddressList *addresses;
235 InternetAddress *address;
236 InternetAddressMailbox *mailbox;
237 const char *email = "MAILER-DAEMON";
239 addresses = internet_address_list_parse_string (from);
241 /* Bail if there is no address here. */
242 if (addresses == NULL || internet_address_list_length (addresses) < 1)
245 /* Otherwise, just use the first address. */
246 address = internet_address_list_get_address (addresses, 0);
248 /* The From header should never contain an address group rather
249 * than a mailbox. So bail if it does. */
250 if (! INTERNET_ADDRESS_IS_MAILBOX (address))
253 mailbox = INTERNET_ADDRESS_MAILBOX (address);
254 email = internet_address_mailbox_get_addr (mailbox);
255 email = talloc_strdup (ctx, email);
259 g_object_unref (addresses);
264 /* Return 1 if 'line' is an mbox From_ line---that is, a line
265 * beginning with zero or more '>' characters followed by the
266 * characters 'F', 'r', 'o', 'm', and space.
268 * Any characters at all may appear after that in the line.
271 _is_from_line (const char *line)
273 const char *s = line;
281 if (STRNCMP_LITERAL (s, "From ") == 0)
287 /* Print a message in "mboxrd" format as documented, for example,
290 * http://qmail.org/qmail-manual-html/man5/mbox.html
293 format_message_mbox (const void *ctx,
294 notmuch_message_t *message,
297 const char *filename;
302 struct tm date_gmtime;
303 char date_asctime[26];
309 filename = notmuch_message_get_filename (message);
310 file = fopen (filename, "r");
312 fprintf (stderr, "Failed to open %s: %s\n",
313 filename, strerror (errno));
317 from = notmuch_message_get_header (message, "from");
318 from = _extract_email_address (ctx, from);
320 date = notmuch_message_get_date (message);
321 gmtime_r (&date, &date_gmtime);
322 asctime_r (&date_gmtime, date_asctime);
324 printf ("From %s %s", from, date_asctime);
326 while ((line_len = getline (&line, &line_size, file)) != -1 ) {
327 if (_is_from_line (line))
339 format_headers_text (const void *ctx, notmuch_message_t *message)
341 const char *headers[] = {
342 "Subject", "From", "To", "Cc", "Bcc", "Date"
344 const char *name, *value;
347 printf ("%s\n", _get_one_line_summary (ctx, message));
349 for (i = 0; i < ARRAY_SIZE (headers); i++) {
351 value = notmuch_message_get_header (message, name);
352 if (value && strlen (value))
353 printf ("%s: %s\n", name, value);
358 format_headers_message_part_text (GMimeMessage *message)
360 InternetAddressList *recipients;
361 const char *recipients_string;
363 printf ("From: %s\n", g_mime_message_get_sender (message));
364 recipients = g_mime_message_get_recipients (message, GMIME_RECIPIENT_TYPE_TO);
365 recipients_string = internet_address_list_to_string (recipients, 0);
366 if (recipients_string)
369 recipients = g_mime_message_get_recipients (message, GMIME_RECIPIENT_TYPE_CC);
370 recipients_string = internet_address_list_to_string (recipients, 0);
371 if (recipients_string)
374 printf ("Subject: %s\n", g_mime_message_get_subject (message));
375 printf ("Date: %s\n", g_mime_message_get_date_as_string (message));
379 format_headers_json (const void *ctx, notmuch_message_t *message)
381 const char *headers[] = {
382 "Subject", "From", "To", "Cc", "Bcc", "Date"
384 const char *name, *value;
386 int first_header = 1;
387 void *ctx_quote = talloc_new (ctx);
389 for (i = 0; i < ARRAY_SIZE (headers); i++) {
391 value = notmuch_message_get_header (message, name);
395 fputs (", ", stdout);
399 json_quote_str (ctx_quote, name),
400 json_quote_str (ctx_quote, value));
404 talloc_free (ctx_quote);
408 format_headers_message_part_json (GMimeMessage *message)
410 void *ctx = talloc_new (NULL);
411 void *ctx_quote = talloc_new (ctx);
412 InternetAddressList *recipients;
413 const char *recipients_string;
416 json_quote_str (ctx_quote, "From"),
417 json_quote_str (ctx_quote, g_mime_message_get_sender (message)));
418 recipients = g_mime_message_get_recipients (message, GMIME_RECIPIENT_TYPE_TO);
419 recipients_string = internet_address_list_to_string (recipients, 0);
420 if (recipients_string)
422 json_quote_str (ctx_quote, "To"),
423 json_quote_str (ctx_quote, recipients_string));
424 recipients = g_mime_message_get_recipients (message, GMIME_RECIPIENT_TYPE_CC);
425 recipients_string = internet_address_list_to_string (recipients, 0);
426 if (recipients_string)
428 json_quote_str (ctx_quote, "Cc"),
429 json_quote_str (ctx_quote, recipients_string));
431 json_quote_str (ctx_quote, "Subject"),
432 json_quote_str (ctx_quote, g_mime_message_get_subject (message)));
434 json_quote_str (ctx_quote, "Date"),
435 json_quote_str (ctx_quote, g_mime_message_get_date_as_string (message)));
437 talloc_free (ctx_quote);
441 /* Write a MIME text part out to the given stream.
443 * Both line-ending conversion (CRLF->LF) and charset conversion ( ->
444 * UTF-8) will be performed, so it is inappropriate to call this
445 * function with a non-text part. Doing so will trigger an internal
449 show_text_part_content (GMimeObject *part, GMimeStream *stream_out)
451 GMimeContentType *content_type = g_mime_object_get_content_type (GMIME_OBJECT (part));
452 GMimeStream *stream_filter = NULL;
453 GMimeDataWrapper *wrapper;
456 if (! g_mime_content_type_is_type (content_type, "text", "*"))
457 INTERNAL_ERROR ("Illegal request to format non-text part (%s) as text.",
458 g_mime_content_type_to_string (content_type));
460 if (stream_out == NULL)
463 stream_filter = g_mime_stream_filter_new (stream_out);
464 g_mime_stream_filter_add(GMIME_STREAM_FILTER (stream_filter),
465 g_mime_filter_crlf_new (FALSE, FALSE));
467 charset = g_mime_object_get_content_type_parameter (part, "charset");
469 GMimeFilter *charset_filter;
470 charset_filter = g_mime_filter_charset_new (charset, "UTF-8");
471 /* This result can be NULL for things like "unknown-8bit".
472 * Don't set a NULL filter as that makes GMime print
473 * annoying assertion-failure messages on stderr. */
474 if (charset_filter) {
475 g_mime_stream_filter_add (GMIME_STREAM_FILTER (stream_filter),
477 g_object_unref (charset_filter);
482 wrapper = g_mime_part_get_content_object (GMIME_PART (part));
483 if (wrapper && stream_filter)
484 g_mime_data_wrapper_write_to_stream (wrapper, stream_filter);
486 g_object_unref(stream_filter);
490 signer_status_to_string (GMimeSignerStatus x)
493 case GMIME_SIGNER_STATUS_NONE:
495 case GMIME_SIGNER_STATUS_GOOD:
497 case GMIME_SIGNER_STATUS_BAD:
499 case GMIME_SIGNER_STATUS_ERROR:
506 format_part_start_text (GMimeObject *part, int *part_count)
508 GMimeContentDisposition *disposition = g_mime_object_get_content_disposition (part);
511 strcmp (disposition->disposition, GMIME_DISPOSITION_ATTACHMENT) == 0)
513 printf ("\fattachment{ ID: %d", *part_count);
517 printf ("\fpart{ ID: %d", *part_count);
522 format_part_content_text (GMimeObject *part)
524 const char *cid = g_mime_object_get_content_id (part);
525 GMimeContentType *content_type = g_mime_object_get_content_type (GMIME_OBJECT (part));
527 if (GMIME_IS_PART (part))
529 const char *filename = g_mime_part_get_filename (GMIME_PART (part));
531 printf (", Filename: %s", filename);
535 printf (", Content-id: %s", cid);
537 printf (", Content-type: %s\n", g_mime_content_type_to_string (content_type));
539 if (g_mime_content_type_is_type (content_type, "text", "*") &&
540 !g_mime_content_type_is_type (content_type, "text", "html"))
542 GMimeStream *stream_stdout = g_mime_stream_file_new (stdout);
543 g_mime_stream_file_set_owner (GMIME_STREAM_FILE (stream_stdout), FALSE);
544 show_text_part_content (part, stream_stdout);
545 g_object_unref(stream_stdout);
547 else if (g_mime_content_type_is_type (content_type, "multipart", "*") ||
548 g_mime_content_type_is_type (content_type, "message", "rfc822"))
550 /* Do nothing for multipart since its content will be printed
555 printf ("Non-text part: %s\n",
556 g_mime_content_type_to_string (content_type));
561 format_part_end_text (GMimeObject *part)
563 GMimeContentDisposition *disposition;
565 disposition = g_mime_object_get_content_disposition (part);
567 strcmp (disposition->disposition, GMIME_DISPOSITION_ATTACHMENT) == 0)
569 printf ("\fattachment}\n");
573 printf ("\fpart}\n");
578 format_part_start_json (unused (GMimeObject *part), int *part_count)
580 printf ("{\"id\": %d", *part_count);
584 format_part_encstatus_json (int status)
586 printf (", \"encstatus\": [{\"status\": ");
596 format_part_sigstatus_json (const GMimeSignatureValidity* validity)
598 printf (", \"sigstatus\": [");
605 const GMimeSigner *signer = g_mime_signature_validity_get_signers (validity);
607 void *ctx_quote = talloc_new (NULL);
618 printf ("\"status\": %s",
619 json_quote_str (ctx_quote,
620 signer_status_to_string (signer->status)));
622 if (signer->status == GMIME_SIGNER_STATUS_GOOD)
624 if (signer->fingerprint)
625 printf (", \"fingerprint\": %s", json_quote_str (ctx_quote, signer->fingerprint));
626 /* these dates are seconds since the epoch; should we
627 * provide a more human-readable format string? */
629 printf (", \"created\": %d", (int) signer->created);
631 printf (", \"expires\": %d", (int) signer->expires);
632 /* output user id only if validity is FULL or ULTIMATE. */
633 /* note that gmime is using the term "trust" here, which
634 * is WRONG. It's actually user id "validity". */
635 if ((signer->name) && (signer->trust)) {
636 if ((signer->trust == GMIME_SIGNER_TRUST_FULLY) || (signer->trust == GMIME_SIGNER_TRUST_ULTIMATE))
637 printf (", \"userid\": %s", json_quote_str (ctx_quote, signer->name));
641 printf (", \"keyid\": %s", json_quote_str (ctx_quote, signer->keyid));
643 if (signer->errors != GMIME_SIGNER_ERROR_NONE) {
644 printf (", \"errors\": %x", signer->errors);
648 signer = signer->next;
653 talloc_free (ctx_quote);
657 format_part_content_json (GMimeObject *part)
659 GMimeContentType *content_type = g_mime_object_get_content_type (GMIME_OBJECT (part));
660 GMimeStream *stream_memory = g_mime_stream_mem_new ();
661 const char *cid = g_mime_object_get_content_id (part);
662 void *ctx = talloc_new (NULL);
663 GByteArray *part_content;
665 printf (", \"content-type\": %s",
666 json_quote_str (ctx, g_mime_content_type_to_string (content_type)));
669 printf(", \"content-id\": %s", json_quote_str (ctx, cid));
671 if (GMIME_IS_PART (part))
673 const char *filename = g_mime_part_get_filename (GMIME_PART (part));
675 printf (", \"filename\": %s", json_quote_str (ctx, filename));
678 if (g_mime_content_type_is_type (content_type, "text", "*"))
680 /* For non-HTML text parts, we include the content in the
681 * JSON. Since JSON must be Unicode, we handle charset
682 * decoding here and do not report a charset to the caller.
683 * For text/html parts, we do not include the content. If a
684 * caller is interested in text/html parts, it should retrieve
685 * them separately and they will not be decoded. Since this
686 * makes charset decoding the responsibility on the caller, we
687 * report the charset for text/html parts.
689 if (g_mime_content_type_is_type (content_type, "text", "html"))
691 const char *content_charset = g_mime_object_get_content_type_parameter (GMIME_OBJECT (part), "charset");
693 if (content_charset != NULL)
694 printf (", \"content-charset\": %s", json_quote_str (ctx, content_charset));
698 show_text_part_content (part, stream_memory);
699 part_content = g_mime_stream_mem_get_byte_array (GMIME_STREAM_MEM (stream_memory));
701 printf (", \"content\": %s", json_quote_chararray (ctx, (char *) part_content->data, part_content->len));
704 else if (g_mime_content_type_is_type (content_type, "multipart", "*"))
706 printf (", \"content\": [");
708 else if (g_mime_content_type_is_type (content_type, "message", "rfc822"))
710 printf (", \"content\": [{");
715 g_object_unref (stream_memory);
719 format_part_end_json (GMimeObject *part)
721 GMimeContentType *content_type = g_mime_object_get_content_type (GMIME_OBJECT (part));
723 if (g_mime_content_type_is_type (content_type, "multipart", "*"))
725 else if (g_mime_content_type_is_type (content_type, "message", "rfc822"))
732 format_part_content_raw (GMimeObject *part)
734 if (! GMIME_IS_PART (part))
737 GMimeStream *stream_stdout;
738 GMimeStream *stream_filter = NULL;
739 GMimeDataWrapper *wrapper;
741 stream_stdout = g_mime_stream_file_new (stdout);
742 g_mime_stream_file_set_owner (GMIME_STREAM_FILE (stream_stdout), FALSE);
744 stream_filter = g_mime_stream_filter_new (stream_stdout);
746 wrapper = g_mime_part_get_content_object (GMIME_PART (part));
748 if (wrapper && stream_filter)
749 g_mime_data_wrapper_write_to_stream (wrapper, stream_filter);
752 g_object_unref (stream_filter);
755 g_object_unref(stream_stdout);
759 show_message (void *ctx,
760 const notmuch_show_format_t *format,
761 notmuch_message_t *message,
763 notmuch_show_params_t *params)
765 if (params->part <= 0) {
766 fputs (format->message_start, stdout);
768 format->message(ctx, message, indent);
770 fputs (format->header_start, stdout);
772 format->header(ctx, message);
773 fputs (format->header_end, stdout);
775 fputs (format->body_start, stdout);
778 if (format->part_content)
779 show_message_body (message, format, params);
781 if (params->part <= 0) {
782 fputs (format->body_end, stdout);
784 fputs (format->message_end, stdout);
789 show_messages (void *ctx,
790 const notmuch_show_format_t *format,
791 notmuch_messages_t *messages,
793 notmuch_show_params_t *params)
795 notmuch_message_t *message;
796 notmuch_bool_t match;
800 fputs (format->message_set_start, stdout);
803 notmuch_messages_valid (messages);
804 notmuch_messages_move_to_next (messages))
807 fputs (format->message_set_sep, stdout);
810 fputs (format->message_set_start, stdout);
812 message = notmuch_messages_get (messages);
814 match = notmuch_message_get_flag (message, NOTMUCH_MESSAGE_FLAG_MATCH);
816 next_indent = indent;
818 if (match || params->entire_thread) {
819 show_message (ctx, format, message, indent, params);
820 next_indent = indent + 1;
822 fputs (format->message_set_sep, stdout);
827 notmuch_message_get_replies (message),
831 notmuch_message_destroy (message);
833 fputs (format->message_set_end, stdout);
836 fputs (format->message_set_end, stdout);
839 /* Formatted output of single message */
841 do_show_single (void *ctx,
842 notmuch_query_t *query,
843 const notmuch_show_format_t *format,
844 notmuch_show_params_t *params)
846 notmuch_messages_t *messages;
847 notmuch_message_t *message;
849 if (notmuch_query_count_messages (query) != 1) {
850 fprintf (stderr, "Error: search term did not match precisely one message.\n");
854 messages = notmuch_query_search_messages (query);
855 message = notmuch_messages_get (messages);
857 if (message == NULL) {
858 fprintf (stderr, "Error: Cannot find matching message.\n");
862 notmuch_message_set_flag (message, NOTMUCH_MESSAGE_FLAG_MATCH, 1);
864 /* Special case for --format=raw of full single message, just cat out file */
865 if (params->raw && 0 == params->part) {
867 const char *filename;
872 filename = notmuch_message_get_filename (message);
873 if (filename == NULL) {
874 fprintf (stderr, "Error: Cannot message filename.\n");
878 file = fopen (filename, "r");
880 fprintf (stderr, "Error: Cannot open file %s: %s\n", filename, strerror (errno));
884 while (!feof (file)) {
885 size = fread (buf, 1, sizeof (buf), file);
886 (void) fwrite (buf, size, 1, stdout);
893 show_message (ctx, format, message, 0, params);
900 /* Formatted output of threads */
903 notmuch_query_t *query,
904 const notmuch_show_format_t *format,
905 notmuch_show_params_t *params)
907 notmuch_threads_t *threads;
908 notmuch_thread_t *thread;
909 notmuch_messages_t *messages;
910 int first_toplevel = 1;
912 fputs (format->message_set_start, stdout);
914 for (threads = notmuch_query_search_threads (query);
915 notmuch_threads_valid (threads);
916 notmuch_threads_move_to_next (threads))
918 thread = notmuch_threads_get (threads);
920 messages = notmuch_thread_get_toplevel_messages (thread);
922 if (messages == NULL)
923 INTERNAL_ERROR ("Thread %s has no toplevel messages.\n",
924 notmuch_thread_get_thread_id (thread));
927 fputs (format->message_set_sep, stdout);
930 show_messages (ctx, format, messages, 0, params);
932 notmuch_thread_destroy (thread);
936 fputs (format->message_set_end, stdout);
942 notmuch_show_command (void *ctx, unused (int argc), unused (char *argv[]))
944 notmuch_config_t *config;
945 notmuch_database_t *notmuch;
946 notmuch_query_t *query;
949 const notmuch_show_format_t *format = &format_text;
950 notmuch_show_params_t params;
952 int format_specified = 0;
955 params.entire_thread = 0;
958 params.cryptoctx = NULL;
961 argc--; argv++; /* skip subcommand argument */
963 for (i = 0; i < argc && argv[i][0] == '-'; i++) {
964 if (strcmp (argv[i], "--") == 0) {
968 if (STRNCMP_LITERAL (argv[i], "--format=") == 0) {
969 opt = argv[i] + sizeof ("--format=") - 1;
970 if (strcmp (opt, "text") == 0) {
971 format = &format_text;
972 } else if (strcmp (opt, "json") == 0) {
973 format = &format_json;
974 params.entire_thread = 1;
975 } else if (strcmp (opt, "mbox") == 0) {
976 format = &format_mbox;
978 } else if (strcmp (opt, "raw") == 0) {
979 format = &format_raw;
982 fprintf (stderr, "Invalid value for --format: %s\n", opt);
985 format_specified = 1;
986 } else if (STRNCMP_LITERAL (argv[i], "--part=") == 0) {
987 params.part = atoi(argv[i] + sizeof ("--part=") - 1);
988 } else if (STRNCMP_LITERAL (argv[i], "--entire-thread") == 0) {
989 params.entire_thread = 1;
990 } else if ((STRNCMP_LITERAL (argv[i], "--verify") == 0) ||
991 (STRNCMP_LITERAL (argv[i], "--decrypt") == 0)) {
992 if (params.cryptoctx == NULL) {
993 GMimeSession* session = g_object_new(g_mime_session_get_type(), NULL);
994 if (NULL == (params.cryptoctx = g_mime_gpg_context_new(session, "gpg")))
995 fprintf (stderr, "Failed to construct gpg context.\n");
997 g_mime_gpg_context_set_always_trust((GMimeGpgContext*)params.cryptoctx, FALSE);
998 g_object_unref (session);
1001 if (STRNCMP_LITERAL (argv[i], "--decrypt") == 0)
1004 fprintf (stderr, "Unrecognized option: %s\n", argv[i]);
1012 config = notmuch_config_open (ctx, NULL, NULL);
1016 query_string = query_string_from_args (ctx, argc, argv);
1017 if (query_string == NULL) {
1018 fprintf (stderr, "Out of memory\n");
1022 if (mbox && params.part > 0) {
1023 fprintf (stderr, "Error: specifying parts is incompatible with mbox output format.\n");
1027 if (*query_string == '\0') {
1028 fprintf (stderr, "Error: notmuch show requires at least one search term.\n");
1032 notmuch = notmuch_database_open (notmuch_config_get_database_path (config),
1033 NOTMUCH_DATABASE_MODE_READ_ONLY);
1034 if (notmuch == NULL)
1037 query = notmuch_query_create (notmuch, query_string);
1038 if (query == NULL) {
1039 fprintf (stderr, "Out of memory\n");
1043 /* if part was requested and format was not specified, use format=raw */
1044 if (params.part >= 0 && !format_specified)
1045 format = &format_raw;
1047 /* If --format=raw specified without specifying part, we can only
1048 * output single message, so set part=0 */
1049 if (params.raw && params.part < 0)
1052 if (params.part >= 0)
1053 return do_show_single (ctx, query, format, ¶ms);
1055 return do_show (ctx, query, format, ¶ms);
1057 notmuch_query_destroy (query);
1058 notmuch_database_close (notmuch);
1060 if (params.cryptoctx)
1061 g_object_unref(params.cryptoctx);