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,
680 /* Any changes to the JSON or S-Expression format should be
681 * reflected in the file devel/schemata. */
683 if (node->envelope_file) {
684 const _notmuch_message_crypto_t *msg_crypto = NULL;
686 format_message_sprinter (sp, node->envelope_file);
688 sp->map_key (sp, "duplicate");
689 sp->integer (sp, duplicate > 0 ? duplicate : 1);
692 sp->map_key (sp, "body");
694 format_part_sprinter (ctx, sp, mime_node_child (node, 0), -1, true, include_html);
698 msg_crypto = mime_node_get_message_crypto_status (node);
699 if (notmuch_format_version >= 4) {
700 sp->map_key (sp, "crypto");
702 if (msg_crypto->sig_list ||
703 msg_crypto->decryption_status != NOTMUCH_MESSAGE_DECRYPTED_NONE) {
704 if (msg_crypto->sig_list) {
705 sp->map_key (sp, "signed");
707 sp->map_key (sp, "status");
708 format_part_sigstatus_sprinter (sp, msg_crypto->sig_list);
709 if (msg_crypto->signature_encrypted) {
710 sp->map_key (sp, "encrypted");
711 sp->boolean (sp, msg_crypto->signature_encrypted);
713 if (msg_crypto->payload_subject) {
714 sp->map_key (sp, "headers");
716 sp->string (sp, "Subject");
721 if (msg_crypto->decryption_status != NOTMUCH_MESSAGE_DECRYPTED_NONE) {
722 sp->map_key (sp, "decrypted");
724 sp->map_key (sp, "status");
725 sp->string (sp, msg_crypto->decryption_status == NOTMUCH_MESSAGE_DECRYPTED_FULL ?
728 if (msg_crypto->payload_subject) {
729 const char *subject = g_mime_message_get_subject GMIME_MESSAGE (node->part);
730 if (subject == NULL || strcmp (subject, msg_crypto->payload_subject)) {
731 /* protected subject differs from the external header */
732 sp->map_key (sp, "header-mask");
734 sp->map_key (sp, "Subject");
738 sp->string (sp, subject);
748 sp->map_key (sp, "headers");
749 format_headers_sprinter (sp, GMIME_MESSAGE (node->part), false, msg_crypto);
755 /* The disposition and content-type metadata are associated with
756 * the envelope for message parts */
757 GMimeObject *meta = node->envelope_part ? (
758 GMIME_OBJECT (node->envelope_part) ) : node->part;
759 GMimeContentType *content_type = g_mime_object_get_content_type (meta);
760 char *content_string;
761 const char *disposition = _get_disposition (meta);
762 const char *cid = g_mime_object_get_content_id (meta);
763 const char *filename = GMIME_IS_PART (node->part) ? (
764 g_mime_part_get_filename (GMIME_PART (node->part) ) ) : NULL;
770 sp->map_key (sp, "id");
771 sp->integer (sp, node->part_num);
773 if (node->decrypt_attempted) {
774 sp->map_key (sp, "encstatus");
777 sp->map_key (sp, "status");
778 sp->string (sp, node->decrypt_success ? "good" : "bad");
783 if (node->verify_attempted) {
784 sp->map_key (sp, "sigstatus");
785 format_part_sigstatus_sprinter (sp, node->sig_list);
788 sp->map_key (sp, "content-type");
789 content_string = g_mime_content_type_get_mime_type (content_type);
790 sp->string (sp, content_string);
791 g_free (content_string);
794 sp->map_key (sp, "content-disposition");
795 sp->string (sp, disposition);
799 sp->map_key (sp, "content-id");
800 sp->string (sp, cid);
804 sp->map_key (sp, "filename");
805 sp->string (sp, filename);
808 if (GMIME_IS_PART (node->part)) {
809 /* For non-HTML text parts, we include the content in the
810 * JSON. Since JSON must be Unicode, we handle charset
811 * decoding here and do not report a charset to the caller.
812 * For text/html parts, we do not include the content unless
813 * the --include-html option has been passed. If a html part
814 * is not included, it can be requested directly. This makes
815 * charset decoding the responsibility on the caller so we
816 * report the charset for text/html parts.
818 if (g_mime_content_type_is_type (content_type, "text", "*") &&
820 ! g_mime_content_type_is_type (content_type, "text", "html"))) {
821 GMimeStream *stream_memory = g_mime_stream_mem_new ();
822 GByteArray *part_content;
823 show_text_part_content (node->part, stream_memory, 0);
824 part_content = g_mime_stream_mem_get_byte_array (GMIME_STREAM_MEM (stream_memory));
825 sp->map_key (sp, "content");
826 sp->string_len (sp, (char *) part_content->data, part_content->len);
827 g_object_unref (stream_memory);
829 /* if we have a child part despite being a standard
830 * (non-multipart) MIME part, that means there is
831 * something to unwrap, which we will present in
833 if (node->nchildren) {
834 sp->map_key (sp, "content");
838 format_omitted_part_meta_sprinter (sp, meta, GMIME_PART (node->part));
840 } else if (GMIME_IS_MULTIPART (node->part)) {
841 sp->map_key (sp, "content");
844 } else if (GMIME_IS_MESSAGE (node->part)) {
845 sp->map_key (sp, "content");
849 sp->map_key (sp, "headers");
850 format_headers_sprinter (sp, GMIME_MESSAGE (node->part), false, NULL);
852 sp->map_key (sp, "body");
857 for (i = 0; i < node->nchildren; i++)
858 format_part_sprinter (ctx, sp, mime_node_child (node, i), -1, true, include_html);
860 /* Close content structures */
861 for (i = 0; i < nclose; i++)
867 static notmuch_status_t
868 format_part_sprinter_entry (const void *ctx, sprinter_t *sp,
869 mime_node_t *node, unused (int indent),
870 const notmuch_show_params_t *params)
872 format_part_sprinter (ctx, sp, node, params->duplicate, params->output_body,
873 params->include_html);
875 return NOTMUCH_STATUS_SUCCESS;
878 /* Print a message in "mboxrd" format as documented, for example,
881 * http://qmail.org/qmail-manual-html/man5/mbox.html
883 static notmuch_status_t
884 format_part_mbox (const void *ctx, unused (sprinter_t *sp), mime_node_t *node,
886 unused (const notmuch_show_params_t *params))
888 notmuch_message_t *message = node->envelope_file;
890 const char *filename;
895 struct tm date_gmtime;
896 char date_asctime[26];
903 INTERNAL_ERROR ("format_part_mbox requires a root part");
905 filename = notmuch_message_get_filename (message);
906 file = gzopen (filename, "r");
908 fprintf (stderr, "Failed to open %s: %s\n",
909 filename, strerror (errno));
910 return NOTMUCH_STATUS_FILE_ERROR;
913 from = notmuch_message_get_header (message, "from");
914 from = _extract_email_address (ctx, from);
916 date = notmuch_message_get_date (message);
917 gmtime_r (&date, &date_gmtime);
918 asctime_r (&date_gmtime, date_asctime);
920 printf ("From %s %s", from, date_asctime);
922 while ((line_len = gz_getline (message, &line, &line_size, file)) != UTIL_EOF ) {
923 if (_is_from_line (line))
932 return NOTMUCH_STATUS_SUCCESS;
935 static notmuch_status_t
936 format_part_raw (unused (const void *ctx), unused (sprinter_t *sp),
937 mime_node_t *node, unused (int indent),
938 const notmuch_show_params_t *params)
940 if (node->envelope_file) {
941 /* Special case the entire message to avoid MIME parsing. */
942 const char *filename;
943 GMimeStream *stream = NULL;
946 notmuch_status_t ret = NOTMUCH_STATUS_FILE_ERROR;
948 filename = _get_filename (node->envelope_file, params->duplicate);
949 if (filename == NULL) {
950 fprintf (stderr, "Error: Cannot get message filename.\n");
954 stream = g_mime_stream_gzfile_open (filename);
955 if (stream == NULL) {
956 fprintf (stderr, "Error: Cannot open file %s: %s\n", filename, strerror (errno));
960 while (! g_mime_stream_eos (stream)) {
961 ssize = g_mime_stream_read (stream, buf, sizeof (buf));
963 fprintf (stderr, "Error: Read failed from %s\n", filename);
967 if (ssize > 0 && fwrite (buf, ssize, 1, stdout) != 1) {
968 fprintf (stderr, "Error: Write %zd chars to stdout failed\n", ssize);
973 ret = NOTMUCH_STATUS_SUCCESS;
975 /* XXX This DONE is just for the special case of a node in a single file */
978 g_object_unref (stream);
983 GMimeStream *stream_filter = g_mime_stream_filter_new (params->out_stream);
985 if (GMIME_IS_PART (node->part)) {
986 /* For leaf parts, we emit only the transfer-decoded
988 GMimeDataWrapper *wrapper;
989 wrapper = g_mime_part_get_content (GMIME_PART (node->part));
991 if (wrapper && stream_filter)
992 g_mime_data_wrapper_write_to_stream (wrapper, stream_filter);
994 /* Write out the whole part. For message parts (the root
995 * part and embedded message parts), this will be the
996 * message including its headers (but not the
997 * encapsulating part's headers). For multipart parts,
998 * this will include the headers. */
1000 g_mime_object_write_to_stream (node->part, NULL, stream_filter);
1004 g_object_unref (stream_filter);
1006 return NOTMUCH_STATUS_SUCCESS;
1009 static notmuch_status_t
1010 show_message (void *ctx,
1011 const notmuch_show_format_t *format,
1013 notmuch_message_t *message,
1015 notmuch_show_params_t *params)
1017 void *local = talloc_new (ctx);
1018 mime_node_t *root, *part;
1019 notmuch_status_t status;
1020 unsigned int session_keys = 0;
1021 notmuch_status_t session_key_count_error = NOTMUCH_STATUS_SUCCESS;
1023 if (params->crypto.decrypt == NOTMUCH_DECRYPT_TRUE)
1024 session_key_count_error = notmuch_message_count_properties (message, "session-key",
1027 status = mime_node_open (local, message, params->duplicate, &(params->crypto), &root);
1030 part = mime_node_seek_dfs (root, (params->part < 0 ? 0 : params->part));
1032 status = format->part (local, sp, part, indent, params);
1033 if (params->crypto.decrypt == NOTMUCH_DECRYPT_TRUE && session_key_count_error ==
1034 NOTMUCH_STATUS_SUCCESS) {
1035 unsigned int new_session_keys = 0;
1036 if (notmuch_message_count_properties (message, "session-key", &new_session_keys) ==
1037 NOTMUCH_STATUS_SUCCESS &&
1038 new_session_keys > session_keys) {
1039 /* try a quiet re-indexing */
1040 notmuch_indexopts_t *indexopts = notmuch_database_get_default_indexopts (
1041 notmuch_message_get_database (message));
1043 notmuch_indexopts_set_decrypt_policy (indexopts, NOTMUCH_DECRYPT_AUTO);
1044 print_status_message ("Error re-indexing message with --decrypt=stash",
1045 message, notmuch_message_reindex (message, indexopts));
1050 talloc_free (local);
1054 static notmuch_status_t
1055 show_messages (void *ctx,
1056 const notmuch_show_format_t *format,
1058 notmuch_messages_t *messages,
1060 notmuch_show_params_t *params)
1062 notmuch_message_t *message;
1066 notmuch_status_t status, res = NOTMUCH_STATUS_SUCCESS;
1068 sp->begin_list (sp);
1071 notmuch_messages_valid (messages);
1072 notmuch_messages_move_to_next (messages)) {
1073 sp->begin_list (sp);
1075 message = notmuch_messages_get (messages);
1077 match = _get_message_flag (message, NOTMUCH_MESSAGE_FLAG_MATCH);
1078 excluded = _get_message_flag (message, NOTMUCH_MESSAGE_FLAG_EXCLUDED);
1080 next_indent = indent;
1082 if ((match && (! excluded || ! params->omit_excluded)) || params->entire_thread) {
1083 status = show_message (ctx, format, sp, message, indent, params);
1084 if (status && ! res)
1086 next_indent = indent + 1;
1091 status = show_messages (ctx,
1093 notmuch_message_get_replies (message),
1096 if (status && ! res)
1099 notmuch_message_destroy (message);
1109 /* Formatted output of single message */
1111 do_show_single (void *ctx,
1112 notmuch_query_t *query,
1113 const notmuch_show_format_t *format,
1115 notmuch_show_params_t *params)
1117 notmuch_messages_t *messages;
1118 notmuch_message_t *message;
1119 notmuch_status_t status;
1122 status = notmuch_query_count_messages (query, &count);
1123 if (print_status_query ("notmuch show", query, status))
1128 "Error: search term did not match precisely one message (matched %u messages).\n",
1133 status = notmuch_query_search_messages (query, &messages);
1134 if (print_status_query ("notmuch show", query, status))
1137 message = notmuch_messages_get (messages);
1139 if (message == NULL) {
1140 fprintf (stderr, "Error: Cannot find matching message.\n");
1144 notmuch_message_set_flag (message, NOTMUCH_MESSAGE_FLAG_MATCH, 1);
1146 return show_message (ctx, format, sp, message, 0, params)
1147 != NOTMUCH_STATUS_SUCCESS;
1150 /* Formatted output of threads */
1152 do_show_threaded (void *ctx,
1153 notmuch_query_t *query,
1154 const notmuch_show_format_t *format,
1156 notmuch_show_params_t *params)
1158 notmuch_threads_t *threads;
1159 notmuch_thread_t *thread;
1160 notmuch_messages_t *messages;
1161 notmuch_status_t status, res = NOTMUCH_STATUS_SUCCESS;
1164 if (params->offset < 0) {
1166 notmuch_status_t s = notmuch_query_count_threads (query, &count);
1167 if (print_status_query ("notmuch show", query, s))
1170 params->offset += count;
1171 if (params->offset < 0)
1175 status = notmuch_query_search_threads (query, &threads);
1176 if (print_status_query ("notmuch show", query, status))
1179 sp->begin_list (sp);
1182 notmuch_threads_valid (threads) && (params->limit < 0 || i < params->offset + params->limit);
1183 notmuch_threads_move_to_next (threads), i++) {
1184 thread = notmuch_threads_get (threads);
1186 if (i < params->offset) {
1187 notmuch_thread_destroy (thread);
1191 messages = notmuch_thread_get_toplevel_messages (thread);
1193 if (messages == NULL)
1194 INTERNAL_ERROR ("Thread %s has no toplevel messages.\n",
1195 notmuch_thread_get_thread_id (thread));
1197 status = show_messages (ctx, format, sp, messages, 0, params);
1198 if (status && ! res)
1201 notmuch_thread_destroy (thread);
1207 return res != NOTMUCH_STATUS_SUCCESS;
1211 do_show_unthreaded (void *ctx,
1212 notmuch_query_t *query,
1213 const notmuch_show_format_t *format,
1215 notmuch_show_params_t *params)
1217 notmuch_messages_t *messages;
1218 notmuch_message_t *message;
1219 notmuch_status_t status, res = NOTMUCH_STATUS_SUCCESS;
1220 notmuch_bool_t excluded;
1223 if (params->offset < 0) {
1225 notmuch_status_t s = notmuch_query_count_messages (query, &count);
1226 if (print_status_query ("notmuch show", query, s))
1229 params->offset += count;
1230 if (params->offset < 0)
1234 status = notmuch_query_search_messages (query, &messages);
1235 if (print_status_query ("notmuch show", query, status))
1238 sp->begin_list (sp);
1241 notmuch_messages_valid (messages) && (params->limit < 0 || i < params->offset + params->limit);
1242 notmuch_messages_move_to_next (messages), i++) {
1243 if (i < params->offset) {
1247 sp->begin_list (sp);
1248 sp->begin_list (sp);
1250 message = notmuch_messages_get (messages);
1252 notmuch_message_set_flag (message, NOTMUCH_MESSAGE_FLAG_MATCH, TRUE);
1253 excluded = _get_message_flag (message, NOTMUCH_MESSAGE_FLAG_EXCLUDED);
1255 if (! excluded || ! params->omit_excluded) {
1256 status = show_message (ctx, format, sp, message, 0, params);
1257 if (status && ! res)
1262 notmuch_message_destroy (message);
1271 NOTMUCH_FORMAT_NOT_SPECIFIED,
1272 NOTMUCH_FORMAT_JSON,
1273 NOTMUCH_FORMAT_SEXP,
1274 NOTMUCH_FORMAT_TEXT,
1275 NOTMUCH_FORMAT_MBOX,
1279 static const notmuch_show_format_t format_json = {
1280 .new_sprinter = sprinter_json_create,
1281 .part = format_part_sprinter_entry,
1284 static const notmuch_show_format_t format_sexp = {
1285 .new_sprinter = sprinter_sexp_create,
1286 .part = format_part_sprinter_entry,
1289 static const notmuch_show_format_t format_text = {
1290 .new_sprinter = sprinter_text_create,
1291 .part = format_part_text,
1294 static const notmuch_show_format_t format_mbox = {
1295 .new_sprinter = sprinter_text_create,
1296 .part = format_part_mbox,
1299 static const notmuch_show_format_t format_raw = {
1300 .new_sprinter = sprinter_text_create,
1301 .part = format_part_raw,
1304 static const notmuch_show_format_t *formatters[] = {
1305 [NOTMUCH_FORMAT_JSON] = &format_json,
1306 [NOTMUCH_FORMAT_SEXP] = &format_sexp,
1307 [NOTMUCH_FORMAT_TEXT] = &format_text,
1308 [NOTMUCH_FORMAT_MBOX] = &format_mbox,
1309 [NOTMUCH_FORMAT_RAW] = &format_raw,
1313 notmuch_show_command (notmuch_database_t *notmuch, int argc, char *argv[])
1315 notmuch_query_t *query;
1318 const notmuch_show_format_t *formatter;
1319 sprinter_t *sprinter;
1320 notmuch_show_params_t params = {
1324 .limit = -1, /* unlimited */
1325 .omit_excluded = true,
1326 .output_body = true,
1327 .crypto = { .decrypt = NOTMUCH_DECRYPT_AUTO },
1329 int format = NOTMUCH_FORMAT_NOT_SPECIFIED;
1330 bool exclude = true;
1331 bool entire_thread_set = false;
1332 bool single_message;
1333 bool unthreaded = FALSE;
1334 notmuch_status_t status;
1335 int sort = NOTMUCH_SORT_NEWEST_FIRST;
1337 notmuch_opt_desc_t options[] = {
1338 { .opt_keyword = &sort, .name = "sort", .keywords =
1339 (notmuch_keyword_t []){ { "oldest-first", NOTMUCH_SORT_OLDEST_FIRST },
1340 { "newest-first", NOTMUCH_SORT_NEWEST_FIRST },
1342 { .opt_keyword = &format, .name = "format", .keywords =
1343 (notmuch_keyword_t []){ { "json", NOTMUCH_FORMAT_JSON },
1344 { "text", NOTMUCH_FORMAT_TEXT },
1345 { "sexp", NOTMUCH_FORMAT_SEXP },
1346 { "mbox", NOTMUCH_FORMAT_MBOX },
1347 { "raw", NOTMUCH_FORMAT_RAW },
1349 { .opt_int = ¬much_format_version, .name = "format-version" },
1350 { .opt_bool = &exclude, .name = "exclude" },
1351 { .opt_bool = ¶ms.entire_thread, .name = "entire-thread",
1352 .present = &entire_thread_set },
1353 { .opt_bool = &unthreaded, .name = "unthreaded" },
1354 { .opt_int = ¶ms.part, .name = "part" },
1355 { .opt_keyword = (int *) (¶ms.crypto.decrypt), .name = "decrypt",
1356 .keyword_no_arg_value = "true", .keywords =
1357 (notmuch_keyword_t []){ { "false", NOTMUCH_DECRYPT_FALSE },
1358 { "auto", NOTMUCH_DECRYPT_AUTO },
1359 { "true", NOTMUCH_DECRYPT_NOSTASH },
1360 { "stash", NOTMUCH_DECRYPT_TRUE },
1362 { .opt_bool = ¶ms.crypto.verify, .name = "verify" },
1363 { .opt_bool = ¶ms.output_body, .name = "body" },
1364 { .opt_bool = ¶ms.include_html, .name = "include-html" },
1365 { .opt_int = ¶ms.duplicate, .name = "duplicate" },
1366 { .opt_int = ¶ms.limit, .name = "limit" },
1367 { .opt_int = ¶ms.offset, .name = "offset" },
1368 { .opt_inherit = notmuch_shared_options },
1372 opt_index = parse_arguments (argc, argv, options, 1);
1374 return EXIT_FAILURE;
1376 notmuch_process_shared_options (notmuch, argv[0]);
1378 /* explicit decryption implies verification */
1379 if (params.crypto.decrypt == NOTMUCH_DECRYPT_NOSTASH ||
1380 params.crypto.decrypt == NOTMUCH_DECRYPT_TRUE)
1381 params.crypto.verify = true;
1383 /* specifying a part implies single message display */
1384 single_message = params.part >= 0;
1386 /* specifying a duplicate also implies single message display */
1387 single_message = single_message || (params.duplicate > 0);
1389 if (format == NOTMUCH_FORMAT_NOT_SPECIFIED) {
1390 /* if part was requested and format was not specified, use format=raw */
1391 if (params.part >= 0)
1392 format = NOTMUCH_FORMAT_RAW;
1394 format = NOTMUCH_FORMAT_TEXT;
1397 if (format == NOTMUCH_FORMAT_MBOX) {
1398 if (params.part > 0) {
1399 fprintf (stderr, "Error: specifying parts is incompatible with mbox output format.\n");
1400 return EXIT_FAILURE;
1402 } else if (format == NOTMUCH_FORMAT_RAW) {
1403 /* raw format only supports single message display */
1404 single_message = true;
1407 notmuch_exit_if_unsupported_format ();
1409 /* Default is entire-thread = false except for format=json and
1411 if (! entire_thread_set &&
1412 (format == NOTMUCH_FORMAT_JSON || format == NOTMUCH_FORMAT_SEXP))
1413 params.entire_thread = true;
1415 if (! params.output_body) {
1416 if (params.part > 0) {
1417 fprintf (stderr, "Warning: --body=false is incompatible with --part > 0. Disabling.\n");
1418 params.output_body = true;
1420 if (format != NOTMUCH_FORMAT_TEXT &&
1421 format != NOTMUCH_FORMAT_JSON &&
1422 format != NOTMUCH_FORMAT_SEXP)
1424 "Warning: --body=false only implemented for format=text, format=json and format=sexp\n");
1428 if (params.include_html &&
1429 (format != NOTMUCH_FORMAT_TEXT &&
1430 format != NOTMUCH_FORMAT_JSON &&
1431 format != NOTMUCH_FORMAT_SEXP)) {
1433 "Warning: --include-html only implemented for format=text, format=json and format=sexp\n");
1436 if (params.crypto.decrypt == NOTMUCH_DECRYPT_TRUE) {
1437 status = notmuch_database_reopen (notmuch, NOTMUCH_DATABASE_MODE_READ_WRITE);
1439 fprintf (stderr, "Error reopening database for READ_WRITE: %s\n",
1440 notmuch_status_to_string (status));
1441 return EXIT_FAILURE;
1445 query_string = query_string_from_args (notmuch, argc - opt_index, argv + opt_index);
1446 if (query_string == NULL) {
1447 fprintf (stderr, "Out of memory\n");
1448 return EXIT_FAILURE;
1451 if (*query_string == '\0') {
1452 fprintf (stderr, "Error: notmuch show requires at least one search term.\n");
1453 return EXIT_FAILURE;
1456 status = notmuch_query_create_with_syntax (notmuch, query_string,
1457 shared_option_query_syntax (),
1459 if (print_status_database ("notmuch show", notmuch, status))
1460 return EXIT_FAILURE;
1462 notmuch_query_set_sort (query, sort);
1464 /* Create structure printer. */
1465 formatter = formatters[format];
1466 sprinter = formatter->new_sprinter (notmuch, stdout);
1468 params.out_stream = g_mime_stream_stdout_new ();
1470 /* If a single message is requested we do not use search_excludes. */
1471 if (single_message) {
1472 ret = do_show_single (notmuch, query, formatter, sprinter, ¶ms);
1474 /* We always apply set the exclude flag. The
1475 * exclude=true|false option controls whether or not we return
1476 * threads that only match in an excluded message */
1477 notmuch_config_values_t *exclude_tags;
1478 notmuch_status_t status;
1480 for (exclude_tags = notmuch_config_get_values (notmuch, NOTMUCH_CONFIG_EXCLUDE_TAGS);
1481 notmuch_config_values_valid (exclude_tags);
1482 notmuch_config_values_move_to_next (exclude_tags)) {
1484 status = notmuch_query_add_tag_exclude (query,
1485 notmuch_config_values_get (exclude_tags));
1486 if (status && status != NOTMUCH_STATUS_IGNORED) {
1487 print_status_query ("notmuch show", query, status);
1493 if (exclude == false) {
1494 notmuch_query_set_omit_excluded (query, false);
1495 params.omit_excluded = false;
1499 ret = do_show_unthreaded (notmuch, query, formatter, sprinter, ¶ms);
1501 ret = do_show_threaded (notmuch, query, formatter, sprinter, ¶ms);
1505 g_mime_stream_flush (params.out_stream);
1506 g_object_unref (params.out_stream);
1508 _notmuch_crypto_cleanup (¶ms.crypto);
1509 notmuch_query_destroy (query);
1510 notmuch_database_destroy (notmuch);
1512 return ret ? EXIT_FAILURE : EXIT_SUCCESS;