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_filename (notmuch_message_t *message, int index)
29 notmuch_filenames_t *filenames = notmuch_message_get_filenames (message);
33 notmuch_filenames_valid (filenames);
34 notmuch_filenames_move_to_next (filenames), i++) {
36 return notmuch_filenames_get (filenames);
42 _get_tags_as_string (const void *ctx, notmuch_message_t *message)
49 result = talloc_strdup (ctx, "");
53 for (tags = notmuch_message_get_tags (message);
54 notmuch_tags_valid (tags);
55 notmuch_tags_move_to_next (tags)) {
56 tag = notmuch_tags_get (tags);
58 result = talloc_asprintf_append (result, "%s%s",
59 first ? "" : " ", tag);
66 /* Get a nice, single-line summary of message. */
68 _get_one_line_summary (const void *ctx, notmuch_message_t *message)
72 const char *relative_date;
75 from = notmuch_message_get_header (message, "from");
77 date = notmuch_message_get_date (message);
78 relative_date = notmuch_time_relative_date (ctx, date);
80 tags = _get_tags_as_string (ctx, message);
82 return talloc_asprintf (ctx, "%s (%s) (%s)",
83 from, relative_date, tags);
87 _get_disposition (GMimeObject *meta)
89 GMimeContentDisposition *disposition;
91 disposition = g_mime_object_get_content_disposition (meta);
95 return g_mime_content_disposition_get_disposition (disposition);
99 _get_message_flag (notmuch_message_t *message, notmuch_message_flag_t flag)
101 notmuch_bool_t is_set;
102 notmuch_status_t status;
104 status = notmuch_message_get_flag_st (message, flag, &is_set);
106 if (print_status_message ("notmuch show", message, status))
107 INTERNAL_ERROR ("unexpected error getting message flag\n");
112 /* Emit a sequence of key/value pairs for the metadata of message.
113 * The caller should begin a map before calling this. */
115 format_message_sprinter (sprinter_t *sp, notmuch_message_t *message)
117 /* Any changes to the JSON or S-Expression format should be
118 * reflected in the file devel/schemata. */
120 void *local = talloc_new (NULL);
121 notmuch_tags_t *tags;
123 const char *relative_date;
125 sp->map_key (sp, "id");
126 sp->string (sp, notmuch_message_get_message_id (message));
128 sp->map_key (sp, "match");
129 sp->boolean (sp, _get_message_flag (message, NOTMUCH_MESSAGE_FLAG_MATCH));
131 sp->map_key (sp, "excluded");
132 sp->boolean (sp, _get_message_flag (message, NOTMUCH_MESSAGE_FLAG_EXCLUDED));
134 sp->map_key (sp, "filename");
135 if (notmuch_format_version >= 3) {
136 notmuch_filenames_t *filenames;
139 for (filenames = notmuch_message_get_filenames (message);
140 notmuch_filenames_valid (filenames);
141 notmuch_filenames_move_to_next (filenames)) {
142 sp->string (sp, notmuch_filenames_get (filenames));
144 notmuch_filenames_destroy (filenames);
147 sp->string (sp, notmuch_message_get_filename (message));
150 sp->map_key (sp, "timestamp");
151 date = notmuch_message_get_date (message);
152 sp->integer (sp, date);
154 sp->map_key (sp, "date_relative");
155 relative_date = notmuch_time_relative_date (local, date);
156 sp->string (sp, relative_date);
158 sp->map_key (sp, "tags");
160 for (tags = notmuch_message_get_tags (message);
161 notmuch_tags_valid (tags);
162 notmuch_tags_move_to_next (tags))
163 sp->string (sp, notmuch_tags_get (tags));
169 /* Extract just the email address from the contents of a From:
172 _extract_email_address (const void *ctx, const char *from)
174 InternetAddressList *addresses;
175 InternetAddress *address;
176 InternetAddressMailbox *mailbox;
177 const char *email = "MAILER-DAEMON";
179 addresses = internet_address_list_parse (NULL, from);
181 /* Bail if there is no address here. */
182 if (addresses == NULL || internet_address_list_length (addresses) < 1)
185 /* Otherwise, just use the first address. */
186 address = internet_address_list_get_address (addresses, 0);
188 /* The From header should never contain an address group rather
189 * than a mailbox. So bail if it does. */
190 if (! INTERNET_ADDRESS_IS_MAILBOX (address))
193 mailbox = INTERNET_ADDRESS_MAILBOX (address);
194 email = internet_address_mailbox_get_addr (mailbox);
195 email = talloc_strdup (ctx, email);
199 g_object_unref (addresses);
204 /* Return 1 if 'line' is an mbox From_ line---that is, a line
205 * beginning with zero or more '>' characters followed by the
206 * characters 'F', 'r', 'o', 'm', and space.
208 * Any characters at all may appear after that in the line.
211 _is_from_line (const char *line)
213 const char *s = line;
221 if (STRNCMP_LITERAL (s, "From ") == 0)
227 /* Output extra headers if configured with the `show.extra_headers'
228 * configuration option
231 format_extra_headers_sprinter (sprinter_t *sp, GMimeMessage *message)
233 GMimeHeaderList *header_list = g_mime_object_get_header_list (GMIME_OBJECT (message));
235 for (notmuch_config_values_t *extra_headers = notmuch_config_get_values (
236 sp->notmuch, NOTMUCH_CONFIG_EXTRA_HEADERS);
237 notmuch_config_values_valid (extra_headers);
238 notmuch_config_values_move_to_next (extra_headers)) {
240 const char *header_name = notmuch_config_values_get (extra_headers);
242 header = g_mime_header_list_get_header (header_list, header_name);
246 sp->map_key (sp, g_mime_header_get_name (header));
247 sp->string (sp, g_mime_header_get_value (header));
252 format_headers_sprinter (sprinter_t *sp, GMimeMessage *message,
253 bool reply, const _notmuch_message_crypto_t *msg_crypto)
255 /* Any changes to the JSON or S-Expression format should be
256 * reflected in the file devel/schemata. */
258 char *recipients_string;
259 const char *reply_to_string;
260 void *local = talloc_new (sp);
264 sp->map_key (sp, "Subject");
265 if (msg_crypto && msg_crypto->payload_subject) {
266 sp->string (sp, msg_crypto->payload_subject);
268 sp->string (sp, g_mime_message_get_subject (message));
270 sp->map_key (sp, "From");
271 sp->string (sp, g_mime_message_get_from_string (message));
273 recipients_string = g_mime_message_get_address_string (message, GMIME_ADDRESS_TYPE_TO);
274 if (recipients_string) {
275 sp->map_key (sp, "To");
276 sp->string (sp, recipients_string);
277 g_free (recipients_string);
280 recipients_string = g_mime_message_get_address_string (message, GMIME_ADDRESS_TYPE_CC);
281 if (recipients_string) {
282 sp->map_key (sp, "Cc");
283 sp->string (sp, recipients_string);
284 g_free (recipients_string);
287 recipients_string = g_mime_message_get_address_string (message, GMIME_ADDRESS_TYPE_BCC);
288 if (recipients_string) {
289 sp->map_key (sp, "Bcc");
290 sp->string (sp, recipients_string);
291 g_free (recipients_string);
294 reply_to_string = g_mime_message_get_reply_to_string (local, message);
295 if (reply_to_string) {
296 sp->map_key (sp, "Reply-To");
297 sp->string (sp, reply_to_string);
301 sp->map_key (sp, "In-reply-to");
302 sp->string (sp, g_mime_object_get_header (GMIME_OBJECT (message), "In-reply-to"));
304 sp->map_key (sp, "References");
305 sp->string (sp, g_mime_object_get_header (GMIME_OBJECT (message), "References"));
307 sp->map_key (sp, "Date");
308 sp->string (sp, g_mime_message_get_date_string (sp, message));
311 /* Output extra headers the user has configured, if any */
313 format_extra_headers_sprinter (sp, message);
318 /* Write a MIME text part out to the given stream.
320 * If (flags & NOTMUCH_SHOW_TEXT_PART_REPLY), this prepends "> " to
323 * Both line-ending conversion (CRLF->LF) and charset conversion ( ->
324 * UTF-8) will be performed, so it is inappropriate to call this
325 * function with a non-text part. Doing so will trigger an internal
329 show_text_part_content (GMimeObject *part, GMimeStream *stream_out,
330 notmuch_show_text_part_flags flags)
332 GMimeContentType *content_type = g_mime_object_get_content_type (GMIME_OBJECT (part));
333 GMimeStream *stream_filter = NULL;
334 GMimeFilter *crlf_filter = NULL;
335 GMimeFilter *windows_filter = NULL;
336 GMimeDataWrapper *wrapper;
339 if (! g_mime_content_type_is_type (content_type, "text", "*"))
340 INTERNAL_ERROR ("Illegal request to format non-text part (%s) as text.",
341 g_mime_content_type_get_mime_type (content_type));
343 if (stream_out == NULL)
346 charset = g_mime_object_get_content_type_parameter (part, "charset");
347 charset = charset ? g_mime_charset_canon_name (charset) : NULL;
348 wrapper = g_mime_part_get_content (GMIME_PART (part));
349 if (wrapper && charset && ! g_ascii_strncasecmp (charset, "iso-8859-", 9)) {
350 GMimeStream *null_stream = NULL;
351 GMimeStream *null_stream_filter = NULL;
353 /* Check for mislabeled Windows encoding */
354 null_stream = g_mime_stream_null_new ();
355 null_stream_filter = g_mime_stream_filter_new (null_stream);
356 windows_filter = g_mime_filter_windows_new (charset);
357 g_mime_stream_filter_add (GMIME_STREAM_FILTER (null_stream_filter),
359 g_mime_data_wrapper_write_to_stream (wrapper, null_stream_filter);
360 charset = g_mime_filter_windows_real_charset (
361 (GMimeFilterWindows *) windows_filter);
363 if (null_stream_filter)
364 g_object_unref (null_stream_filter);
366 g_object_unref (null_stream);
367 /* Keep a reference to windows_filter in order to prevent the
368 * charset string from deallocation. */
371 stream_filter = g_mime_stream_filter_new (stream_out);
372 crlf_filter = g_mime_filter_dos2unix_new (false);
373 g_mime_stream_filter_add (GMIME_STREAM_FILTER (stream_filter),
375 g_object_unref (crlf_filter);
378 GMimeFilter *charset_filter;
379 charset_filter = g_mime_filter_charset_new (charset, "UTF-8");
380 /* This result can be NULL for things like "unknown-8bit".
381 * Don't set a NULL filter as that makes GMime print
382 * annoying assertion-failure messages on stderr. */
383 if (charset_filter) {
384 g_mime_stream_filter_add (GMIME_STREAM_FILTER (stream_filter),
386 g_object_unref (charset_filter);
391 if (flags & NOTMUCH_SHOW_TEXT_PART_REPLY) {
392 GMimeFilter *reply_filter;
393 reply_filter = g_mime_filter_reply_new (true);
395 g_mime_stream_filter_add (GMIME_STREAM_FILTER (stream_filter),
397 g_object_unref (reply_filter);
401 if (wrapper && stream_filter)
402 g_mime_data_wrapper_write_to_stream (wrapper, stream_filter);
404 g_object_unref (stream_filter);
406 g_object_unref (windows_filter);
410 signature_status_to_string (GMimeSignatureStatus status)
412 if (g_mime_signature_status_bad (status))
415 if (g_mime_signature_status_error (status))
418 if (g_mime_signature_status_good (status))
424 /* Print signature flags */
425 struct key_map_struct {
426 GMimeSignatureStatus bit;
431 do_format_signature_errors (sprinter_t *sp, struct key_map_struct *key_map,
432 unsigned int array_map_len, GMimeSignatureStatus errors)
434 sp->map_key (sp, "errors");
437 for (unsigned int i = 0; i < array_map_len; i++) {
438 if (errors & key_map[i].bit) {
439 sp->map_key (sp, key_map[i].string);
440 sp->boolean (sp, true);
448 format_signature_errors (sprinter_t *sp, GMimeSignature *signature)
450 GMimeSignatureStatus errors = g_mime_signature_get_status (signature);
452 if (! (errors & GMIME_SIGNATURE_STATUS_ERROR_MASK))
455 struct key_map_struct key_map[] = {
456 { GMIME_SIGNATURE_STATUS_KEY_REVOKED, "key-revoked" },
457 { GMIME_SIGNATURE_STATUS_KEY_EXPIRED, "key-expired" },
458 { GMIME_SIGNATURE_STATUS_SIG_EXPIRED, "sig-expired" },
459 { GMIME_SIGNATURE_STATUS_KEY_MISSING, "key-missing" },
460 { GMIME_SIGNATURE_STATUS_CRL_MISSING, "crl-missing" },
461 { GMIME_SIGNATURE_STATUS_CRL_TOO_OLD, "crl-too-old" },
462 { GMIME_SIGNATURE_STATUS_BAD_POLICY, "bad-policy" },
463 { GMIME_SIGNATURE_STATUS_SYS_ERROR, "sys-error" },
464 { GMIME_SIGNATURE_STATUS_TOFU_CONFLICT, "tofu-conflict" },
467 do_format_signature_errors (sp, key_map, ARRAY_SIZE (key_map), errors);
470 /* Signature status sprinter */
472 format_part_sigstatus_sprinter (sprinter_t *sp, GMimeSignatureList *siglist)
474 /* Any changes to the JSON or S-Expression format should be
475 * reflected in the file devel/schemata. */
486 for (i = 0; i < g_mime_signature_list_length (siglist); i++) {
487 GMimeSignature *signature = g_mime_signature_list_get_signature (siglist, i);
492 GMimeSignatureStatus status = g_mime_signature_get_status (signature);
493 sp->map_key (sp, "status");
494 sp->string (sp, signature_status_to_string (status));
496 GMimeCertificate *certificate = g_mime_signature_get_certificate (signature);
497 if (g_mime_signature_status_good (status)) {
499 sp->map_key (sp, "fingerprint");
500 sp->string (sp, g_mime_certificate_get_fingerprint (certificate));
502 /* these dates are seconds since the epoch; should we
503 * provide a more human-readable format string? */
504 time_t created = g_mime_signature_get_created (signature);
506 sp->map_key (sp, "created");
507 sp->integer (sp, created);
509 time_t expires = g_mime_signature_get_expires (signature);
511 sp->map_key (sp, "expires");
512 sp->integer (sp, expires);
515 const char *uid = g_mime_certificate_get_valid_userid (certificate);
517 sp->map_key (sp, "userid");
518 sp->string (sp, uid);
520 const char *email = g_mime_certificate_get_valid_email (certificate);
522 sp->map_key (sp, "email");
523 sp->string (sp, email);
526 } else if (certificate) {
527 const char *key_id = g_mime_certificate_get_fpr16 (certificate);
529 sp->map_key (sp, "keyid");
530 sp->string (sp, key_id);
534 if (notmuch_format_version <= 3) {
535 GMimeSignatureStatus errors = g_mime_signature_get_status (signature);
536 if (g_mime_signature_status_error (errors)) {
537 sp->map_key (sp, "errors");
538 sp->integer (sp, errors);
541 format_signature_errors (sp, signature);
550 static notmuch_status_t
551 format_part_text (const void *ctx, sprinter_t *sp, mime_node_t *node,
552 int indent, const notmuch_show_params_t *params)
554 /* The disposition and content-type metadata are associated with
555 * the envelope for message parts */
556 GMimeObject *meta = node->envelope_part ? (
557 GMIME_OBJECT (node->envelope_part) ) : node->part;
558 GMimeContentType *content_type = g_mime_object_get_content_type (meta);
559 const bool leaf = GMIME_IS_PART (node->part);
560 GMimeStream *stream = params->out_stream;
561 const char *part_type;
564 if (node->envelope_file) {
565 notmuch_message_t *message = node->envelope_file;
567 part_type = "message";
568 g_mime_stream_printf (stream, "\f%s{ id:%s depth:%d match:%d excluded:%d filename:%s\n",
570 notmuch_message_get_message_id (message),
572 _get_message_flag (message, NOTMUCH_MESSAGE_FLAG_MATCH) ? 1 : 0,
573 _get_message_flag (message, NOTMUCH_MESSAGE_FLAG_EXCLUDED) ? 1 : 0,
574 notmuch_message_get_filename (message));
576 char *content_string;
577 const char *disposition = _get_disposition (meta);
578 const char *cid = g_mime_object_get_content_id (meta);
579 const char *filename = leaf ? (
580 g_mime_part_get_filename (GMIME_PART (node->part)) ) : NULL;
583 strcasecmp (disposition, GMIME_DISPOSITION_ATTACHMENT) == 0)
584 part_type = "attachment";
588 g_mime_stream_printf (stream, "\f%s{ ID: %d", part_type, node->part_num);
590 g_mime_stream_printf (stream, ", Filename: %s", filename);
592 g_mime_stream_printf (stream, ", Content-id: %s", cid);
594 content_string = g_mime_content_type_get_mime_type (content_type);
595 g_mime_stream_printf (stream, ", Content-type: %s\n", content_string);
596 g_free (content_string);
599 if (GMIME_IS_MESSAGE (node->part)) {
600 GMimeMessage *message = GMIME_MESSAGE (node->part);
601 char *recipients_string;
604 g_mime_stream_printf (stream, "\fheader{\n");
605 if (node->envelope_file)
606 g_mime_stream_printf (stream, "%s\n", _get_one_line_summary (ctx, node->envelope_file));
607 g_mime_stream_printf (stream, "Subject: %s\n", g_mime_message_get_subject (message));
608 g_mime_stream_printf (stream, "From: %s\n", g_mime_message_get_from_string (message));
609 recipients_string = g_mime_message_get_address_string (message, GMIME_ADDRESS_TYPE_TO);
610 if (recipients_string)
611 g_mime_stream_printf (stream, "To: %s\n", recipients_string);
612 g_free (recipients_string);
613 recipients_string = g_mime_message_get_address_string (message, GMIME_ADDRESS_TYPE_CC);
614 if (recipients_string)
615 g_mime_stream_printf (stream, "Cc: %s\n", recipients_string);
616 g_free (recipients_string);
617 date_string = g_mime_message_get_date_string (node, message);
618 g_mime_stream_printf (stream, "Date: %s\n", date_string);
619 g_mime_stream_printf (stream, "\fheader}\n");
621 if (! params->output_body) {
622 g_mime_stream_printf (stream, "\f%s}\n", part_type);
623 return NOTMUCH_STATUS_SUCCESS;
625 g_mime_stream_printf (stream, "\fbody{\n");
629 if (g_mime_content_type_is_type (content_type, "text", "*") &&
630 (params->include_html ||
631 ! g_mime_content_type_is_type (content_type, "text", "html"))) {
632 show_text_part_content (node->part, stream, 0);
634 char *content_string = g_mime_content_type_get_mime_type (content_type);
635 g_mime_stream_printf (stream, "Non-text part: %s\n", content_string);
636 g_free (content_string);
640 for (i = 0; i < node->nchildren; i++)
641 format_part_text (ctx, sp, mime_node_child (node, i), indent, params);
643 if (GMIME_IS_MESSAGE (node->part))
644 g_mime_stream_printf (stream, "\fbody}\n");
646 g_mime_stream_printf (stream, "\f%s}\n", part_type);
648 return NOTMUCH_STATUS_SUCCESS;
652 format_omitted_part_meta_sprinter (sprinter_t *sp, GMimeObject *meta, GMimePart *part)
654 const char *content_charset = g_mime_object_get_content_type_parameter (meta, "charset");
655 const char *cte = g_mime_object_get_header (meta, "content-transfer-encoding");
656 GMimeDataWrapper *wrapper = g_mime_part_get_content (part);
657 GMimeStream *stream = g_mime_data_wrapper_get_stream (wrapper);
658 ssize_t content_length = g_mime_stream_length (stream);
660 if (content_charset != NULL) {
661 sp->map_key (sp, "content-charset");
662 sp->string (sp, content_charset);
665 sp->map_key (sp, "content-transfer-encoding");
666 sp->string (sp, cte);
668 if (content_length >= 0) {
669 sp->map_key (sp, "content-length");
670 sp->integer (sp, content_length);
675 format_part_sprinter (const void *ctx, sprinter_t *sp, mime_node_t *node,
679 /* Any changes to the JSON or S-Expression format should be
680 * reflected in the file devel/schemata. */
682 if (node->envelope_file) {
683 const _notmuch_message_crypto_t *msg_crypto = NULL;
685 format_message_sprinter (sp, node->envelope_file);
688 sp->map_key (sp, "body");
690 format_part_sprinter (ctx, sp, mime_node_child (node, 0), true, include_html);
694 msg_crypto = mime_node_get_message_crypto_status (node);
695 if (notmuch_format_version >= 4) {
696 sp->map_key (sp, "crypto");
698 if (msg_crypto->sig_list ||
699 msg_crypto->decryption_status != NOTMUCH_MESSAGE_DECRYPTED_NONE) {
700 if (msg_crypto->sig_list) {
701 sp->map_key (sp, "signed");
703 sp->map_key (sp, "status");
704 format_part_sigstatus_sprinter (sp, msg_crypto->sig_list);
705 if (msg_crypto->signature_encrypted) {
706 sp->map_key (sp, "encrypted");
707 sp->boolean (sp, msg_crypto->signature_encrypted);
709 if (msg_crypto->payload_subject) {
710 sp->map_key (sp, "headers");
712 sp->string (sp, "Subject");
717 if (msg_crypto->decryption_status != NOTMUCH_MESSAGE_DECRYPTED_NONE) {
718 sp->map_key (sp, "decrypted");
720 sp->map_key (sp, "status");
721 sp->string (sp, msg_crypto->decryption_status == NOTMUCH_MESSAGE_DECRYPTED_FULL ?
724 if (msg_crypto->payload_subject) {
725 const char *subject = g_mime_message_get_subject GMIME_MESSAGE (node->part);
726 if (subject == NULL || strcmp (subject, msg_crypto->payload_subject)) {
727 /* protected subject differs from the external header */
728 sp->map_key (sp, "header-mask");
730 sp->map_key (sp, "Subject");
734 sp->string (sp, subject);
744 sp->map_key (sp, "headers");
745 format_headers_sprinter (sp, GMIME_MESSAGE (node->part), false, msg_crypto);
751 /* The disposition and content-type metadata are associated with
752 * the envelope for message parts */
753 GMimeObject *meta = node->envelope_part ? (
754 GMIME_OBJECT (node->envelope_part) ) : node->part;
755 GMimeContentType *content_type = g_mime_object_get_content_type (meta);
756 char *content_string;
757 const char *disposition = _get_disposition (meta);
758 const char *cid = g_mime_object_get_content_id (meta);
759 const char *filename = GMIME_IS_PART (node->part) ? (
760 g_mime_part_get_filename (GMIME_PART (node->part) ) ) : NULL;
766 sp->map_key (sp, "id");
767 sp->integer (sp, node->part_num);
769 if (node->decrypt_attempted) {
770 sp->map_key (sp, "encstatus");
773 sp->map_key (sp, "status");
774 sp->string (sp, node->decrypt_success ? "good" : "bad");
779 if (node->verify_attempted) {
780 sp->map_key (sp, "sigstatus");
781 format_part_sigstatus_sprinter (sp, node->sig_list);
784 sp->map_key (sp, "content-type");
785 content_string = g_mime_content_type_get_mime_type (content_type);
786 sp->string (sp, content_string);
787 g_free (content_string);
790 sp->map_key (sp, "content-disposition");
791 sp->string (sp, disposition);
795 sp->map_key (sp, "content-id");
796 sp->string (sp, cid);
800 sp->map_key (sp, "filename");
801 sp->string (sp, filename);
804 if (GMIME_IS_PART (node->part)) {
805 /* For non-HTML text parts, we include the content in the
806 * JSON. Since JSON must be Unicode, we handle charset
807 * decoding here and do not report a charset to the caller.
808 * For text/html parts, we do not include the content unless
809 * the --include-html option has been passed. If a html part
810 * is not included, it can be requested directly. This makes
811 * charset decoding the responsibility on the caller so we
812 * report the charset for text/html parts.
814 if (g_mime_content_type_is_type (content_type, "text", "*") &&
816 ! g_mime_content_type_is_type (content_type, "text", "html"))) {
817 GMimeStream *stream_memory = g_mime_stream_mem_new ();
818 GByteArray *part_content;
819 show_text_part_content (node->part, stream_memory, 0);
820 part_content = g_mime_stream_mem_get_byte_array (GMIME_STREAM_MEM (stream_memory));
821 sp->map_key (sp, "content");
822 sp->string_len (sp, (char *) part_content->data, part_content->len);
823 g_object_unref (stream_memory);
825 /* if we have a child part despite being a standard
826 * (non-multipart) MIME part, that means there is
827 * something to unwrap, which we will present in
829 if (node->nchildren) {
830 sp->map_key (sp, "content");
834 format_omitted_part_meta_sprinter (sp, meta, GMIME_PART (node->part));
836 } else if (GMIME_IS_MULTIPART (node->part)) {
837 sp->map_key (sp, "content");
840 } else if (GMIME_IS_MESSAGE (node->part)) {
841 sp->map_key (sp, "content");
845 sp->map_key (sp, "headers");
846 format_headers_sprinter (sp, GMIME_MESSAGE (node->part), false, NULL);
848 sp->map_key (sp, "body");
853 for (i = 0; i < node->nchildren; i++)
854 format_part_sprinter (ctx, sp, mime_node_child (node, i), true, include_html);
856 /* Close content structures */
857 for (i = 0; i < nclose; i++)
863 static notmuch_status_t
864 format_part_sprinter_entry (const void *ctx, sprinter_t *sp,
865 mime_node_t *node, unused (int indent),
866 const notmuch_show_params_t *params)
868 format_part_sprinter (ctx, sp, node, params->output_body, params->include_html);
870 return NOTMUCH_STATUS_SUCCESS;
873 /* Print a message in "mboxrd" format as documented, for example,
876 * http://qmail.org/qmail-manual-html/man5/mbox.html
878 static notmuch_status_t
879 format_part_mbox (const void *ctx, unused (sprinter_t *sp), mime_node_t *node,
881 unused (const notmuch_show_params_t *params))
883 notmuch_message_t *message = node->envelope_file;
885 const char *filename;
890 struct tm date_gmtime;
891 char date_asctime[26];
898 INTERNAL_ERROR ("format_part_mbox requires a root part");
900 filename = notmuch_message_get_filename (message);
901 file = gzopen (filename, "r");
903 fprintf (stderr, "Failed to open %s: %s\n",
904 filename, strerror (errno));
905 return NOTMUCH_STATUS_FILE_ERROR;
908 from = notmuch_message_get_header (message, "from");
909 from = _extract_email_address (ctx, from);
911 date = notmuch_message_get_date (message);
912 gmtime_r (&date, &date_gmtime);
913 asctime_r (&date_gmtime, date_asctime);
915 printf ("From %s %s", from, date_asctime);
917 while ((line_len = gz_getline (message, &line, &line_size, file)) != UTIL_EOF ) {
918 if (_is_from_line (line))
927 return NOTMUCH_STATUS_SUCCESS;
930 static notmuch_status_t
931 format_part_raw (unused (const void *ctx), unused (sprinter_t *sp),
932 mime_node_t *node, unused (int indent),
933 const notmuch_show_params_t *params)
935 if (node->envelope_file) {
936 /* Special case the entire message to avoid MIME parsing. */
937 const char *filename;
938 GMimeStream *stream = NULL;
941 notmuch_status_t ret = NOTMUCH_STATUS_FILE_ERROR;
943 filename = _get_filename (node->envelope_file, params->duplicate);
944 if (filename == NULL) {
945 fprintf (stderr, "Error: Cannot get message filename.\n");
949 stream = g_mime_stream_gzfile_open (filename);
950 if (stream == NULL) {
951 fprintf (stderr, "Error: Cannot open file %s: %s\n", filename, strerror (errno));
955 while (! g_mime_stream_eos (stream)) {
956 ssize = g_mime_stream_read (stream, buf, sizeof (buf));
958 fprintf (stderr, "Error: Read failed from %s\n", filename);
962 if (ssize > 0 && fwrite (buf, ssize, 1, stdout) != 1) {
963 fprintf (stderr, "Error: Write %zd chars to stdout failed\n", ssize);
968 ret = NOTMUCH_STATUS_SUCCESS;
970 /* XXX This DONE is just for the special case of a node in a single file */
973 g_object_unref (stream);
978 GMimeStream *stream_filter = g_mime_stream_filter_new (params->out_stream);
980 if (GMIME_IS_PART (node->part)) {
981 /* For leaf parts, we emit only the transfer-decoded
983 GMimeDataWrapper *wrapper;
984 wrapper = g_mime_part_get_content (GMIME_PART (node->part));
986 if (wrapper && stream_filter)
987 g_mime_data_wrapper_write_to_stream (wrapper, stream_filter);
989 /* Write out the whole part. For message parts (the root
990 * part and embedded message parts), this will be the
991 * message including its headers (but not the
992 * encapsulating part's headers). For multipart parts,
993 * this will include the headers. */
995 g_mime_object_write_to_stream (node->part, NULL, stream_filter);
999 g_object_unref (stream_filter);
1001 return NOTMUCH_STATUS_SUCCESS;
1004 static notmuch_status_t
1005 show_message (void *ctx,
1006 const notmuch_show_format_t *format,
1008 notmuch_message_t *message,
1010 notmuch_show_params_t *params)
1012 void *local = talloc_new (ctx);
1013 mime_node_t *root, *part;
1014 notmuch_status_t status;
1015 unsigned int session_keys = 0;
1016 notmuch_status_t session_key_count_error = NOTMUCH_STATUS_SUCCESS;
1018 if (params->crypto.decrypt == NOTMUCH_DECRYPT_TRUE)
1019 session_key_count_error = notmuch_message_count_properties (message, "session-key",
1022 status = mime_node_open (local, message, &(params->crypto), &root);
1025 part = mime_node_seek_dfs (root, (params->part < 0 ? 0 : params->part));
1027 status = format->part (local, sp, part, indent, params);
1028 if (params->crypto.decrypt == NOTMUCH_DECRYPT_TRUE && session_key_count_error ==
1029 NOTMUCH_STATUS_SUCCESS) {
1030 unsigned int new_session_keys = 0;
1031 if (notmuch_message_count_properties (message, "session-key", &new_session_keys) ==
1032 NOTMUCH_STATUS_SUCCESS &&
1033 new_session_keys > session_keys) {
1034 /* try a quiet re-indexing */
1035 notmuch_indexopts_t *indexopts = notmuch_database_get_default_indexopts (
1036 notmuch_message_get_database (message));
1038 notmuch_indexopts_set_decrypt_policy (indexopts, NOTMUCH_DECRYPT_AUTO);
1039 print_status_message ("Error re-indexing message with --decrypt=stash",
1040 message, notmuch_message_reindex (message, indexopts));
1045 talloc_free (local);
1049 static notmuch_status_t
1050 show_messages (void *ctx,
1051 const notmuch_show_format_t *format,
1053 notmuch_messages_t *messages,
1055 notmuch_show_params_t *params)
1057 notmuch_message_t *message;
1061 notmuch_status_t status, res = NOTMUCH_STATUS_SUCCESS;
1063 sp->begin_list (sp);
1066 notmuch_messages_valid (messages);
1067 notmuch_messages_move_to_next (messages)) {
1068 sp->begin_list (sp);
1070 message = notmuch_messages_get (messages);
1072 match = _get_message_flag (message, NOTMUCH_MESSAGE_FLAG_MATCH);
1073 excluded = _get_message_flag (message, NOTMUCH_MESSAGE_FLAG_EXCLUDED);
1075 next_indent = indent;
1077 if ((match && (! excluded || ! params->omit_excluded)) || params->entire_thread) {
1078 status = show_message (ctx, format, sp, message, indent, params);
1079 if (status && ! res)
1081 next_indent = indent + 1;
1086 status = show_messages (ctx,
1088 notmuch_message_get_replies (message),
1091 if (status && ! res)
1094 notmuch_message_destroy (message);
1104 /* Formatted output of single message */
1106 do_show_single (void *ctx,
1107 notmuch_query_t *query,
1108 const notmuch_show_format_t *format,
1110 notmuch_show_params_t *params)
1112 notmuch_messages_t *messages;
1113 notmuch_message_t *message;
1114 notmuch_status_t status;
1117 status = notmuch_query_count_messages (query, &count);
1118 if (print_status_query ("notmuch show", query, status))
1123 "Error: search term did not match precisely one message (matched %u messages).\n",
1128 status = notmuch_query_search_messages (query, &messages);
1129 if (print_status_query ("notmuch show", query, status))
1132 message = notmuch_messages_get (messages);
1134 if (message == NULL) {
1135 fprintf (stderr, "Error: Cannot find matching message.\n");
1139 notmuch_message_set_flag (message, NOTMUCH_MESSAGE_FLAG_MATCH, 1);
1141 return show_message (ctx, format, sp, message, 0, params)
1142 != NOTMUCH_STATUS_SUCCESS;
1145 /* Formatted output of threads */
1147 do_show_threaded (void *ctx,
1148 notmuch_query_t *query,
1149 const notmuch_show_format_t *format,
1151 notmuch_show_params_t *params)
1153 notmuch_threads_t *threads;
1154 notmuch_thread_t *thread;
1155 notmuch_messages_t *messages;
1156 notmuch_status_t status, res = NOTMUCH_STATUS_SUCCESS;
1158 status = notmuch_query_search_threads (query, &threads);
1159 if (print_status_query ("notmuch show", query, status))
1162 sp->begin_list (sp);
1165 notmuch_threads_valid (threads);
1166 notmuch_threads_move_to_next (threads)) {
1167 thread = notmuch_threads_get (threads);
1169 messages = notmuch_thread_get_toplevel_messages (thread);
1171 if (messages == NULL)
1172 INTERNAL_ERROR ("Thread %s has no toplevel messages.\n",
1173 notmuch_thread_get_thread_id (thread));
1175 status = show_messages (ctx, format, sp, messages, 0, params);
1176 if (status && ! res)
1179 notmuch_thread_destroy (thread);
1185 return res != NOTMUCH_STATUS_SUCCESS;
1189 do_show_unthreaded (void *ctx,
1190 notmuch_query_t *query,
1191 const notmuch_show_format_t *format,
1193 notmuch_show_params_t *params)
1195 notmuch_messages_t *messages;
1196 notmuch_message_t *message;
1197 notmuch_status_t status, res = NOTMUCH_STATUS_SUCCESS;
1198 notmuch_bool_t excluded;
1200 status = notmuch_query_search_messages (query, &messages);
1201 if (print_status_query ("notmuch show", query, status))
1204 sp->begin_list (sp);
1207 notmuch_messages_valid (messages);
1208 notmuch_messages_move_to_next (messages)) {
1209 sp->begin_list (sp);
1210 sp->begin_list (sp);
1212 message = notmuch_messages_get (messages);
1214 notmuch_message_set_flag (message, NOTMUCH_MESSAGE_FLAG_MATCH, TRUE);
1215 excluded = _get_message_flag (message, NOTMUCH_MESSAGE_FLAG_EXCLUDED);
1217 if (! excluded || ! params->omit_excluded) {
1218 status = show_message (ctx, format, sp, message, 0, params);
1219 if (status && ! res)
1224 notmuch_message_destroy (message);
1233 NOTMUCH_FORMAT_NOT_SPECIFIED,
1234 NOTMUCH_FORMAT_JSON,
1235 NOTMUCH_FORMAT_SEXP,
1236 NOTMUCH_FORMAT_TEXT,
1237 NOTMUCH_FORMAT_MBOX,
1241 static const notmuch_show_format_t format_json = {
1242 .new_sprinter = sprinter_json_create,
1243 .part = format_part_sprinter_entry,
1246 static const notmuch_show_format_t format_sexp = {
1247 .new_sprinter = sprinter_sexp_create,
1248 .part = format_part_sprinter_entry,
1251 static const notmuch_show_format_t format_text = {
1252 .new_sprinter = sprinter_text_create,
1253 .part = format_part_text,
1256 static const notmuch_show_format_t format_mbox = {
1257 .new_sprinter = sprinter_text_create,
1258 .part = format_part_mbox,
1261 static const notmuch_show_format_t format_raw = {
1262 .new_sprinter = sprinter_text_create,
1263 .part = format_part_raw,
1266 static const notmuch_show_format_t *formatters[] = {
1267 [NOTMUCH_FORMAT_JSON] = &format_json,
1268 [NOTMUCH_FORMAT_SEXP] = &format_sexp,
1269 [NOTMUCH_FORMAT_TEXT] = &format_text,
1270 [NOTMUCH_FORMAT_MBOX] = &format_mbox,
1271 [NOTMUCH_FORMAT_RAW] = &format_raw,
1275 notmuch_show_command (notmuch_database_t *notmuch, int argc, char *argv[])
1277 notmuch_query_t *query;
1280 const notmuch_show_format_t *formatter;
1281 sprinter_t *sprinter;
1282 notmuch_show_params_t params = {
1285 .omit_excluded = true,
1286 .output_body = true,
1287 .crypto = { .decrypt = NOTMUCH_DECRYPT_AUTO },
1289 int format = NOTMUCH_FORMAT_NOT_SPECIFIED;
1290 bool exclude = true;
1291 bool entire_thread_set = false;
1292 bool single_message;
1293 bool unthreaded = FALSE;
1294 notmuch_status_t status;
1295 int sort = NOTMUCH_SORT_NEWEST_FIRST;
1297 notmuch_opt_desc_t options[] = {
1298 { .opt_keyword = &sort, .name = "sort", .keywords =
1299 (notmuch_keyword_t []){ { "oldest-first", NOTMUCH_SORT_OLDEST_FIRST },
1300 { "newest-first", NOTMUCH_SORT_NEWEST_FIRST },
1302 { .opt_keyword = &format, .name = "format", .keywords =
1303 (notmuch_keyword_t []){ { "json", NOTMUCH_FORMAT_JSON },
1304 { "text", NOTMUCH_FORMAT_TEXT },
1305 { "sexp", NOTMUCH_FORMAT_SEXP },
1306 { "mbox", NOTMUCH_FORMAT_MBOX },
1307 { "raw", NOTMUCH_FORMAT_RAW },
1309 { .opt_int = ¬much_format_version, .name = "format-version" },
1310 { .opt_bool = &exclude, .name = "exclude" },
1311 { .opt_bool = ¶ms.entire_thread, .name = "entire-thread",
1312 .present = &entire_thread_set },
1313 { .opt_bool = &unthreaded, .name = "unthreaded" },
1314 { .opt_int = ¶ms.part, .name = "part" },
1315 { .opt_keyword = (int *) (¶ms.crypto.decrypt), .name = "decrypt",
1316 .keyword_no_arg_value = "true", .keywords =
1317 (notmuch_keyword_t []){ { "false", NOTMUCH_DECRYPT_FALSE },
1318 { "auto", NOTMUCH_DECRYPT_AUTO },
1319 { "true", NOTMUCH_DECRYPT_NOSTASH },
1320 { "stash", NOTMUCH_DECRYPT_TRUE },
1322 { .opt_bool = ¶ms.crypto.verify, .name = "verify" },
1323 { .opt_bool = ¶ms.output_body, .name = "body" },
1324 { .opt_bool = ¶ms.include_html, .name = "include-html" },
1325 { .opt_int = ¶ms.duplicate, .name = "duplicate" },
1326 { .opt_inherit = notmuch_shared_options },
1330 opt_index = parse_arguments (argc, argv, options, 1);
1332 return EXIT_FAILURE;
1334 notmuch_process_shared_options (notmuch, argv[0]);
1336 /* explicit decryption implies verification */
1337 if (params.crypto.decrypt == NOTMUCH_DECRYPT_NOSTASH ||
1338 params.crypto.decrypt == NOTMUCH_DECRYPT_TRUE)
1339 params.crypto.verify = true;
1341 /* specifying a part implies single message display */
1342 single_message = params.part >= 0;
1344 /* specifying a duplicate also implies single message display */
1345 single_message = single_message || (params.duplicate > 0);
1347 if (format == NOTMUCH_FORMAT_NOT_SPECIFIED) {
1348 /* if part was requested and format was not specified, use format=raw */
1349 if (params.part >= 0)
1350 format = NOTMUCH_FORMAT_RAW;
1352 format = NOTMUCH_FORMAT_TEXT;
1355 if (format == NOTMUCH_FORMAT_MBOX) {
1356 if (params.part > 0) {
1357 fprintf (stderr, "Error: specifying parts is incompatible with mbox output format.\n");
1358 return EXIT_FAILURE;
1360 } else if (format == NOTMUCH_FORMAT_RAW) {
1361 /* raw format only supports single message display */
1362 single_message = true;
1365 notmuch_exit_if_unsupported_format ();
1367 /* Default is entire-thread = false except for format=json and
1369 if (! entire_thread_set &&
1370 (format == NOTMUCH_FORMAT_JSON || format == NOTMUCH_FORMAT_SEXP))
1371 params.entire_thread = true;
1373 if (! params.output_body) {
1374 if (params.part > 0) {
1375 fprintf (stderr, "Warning: --body=false is incompatible with --part > 0. Disabling.\n");
1376 params.output_body = true;
1378 if (format != NOTMUCH_FORMAT_TEXT &&
1379 format != NOTMUCH_FORMAT_JSON &&
1380 format != NOTMUCH_FORMAT_SEXP)
1382 "Warning: --body=false only implemented for format=text, format=json and format=sexp\n");
1386 if (params.include_html &&
1387 (format != NOTMUCH_FORMAT_TEXT &&
1388 format != NOTMUCH_FORMAT_JSON &&
1389 format != NOTMUCH_FORMAT_SEXP)) {
1391 "Warning: --include-html only implemented for format=text, format=json and format=sexp\n");
1394 if (params.crypto.decrypt == NOTMUCH_DECRYPT_TRUE) {
1395 status = notmuch_database_reopen (notmuch, NOTMUCH_DATABASE_MODE_READ_WRITE);
1397 fprintf (stderr, "Error reopening database for READ_WRITE: %s\n",
1398 notmuch_status_to_string (status));
1399 return EXIT_FAILURE;
1403 query_string = query_string_from_args (notmuch, argc - opt_index, argv + opt_index);
1404 if (query_string == NULL) {
1405 fprintf (stderr, "Out of memory\n");
1406 return EXIT_FAILURE;
1409 if (*query_string == '\0') {
1410 fprintf (stderr, "Error: notmuch show requires at least one search term.\n");
1411 return EXIT_FAILURE;
1414 status = notmuch_query_create_with_syntax (notmuch, query_string,
1415 shared_option_query_syntax (),
1417 if (print_status_database ("notmuch show", notmuch, status))
1418 return EXIT_FAILURE;
1420 notmuch_query_set_sort (query, sort);
1422 /* Create structure printer. */
1423 formatter = formatters[format];
1424 sprinter = formatter->new_sprinter (notmuch, stdout);
1426 params.out_stream = g_mime_stream_stdout_new ();
1428 /* If a single message is requested we do not use search_excludes. */
1429 if (single_message) {
1430 ret = do_show_single (notmuch, query, formatter, sprinter, ¶ms);
1432 /* We always apply set the exclude flag. The
1433 * exclude=true|false option controls whether or not we return
1434 * threads that only match in an excluded message */
1435 notmuch_config_values_t *exclude_tags;
1436 notmuch_status_t status;
1438 for (exclude_tags = notmuch_config_get_values (notmuch, NOTMUCH_CONFIG_EXCLUDE_TAGS);
1439 notmuch_config_values_valid (exclude_tags);
1440 notmuch_config_values_move_to_next (exclude_tags)) {
1442 status = notmuch_query_add_tag_exclude (query,
1443 notmuch_config_values_get (exclude_tags));
1444 if (status && status != NOTMUCH_STATUS_IGNORED) {
1445 print_status_query ("notmuch show", query, status);
1451 if (exclude == false) {
1452 notmuch_query_set_omit_excluded (query, false);
1453 params.omit_excluded = false;
1457 ret = do_show_unthreaded (notmuch, query, formatter, sprinter, ¶ms);
1459 ret = do_show_threaded (notmuch, query, formatter, sprinter, ¶ms);
1463 g_mime_stream_flush (params.out_stream);
1464 g_object_unref (params.out_stream);
1466 _notmuch_crypto_cleanup (¶ms.crypto);
1467 notmuch_query_destroy (query);
1468 notmuch_database_destroy (notmuch);
1470 return ret ? EXIT_FAILURE : EXIT_SUCCESS;