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,
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 sp->string (sp, g_mime_message_get_subject (message));
214 sp->map_key (sp, "From");
215 sp->string (sp, g_mime_message_get_from_string (message));
217 recipients_string = g_mime_message_get_address_string (message, GMIME_ADDRESS_TYPE_TO);
218 if (recipients_string) {
219 sp->map_key (sp, "To");
220 sp->string (sp, recipients_string);
221 g_free (recipients_string);
224 recipients_string = g_mime_message_get_address_string (message, GMIME_ADDRESS_TYPE_CC);
225 if (recipients_string) {
226 sp->map_key (sp, "Cc");
227 sp->string (sp, recipients_string);
228 g_free (recipients_string);
231 recipients_string = g_mime_message_get_address_string (message, GMIME_ADDRESS_TYPE_BCC);
232 if (recipients_string) {
233 sp->map_key (sp, "Bcc");
234 sp->string (sp, recipients_string);
235 g_free (recipients_string);
238 reply_to_string = g_mime_message_get_reply_to_string (local, message);
239 if (reply_to_string) {
240 sp->map_key (sp, "Reply-To");
241 sp->string (sp, reply_to_string);
245 sp->map_key (sp, "In-reply-to");
246 sp->string (sp, g_mime_object_get_header (GMIME_OBJECT (message), "In-reply-to"));
248 sp->map_key (sp, "References");
249 sp->string (sp, g_mime_object_get_header (GMIME_OBJECT (message), "References"));
251 sp->map_key (sp, "Date");
252 sp->string (sp, g_mime_message_get_date_string (sp, message));
259 /* Write a MIME text part out to the given stream.
261 * If (flags & NOTMUCH_SHOW_TEXT_PART_REPLY), this prepends "> " to
264 * Both line-ending conversion (CRLF->LF) and charset conversion ( ->
265 * UTF-8) will be performed, so it is inappropriate to call this
266 * function with a non-text part. Doing so will trigger an internal
270 show_text_part_content (GMimeObject *part, GMimeStream *stream_out,
271 notmuch_show_text_part_flags flags)
273 GMimeContentType *content_type = g_mime_object_get_content_type (GMIME_OBJECT (part));
274 GMimeStream *stream_filter = NULL;
275 GMimeFilter *crlf_filter = NULL;
276 GMimeFilter *windows_filter = NULL;
277 GMimeDataWrapper *wrapper;
280 if (! g_mime_content_type_is_type (content_type, "text", "*"))
281 INTERNAL_ERROR ("Illegal request to format non-text part (%s) as text.",
282 g_mime_content_type_get_mime_type (content_type));
284 if (stream_out == NULL)
287 charset = g_mime_object_get_content_type_parameter (part, "charset");
288 charset = charset ? g_mime_charset_canon_name (charset) : NULL;
289 wrapper = g_mime_part_get_content (GMIME_PART (part));
290 if (wrapper && charset && !g_ascii_strncasecmp (charset, "iso-8859-", 9)) {
291 GMimeStream *null_stream = NULL;
292 GMimeStream *null_stream_filter = NULL;
294 /* Check for mislabeled Windows encoding */
295 null_stream = g_mime_stream_null_new ();
296 null_stream_filter = g_mime_stream_filter_new (null_stream);
297 windows_filter = g_mime_filter_windows_new (charset);
298 g_mime_stream_filter_add(GMIME_STREAM_FILTER (null_stream_filter),
300 g_mime_data_wrapper_write_to_stream (wrapper, null_stream_filter);
301 charset = g_mime_filter_windows_real_charset(
302 (GMimeFilterWindows *) windows_filter);
304 if (null_stream_filter)
305 g_object_unref (null_stream_filter);
307 g_object_unref (null_stream);
308 /* Keep a reference to windows_filter in order to prevent the
309 * charset string from deallocation. */
312 stream_filter = g_mime_stream_filter_new (stream_out);
313 crlf_filter = g_mime_filter_dos2unix_new (false);
314 g_mime_stream_filter_add(GMIME_STREAM_FILTER (stream_filter),
316 g_object_unref (crlf_filter);
319 GMimeFilter *charset_filter;
320 charset_filter = g_mime_filter_charset_new (charset, "UTF-8");
321 /* This result can be NULL for things like "unknown-8bit".
322 * Don't set a NULL filter as that makes GMime print
323 * annoying assertion-failure messages on stderr. */
324 if (charset_filter) {
325 g_mime_stream_filter_add (GMIME_STREAM_FILTER (stream_filter),
327 g_object_unref (charset_filter);
332 if (flags & NOTMUCH_SHOW_TEXT_PART_REPLY) {
333 GMimeFilter *reply_filter;
334 reply_filter = g_mime_filter_reply_new (true);
336 g_mime_stream_filter_add (GMIME_STREAM_FILTER (stream_filter),
338 g_object_unref (reply_filter);
342 if (wrapper && stream_filter)
343 g_mime_data_wrapper_write_to_stream (wrapper, stream_filter);
345 g_object_unref(stream_filter);
347 g_object_unref (windows_filter);
351 signature_status_to_string (GMimeSignatureStatus status)
353 if (g_mime_signature_status_bad (status))
356 if (g_mime_signature_status_error (status))
359 if (g_mime_signature_status_good (status))
365 /* Print signature flags */
366 struct key_map_struct {
367 GMimeSignatureStatus bit;
372 do_format_signature_errors (sprinter_t *sp, struct key_map_struct *key_map,
373 unsigned int array_map_len, GMimeSignatureStatus errors) {
374 sp->map_key (sp, "errors");
377 for (unsigned int i = 0; i < array_map_len; i++) {
378 if (errors & key_map[i].bit) {
379 sp->map_key (sp, key_map[i].string);
380 sp->boolean (sp, true);
388 format_signature_errors (sprinter_t *sp, GMimeSignature *signature)
390 GMimeSignatureStatus errors = g_mime_signature_get_status (signature);
392 if (!(errors & GMIME_SIGNATURE_STATUS_ERROR_MASK))
395 struct key_map_struct key_map[] = {
396 { GMIME_SIGNATURE_STATUS_KEY_REVOKED, "key-revoked"},
397 { GMIME_SIGNATURE_STATUS_KEY_EXPIRED, "key-expired"},
398 { GMIME_SIGNATURE_STATUS_SIG_EXPIRED, "sig-expired" },
399 { GMIME_SIGNATURE_STATUS_KEY_MISSING, "key-missing"},
400 { GMIME_SIGNATURE_STATUS_CRL_MISSING, "crl-missing"},
401 { GMIME_SIGNATURE_STATUS_CRL_TOO_OLD, "crl-too-old"},
402 { GMIME_SIGNATURE_STATUS_BAD_POLICY, "bad-policy"},
403 { GMIME_SIGNATURE_STATUS_SYS_ERROR, "sys-error"},
404 { GMIME_SIGNATURE_STATUS_TOFU_CONFLICT, "tofu-conflict"},
407 do_format_signature_errors (sp, key_map, ARRAY_SIZE(key_map), errors);
410 /* Signature status sprinter */
412 format_part_sigstatus_sprinter (sprinter_t *sp, GMimeSignatureList *siglist)
414 /* Any changes to the JSON or S-Expression format should be
415 * reflected in the file devel/schemata. */
425 for (i = 0; i < g_mime_signature_list_length (siglist); i++) {
426 GMimeSignature *signature = g_mime_signature_list_get_signature (siglist, i);
431 GMimeSignatureStatus status = g_mime_signature_get_status (signature);
432 sp->map_key (sp, "status");
433 sp->string (sp, signature_status_to_string (status));
435 GMimeCertificate *certificate = g_mime_signature_get_certificate (signature);
436 if (g_mime_signature_status_good (status)) {
438 sp->map_key (sp, "fingerprint");
439 sp->string (sp, g_mime_certificate_get_fingerprint (certificate));
441 /* these dates are seconds since the epoch; should we
442 * provide a more human-readable format string? */
443 time_t created = g_mime_signature_get_created (signature);
445 sp->map_key (sp, "created");
446 sp->integer (sp, created);
448 time_t expires = g_mime_signature_get_expires (signature);
450 sp->map_key (sp, "expires");
451 sp->integer (sp, expires);
454 const char *uid = g_mime_certificate_get_valid_userid (certificate);
456 sp->map_key (sp, "userid");
457 sp->string (sp, uid);
460 } else if (certificate) {
461 const char *key_id = g_mime_certificate_get_fpr16 (certificate);
463 sp->map_key (sp, "keyid");
464 sp->string (sp, key_id);
468 if (notmuch_format_version <= 3) {
469 GMimeSignatureStatus errors = g_mime_signature_get_status (signature);
470 if (g_mime_signature_status_error (errors)) {
471 sp->map_key (sp, "errors");
472 sp->integer (sp, errors);
475 format_signature_errors (sp, signature);
484 static notmuch_status_t
485 format_part_text (const void *ctx, sprinter_t *sp, mime_node_t *node,
486 int indent, const notmuch_show_params_t *params)
488 /* The disposition and content-type metadata are associated with
489 * the envelope for message parts */
490 GMimeObject *meta = node->envelope_part ?
491 GMIME_OBJECT (node->envelope_part) : node->part;
492 GMimeContentType *content_type = g_mime_object_get_content_type (meta);
493 const bool leaf = GMIME_IS_PART (node->part);
494 GMimeStream *stream = params->out_stream;
495 const char *part_type;
498 if (node->envelope_file) {
499 notmuch_message_t *message = node->envelope_file;
501 part_type = "message";
502 g_mime_stream_printf (stream, "\f%s{ id:%s depth:%d match:%d excluded:%d filename:%s\n",
504 notmuch_message_get_message_id (message),
506 notmuch_message_get_flag (message, NOTMUCH_MESSAGE_FLAG_MATCH) ? 1 : 0,
507 notmuch_message_get_flag (message, NOTMUCH_MESSAGE_FLAG_EXCLUDED) ? 1 : 0,
508 notmuch_message_get_filename (message));
510 char *content_string;
511 const char *disposition = _get_disposition (meta);
512 const char *cid = g_mime_object_get_content_id (meta);
513 const char *filename = leaf ?
514 g_mime_part_get_filename (GMIME_PART (node->part)) : NULL;
517 strcasecmp (disposition, GMIME_DISPOSITION_ATTACHMENT) == 0)
518 part_type = "attachment";
522 g_mime_stream_printf (stream, "\f%s{ ID: %d", part_type, node->part_num);
524 g_mime_stream_printf (stream, ", Filename: %s", filename);
526 g_mime_stream_printf (stream, ", Content-id: %s", cid);
528 content_string = g_mime_content_type_get_mime_type (content_type);
529 g_mime_stream_printf (stream, ", Content-type: %s\n", content_string);
530 g_free (content_string);
533 if (GMIME_IS_MESSAGE (node->part)) {
534 GMimeMessage *message = GMIME_MESSAGE (node->part);
535 char *recipients_string;
538 g_mime_stream_printf (stream, "\fheader{\n");
539 if (node->envelope_file)
540 g_mime_stream_printf (stream, "%s\n", _get_one_line_summary (ctx, node->envelope_file));
541 g_mime_stream_printf (stream, "Subject: %s\n", g_mime_message_get_subject (message));
542 g_mime_stream_printf (stream, "From: %s\n", g_mime_message_get_from_string (message));
543 recipients_string = g_mime_message_get_address_string (message, GMIME_ADDRESS_TYPE_TO);
544 if (recipients_string)
545 g_mime_stream_printf (stream, "To: %s\n", recipients_string);
546 g_free (recipients_string);
547 recipients_string = g_mime_message_get_address_string (message, GMIME_ADDRESS_TYPE_CC);
548 if (recipients_string)
549 g_mime_stream_printf (stream, "Cc: %s\n", recipients_string);
550 g_free (recipients_string);
551 date_string = g_mime_message_get_date_string (node, message);
552 g_mime_stream_printf (stream, "Date: %s\n", date_string);
553 g_mime_stream_printf (stream, "\fheader}\n");
555 if (!params->output_body)
557 g_mime_stream_printf (stream, "\f%s}\n", part_type);
558 return NOTMUCH_STATUS_SUCCESS;
560 g_mime_stream_printf (stream, "\fbody{\n");
564 if (g_mime_content_type_is_type (content_type, "text", "*") &&
565 (params->include_html ||
566 ! g_mime_content_type_is_type (content_type, "text", "html")))
568 show_text_part_content (node->part, stream, 0);
570 char *content_string = g_mime_content_type_get_mime_type (content_type);
571 g_mime_stream_printf (stream, "Non-text part: %s\n", content_string);
572 g_free (content_string);
576 for (i = 0; i < node->nchildren; i++)
577 format_part_text (ctx, sp, mime_node_child (node, i), indent, params);
579 if (GMIME_IS_MESSAGE (node->part))
580 g_mime_stream_printf (stream, "\fbody}\n");
582 g_mime_stream_printf (stream, "\f%s}\n", part_type);
584 return NOTMUCH_STATUS_SUCCESS;
588 format_omitted_part_meta_sprinter (sprinter_t *sp, GMimeObject *meta, GMimePart *part)
590 const char *content_charset = g_mime_object_get_content_type_parameter (meta, "charset");
591 const char *cte = g_mime_object_get_header (meta, "content-transfer-encoding");
592 GMimeDataWrapper *wrapper = g_mime_part_get_content (part);
593 GMimeStream *stream = g_mime_data_wrapper_get_stream (wrapper);
594 ssize_t content_length = g_mime_stream_length (stream);
596 if (content_charset != NULL) {
597 sp->map_key (sp, "content-charset");
598 sp->string (sp, content_charset);
601 sp->map_key (sp, "content-transfer-encoding");
602 sp->string (sp, cte);
604 if (content_length >= 0) {
605 sp->map_key (sp, "content-length");
606 sp->integer (sp, content_length);
611 format_part_sprinter (const void *ctx, sprinter_t *sp, mime_node_t *node,
615 /* Any changes to the JSON or S-Expression format should be
616 * reflected in the file devel/schemata. */
618 if (node->envelope_file) {
620 format_message_sprinter (sp, node->envelope_file);
622 sp->map_key (sp, "headers");
623 format_headers_sprinter (sp, GMIME_MESSAGE (node->part), false);
626 sp->map_key (sp, "body");
628 format_part_sprinter (ctx, sp, mime_node_child (node, 0), true, include_html);
635 /* The disposition and content-type metadata are associated with
636 * the envelope for message parts */
637 GMimeObject *meta = node->envelope_part ?
638 GMIME_OBJECT (node->envelope_part) : node->part;
639 GMimeContentType *content_type = g_mime_object_get_content_type (meta);
640 char *content_string;
641 const char *disposition = _get_disposition (meta);
642 const char *cid = g_mime_object_get_content_id (meta);
643 const char *filename = GMIME_IS_PART (node->part) ?
644 g_mime_part_get_filename (GMIME_PART (node->part)) : NULL;
650 sp->map_key (sp, "id");
651 sp->integer (sp, node->part_num);
653 if (node->decrypt_attempted) {
654 sp->map_key (sp, "encstatus");
657 sp->map_key (sp, "status");
658 sp->string (sp, node->decrypt_success ? "good" : "bad");
663 if (node->verify_attempted) {
664 sp->map_key (sp, "sigstatus");
665 format_part_sigstatus_sprinter (sp, node->sig_list);
668 sp->map_key (sp, "content-type");
669 content_string = g_mime_content_type_get_mime_type (content_type);
670 sp->string (sp, content_string);
671 g_free (content_string);
674 sp->map_key (sp, "content-disposition");
675 sp->string (sp, disposition);
679 sp->map_key (sp, "content-id");
680 sp->string (sp, cid);
684 sp->map_key (sp, "filename");
685 sp->string (sp, filename);
688 if (GMIME_IS_PART (node->part)) {
689 /* For non-HTML text parts, we include the content in the
690 * JSON. Since JSON must be Unicode, we handle charset
691 * decoding here and do not report a charset to the caller.
692 * For text/html parts, we do not include the content unless
693 * the --include-html option has been passed. If a html part
694 * is not included, it can be requested directly. This makes
695 * charset decoding the responsibility on the caller so we
696 * report the charset for text/html parts.
698 if (g_mime_content_type_is_type (content_type, "text", "*") &&
700 ! g_mime_content_type_is_type (content_type, "text", "html")))
702 GMimeStream *stream_memory = g_mime_stream_mem_new ();
703 GByteArray *part_content;
704 show_text_part_content (node->part, stream_memory, 0);
705 part_content = g_mime_stream_mem_get_byte_array (GMIME_STREAM_MEM (stream_memory));
706 sp->map_key (sp, "content");
707 sp->string_len (sp, (char *) part_content->data, part_content->len);
708 g_object_unref (stream_memory);
710 format_omitted_part_meta_sprinter (sp, meta, GMIME_PART (node->part));
712 } else if (GMIME_IS_MULTIPART (node->part)) {
713 sp->map_key (sp, "content");
716 } else if (GMIME_IS_MESSAGE (node->part)) {
717 sp->map_key (sp, "content");
721 sp->map_key (sp, "headers");
722 format_headers_sprinter (sp, GMIME_MESSAGE (node->part), false);
724 sp->map_key (sp, "body");
729 for (i = 0; i < node->nchildren; i++)
730 format_part_sprinter (ctx, sp, mime_node_child (node, i), true, include_html);
732 /* Close content structures */
733 for (i = 0; i < nclose; i++)
739 static notmuch_status_t
740 format_part_sprinter_entry (const void *ctx, sprinter_t *sp,
741 mime_node_t *node, unused (int indent),
742 const notmuch_show_params_t *params)
744 format_part_sprinter (ctx, sp, node, params->output_body, params->include_html);
746 return NOTMUCH_STATUS_SUCCESS;
749 /* Print a message in "mboxrd" format as documented, for example,
752 * http://qmail.org/qmail-manual-html/man5/mbox.html
754 static notmuch_status_t
755 format_part_mbox (const void *ctx, unused (sprinter_t *sp), mime_node_t *node,
757 unused (const notmuch_show_params_t *params))
759 notmuch_message_t *message = node->envelope_file;
761 const char *filename;
766 struct tm date_gmtime;
767 char date_asctime[26];
774 INTERNAL_ERROR ("format_part_mbox requires a root part");
776 filename = notmuch_message_get_filename (message);
777 file = gzopen (filename, "r");
779 fprintf (stderr, "Failed to open %s: %s\n",
780 filename, strerror (errno));
781 return NOTMUCH_STATUS_FILE_ERROR;
784 from = notmuch_message_get_header (message, "from");
785 from = _extract_email_address (ctx, from);
787 date = notmuch_message_get_date (message);
788 gmtime_r (&date, &date_gmtime);
789 asctime_r (&date_gmtime, date_asctime);
791 printf ("From %s %s", from, date_asctime);
793 while ((line_len = gz_getline (message, &line, &line_size, file)) != UTIL_EOF ) {
794 if (_is_from_line (line))
803 return NOTMUCH_STATUS_SUCCESS;
806 static notmuch_status_t
807 format_part_raw (unused (const void *ctx), unused (sprinter_t *sp),
808 mime_node_t *node, unused (int indent),
809 const notmuch_show_params_t *params)
811 if (node->envelope_file) {
812 /* Special case the entire message to avoid MIME parsing. */
813 const char *filename;
814 GMimeStream *stream = NULL;
817 notmuch_status_t ret = NOTMUCH_STATUS_FILE_ERROR;
819 filename = notmuch_message_get_filename (node->envelope_file);
820 if (filename == NULL) {
821 fprintf (stderr, "Error: Cannot get message filename.\n");
825 stream = g_mime_stream_gzfile_open (filename);
826 if (stream == NULL) {
827 fprintf (stderr, "Error: Cannot open file %s: %s\n", filename, strerror (errno));
831 while (! g_mime_stream_eos (stream)) {
832 ssize = g_mime_stream_read (stream, buf, sizeof(buf));
834 fprintf (stderr, "Error: Read failed from %s\n", filename);
838 if (ssize > 0 && fwrite (buf, ssize, 1, stdout) != 1) {
839 fprintf (stderr, "Error: Write %ld chars to stdout failed\n", ssize);
844 ret = NOTMUCH_STATUS_SUCCESS;
846 /* XXX This DONE is just for the special case of a node in a single file */
849 g_object_unref (stream);
854 GMimeStream *stream_filter = g_mime_stream_filter_new (params->out_stream);
856 if (GMIME_IS_PART (node->part)) {
857 /* For leaf parts, we emit only the transfer-decoded
859 GMimeDataWrapper *wrapper;
860 wrapper = g_mime_part_get_content (GMIME_PART (node->part));
862 if (wrapper && stream_filter)
863 g_mime_data_wrapper_write_to_stream (wrapper, stream_filter);
865 /* Write out the whole part. For message parts (the root
866 * part and embedded message parts), this will be the
867 * message including its headers (but not the
868 * encapsulating part's headers). For multipart parts,
869 * this will include the headers. */
871 g_mime_object_write_to_stream (node->part, NULL, stream_filter);
875 g_object_unref (stream_filter);
877 return NOTMUCH_STATUS_SUCCESS;
880 static notmuch_status_t
881 show_message (void *ctx,
882 const notmuch_show_format_t *format,
884 notmuch_message_t *message,
886 notmuch_show_params_t *params)
888 void *local = talloc_new (ctx);
889 mime_node_t *root, *part;
890 notmuch_status_t status;
891 unsigned int session_keys = 0;
892 notmuch_status_t session_key_count_error = NOTMUCH_STATUS_SUCCESS;
894 if (params->crypto.decrypt == NOTMUCH_DECRYPT_TRUE)
895 session_key_count_error = notmuch_message_count_properties (message, "session-key", &session_keys);
897 status = mime_node_open (local, message, &(params->crypto), &root);
900 part = mime_node_seek_dfs (root, (params->part < 0 ? 0 : params->part));
902 status = format->part (local, sp, part, indent, params);
903 if (params->crypto.decrypt == NOTMUCH_DECRYPT_TRUE && session_key_count_error == NOTMUCH_STATUS_SUCCESS) {
904 unsigned int new_session_keys = 0;
905 if (notmuch_message_count_properties (message, "session-key", &new_session_keys) == NOTMUCH_STATUS_SUCCESS &&
906 new_session_keys > session_keys) {
907 /* try a quiet re-indexing */
908 notmuch_indexopts_t *indexopts = notmuch_database_get_default_indexopts (notmuch_message_get_database (message));
910 notmuch_indexopts_set_decrypt_policy (indexopts, NOTMUCH_DECRYPT_AUTO);
911 print_status_message ("Error re-indexing message with --decrypt=stash",
912 message, notmuch_message_reindex (message, indexopts));
921 static notmuch_status_t
922 show_messages (void *ctx,
923 const notmuch_show_format_t *format,
925 notmuch_messages_t *messages,
927 notmuch_show_params_t *params)
929 notmuch_message_t *message;
933 notmuch_status_t status, res = NOTMUCH_STATUS_SUCCESS;
938 notmuch_messages_valid (messages);
939 notmuch_messages_move_to_next (messages))
943 message = notmuch_messages_get (messages);
945 match = notmuch_message_get_flag (message, NOTMUCH_MESSAGE_FLAG_MATCH);
946 excluded = notmuch_message_get_flag (message, NOTMUCH_MESSAGE_FLAG_EXCLUDED);
948 next_indent = indent;
950 if ((match && (!excluded || !params->omit_excluded)) || params->entire_thread) {
951 status = show_message (ctx, format, sp, message, indent, params);
954 next_indent = indent + 1;
959 status = show_messages (ctx,
961 notmuch_message_get_replies (message),
967 notmuch_message_destroy (message);
977 /* Formatted output of single message */
979 do_show_single (void *ctx,
980 notmuch_query_t *query,
981 const notmuch_show_format_t *format,
983 notmuch_show_params_t *params)
985 notmuch_messages_t *messages;
986 notmuch_message_t *message;
987 notmuch_status_t status;
990 status = notmuch_query_count_messages (query, &count);
991 if (print_status_query ("notmuch show", query, status))
995 fprintf (stderr, "Error: search term did not match precisely one message (matched %u messages).\n", count);
999 status = notmuch_query_search_messages (query, &messages);
1000 if (print_status_query ("notmuch show", query, status))
1003 message = notmuch_messages_get (messages);
1005 if (message == NULL) {
1006 fprintf (stderr, "Error: Cannot find matching message.\n");
1010 notmuch_message_set_flag (message, NOTMUCH_MESSAGE_FLAG_MATCH, 1);
1012 return show_message (ctx, format, sp, message, 0, params)
1013 != NOTMUCH_STATUS_SUCCESS;
1016 /* Formatted output of threads */
1019 notmuch_query_t *query,
1020 const notmuch_show_format_t *format,
1022 notmuch_show_params_t *params)
1024 notmuch_threads_t *threads;
1025 notmuch_thread_t *thread;
1026 notmuch_messages_t *messages;
1027 notmuch_status_t status, res = NOTMUCH_STATUS_SUCCESS;
1029 status= notmuch_query_search_threads (query, &threads);
1030 if (print_status_query ("notmuch show", query, status))
1033 sp->begin_list (sp);
1036 notmuch_threads_valid (threads);
1037 notmuch_threads_move_to_next (threads))
1039 thread = notmuch_threads_get (threads);
1041 messages = notmuch_thread_get_toplevel_messages (thread);
1043 if (messages == NULL)
1044 INTERNAL_ERROR ("Thread %s has no toplevel messages.\n",
1045 notmuch_thread_get_thread_id (thread));
1047 status = show_messages (ctx, format, sp, messages, 0, params);
1051 notmuch_thread_destroy (thread);
1057 return res != NOTMUCH_STATUS_SUCCESS;
1061 NOTMUCH_FORMAT_NOT_SPECIFIED,
1062 NOTMUCH_FORMAT_JSON,
1063 NOTMUCH_FORMAT_SEXP,
1064 NOTMUCH_FORMAT_TEXT,
1065 NOTMUCH_FORMAT_MBOX,
1069 static const notmuch_show_format_t format_json = {
1070 .new_sprinter = sprinter_json_create,
1071 .part = format_part_sprinter_entry,
1074 static const notmuch_show_format_t format_sexp = {
1075 .new_sprinter = sprinter_sexp_create,
1076 .part = format_part_sprinter_entry,
1079 static const notmuch_show_format_t format_text = {
1080 .new_sprinter = sprinter_text_create,
1081 .part = format_part_text,
1084 static const notmuch_show_format_t format_mbox = {
1085 .new_sprinter = sprinter_text_create,
1086 .part = format_part_mbox,
1089 static const notmuch_show_format_t format_raw = {
1090 .new_sprinter = sprinter_text_create,
1091 .part = format_part_raw,
1094 static const notmuch_show_format_t *formatters[] = {
1095 [NOTMUCH_FORMAT_JSON] = &format_json,
1096 [NOTMUCH_FORMAT_SEXP] = &format_sexp,
1097 [NOTMUCH_FORMAT_TEXT] = &format_text,
1098 [NOTMUCH_FORMAT_MBOX] = &format_mbox,
1099 [NOTMUCH_FORMAT_RAW] = &format_raw,
1103 notmuch_show_command (notmuch_config_t *config, int argc, char *argv[])
1105 notmuch_database_t *notmuch;
1106 notmuch_query_t *query;
1109 const notmuch_show_format_t *formatter;
1110 sprinter_t *sprinter;
1111 notmuch_show_params_t params = {
1113 .omit_excluded = true,
1114 .output_body = true,
1115 .crypto = { .decrypt = NOTMUCH_DECRYPT_AUTO },
1117 int format = NOTMUCH_FORMAT_NOT_SPECIFIED;
1118 bool exclude = true;
1119 bool entire_thread_set = false;
1120 bool single_message;
1122 notmuch_opt_desc_t options[] = {
1123 { .opt_keyword = &format, .name = "format", .keywords =
1124 (notmuch_keyword_t []){ { "json", NOTMUCH_FORMAT_JSON },
1125 { "text", NOTMUCH_FORMAT_TEXT },
1126 { "sexp", NOTMUCH_FORMAT_SEXP },
1127 { "mbox", NOTMUCH_FORMAT_MBOX },
1128 { "raw", NOTMUCH_FORMAT_RAW },
1130 { .opt_int = ¬much_format_version, .name = "format-version" },
1131 { .opt_bool = &exclude, .name = "exclude" },
1132 { .opt_bool = ¶ms.entire_thread, .name = "entire-thread",
1133 .present = &entire_thread_set },
1134 { .opt_int = ¶ms.part, .name = "part" },
1135 { .opt_keyword = (int*)(¶ms.crypto.decrypt), .name = "decrypt",
1136 .keyword_no_arg_value = "true", .keywords =
1137 (notmuch_keyword_t []){ { "false", NOTMUCH_DECRYPT_FALSE },
1138 { "auto", NOTMUCH_DECRYPT_AUTO },
1139 { "true", NOTMUCH_DECRYPT_NOSTASH },
1140 { "stash", NOTMUCH_DECRYPT_TRUE },
1142 { .opt_bool = ¶ms.crypto.verify, .name = "verify" },
1143 { .opt_bool = ¶ms.output_body, .name = "body" },
1144 { .opt_bool = ¶ms.include_html, .name = "include-html" },
1145 { .opt_inherit = notmuch_shared_options },
1149 opt_index = parse_arguments (argc, argv, options, 1);
1151 return EXIT_FAILURE;
1153 notmuch_process_shared_options (argv[0]);
1155 /* explicit decryption implies verification */
1156 if (params.crypto.decrypt == NOTMUCH_DECRYPT_NOSTASH ||
1157 params.crypto.decrypt == NOTMUCH_DECRYPT_TRUE)
1158 params.crypto.verify = true;
1160 /* specifying a part implies single message display */
1161 single_message = params.part >= 0;
1163 if (format == NOTMUCH_FORMAT_NOT_SPECIFIED) {
1164 /* if part was requested and format was not specified, use format=raw */
1165 if (params.part >= 0)
1166 format = NOTMUCH_FORMAT_RAW;
1168 format = NOTMUCH_FORMAT_TEXT;
1171 if (format == NOTMUCH_FORMAT_MBOX) {
1172 if (params.part > 0) {
1173 fprintf (stderr, "Error: specifying parts is incompatible with mbox output format.\n");
1174 return EXIT_FAILURE;
1176 } else if (format == NOTMUCH_FORMAT_RAW) {
1177 /* raw format only supports single message display */
1178 single_message = true;
1181 notmuch_exit_if_unsupported_format ();
1183 /* Default is entire-thread = false except for format=json and
1185 if (! entire_thread_set &&
1186 (format == NOTMUCH_FORMAT_JSON || format == NOTMUCH_FORMAT_SEXP))
1187 params.entire_thread = true;
1189 if (!params.output_body) {
1190 if (params.part > 0) {
1191 fprintf (stderr, "Warning: --body=false is incompatible with --part > 0. Disabling.\n");
1192 params.output_body = true;
1194 if (format != NOTMUCH_FORMAT_TEXT &&
1195 format != NOTMUCH_FORMAT_JSON &&
1196 format != NOTMUCH_FORMAT_SEXP)
1198 "Warning: --body=false only implemented for format=text, format=json and format=sexp\n");
1202 if (params.include_html &&
1203 (format != NOTMUCH_FORMAT_TEXT &&
1204 format != NOTMUCH_FORMAT_JSON &&
1205 format != NOTMUCH_FORMAT_SEXP)) {
1206 fprintf (stderr, "Warning: --include-html only implemented for format=text, format=json and format=sexp\n");
1209 query_string = query_string_from_args (config, argc-opt_index, argv+opt_index);
1210 if (query_string == NULL) {
1211 fprintf (stderr, "Out of memory\n");
1212 return EXIT_FAILURE;
1215 if (*query_string == '\0') {
1216 fprintf (stderr, "Error: notmuch show requires at least one search term.\n");
1217 return EXIT_FAILURE;
1220 notmuch_database_mode_t mode = NOTMUCH_DATABASE_MODE_READ_ONLY;
1221 if (params.crypto.decrypt == NOTMUCH_DECRYPT_TRUE)
1222 mode = NOTMUCH_DATABASE_MODE_READ_WRITE;
1223 if (notmuch_database_open (notmuch_config_get_database_path (config),
1225 return EXIT_FAILURE;
1227 notmuch_exit_if_unmatched_db_uuid (notmuch);
1229 query = notmuch_query_create (notmuch, query_string);
1230 if (query == NULL) {
1231 fprintf (stderr, "Out of memory\n");
1232 return EXIT_FAILURE;
1235 /* Create structure printer. */
1236 formatter = formatters[format];
1237 sprinter = formatter->new_sprinter(config, stdout);
1239 params.out_stream = g_mime_stream_stdout_new ();
1241 /* If a single message is requested we do not use search_excludes. */
1242 if (single_message) {
1243 ret = do_show_single (config, query, formatter, sprinter, ¶ms);
1245 /* We always apply set the exclude flag. The
1246 * exclude=true|false option controls whether or not we return
1247 * threads that only match in an excluded message */
1248 const char **search_exclude_tags;
1249 size_t search_exclude_tags_length;
1251 notmuch_status_t status;
1253 search_exclude_tags = notmuch_config_get_search_exclude_tags
1254 (config, &search_exclude_tags_length);
1256 for (i = 0; i < search_exclude_tags_length; i++) {
1257 status = notmuch_query_add_tag_exclude (query, search_exclude_tags[i]);
1258 if (status && status != NOTMUCH_STATUS_IGNORED) {
1259 print_status_query ("notmuch show", query, status);
1265 if (exclude == false) {
1266 notmuch_query_set_omit_excluded (query, false);
1267 params.omit_excluded = false;
1270 ret = do_show (config, query, formatter, sprinter, ¶ms);
1274 g_mime_stream_flush (params.out_stream);
1275 g_object_unref (params.out_stream);
1277 _notmuch_crypto_cleanup (¶ms.crypto);
1278 notmuch_query_destroy (query);
1279 notmuch_database_destroy (notmuch);
1281 return ret ? EXIT_FAILURE : EXIT_SUCCESS;