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 https://www.gnu.org/licenses/ .
18 * Author: Carl Worth <cworth@cworth.org>
21 #include "notmuch-client.h"
22 #include "gmime-filter-reply.h"
24 #include "zlib-extra.h"
27 _get_tags_as_string (const void *ctx, notmuch_message_t *message)
34 result = talloc_strdup (ctx, "");
38 for (tags = notmuch_message_get_tags (message);
39 notmuch_tags_valid (tags);
40 notmuch_tags_move_to_next (tags)) {
41 tag = notmuch_tags_get (tags);
43 result = talloc_asprintf_append (result, "%s%s",
44 first ? "" : " ", tag);
51 /* Get a nice, single-line summary of message. */
53 _get_one_line_summary (const void *ctx, notmuch_message_t *message)
57 const char *relative_date;
60 from = notmuch_message_get_header (message, "from");
62 date = notmuch_message_get_date (message);
63 relative_date = notmuch_time_relative_date (ctx, date);
65 tags = _get_tags_as_string (ctx, message);
67 return talloc_asprintf (ctx, "%s (%s) (%s)",
68 from, relative_date, tags);
72 _get_disposition (GMimeObject *meta)
74 GMimeContentDisposition *disposition;
76 disposition = g_mime_object_get_content_disposition (meta);
80 return g_mime_content_disposition_get_disposition (disposition);
83 static bool _get_message_flag (notmuch_message_t *message, notmuch_message_flag_t flag) {
84 notmuch_bool_t is_set;
85 notmuch_status_t status;
87 status = notmuch_message_get_flag_st (message, flag, &is_set);
89 if (print_status_message ("notmuch show", message, status))
90 INTERNAL_ERROR("unexpected error getting message flag\n");
95 /* Emit a sequence of key/value pairs for the metadata of message.
96 * The caller should begin a map before calling this. */
98 format_message_sprinter (sprinter_t *sp, notmuch_message_t *message)
100 /* Any changes to the JSON or S-Expression format should be
101 * reflected in the file devel/schemata. */
103 void *local = talloc_new (NULL);
104 notmuch_tags_t *tags;
106 const char *relative_date;
108 sp->map_key (sp, "id");
109 sp->string (sp, notmuch_message_get_message_id (message));
111 sp->map_key (sp, "match");
112 sp->boolean (sp, _get_message_flag (message, NOTMUCH_MESSAGE_FLAG_MATCH));
114 sp->map_key (sp, "excluded");
115 sp->boolean (sp, _get_message_flag (message, NOTMUCH_MESSAGE_FLAG_EXCLUDED));
117 sp->map_key (sp, "filename");
118 if (notmuch_format_version >= 3) {
119 notmuch_filenames_t *filenames;
122 for (filenames = notmuch_message_get_filenames (message);
123 notmuch_filenames_valid (filenames);
124 notmuch_filenames_move_to_next (filenames)) {
125 sp->string (sp, notmuch_filenames_get (filenames));
127 notmuch_filenames_destroy (filenames);
130 sp->string (sp, notmuch_message_get_filename (message));
133 sp->map_key (sp, "timestamp");
134 date = notmuch_message_get_date (message);
135 sp->integer (sp, date);
137 sp->map_key (sp, "date_relative");
138 relative_date = notmuch_time_relative_date (local, date);
139 sp->string (sp, relative_date);
141 sp->map_key (sp, "tags");
143 for (tags = notmuch_message_get_tags (message);
144 notmuch_tags_valid (tags);
145 notmuch_tags_move_to_next (tags))
146 sp->string (sp, notmuch_tags_get (tags));
152 /* Extract just the email address from the contents of a From:
155 _extract_email_address (const void *ctx, const char *from)
157 InternetAddressList *addresses;
158 InternetAddress *address;
159 InternetAddressMailbox *mailbox;
160 const char *email = "MAILER-DAEMON";
162 addresses = internet_address_list_parse (NULL, from);
164 /* Bail if there is no address here. */
165 if (addresses == NULL || internet_address_list_length (addresses) < 1)
168 /* Otherwise, just use the first address. */
169 address = internet_address_list_get_address (addresses, 0);
171 /* The From header should never contain an address group rather
172 * than a mailbox. So bail if it does. */
173 if (! INTERNET_ADDRESS_IS_MAILBOX (address))
176 mailbox = INTERNET_ADDRESS_MAILBOX (address);
177 email = internet_address_mailbox_get_addr (mailbox);
178 email = talloc_strdup (ctx, email);
182 g_object_unref (addresses);
187 /* Return 1 if 'line' is an mbox From_ line---that is, a line
188 * beginning with zero or more '>' characters followed by the
189 * characters 'F', 'r', 'o', 'm', and space.
191 * Any characters at all may appear after that in the line.
194 _is_from_line (const char *line)
196 const char *s = line;
204 if (STRNCMP_LITERAL (s, "From ") == 0)
211 format_headers_sprinter (sprinter_t *sp, GMimeMessage *message,
212 bool reply, const _notmuch_message_crypto_t *msg_crypto)
214 /* Any changes to the JSON or S-Expression format should be
215 * reflected in the file devel/schemata. */
217 char *recipients_string;
218 const char *reply_to_string;
219 void *local = talloc_new (sp);
223 sp->map_key (sp, "Subject");
224 if (msg_crypto && msg_crypto->payload_subject) {
225 sp->string (sp, msg_crypto->payload_subject);
227 sp->string (sp, g_mime_message_get_subject (message));
229 sp->map_key (sp, "From");
230 sp->string (sp, g_mime_message_get_from_string (message));
232 recipients_string = g_mime_message_get_address_string (message, GMIME_ADDRESS_TYPE_TO);
233 if (recipients_string) {
234 sp->map_key (sp, "To");
235 sp->string (sp, recipients_string);
236 g_free (recipients_string);
239 recipients_string = g_mime_message_get_address_string (message, GMIME_ADDRESS_TYPE_CC);
240 if (recipients_string) {
241 sp->map_key (sp, "Cc");
242 sp->string (sp, recipients_string);
243 g_free (recipients_string);
246 recipients_string = g_mime_message_get_address_string (message, GMIME_ADDRESS_TYPE_BCC);
247 if (recipients_string) {
248 sp->map_key (sp, "Bcc");
249 sp->string (sp, recipients_string);
250 g_free (recipients_string);
253 reply_to_string = g_mime_message_get_reply_to_string (local, message);
254 if (reply_to_string) {
255 sp->map_key (sp, "Reply-To");
256 sp->string (sp, reply_to_string);
260 sp->map_key (sp, "In-reply-to");
261 sp->string (sp, g_mime_object_get_header (GMIME_OBJECT (message), "In-reply-to"));
263 sp->map_key (sp, "References");
264 sp->string (sp, g_mime_object_get_header (GMIME_OBJECT (message), "References"));
266 sp->map_key (sp, "Date");
267 sp->string (sp, g_mime_message_get_date_string (sp, message));
274 /* Write a MIME text part out to the given stream.
276 * If (flags & NOTMUCH_SHOW_TEXT_PART_REPLY), this prepends "> " to
279 * Both line-ending conversion (CRLF->LF) and charset conversion ( ->
280 * UTF-8) will be performed, so it is inappropriate to call this
281 * function with a non-text part. Doing so will trigger an internal
285 show_text_part_content (GMimeObject *part, GMimeStream *stream_out,
286 notmuch_show_text_part_flags flags)
288 GMimeContentType *content_type = g_mime_object_get_content_type (GMIME_OBJECT (part));
289 GMimeStream *stream_filter = NULL;
290 GMimeFilter *crlf_filter = NULL;
291 GMimeFilter *windows_filter = NULL;
292 GMimeDataWrapper *wrapper;
295 if (! g_mime_content_type_is_type (content_type, "text", "*"))
296 INTERNAL_ERROR ("Illegal request to format non-text part (%s) as text.",
297 g_mime_content_type_get_mime_type (content_type));
299 if (stream_out == NULL)
302 charset = g_mime_object_get_content_type_parameter (part, "charset");
303 charset = charset ? g_mime_charset_canon_name (charset) : NULL;
304 wrapper = g_mime_part_get_content (GMIME_PART (part));
305 if (wrapper && charset && ! g_ascii_strncasecmp (charset, "iso-8859-", 9)) {
306 GMimeStream *null_stream = NULL;
307 GMimeStream *null_stream_filter = NULL;
309 /* Check for mislabeled Windows encoding */
310 null_stream = g_mime_stream_null_new ();
311 null_stream_filter = g_mime_stream_filter_new (null_stream);
312 windows_filter = g_mime_filter_windows_new (charset);
313 g_mime_stream_filter_add (GMIME_STREAM_FILTER (null_stream_filter),
315 g_mime_data_wrapper_write_to_stream (wrapper, null_stream_filter);
316 charset = g_mime_filter_windows_real_charset (
317 (GMimeFilterWindows *) windows_filter);
319 if (null_stream_filter)
320 g_object_unref (null_stream_filter);
322 g_object_unref (null_stream);
323 /* Keep a reference to windows_filter in order to prevent the
324 * charset string from deallocation. */
327 stream_filter = g_mime_stream_filter_new (stream_out);
328 crlf_filter = g_mime_filter_dos2unix_new (false);
329 g_mime_stream_filter_add (GMIME_STREAM_FILTER (stream_filter),
331 g_object_unref (crlf_filter);
334 GMimeFilter *charset_filter;
335 charset_filter = g_mime_filter_charset_new (charset, "UTF-8");
336 /* This result can be NULL for things like "unknown-8bit".
337 * Don't set a NULL filter as that makes GMime print
338 * annoying assertion-failure messages on stderr. */
339 if (charset_filter) {
340 g_mime_stream_filter_add (GMIME_STREAM_FILTER (stream_filter),
342 g_object_unref (charset_filter);
347 if (flags & NOTMUCH_SHOW_TEXT_PART_REPLY) {
348 GMimeFilter *reply_filter;
349 reply_filter = g_mime_filter_reply_new (true);
351 g_mime_stream_filter_add (GMIME_STREAM_FILTER (stream_filter),
353 g_object_unref (reply_filter);
357 if (wrapper && stream_filter)
358 g_mime_data_wrapper_write_to_stream (wrapper, stream_filter);
360 g_object_unref (stream_filter);
362 g_object_unref (windows_filter);
366 signature_status_to_string (GMimeSignatureStatus status)
368 if (g_mime_signature_status_bad (status))
371 if (g_mime_signature_status_error (status))
374 if (g_mime_signature_status_good (status))
380 /* Print signature flags */
381 struct key_map_struct {
382 GMimeSignatureStatus bit;
387 do_format_signature_errors (sprinter_t *sp, struct key_map_struct *key_map,
388 unsigned int array_map_len, GMimeSignatureStatus errors)
390 sp->map_key (sp, "errors");
393 for (unsigned int i = 0; i < array_map_len; i++) {
394 if (errors & key_map[i].bit) {
395 sp->map_key (sp, key_map[i].string);
396 sp->boolean (sp, true);
404 format_signature_errors (sprinter_t *sp, GMimeSignature *signature)
406 GMimeSignatureStatus errors = g_mime_signature_get_status (signature);
408 if (! (errors & GMIME_SIGNATURE_STATUS_ERROR_MASK))
411 struct key_map_struct key_map[] = {
412 { GMIME_SIGNATURE_STATUS_KEY_REVOKED, "key-revoked" },
413 { GMIME_SIGNATURE_STATUS_KEY_EXPIRED, "key-expired" },
414 { GMIME_SIGNATURE_STATUS_SIG_EXPIRED, "sig-expired" },
415 { GMIME_SIGNATURE_STATUS_KEY_MISSING, "key-missing" },
416 { GMIME_SIGNATURE_STATUS_CRL_MISSING, "crl-missing" },
417 { GMIME_SIGNATURE_STATUS_CRL_TOO_OLD, "crl-too-old" },
418 { GMIME_SIGNATURE_STATUS_BAD_POLICY, "bad-policy" },
419 { GMIME_SIGNATURE_STATUS_SYS_ERROR, "sys-error" },
420 { GMIME_SIGNATURE_STATUS_TOFU_CONFLICT, "tofu-conflict" },
423 do_format_signature_errors (sp, key_map, ARRAY_SIZE (key_map), errors);
426 /* Signature status sprinter */
428 format_part_sigstatus_sprinter (sprinter_t *sp, GMimeSignatureList *siglist)
430 /* Any changes to the JSON or S-Expression format should be
431 * reflected in the file devel/schemata. */
441 for (i = 0; i < g_mime_signature_list_length (siglist); i++) {
442 GMimeSignature *signature = g_mime_signature_list_get_signature (siglist, i);
447 GMimeSignatureStatus status = g_mime_signature_get_status (signature);
448 sp->map_key (sp, "status");
449 sp->string (sp, signature_status_to_string (status));
451 GMimeCertificate *certificate = g_mime_signature_get_certificate (signature);
452 if (g_mime_signature_status_good (status)) {
454 sp->map_key (sp, "fingerprint");
455 sp->string (sp, g_mime_certificate_get_fingerprint (certificate));
457 /* these dates are seconds since the epoch; should we
458 * provide a more human-readable format string? */
459 time_t created = g_mime_signature_get_created (signature);
461 sp->map_key (sp, "created");
462 sp->integer (sp, created);
464 time_t expires = g_mime_signature_get_expires (signature);
466 sp->map_key (sp, "expires");
467 sp->integer (sp, expires);
470 const char *uid = g_mime_certificate_get_valid_userid (certificate);
472 sp->map_key (sp, "userid");
473 sp->string (sp, uid);
476 } else if (certificate) {
477 const char *key_id = g_mime_certificate_get_fpr16 (certificate);
479 sp->map_key (sp, "keyid");
480 sp->string (sp, key_id);
484 if (notmuch_format_version <= 3) {
485 GMimeSignatureStatus errors = g_mime_signature_get_status (signature);
486 if (g_mime_signature_status_error (errors)) {
487 sp->map_key (sp, "errors");
488 sp->integer (sp, errors);
491 format_signature_errors (sp, signature);
500 static notmuch_status_t
501 format_part_text (const void *ctx, sprinter_t *sp, mime_node_t *node,
502 int indent, const notmuch_show_params_t *params)
504 /* The disposition and content-type metadata are associated with
505 * the envelope for message parts */
506 GMimeObject *meta = node->envelope_part ? (
507 GMIME_OBJECT (node->envelope_part) ) : node->part;
508 GMimeContentType *content_type = g_mime_object_get_content_type (meta);
509 const bool leaf = GMIME_IS_PART (node->part);
510 GMimeStream *stream = params->out_stream;
511 const char *part_type;
514 if (node->envelope_file) {
515 notmuch_message_t *message = node->envelope_file;
517 part_type = "message";
518 g_mime_stream_printf (stream, "\f%s{ id:%s depth:%d match:%d excluded:%d filename:%s\n",
520 notmuch_message_get_message_id (message),
522 _get_message_flag (message, NOTMUCH_MESSAGE_FLAG_MATCH) ? 1 : 0,
523 _get_message_flag (message, NOTMUCH_MESSAGE_FLAG_EXCLUDED) ? 1 : 0,
524 notmuch_message_get_filename (message));
526 char *content_string;
527 const char *disposition = _get_disposition (meta);
528 const char *cid = g_mime_object_get_content_id (meta);
529 const char *filename = leaf ? (
530 g_mime_part_get_filename (GMIME_PART (node->part)) ) : NULL;
533 strcasecmp (disposition, GMIME_DISPOSITION_ATTACHMENT) == 0)
534 part_type = "attachment";
538 g_mime_stream_printf (stream, "\f%s{ ID: %d", part_type, node->part_num);
540 g_mime_stream_printf (stream, ", Filename: %s", filename);
542 g_mime_stream_printf (stream, ", Content-id: %s", cid);
544 content_string = g_mime_content_type_get_mime_type (content_type);
545 g_mime_stream_printf (stream, ", Content-type: %s\n", content_string);
546 g_free (content_string);
549 if (GMIME_IS_MESSAGE (node->part)) {
550 GMimeMessage *message = GMIME_MESSAGE (node->part);
551 char *recipients_string;
554 g_mime_stream_printf (stream, "\fheader{\n");
555 if (node->envelope_file)
556 g_mime_stream_printf (stream, "%s\n", _get_one_line_summary (ctx, node->envelope_file));
557 g_mime_stream_printf (stream, "Subject: %s\n", g_mime_message_get_subject (message));
558 g_mime_stream_printf (stream, "From: %s\n", g_mime_message_get_from_string (message));
559 recipients_string = g_mime_message_get_address_string (message, GMIME_ADDRESS_TYPE_TO);
560 if (recipients_string)
561 g_mime_stream_printf (stream, "To: %s\n", recipients_string);
562 g_free (recipients_string);
563 recipients_string = g_mime_message_get_address_string (message, GMIME_ADDRESS_TYPE_CC);
564 if (recipients_string)
565 g_mime_stream_printf (stream, "Cc: %s\n", recipients_string);
566 g_free (recipients_string);
567 date_string = g_mime_message_get_date_string (node, message);
568 g_mime_stream_printf (stream, "Date: %s\n", date_string);
569 g_mime_stream_printf (stream, "\fheader}\n");
571 if (! params->output_body) {
572 g_mime_stream_printf (stream, "\f%s}\n", part_type);
573 return NOTMUCH_STATUS_SUCCESS;
575 g_mime_stream_printf (stream, "\fbody{\n");
579 if (g_mime_content_type_is_type (content_type, "text", "*") &&
580 (params->include_html ||
581 ! g_mime_content_type_is_type (content_type, "text", "html"))) {
582 show_text_part_content (node->part, stream, 0);
584 char *content_string = g_mime_content_type_get_mime_type (content_type);
585 g_mime_stream_printf (stream, "Non-text part: %s\n", content_string);
586 g_free (content_string);
590 for (i = 0; i < node->nchildren; i++)
591 format_part_text (ctx, sp, mime_node_child (node, i), indent, params);
593 if (GMIME_IS_MESSAGE (node->part))
594 g_mime_stream_printf (stream, "\fbody}\n");
596 g_mime_stream_printf (stream, "\f%s}\n", part_type);
598 return NOTMUCH_STATUS_SUCCESS;
602 format_omitted_part_meta_sprinter (sprinter_t *sp, GMimeObject *meta, GMimePart *part)
604 const char *content_charset = g_mime_object_get_content_type_parameter (meta, "charset");
605 const char *cte = g_mime_object_get_header (meta, "content-transfer-encoding");
606 GMimeDataWrapper *wrapper = g_mime_part_get_content (part);
607 GMimeStream *stream = g_mime_data_wrapper_get_stream (wrapper);
608 ssize_t content_length = g_mime_stream_length (stream);
610 if (content_charset != NULL) {
611 sp->map_key (sp, "content-charset");
612 sp->string (sp, content_charset);
615 sp->map_key (sp, "content-transfer-encoding");
616 sp->string (sp, cte);
618 if (content_length >= 0) {
619 sp->map_key (sp, "content-length");
620 sp->integer (sp, content_length);
625 format_part_sprinter (const void *ctx, sprinter_t *sp, mime_node_t *node,
629 /* Any changes to the JSON or S-Expression format should be
630 * reflected in the file devel/schemata. */
632 if (node->envelope_file) {
633 const _notmuch_message_crypto_t *msg_crypto = NULL;
635 format_message_sprinter (sp, node->envelope_file);
638 sp->map_key (sp, "body");
640 format_part_sprinter (ctx, sp, mime_node_child (node, 0), true, include_html);
644 msg_crypto = mime_node_get_message_crypto_status (node);
645 if (notmuch_format_version >= 4) {
646 sp->map_key (sp, "crypto");
648 if (msg_crypto->sig_list ||
649 msg_crypto->decryption_status != NOTMUCH_MESSAGE_DECRYPTED_NONE) {
650 if (msg_crypto->sig_list) {
651 sp->map_key (sp, "signed");
653 sp->map_key (sp, "status");
654 format_part_sigstatus_sprinter (sp, msg_crypto->sig_list);
655 if (msg_crypto->signature_encrypted) {
656 sp->map_key (sp, "encrypted");
657 sp->boolean (sp, msg_crypto->signature_encrypted);
659 if (msg_crypto->payload_subject) {
660 sp->map_key (sp, "headers");
662 sp->string (sp, "Subject");
667 if (msg_crypto->decryption_status != NOTMUCH_MESSAGE_DECRYPTED_NONE) {
668 sp->map_key (sp, "decrypted");
670 sp->map_key (sp, "status");
671 sp->string (sp, msg_crypto->decryption_status == NOTMUCH_MESSAGE_DECRYPTED_FULL ? "full" : "partial");
673 if (msg_crypto->payload_subject) {
674 const char *subject = g_mime_message_get_subject GMIME_MESSAGE (node->part);
675 if (subject == NULL || strcmp (subject, msg_crypto->payload_subject)) {
676 /* protected subject differs from the external header */
677 sp->map_key (sp, "header-mask");
679 sp->map_key (sp, "Subject");
683 sp->string (sp, subject);
693 sp->map_key (sp, "headers");
694 format_headers_sprinter (sp, GMIME_MESSAGE (node->part), false, msg_crypto);
700 /* The disposition and content-type metadata are associated with
701 * the envelope for message parts */
702 GMimeObject *meta = node->envelope_part ? (
703 GMIME_OBJECT (node->envelope_part) ) : node->part;
704 GMimeContentType *content_type = g_mime_object_get_content_type (meta);
705 char *content_string;
706 const char *disposition = _get_disposition (meta);
707 const char *cid = g_mime_object_get_content_id (meta);
708 const char *filename = GMIME_IS_PART (node->part) ? (
709 g_mime_part_get_filename (GMIME_PART (node->part) ) ) : NULL;
715 sp->map_key (sp, "id");
716 sp->integer (sp, node->part_num);
718 if (node->decrypt_attempted) {
719 sp->map_key (sp, "encstatus");
722 sp->map_key (sp, "status");
723 sp->string (sp, node->decrypt_success ? "good" : "bad");
728 if (node->verify_attempted) {
729 sp->map_key (sp, "sigstatus");
730 format_part_sigstatus_sprinter (sp, node->sig_list);
733 sp->map_key (sp, "content-type");
734 content_string = g_mime_content_type_get_mime_type (content_type);
735 sp->string (sp, content_string);
736 g_free (content_string);
739 sp->map_key (sp, "content-disposition");
740 sp->string (sp, disposition);
744 sp->map_key (sp, "content-id");
745 sp->string (sp, cid);
749 sp->map_key (sp, "filename");
750 sp->string (sp, filename);
753 if (GMIME_IS_PART (node->part)) {
754 /* For non-HTML text parts, we include the content in the
755 * JSON. Since JSON must be Unicode, we handle charset
756 * decoding here and do not report a charset to the caller.
757 * For text/html parts, we do not include the content unless
758 * the --include-html option has been passed. If a html part
759 * is not included, it can be requested directly. This makes
760 * charset decoding the responsibility on the caller so we
761 * report the charset for text/html parts.
763 if (g_mime_content_type_is_type (content_type, "text", "*") &&
765 ! g_mime_content_type_is_type (content_type, "text", "html"))) {
766 GMimeStream *stream_memory = g_mime_stream_mem_new ();
767 GByteArray *part_content;
768 show_text_part_content (node->part, stream_memory, 0);
769 part_content = g_mime_stream_mem_get_byte_array (GMIME_STREAM_MEM (stream_memory));
770 sp->map_key (sp, "content");
771 sp->string_len (sp, (char *) part_content->data, part_content->len);
772 g_object_unref (stream_memory);
774 /* if we have a child part despite being a standard
775 * (non-multipart) MIME part, that means there is
776 * something to unwrap, which we will present in
778 if (node->nchildren) {
779 sp->map_key (sp, "content");
783 format_omitted_part_meta_sprinter (sp, meta, GMIME_PART (node->part));
785 } else if (GMIME_IS_MULTIPART (node->part)) {
786 sp->map_key (sp, "content");
789 } else if (GMIME_IS_MESSAGE (node->part)) {
790 sp->map_key (sp, "content");
794 sp->map_key (sp, "headers");
795 format_headers_sprinter (sp, GMIME_MESSAGE (node->part), false, NULL);
797 sp->map_key (sp, "body");
802 for (i = 0; i < node->nchildren; i++)
803 format_part_sprinter (ctx, sp, mime_node_child (node, i), true, include_html);
805 /* Close content structures */
806 for (i = 0; i < nclose; i++)
812 static notmuch_status_t
813 format_part_sprinter_entry (const void *ctx, sprinter_t *sp,
814 mime_node_t *node, unused (int indent),
815 const notmuch_show_params_t *params)
817 format_part_sprinter (ctx, sp, node, params->output_body, params->include_html);
819 return NOTMUCH_STATUS_SUCCESS;
822 /* Print a message in "mboxrd" format as documented, for example,
825 * http://qmail.org/qmail-manual-html/man5/mbox.html
827 static notmuch_status_t
828 format_part_mbox (const void *ctx, unused (sprinter_t *sp), mime_node_t *node,
830 unused (const notmuch_show_params_t *params))
832 notmuch_message_t *message = node->envelope_file;
834 const char *filename;
839 struct tm date_gmtime;
840 char date_asctime[26];
847 INTERNAL_ERROR ("format_part_mbox requires a root part");
849 filename = notmuch_message_get_filename (message);
850 file = gzopen (filename, "r");
852 fprintf (stderr, "Failed to open %s: %s\n",
853 filename, strerror (errno));
854 return NOTMUCH_STATUS_FILE_ERROR;
857 from = notmuch_message_get_header (message, "from");
858 from = _extract_email_address (ctx, from);
860 date = notmuch_message_get_date (message);
861 gmtime_r (&date, &date_gmtime);
862 asctime_r (&date_gmtime, date_asctime);
864 printf ("From %s %s", from, date_asctime);
866 while ((line_len = gz_getline (message, &line, &line_size, file)) != UTIL_EOF ) {
867 if (_is_from_line (line))
876 return NOTMUCH_STATUS_SUCCESS;
879 static notmuch_status_t
880 format_part_raw (unused (const void *ctx), unused (sprinter_t *sp),
881 mime_node_t *node, unused (int indent),
882 const notmuch_show_params_t *params)
884 if (node->envelope_file) {
885 /* Special case the entire message to avoid MIME parsing. */
886 const char *filename;
887 GMimeStream *stream = NULL;
890 notmuch_status_t ret = NOTMUCH_STATUS_FILE_ERROR;
892 filename = notmuch_message_get_filename (node->envelope_file);
893 if (filename == NULL) {
894 fprintf (stderr, "Error: Cannot get message filename.\n");
898 stream = g_mime_stream_gzfile_open (filename);
899 if (stream == NULL) {
900 fprintf (stderr, "Error: Cannot open file %s: %s\n", filename, strerror (errno));
904 while (! g_mime_stream_eos (stream)) {
905 ssize = g_mime_stream_read (stream, buf, sizeof (buf));
907 fprintf (stderr, "Error: Read failed from %s\n", filename);
911 if (ssize > 0 && fwrite (buf, ssize, 1, stdout) != 1) {
912 fprintf (stderr, "Error: Write %ld chars to stdout failed\n", ssize);
917 ret = NOTMUCH_STATUS_SUCCESS;
919 /* XXX This DONE is just for the special case of a node in a single file */
922 g_object_unref (stream);
927 GMimeStream *stream_filter = g_mime_stream_filter_new (params->out_stream);
929 if (GMIME_IS_PART (node->part)) {
930 /* For leaf parts, we emit only the transfer-decoded
932 GMimeDataWrapper *wrapper;
933 wrapper = g_mime_part_get_content (GMIME_PART (node->part));
935 if (wrapper && stream_filter)
936 g_mime_data_wrapper_write_to_stream (wrapper, stream_filter);
938 /* Write out the whole part. For message parts (the root
939 * part and embedded message parts), this will be the
940 * message including its headers (but not the
941 * encapsulating part's headers). For multipart parts,
942 * this will include the headers. */
944 g_mime_object_write_to_stream (node->part, NULL, stream_filter);
948 g_object_unref (stream_filter);
950 return NOTMUCH_STATUS_SUCCESS;
953 static notmuch_status_t
954 show_message (void *ctx,
955 const notmuch_show_format_t *format,
957 notmuch_message_t *message,
959 notmuch_show_params_t *params)
961 void *local = talloc_new (ctx);
962 mime_node_t *root, *part;
963 notmuch_status_t status;
964 unsigned int session_keys = 0;
965 notmuch_status_t session_key_count_error = NOTMUCH_STATUS_SUCCESS;
967 if (params->crypto.decrypt == NOTMUCH_DECRYPT_TRUE)
968 session_key_count_error = notmuch_message_count_properties (message, "session-key", &session_keys);
970 status = mime_node_open (local, message, &(params->crypto), &root);
973 part = mime_node_seek_dfs (root, (params->part < 0 ? 0 : params->part));
975 status = format->part (local, sp, part, indent, params);
976 if (params->crypto.decrypt == NOTMUCH_DECRYPT_TRUE && session_key_count_error == NOTMUCH_STATUS_SUCCESS) {
977 unsigned int new_session_keys = 0;
978 if (notmuch_message_count_properties (message, "session-key", &new_session_keys) == NOTMUCH_STATUS_SUCCESS &&
979 new_session_keys > session_keys) {
980 /* try a quiet re-indexing */
981 notmuch_indexopts_t *indexopts = notmuch_database_get_default_indexopts (notmuch_message_get_database (message));
983 notmuch_indexopts_set_decrypt_policy (indexopts, NOTMUCH_DECRYPT_AUTO);
984 print_status_message ("Error re-indexing message with --decrypt=stash",
985 message, notmuch_message_reindex (message, indexopts));
994 static notmuch_status_t
995 show_messages (void *ctx,
996 const notmuch_show_format_t *format,
998 notmuch_messages_t *messages,
1000 notmuch_show_params_t *params)
1002 notmuch_message_t *message;
1006 notmuch_status_t status, res = NOTMUCH_STATUS_SUCCESS;
1008 sp->begin_list (sp);
1011 notmuch_messages_valid (messages);
1012 notmuch_messages_move_to_next (messages)) {
1013 sp->begin_list (sp);
1015 message = notmuch_messages_get (messages);
1017 match = _get_message_flag (message, NOTMUCH_MESSAGE_FLAG_MATCH);
1018 excluded = _get_message_flag (message, NOTMUCH_MESSAGE_FLAG_EXCLUDED);
1020 next_indent = indent;
1022 if ((match && (! excluded || ! params->omit_excluded)) || params->entire_thread) {
1023 status = show_message (ctx, format, sp, message, indent, params);
1024 if (status && ! res)
1026 next_indent = indent + 1;
1031 status = show_messages (ctx,
1033 notmuch_message_get_replies (message),
1036 if (status && ! res)
1039 notmuch_message_destroy (message);
1049 /* Formatted output of single message */
1051 do_show_single (void *ctx,
1052 notmuch_query_t *query,
1053 const notmuch_show_format_t *format,
1055 notmuch_show_params_t *params)
1057 notmuch_messages_t *messages;
1058 notmuch_message_t *message;
1059 notmuch_status_t status;
1062 status = notmuch_query_count_messages (query, &count);
1063 if (print_status_query ("notmuch show", query, status))
1067 fprintf (stderr, "Error: search term did not match precisely one message (matched %u messages).\n", count);
1071 status = notmuch_query_search_messages (query, &messages);
1072 if (print_status_query ("notmuch show", query, status))
1075 message = notmuch_messages_get (messages);
1077 if (message == NULL) {
1078 fprintf (stderr, "Error: Cannot find matching message.\n");
1082 notmuch_message_set_flag (message, NOTMUCH_MESSAGE_FLAG_MATCH, 1);
1084 return show_message (ctx, format, sp, message, 0, params)
1085 != NOTMUCH_STATUS_SUCCESS;
1088 /* Formatted output of threads */
1090 do_show_threaded (void *ctx,
1091 notmuch_query_t *query,
1092 const notmuch_show_format_t *format,
1094 notmuch_show_params_t *params)
1096 notmuch_threads_t *threads;
1097 notmuch_thread_t *thread;
1098 notmuch_messages_t *messages;
1099 notmuch_status_t status, res = NOTMUCH_STATUS_SUCCESS;
1101 status = notmuch_query_search_threads (query, &threads);
1102 if (print_status_query ("notmuch show", query, status))
1105 sp->begin_list (sp);
1108 notmuch_threads_valid (threads);
1109 notmuch_threads_move_to_next (threads)) {
1110 thread = notmuch_threads_get (threads);
1112 messages = notmuch_thread_get_toplevel_messages (thread);
1114 if (messages == NULL)
1115 INTERNAL_ERROR ("Thread %s has no toplevel messages.\n",
1116 notmuch_thread_get_thread_id (thread));
1118 status = show_messages (ctx, format, sp, messages, 0, params);
1119 if (status && ! res)
1122 notmuch_thread_destroy (thread);
1128 return res != NOTMUCH_STATUS_SUCCESS;
1132 do_show_unthreaded (void *ctx,
1133 notmuch_query_t *query,
1134 const notmuch_show_format_t *format,
1136 notmuch_show_params_t *params)
1138 notmuch_messages_t *messages;
1139 notmuch_message_t *message;
1140 notmuch_status_t status, res = NOTMUCH_STATUS_SUCCESS;
1141 notmuch_bool_t excluded;
1143 status = notmuch_query_search_messages (query, &messages);
1144 if (print_status_query ("notmuch show", query, status))
1147 sp->begin_list (sp);
1150 notmuch_messages_valid (messages);
1151 notmuch_messages_move_to_next (messages)) {
1152 sp->begin_list (sp);
1153 sp->begin_list (sp);
1155 message = notmuch_messages_get (messages);
1157 notmuch_message_set_flag (message, NOTMUCH_MESSAGE_FLAG_MATCH, TRUE);
1158 excluded = _get_message_flag (message, NOTMUCH_MESSAGE_FLAG_EXCLUDED);
1160 if (!excluded || !params->omit_excluded) {
1161 status = show_message (ctx, format, sp, message, 0, params);
1167 notmuch_message_destroy (message);
1176 NOTMUCH_FORMAT_NOT_SPECIFIED,
1177 NOTMUCH_FORMAT_JSON,
1178 NOTMUCH_FORMAT_SEXP,
1179 NOTMUCH_FORMAT_TEXT,
1180 NOTMUCH_FORMAT_MBOX,
1184 static const notmuch_show_format_t format_json = {
1185 .new_sprinter = sprinter_json_create,
1186 .part = format_part_sprinter_entry,
1189 static const notmuch_show_format_t format_sexp = {
1190 .new_sprinter = sprinter_sexp_create,
1191 .part = format_part_sprinter_entry,
1194 static const notmuch_show_format_t format_text = {
1195 .new_sprinter = sprinter_text_create,
1196 .part = format_part_text,
1199 static const notmuch_show_format_t format_mbox = {
1200 .new_sprinter = sprinter_text_create,
1201 .part = format_part_mbox,
1204 static const notmuch_show_format_t format_raw = {
1205 .new_sprinter = sprinter_text_create,
1206 .part = format_part_raw,
1209 static const notmuch_show_format_t *formatters[] = {
1210 [NOTMUCH_FORMAT_JSON] = &format_json,
1211 [NOTMUCH_FORMAT_SEXP] = &format_sexp,
1212 [NOTMUCH_FORMAT_TEXT] = &format_text,
1213 [NOTMUCH_FORMAT_MBOX] = &format_mbox,
1214 [NOTMUCH_FORMAT_RAW] = &format_raw,
1218 notmuch_show_command (notmuch_config_t *config, int argc, char *argv[])
1220 notmuch_database_t *notmuch;
1221 notmuch_query_t *query;
1224 const notmuch_show_format_t *formatter;
1225 sprinter_t *sprinter;
1226 notmuch_show_params_t params = {
1228 .omit_excluded = true,
1229 .output_body = true,
1230 .crypto = { .decrypt = NOTMUCH_DECRYPT_AUTO },
1232 int format = NOTMUCH_FORMAT_NOT_SPECIFIED;
1233 bool exclude = true;
1234 bool entire_thread_set = false;
1235 bool single_message;
1236 bool unthreaded = FALSE;
1238 notmuch_opt_desc_t options[] = {
1239 { .opt_keyword = &format, .name = "format", .keywords =
1240 (notmuch_keyword_t []){ { "json", NOTMUCH_FORMAT_JSON },
1241 { "text", NOTMUCH_FORMAT_TEXT },
1242 { "sexp", NOTMUCH_FORMAT_SEXP },
1243 { "mbox", NOTMUCH_FORMAT_MBOX },
1244 { "raw", NOTMUCH_FORMAT_RAW },
1246 { .opt_int = ¬much_format_version, .name = "format-version" },
1247 { .opt_bool = &exclude, .name = "exclude" },
1248 { .opt_bool = ¶ms.entire_thread, .name = "entire-thread",
1249 .present = &entire_thread_set },
1250 { .opt_bool = &unthreaded, .name = "unthreaded" },
1251 { .opt_int = ¶ms.part, .name = "part" },
1252 { .opt_keyword = (int *) (¶ms.crypto.decrypt), .name = "decrypt",
1253 .keyword_no_arg_value = "true", .keywords =
1254 (notmuch_keyword_t []){ { "false", NOTMUCH_DECRYPT_FALSE },
1255 { "auto", NOTMUCH_DECRYPT_AUTO },
1256 { "true", NOTMUCH_DECRYPT_NOSTASH },
1257 { "stash", NOTMUCH_DECRYPT_TRUE },
1259 { .opt_bool = ¶ms.crypto.verify, .name = "verify" },
1260 { .opt_bool = ¶ms.output_body, .name = "body" },
1261 { .opt_bool = ¶ms.include_html, .name = "include-html" },
1262 { .opt_inherit = notmuch_shared_options },
1266 opt_index = parse_arguments (argc, argv, options, 1);
1268 return EXIT_FAILURE;
1270 notmuch_process_shared_options (argv[0]);
1272 /* explicit decryption implies verification */
1273 if (params.crypto.decrypt == NOTMUCH_DECRYPT_NOSTASH ||
1274 params.crypto.decrypt == NOTMUCH_DECRYPT_TRUE)
1275 params.crypto.verify = true;
1277 /* specifying a part implies single message display */
1278 single_message = params.part >= 0;
1280 if (format == NOTMUCH_FORMAT_NOT_SPECIFIED) {
1281 /* if part was requested and format was not specified, use format=raw */
1282 if (params.part >= 0)
1283 format = NOTMUCH_FORMAT_RAW;
1285 format = NOTMUCH_FORMAT_TEXT;
1288 if (format == NOTMUCH_FORMAT_MBOX) {
1289 if (params.part > 0) {
1290 fprintf (stderr, "Error: specifying parts is incompatible with mbox output format.\n");
1291 return EXIT_FAILURE;
1293 } else if (format == NOTMUCH_FORMAT_RAW) {
1294 /* raw format only supports single message display */
1295 single_message = true;
1298 notmuch_exit_if_unsupported_format ();
1300 /* Default is entire-thread = false except for format=json and
1302 if (! entire_thread_set &&
1303 (format == NOTMUCH_FORMAT_JSON || format == NOTMUCH_FORMAT_SEXP))
1304 params.entire_thread = true;
1306 if (! params.output_body) {
1307 if (params.part > 0) {
1308 fprintf (stderr, "Warning: --body=false is incompatible with --part > 0. Disabling.\n");
1309 params.output_body = true;
1311 if (format != NOTMUCH_FORMAT_TEXT &&
1312 format != NOTMUCH_FORMAT_JSON &&
1313 format != NOTMUCH_FORMAT_SEXP)
1315 "Warning: --body=false only implemented for format=text, format=json and format=sexp\n");
1319 if (params.include_html &&
1320 (format != NOTMUCH_FORMAT_TEXT &&
1321 format != NOTMUCH_FORMAT_JSON &&
1322 format != NOTMUCH_FORMAT_SEXP)) {
1323 fprintf (stderr, "Warning: --include-html only implemented for format=text, format=json and format=sexp\n");
1326 query_string = query_string_from_args (config, argc - opt_index, argv + opt_index);
1327 if (query_string == NULL) {
1328 fprintf (stderr, "Out of memory\n");
1329 return EXIT_FAILURE;
1332 if (*query_string == '\0') {
1333 fprintf (stderr, "Error: notmuch show requires at least one search term.\n");
1334 return EXIT_FAILURE;
1337 notmuch_database_mode_t mode = NOTMUCH_DATABASE_MODE_READ_ONLY;
1338 if (params.crypto.decrypt == NOTMUCH_DECRYPT_TRUE)
1339 mode = NOTMUCH_DATABASE_MODE_READ_WRITE;
1340 if (notmuch_database_open (notmuch_config_get_database_path (config),
1342 return EXIT_FAILURE;
1344 notmuch_exit_if_unmatched_db_uuid (notmuch);
1346 query = notmuch_query_create (notmuch, query_string);
1347 if (query == NULL) {
1348 fprintf (stderr, "Out of memory\n");
1349 return EXIT_FAILURE;
1352 /* Create structure printer. */
1353 formatter = formatters[format];
1354 sprinter = formatter->new_sprinter (config, stdout);
1356 params.out_stream = g_mime_stream_stdout_new ();
1358 /* If a single message is requested we do not use search_excludes. */
1359 if (single_message) {
1360 ret = do_show_single (config, query, formatter, sprinter, ¶ms);
1362 /* We always apply set the exclude flag. The
1363 * exclude=true|false option controls whether or not we return
1364 * threads that only match in an excluded message */
1365 const char **search_exclude_tags;
1366 size_t search_exclude_tags_length;
1368 notmuch_status_t status;
1370 search_exclude_tags = notmuch_config_get_search_exclude_tags
1371 (config, &search_exclude_tags_length);
1373 for (i = 0; i < search_exclude_tags_length; i++) {
1374 status = notmuch_query_add_tag_exclude (query, search_exclude_tags[i]);
1375 if (status && status != NOTMUCH_STATUS_IGNORED) {
1376 print_status_query ("notmuch show", query, status);
1382 if (exclude == false) {
1383 notmuch_query_set_omit_excluded (query, false);
1384 params.omit_excluded = false;
1388 ret = do_show_unthreaded (config, query, formatter, sprinter, ¶ms);
1390 ret = do_show_threaded (config, query, formatter, sprinter, ¶ms);
1394 g_mime_stream_flush (params.out_stream);
1395 g_object_unref (params.out_stream);
1397 _notmuch_crypto_cleanup (¶ms.crypto);
1398 notmuch_query_destroy (query);
1399 notmuch_database_destroy (notmuch);
1401 return ret ? EXIT_FAILURE : EXIT_SUCCESS;