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))
42 tag = notmuch_tags_get (tags);
44 result = talloc_asprintf_append (result, "%s%s",
45 first ? "" : " ", tag);
52 /* Get a nice, single-line summary of message. */
54 _get_one_line_summary (const void *ctx, notmuch_message_t *message)
58 const char *relative_date;
61 from = notmuch_message_get_header (message, "from");
63 date = notmuch_message_get_date (message);
64 relative_date = notmuch_time_relative_date (ctx, date);
66 tags = _get_tags_as_string (ctx, message);
68 return talloc_asprintf (ctx, "%s (%s) (%s)",
69 from, relative_date, tags);
72 static const char *_get_disposition(GMimeObject *meta)
74 GMimeContentDisposition *disposition;
76 disposition = g_mime_object_get_content_disposition (meta);
80 return g_mime_content_disposition_get_disposition (disposition);
83 /* Emit a sequence of key/value pairs for the metadata of message.
84 * The caller should begin a map before calling this. */
86 format_message_sprinter (sprinter_t *sp, notmuch_message_t *message)
88 /* Any changes to the JSON or S-Expression format should be
89 * reflected in the file devel/schemata. */
91 void *local = talloc_new (NULL);
94 const char *relative_date;
96 sp->map_key (sp, "id");
97 sp->string (sp, notmuch_message_get_message_id (message));
99 sp->map_key (sp, "match");
100 sp->boolean (sp, notmuch_message_get_flag (message, NOTMUCH_MESSAGE_FLAG_MATCH));
102 sp->map_key (sp, "excluded");
103 sp->boolean (sp, notmuch_message_get_flag (message, NOTMUCH_MESSAGE_FLAG_EXCLUDED));
105 sp->map_key (sp, "filename");
106 if (notmuch_format_version >= 3) {
107 notmuch_filenames_t *filenames;
110 for (filenames = notmuch_message_get_filenames (message);
111 notmuch_filenames_valid (filenames);
112 notmuch_filenames_move_to_next (filenames)) {
113 sp->string (sp, notmuch_filenames_get (filenames));
115 notmuch_filenames_destroy (filenames);
118 sp->string (sp, notmuch_message_get_filename (message));
121 sp->map_key (sp, "timestamp");
122 date = notmuch_message_get_date (message);
123 sp->integer (sp, date);
125 sp->map_key (sp, "date_relative");
126 relative_date = notmuch_time_relative_date (local, date);
127 sp->string (sp, relative_date);
129 sp->map_key (sp, "tags");
131 for (tags = notmuch_message_get_tags (message);
132 notmuch_tags_valid (tags);
133 notmuch_tags_move_to_next (tags))
134 sp->string (sp, notmuch_tags_get (tags));
140 /* Extract just the email address from the contents of a From:
143 _extract_email_address (const void *ctx, const char *from)
145 InternetAddressList *addresses;
146 InternetAddress *address;
147 InternetAddressMailbox *mailbox;
148 const char *email = "MAILER-DAEMON";
150 addresses = internet_address_list_parse (NULL, from);
152 /* Bail if there is no address here. */
153 if (addresses == NULL || internet_address_list_length (addresses) < 1)
156 /* Otherwise, just use the first address. */
157 address = internet_address_list_get_address (addresses, 0);
159 /* The From header should never contain an address group rather
160 * than a mailbox. So bail if it does. */
161 if (! INTERNET_ADDRESS_IS_MAILBOX (address))
164 mailbox = INTERNET_ADDRESS_MAILBOX (address);
165 email = internet_address_mailbox_get_addr (mailbox);
166 email = talloc_strdup (ctx, email);
170 g_object_unref (addresses);
175 /* Return 1 if 'line' is an mbox From_ line---that is, a line
176 * beginning with zero or more '>' characters followed by the
177 * characters 'F', 'r', 'o', 'm', and space.
179 * Any characters at all may appear after that in the line.
182 _is_from_line (const char *line)
184 const char *s = line;
192 if (STRNCMP_LITERAL (s, "From ") == 0)
199 format_headers_sprinter (sprinter_t *sp, GMimeMessage *message,
200 bool reply, const _notmuch_message_crypto_t *msg_crypto)
202 /* Any changes to the JSON or S-Expression format should be
203 * reflected in the file devel/schemata. */
205 char *recipients_string;
206 const char *reply_to_string;
207 void *local = talloc_new (sp);
211 sp->map_key (sp, "Subject");
212 if (msg_crypto && msg_crypto->payload_subject) {
213 sp->string (sp, msg_crypto->payload_subject);
215 sp->string (sp, g_mime_message_get_subject (message));
217 sp->map_key (sp, "From");
218 sp->string (sp, g_mime_message_get_from_string (message));
220 recipients_string = g_mime_message_get_address_string (message, GMIME_ADDRESS_TYPE_TO);
221 if (recipients_string) {
222 sp->map_key (sp, "To");
223 sp->string (sp, recipients_string);
224 g_free (recipients_string);
227 recipients_string = g_mime_message_get_address_string (message, GMIME_ADDRESS_TYPE_CC);
228 if (recipients_string) {
229 sp->map_key (sp, "Cc");
230 sp->string (sp, recipients_string);
231 g_free (recipients_string);
234 recipients_string = g_mime_message_get_address_string (message, GMIME_ADDRESS_TYPE_BCC);
235 if (recipients_string) {
236 sp->map_key (sp, "Bcc");
237 sp->string (sp, recipients_string);
238 g_free (recipients_string);
241 reply_to_string = g_mime_message_get_reply_to_string (local, message);
242 if (reply_to_string) {
243 sp->map_key (sp, "Reply-To");
244 sp->string (sp, reply_to_string);
248 sp->map_key (sp, "In-reply-to");
249 sp->string (sp, g_mime_object_get_header (GMIME_OBJECT (message), "In-reply-to"));
251 sp->map_key (sp, "References");
252 sp->string (sp, g_mime_object_get_header (GMIME_OBJECT (message), "References"));
254 sp->map_key (sp, "Date");
255 sp->string (sp, g_mime_message_get_date_string (sp, message));
262 /* Write a MIME text part out to the given stream.
264 * If (flags & NOTMUCH_SHOW_TEXT_PART_REPLY), this prepends "> " to
267 * Both line-ending conversion (CRLF->LF) and charset conversion ( ->
268 * UTF-8) will be performed, so it is inappropriate to call this
269 * function with a non-text part. Doing so will trigger an internal
273 show_text_part_content (GMimeObject *part, GMimeStream *stream_out,
274 notmuch_show_text_part_flags flags)
276 GMimeContentType *content_type = g_mime_object_get_content_type (GMIME_OBJECT (part));
277 GMimeStream *stream_filter = NULL;
278 GMimeFilter *crlf_filter = NULL;
279 GMimeFilter *windows_filter = NULL;
280 GMimeDataWrapper *wrapper;
283 if (! g_mime_content_type_is_type (content_type, "text", "*"))
284 INTERNAL_ERROR ("Illegal request to format non-text part (%s) as text.",
285 g_mime_content_type_get_mime_type (content_type));
287 if (stream_out == NULL)
290 charset = g_mime_object_get_content_type_parameter (part, "charset");
291 charset = charset ? g_mime_charset_canon_name (charset) : NULL;
292 wrapper = g_mime_part_get_content (GMIME_PART (part));
293 if (wrapper && charset && !g_ascii_strncasecmp (charset, "iso-8859-", 9)) {
294 GMimeStream *null_stream = NULL;
295 GMimeStream *null_stream_filter = NULL;
297 /* Check for mislabeled Windows encoding */
298 null_stream = g_mime_stream_null_new ();
299 null_stream_filter = g_mime_stream_filter_new (null_stream);
300 windows_filter = g_mime_filter_windows_new (charset);
301 g_mime_stream_filter_add(GMIME_STREAM_FILTER (null_stream_filter),
303 g_mime_data_wrapper_write_to_stream (wrapper, null_stream_filter);
304 charset = g_mime_filter_windows_real_charset(
305 (GMimeFilterWindows *) windows_filter);
307 if (null_stream_filter)
308 g_object_unref (null_stream_filter);
310 g_object_unref (null_stream);
311 /* Keep a reference to windows_filter in order to prevent the
312 * charset string from deallocation. */
315 stream_filter = g_mime_stream_filter_new (stream_out);
316 crlf_filter = g_mime_filter_dos2unix_new (false);
317 g_mime_stream_filter_add(GMIME_STREAM_FILTER (stream_filter),
319 g_object_unref (crlf_filter);
322 GMimeFilter *charset_filter;
323 charset_filter = g_mime_filter_charset_new (charset, "UTF-8");
324 /* This result can be NULL for things like "unknown-8bit".
325 * Don't set a NULL filter as that makes GMime print
326 * annoying assertion-failure messages on stderr. */
327 if (charset_filter) {
328 g_mime_stream_filter_add (GMIME_STREAM_FILTER (stream_filter),
330 g_object_unref (charset_filter);
335 if (flags & NOTMUCH_SHOW_TEXT_PART_REPLY) {
336 GMimeFilter *reply_filter;
337 reply_filter = g_mime_filter_reply_new (true);
339 g_mime_stream_filter_add (GMIME_STREAM_FILTER (stream_filter),
341 g_object_unref (reply_filter);
345 if (wrapper && stream_filter)
346 g_mime_data_wrapper_write_to_stream (wrapper, stream_filter);
348 g_object_unref(stream_filter);
350 g_object_unref (windows_filter);
354 signature_status_to_string (GMimeSignatureStatus status)
356 if (g_mime_signature_status_bad (status))
359 if (g_mime_signature_status_error (status))
362 if (g_mime_signature_status_good (status))
368 /* Print signature flags */
369 struct key_map_struct {
370 GMimeSignatureStatus bit;
375 do_format_signature_errors (sprinter_t *sp, struct key_map_struct *key_map,
376 unsigned int array_map_len, GMimeSignatureStatus errors) {
377 sp->map_key (sp, "errors");
380 for (unsigned int i = 0; i < array_map_len; i++) {
381 if (errors & key_map[i].bit) {
382 sp->map_key (sp, key_map[i].string);
383 sp->boolean (sp, true);
391 format_signature_errors (sprinter_t *sp, GMimeSignature *signature)
393 GMimeSignatureStatus errors = g_mime_signature_get_status (signature);
395 if (!(errors & GMIME_SIGNATURE_STATUS_ERROR_MASK))
398 struct key_map_struct key_map[] = {
399 { GMIME_SIGNATURE_STATUS_KEY_REVOKED, "key-revoked"},
400 { GMIME_SIGNATURE_STATUS_KEY_EXPIRED, "key-expired"},
401 { GMIME_SIGNATURE_STATUS_SIG_EXPIRED, "sig-expired" },
402 { GMIME_SIGNATURE_STATUS_KEY_MISSING, "key-missing"},
403 { GMIME_SIGNATURE_STATUS_CRL_MISSING, "crl-missing"},
404 { GMIME_SIGNATURE_STATUS_CRL_TOO_OLD, "crl-too-old"},
405 { GMIME_SIGNATURE_STATUS_BAD_POLICY, "bad-policy"},
406 { GMIME_SIGNATURE_STATUS_SYS_ERROR, "sys-error"},
407 { GMIME_SIGNATURE_STATUS_TOFU_CONFLICT, "tofu-conflict"},
410 do_format_signature_errors (sp, key_map, ARRAY_SIZE(key_map), errors);
413 /* Signature status sprinter */
415 format_part_sigstatus_sprinter (sprinter_t *sp, GMimeSignatureList *siglist)
417 /* Any changes to the JSON or S-Expression format should be
418 * reflected in the file devel/schemata. */
428 for (i = 0; i < g_mime_signature_list_length (siglist); i++) {
429 GMimeSignature *signature = g_mime_signature_list_get_signature (siglist, i);
434 GMimeSignatureStatus status = g_mime_signature_get_status (signature);
435 sp->map_key (sp, "status");
436 sp->string (sp, signature_status_to_string (status));
438 GMimeCertificate *certificate = g_mime_signature_get_certificate (signature);
439 if (g_mime_signature_status_good (status)) {
441 sp->map_key (sp, "fingerprint");
442 sp->string (sp, g_mime_certificate_get_fingerprint (certificate));
444 /* these dates are seconds since the epoch; should we
445 * provide a more human-readable format string? */
446 time_t created = g_mime_signature_get_created (signature);
448 sp->map_key (sp, "created");
449 sp->integer (sp, created);
451 time_t expires = g_mime_signature_get_expires (signature);
453 sp->map_key (sp, "expires");
454 sp->integer (sp, expires);
457 const char *uid = g_mime_certificate_get_valid_userid (certificate);
459 sp->map_key (sp, "userid");
460 sp->string (sp, uid);
463 } else if (certificate) {
464 const char *key_id = g_mime_certificate_get_fpr16 (certificate);
466 sp->map_key (sp, "keyid");
467 sp->string (sp, key_id);
471 if (notmuch_format_version <= 3) {
472 GMimeSignatureStatus errors = g_mime_signature_get_status (signature);
473 if (g_mime_signature_status_error (errors)) {
474 sp->map_key (sp, "errors");
475 sp->integer (sp, errors);
478 format_signature_errors (sp, signature);
487 static notmuch_status_t
488 format_part_text (const void *ctx, sprinter_t *sp, mime_node_t *node,
489 int indent, const notmuch_show_params_t *params)
491 /* The disposition and content-type metadata are associated with
492 * the envelope for message parts */
493 GMimeObject *meta = node->envelope_part ?
494 GMIME_OBJECT (node->envelope_part) : node->part;
495 GMimeContentType *content_type = g_mime_object_get_content_type (meta);
496 const bool leaf = GMIME_IS_PART (node->part);
497 GMimeStream *stream = params->out_stream;
498 const char *part_type;
501 if (node->envelope_file) {
502 notmuch_message_t *message = node->envelope_file;
504 part_type = "message";
505 g_mime_stream_printf (stream, "\f%s{ id:%s depth:%d match:%d excluded:%d filename:%s\n",
507 notmuch_message_get_message_id (message),
509 notmuch_message_get_flag (message, NOTMUCH_MESSAGE_FLAG_MATCH) ? 1 : 0,
510 notmuch_message_get_flag (message, NOTMUCH_MESSAGE_FLAG_EXCLUDED) ? 1 : 0,
511 notmuch_message_get_filename (message));
513 char *content_string;
514 const char *disposition = _get_disposition (meta);
515 const char *cid = g_mime_object_get_content_id (meta);
516 const char *filename = leaf ?
517 g_mime_part_get_filename (GMIME_PART (node->part)) : NULL;
520 strcasecmp (disposition, GMIME_DISPOSITION_ATTACHMENT) == 0)
521 part_type = "attachment";
525 g_mime_stream_printf (stream, "\f%s{ ID: %d", part_type, node->part_num);
527 g_mime_stream_printf (stream, ", Filename: %s", filename);
529 g_mime_stream_printf (stream, ", Content-id: %s", cid);
531 content_string = g_mime_content_type_get_mime_type (content_type);
532 g_mime_stream_printf (stream, ", Content-type: %s\n", content_string);
533 g_free (content_string);
536 if (GMIME_IS_MESSAGE (node->part)) {
537 GMimeMessage *message = GMIME_MESSAGE (node->part);
538 char *recipients_string;
541 g_mime_stream_printf (stream, "\fheader{\n");
542 if (node->envelope_file)
543 g_mime_stream_printf (stream, "%s\n", _get_one_line_summary (ctx, node->envelope_file));
544 g_mime_stream_printf (stream, "Subject: %s\n", g_mime_message_get_subject (message));
545 g_mime_stream_printf (stream, "From: %s\n", g_mime_message_get_from_string (message));
546 recipients_string = g_mime_message_get_address_string (message, GMIME_ADDRESS_TYPE_TO);
547 if (recipients_string)
548 g_mime_stream_printf (stream, "To: %s\n", recipients_string);
549 g_free (recipients_string);
550 recipients_string = g_mime_message_get_address_string (message, GMIME_ADDRESS_TYPE_CC);
551 if (recipients_string)
552 g_mime_stream_printf (stream, "Cc: %s\n", recipients_string);
553 g_free (recipients_string);
554 date_string = g_mime_message_get_date_string (node, message);
555 g_mime_stream_printf (stream, "Date: %s\n", date_string);
556 g_mime_stream_printf (stream, "\fheader}\n");
558 if (!params->output_body)
560 g_mime_stream_printf (stream, "\f%s}\n", part_type);
561 return NOTMUCH_STATUS_SUCCESS;
563 g_mime_stream_printf (stream, "\fbody{\n");
567 if (g_mime_content_type_is_type (content_type, "text", "*") &&
568 (params->include_html ||
569 ! g_mime_content_type_is_type (content_type, "text", "html")))
571 show_text_part_content (node->part, stream, 0);
573 char *content_string = g_mime_content_type_get_mime_type (content_type);
574 g_mime_stream_printf (stream, "Non-text part: %s\n", content_string);
575 g_free (content_string);
579 for (i = 0; i < node->nchildren; i++)
580 format_part_text (ctx, sp, mime_node_child (node, i), indent, params);
582 if (GMIME_IS_MESSAGE (node->part))
583 g_mime_stream_printf (stream, "\fbody}\n");
585 g_mime_stream_printf (stream, "\f%s}\n", part_type);
587 return NOTMUCH_STATUS_SUCCESS;
591 format_omitted_part_meta_sprinter (sprinter_t *sp, GMimeObject *meta, GMimePart *part)
593 const char *content_charset = g_mime_object_get_content_type_parameter (meta, "charset");
594 const char *cte = g_mime_object_get_header (meta, "content-transfer-encoding");
595 GMimeDataWrapper *wrapper = g_mime_part_get_content (part);
596 GMimeStream *stream = g_mime_data_wrapper_get_stream (wrapper);
597 ssize_t content_length = g_mime_stream_length (stream);
599 if (content_charset != NULL) {
600 sp->map_key (sp, "content-charset");
601 sp->string (sp, content_charset);
604 sp->map_key (sp, "content-transfer-encoding");
605 sp->string (sp, cte);
607 if (content_length >= 0) {
608 sp->map_key (sp, "content-length");
609 sp->integer (sp, content_length);
614 format_part_sprinter (const void *ctx, sprinter_t *sp, mime_node_t *node,
618 /* Any changes to the JSON or S-Expression format should be
619 * reflected in the file devel/schemata. */
621 if (node->envelope_file) {
622 const _notmuch_message_crypto_t *msg_crypto = NULL;
624 format_message_sprinter (sp, node->envelope_file);
627 sp->map_key (sp, "body");
629 format_part_sprinter (ctx, sp, mime_node_child (node, 0), true, include_html);
633 msg_crypto = mime_node_get_message_crypto_status (node);
634 if (notmuch_format_version >= 4) {
635 sp->map_key (sp, "crypto");
637 if (msg_crypto->sig_list ||
638 msg_crypto->decryption_status != NOTMUCH_MESSAGE_DECRYPTED_NONE) {
639 if (msg_crypto->sig_list) {
640 sp->map_key (sp, "signed");
642 sp->map_key (sp, "status");
643 format_part_sigstatus_sprinter (sp, msg_crypto->sig_list);
644 if (msg_crypto->signature_encrypted) {
645 sp->map_key (sp, "encrypted");
646 sp->boolean (sp, msg_crypto->signature_encrypted);
648 if (msg_crypto->payload_subject) {
649 sp->map_key (sp, "headers");
651 sp->string (sp, "Subject");
656 if (msg_crypto->decryption_status != NOTMUCH_MESSAGE_DECRYPTED_NONE) {
657 sp->map_key (sp, "decrypted");
659 sp->map_key (sp, "status");
660 sp->string (sp, msg_crypto->decryption_status == NOTMUCH_MESSAGE_DECRYPTED_FULL ? "full" : "partial");
662 if (msg_crypto->payload_subject) {
663 const char *subject = g_mime_message_get_subject GMIME_MESSAGE (node->part);
664 if (subject == NULL || strcmp (subject, msg_crypto->payload_subject)) {
665 /* protected subject differs from the external header */
666 sp->map_key (sp, "header-mask");
668 sp->map_key (sp, "Subject");
672 sp->string (sp, subject);
682 sp->map_key (sp, "headers");
683 format_headers_sprinter (sp, GMIME_MESSAGE (node->part), false, msg_crypto);
689 /* The disposition and content-type metadata are associated with
690 * the envelope for message parts */
691 GMimeObject *meta = node->envelope_part ?
692 GMIME_OBJECT (node->envelope_part) : node->part;
693 GMimeContentType *content_type = g_mime_object_get_content_type (meta);
694 char *content_string;
695 const char *disposition = _get_disposition (meta);
696 const char *cid = g_mime_object_get_content_id (meta);
697 const char *filename = GMIME_IS_PART (node->part) ?
698 g_mime_part_get_filename (GMIME_PART (node->part)) : NULL;
704 sp->map_key (sp, "id");
705 sp->integer (sp, node->part_num);
707 if (node->decrypt_attempted) {
708 sp->map_key (sp, "encstatus");
711 sp->map_key (sp, "status");
712 sp->string (sp, node->decrypt_success ? "good" : "bad");
717 if (node->verify_attempted) {
718 sp->map_key (sp, "sigstatus");
719 format_part_sigstatus_sprinter (sp, node->sig_list);
722 sp->map_key (sp, "content-type");
723 content_string = g_mime_content_type_get_mime_type (content_type);
724 sp->string (sp, content_string);
725 g_free (content_string);
728 sp->map_key (sp, "content-disposition");
729 sp->string (sp, disposition);
733 sp->map_key (sp, "content-id");
734 sp->string (sp, cid);
738 sp->map_key (sp, "filename");
739 sp->string (sp, filename);
742 if (GMIME_IS_PART (node->part)) {
743 /* For non-HTML text parts, we include the content in the
744 * JSON. Since JSON must be Unicode, we handle charset
745 * decoding here and do not report a charset to the caller.
746 * For text/html parts, we do not include the content unless
747 * the --include-html option has been passed. If a html part
748 * is not included, it can be requested directly. This makes
749 * charset decoding the responsibility on the caller so we
750 * report the charset for text/html parts.
752 if (g_mime_content_type_is_type (content_type, "text", "*") &&
754 ! g_mime_content_type_is_type (content_type, "text", "html")))
756 GMimeStream *stream_memory = g_mime_stream_mem_new ();
757 GByteArray *part_content;
758 show_text_part_content (node->part, stream_memory, 0);
759 part_content = g_mime_stream_mem_get_byte_array (GMIME_STREAM_MEM (stream_memory));
760 sp->map_key (sp, "content");
761 sp->string_len (sp, (char *) part_content->data, part_content->len);
762 g_object_unref (stream_memory);
764 format_omitted_part_meta_sprinter (sp, meta, GMIME_PART (node->part));
766 } else if (GMIME_IS_MULTIPART (node->part)) {
767 sp->map_key (sp, "content");
770 } else if (GMIME_IS_MESSAGE (node->part)) {
771 sp->map_key (sp, "content");
775 sp->map_key (sp, "headers");
776 format_headers_sprinter (sp, GMIME_MESSAGE (node->part), false, NULL);
778 sp->map_key (sp, "body");
783 for (i = 0; i < node->nchildren; i++)
784 format_part_sprinter (ctx, sp, mime_node_child (node, i), true, include_html);
786 /* Close content structures */
787 for (i = 0; i < nclose; i++)
793 static notmuch_status_t
794 format_part_sprinter_entry (const void *ctx, sprinter_t *sp,
795 mime_node_t *node, unused (int indent),
796 const notmuch_show_params_t *params)
798 format_part_sprinter (ctx, sp, node, params->output_body, params->include_html);
800 return NOTMUCH_STATUS_SUCCESS;
803 /* Print a message in "mboxrd" format as documented, for example,
806 * http://qmail.org/qmail-manual-html/man5/mbox.html
808 static notmuch_status_t
809 format_part_mbox (const void *ctx, unused (sprinter_t *sp), mime_node_t *node,
811 unused (const notmuch_show_params_t *params))
813 notmuch_message_t *message = node->envelope_file;
815 const char *filename;
820 struct tm date_gmtime;
821 char date_asctime[26];
828 INTERNAL_ERROR ("format_part_mbox requires a root part");
830 filename = notmuch_message_get_filename (message);
831 file = gzopen (filename, "r");
833 fprintf (stderr, "Failed to open %s: %s\n",
834 filename, strerror (errno));
835 return NOTMUCH_STATUS_FILE_ERROR;
838 from = notmuch_message_get_header (message, "from");
839 from = _extract_email_address (ctx, from);
841 date = notmuch_message_get_date (message);
842 gmtime_r (&date, &date_gmtime);
843 asctime_r (&date_gmtime, date_asctime);
845 printf ("From %s %s", from, date_asctime);
847 while ((line_len = gz_getline (message, &line, &line_size, file)) != UTIL_EOF ) {
848 if (_is_from_line (line))
857 return NOTMUCH_STATUS_SUCCESS;
860 static notmuch_status_t
861 format_part_raw (unused (const void *ctx), unused (sprinter_t *sp),
862 mime_node_t *node, unused (int indent),
863 const notmuch_show_params_t *params)
865 if (node->envelope_file) {
866 /* Special case the entire message to avoid MIME parsing. */
867 const char *filename;
868 GMimeStream *stream = NULL;
871 notmuch_status_t ret = NOTMUCH_STATUS_FILE_ERROR;
873 filename = notmuch_message_get_filename (node->envelope_file);
874 if (filename == NULL) {
875 fprintf (stderr, "Error: Cannot get message filename.\n");
879 stream = g_mime_stream_gzfile_open (filename);
880 if (stream == NULL) {
881 fprintf (stderr, "Error: Cannot open file %s: %s\n", filename, strerror (errno));
885 while (! g_mime_stream_eos (stream)) {
886 ssize = g_mime_stream_read (stream, buf, sizeof(buf));
888 fprintf (stderr, "Error: Read failed from %s\n", filename);
892 if (ssize > 0 && fwrite (buf, ssize, 1, stdout) != 1) {
893 fprintf (stderr, "Error: Write %ld chars to stdout failed\n", ssize);
898 ret = NOTMUCH_STATUS_SUCCESS;
900 /* XXX This DONE is just for the special case of a node in a single file */
903 g_object_unref (stream);
908 GMimeStream *stream_filter = g_mime_stream_filter_new (params->out_stream);
910 if (GMIME_IS_PART (node->part)) {
911 /* For leaf parts, we emit only the transfer-decoded
913 GMimeDataWrapper *wrapper;
914 wrapper = g_mime_part_get_content (GMIME_PART (node->part));
916 if (wrapper && stream_filter)
917 g_mime_data_wrapper_write_to_stream (wrapper, stream_filter);
919 /* Write out the whole part. For message parts (the root
920 * part and embedded message parts), this will be the
921 * message including its headers (but not the
922 * encapsulating part's headers). For multipart parts,
923 * this will include the headers. */
925 g_mime_object_write_to_stream (node->part, NULL, stream_filter);
929 g_object_unref (stream_filter);
931 return NOTMUCH_STATUS_SUCCESS;
934 static notmuch_status_t
935 show_message (void *ctx,
936 const notmuch_show_format_t *format,
938 notmuch_message_t *message,
940 notmuch_show_params_t *params)
942 void *local = talloc_new (ctx);
943 mime_node_t *root, *part;
944 notmuch_status_t status;
945 unsigned int session_keys = 0;
946 notmuch_status_t session_key_count_error = NOTMUCH_STATUS_SUCCESS;
948 if (params->crypto.decrypt == NOTMUCH_DECRYPT_TRUE)
949 session_key_count_error = notmuch_message_count_properties (message, "session-key", &session_keys);
951 status = mime_node_open (local, message, &(params->crypto), &root);
954 part = mime_node_seek_dfs (root, (params->part < 0 ? 0 : params->part));
956 status = format->part (local, sp, part, indent, params);
957 if (params->crypto.decrypt == NOTMUCH_DECRYPT_TRUE && session_key_count_error == NOTMUCH_STATUS_SUCCESS) {
958 unsigned int new_session_keys = 0;
959 if (notmuch_message_count_properties (message, "session-key", &new_session_keys) == NOTMUCH_STATUS_SUCCESS &&
960 new_session_keys > session_keys) {
961 /* try a quiet re-indexing */
962 notmuch_indexopts_t *indexopts = notmuch_database_get_default_indexopts (notmuch_message_get_database (message));
964 notmuch_indexopts_set_decrypt_policy (indexopts, NOTMUCH_DECRYPT_AUTO);
965 print_status_message ("Error re-indexing message with --decrypt=stash",
966 message, notmuch_message_reindex (message, indexopts));
975 static notmuch_status_t
976 show_messages (void *ctx,
977 const notmuch_show_format_t *format,
979 notmuch_messages_t *messages,
981 notmuch_show_params_t *params)
983 notmuch_message_t *message;
987 notmuch_status_t status, res = NOTMUCH_STATUS_SUCCESS;
992 notmuch_messages_valid (messages);
993 notmuch_messages_move_to_next (messages))
997 message = notmuch_messages_get (messages);
999 match = notmuch_message_get_flag (message, NOTMUCH_MESSAGE_FLAG_MATCH);
1000 excluded = notmuch_message_get_flag (message, NOTMUCH_MESSAGE_FLAG_EXCLUDED);
1002 next_indent = indent;
1004 if ((match && (!excluded || !params->omit_excluded)) || params->entire_thread) {
1005 status = show_message (ctx, format, sp, message, indent, params);
1008 next_indent = indent + 1;
1013 status = show_messages (ctx,
1015 notmuch_message_get_replies (message),
1021 notmuch_message_destroy (message);
1031 /* Formatted output of single message */
1033 do_show_single (void *ctx,
1034 notmuch_query_t *query,
1035 const notmuch_show_format_t *format,
1037 notmuch_show_params_t *params)
1039 notmuch_messages_t *messages;
1040 notmuch_message_t *message;
1041 notmuch_status_t status;
1044 status = notmuch_query_count_messages (query, &count);
1045 if (print_status_query ("notmuch show", query, status))
1049 fprintf (stderr, "Error: search term did not match precisely one message (matched %u messages).\n", count);
1053 status = notmuch_query_search_messages (query, &messages);
1054 if (print_status_query ("notmuch show", query, status))
1057 message = notmuch_messages_get (messages);
1059 if (message == NULL) {
1060 fprintf (stderr, "Error: Cannot find matching message.\n");
1064 notmuch_message_set_flag (message, NOTMUCH_MESSAGE_FLAG_MATCH, 1);
1066 return show_message (ctx, format, sp, message, 0, params)
1067 != NOTMUCH_STATUS_SUCCESS;
1070 /* Formatted output of threads */
1073 notmuch_query_t *query,
1074 const notmuch_show_format_t *format,
1076 notmuch_show_params_t *params)
1078 notmuch_threads_t *threads;
1079 notmuch_thread_t *thread;
1080 notmuch_messages_t *messages;
1081 notmuch_status_t status, res = NOTMUCH_STATUS_SUCCESS;
1083 status= notmuch_query_search_threads (query, &threads);
1084 if (print_status_query ("notmuch show", query, status))
1087 sp->begin_list (sp);
1090 notmuch_threads_valid (threads);
1091 notmuch_threads_move_to_next (threads))
1093 thread = notmuch_threads_get (threads);
1095 messages = notmuch_thread_get_toplevel_messages (thread);
1097 if (messages == NULL)
1098 INTERNAL_ERROR ("Thread %s has no toplevel messages.\n",
1099 notmuch_thread_get_thread_id (thread));
1101 status = show_messages (ctx, format, sp, messages, 0, params);
1105 notmuch_thread_destroy (thread);
1111 return res != NOTMUCH_STATUS_SUCCESS;
1115 NOTMUCH_FORMAT_NOT_SPECIFIED,
1116 NOTMUCH_FORMAT_JSON,
1117 NOTMUCH_FORMAT_SEXP,
1118 NOTMUCH_FORMAT_TEXT,
1119 NOTMUCH_FORMAT_MBOX,
1123 static const notmuch_show_format_t format_json = {
1124 .new_sprinter = sprinter_json_create,
1125 .part = format_part_sprinter_entry,
1128 static const notmuch_show_format_t format_sexp = {
1129 .new_sprinter = sprinter_sexp_create,
1130 .part = format_part_sprinter_entry,
1133 static const notmuch_show_format_t format_text = {
1134 .new_sprinter = sprinter_text_create,
1135 .part = format_part_text,
1138 static const notmuch_show_format_t format_mbox = {
1139 .new_sprinter = sprinter_text_create,
1140 .part = format_part_mbox,
1143 static const notmuch_show_format_t format_raw = {
1144 .new_sprinter = sprinter_text_create,
1145 .part = format_part_raw,
1148 static const notmuch_show_format_t *formatters[] = {
1149 [NOTMUCH_FORMAT_JSON] = &format_json,
1150 [NOTMUCH_FORMAT_SEXP] = &format_sexp,
1151 [NOTMUCH_FORMAT_TEXT] = &format_text,
1152 [NOTMUCH_FORMAT_MBOX] = &format_mbox,
1153 [NOTMUCH_FORMAT_RAW] = &format_raw,
1157 notmuch_show_command (notmuch_config_t *config, int argc, char *argv[])
1159 notmuch_database_t *notmuch;
1160 notmuch_query_t *query;
1163 const notmuch_show_format_t *formatter;
1164 sprinter_t *sprinter;
1165 notmuch_show_params_t params = {
1167 .omit_excluded = true,
1168 .output_body = true,
1169 .crypto = { .decrypt = NOTMUCH_DECRYPT_AUTO },
1171 int format = NOTMUCH_FORMAT_NOT_SPECIFIED;
1172 bool exclude = true;
1173 bool entire_thread_set = false;
1174 bool single_message;
1176 notmuch_opt_desc_t options[] = {
1177 { .opt_keyword = &format, .name = "format", .keywords =
1178 (notmuch_keyword_t []){ { "json", NOTMUCH_FORMAT_JSON },
1179 { "text", NOTMUCH_FORMAT_TEXT },
1180 { "sexp", NOTMUCH_FORMAT_SEXP },
1181 { "mbox", NOTMUCH_FORMAT_MBOX },
1182 { "raw", NOTMUCH_FORMAT_RAW },
1184 { .opt_int = ¬much_format_version, .name = "format-version" },
1185 { .opt_bool = &exclude, .name = "exclude" },
1186 { .opt_bool = ¶ms.entire_thread, .name = "entire-thread",
1187 .present = &entire_thread_set },
1188 { .opt_int = ¶ms.part, .name = "part" },
1189 { .opt_keyword = (int*)(¶ms.crypto.decrypt), .name = "decrypt",
1190 .keyword_no_arg_value = "true", .keywords =
1191 (notmuch_keyword_t []){ { "false", NOTMUCH_DECRYPT_FALSE },
1192 { "auto", NOTMUCH_DECRYPT_AUTO },
1193 { "true", NOTMUCH_DECRYPT_NOSTASH },
1194 { "stash", NOTMUCH_DECRYPT_TRUE },
1196 { .opt_bool = ¶ms.crypto.verify, .name = "verify" },
1197 { .opt_bool = ¶ms.output_body, .name = "body" },
1198 { .opt_bool = ¶ms.include_html, .name = "include-html" },
1199 { .opt_inherit = notmuch_shared_options },
1203 opt_index = parse_arguments (argc, argv, options, 1);
1205 return EXIT_FAILURE;
1207 notmuch_process_shared_options (argv[0]);
1209 /* explicit decryption implies verification */
1210 if (params.crypto.decrypt == NOTMUCH_DECRYPT_NOSTASH ||
1211 params.crypto.decrypt == NOTMUCH_DECRYPT_TRUE)
1212 params.crypto.verify = true;
1214 /* specifying a part implies single message display */
1215 single_message = params.part >= 0;
1217 if (format == NOTMUCH_FORMAT_NOT_SPECIFIED) {
1218 /* if part was requested and format was not specified, use format=raw */
1219 if (params.part >= 0)
1220 format = NOTMUCH_FORMAT_RAW;
1222 format = NOTMUCH_FORMAT_TEXT;
1225 if (format == NOTMUCH_FORMAT_MBOX) {
1226 if (params.part > 0) {
1227 fprintf (stderr, "Error: specifying parts is incompatible with mbox output format.\n");
1228 return EXIT_FAILURE;
1230 } else if (format == NOTMUCH_FORMAT_RAW) {
1231 /* raw format only supports single message display */
1232 single_message = true;
1235 notmuch_exit_if_unsupported_format ();
1237 /* Default is entire-thread = false except for format=json and
1239 if (! entire_thread_set &&
1240 (format == NOTMUCH_FORMAT_JSON || format == NOTMUCH_FORMAT_SEXP))
1241 params.entire_thread = true;
1243 if (!params.output_body) {
1244 if (params.part > 0) {
1245 fprintf (stderr, "Warning: --body=false is incompatible with --part > 0. Disabling.\n");
1246 params.output_body = true;
1248 if (format != NOTMUCH_FORMAT_TEXT &&
1249 format != NOTMUCH_FORMAT_JSON &&
1250 format != NOTMUCH_FORMAT_SEXP)
1252 "Warning: --body=false only implemented for format=text, format=json and format=sexp\n");
1256 if (params.include_html &&
1257 (format != NOTMUCH_FORMAT_TEXT &&
1258 format != NOTMUCH_FORMAT_JSON &&
1259 format != NOTMUCH_FORMAT_SEXP)) {
1260 fprintf (stderr, "Warning: --include-html only implemented for format=text, format=json and format=sexp\n");
1263 query_string = query_string_from_args (config, argc-opt_index, argv+opt_index);
1264 if (query_string == NULL) {
1265 fprintf (stderr, "Out of memory\n");
1266 return EXIT_FAILURE;
1269 if (*query_string == '\0') {
1270 fprintf (stderr, "Error: notmuch show requires at least one search term.\n");
1271 return EXIT_FAILURE;
1274 notmuch_database_mode_t mode = NOTMUCH_DATABASE_MODE_READ_ONLY;
1275 if (params.crypto.decrypt == NOTMUCH_DECRYPT_TRUE)
1276 mode = NOTMUCH_DATABASE_MODE_READ_WRITE;
1277 if (notmuch_database_open (notmuch_config_get_database_path (config),
1279 return EXIT_FAILURE;
1281 notmuch_exit_if_unmatched_db_uuid (notmuch);
1283 query = notmuch_query_create (notmuch, query_string);
1284 if (query == NULL) {
1285 fprintf (stderr, "Out of memory\n");
1286 return EXIT_FAILURE;
1289 /* Create structure printer. */
1290 formatter = formatters[format];
1291 sprinter = formatter->new_sprinter(config, stdout);
1293 params.out_stream = g_mime_stream_stdout_new ();
1295 /* If a single message is requested we do not use search_excludes. */
1296 if (single_message) {
1297 ret = do_show_single (config, query, formatter, sprinter, ¶ms);
1299 /* We always apply set the exclude flag. The
1300 * exclude=true|false option controls whether or not we return
1301 * threads that only match in an excluded message */
1302 const char **search_exclude_tags;
1303 size_t search_exclude_tags_length;
1305 notmuch_status_t status;
1307 search_exclude_tags = notmuch_config_get_search_exclude_tags
1308 (config, &search_exclude_tags_length);
1310 for (i = 0; i < search_exclude_tags_length; i++) {
1311 status = notmuch_query_add_tag_exclude (query, search_exclude_tags[i]);
1312 if (status && status != NOTMUCH_STATUS_IGNORED) {
1313 print_status_query ("notmuch show", query, status);
1319 if (exclude == false) {
1320 notmuch_query_set_omit_excluded (query, false);
1321 params.omit_excluded = false;
1324 ret = do_show (config, query, formatter, sprinter, ¶ms);
1328 g_mime_stream_flush (params.out_stream);
1329 g_object_unref (params.out_stream);
1331 _notmuch_crypto_cleanup (¶ms.crypto);
1332 notmuch_query_destroy (query);
1333 notmuch_database_destroy (notmuch);
1335 return ret ? EXIT_FAILURE : EXIT_SUCCESS;