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_headers_message_part_text (GMimeMessage *message);
27 format_part_text (const void *ctx, mime_node_t *node,
28 int indent, const notmuch_show_params_t *params);
30 static const notmuch_show_format_t format_text = {
31 .message_set_start = "",
32 .part = format_part_text,
33 .message_set_sep = "",
38 format_part_json_entry (const void *ctx, mime_node_t *node,
39 int indent, const notmuch_show_params_t *params);
41 static const notmuch_show_format_t format_json = {
42 .message_set_start = "[",
43 .part = format_part_json_entry,
44 .message_set_sep = ", ",
45 .message_set_end = "]"
49 format_message_mbox (const void *ctx,
50 notmuch_message_t *message,
53 static const notmuch_show_format_t format_mbox = {
55 "", format_message_mbox,
70 format_part_content_raw (GMimeObject *part);
72 static const notmuch_show_format_t format_raw = {
75 "", NULL, format_headers_message_part_text, "\n",
80 format_part_content_raw,
89 _get_tags_as_string (const void *ctx, notmuch_message_t *message)
96 result = talloc_strdup (ctx, "");
100 for (tags = notmuch_message_get_tags (message);
101 notmuch_tags_valid (tags);
102 notmuch_tags_move_to_next (tags))
104 tag = notmuch_tags_get (tags);
106 result = talloc_asprintf_append (result, "%s%s",
107 first ? "" : " ", tag);
114 /* Get a nice, single-line summary of message. */
116 _get_one_line_summary (const void *ctx, notmuch_message_t *message)
120 const char *relative_date;
123 from = notmuch_message_get_header (message, "from");
125 date = notmuch_message_get_date (message);
126 relative_date = notmuch_time_relative_date (ctx, date);
128 tags = _get_tags_as_string (ctx, message);
130 return talloc_asprintf (ctx, "%s (%s) (%s)",
131 from, relative_date, tags);
135 format_message_json (const void *ctx, notmuch_message_t *message)
137 notmuch_tags_t *tags;
139 void *ctx_quote = talloc_new (ctx);
141 const char *relative_date;
143 date = notmuch_message_get_date (message);
144 relative_date = notmuch_time_relative_date (ctx, date);
146 printf ("\"id\": %s, \"match\": %s, \"filename\": %s, \"timestamp\": %ld, \"date_relative\": \"%s\", \"tags\": [",
147 json_quote_str (ctx_quote, notmuch_message_get_message_id (message)),
148 notmuch_message_get_flag (message, NOTMUCH_MESSAGE_FLAG_MATCH) ? "true" : "false",
149 json_quote_str (ctx_quote, notmuch_message_get_filename (message)),
150 date, relative_date);
152 for (tags = notmuch_message_get_tags (message);
153 notmuch_tags_valid (tags);
154 notmuch_tags_move_to_next (tags))
156 printf("%s%s", first ? "" : ",",
157 json_quote_str (ctx_quote, notmuch_tags_get (tags)));
161 talloc_free (ctx_quote);
164 /* Extract just the email address from the contents of a From:
167 _extract_email_address (const void *ctx, const char *from)
169 InternetAddressList *addresses;
170 InternetAddress *address;
171 InternetAddressMailbox *mailbox;
172 const char *email = "MAILER-DAEMON";
174 addresses = internet_address_list_parse_string (from);
176 /* Bail if there is no address here. */
177 if (addresses == NULL || internet_address_list_length (addresses) < 1)
180 /* Otherwise, just use the first address. */
181 address = internet_address_list_get_address (addresses, 0);
183 /* The From header should never contain an address group rather
184 * than a mailbox. So bail if it does. */
185 if (! INTERNET_ADDRESS_IS_MAILBOX (address))
188 mailbox = INTERNET_ADDRESS_MAILBOX (address);
189 email = internet_address_mailbox_get_addr (mailbox);
190 email = talloc_strdup (ctx, email);
194 g_object_unref (addresses);
199 /* Return 1 if 'line' is an mbox From_ line---that is, a line
200 * beginning with zero or more '>' characters followed by the
201 * characters 'F', 'r', 'o', 'm', and space.
203 * Any characters at all may appear after that in the line.
206 _is_from_line (const char *line)
208 const char *s = line;
216 if (STRNCMP_LITERAL (s, "From ") == 0)
222 /* Print a message in "mboxrd" format as documented, for example,
225 * http://qmail.org/qmail-manual-html/man5/mbox.html
228 format_message_mbox (const void *ctx,
229 notmuch_message_t *message,
232 const char *filename;
237 struct tm date_gmtime;
238 char date_asctime[26];
244 filename = notmuch_message_get_filename (message);
245 file = fopen (filename, "r");
247 fprintf (stderr, "Failed to open %s: %s\n",
248 filename, strerror (errno));
252 from = notmuch_message_get_header (message, "from");
253 from = _extract_email_address (ctx, from);
255 date = notmuch_message_get_date (message);
256 gmtime_r (&date, &date_gmtime);
257 asctime_r (&date_gmtime, date_asctime);
259 printf ("From %s %s", from, date_asctime);
261 while ((line_len = getline (&line, &line_size, file)) != -1 ) {
262 if (_is_from_line (line))
273 format_headers_message_part_text (GMimeMessage *message)
275 InternetAddressList *recipients;
276 const char *recipients_string;
278 printf ("Subject: %s\n", g_mime_message_get_subject (message));
279 printf ("From: %s\n", g_mime_message_get_sender (message));
280 recipients = g_mime_message_get_recipients (message, GMIME_RECIPIENT_TYPE_TO);
281 recipients_string = internet_address_list_to_string (recipients, 0);
282 if (recipients_string)
285 recipients = g_mime_message_get_recipients (message, GMIME_RECIPIENT_TYPE_CC);
286 recipients_string = internet_address_list_to_string (recipients, 0);
287 if (recipients_string)
290 printf ("Date: %s\n", g_mime_message_get_date_as_string (message));
294 format_headers_json (const void *ctx, GMimeMessage *message)
296 void *local = talloc_new (ctx);
297 InternetAddressList *recipients;
298 const char *recipients_string;
301 json_quote_str (local, "Subject"),
302 json_quote_str (local, g_mime_message_get_subject (message)));
304 json_quote_str (local, "From"),
305 json_quote_str (local, g_mime_message_get_sender (message)));
306 recipients = g_mime_message_get_recipients (message, GMIME_RECIPIENT_TYPE_TO);
307 recipients_string = internet_address_list_to_string (recipients, 0);
308 if (recipients_string)
310 json_quote_str (local, "To"),
311 json_quote_str (local, recipients_string));
312 recipients = g_mime_message_get_recipients (message, GMIME_RECIPIENT_TYPE_CC);
313 recipients_string = internet_address_list_to_string (recipients, 0);
314 if (recipients_string)
316 json_quote_str (local, "Cc"),
317 json_quote_str (local, recipients_string));
319 json_quote_str (local, "Date"),
320 json_quote_str (local, g_mime_message_get_date_as_string (message)));
325 /* Write a MIME text part out to the given stream.
327 * Both line-ending conversion (CRLF->LF) and charset conversion ( ->
328 * UTF-8) will be performed, so it is inappropriate to call this
329 * function with a non-text part. Doing so will trigger an internal
333 show_text_part_content (GMimeObject *part, GMimeStream *stream_out)
335 GMimeContentType *content_type = g_mime_object_get_content_type (GMIME_OBJECT (part));
336 GMimeStream *stream_filter = NULL;
337 GMimeDataWrapper *wrapper;
340 if (! g_mime_content_type_is_type (content_type, "text", "*"))
341 INTERNAL_ERROR ("Illegal request to format non-text part (%s) as text.",
342 g_mime_content_type_to_string (content_type));
344 if (stream_out == NULL)
347 stream_filter = g_mime_stream_filter_new (stream_out);
348 g_mime_stream_filter_add(GMIME_STREAM_FILTER (stream_filter),
349 g_mime_filter_crlf_new (FALSE, FALSE));
351 charset = g_mime_object_get_content_type_parameter (part, "charset");
353 GMimeFilter *charset_filter;
354 charset_filter = g_mime_filter_charset_new (charset, "UTF-8");
355 /* This result can be NULL for things like "unknown-8bit".
356 * Don't set a NULL filter as that makes GMime print
357 * annoying assertion-failure messages on stderr. */
358 if (charset_filter) {
359 g_mime_stream_filter_add (GMIME_STREAM_FILTER (stream_filter),
361 g_object_unref (charset_filter);
366 wrapper = g_mime_part_get_content_object (GMIME_PART (part));
367 if (wrapper && stream_filter)
368 g_mime_data_wrapper_write_to_stream (wrapper, stream_filter);
370 g_object_unref(stream_filter);
373 #ifdef GMIME_ATLEAST_26
375 signature_status_to_string (GMimeSignatureStatus x)
378 case GMIME_SIGNATURE_STATUS_GOOD:
380 case GMIME_SIGNATURE_STATUS_BAD:
382 case GMIME_SIGNATURE_STATUS_ERROR:
389 signer_status_to_string (GMimeSignerStatus x)
392 case GMIME_SIGNER_STATUS_NONE:
394 case GMIME_SIGNER_STATUS_GOOD:
396 case GMIME_SIGNER_STATUS_BAD:
398 case GMIME_SIGNER_STATUS_ERROR:
405 #ifdef GMIME_ATLEAST_26
407 format_part_sigstatus_json (GMimeSignatureList *siglist)
416 void *ctx_quote = talloc_new (NULL);
418 for (i = 0; i < g_mime_signature_list_length (siglist); i++) {
419 GMimeSignature *signature = g_mime_signature_list_get_signature (siglist, i);
427 GMimeSignatureStatus status = g_mime_signature_get_status (signature);
428 printf ("\"status\": %s",
429 json_quote_str (ctx_quote,
430 signature_status_to_string (status)));
432 GMimeCertificate *certificate = g_mime_signature_get_certificate (signature);
433 if (status == GMIME_SIGNATURE_STATUS_GOOD) {
435 printf (", \"fingerprint\": %s", json_quote_str (ctx_quote, g_mime_certificate_get_fingerprint (certificate)));
436 /* these dates are seconds since the epoch; should we
437 * provide a more human-readable format string? */
438 time_t created = g_mime_signature_get_created (signature);
440 printf (", \"created\": %d", (int) created);
441 time_t expires = g_mime_signature_get_expires (signature);
443 printf (", \"expires\": %d", (int) expires);
444 /* output user id only if validity is FULL or ULTIMATE. */
445 /* note that gmime is using the term "trust" here, which
446 * is WRONG. It's actually user id "validity". */
448 const char *name = g_mime_certificate_get_name (certificate);
449 GMimeCertificateTrust trust = g_mime_certificate_get_trust (certificate);
450 if (name && (trust == GMIME_CERTIFICATE_TRUST_FULLY || trust == GMIME_CERTIFICATE_TRUST_ULTIMATE))
451 printf (", \"userid\": %s", json_quote_str (ctx_quote, name));
453 } else if (certificate) {
454 const char *key_id = g_mime_certificate_get_key_id (certificate);
456 printf (", \"keyid\": %s", json_quote_str (ctx_quote, key_id));
459 GMimeSignatureError errors = g_mime_signature_get_errors (signature);
460 if (errors != GMIME_SIGNATURE_ERROR_NONE) {
461 printf (", \"errors\": %d", errors);
469 talloc_free (ctx_quote);
473 format_part_sigstatus_json (const GMimeSignatureValidity* validity)
482 const GMimeSigner *signer = g_mime_signature_validity_get_signers (validity);
484 void *ctx_quote = talloc_new (NULL);
495 printf ("\"status\": %s",
496 json_quote_str (ctx_quote,
497 signer_status_to_string (signer->status)));
499 if (signer->status == GMIME_SIGNER_STATUS_GOOD)
501 if (signer->fingerprint)
502 printf (", \"fingerprint\": %s", json_quote_str (ctx_quote, signer->fingerprint));
503 /* these dates are seconds since the epoch; should we
504 * provide a more human-readable format string? */
506 printf (", \"created\": %d", (int) signer->created);
508 printf (", \"expires\": %d", (int) signer->expires);
509 /* output user id only if validity is FULL or ULTIMATE. */
510 /* note that gmime is using the term "trust" here, which
511 * is WRONG. It's actually user id "validity". */
512 if ((signer->name) && (signer->trust)) {
513 if ((signer->trust == GMIME_SIGNER_TRUST_FULLY) || (signer->trust == GMIME_SIGNER_TRUST_ULTIMATE))
514 printf (", \"userid\": %s", json_quote_str (ctx_quote, signer->name));
518 printf (", \"keyid\": %s", json_quote_str (ctx_quote, signer->keyid));
520 if (signer->errors != GMIME_SIGNER_ERROR_NONE) {
521 printf (", \"errors\": %d", signer->errors);
525 signer = signer->next;
530 talloc_free (ctx_quote);
535 format_part_content_raw (GMimeObject *part)
537 if (! GMIME_IS_PART (part))
540 GMimeStream *stream_stdout;
541 GMimeStream *stream_filter = NULL;
542 GMimeDataWrapper *wrapper;
544 stream_stdout = g_mime_stream_file_new (stdout);
545 g_mime_stream_file_set_owner (GMIME_STREAM_FILE (stream_stdout), FALSE);
547 stream_filter = g_mime_stream_filter_new (stream_stdout);
549 wrapper = g_mime_part_get_content_object (GMIME_PART (part));
551 if (wrapper && stream_filter)
552 g_mime_data_wrapper_write_to_stream (wrapper, stream_filter);
555 g_object_unref (stream_filter);
558 g_object_unref(stream_stdout);
562 format_part_text (const void *ctx, mime_node_t *node,
563 int indent, const notmuch_show_params_t *params)
565 /* The disposition and content-type metadata are associated with
566 * the envelope for message parts */
567 GMimeObject *meta = node->envelope_part ?
568 GMIME_OBJECT (node->envelope_part) : node->part;
569 GMimeContentType *content_type = g_mime_object_get_content_type (meta);
570 const notmuch_bool_t leaf = GMIME_IS_PART (node->part);
571 const char *part_type;
574 if (node->envelope_file) {
575 notmuch_message_t *message = node->envelope_file;
577 part_type = "message";
578 printf ("\f%s{ id:%s depth:%d match:%d filename:%s\n",
580 notmuch_message_get_message_id (message),
582 notmuch_message_get_flag (message, NOTMUCH_MESSAGE_FLAG_MATCH),
583 notmuch_message_get_filename (message));
585 GMimeContentDisposition *disposition = g_mime_object_get_content_disposition (meta);
586 const char *cid = g_mime_object_get_content_id (meta);
587 const char *filename = leaf ?
588 g_mime_part_get_filename (GMIME_PART (node->part)) : NULL;
591 strcmp (disposition->disposition, GMIME_DISPOSITION_ATTACHMENT) == 0)
592 part_type = "attachment";
596 printf ("\f%s{ ID: %d", part_type, node->part_num);
598 printf (", Filename: %s", filename);
600 printf (", Content-id: %s", cid);
601 printf (", Content-type: %s\n", g_mime_content_type_to_string (content_type));
604 if (GMIME_IS_MESSAGE (node->part)) {
605 GMimeMessage *message = GMIME_MESSAGE (node->part);
606 InternetAddressList *recipients;
607 const char *recipients_string;
609 printf ("\fheader{\n");
610 if (node->envelope_file)
611 printf ("%s\n", _get_one_line_summary (ctx, node->envelope_file));
612 printf ("Subject: %s\n", g_mime_message_get_subject (message));
613 printf ("From: %s\n", g_mime_message_get_sender (message));
614 recipients = g_mime_message_get_recipients (message, GMIME_RECIPIENT_TYPE_TO);
615 recipients_string = internet_address_list_to_string (recipients, 0);
616 if (recipients_string)
617 printf ("To: %s\n", recipients_string);
618 recipients = g_mime_message_get_recipients (message, GMIME_RECIPIENT_TYPE_CC);
619 recipients_string = internet_address_list_to_string (recipients, 0);
620 if (recipients_string)
621 printf ("Cc: %s\n", recipients_string);
622 printf ("Date: %s\n", g_mime_message_get_date_as_string (message));
623 printf ("\fheader}\n");
625 printf ("\fbody{\n");
629 if (g_mime_content_type_is_type (content_type, "text", "*") &&
630 !g_mime_content_type_is_type (content_type, "text", "html"))
632 GMimeStream *stream_stdout = g_mime_stream_file_new (stdout);
633 g_mime_stream_file_set_owner (GMIME_STREAM_FILE (stream_stdout), FALSE);
634 show_text_part_content (node->part, stream_stdout);
635 g_object_unref(stream_stdout);
637 printf ("Non-text part: %s\n",
638 g_mime_content_type_to_string (content_type));
642 for (i = 0; i < node->nchildren; i++)
643 format_part_text (ctx, mime_node_child (node, i), indent, params);
645 if (GMIME_IS_MESSAGE (node->part))
646 printf ("\fbody}\n");
648 printf ("\f%s}\n", part_type);
652 format_part_json (const void *ctx, mime_node_t *node, notmuch_bool_t first)
654 /* Any changes to the JSON format should be reflected in the file
657 if (node->envelope_file) {
659 format_message_json (ctx, node->envelope_file);
661 printf ("\"headers\": ");
662 format_headers_json (ctx, GMIME_MESSAGE (node->part));
664 printf (", \"body\": [");
665 format_part_json (ctx, mime_node_child (node, 0), first);
671 void *local = talloc_new (ctx);
672 /* The disposition and content-type metadata are associated with
673 * the envelope for message parts */
674 GMimeObject *meta = node->envelope_part ?
675 GMIME_OBJECT (node->envelope_part) : node->part;
676 GMimeContentType *content_type = g_mime_object_get_content_type (meta);
677 GMimeStream *stream_memory = g_mime_stream_mem_new ();
678 const char *cid = g_mime_object_get_content_id (meta);
679 GByteArray *part_content;
685 printf ("{\"id\": %d", node->part_num);
687 if (node->decrypt_attempted) {
688 printf (", \"encstatus\": [{\"status\": ");
689 if (node->decrypt_success) {
697 if (node->verify_attempted) {
698 printf (", \"sigstatus\": ");
699 #ifdef GMIME_ATLEAST_26
700 format_part_sigstatus_json (node->sig_list);
702 format_part_sigstatus_json (node->sig_validity);
706 printf (", \"content-type\": %s",
707 json_quote_str (local, g_mime_content_type_to_string (content_type)));
710 printf(", \"content-id\": %s", json_quote_str (local, cid));
712 if (GMIME_IS_PART (node->part)) {
713 const char *filename = g_mime_part_get_filename (GMIME_PART (node->part));
715 printf (", \"filename\": %s", json_quote_str (local, filename));
718 if (g_mime_content_type_is_type (content_type, "text", "*")) {
719 /* For non-HTML text parts, we include the content in the
720 * JSON. Since JSON must be Unicode, we handle charset
721 * decoding here and do not report a charset to the caller.
722 * For text/html parts, we do not include the content. If a
723 * caller is interested in text/html parts, it should retrieve
724 * them separately and they will not be decoded. Since this
725 * makes charset decoding the responsibility on the caller, we
726 * report the charset for text/html parts.
728 if (g_mime_content_type_is_type (content_type, "text", "html")) {
729 const char *content_charset = g_mime_object_get_content_type_parameter (meta, "charset");
731 if (content_charset != NULL)
732 printf (", \"content-charset\": %s", json_quote_str (local, content_charset));
734 show_text_part_content (node->part, stream_memory);
735 part_content = g_mime_stream_mem_get_byte_array (GMIME_STREAM_MEM (stream_memory));
737 printf (", \"content\": %s", json_quote_chararray (local, (char *) part_content->data, part_content->len));
739 } else if (g_mime_content_type_is_type (content_type, "multipart", "*")) {
740 printf (", \"content\": [");
741 } else if (g_mime_content_type_is_type (content_type, "message", "rfc822")) {
742 printf (", \"content\": [{");
746 g_object_unref (stream_memory);
748 if (GMIME_IS_MESSAGE (node->part)) {
749 printf ("\"headers\": ");
750 format_headers_json (local, GMIME_MESSAGE (node->part));
752 printf (", \"body\": [");
755 for (i = 0; i < node->nchildren; i++)
756 format_part_json (ctx, mime_node_child (node, i), i == 0);
758 if (GMIME_IS_MESSAGE (node->part))
761 if (g_mime_content_type_is_type (content_type, "multipart", "*"))
763 else if (g_mime_content_type_is_type (content_type, "message", "rfc822"))
772 format_part_json_entry (const void *ctx, mime_node_t *node, unused (int indent),
773 unused (const notmuch_show_params_t *params))
775 format_part_json (ctx, node, TRUE);
779 show_message (void *ctx,
780 const notmuch_show_format_t *format,
781 notmuch_message_t *message,
783 notmuch_show_params_t *params)
786 void *local = talloc_new (ctx);
787 mime_node_t *root, *part;
789 if (mime_node_open (local, message, params->cryptoctx, params->decrypt,
790 &root) == NOTMUCH_STATUS_SUCCESS &&
791 (part = mime_node_seek_dfs (root, (params->part < 0 ?
793 format->part (local, part, indent, params);
798 if (params->part <= 0) {
799 fputs (format->message_start, stdout);
801 format->message(ctx, message, indent);
803 fputs (format->header_start, stdout);
805 format->header(ctx, message);
806 fputs (format->header_end, stdout);
808 fputs (format->body_start, stdout);
811 if (format->part_content)
812 show_message_body (message, format, params);
814 if (params->part <= 0) {
815 fputs (format->body_end, stdout);
817 fputs (format->message_end, stdout);
822 show_messages (void *ctx,
823 const notmuch_show_format_t *format,
824 notmuch_messages_t *messages,
826 notmuch_show_params_t *params)
828 notmuch_message_t *message;
829 notmuch_bool_t match;
833 fputs (format->message_set_start, stdout);
836 notmuch_messages_valid (messages);
837 notmuch_messages_move_to_next (messages))
840 fputs (format->message_set_sep, stdout);
843 fputs (format->message_set_start, stdout);
845 message = notmuch_messages_get (messages);
847 match = notmuch_message_get_flag (message, NOTMUCH_MESSAGE_FLAG_MATCH);
849 next_indent = indent;
851 if (match || params->entire_thread) {
852 show_message (ctx, format, message, indent, params);
853 next_indent = indent + 1;
855 fputs (format->message_set_sep, stdout);
860 notmuch_message_get_replies (message),
864 notmuch_message_destroy (message);
866 fputs (format->message_set_end, stdout);
869 fputs (format->message_set_end, stdout);
872 /* Formatted output of single message */
874 do_show_single (void *ctx,
875 notmuch_query_t *query,
876 const notmuch_show_format_t *format,
877 notmuch_show_params_t *params)
879 notmuch_messages_t *messages;
880 notmuch_message_t *message;
882 if (notmuch_query_count_messages (query) != 1) {
883 fprintf (stderr, "Error: search term did not match precisely one message.\n");
887 messages = notmuch_query_search_messages (query);
888 message = notmuch_messages_get (messages);
890 if (message == NULL) {
891 fprintf (stderr, "Error: Cannot find matching message.\n");
895 notmuch_message_set_flag (message, NOTMUCH_MESSAGE_FLAG_MATCH, 1);
897 /* Special case for --format=raw of full single message, just cat out file */
898 if (params->raw && 0 == params->part) {
900 const char *filename;
905 filename = notmuch_message_get_filename (message);
906 if (filename == NULL) {
907 fprintf (stderr, "Error: Cannot message filename.\n");
911 file = fopen (filename, "r");
913 fprintf (stderr, "Error: Cannot open file %s: %s\n", filename, strerror (errno));
917 while (!feof (file)) {
918 size = fread (buf, 1, sizeof (buf), file);
920 fprintf (stderr, "Error: Read failed from %s\n", filename);
925 if (fwrite (buf, size, 1, stdout) != 1) {
926 fprintf (stderr, "Error: Write failed\n");
936 show_message (ctx, format, message, 0, params);
943 /* Formatted output of threads */
946 notmuch_query_t *query,
947 const notmuch_show_format_t *format,
948 notmuch_show_params_t *params)
950 notmuch_threads_t *threads;
951 notmuch_thread_t *thread;
952 notmuch_messages_t *messages;
953 int first_toplevel = 1;
955 fputs (format->message_set_start, stdout);
957 for (threads = notmuch_query_search_threads (query);
958 notmuch_threads_valid (threads);
959 notmuch_threads_move_to_next (threads))
961 thread = notmuch_threads_get (threads);
963 messages = notmuch_thread_get_toplevel_messages (thread);
965 if (messages == NULL)
966 INTERNAL_ERROR ("Thread %s has no toplevel messages.\n",
967 notmuch_thread_get_thread_id (thread));
970 fputs (format->message_set_sep, stdout);
973 show_messages (ctx, format, messages, 0, params);
975 notmuch_thread_destroy (thread);
979 fputs (format->message_set_end, stdout);
985 NOTMUCH_FORMAT_NOT_SPECIFIED,
993 notmuch_show_command (void *ctx, unused (int argc), unused (char *argv[]))
995 notmuch_config_t *config;
996 notmuch_database_t *notmuch;
997 notmuch_query_t *query;
1000 const notmuch_show_format_t *format = &format_text;
1001 notmuch_show_params_t params = { .part = -1 };
1002 int format_sel = NOTMUCH_FORMAT_NOT_SPECIFIED;
1003 notmuch_bool_t verify = FALSE;
1005 notmuch_opt_desc_t options[] = {
1006 { NOTMUCH_OPT_KEYWORD, &format_sel, "format", 'f',
1007 (notmuch_keyword_t []){ { "json", NOTMUCH_FORMAT_JSON },
1008 { "text", NOTMUCH_FORMAT_TEXT },
1009 { "mbox", NOTMUCH_FORMAT_MBOX },
1010 { "raw", NOTMUCH_FORMAT_RAW },
1012 { NOTMUCH_OPT_INT, ¶ms.part, "part", 'p', 0 },
1013 { NOTMUCH_OPT_BOOLEAN, ¶ms.entire_thread, "entire-thread", 't', 0 },
1014 { NOTMUCH_OPT_BOOLEAN, ¶ms.decrypt, "decrypt", 'd', 0 },
1015 { NOTMUCH_OPT_BOOLEAN, &verify, "verify", 'v', 0 },
1019 opt_index = parse_arguments (argc, argv, options, 1);
1020 if (opt_index < 0) {
1021 /* diagnostics already printed */
1025 if (format_sel == NOTMUCH_FORMAT_NOT_SPECIFIED) {
1026 /* if part was requested and format was not specified, use format=raw */
1027 if (params.part >= 0)
1028 format_sel = NOTMUCH_FORMAT_RAW;
1030 format_sel = NOTMUCH_FORMAT_TEXT;
1033 switch (format_sel) {
1034 case NOTMUCH_FORMAT_JSON:
1035 format = &format_json;
1036 params.entire_thread = TRUE;
1038 case NOTMUCH_FORMAT_TEXT:
1039 format = &format_text;
1041 case NOTMUCH_FORMAT_MBOX:
1042 if (params.part > 0) {
1043 fprintf (stderr, "Error: specifying parts is incompatible with mbox output format.\n");
1046 format = &format_mbox;
1048 case NOTMUCH_FORMAT_RAW:
1049 format = &format_raw;
1050 /* If --format=raw specified without specifying part, we can only
1051 * output single message, so set part=0 */
1052 if (params.part < 0)
1058 if (params.decrypt || verify) {
1059 #ifdef GMIME_ATLEAST_26
1060 /* TODO: GMimePasswordRequestFunc */
1061 params.cryptoctx = g_mime_gpg_context_new (NULL, "gpg");
1063 GMimeSession* session = g_object_new (g_mime_session_get_type(), NULL);
1064 params.cryptoctx = g_mime_gpg_context_new (session, "gpg");
1066 if (params.cryptoctx) {
1067 g_mime_gpg_context_set_always_trust ((GMimeGpgContext*) params.cryptoctx, FALSE);
1069 params.decrypt = FALSE;
1070 fprintf (stderr, "Failed to construct gpg context.\n");
1072 #ifndef GMIME_ATLEAST_26
1073 g_object_unref (session);
1077 config = notmuch_config_open (ctx, NULL, NULL);
1081 query_string = query_string_from_args (ctx, argc-opt_index, argv+opt_index);
1082 if (query_string == NULL) {
1083 fprintf (stderr, "Out of memory\n");
1087 if (*query_string == '\0') {
1088 fprintf (stderr, "Error: notmuch show requires at least one search term.\n");
1092 notmuch = notmuch_database_open (notmuch_config_get_database_path (config),
1093 NOTMUCH_DATABASE_MODE_READ_ONLY);
1094 if (notmuch == NULL)
1097 query = notmuch_query_create (notmuch, query_string);
1098 if (query == NULL) {
1099 fprintf (stderr, "Out of memory\n");
1103 if (params.part >= 0)
1104 ret = do_show_single (ctx, query, format, ¶ms);
1106 ret = do_show (ctx, query, format, ¶ms);
1108 notmuch_query_destroy (query);
1109 notmuch_database_close (notmuch);
1111 if (params.cryptoctx)
1112 g_object_unref(params.cryptoctx);