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"
26 _get_tags_as_string (const void *ctx, notmuch_message_t *message)
33 result = talloc_strdup (ctx, "");
37 for (tags = notmuch_message_get_tags (message);
38 notmuch_tags_valid (tags);
39 notmuch_tags_move_to_next (tags))
41 tag = notmuch_tags_get (tags);
43 result = talloc_asprintf_append (result, "%s%s",
44 first ? "" : " ", tag);
51 /* Get a nice, single-line summary of message. */
53 _get_one_line_summary (const void *ctx, notmuch_message_t *message)
57 const char *relative_date;
60 from = notmuch_message_get_header (message, "from");
62 date = notmuch_message_get_date (message);
63 relative_date = notmuch_time_relative_date (ctx, date);
65 tags = _get_tags_as_string (ctx, message);
67 return talloc_asprintf (ctx, "%s (%s) (%s)",
68 from, relative_date, tags);
71 static const char *_get_disposition(GMimeObject *meta)
73 GMimeContentDisposition *disposition;
75 disposition = g_mime_object_get_content_disposition (meta);
79 return g_mime_content_disposition_get_disposition (disposition);
82 /* Emit a sequence of key/value pairs for the metadata of message.
83 * The caller should begin a map before calling this. */
85 format_message_sprinter (sprinter_t *sp, notmuch_message_t *message)
87 /* Any changes to the JSON or S-Expression format should be
88 * reflected in the file devel/schemata. */
90 void *local = talloc_new (NULL);
93 const char *relative_date;
95 sp->map_key (sp, "id");
96 sp->string (sp, notmuch_message_get_message_id (message));
98 sp->map_key (sp, "match");
99 sp->boolean (sp, notmuch_message_get_flag (message, NOTMUCH_MESSAGE_FLAG_MATCH));
101 sp->map_key (sp, "excluded");
102 sp->boolean (sp, notmuch_message_get_flag (message, NOTMUCH_MESSAGE_FLAG_EXCLUDED));
104 sp->map_key (sp, "filename");
105 if (notmuch_format_version >= 3) {
106 notmuch_filenames_t *filenames;
109 for (filenames = notmuch_message_get_filenames (message);
110 notmuch_filenames_valid (filenames);
111 notmuch_filenames_move_to_next (filenames)) {
112 sp->string (sp, notmuch_filenames_get (filenames));
114 notmuch_filenames_destroy (filenames);
117 sp->string (sp, notmuch_message_get_filename (message));
120 sp->map_key (sp, "timestamp");
121 date = notmuch_message_get_date (message);
122 sp->integer (sp, date);
124 sp->map_key (sp, "date_relative");
125 relative_date = notmuch_time_relative_date (local, date);
126 sp->string (sp, relative_date);
128 sp->map_key (sp, "tags");
130 for (tags = notmuch_message_get_tags (message);
131 notmuch_tags_valid (tags);
132 notmuch_tags_move_to_next (tags))
133 sp->string (sp, notmuch_tags_get (tags));
139 /* Extract just the email address from the contents of a From:
142 _extract_email_address (const void *ctx, const char *from)
144 InternetAddressList *addresses;
145 InternetAddress *address;
146 InternetAddressMailbox *mailbox;
147 const char *email = "MAILER-DAEMON";
149 addresses = internet_address_list_parse_string (from);
151 /* Bail if there is no address here. */
152 if (addresses == NULL || internet_address_list_length (addresses) < 1)
155 /* Otherwise, just use the first address. */
156 address = internet_address_list_get_address (addresses, 0);
158 /* The From header should never contain an address group rather
159 * than a mailbox. So bail if it does. */
160 if (! INTERNET_ADDRESS_IS_MAILBOX (address))
163 mailbox = INTERNET_ADDRESS_MAILBOX (address);
164 email = internet_address_mailbox_get_addr (mailbox);
165 email = talloc_strdup (ctx, email);
169 g_object_unref (addresses);
174 /* Return 1 if 'line' is an mbox From_ line---that is, a line
175 * beginning with zero or more '>' characters followed by the
176 * characters 'F', 'r', 'o', 'm', and space.
178 * Any characters at all may appear after that in the line.
181 _is_from_line (const char *line)
183 const char *s = line;
191 if (STRNCMP_LITERAL (s, "From ") == 0)
198 format_headers_sprinter (sprinter_t *sp, GMimeMessage *message,
201 /* Any changes to the JSON or S-Expression format should be
202 * reflected in the file devel/schemata. */
204 char *recipients_string;
205 const char *reply_to_string;
206 void *local = talloc_new (sp);
210 sp->map_key (sp, "Subject");
211 sp->string (sp, g_mime_message_get_subject (message));
213 sp->map_key (sp, "From");
214 sp->string (sp, g_mime_message_get_from_string (message));
216 recipients_string = g_mime_message_get_address_string (message, GMIME_ADDRESS_TYPE_TO);
217 if (recipients_string) {
218 sp->map_key (sp, "To");
219 sp->string (sp, recipients_string);
220 g_free (recipients_string);
223 recipients_string = g_mime_message_get_address_string (message, GMIME_ADDRESS_TYPE_CC);
224 if (recipients_string) {
225 sp->map_key (sp, "Cc");
226 sp->string (sp, recipients_string);
227 g_free (recipients_string);
230 recipients_string = g_mime_message_get_address_string (message, GMIME_ADDRESS_TYPE_BCC);
231 if (recipients_string) {
232 sp->map_key (sp, "Bcc");
233 sp->string (sp, recipients_string);
234 g_free (recipients_string);
237 reply_to_string = g_mime_message_get_reply_to_string (local, message);
238 if (reply_to_string) {
239 sp->map_key (sp, "Reply-To");
240 sp->string (sp, reply_to_string);
244 sp->map_key (sp, "In-reply-to");
245 sp->string (sp, g_mime_object_get_header (GMIME_OBJECT (message), "In-reply-to"));
247 sp->map_key (sp, "References");
248 sp->string (sp, g_mime_object_get_header (GMIME_OBJECT (message), "References"));
250 sp->map_key (sp, "Date");
251 sp->string (sp, g_mime_message_get_date_string (sp, message));
258 /* Write a MIME text part out to the given stream.
260 * If (flags & NOTMUCH_SHOW_TEXT_PART_REPLY), this prepends "> " to
263 * Both line-ending conversion (CRLF->LF) and charset conversion ( ->
264 * UTF-8) will be performed, so it is inappropriate to call this
265 * function with a non-text part. Doing so will trigger an internal
269 show_text_part_content (GMimeObject *part, GMimeStream *stream_out,
270 notmuch_show_text_part_flags flags)
272 GMimeContentType *content_type = g_mime_object_get_content_type (GMIME_OBJECT (part));
273 GMimeStream *stream_filter = NULL;
274 GMimeFilter *crlf_filter = NULL;
275 GMimeDataWrapper *wrapper;
278 if (! g_mime_content_type_is_type (content_type, "text", "*"))
279 INTERNAL_ERROR ("Illegal request to format non-text part (%s) as text.",
280 g_mime_content_type_to_string (content_type));
282 if (stream_out == NULL)
285 stream_filter = g_mime_stream_filter_new (stream_out);
286 crlf_filter = g_mime_filter_crlf_new (false, false);
287 g_mime_stream_filter_add(GMIME_STREAM_FILTER (stream_filter),
289 g_object_unref (crlf_filter);
291 charset = g_mime_object_get_content_type_parameter (part, "charset");
293 GMimeFilter *charset_filter;
294 charset_filter = g_mime_filter_charset_new (charset, "UTF-8");
295 /* This result can be NULL for things like "unknown-8bit".
296 * Don't set a NULL filter as that makes GMime print
297 * annoying assertion-failure messages on stderr. */
298 if (charset_filter) {
299 g_mime_stream_filter_add (GMIME_STREAM_FILTER (stream_filter),
301 g_object_unref (charset_filter);
306 if (flags & NOTMUCH_SHOW_TEXT_PART_REPLY) {
307 GMimeFilter *reply_filter;
308 reply_filter = g_mime_filter_reply_new (true);
310 g_mime_stream_filter_add (GMIME_STREAM_FILTER (stream_filter),
312 g_object_unref (reply_filter);
316 wrapper = g_mime_part_get_content_object (GMIME_PART (part));
317 if (wrapper && stream_filter)
318 g_mime_data_wrapper_write_to_stream (wrapper, stream_filter);
320 g_object_unref(stream_filter);
324 signature_status_to_string (GMimeSignatureStatus status)
326 if (g_mime_signature_status_bad (status))
329 if (g_mime_signature_status_error (status))
332 if (g_mime_signature_status_good (status))
338 /* Print signature flags */
339 struct key_map_struct {
340 GMimeSignatureError bit;
345 do_format_signature_errors (sprinter_t *sp, struct key_map_struct *key_map,
346 unsigned int array_map_len, GMimeSignatureError errors) {
347 sp->map_key (sp, "errors");
350 for (unsigned int i = 0; i < array_map_len; i++) {
351 if (errors & key_map[i].bit) {
352 sp->map_key (sp, key_map[i].string);
353 sp->boolean (sp, true);
360 #if (GMIME_MAJOR_VERSION < 3)
362 format_signature_errors (sprinter_t *sp, GMimeSignature *signature)
364 GMimeSignatureError errors = g_mime_signature_get_errors (signature);
366 if (errors == GMIME_SIGNATURE_ERROR_NONE)
369 struct key_map_struct key_map[] = {
370 { GMIME_SIGNATURE_ERROR_EXPSIG, "sig-expired" },
371 { GMIME_SIGNATURE_ERROR_NO_PUBKEY, "key-missing"},
372 { GMIME_SIGNATURE_ERROR_EXPKEYSIG, "key-expired"},
373 { GMIME_SIGNATURE_ERROR_REVKEYSIG, "key-revoked"},
374 { GMIME_SIGNATURE_ERROR_UNSUPP_ALGO, "alg-unsupported"},
377 do_format_signature_errors (sp, key_map, ARRAY_SIZE(key_map), errors);
381 format_signature_errors (sprinter_t *sp, GMimeSignature *signature)
383 GMimeSignatureError errors = g_mime_signature_get_errors (signature);
385 if (!(errors & GMIME_SIGNATURE_STATUS_ERROR_MASK))
388 struct key_map_struct key_map[] = {
389 { GMIME_SIGNATURE_STATUS_KEY_REVOKED, "key-revoked"},
390 { GMIME_SIGNATURE_STATUS_KEY_EXPIRED, "key-expired"},
391 { GMIME_SIGNATURE_STATUS_SIG_EXPIRED, "sig-expired" },
392 { GMIME_SIGNATURE_STATUS_KEY_MISSING, "key-missing"},
393 { GMIME_SIGNATURE_STATUS_CRL_MISSING, "crl-missing"},
394 { GMIME_SIGNATURE_STATUS_CRL_TOO_OLD, "crl-too-old"},
395 { GMIME_SIGNATURE_STATUS_BAD_POLICY, "bad-policy"},
396 { GMIME_SIGNATURE_STATUS_SYS_ERROR, "sys-error"},
397 { GMIME_SIGNATURE_STATUS_TOFU_CONFLICT, "tofu-conflict"},
400 do_format_signature_errors (sp, key_map, ARRAY_SIZE(key_map), errors);
404 /* Signature status sprinter (GMime 2.6) */
406 format_part_sigstatus_sprinter (sprinter_t *sp, mime_node_t *node)
408 /* Any changes to the JSON or S-Expression format should be
409 * reflected in the file devel/schemata. */
411 GMimeSignatureList *siglist = node->sig_list;
421 for (i = 0; i < g_mime_signature_list_length (siglist); i++) {
422 GMimeSignature *signature = g_mime_signature_list_get_signature (siglist, i);
427 GMimeSignatureStatus status = g_mime_signature_get_status (signature);
428 sp->map_key (sp, "status");
429 sp->string (sp, signature_status_to_string (status));
431 GMimeCertificate *certificate = g_mime_signature_get_certificate (signature);
432 if (g_mime_signature_status_good (status)) {
434 sp->map_key (sp, "fingerprint");
435 sp->string (sp, g_mime_certificate_get_fingerprint (certificate));
437 /* these dates are seconds since the epoch; should we
438 * provide a more human-readable format string? */
439 time_t created = g_mime_signature_get_created (signature);
441 sp->map_key (sp, "created");
442 sp->integer (sp, created);
444 time_t expires = g_mime_signature_get_expires (signature);
446 sp->map_key (sp, "expires");
447 sp->integer (sp, expires);
450 const char *uid = g_mime_certificate_get_valid_userid (certificate);
452 sp->map_key (sp, "userid");
453 sp->string (sp, uid);
456 } else if (certificate) {
457 const char *key_id = g_mime_certificate_get_fpr16 (certificate);
459 sp->map_key (sp, "keyid");
460 sp->string (sp, key_id);
464 if (notmuch_format_version <= 3) {
465 GMimeSignatureError errors = g_mime_signature_get_errors (signature);
466 if (g_mime_signature_status_error (errors)) {
467 sp->map_key (sp, "errors");
468 sp->integer (sp, errors);
471 format_signature_errors (sp, signature);
480 static notmuch_status_t
481 format_part_text (const void *ctx, sprinter_t *sp, mime_node_t *node,
482 int indent, const notmuch_show_params_t *params)
484 /* The disposition and content-type metadata are associated with
485 * the envelope for message parts */
486 GMimeObject *meta = node->envelope_part ?
487 GMIME_OBJECT (node->envelope_part) : node->part;
488 GMimeContentType *content_type = g_mime_object_get_content_type (meta);
489 const bool leaf = GMIME_IS_PART (node->part);
490 GMimeStream *stream = params->out_stream;
491 const char *part_type;
494 if (node->envelope_file) {
495 notmuch_message_t *message = node->envelope_file;
497 part_type = "message";
498 g_mime_stream_printf (stream, "\f%s{ id:%s depth:%d match:%d excluded:%d filename:%s\n",
500 notmuch_message_get_message_id (message),
502 notmuch_message_get_flag (message, NOTMUCH_MESSAGE_FLAG_MATCH) ? 1 : 0,
503 notmuch_message_get_flag (message, NOTMUCH_MESSAGE_FLAG_EXCLUDED) ? 1 : 0,
504 notmuch_message_get_filename (message));
506 char *content_string;
507 const char *disposition = _get_disposition (meta);
508 const char *cid = g_mime_object_get_content_id (meta);
509 const char *filename = leaf ?
510 g_mime_part_get_filename (GMIME_PART (node->part)) : NULL;
513 strcasecmp (disposition, GMIME_DISPOSITION_ATTACHMENT) == 0)
514 part_type = "attachment";
518 g_mime_stream_printf (stream, "\f%s{ ID: %d", part_type, node->part_num);
520 g_mime_stream_printf (stream, ", Filename: %s", filename);
522 g_mime_stream_printf (stream, ", Content-id: %s", cid);
524 content_string = g_mime_content_type_to_string (content_type);
525 g_mime_stream_printf (stream, ", Content-type: %s\n", content_string);
526 g_free (content_string);
529 if (GMIME_IS_MESSAGE (node->part)) {
530 GMimeMessage *message = GMIME_MESSAGE (node->part);
531 char *recipients_string;
534 g_mime_stream_printf (stream, "\fheader{\n");
535 if (node->envelope_file)
536 g_mime_stream_printf (stream, "%s\n", _get_one_line_summary (ctx, node->envelope_file));
537 g_mime_stream_printf (stream, "Subject: %s\n", g_mime_message_get_subject (message));
538 g_mime_stream_printf (stream, "From: %s\n", g_mime_message_get_from_string (message));
539 recipients_string = g_mime_message_get_address_string (message, GMIME_ADDRESS_TYPE_TO);
540 if (recipients_string)
541 g_mime_stream_printf (stream, "To: %s\n", recipients_string);
542 g_free (recipients_string);
543 recipients_string = g_mime_message_get_address_string (message, GMIME_ADDRESS_TYPE_CC);
544 if (recipients_string)
545 g_mime_stream_printf (stream, "Cc: %s\n", recipients_string);
546 g_free (recipients_string);
547 date_string = g_mime_message_get_date_string (node, message);
548 g_mime_stream_printf (stream, "Date: %s\n", date_string);
549 g_mime_stream_printf (stream, "\fheader}\n");
551 g_mime_stream_printf (stream, "\fbody{\n");
555 if (g_mime_content_type_is_type (content_type, "text", "*") &&
556 !g_mime_content_type_is_type (content_type, "text", "html"))
558 show_text_part_content (node->part, stream, 0);
560 char *content_string = g_mime_content_type_to_string (content_type);
561 g_mime_stream_printf (stream, "Non-text part: %s\n", content_string);
562 g_free (content_string);
566 for (i = 0; i < node->nchildren; i++)
567 format_part_text (ctx, sp, mime_node_child (node, i), indent, params);
569 if (GMIME_IS_MESSAGE (node->part))
570 g_mime_stream_printf (stream, "\fbody}\n");
572 g_mime_stream_printf (stream, "\f%s}\n", part_type);
574 return NOTMUCH_STATUS_SUCCESS;
578 format_omitted_part_meta_sprinter (sprinter_t *sp, GMimeObject *meta, GMimePart *part)
580 const char *content_charset = g_mime_object_get_content_type_parameter (meta, "charset");
581 const char *cte = g_mime_object_get_header (meta, "content-transfer-encoding");
582 GMimeDataWrapper *wrapper = g_mime_part_get_content_object (part);
583 GMimeStream *stream = g_mime_data_wrapper_get_stream (wrapper);
584 ssize_t content_length = g_mime_stream_length (stream);
586 if (content_charset != NULL) {
587 sp->map_key (sp, "content-charset");
588 sp->string (sp, content_charset);
591 sp->map_key (sp, "content-transfer-encoding");
592 sp->string (sp, cte);
594 if (content_length >= 0) {
595 sp->map_key (sp, "content-length");
596 sp->integer (sp, content_length);
601 format_part_sprinter (const void *ctx, sprinter_t *sp, mime_node_t *node,
605 /* Any changes to the JSON or S-Expression format should be
606 * reflected in the file devel/schemata. */
608 if (node->envelope_file) {
610 format_message_sprinter (sp, node->envelope_file);
612 sp->map_key (sp, "headers");
613 format_headers_sprinter (sp, GMIME_MESSAGE (node->part), false);
616 sp->map_key (sp, "body");
618 format_part_sprinter (ctx, sp, mime_node_child (node, 0), true, include_html);
625 /* The disposition and content-type metadata are associated with
626 * the envelope for message parts */
627 GMimeObject *meta = node->envelope_part ?
628 GMIME_OBJECT (node->envelope_part) : node->part;
629 GMimeContentType *content_type = g_mime_object_get_content_type (meta);
630 char *content_string;
631 const char *disposition = _get_disposition (meta);
632 const char *cid = g_mime_object_get_content_id (meta);
633 const char *filename = GMIME_IS_PART (node->part) ?
634 g_mime_part_get_filename (GMIME_PART (node->part)) : NULL;
640 sp->map_key (sp, "id");
641 sp->integer (sp, node->part_num);
643 if (node->decrypt_attempted) {
644 sp->map_key (sp, "encstatus");
647 sp->map_key (sp, "status");
648 sp->string (sp, node->decrypt_success ? "good" : "bad");
653 if (node->verify_attempted) {
654 sp->map_key (sp, "sigstatus");
655 format_part_sigstatus_sprinter (sp, node);
658 sp->map_key (sp, "content-type");
659 content_string = g_mime_content_type_to_string (content_type);
660 sp->string (sp, content_string);
661 g_free (content_string);
664 sp->map_key (sp, "content-disposition");
665 sp->string (sp, disposition);
669 sp->map_key (sp, "content-id");
670 sp->string (sp, cid);
674 sp->map_key (sp, "filename");
675 sp->string (sp, filename);
678 if (GMIME_IS_PART (node->part)) {
679 /* For non-HTML text parts, we include the content in the
680 * JSON. Since JSON must be Unicode, we handle charset
681 * decoding here and do not report a charset to the caller.
682 * For text/html parts, we do not include the content unless
683 * the --include-html option has been passed. If a html part
684 * is not included, it can be requested directly. This makes
685 * charset decoding the responsibility on the caller so we
686 * report the charset for text/html parts.
688 if (g_mime_content_type_is_type (content_type, "text", "*") &&
690 ! g_mime_content_type_is_type (content_type, "text", "html")))
692 GMimeStream *stream_memory = g_mime_stream_mem_new ();
693 GByteArray *part_content;
694 show_text_part_content (node->part, stream_memory, 0);
695 part_content = g_mime_stream_mem_get_byte_array (GMIME_STREAM_MEM (stream_memory));
696 sp->map_key (sp, "content");
697 sp->string_len (sp, (char *) part_content->data, part_content->len);
698 g_object_unref (stream_memory);
700 format_omitted_part_meta_sprinter (sp, meta, GMIME_PART (node->part));
702 } else if (GMIME_IS_MULTIPART (node->part)) {
703 sp->map_key (sp, "content");
706 } else if (GMIME_IS_MESSAGE (node->part)) {
707 sp->map_key (sp, "content");
711 sp->map_key (sp, "headers");
712 format_headers_sprinter (sp, GMIME_MESSAGE (node->part), false);
714 sp->map_key (sp, "body");
719 for (i = 0; i < node->nchildren; i++)
720 format_part_sprinter (ctx, sp, mime_node_child (node, i), true, include_html);
722 /* Close content structures */
723 for (i = 0; i < nclose; i++)
729 static notmuch_status_t
730 format_part_sprinter_entry (const void *ctx, sprinter_t *sp,
731 mime_node_t *node, unused (int indent),
732 const notmuch_show_params_t *params)
734 format_part_sprinter (ctx, sp, node, params->output_body, params->include_html);
736 return NOTMUCH_STATUS_SUCCESS;
739 /* Print a message in "mboxrd" format as documented, for example,
742 * http://qmail.org/qmail-manual-html/man5/mbox.html
744 static notmuch_status_t
745 format_part_mbox (const void *ctx, unused (sprinter_t *sp), mime_node_t *node,
747 unused (const notmuch_show_params_t *params))
749 notmuch_message_t *message = node->envelope_file;
751 const char *filename;
756 struct tm date_gmtime;
757 char date_asctime[26];
764 INTERNAL_ERROR ("format_part_mbox requires a root part");
766 filename = notmuch_message_get_filename (message);
767 file = fopen (filename, "r");
769 fprintf (stderr, "Failed to open %s: %s\n",
770 filename, strerror (errno));
771 return NOTMUCH_STATUS_FILE_ERROR;
774 from = notmuch_message_get_header (message, "from");
775 from = _extract_email_address (ctx, from);
777 date = notmuch_message_get_date (message);
778 gmtime_r (&date, &date_gmtime);
779 asctime_r (&date_gmtime, date_asctime);
781 printf ("From %s %s", from, date_asctime);
783 while ((line_len = getline (&line, &line_size, file)) != -1 ) {
784 if (_is_from_line (line))
793 return NOTMUCH_STATUS_SUCCESS;
796 static notmuch_status_t
797 format_part_raw (unused (const void *ctx), unused (sprinter_t *sp),
798 mime_node_t *node, unused (int indent),
799 const notmuch_show_params_t *params)
801 if (node->envelope_file) {
802 /* Special case the entire message to avoid MIME parsing. */
803 const char *filename;
808 filename = notmuch_message_get_filename (node->envelope_file);
809 if (filename == NULL) {
810 fprintf (stderr, "Error: Cannot get message filename.\n");
811 return NOTMUCH_STATUS_FILE_ERROR;
814 file = fopen (filename, "r");
816 fprintf (stderr, "Error: Cannot open file %s: %s\n", filename, strerror (errno));
817 return NOTMUCH_STATUS_FILE_ERROR;
820 while (!feof (file)) {
821 size = fread (buf, 1, sizeof (buf), file);
823 fprintf (stderr, "Error: Read failed from %s\n", filename);
825 return NOTMUCH_STATUS_FILE_ERROR;
828 if (fwrite (buf, size, 1, stdout) != 1) {
829 fprintf (stderr, "Error: Write failed\n");
831 return NOTMUCH_STATUS_FILE_ERROR;
836 return NOTMUCH_STATUS_SUCCESS;
839 GMimeStream *stream_filter = g_mime_stream_filter_new (params->out_stream);
841 if (GMIME_IS_PART (node->part)) {
842 /* For leaf parts, we emit only the transfer-decoded
844 GMimeDataWrapper *wrapper;
845 wrapper = g_mime_part_get_content_object (GMIME_PART (node->part));
847 if (wrapper && stream_filter)
848 g_mime_data_wrapper_write_to_stream (wrapper, stream_filter);
850 /* Write out the whole part. For message parts (the root
851 * part and embedded message parts), this will be the
852 * message including its headers (but not the
853 * encapsulating part's headers). For multipart parts,
854 * this will include the headers. */
856 g_mime_object_write_to_stream (node->part, stream_filter);
860 g_object_unref (stream_filter);
862 return NOTMUCH_STATUS_SUCCESS;
865 static notmuch_status_t
866 show_message (void *ctx,
867 const notmuch_show_format_t *format,
869 notmuch_message_t *message,
871 notmuch_show_params_t *params)
873 void *local = talloc_new (ctx);
874 mime_node_t *root, *part;
875 notmuch_status_t status;
876 unsigned int session_keys = 0;
877 notmuch_status_t session_key_count_error = NOTMUCH_STATUS_SUCCESS;
879 if (params->crypto.decrypt == NOTMUCH_DECRYPT_TRUE)
880 session_key_count_error = notmuch_message_count_properties (message, "session-key", &session_keys);
882 status = mime_node_open (local, message, &(params->crypto), &root);
885 part = mime_node_seek_dfs (root, (params->part < 0 ? 0 : params->part));
887 status = format->part (local, sp, part, indent, params);
888 #if HAVE_GMIME_SESSION_KEYS
889 if (params->crypto.decrypt == NOTMUCH_DECRYPT_TRUE && session_key_count_error == NOTMUCH_STATUS_SUCCESS) {
890 unsigned int new_session_keys = 0;
891 if (notmuch_message_count_properties (message, "session-key", &new_session_keys) == NOTMUCH_STATUS_SUCCESS &&
892 new_session_keys > session_keys) {
893 /* try a quiet re-indexing */
894 notmuch_indexopts_t *indexopts = notmuch_database_get_default_indexopts (notmuch_message_get_database (message));
896 notmuch_indexopts_set_decrypt_policy (indexopts, NOTMUCH_DECRYPT_AUTO);
897 print_status_message ("Error re-indexing message with --decrypt=stash",
898 message, notmuch_message_reindex (message, indexopts));
908 static notmuch_status_t
909 show_messages (void *ctx,
910 const notmuch_show_format_t *format,
912 notmuch_messages_t *messages,
914 notmuch_show_params_t *params)
916 notmuch_message_t *message;
920 notmuch_status_t status, res = NOTMUCH_STATUS_SUCCESS;
925 notmuch_messages_valid (messages);
926 notmuch_messages_move_to_next (messages))
930 message = notmuch_messages_get (messages);
932 match = notmuch_message_get_flag (message, NOTMUCH_MESSAGE_FLAG_MATCH);
933 excluded = notmuch_message_get_flag (message, NOTMUCH_MESSAGE_FLAG_EXCLUDED);
935 next_indent = indent;
937 if ((match && (!excluded || !params->omit_excluded)) || params->entire_thread) {
938 status = show_message (ctx, format, sp, message, indent, params);
941 next_indent = indent + 1;
946 status = show_messages (ctx,
948 notmuch_message_get_replies (message),
954 notmuch_message_destroy (message);
964 /* Formatted output of single message */
966 do_show_single (void *ctx,
967 notmuch_query_t *query,
968 const notmuch_show_format_t *format,
970 notmuch_show_params_t *params)
972 notmuch_messages_t *messages;
973 notmuch_message_t *message;
974 notmuch_status_t status;
977 status = notmuch_query_count_messages (query, &count);
978 if (print_status_query ("notmuch show", query, status))
982 fprintf (stderr, "Error: search term did not match precisely one message (matched %u messages).\n", count);
986 status = notmuch_query_search_messages (query, &messages);
987 if (print_status_query ("notmuch show", query, status))
990 message = notmuch_messages_get (messages);
992 if (message == NULL) {
993 fprintf (stderr, "Error: Cannot find matching message.\n");
997 notmuch_message_set_flag (message, NOTMUCH_MESSAGE_FLAG_MATCH, 1);
999 return show_message (ctx, format, sp, message, 0, params)
1000 != NOTMUCH_STATUS_SUCCESS;
1003 /* Formatted output of threads */
1006 notmuch_query_t *query,
1007 const notmuch_show_format_t *format,
1009 notmuch_show_params_t *params)
1011 notmuch_threads_t *threads;
1012 notmuch_thread_t *thread;
1013 notmuch_messages_t *messages;
1014 notmuch_status_t status, res = NOTMUCH_STATUS_SUCCESS;
1016 status= notmuch_query_search_threads (query, &threads);
1017 if (print_status_query ("notmuch show", query, status))
1020 sp->begin_list (sp);
1023 notmuch_threads_valid (threads);
1024 notmuch_threads_move_to_next (threads))
1026 thread = notmuch_threads_get (threads);
1028 messages = notmuch_thread_get_toplevel_messages (thread);
1030 if (messages == NULL)
1031 INTERNAL_ERROR ("Thread %s has no toplevel messages.\n",
1032 notmuch_thread_get_thread_id (thread));
1034 status = show_messages (ctx, format, sp, messages, 0, params);
1038 notmuch_thread_destroy (thread);
1044 return res != NOTMUCH_STATUS_SUCCESS;
1048 NOTMUCH_FORMAT_NOT_SPECIFIED,
1049 NOTMUCH_FORMAT_JSON,
1050 NOTMUCH_FORMAT_SEXP,
1051 NOTMUCH_FORMAT_TEXT,
1052 NOTMUCH_FORMAT_MBOX,
1056 static const notmuch_show_format_t format_json = {
1057 .new_sprinter = sprinter_json_create,
1058 .part = format_part_sprinter_entry,
1061 static const notmuch_show_format_t format_sexp = {
1062 .new_sprinter = sprinter_sexp_create,
1063 .part = format_part_sprinter_entry,
1066 static const notmuch_show_format_t format_text = {
1067 .new_sprinter = sprinter_text_create,
1068 .part = format_part_text,
1071 static const notmuch_show_format_t format_mbox = {
1072 .new_sprinter = sprinter_text_create,
1073 .part = format_part_mbox,
1076 static const notmuch_show_format_t format_raw = {
1077 .new_sprinter = sprinter_text_create,
1078 .part = format_part_raw,
1081 static const notmuch_show_format_t *formatters[] = {
1082 [NOTMUCH_FORMAT_JSON] = &format_json,
1083 [NOTMUCH_FORMAT_SEXP] = &format_sexp,
1084 [NOTMUCH_FORMAT_TEXT] = &format_text,
1085 [NOTMUCH_FORMAT_MBOX] = &format_mbox,
1086 [NOTMUCH_FORMAT_RAW] = &format_raw,
1090 notmuch_show_command (notmuch_config_t *config, int argc, char *argv[])
1092 notmuch_database_t *notmuch;
1093 notmuch_query_t *query;
1096 const notmuch_show_format_t *formatter;
1097 sprinter_t *sprinter;
1098 notmuch_show_params_t params = {
1100 .omit_excluded = true,
1101 .output_body = true,
1102 .crypto = { .decrypt = NOTMUCH_DECRYPT_AUTO },
1104 int format = NOTMUCH_FORMAT_NOT_SPECIFIED;
1105 bool exclude = true;
1106 bool entire_thread_set = false;
1107 bool single_message;
1109 notmuch_opt_desc_t options[] = {
1110 { .opt_keyword = &format, .name = "format", .keywords =
1111 (notmuch_keyword_t []){ { "json", NOTMUCH_FORMAT_JSON },
1112 { "text", NOTMUCH_FORMAT_TEXT },
1113 { "sexp", NOTMUCH_FORMAT_SEXP },
1114 { "mbox", NOTMUCH_FORMAT_MBOX },
1115 { "raw", NOTMUCH_FORMAT_RAW },
1117 { .opt_int = ¬much_format_version, .name = "format-version" },
1118 { .opt_bool = &exclude, .name = "exclude" },
1119 { .opt_bool = ¶ms.entire_thread, .name = "entire-thread",
1120 .present = &entire_thread_set },
1121 { .opt_int = ¶ms.part, .name = "part" },
1122 { .opt_keyword = (int*)(¶ms.crypto.decrypt), .name = "decrypt",
1123 .keyword_no_arg_value = "true", .keywords =
1124 (notmuch_keyword_t []){ { "false", NOTMUCH_DECRYPT_FALSE },
1125 { "auto", NOTMUCH_DECRYPT_AUTO },
1126 { "true", NOTMUCH_DECRYPT_NOSTASH },
1127 { "stash", NOTMUCH_DECRYPT_TRUE },
1129 { .opt_bool = ¶ms.crypto.verify, .name = "verify" },
1130 { .opt_bool = ¶ms.output_body, .name = "body" },
1131 { .opt_bool = ¶ms.include_html, .name = "include-html" },
1132 { .opt_inherit = notmuch_shared_options },
1136 opt_index = parse_arguments (argc, argv, options, 1);
1138 return EXIT_FAILURE;
1140 notmuch_process_shared_options (argv[0]);
1142 /* explicit decryption implies verification */
1143 if (params.crypto.decrypt == NOTMUCH_DECRYPT_NOSTASH ||
1144 params.crypto.decrypt == NOTMUCH_DECRYPT_TRUE)
1145 params.crypto.verify = true;
1147 /* specifying a part implies single message display */
1148 single_message = params.part >= 0;
1150 if (format == NOTMUCH_FORMAT_NOT_SPECIFIED) {
1151 /* if part was requested and format was not specified, use format=raw */
1152 if (params.part >= 0)
1153 format = NOTMUCH_FORMAT_RAW;
1155 format = NOTMUCH_FORMAT_TEXT;
1158 if (format == NOTMUCH_FORMAT_MBOX) {
1159 if (params.part > 0) {
1160 fprintf (stderr, "Error: specifying parts is incompatible with mbox output format.\n");
1161 return EXIT_FAILURE;
1163 } else if (format == NOTMUCH_FORMAT_RAW) {
1164 /* raw format only supports single message display */
1165 single_message = true;
1168 notmuch_exit_if_unsupported_format ();
1170 /* Default is entire-thread = false except for format=json and
1172 if (! entire_thread_set &&
1173 (format == NOTMUCH_FORMAT_JSON || format == NOTMUCH_FORMAT_SEXP))
1174 params.entire_thread = true;
1176 if (!params.output_body) {
1177 if (params.part > 0) {
1178 fprintf (stderr, "Warning: --body=false is incompatible with --part > 0. Disabling.\n");
1179 params.output_body = true;
1181 if (format != NOTMUCH_FORMAT_JSON && format != NOTMUCH_FORMAT_SEXP)
1183 "Warning: --body=false only implemented for format=json and format=sexp\n");
1187 if (params.include_html &&
1188 (format != NOTMUCH_FORMAT_JSON && format != NOTMUCH_FORMAT_SEXP)) {
1189 fprintf (stderr, "Warning: --include-html only implemented for format=json and format=sexp\n");
1192 query_string = query_string_from_args (config, argc-opt_index, argv+opt_index);
1193 if (query_string == NULL) {
1194 fprintf (stderr, "Out of memory\n");
1195 return EXIT_FAILURE;
1198 if (*query_string == '\0') {
1199 fprintf (stderr, "Error: notmuch show requires at least one search term.\n");
1200 return EXIT_FAILURE;
1203 #if (GMIME_MAJOR_VERSION < 3)
1204 params.crypto.gpgpath = notmuch_config_get_crypto_gpg_path (config);
1207 notmuch_database_mode_t mode = NOTMUCH_DATABASE_MODE_READ_ONLY;
1208 if (params.crypto.decrypt == NOTMUCH_DECRYPT_TRUE)
1209 mode = NOTMUCH_DATABASE_MODE_READ_WRITE;
1210 if (notmuch_database_open (notmuch_config_get_database_path (config),
1212 return EXIT_FAILURE;
1214 notmuch_exit_if_unmatched_db_uuid (notmuch);
1216 query = notmuch_query_create (notmuch, query_string);
1217 if (query == NULL) {
1218 fprintf (stderr, "Out of memory\n");
1219 return EXIT_FAILURE;
1222 /* Create structure printer. */
1223 formatter = formatters[format];
1224 sprinter = formatter->new_sprinter(config, stdout);
1226 params.out_stream = g_mime_stream_stdout_new ();
1228 /* If a single message is requested we do not use search_excludes. */
1229 if (single_message) {
1230 ret = do_show_single (config, query, formatter, sprinter, ¶ms);
1232 /* We always apply set the exclude flag. The
1233 * exclude=true|false option controls whether or not we return
1234 * threads that only match in an excluded message */
1235 const char **search_exclude_tags;
1236 size_t search_exclude_tags_length;
1238 notmuch_status_t status;
1240 search_exclude_tags = notmuch_config_get_search_exclude_tags
1241 (config, &search_exclude_tags_length);
1243 for (i = 0; i < search_exclude_tags_length; i++) {
1244 status = notmuch_query_add_tag_exclude (query, search_exclude_tags[i]);
1245 if (status && status != NOTMUCH_STATUS_IGNORED) {
1246 print_status_query ("notmuch show", query, status);
1252 if (exclude == false) {
1253 notmuch_query_set_omit_excluded (query, false);
1254 params.omit_excluded = false;
1257 ret = do_show (config, query, formatter, sprinter, ¶ms);
1261 g_mime_stream_flush (params.out_stream);
1262 g_object_unref (params.out_stream);
1264 _notmuch_crypto_cleanup (¶ms.crypto);
1265 notmuch_query_destroy (query);
1266 notmuch_database_destroy (notmuch);
1268 return ret ? EXIT_FAILURE : EXIT_SUCCESS;