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);
84 _get_message_flag (notmuch_message_t *message, notmuch_message_flag_t flag)
86 notmuch_bool_t is_set;
87 notmuch_status_t status;
89 status = notmuch_message_get_flag_st (message, flag, &is_set);
91 if (print_status_message ("notmuch show", message, status))
92 INTERNAL_ERROR ("unexpected error getting message flag\n");
97 /* Emit a sequence of key/value pairs for the metadata of message.
98 * The caller should begin a map before calling this. */
100 format_message_sprinter (sprinter_t *sp, notmuch_message_t *message)
102 /* Any changes to the JSON or S-Expression format should be
103 * reflected in the file devel/schemata. */
105 void *local = talloc_new (NULL);
106 notmuch_tags_t *tags;
108 const char *relative_date;
110 sp->map_key (sp, "id");
111 sp->string (sp, notmuch_message_get_message_id (message));
113 sp->map_key (sp, "match");
114 sp->boolean (sp, _get_message_flag (message, NOTMUCH_MESSAGE_FLAG_MATCH));
116 sp->map_key (sp, "excluded");
117 sp->boolean (sp, _get_message_flag (message, NOTMUCH_MESSAGE_FLAG_EXCLUDED));
119 sp->map_key (sp, "filename");
120 if (notmuch_format_version >= 3) {
121 notmuch_filenames_t *filenames;
124 for (filenames = notmuch_message_get_filenames (message);
125 notmuch_filenames_valid (filenames);
126 notmuch_filenames_move_to_next (filenames)) {
127 sp->string (sp, notmuch_filenames_get (filenames));
129 notmuch_filenames_destroy (filenames);
132 sp->string (sp, notmuch_message_get_filename (message));
135 sp->map_key (sp, "timestamp");
136 date = notmuch_message_get_date (message);
137 sp->integer (sp, date);
139 sp->map_key (sp, "date_relative");
140 relative_date = notmuch_time_relative_date (local, date);
141 sp->string (sp, relative_date);
143 sp->map_key (sp, "tags");
145 for (tags = notmuch_message_get_tags (message);
146 notmuch_tags_valid (tags);
147 notmuch_tags_move_to_next (tags))
148 sp->string (sp, notmuch_tags_get (tags));
154 /* Extract just the email address from the contents of a From:
157 _extract_email_address (const void *ctx, const char *from)
159 InternetAddressList *addresses;
160 InternetAddress *address;
161 InternetAddressMailbox *mailbox;
162 const char *email = "MAILER-DAEMON";
164 addresses = internet_address_list_parse (NULL, from);
166 /* Bail if there is no address here. */
167 if (addresses == NULL || internet_address_list_length (addresses) < 1)
170 /* Otherwise, just use the first address. */
171 address = internet_address_list_get_address (addresses, 0);
173 /* The From header should never contain an address group rather
174 * than a mailbox. So bail if it does. */
175 if (! INTERNET_ADDRESS_IS_MAILBOX (address))
178 mailbox = INTERNET_ADDRESS_MAILBOX (address);
179 email = internet_address_mailbox_get_addr (mailbox);
180 email = talloc_strdup (ctx, email);
184 g_object_unref (addresses);
189 /* Return 1 if 'line' is an mbox From_ line---that is, a line
190 * beginning with zero or more '>' characters followed by the
191 * characters 'F', 'r', 'o', 'm', and space.
193 * Any characters at all may appear after that in the line.
196 _is_from_line (const char *line)
198 const char *s = line;
206 if (STRNCMP_LITERAL (s, "From ") == 0)
213 format_headers_sprinter (sprinter_t *sp, GMimeMessage *message,
214 bool reply, const _notmuch_message_crypto_t *msg_crypto)
216 /* Any changes to the JSON or S-Expression format should be
217 * reflected in the file devel/schemata. */
219 char *recipients_string;
220 const char *reply_to_string;
221 void *local = talloc_new (sp);
225 sp->map_key (sp, "Subject");
226 if (msg_crypto && msg_crypto->payload_subject) {
227 sp->string (sp, msg_crypto->payload_subject);
229 sp->string (sp, g_mime_message_get_subject (message));
231 sp->map_key (sp, "From");
232 sp->string (sp, g_mime_message_get_from_string (message));
234 recipients_string = g_mime_message_get_address_string (message, GMIME_ADDRESS_TYPE_TO);
235 if (recipients_string) {
236 sp->map_key (sp, "To");
237 sp->string (sp, recipients_string);
238 g_free (recipients_string);
241 recipients_string = g_mime_message_get_address_string (message, GMIME_ADDRESS_TYPE_CC);
242 if (recipients_string) {
243 sp->map_key (sp, "Cc");
244 sp->string (sp, recipients_string);
245 g_free (recipients_string);
248 recipients_string = g_mime_message_get_address_string (message, GMIME_ADDRESS_TYPE_BCC);
249 if (recipients_string) {
250 sp->map_key (sp, "Bcc");
251 sp->string (sp, recipients_string);
252 g_free (recipients_string);
255 reply_to_string = g_mime_message_get_reply_to_string (local, message);
256 if (reply_to_string) {
257 sp->map_key (sp, "Reply-To");
258 sp->string (sp, reply_to_string);
262 sp->map_key (sp, "In-reply-to");
263 sp->string (sp, g_mime_object_get_header (GMIME_OBJECT (message), "In-reply-to"));
265 sp->map_key (sp, "References");
266 sp->string (sp, g_mime_object_get_header (GMIME_OBJECT (message), "References"));
268 sp->map_key (sp, "Date");
269 sp->string (sp, g_mime_message_get_date_string (sp, message));
276 /* Write a MIME text part out to the given stream.
278 * If (flags & NOTMUCH_SHOW_TEXT_PART_REPLY), this prepends "> " to
281 * Both line-ending conversion (CRLF->LF) and charset conversion ( ->
282 * UTF-8) will be performed, so it is inappropriate to call this
283 * function with a non-text part. Doing so will trigger an internal
287 show_text_part_content (GMimeObject *part, GMimeStream *stream_out,
288 notmuch_show_text_part_flags flags)
290 GMimeContentType *content_type = g_mime_object_get_content_type (GMIME_OBJECT (part));
291 GMimeStream *stream_filter = NULL;
292 GMimeFilter *crlf_filter = NULL;
293 GMimeFilter *windows_filter = NULL;
294 GMimeDataWrapper *wrapper;
297 if (! g_mime_content_type_is_type (content_type, "text", "*"))
298 INTERNAL_ERROR ("Illegal request to format non-text part (%s) as text.",
299 g_mime_content_type_get_mime_type (content_type));
301 if (stream_out == NULL)
304 charset = g_mime_object_get_content_type_parameter (part, "charset");
305 charset = charset ? g_mime_charset_canon_name (charset) : NULL;
306 wrapper = g_mime_part_get_content (GMIME_PART (part));
307 if (wrapper && charset && ! g_ascii_strncasecmp (charset, "iso-8859-", 9)) {
308 GMimeStream *null_stream = NULL;
309 GMimeStream *null_stream_filter = NULL;
311 /* Check for mislabeled Windows encoding */
312 null_stream = g_mime_stream_null_new ();
313 null_stream_filter = g_mime_stream_filter_new (null_stream);
314 windows_filter = g_mime_filter_windows_new (charset);
315 g_mime_stream_filter_add (GMIME_STREAM_FILTER (null_stream_filter),
317 g_mime_data_wrapper_write_to_stream (wrapper, null_stream_filter);
318 charset = g_mime_filter_windows_real_charset (
319 (GMimeFilterWindows *) windows_filter);
321 if (null_stream_filter)
322 g_object_unref (null_stream_filter);
324 g_object_unref (null_stream);
325 /* Keep a reference to windows_filter in order to prevent the
326 * charset string from deallocation. */
329 stream_filter = g_mime_stream_filter_new (stream_out);
330 crlf_filter = g_mime_filter_dos2unix_new (false);
331 g_mime_stream_filter_add (GMIME_STREAM_FILTER (stream_filter),
333 g_object_unref (crlf_filter);
336 GMimeFilter *charset_filter;
337 charset_filter = g_mime_filter_charset_new (charset, "UTF-8");
338 /* This result can be NULL for things like "unknown-8bit".
339 * Don't set a NULL filter as that makes GMime print
340 * annoying assertion-failure messages on stderr. */
341 if (charset_filter) {
342 g_mime_stream_filter_add (GMIME_STREAM_FILTER (stream_filter),
344 g_object_unref (charset_filter);
349 if (flags & NOTMUCH_SHOW_TEXT_PART_REPLY) {
350 GMimeFilter *reply_filter;
351 reply_filter = g_mime_filter_reply_new (true);
353 g_mime_stream_filter_add (GMIME_STREAM_FILTER (stream_filter),
355 g_object_unref (reply_filter);
359 if (wrapper && stream_filter)
360 g_mime_data_wrapper_write_to_stream (wrapper, stream_filter);
362 g_object_unref (stream_filter);
364 g_object_unref (windows_filter);
368 signature_status_to_string (GMimeSignatureStatus status)
370 if (g_mime_signature_status_bad (status))
373 if (g_mime_signature_status_error (status))
376 if (g_mime_signature_status_good (status))
382 /* Print signature flags */
383 struct key_map_struct {
384 GMimeSignatureStatus bit;
389 do_format_signature_errors (sprinter_t *sp, struct key_map_struct *key_map,
390 unsigned int array_map_len, GMimeSignatureStatus errors)
392 sp->map_key (sp, "errors");
395 for (unsigned int i = 0; i < array_map_len; i++) {
396 if (errors & key_map[i].bit) {
397 sp->map_key (sp, key_map[i].string);
398 sp->boolean (sp, true);
406 format_signature_errors (sprinter_t *sp, GMimeSignature *signature)
408 GMimeSignatureStatus errors = g_mime_signature_get_status (signature);
410 if (! (errors & GMIME_SIGNATURE_STATUS_ERROR_MASK))
413 struct key_map_struct key_map[] = {
414 { GMIME_SIGNATURE_STATUS_KEY_REVOKED, "key-revoked" },
415 { GMIME_SIGNATURE_STATUS_KEY_EXPIRED, "key-expired" },
416 { GMIME_SIGNATURE_STATUS_SIG_EXPIRED, "sig-expired" },
417 { GMIME_SIGNATURE_STATUS_KEY_MISSING, "key-missing" },
418 { GMIME_SIGNATURE_STATUS_CRL_MISSING, "crl-missing" },
419 { GMIME_SIGNATURE_STATUS_CRL_TOO_OLD, "crl-too-old" },
420 { GMIME_SIGNATURE_STATUS_BAD_POLICY, "bad-policy" },
421 { GMIME_SIGNATURE_STATUS_SYS_ERROR, "sys-error" },
422 { GMIME_SIGNATURE_STATUS_TOFU_CONFLICT, "tofu-conflict" },
425 do_format_signature_errors (sp, key_map, ARRAY_SIZE (key_map), errors);
428 /* Signature status sprinter */
430 format_part_sigstatus_sprinter (sprinter_t *sp, GMimeSignatureList *siglist)
432 /* Any changes to the JSON or S-Expression format should be
433 * reflected in the file devel/schemata. */
444 for (i = 0; i < g_mime_signature_list_length (siglist); i++) {
445 GMimeSignature *signature = g_mime_signature_list_get_signature (siglist, i);
450 GMimeSignatureStatus status = g_mime_signature_get_status (signature);
451 sp->map_key (sp, "status");
452 sp->string (sp, signature_status_to_string (status));
454 GMimeCertificate *certificate = g_mime_signature_get_certificate (signature);
455 if (g_mime_signature_status_good (status)) {
457 sp->map_key (sp, "fingerprint");
458 sp->string (sp, g_mime_certificate_get_fingerprint (certificate));
460 /* these dates are seconds since the epoch; should we
461 * provide a more human-readable format string? */
462 time_t created = g_mime_signature_get_created (signature);
464 sp->map_key (sp, "created");
465 sp->integer (sp, created);
467 time_t expires = g_mime_signature_get_expires (signature);
469 sp->map_key (sp, "expires");
470 sp->integer (sp, expires);
473 const char *uid = g_mime_certificate_get_valid_userid (certificate);
475 sp->map_key (sp, "userid");
476 sp->string (sp, uid);
478 const char *email = g_mime_certificate_get_valid_email (certificate);
480 sp->map_key (sp, "email");
481 sp->string (sp, email);
484 } else if (certificate) {
485 const char *key_id = g_mime_certificate_get_fpr16 (certificate);
487 sp->map_key (sp, "keyid");
488 sp->string (sp, key_id);
492 if (notmuch_format_version <= 3) {
493 GMimeSignatureStatus errors = g_mime_signature_get_status (signature);
494 if (g_mime_signature_status_error (errors)) {
495 sp->map_key (sp, "errors");
496 sp->integer (sp, errors);
499 format_signature_errors (sp, signature);
508 static notmuch_status_t
509 format_part_text (const void *ctx, sprinter_t *sp, mime_node_t *node,
510 int indent, const notmuch_show_params_t *params)
512 /* The disposition and content-type metadata are associated with
513 * the envelope for message parts */
514 GMimeObject *meta = node->envelope_part ? (
515 GMIME_OBJECT (node->envelope_part) ) : node->part;
516 GMimeContentType *content_type = g_mime_object_get_content_type (meta);
517 const bool leaf = GMIME_IS_PART (node->part);
518 GMimeStream *stream = params->out_stream;
519 const char *part_type;
522 if (node->envelope_file) {
523 notmuch_message_t *message = node->envelope_file;
525 part_type = "message";
526 g_mime_stream_printf (stream, "\f%s{ id:%s depth:%d match:%d excluded:%d filename:%s\n",
528 notmuch_message_get_message_id (message),
530 _get_message_flag (message, NOTMUCH_MESSAGE_FLAG_MATCH) ? 1 : 0,
531 _get_message_flag (message, NOTMUCH_MESSAGE_FLAG_EXCLUDED) ? 1 : 0,
532 notmuch_message_get_filename (message));
534 char *content_string;
535 const char *disposition = _get_disposition (meta);
536 const char *cid = g_mime_object_get_content_id (meta);
537 const char *filename = leaf ? (
538 g_mime_part_get_filename (GMIME_PART (node->part)) ) : NULL;
541 strcasecmp (disposition, GMIME_DISPOSITION_ATTACHMENT) == 0)
542 part_type = "attachment";
546 g_mime_stream_printf (stream, "\f%s{ ID: %d", part_type, node->part_num);
548 g_mime_stream_printf (stream, ", Filename: %s", filename);
550 g_mime_stream_printf (stream, ", Content-id: %s", cid);
552 content_string = g_mime_content_type_get_mime_type (content_type);
553 g_mime_stream_printf (stream, ", Content-type: %s\n", content_string);
554 g_free (content_string);
557 if (GMIME_IS_MESSAGE (node->part)) {
558 GMimeMessage *message = GMIME_MESSAGE (node->part);
559 char *recipients_string;
562 g_mime_stream_printf (stream, "\fheader{\n");
563 if (node->envelope_file)
564 g_mime_stream_printf (stream, "%s\n", _get_one_line_summary (ctx, node->envelope_file));
565 g_mime_stream_printf (stream, "Subject: %s\n", g_mime_message_get_subject (message));
566 g_mime_stream_printf (stream, "From: %s\n", g_mime_message_get_from_string (message));
567 recipients_string = g_mime_message_get_address_string (message, GMIME_ADDRESS_TYPE_TO);
568 if (recipients_string)
569 g_mime_stream_printf (stream, "To: %s\n", recipients_string);
570 g_free (recipients_string);
571 recipients_string = g_mime_message_get_address_string (message, GMIME_ADDRESS_TYPE_CC);
572 if (recipients_string)
573 g_mime_stream_printf (stream, "Cc: %s\n", recipients_string);
574 g_free (recipients_string);
575 date_string = g_mime_message_get_date_string (node, message);
576 g_mime_stream_printf (stream, "Date: %s\n", date_string);
577 g_mime_stream_printf (stream, "\fheader}\n");
579 if (! params->output_body) {
580 g_mime_stream_printf (stream, "\f%s}\n", part_type);
581 return NOTMUCH_STATUS_SUCCESS;
583 g_mime_stream_printf (stream, "\fbody{\n");
587 if (g_mime_content_type_is_type (content_type, "text", "*") &&
588 (params->include_html ||
589 ! g_mime_content_type_is_type (content_type, "text", "html"))) {
590 show_text_part_content (node->part, stream, 0);
592 char *content_string = g_mime_content_type_get_mime_type (content_type);
593 g_mime_stream_printf (stream, "Non-text part: %s\n", content_string);
594 g_free (content_string);
598 for (i = 0; i < node->nchildren; i++)
599 format_part_text (ctx, sp, mime_node_child (node, i), indent, params);
601 if (GMIME_IS_MESSAGE (node->part))
602 g_mime_stream_printf (stream, "\fbody}\n");
604 g_mime_stream_printf (stream, "\f%s}\n", part_type);
606 return NOTMUCH_STATUS_SUCCESS;
610 format_omitted_part_meta_sprinter (sprinter_t *sp, GMimeObject *meta, GMimePart *part)
612 const char *content_charset = g_mime_object_get_content_type_parameter (meta, "charset");
613 const char *cte = g_mime_object_get_header (meta, "content-transfer-encoding");
614 GMimeDataWrapper *wrapper = g_mime_part_get_content (part);
615 GMimeStream *stream = g_mime_data_wrapper_get_stream (wrapper);
616 ssize_t content_length = g_mime_stream_length (stream);
618 if (content_charset != NULL) {
619 sp->map_key (sp, "content-charset");
620 sp->string (sp, content_charset);
623 sp->map_key (sp, "content-transfer-encoding");
624 sp->string (sp, cte);
626 if (content_length >= 0) {
627 sp->map_key (sp, "content-length");
628 sp->integer (sp, content_length);
633 format_part_sprinter (const void *ctx, sprinter_t *sp, mime_node_t *node,
637 /* Any changes to the JSON or S-Expression format should be
638 * reflected in the file devel/schemata. */
640 if (node->envelope_file) {
641 const _notmuch_message_crypto_t *msg_crypto = NULL;
643 format_message_sprinter (sp, node->envelope_file);
646 sp->map_key (sp, "body");
648 format_part_sprinter (ctx, sp, mime_node_child (node, 0), true, include_html);
652 msg_crypto = mime_node_get_message_crypto_status (node);
653 if (notmuch_format_version >= 4) {
654 sp->map_key (sp, "crypto");
656 if (msg_crypto->sig_list ||
657 msg_crypto->decryption_status != NOTMUCH_MESSAGE_DECRYPTED_NONE) {
658 if (msg_crypto->sig_list) {
659 sp->map_key (sp, "signed");
661 sp->map_key (sp, "status");
662 format_part_sigstatus_sprinter (sp, msg_crypto->sig_list);
663 if (msg_crypto->signature_encrypted) {
664 sp->map_key (sp, "encrypted");
665 sp->boolean (sp, msg_crypto->signature_encrypted);
667 if (msg_crypto->payload_subject) {
668 sp->map_key (sp, "headers");
670 sp->string (sp, "Subject");
675 if (msg_crypto->decryption_status != NOTMUCH_MESSAGE_DECRYPTED_NONE) {
676 sp->map_key (sp, "decrypted");
678 sp->map_key (sp, "status");
679 sp->string (sp, msg_crypto->decryption_status == NOTMUCH_MESSAGE_DECRYPTED_FULL ?
682 if (msg_crypto->payload_subject) {
683 const char *subject = g_mime_message_get_subject GMIME_MESSAGE (node->part);
684 if (subject == NULL || strcmp (subject, msg_crypto->payload_subject)) {
685 /* protected subject differs from the external header */
686 sp->map_key (sp, "header-mask");
688 sp->map_key (sp, "Subject");
692 sp->string (sp, subject);
702 sp->map_key (sp, "headers");
703 format_headers_sprinter (sp, GMIME_MESSAGE (node->part), false, msg_crypto);
709 /* The disposition and content-type metadata are associated with
710 * the envelope for message parts */
711 GMimeObject *meta = node->envelope_part ? (
712 GMIME_OBJECT (node->envelope_part) ) : node->part;
713 GMimeContentType *content_type = g_mime_object_get_content_type (meta);
714 char *content_string;
715 const char *disposition = _get_disposition (meta);
716 const char *cid = g_mime_object_get_content_id (meta);
717 const char *filename = GMIME_IS_PART (node->part) ? (
718 g_mime_part_get_filename (GMIME_PART (node->part) ) ) : NULL;
724 sp->map_key (sp, "id");
725 sp->integer (sp, node->part_num);
727 if (node->decrypt_attempted) {
728 sp->map_key (sp, "encstatus");
731 sp->map_key (sp, "status");
732 sp->string (sp, node->decrypt_success ? "good" : "bad");
737 if (node->verify_attempted) {
738 sp->map_key (sp, "sigstatus");
739 format_part_sigstatus_sprinter (sp, node->sig_list);
742 sp->map_key (sp, "content-type");
743 content_string = g_mime_content_type_get_mime_type (content_type);
744 sp->string (sp, content_string);
745 g_free (content_string);
748 sp->map_key (sp, "content-disposition");
749 sp->string (sp, disposition);
753 sp->map_key (sp, "content-id");
754 sp->string (sp, cid);
758 sp->map_key (sp, "filename");
759 sp->string (sp, filename);
762 if (GMIME_IS_PART (node->part)) {
763 /* For non-HTML text parts, we include the content in the
764 * JSON. Since JSON must be Unicode, we handle charset
765 * decoding here and do not report a charset to the caller.
766 * For text/html parts, we do not include the content unless
767 * the --include-html option has been passed. If a html part
768 * is not included, it can be requested directly. This makes
769 * charset decoding the responsibility on the caller so we
770 * report the charset for text/html parts.
772 if (g_mime_content_type_is_type (content_type, "text", "*") &&
774 ! g_mime_content_type_is_type (content_type, "text", "html"))) {
775 GMimeStream *stream_memory = g_mime_stream_mem_new ();
776 GByteArray *part_content;
777 show_text_part_content (node->part, stream_memory, 0);
778 part_content = g_mime_stream_mem_get_byte_array (GMIME_STREAM_MEM (stream_memory));
779 sp->map_key (sp, "content");
780 sp->string_len (sp, (char *) part_content->data, part_content->len);
781 g_object_unref (stream_memory);
783 /* if we have a child part despite being a standard
784 * (non-multipart) MIME part, that means there is
785 * something to unwrap, which we will present in
787 if (node->nchildren) {
788 sp->map_key (sp, "content");
792 format_omitted_part_meta_sprinter (sp, meta, GMIME_PART (node->part));
794 } else if (GMIME_IS_MULTIPART (node->part)) {
795 sp->map_key (sp, "content");
798 } else if (GMIME_IS_MESSAGE (node->part)) {
799 sp->map_key (sp, "content");
803 sp->map_key (sp, "headers");
804 format_headers_sprinter (sp, GMIME_MESSAGE (node->part), false, NULL);
806 sp->map_key (sp, "body");
811 for (i = 0; i < node->nchildren; i++)
812 format_part_sprinter (ctx, sp, mime_node_child (node, i), true, include_html);
814 /* Close content structures */
815 for (i = 0; i < nclose; i++)
821 static notmuch_status_t
822 format_part_sprinter_entry (const void *ctx, sprinter_t *sp,
823 mime_node_t *node, unused (int indent),
824 const notmuch_show_params_t *params)
826 format_part_sprinter (ctx, sp, node, params->output_body, params->include_html);
828 return NOTMUCH_STATUS_SUCCESS;
831 /* Print a message in "mboxrd" format as documented, for example,
834 * http://qmail.org/qmail-manual-html/man5/mbox.html
836 static notmuch_status_t
837 format_part_mbox (const void *ctx, unused (sprinter_t *sp), mime_node_t *node,
839 unused (const notmuch_show_params_t *params))
841 notmuch_message_t *message = node->envelope_file;
843 const char *filename;
848 struct tm date_gmtime;
849 char date_asctime[26];
856 INTERNAL_ERROR ("format_part_mbox requires a root part");
858 filename = notmuch_message_get_filename (message);
859 file = gzopen (filename, "r");
861 fprintf (stderr, "Failed to open %s: %s\n",
862 filename, strerror (errno));
863 return NOTMUCH_STATUS_FILE_ERROR;
866 from = notmuch_message_get_header (message, "from");
867 from = _extract_email_address (ctx, from);
869 date = notmuch_message_get_date (message);
870 gmtime_r (&date, &date_gmtime);
871 asctime_r (&date_gmtime, date_asctime);
873 printf ("From %s %s", from, date_asctime);
875 while ((line_len = gz_getline (message, &line, &line_size, file)) != UTIL_EOF ) {
876 if (_is_from_line (line))
885 return NOTMUCH_STATUS_SUCCESS;
888 static notmuch_status_t
889 format_part_raw (unused (const void *ctx), unused (sprinter_t *sp),
890 mime_node_t *node, unused (int indent),
891 const notmuch_show_params_t *params)
893 if (node->envelope_file) {
894 /* Special case the entire message to avoid MIME parsing. */
895 const char *filename;
896 GMimeStream *stream = NULL;
899 notmuch_status_t ret = NOTMUCH_STATUS_FILE_ERROR;
901 filename = notmuch_message_get_filename (node->envelope_file);
902 if (filename == NULL) {
903 fprintf (stderr, "Error: Cannot get message filename.\n");
907 stream = g_mime_stream_gzfile_open (filename);
908 if (stream == NULL) {
909 fprintf (stderr, "Error: Cannot open file %s: %s\n", filename, strerror (errno));
913 while (! g_mime_stream_eos (stream)) {
914 ssize = g_mime_stream_read (stream, buf, sizeof (buf));
916 fprintf (stderr, "Error: Read failed from %s\n", filename);
920 if (ssize > 0 && fwrite (buf, ssize, 1, stdout) != 1) {
921 fprintf (stderr, "Error: Write %zd chars to stdout failed\n", ssize);
926 ret = NOTMUCH_STATUS_SUCCESS;
928 /* XXX This DONE is just for the special case of a node in a single file */
931 g_object_unref (stream);
936 GMimeStream *stream_filter = g_mime_stream_filter_new (params->out_stream);
938 if (GMIME_IS_PART (node->part)) {
939 /* For leaf parts, we emit only the transfer-decoded
941 GMimeDataWrapper *wrapper;
942 wrapper = g_mime_part_get_content (GMIME_PART (node->part));
944 if (wrapper && stream_filter)
945 g_mime_data_wrapper_write_to_stream (wrapper, stream_filter);
947 /* Write out the whole part. For message parts (the root
948 * part and embedded message parts), this will be the
949 * message including its headers (but not the
950 * encapsulating part's headers). For multipart parts,
951 * this will include the headers. */
953 g_mime_object_write_to_stream (node->part, NULL, stream_filter);
957 g_object_unref (stream_filter);
959 return NOTMUCH_STATUS_SUCCESS;
962 static notmuch_status_t
963 show_message (void *ctx,
964 const notmuch_show_format_t *format,
966 notmuch_message_t *message,
968 notmuch_show_params_t *params)
970 void *local = talloc_new (ctx);
971 mime_node_t *root, *part;
972 notmuch_status_t status;
973 unsigned int session_keys = 0;
974 notmuch_status_t session_key_count_error = NOTMUCH_STATUS_SUCCESS;
976 if (params->crypto.decrypt == NOTMUCH_DECRYPT_TRUE)
977 session_key_count_error = notmuch_message_count_properties (message, "session-key",
980 status = mime_node_open (local, message, &(params->crypto), &root);
983 part = mime_node_seek_dfs (root, (params->part < 0 ? 0 : params->part));
985 status = format->part (local, sp, part, indent, params);
986 if (params->crypto.decrypt == NOTMUCH_DECRYPT_TRUE && session_key_count_error ==
987 NOTMUCH_STATUS_SUCCESS) {
988 unsigned int new_session_keys = 0;
989 if (notmuch_message_count_properties (message, "session-key", &new_session_keys) ==
990 NOTMUCH_STATUS_SUCCESS &&
991 new_session_keys > session_keys) {
992 /* try a quiet re-indexing */
993 notmuch_indexopts_t *indexopts = notmuch_database_get_default_indexopts (
994 notmuch_message_get_database (message));
996 notmuch_indexopts_set_decrypt_policy (indexopts, NOTMUCH_DECRYPT_AUTO);
997 print_status_message ("Error re-indexing message with --decrypt=stash",
998 message, notmuch_message_reindex (message, indexopts));
1003 talloc_free (local);
1007 static notmuch_status_t
1008 show_messages (void *ctx,
1009 const notmuch_show_format_t *format,
1011 notmuch_messages_t *messages,
1013 notmuch_show_params_t *params)
1015 notmuch_message_t *message;
1019 notmuch_status_t status, res = NOTMUCH_STATUS_SUCCESS;
1021 sp->begin_list (sp);
1024 notmuch_messages_valid (messages);
1025 notmuch_messages_move_to_next (messages)) {
1026 sp->begin_list (sp);
1028 message = notmuch_messages_get (messages);
1030 match = _get_message_flag (message, NOTMUCH_MESSAGE_FLAG_MATCH);
1031 excluded = _get_message_flag (message, NOTMUCH_MESSAGE_FLAG_EXCLUDED);
1033 next_indent = indent;
1035 if ((match && (! excluded || ! params->omit_excluded)) || params->entire_thread) {
1036 status = show_message (ctx, format, sp, message, indent, params);
1037 if (status && ! res)
1039 next_indent = indent + 1;
1044 status = show_messages (ctx,
1046 notmuch_message_get_replies (message),
1049 if (status && ! res)
1052 notmuch_message_destroy (message);
1062 /* Formatted output of single message */
1064 do_show_single (void *ctx,
1065 notmuch_query_t *query,
1066 const notmuch_show_format_t *format,
1068 notmuch_show_params_t *params)
1070 notmuch_messages_t *messages;
1071 notmuch_message_t *message;
1072 notmuch_status_t status;
1075 status = notmuch_query_count_messages (query, &count);
1076 if (print_status_query ("notmuch show", query, status))
1081 "Error: search term did not match precisely one message (matched %u messages).\n",
1086 status = notmuch_query_search_messages (query, &messages);
1087 if (print_status_query ("notmuch show", query, status))
1090 message = notmuch_messages_get (messages);
1092 if (message == NULL) {
1093 fprintf (stderr, "Error: Cannot find matching message.\n");
1097 notmuch_message_set_flag (message, NOTMUCH_MESSAGE_FLAG_MATCH, 1);
1099 return show_message (ctx, format, sp, message, 0, params)
1100 != NOTMUCH_STATUS_SUCCESS;
1103 /* Formatted output of threads */
1105 do_show_threaded (void *ctx,
1106 notmuch_query_t *query,
1107 const notmuch_show_format_t *format,
1109 notmuch_show_params_t *params)
1111 notmuch_threads_t *threads;
1112 notmuch_thread_t *thread;
1113 notmuch_messages_t *messages;
1114 notmuch_status_t status, res = NOTMUCH_STATUS_SUCCESS;
1116 status = notmuch_query_search_threads (query, &threads);
1117 if (print_status_query ("notmuch show", query, status))
1120 sp->begin_list (sp);
1123 notmuch_threads_valid (threads);
1124 notmuch_threads_move_to_next (threads)) {
1125 thread = notmuch_threads_get (threads);
1127 messages = notmuch_thread_get_toplevel_messages (thread);
1129 if (messages == NULL)
1130 INTERNAL_ERROR ("Thread %s has no toplevel messages.\n",
1131 notmuch_thread_get_thread_id (thread));
1133 status = show_messages (ctx, format, sp, messages, 0, params);
1134 if (status && ! res)
1137 notmuch_thread_destroy (thread);
1143 return res != NOTMUCH_STATUS_SUCCESS;
1147 do_show_unthreaded (void *ctx,
1148 notmuch_query_t *query,
1149 const notmuch_show_format_t *format,
1151 notmuch_show_params_t *params)
1153 notmuch_messages_t *messages;
1154 notmuch_message_t *message;
1155 notmuch_status_t status, res = NOTMUCH_STATUS_SUCCESS;
1156 notmuch_bool_t excluded;
1158 status = notmuch_query_search_messages (query, &messages);
1159 if (print_status_query ("notmuch show", query, status))
1162 sp->begin_list (sp);
1165 notmuch_messages_valid (messages);
1166 notmuch_messages_move_to_next (messages)) {
1167 sp->begin_list (sp);
1168 sp->begin_list (sp);
1170 message = notmuch_messages_get (messages);
1172 notmuch_message_set_flag (message, NOTMUCH_MESSAGE_FLAG_MATCH, TRUE);
1173 excluded = _get_message_flag (message, NOTMUCH_MESSAGE_FLAG_EXCLUDED);
1175 if (! excluded || ! params->omit_excluded) {
1176 status = show_message (ctx, format, sp, message, 0, params);
1177 if (status && ! res)
1182 notmuch_message_destroy (message);
1191 NOTMUCH_FORMAT_NOT_SPECIFIED,
1192 NOTMUCH_FORMAT_JSON,
1193 NOTMUCH_FORMAT_SEXP,
1194 NOTMUCH_FORMAT_TEXT,
1195 NOTMUCH_FORMAT_MBOX,
1199 static const notmuch_show_format_t format_json = {
1200 .new_sprinter = sprinter_json_create,
1201 .part = format_part_sprinter_entry,
1204 static const notmuch_show_format_t format_sexp = {
1205 .new_sprinter = sprinter_sexp_create,
1206 .part = format_part_sprinter_entry,
1209 static const notmuch_show_format_t format_text = {
1210 .new_sprinter = sprinter_text_create,
1211 .part = format_part_text,
1214 static const notmuch_show_format_t format_mbox = {
1215 .new_sprinter = sprinter_text_create,
1216 .part = format_part_mbox,
1219 static const notmuch_show_format_t format_raw = {
1220 .new_sprinter = sprinter_text_create,
1221 .part = format_part_raw,
1224 static const notmuch_show_format_t *formatters[] = {
1225 [NOTMUCH_FORMAT_JSON] = &format_json,
1226 [NOTMUCH_FORMAT_SEXP] = &format_sexp,
1227 [NOTMUCH_FORMAT_TEXT] = &format_text,
1228 [NOTMUCH_FORMAT_MBOX] = &format_mbox,
1229 [NOTMUCH_FORMAT_RAW] = &format_raw,
1233 notmuch_show_command (notmuch_database_t *notmuch, int argc, char *argv[])
1235 notmuch_query_t *query;
1238 const notmuch_show_format_t *formatter;
1239 sprinter_t *sprinter;
1240 notmuch_show_params_t params = {
1242 .omit_excluded = true,
1243 .output_body = true,
1244 .crypto = { .decrypt = NOTMUCH_DECRYPT_AUTO },
1246 int format = NOTMUCH_FORMAT_NOT_SPECIFIED;
1247 bool exclude = true;
1248 bool entire_thread_set = false;
1249 bool single_message;
1250 bool unthreaded = FALSE;
1251 notmuch_status_t status;
1253 notmuch_opt_desc_t options[] = {
1254 { .opt_keyword = &format, .name = "format", .keywords =
1255 (notmuch_keyword_t []){ { "json", NOTMUCH_FORMAT_JSON },
1256 { "text", NOTMUCH_FORMAT_TEXT },
1257 { "sexp", NOTMUCH_FORMAT_SEXP },
1258 { "mbox", NOTMUCH_FORMAT_MBOX },
1259 { "raw", NOTMUCH_FORMAT_RAW },
1261 { .opt_int = ¬much_format_version, .name = "format-version" },
1262 { .opt_bool = &exclude, .name = "exclude" },
1263 { .opt_bool = ¶ms.entire_thread, .name = "entire-thread",
1264 .present = &entire_thread_set },
1265 { .opt_bool = &unthreaded, .name = "unthreaded" },
1266 { .opt_int = ¶ms.part, .name = "part" },
1267 { .opt_keyword = (int *) (¶ms.crypto.decrypt), .name = "decrypt",
1268 .keyword_no_arg_value = "true", .keywords =
1269 (notmuch_keyword_t []){ { "false", NOTMUCH_DECRYPT_FALSE },
1270 { "auto", NOTMUCH_DECRYPT_AUTO },
1271 { "true", NOTMUCH_DECRYPT_NOSTASH },
1272 { "stash", NOTMUCH_DECRYPT_TRUE },
1274 { .opt_bool = ¶ms.crypto.verify, .name = "verify" },
1275 { .opt_bool = ¶ms.output_body, .name = "body" },
1276 { .opt_bool = ¶ms.include_html, .name = "include-html" },
1277 { .opt_inherit = notmuch_shared_options },
1281 opt_index = parse_arguments (argc, argv, options, 1);
1283 return EXIT_FAILURE;
1285 notmuch_process_shared_options (argv[0]);
1287 /* explicit decryption implies verification */
1288 if (params.crypto.decrypt == NOTMUCH_DECRYPT_NOSTASH ||
1289 params.crypto.decrypt == NOTMUCH_DECRYPT_TRUE)
1290 params.crypto.verify = true;
1292 /* specifying a part implies single message display */
1293 single_message = params.part >= 0;
1295 if (format == NOTMUCH_FORMAT_NOT_SPECIFIED) {
1296 /* if part was requested and format was not specified, use format=raw */
1297 if (params.part >= 0)
1298 format = NOTMUCH_FORMAT_RAW;
1300 format = NOTMUCH_FORMAT_TEXT;
1303 if (format == NOTMUCH_FORMAT_MBOX) {
1304 if (params.part > 0) {
1305 fprintf (stderr, "Error: specifying parts is incompatible with mbox output format.\n");
1306 return EXIT_FAILURE;
1308 } else if (format == NOTMUCH_FORMAT_RAW) {
1309 /* raw format only supports single message display */
1310 single_message = true;
1313 notmuch_exit_if_unsupported_format ();
1315 /* Default is entire-thread = false except for format=json and
1317 if (! entire_thread_set &&
1318 (format == NOTMUCH_FORMAT_JSON || format == NOTMUCH_FORMAT_SEXP))
1319 params.entire_thread = true;
1321 if (! params.output_body) {
1322 if (params.part > 0) {
1323 fprintf (stderr, "Warning: --body=false is incompatible with --part > 0. Disabling.\n");
1324 params.output_body = true;
1326 if (format != NOTMUCH_FORMAT_TEXT &&
1327 format != NOTMUCH_FORMAT_JSON &&
1328 format != NOTMUCH_FORMAT_SEXP)
1330 "Warning: --body=false only implemented for format=text, format=json and format=sexp\n");
1334 if (params.include_html &&
1335 (format != NOTMUCH_FORMAT_TEXT &&
1336 format != NOTMUCH_FORMAT_JSON &&
1337 format != NOTMUCH_FORMAT_SEXP)) {
1339 "Warning: --include-html only implemented for format=text, format=json and format=sexp\n");
1342 if (params.crypto.decrypt == NOTMUCH_DECRYPT_TRUE) {
1343 status = notmuch_database_reopen (notmuch, NOTMUCH_DATABASE_MODE_READ_WRITE);
1345 fprintf (stderr, "Error reopening database for READ_WRITE: %s\n",
1346 notmuch_status_to_string (status));
1347 return EXIT_FAILURE;
1351 notmuch_exit_if_unmatched_db_uuid (notmuch);
1353 query_string = query_string_from_args (notmuch, argc - opt_index, argv + opt_index);
1354 if (query_string == NULL) {
1355 fprintf (stderr, "Out of memory\n");
1356 return EXIT_FAILURE;
1359 if (*query_string == '\0') {
1360 fprintf (stderr, "Error: notmuch show requires at least one search term.\n");
1361 return EXIT_FAILURE;
1364 query = notmuch_query_create (notmuch, query_string);
1365 if (query == NULL) {
1366 fprintf (stderr, "Out of memory\n");
1367 return EXIT_FAILURE;
1370 /* Create structure printer. */
1371 formatter = formatters[format];
1372 sprinter = formatter->new_sprinter (notmuch, stdout);
1374 params.out_stream = g_mime_stream_stdout_new ();
1376 /* If a single message is requested we do not use search_excludes. */
1377 if (single_message) {
1378 ret = do_show_single (notmuch, query, formatter, sprinter, ¶ms);
1380 /* We always apply set the exclude flag. The
1381 * exclude=true|false option controls whether or not we return
1382 * threads that only match in an excluded message */
1383 notmuch_config_values_t *exclude_tags;
1384 notmuch_status_t status;
1386 for (exclude_tags = notmuch_config_get_values (notmuch, NOTMUCH_CONFIG_EXCLUDE_TAGS);
1387 notmuch_config_values_valid (exclude_tags);
1388 notmuch_config_values_move_to_next (exclude_tags)) {
1390 status = notmuch_query_add_tag_exclude (query,
1391 notmuch_config_values_get (exclude_tags));
1392 if (status && status != NOTMUCH_STATUS_IGNORED) {
1393 print_status_query ("notmuch show", query, status);
1399 if (exclude == false) {
1400 notmuch_query_set_omit_excluded (query, false);
1401 params.omit_excluded = false;
1405 ret = do_show_unthreaded (notmuch, query, formatter, sprinter, ¶ms);
1407 ret = do_show_threaded (notmuch, query, formatter, sprinter, ¶ms);
1411 g_mime_stream_flush (params.out_stream);
1412 g_object_unref (params.out_stream);
1414 _notmuch_crypto_cleanup (¶ms.crypto);
1415 notmuch_query_destroy (query);
1416 notmuch_database_destroy (notmuch);
1418 return ret ? EXIT_FAILURE : EXIT_SUCCESS;