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_message_json (const void *ctx,
39 notmuch_message_t *message,
42 format_headers_json (const void *ctx,
43 notmuch_message_t *message);
46 format_headers_message_part_json (GMimeMessage *message);
49 format_part_start_json (unused (GMimeObject *part),
53 format_part_encstatus_json (int status);
56 #ifdef GMIME_ATLEAST_26
57 format_part_sigstatus_json (GMimeSignatureList* siglist);
59 format_part_sigstatus_json (const GMimeSignatureValidity* validity);
63 format_part_content_json (GMimeObject *part);
66 format_part_end_json (GMimeObject *part);
68 static const notmuch_show_format_t format_json = {
70 "{", format_message_json,
71 "\"headers\": {", format_headers_json, format_headers_message_part_json, "}",
73 format_part_start_json,
74 format_part_encstatus_json,
75 format_part_sigstatus_json,
76 format_part_content_json,
85 format_message_mbox (const void *ctx,
86 notmuch_message_t *message,
89 static const notmuch_show_format_t format_mbox = {
91 "", format_message_mbox,
106 format_part_content_raw (GMimeObject *part);
108 static const notmuch_show_format_t format_raw = {
111 "", NULL, format_headers_message_part_text, "\n",
116 format_part_content_raw,
125 _get_tags_as_string (const void *ctx, notmuch_message_t *message)
127 notmuch_tags_t *tags;
132 result = talloc_strdup (ctx, "");
136 for (tags = notmuch_message_get_tags (message);
137 notmuch_tags_valid (tags);
138 notmuch_tags_move_to_next (tags))
140 tag = notmuch_tags_get (tags);
142 result = talloc_asprintf_append (result, "%s%s",
143 first ? "" : " ", tag);
150 /* Get a nice, single-line summary of message. */
152 _get_one_line_summary (const void *ctx, notmuch_message_t *message)
156 const char *relative_date;
159 from = notmuch_message_get_header (message, "from");
161 date = notmuch_message_get_date (message);
162 relative_date = notmuch_time_relative_date (ctx, date);
164 tags = _get_tags_as_string (ctx, message);
166 return talloc_asprintf (ctx, "%s (%s) (%s)",
167 from, relative_date, tags);
171 format_message_json (const void *ctx, notmuch_message_t *message, unused (int indent))
173 notmuch_tags_t *tags;
175 void *ctx_quote = talloc_new (ctx);
177 const char *relative_date;
179 date = notmuch_message_get_date (message);
180 relative_date = notmuch_time_relative_date (ctx, date);
182 printf ("\"id\": %s, \"match\": %s, \"filename\": %s, \"timestamp\": %ld, \"date_relative\": \"%s\", \"tags\": [",
183 json_quote_str (ctx_quote, notmuch_message_get_message_id (message)),
184 notmuch_message_get_flag (message, NOTMUCH_MESSAGE_FLAG_MATCH) ? "true" : "false",
185 json_quote_str (ctx_quote, notmuch_message_get_filename (message)),
186 date, relative_date);
188 for (tags = notmuch_message_get_tags (message);
189 notmuch_tags_valid (tags);
190 notmuch_tags_move_to_next (tags))
192 printf("%s%s", first ? "" : ",",
193 json_quote_str (ctx_quote, notmuch_tags_get (tags)));
197 talloc_free (ctx_quote);
200 /* Extract just the email address from the contents of a From:
203 _extract_email_address (const void *ctx, const char *from)
205 InternetAddressList *addresses;
206 InternetAddress *address;
207 InternetAddressMailbox *mailbox;
208 const char *email = "MAILER-DAEMON";
210 addresses = internet_address_list_parse_string (from);
212 /* Bail if there is no address here. */
213 if (addresses == NULL || internet_address_list_length (addresses) < 1)
216 /* Otherwise, just use the first address. */
217 address = internet_address_list_get_address (addresses, 0);
219 /* The From header should never contain an address group rather
220 * than a mailbox. So bail if it does. */
221 if (! INTERNET_ADDRESS_IS_MAILBOX (address))
224 mailbox = INTERNET_ADDRESS_MAILBOX (address);
225 email = internet_address_mailbox_get_addr (mailbox);
226 email = talloc_strdup (ctx, email);
230 g_object_unref (addresses);
235 /* Return 1 if 'line' is an mbox From_ line---that is, a line
236 * beginning with zero or more '>' characters followed by the
237 * characters 'F', 'r', 'o', 'm', and space.
239 * Any characters at all may appear after that in the line.
242 _is_from_line (const char *line)
244 const char *s = line;
252 if (STRNCMP_LITERAL (s, "From ") == 0)
258 /* Print a message in "mboxrd" format as documented, for example,
261 * http://qmail.org/qmail-manual-html/man5/mbox.html
264 format_message_mbox (const void *ctx,
265 notmuch_message_t *message,
268 const char *filename;
273 struct tm date_gmtime;
274 char date_asctime[26];
280 filename = notmuch_message_get_filename (message);
281 file = fopen (filename, "r");
283 fprintf (stderr, "Failed to open %s: %s\n",
284 filename, strerror (errno));
288 from = notmuch_message_get_header (message, "from");
289 from = _extract_email_address (ctx, from);
291 date = notmuch_message_get_date (message);
292 gmtime_r (&date, &date_gmtime);
293 asctime_r (&date_gmtime, date_asctime);
295 printf ("From %s %s", from, date_asctime);
297 while ((line_len = getline (&line, &line_size, file)) != -1 ) {
298 if (_is_from_line (line))
309 format_headers_message_part_text (GMimeMessage *message)
311 InternetAddressList *recipients;
312 const char *recipients_string;
314 printf ("Subject: %s\n", g_mime_message_get_subject (message));
315 printf ("From: %s\n", g_mime_message_get_sender (message));
316 recipients = g_mime_message_get_recipients (message, GMIME_RECIPIENT_TYPE_TO);
317 recipients_string = internet_address_list_to_string (recipients, 0);
318 if (recipients_string)
321 recipients = g_mime_message_get_recipients (message, GMIME_RECIPIENT_TYPE_CC);
322 recipients_string = internet_address_list_to_string (recipients, 0);
323 if (recipients_string)
326 printf ("Date: %s\n", g_mime_message_get_date_as_string (message));
330 format_headers_json (const void *ctx, notmuch_message_t *message)
332 const char *headers[] = {
333 "Subject", "From", "To", "Cc", "Bcc", "Date"
335 const char *name, *value;
337 int first_header = 1;
338 void *ctx_quote = talloc_new (ctx);
340 for (i = 0; i < ARRAY_SIZE (headers); i++) {
342 value = notmuch_message_get_header (message, name);
346 fputs (", ", stdout);
350 json_quote_str (ctx_quote, name),
351 json_quote_str (ctx_quote, value));
355 talloc_free (ctx_quote);
359 format_headers_message_part_json (GMimeMessage *message)
361 void *ctx = talloc_new (NULL);
362 void *ctx_quote = talloc_new (ctx);
363 InternetAddressList *recipients;
364 const char *recipients_string;
367 json_quote_str (ctx_quote, "From"),
368 json_quote_str (ctx_quote, g_mime_message_get_sender (message)));
369 recipients = g_mime_message_get_recipients (message, GMIME_RECIPIENT_TYPE_TO);
370 recipients_string = internet_address_list_to_string (recipients, 0);
371 if (recipients_string)
373 json_quote_str (ctx_quote, "To"),
374 json_quote_str (ctx_quote, recipients_string));
375 recipients = g_mime_message_get_recipients (message, GMIME_RECIPIENT_TYPE_CC);
376 recipients_string = internet_address_list_to_string (recipients, 0);
377 if (recipients_string)
379 json_quote_str (ctx_quote, "Cc"),
380 json_quote_str (ctx_quote, recipients_string));
382 json_quote_str (ctx_quote, "Subject"),
383 json_quote_str (ctx_quote, g_mime_message_get_subject (message)));
385 json_quote_str (ctx_quote, "Date"),
386 json_quote_str (ctx_quote, g_mime_message_get_date_as_string (message)));
388 talloc_free (ctx_quote);
392 /* Write a MIME text part out to the given stream.
394 * Both line-ending conversion (CRLF->LF) and charset conversion ( ->
395 * UTF-8) will be performed, so it is inappropriate to call this
396 * function with a non-text part. Doing so will trigger an internal
400 show_text_part_content (GMimeObject *part, GMimeStream *stream_out)
402 GMimeContentType *content_type = g_mime_object_get_content_type (GMIME_OBJECT (part));
403 GMimeStream *stream_filter = NULL;
404 GMimeDataWrapper *wrapper;
407 if (! g_mime_content_type_is_type (content_type, "text", "*"))
408 INTERNAL_ERROR ("Illegal request to format non-text part (%s) as text.",
409 g_mime_content_type_to_string (content_type));
411 if (stream_out == NULL)
414 stream_filter = g_mime_stream_filter_new (stream_out);
415 g_mime_stream_filter_add(GMIME_STREAM_FILTER (stream_filter),
416 g_mime_filter_crlf_new (FALSE, FALSE));
418 charset = g_mime_object_get_content_type_parameter (part, "charset");
420 GMimeFilter *charset_filter;
421 charset_filter = g_mime_filter_charset_new (charset, "UTF-8");
422 /* This result can be NULL for things like "unknown-8bit".
423 * Don't set a NULL filter as that makes GMime print
424 * annoying assertion-failure messages on stderr. */
425 if (charset_filter) {
426 g_mime_stream_filter_add (GMIME_STREAM_FILTER (stream_filter),
428 g_object_unref (charset_filter);
433 wrapper = g_mime_part_get_content_object (GMIME_PART (part));
434 if (wrapper && stream_filter)
435 g_mime_data_wrapper_write_to_stream (wrapper, stream_filter);
437 g_object_unref(stream_filter);
440 #ifdef GMIME_ATLEAST_26
442 signature_status_to_string (GMimeSignatureStatus x)
445 case GMIME_SIGNATURE_STATUS_GOOD:
447 case GMIME_SIGNATURE_STATUS_BAD:
449 case GMIME_SIGNATURE_STATUS_ERROR:
456 signer_status_to_string (GMimeSignerStatus x)
459 case GMIME_SIGNER_STATUS_NONE:
461 case GMIME_SIGNER_STATUS_GOOD:
463 case GMIME_SIGNER_STATUS_BAD:
465 case GMIME_SIGNER_STATUS_ERROR:
473 format_part_start_json (unused (GMimeObject *part), int *part_count)
475 printf ("{\"id\": %d", *part_count);
479 format_part_encstatus_json (int status)
481 printf (", \"encstatus\": [{\"status\": ");
490 #ifdef GMIME_ATLEAST_26
492 format_part_sigstatus_json (GMimeSignatureList *siglist)
494 printf (", \"sigstatus\": [");
501 void *ctx_quote = talloc_new (NULL);
503 for (i = 0; i < g_mime_signature_list_length (siglist); i++) {
504 GMimeSignature *signature = g_mime_signature_list_get_signature (siglist, i);
512 GMimeSignatureStatus status = g_mime_signature_get_status (signature);
513 printf ("\"status\": %s",
514 json_quote_str (ctx_quote,
515 signature_status_to_string (status)));
517 GMimeCertificate *certificate = g_mime_signature_get_certificate (signature);
518 if (status == GMIME_SIGNATURE_STATUS_GOOD) {
520 printf (", \"fingerprint\": %s", json_quote_str (ctx_quote, g_mime_certificate_get_fingerprint (certificate)));
521 /* these dates are seconds since the epoch; should we
522 * provide a more human-readable format string? */
523 time_t created = g_mime_signature_get_created (signature);
525 printf (", \"created\": %d", (int) created);
526 time_t expires = g_mime_signature_get_expires (signature);
528 printf (", \"expires\": %d", (int) expires);
529 /* output user id only if validity is FULL or ULTIMATE. */
530 /* note that gmime is using the term "trust" here, which
531 * is WRONG. It's actually user id "validity". */
533 const char *name = g_mime_certificate_get_name (certificate);
534 GMimeCertificateTrust trust = g_mime_certificate_get_trust (certificate);
535 if (name && (trust == GMIME_CERTIFICATE_TRUST_FULLY || trust == GMIME_CERTIFICATE_TRUST_ULTIMATE))
536 printf (", \"userid\": %s", json_quote_str (ctx_quote, name));
538 } else if (certificate) {
539 const char *key_id = g_mime_certificate_get_key_id (certificate);
541 printf (", \"keyid\": %s", json_quote_str (ctx_quote, key_id));
544 GMimeSignatureError errors = g_mime_signature_get_errors (signature);
545 if (errors != GMIME_SIGNATURE_ERROR_NONE) {
546 printf (", \"errors\": %d", errors);
554 talloc_free (ctx_quote);
558 format_part_sigstatus_json (const GMimeSignatureValidity* validity)
560 printf (", \"sigstatus\": [");
567 const GMimeSigner *signer = g_mime_signature_validity_get_signers (validity);
569 void *ctx_quote = talloc_new (NULL);
580 printf ("\"status\": %s",
581 json_quote_str (ctx_quote,
582 signer_status_to_string (signer->status)));
584 if (signer->status == GMIME_SIGNER_STATUS_GOOD)
586 if (signer->fingerprint)
587 printf (", \"fingerprint\": %s", json_quote_str (ctx_quote, signer->fingerprint));
588 /* these dates are seconds since the epoch; should we
589 * provide a more human-readable format string? */
591 printf (", \"created\": %d", (int) signer->created);
593 printf (", \"expires\": %d", (int) signer->expires);
594 /* output user id only if validity is FULL or ULTIMATE. */
595 /* note that gmime is using the term "trust" here, which
596 * is WRONG. It's actually user id "validity". */
597 if ((signer->name) && (signer->trust)) {
598 if ((signer->trust == GMIME_SIGNER_TRUST_FULLY) || (signer->trust == GMIME_SIGNER_TRUST_ULTIMATE))
599 printf (", \"userid\": %s", json_quote_str (ctx_quote, signer->name));
603 printf (", \"keyid\": %s", json_quote_str (ctx_quote, signer->keyid));
605 if (signer->errors != GMIME_SIGNER_ERROR_NONE) {
606 printf (", \"errors\": %d", signer->errors);
610 signer = signer->next;
615 talloc_free (ctx_quote);
620 format_part_content_json (GMimeObject *part)
622 GMimeContentType *content_type = g_mime_object_get_content_type (GMIME_OBJECT (part));
623 GMimeStream *stream_memory = g_mime_stream_mem_new ();
624 const char *cid = g_mime_object_get_content_id (part);
625 void *ctx = talloc_new (NULL);
626 GByteArray *part_content;
628 printf (", \"content-type\": %s",
629 json_quote_str (ctx, g_mime_content_type_to_string (content_type)));
632 printf(", \"content-id\": %s", json_quote_str (ctx, cid));
634 if (GMIME_IS_PART (part))
636 const char *filename = g_mime_part_get_filename (GMIME_PART (part));
638 printf (", \"filename\": %s", json_quote_str (ctx, filename));
641 if (g_mime_content_type_is_type (content_type, "text", "*"))
643 /* For non-HTML text parts, we include the content in the
644 * JSON. Since JSON must be Unicode, we handle charset
645 * decoding here and do not report a charset to the caller.
646 * For text/html parts, we do not include the content. If a
647 * caller is interested in text/html parts, it should retrieve
648 * them separately and they will not be decoded. Since this
649 * makes charset decoding the responsibility on the caller, we
650 * report the charset for text/html parts.
652 if (g_mime_content_type_is_type (content_type, "text", "html"))
654 const char *content_charset = g_mime_object_get_content_type_parameter (GMIME_OBJECT (part), "charset");
656 if (content_charset != NULL)
657 printf (", \"content-charset\": %s", json_quote_str (ctx, content_charset));
661 show_text_part_content (part, stream_memory);
662 part_content = g_mime_stream_mem_get_byte_array (GMIME_STREAM_MEM (stream_memory));
664 printf (", \"content\": %s", json_quote_chararray (ctx, (char *) part_content->data, part_content->len));
667 else if (g_mime_content_type_is_type (content_type, "multipart", "*"))
669 printf (", \"content\": [");
671 else if (g_mime_content_type_is_type (content_type, "message", "rfc822"))
673 printf (", \"content\": [{");
678 g_object_unref (stream_memory);
682 format_part_end_json (GMimeObject *part)
684 GMimeContentType *content_type = g_mime_object_get_content_type (GMIME_OBJECT (part));
686 if (g_mime_content_type_is_type (content_type, "multipart", "*"))
688 else if (g_mime_content_type_is_type (content_type, "message", "rfc822"))
695 format_part_content_raw (GMimeObject *part)
697 if (! GMIME_IS_PART (part))
700 GMimeStream *stream_stdout;
701 GMimeStream *stream_filter = NULL;
702 GMimeDataWrapper *wrapper;
704 stream_stdout = g_mime_stream_file_new (stdout);
705 g_mime_stream_file_set_owner (GMIME_STREAM_FILE (stream_stdout), FALSE);
707 stream_filter = g_mime_stream_filter_new (stream_stdout);
709 wrapper = g_mime_part_get_content_object (GMIME_PART (part));
711 if (wrapper && stream_filter)
712 g_mime_data_wrapper_write_to_stream (wrapper, stream_filter);
715 g_object_unref (stream_filter);
718 g_object_unref(stream_stdout);
722 format_part_text (const void *ctx, mime_node_t *node,
723 int indent, const notmuch_show_params_t *params)
725 /* The disposition and content-type metadata are associated with
726 * the envelope for message parts */
727 GMimeObject *meta = node->envelope_part ?
728 GMIME_OBJECT (node->envelope_part) : node->part;
729 GMimeContentType *content_type = g_mime_object_get_content_type (meta);
730 const notmuch_bool_t leaf = GMIME_IS_PART (node->part);
731 const char *part_type;
734 if (node->envelope_file) {
735 notmuch_message_t *message = node->envelope_file;
737 part_type = "message";
738 printf ("\f%s{ id:%s depth:%d match:%d filename:%s\n",
740 notmuch_message_get_message_id (message),
742 notmuch_message_get_flag (message, NOTMUCH_MESSAGE_FLAG_MATCH),
743 notmuch_message_get_filename (message));
745 GMimeContentDisposition *disposition = g_mime_object_get_content_disposition (meta);
746 const char *cid = g_mime_object_get_content_id (meta);
747 const char *filename = leaf ?
748 g_mime_part_get_filename (GMIME_PART (node->part)) : NULL;
751 strcmp (disposition->disposition, GMIME_DISPOSITION_ATTACHMENT) == 0)
752 part_type = "attachment";
756 printf ("\f%s{ ID: %d", part_type, node->part_num);
758 printf (", Filename: %s", filename);
760 printf (", Content-id: %s", cid);
761 printf (", Content-type: %s\n", g_mime_content_type_to_string (content_type));
764 if (GMIME_IS_MESSAGE (node->part)) {
765 GMimeMessage *message = GMIME_MESSAGE (node->part);
766 InternetAddressList *recipients;
767 const char *recipients_string;
769 printf ("\fheader{\n");
770 if (node->envelope_file)
771 printf ("%s\n", _get_one_line_summary (ctx, node->envelope_file));
772 printf ("Subject: %s\n", g_mime_message_get_subject (message));
773 printf ("From: %s\n", g_mime_message_get_sender (message));
774 recipients = g_mime_message_get_recipients (message, GMIME_RECIPIENT_TYPE_TO);
775 recipients_string = internet_address_list_to_string (recipients, 0);
776 if (recipients_string)
777 printf ("To: %s\n", recipients_string);
778 recipients = g_mime_message_get_recipients (message, GMIME_RECIPIENT_TYPE_CC);
779 recipients_string = internet_address_list_to_string (recipients, 0);
780 if (recipients_string)
781 printf ("Cc: %s\n", recipients_string);
782 printf ("Date: %s\n", g_mime_message_get_date_as_string (message));
783 printf ("\fheader}\n");
785 printf ("\fbody{\n");
789 if (g_mime_content_type_is_type (content_type, "text", "*") &&
790 !g_mime_content_type_is_type (content_type, "text", "html"))
792 GMimeStream *stream_stdout = g_mime_stream_file_new (stdout);
793 g_mime_stream_file_set_owner (GMIME_STREAM_FILE (stream_stdout), FALSE);
794 show_text_part_content (node->part, stream_stdout);
795 g_object_unref(stream_stdout);
797 printf ("Non-text part: %s\n",
798 g_mime_content_type_to_string (content_type));
802 for (i = 0; i < node->nchildren; i++)
803 format_part_text (ctx, mime_node_child (node, i), indent, params);
805 if (GMIME_IS_MESSAGE (node->part))
806 printf ("\fbody}\n");
808 printf ("\f%s}\n", part_type);
812 show_message (void *ctx,
813 const notmuch_show_format_t *format,
814 notmuch_message_t *message,
816 notmuch_show_params_t *params)
819 void *local = talloc_new (ctx);
820 mime_node_t *root, *part;
822 if (mime_node_open (local, message, params->cryptoctx, params->decrypt,
823 &root) == NOTMUCH_STATUS_SUCCESS &&
824 (part = mime_node_seek_dfs (root, (params->part < 0 ?
826 format->part (local, part, indent, params);
831 if (params->part <= 0) {
832 fputs (format->message_start, stdout);
834 format->message(ctx, message, indent);
836 fputs (format->header_start, stdout);
838 format->header(ctx, message);
839 fputs (format->header_end, stdout);
841 fputs (format->body_start, stdout);
844 if (format->part_content)
845 show_message_body (message, format, params);
847 if (params->part <= 0) {
848 fputs (format->body_end, stdout);
850 fputs (format->message_end, stdout);
855 show_messages (void *ctx,
856 const notmuch_show_format_t *format,
857 notmuch_messages_t *messages,
859 notmuch_show_params_t *params)
861 notmuch_message_t *message;
862 notmuch_bool_t match;
866 fputs (format->message_set_start, stdout);
869 notmuch_messages_valid (messages);
870 notmuch_messages_move_to_next (messages))
873 fputs (format->message_set_sep, stdout);
876 fputs (format->message_set_start, stdout);
878 message = notmuch_messages_get (messages);
880 match = notmuch_message_get_flag (message, NOTMUCH_MESSAGE_FLAG_MATCH);
882 next_indent = indent;
884 if (match || params->entire_thread) {
885 show_message (ctx, format, message, indent, params);
886 next_indent = indent + 1;
888 fputs (format->message_set_sep, stdout);
893 notmuch_message_get_replies (message),
897 notmuch_message_destroy (message);
899 fputs (format->message_set_end, stdout);
902 fputs (format->message_set_end, stdout);
905 /* Formatted output of single message */
907 do_show_single (void *ctx,
908 notmuch_query_t *query,
909 const notmuch_show_format_t *format,
910 notmuch_show_params_t *params)
912 notmuch_messages_t *messages;
913 notmuch_message_t *message;
915 if (notmuch_query_count_messages (query) != 1) {
916 fprintf (stderr, "Error: search term did not match precisely one message.\n");
920 messages = notmuch_query_search_messages (query);
921 message = notmuch_messages_get (messages);
923 if (message == NULL) {
924 fprintf (stderr, "Error: Cannot find matching message.\n");
928 notmuch_message_set_flag (message, NOTMUCH_MESSAGE_FLAG_MATCH, 1);
930 /* Special case for --format=raw of full single message, just cat out file */
931 if (params->raw && 0 == params->part) {
933 const char *filename;
938 filename = notmuch_message_get_filename (message);
939 if (filename == NULL) {
940 fprintf (stderr, "Error: Cannot message filename.\n");
944 file = fopen (filename, "r");
946 fprintf (stderr, "Error: Cannot open file %s: %s\n", filename, strerror (errno));
950 while (!feof (file)) {
951 size = fread (buf, 1, sizeof (buf), file);
953 fprintf (stderr, "Error: Read failed from %s\n", filename);
958 if (fwrite (buf, size, 1, stdout) != 1) {
959 fprintf (stderr, "Error: Write failed\n");
969 show_message (ctx, format, message, 0, params);
976 /* Formatted output of threads */
979 notmuch_query_t *query,
980 const notmuch_show_format_t *format,
981 notmuch_show_params_t *params)
983 notmuch_threads_t *threads;
984 notmuch_thread_t *thread;
985 notmuch_messages_t *messages;
986 int first_toplevel = 1;
988 fputs (format->message_set_start, stdout);
990 for (threads = notmuch_query_search_threads (query);
991 notmuch_threads_valid (threads);
992 notmuch_threads_move_to_next (threads))
994 thread = notmuch_threads_get (threads);
996 messages = notmuch_thread_get_toplevel_messages (thread);
998 if (messages == NULL)
999 INTERNAL_ERROR ("Thread %s has no toplevel messages.\n",
1000 notmuch_thread_get_thread_id (thread));
1002 if (!first_toplevel)
1003 fputs (format->message_set_sep, stdout);
1006 show_messages (ctx, format, messages, 0, params);
1008 notmuch_thread_destroy (thread);
1012 fputs (format->message_set_end, stdout);
1018 NOTMUCH_FORMAT_NOT_SPECIFIED,
1019 NOTMUCH_FORMAT_JSON,
1020 NOTMUCH_FORMAT_TEXT,
1021 NOTMUCH_FORMAT_MBOX,
1026 notmuch_show_command (void *ctx, unused (int argc), unused (char *argv[]))
1028 notmuch_config_t *config;
1029 notmuch_database_t *notmuch;
1030 notmuch_query_t *query;
1033 const notmuch_show_format_t *format = &format_text;
1034 notmuch_show_params_t params = { .part = -1 };
1035 int format_sel = NOTMUCH_FORMAT_NOT_SPECIFIED;
1036 notmuch_bool_t verify = FALSE;
1038 notmuch_opt_desc_t options[] = {
1039 { NOTMUCH_OPT_KEYWORD, &format_sel, "format", 'f',
1040 (notmuch_keyword_t []){ { "json", NOTMUCH_FORMAT_JSON },
1041 { "text", NOTMUCH_FORMAT_TEXT },
1042 { "mbox", NOTMUCH_FORMAT_MBOX },
1043 { "raw", NOTMUCH_FORMAT_RAW },
1045 { NOTMUCH_OPT_INT, ¶ms.part, "part", 'p', 0 },
1046 { NOTMUCH_OPT_BOOLEAN, ¶ms.entire_thread, "entire-thread", 't', 0 },
1047 { NOTMUCH_OPT_BOOLEAN, ¶ms.decrypt, "decrypt", 'd', 0 },
1048 { NOTMUCH_OPT_BOOLEAN, &verify, "verify", 'v', 0 },
1052 opt_index = parse_arguments (argc, argv, options, 1);
1053 if (opt_index < 0) {
1054 /* diagnostics already printed */
1058 if (format_sel == NOTMUCH_FORMAT_NOT_SPECIFIED) {
1059 /* if part was requested and format was not specified, use format=raw */
1060 if (params.part >= 0)
1061 format_sel = NOTMUCH_FORMAT_RAW;
1063 format_sel = NOTMUCH_FORMAT_TEXT;
1066 switch (format_sel) {
1067 case NOTMUCH_FORMAT_JSON:
1068 format = &format_json;
1069 params.entire_thread = TRUE;
1071 case NOTMUCH_FORMAT_TEXT:
1072 format = &format_text;
1074 case NOTMUCH_FORMAT_MBOX:
1075 if (params.part > 0) {
1076 fprintf (stderr, "Error: specifying parts is incompatible with mbox output format.\n");
1079 format = &format_mbox;
1081 case NOTMUCH_FORMAT_RAW:
1082 format = &format_raw;
1083 /* If --format=raw specified without specifying part, we can only
1084 * output single message, so set part=0 */
1085 if (params.part < 0)
1091 if (params.decrypt || verify) {
1092 #ifdef GMIME_ATLEAST_26
1093 /* TODO: GMimePasswordRequestFunc */
1094 params.cryptoctx = g_mime_gpg_context_new (NULL, "gpg");
1096 GMimeSession* session = g_object_new (g_mime_session_get_type(), NULL);
1097 params.cryptoctx = g_mime_gpg_context_new (session, "gpg");
1099 if (params.cryptoctx) {
1100 g_mime_gpg_context_set_always_trust ((GMimeGpgContext*) params.cryptoctx, FALSE);
1102 params.decrypt = FALSE;
1103 fprintf (stderr, "Failed to construct gpg context.\n");
1105 #ifndef GMIME_ATLEAST_26
1106 g_object_unref (session);
1110 config = notmuch_config_open (ctx, NULL, NULL);
1114 query_string = query_string_from_args (ctx, argc-opt_index, argv+opt_index);
1115 if (query_string == NULL) {
1116 fprintf (stderr, "Out of memory\n");
1120 if (*query_string == '\0') {
1121 fprintf (stderr, "Error: notmuch show requires at least one search term.\n");
1125 notmuch = notmuch_database_open (notmuch_config_get_database_path (config),
1126 NOTMUCH_DATABASE_MODE_READ_ONLY);
1127 if (notmuch == NULL)
1130 query = notmuch_query_create (notmuch, query_string);
1131 if (query == NULL) {
1132 fprintf (stderr, "Out of memory\n");
1136 if (params.part >= 0)
1137 ret = do_show_single (ctx, query, format, ¶ms);
1139 ret = do_show (ctx, query, format, ¶ms);
1141 notmuch_query_destroy (query);
1142 notmuch_database_close (notmuch);
1144 if (params.cryptoctx)
1145 g_object_unref(params.cryptoctx);