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,
199 notmuch_bool_t reply)
201 /* Any changes to the JSON or S-Expression format should be
202 * reflected in the file devel/schemata. */
204 InternetAddressList *recipients;
205 char *recipients_string;
206 const char *reply_to_string;
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_sender (message));
217 recipients = g_mime_message_get_recipients (message, GMIME_RECIPIENT_TYPE_TO);
218 recipients_string = internet_address_list_to_string (recipients, 0);
219 if (recipients_string) {
220 sp->map_key (sp, "To");
221 sp->string (sp, recipients_string);
222 g_free (recipients_string);
225 recipients = g_mime_message_get_recipients (message, GMIME_RECIPIENT_TYPE_CC);
226 recipients_string = internet_address_list_to_string (recipients, 0);
227 if (recipients_string) {
228 sp->map_key (sp, "Cc");
229 sp->string (sp, recipients_string);
230 g_free (recipients_string);
233 recipients = g_mime_message_get_recipients (message, GMIME_RECIPIENT_TYPE_BCC);
234 recipients_string = internet_address_list_to_string (recipients, 0);
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 (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 date_string = g_mime_message_get_date_as_string (message);
256 sp->string (sp, date_string);
257 g_free (date_string);
263 /* Write a MIME text part out to the given stream.
265 * If (flags & NOTMUCH_SHOW_TEXT_PART_REPLY), this prepends "> " to
268 * Both line-ending conversion (CRLF->LF) and charset conversion ( ->
269 * UTF-8) will be performed, so it is inappropriate to call this
270 * function with a non-text part. Doing so will trigger an internal
274 show_text_part_content (GMimeObject *part, GMimeStream *stream_out,
275 notmuch_show_text_part_flags flags)
277 GMimeContentType *content_type = g_mime_object_get_content_type (GMIME_OBJECT (part));
278 GMimeStream *stream_filter = NULL;
279 GMimeFilter *crlf_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_to_string (content_type));
287 if (stream_out == NULL)
290 stream_filter = g_mime_stream_filter_new (stream_out);
291 crlf_filter = g_mime_filter_crlf_new (FALSE, FALSE);
292 g_mime_stream_filter_add(GMIME_STREAM_FILTER (stream_filter),
294 g_object_unref (crlf_filter);
296 charset = g_mime_object_get_content_type_parameter (part, "charset");
298 GMimeFilter *charset_filter;
299 charset_filter = g_mime_filter_charset_new (charset, "UTF-8");
300 /* This result can be NULL for things like "unknown-8bit".
301 * Don't set a NULL filter as that makes GMime print
302 * annoying assertion-failure messages on stderr. */
303 if (charset_filter) {
304 g_mime_stream_filter_add (GMIME_STREAM_FILTER (stream_filter),
306 g_object_unref (charset_filter);
311 if (flags & NOTMUCH_SHOW_TEXT_PART_REPLY) {
312 GMimeFilter *reply_filter;
313 reply_filter = g_mime_filter_reply_new (TRUE);
315 g_mime_stream_filter_add (GMIME_STREAM_FILTER (stream_filter),
317 g_object_unref (reply_filter);
321 wrapper = g_mime_part_get_content_object (GMIME_PART (part));
322 if (wrapper && stream_filter)
323 g_mime_data_wrapper_write_to_stream (wrapper, stream_filter);
325 g_object_unref(stream_filter);
328 /* Get signature status string (GMime 2.6) */
330 signature_status_to_string (GMimeSignatureStatus x)
333 case GMIME_SIGNATURE_STATUS_GOOD:
335 case GMIME_SIGNATURE_STATUS_BAD:
337 case GMIME_SIGNATURE_STATUS_ERROR:
343 /* Signature status sprinter (GMime 2.6) */
345 format_part_sigstatus_sprinter (sprinter_t *sp, mime_node_t *node)
347 /* Any changes to the JSON or S-Expression format should be
348 * reflected in the file devel/schemata. */
350 GMimeSignatureList *siglist = node->sig_list;
360 for (i = 0; i < g_mime_signature_list_length (siglist); i++) {
361 GMimeSignature *signature = g_mime_signature_list_get_signature (siglist, i);
366 GMimeSignatureStatus status = g_mime_signature_get_status (signature);
367 sp->map_key (sp, "status");
368 sp->string (sp, signature_status_to_string (status));
370 GMimeCertificate *certificate = g_mime_signature_get_certificate (signature);
371 if (status == GMIME_SIGNATURE_STATUS_GOOD) {
373 sp->map_key (sp, "fingerprint");
374 sp->string (sp, g_mime_certificate_get_fingerprint (certificate));
376 /* these dates are seconds since the epoch; should we
377 * provide a more human-readable format string? */
378 time_t created = g_mime_signature_get_created (signature);
380 sp->map_key (sp, "created");
381 sp->integer (sp, created);
383 time_t expires = g_mime_signature_get_expires (signature);
385 sp->map_key (sp, "expires");
386 sp->integer (sp, expires);
388 /* output user id only if validity is FULL or ULTIMATE. */
389 /* note that gmime is using the term "trust" here, which
390 * is WRONG. It's actually user id "validity". */
392 const char *name = g_mime_certificate_get_name (certificate);
393 GMimeCertificateTrust trust = g_mime_certificate_get_trust (certificate);
394 if (name && (trust == GMIME_CERTIFICATE_TRUST_FULLY || trust == GMIME_CERTIFICATE_TRUST_ULTIMATE)) {
395 sp->map_key (sp, "userid");
396 sp->string (sp, name);
399 } else if (certificate) {
400 const char *key_id = g_mime_certificate_get_key_id (certificate);
402 sp->map_key (sp, "keyid");
403 sp->string (sp, key_id);
407 GMimeSignatureError errors = g_mime_signature_get_errors (signature);
408 if (errors != GMIME_SIGNATURE_ERROR_NONE) {
409 sp->map_key (sp, "errors");
410 sp->integer (sp, errors);
419 static notmuch_status_t
420 format_part_text (const void *ctx, sprinter_t *sp, mime_node_t *node,
421 int indent, const notmuch_show_params_t *params)
423 /* The disposition and content-type metadata are associated with
424 * the envelope for message parts */
425 GMimeObject *meta = node->envelope_part ?
426 GMIME_OBJECT (node->envelope_part) : node->part;
427 GMimeContentType *content_type = g_mime_object_get_content_type (meta);
428 const notmuch_bool_t leaf = GMIME_IS_PART (node->part);
429 const char *part_type;
432 if (node->envelope_file) {
433 notmuch_message_t *message = node->envelope_file;
435 part_type = "message";
436 printf ("\f%s{ id:%s depth:%d match:%d excluded:%d filename:%s\n",
438 notmuch_message_get_message_id (message),
440 notmuch_message_get_flag (message, NOTMUCH_MESSAGE_FLAG_MATCH) ? 1 : 0,
441 notmuch_message_get_flag (message, NOTMUCH_MESSAGE_FLAG_EXCLUDED) ? 1 : 0,
442 notmuch_message_get_filename (message));
444 char *content_string;
445 const char *disposition = _get_disposition (meta);
446 const char *cid = g_mime_object_get_content_id (meta);
447 const char *filename = leaf ?
448 g_mime_part_get_filename (GMIME_PART (node->part)) : NULL;
451 strcasecmp (disposition, GMIME_DISPOSITION_ATTACHMENT) == 0)
452 part_type = "attachment";
456 printf ("\f%s{ ID: %d", part_type, node->part_num);
458 printf (", Filename: %s", filename);
460 printf (", Content-id: %s", cid);
462 content_string = g_mime_content_type_to_string (content_type);
463 printf (", Content-type: %s\n", content_string);
464 g_free (content_string);
467 if (GMIME_IS_MESSAGE (node->part)) {
468 GMimeMessage *message = GMIME_MESSAGE (node->part);
469 InternetAddressList *recipients;
470 char *recipients_string;
473 printf ("\fheader{\n");
474 if (node->envelope_file)
475 printf ("%s\n", _get_one_line_summary (ctx, node->envelope_file));
476 printf ("Subject: %s\n", g_mime_message_get_subject (message));
477 printf ("From: %s\n", g_mime_message_get_sender (message));
478 recipients = g_mime_message_get_recipients (message, GMIME_RECIPIENT_TYPE_TO);
479 recipients_string = internet_address_list_to_string (recipients, 0);
480 if (recipients_string)
481 printf ("To: %s\n", recipients_string);
482 g_free (recipients_string);
483 recipients = g_mime_message_get_recipients (message, GMIME_RECIPIENT_TYPE_CC);
484 recipients_string = internet_address_list_to_string (recipients, 0);
485 if (recipients_string)
486 printf ("Cc: %s\n", recipients_string);
487 g_free (recipients_string);
488 date_string = g_mime_message_get_date_as_string (message);
489 printf ("Date: %s\n", date_string);
490 g_free (date_string);
491 printf ("\fheader}\n");
493 printf ("\fbody{\n");
497 if (g_mime_content_type_is_type (content_type, "text", "*") &&
498 !g_mime_content_type_is_type (content_type, "text", "html"))
500 GMimeStream *stream_stdout = g_mime_stream_file_new (stdout);
501 g_mime_stream_file_set_owner (GMIME_STREAM_FILE (stream_stdout), FALSE);
502 show_text_part_content (node->part, stream_stdout, 0);
503 g_object_unref(stream_stdout);
505 char *content_string = g_mime_content_type_to_string (content_type);
506 printf ("Non-text part: %s\n", content_string);
507 g_free (content_string);
511 for (i = 0; i < node->nchildren; i++)
512 format_part_text (ctx, sp, mime_node_child (node, i), indent, params);
514 if (GMIME_IS_MESSAGE (node->part))
515 printf ("\fbody}\n");
517 printf ("\f%s}\n", part_type);
519 return NOTMUCH_STATUS_SUCCESS;
523 format_omitted_part_meta_sprinter (sprinter_t *sp, GMimeObject *meta, GMimePart *part)
525 const char *content_charset = g_mime_object_get_content_type_parameter (meta, "charset");
526 const char *cte = g_mime_object_get_header (meta, "content-transfer-encoding");
527 GMimeDataWrapper *wrapper = g_mime_part_get_content_object (part);
528 GMimeStream *stream = g_mime_data_wrapper_get_stream (wrapper);
529 ssize_t content_length = g_mime_stream_length (stream);
531 if (content_charset != NULL) {
532 sp->map_key (sp, "content-charset");
533 sp->string (sp, content_charset);
536 sp->map_key (sp, "content-transfer-encoding");
537 sp->string (sp, cte);
539 if (content_length >= 0) {
540 sp->map_key (sp, "content-length");
541 sp->integer (sp, content_length);
546 format_part_sprinter (const void *ctx, sprinter_t *sp, mime_node_t *node,
547 notmuch_bool_t first, notmuch_bool_t output_body,
548 notmuch_bool_t include_html)
550 /* Any changes to the JSON or S-Expression format should be
551 * reflected in the file devel/schemata. */
553 if (node->envelope_file) {
555 format_message_sprinter (sp, node->envelope_file);
557 sp->map_key (sp, "headers");
558 format_headers_sprinter (sp, GMIME_MESSAGE (node->part), FALSE);
561 sp->map_key (sp, "body");
563 format_part_sprinter (ctx, sp, mime_node_child (node, 0), first, TRUE, include_html);
570 /* The disposition and content-type metadata are associated with
571 * the envelope for message parts */
572 GMimeObject *meta = node->envelope_part ?
573 GMIME_OBJECT (node->envelope_part) : node->part;
574 GMimeContentType *content_type = g_mime_object_get_content_type (meta);
575 char *content_string;
576 const char *disposition = _get_disposition (meta);
577 const char *cid = g_mime_object_get_content_id (meta);
578 const char *filename = GMIME_IS_PART (node->part) ?
579 g_mime_part_get_filename (GMIME_PART (node->part)) : NULL;
585 sp->map_key (sp, "id");
586 sp->integer (sp, node->part_num);
588 if (node->decrypt_attempted) {
589 sp->map_key (sp, "encstatus");
592 sp->map_key (sp, "status");
593 sp->string (sp, node->decrypt_success ? "good" : "bad");
598 if (node->verify_attempted) {
599 sp->map_key (sp, "sigstatus");
600 format_part_sigstatus_sprinter (sp, node);
603 sp->map_key (sp, "content-type");
604 content_string = g_mime_content_type_to_string (content_type);
605 sp->string (sp, content_string);
606 g_free (content_string);
609 sp->map_key (sp, "content-disposition");
610 sp->string (sp, disposition);
614 sp->map_key (sp, "content-id");
615 sp->string (sp, cid);
619 sp->map_key (sp, "filename");
620 sp->string (sp, filename);
623 if (GMIME_IS_PART (node->part)) {
624 /* For non-HTML text parts, we include the content in the
625 * JSON. Since JSON must be Unicode, we handle charset
626 * decoding here and do not report a charset to the caller.
627 * For text/html parts, we do not include the content unless
628 * the --include-html option has been passed. If a html part
629 * is not included, it can be requested directly. This makes
630 * charset decoding the responsibility on the caller so we
631 * report the charset for text/html parts.
633 if (g_mime_content_type_is_type (content_type, "text", "*") &&
635 ! g_mime_content_type_is_type (content_type, "text", "html")))
637 GMimeStream *stream_memory = g_mime_stream_mem_new ();
638 GByteArray *part_content;
639 show_text_part_content (node->part, stream_memory, 0);
640 part_content = g_mime_stream_mem_get_byte_array (GMIME_STREAM_MEM (stream_memory));
641 sp->map_key (sp, "content");
642 sp->string_len (sp, (char *) part_content->data, part_content->len);
643 g_object_unref (stream_memory);
645 format_omitted_part_meta_sprinter (sp, meta, GMIME_PART (node->part));
647 } else if (GMIME_IS_MULTIPART (node->part)) {
648 sp->map_key (sp, "content");
651 } else if (GMIME_IS_MESSAGE (node->part)) {
652 sp->map_key (sp, "content");
656 sp->map_key (sp, "headers");
657 format_headers_sprinter (sp, GMIME_MESSAGE (node->part), FALSE);
659 sp->map_key (sp, "body");
664 for (i = 0; i < node->nchildren; i++)
665 format_part_sprinter (ctx, sp, mime_node_child (node, i), i == 0, TRUE, include_html);
667 /* Close content structures */
668 for (i = 0; i < nclose; i++)
674 static notmuch_status_t
675 format_part_sprinter_entry (const void *ctx, sprinter_t *sp,
676 mime_node_t *node, unused (int indent),
677 const notmuch_show_params_t *params)
679 format_part_sprinter (ctx, sp, node, TRUE, params->output_body, params->include_html);
681 return NOTMUCH_STATUS_SUCCESS;
684 /* Print a message in "mboxrd" format as documented, for example,
687 * http://qmail.org/qmail-manual-html/man5/mbox.html
689 static notmuch_status_t
690 format_part_mbox (const void *ctx, unused (sprinter_t *sp), mime_node_t *node,
692 unused (const notmuch_show_params_t *params))
694 notmuch_message_t *message = node->envelope_file;
696 const char *filename;
701 struct tm date_gmtime;
702 char date_asctime[26];
709 INTERNAL_ERROR ("format_part_mbox requires a root part");
711 filename = notmuch_message_get_filename (message);
712 file = fopen (filename, "r");
714 fprintf (stderr, "Failed to open %s: %s\n",
715 filename, strerror (errno));
716 return NOTMUCH_STATUS_FILE_ERROR;
719 from = notmuch_message_get_header (message, "from");
720 from = _extract_email_address (ctx, from);
722 date = notmuch_message_get_date (message);
723 gmtime_r (&date, &date_gmtime);
724 asctime_r (&date_gmtime, date_asctime);
726 printf ("From %s %s", from, date_asctime);
728 while ((line_len = getline (&line, &line_size, file)) != -1 ) {
729 if (_is_from_line (line))
738 return NOTMUCH_STATUS_SUCCESS;
741 static notmuch_status_t
742 format_part_raw (unused (const void *ctx), unused (sprinter_t *sp),
743 mime_node_t *node, unused (int indent),
744 unused (const notmuch_show_params_t *params))
746 if (node->envelope_file) {
747 /* Special case the entire message to avoid MIME parsing. */
748 const char *filename;
753 filename = notmuch_message_get_filename (node->envelope_file);
754 if (filename == NULL) {
755 fprintf (stderr, "Error: Cannot get message filename.\n");
756 return NOTMUCH_STATUS_FILE_ERROR;
759 file = fopen (filename, "r");
761 fprintf (stderr, "Error: Cannot open file %s: %s\n", filename, strerror (errno));
762 return NOTMUCH_STATUS_FILE_ERROR;
765 while (!feof (file)) {
766 size = fread (buf, 1, sizeof (buf), file);
768 fprintf (stderr, "Error: Read failed from %s\n", filename);
770 return NOTMUCH_STATUS_FILE_ERROR;
773 if (fwrite (buf, size, 1, stdout) != 1) {
774 fprintf (stderr, "Error: Write failed\n");
776 return NOTMUCH_STATUS_FILE_ERROR;
781 return NOTMUCH_STATUS_SUCCESS;
784 GMimeStream *stream_stdout;
785 GMimeStream *stream_filter = NULL;
787 stream_stdout = g_mime_stream_file_new (stdout);
788 g_mime_stream_file_set_owner (GMIME_STREAM_FILE (stream_stdout), FALSE);
790 stream_filter = g_mime_stream_filter_new (stream_stdout);
792 if (GMIME_IS_PART (node->part)) {
793 /* For leaf parts, we emit only the transfer-decoded
795 GMimeDataWrapper *wrapper;
796 wrapper = g_mime_part_get_content_object (GMIME_PART (node->part));
798 if (wrapper && stream_filter)
799 g_mime_data_wrapper_write_to_stream (wrapper, stream_filter);
801 /* Write out the whole part. For message parts (the root
802 * part and embedded message parts), this will be the
803 * message including its headers (but not the
804 * encapsulating part's headers). For multipart parts,
805 * this will include the headers. */
807 g_mime_object_write_to_stream (node->part, stream_filter);
811 g_object_unref (stream_filter);
814 g_object_unref(stream_stdout);
816 return NOTMUCH_STATUS_SUCCESS;
819 static notmuch_status_t
820 show_message (void *ctx,
821 const notmuch_show_format_t *format,
823 notmuch_message_t *message,
825 notmuch_show_params_t *params)
827 void *local = talloc_new (ctx);
828 mime_node_t *root, *part;
829 notmuch_status_t status;
831 status = mime_node_open (local, message, &(params->crypto), &root);
834 part = mime_node_seek_dfs (root, (params->part < 0 ? 0 : params->part));
836 status = format->part (local, sp, part, indent, params);
842 static notmuch_status_t
843 show_messages (void *ctx,
844 const notmuch_show_format_t *format,
846 notmuch_messages_t *messages,
848 notmuch_show_params_t *params)
850 notmuch_message_t *message;
851 notmuch_bool_t match;
852 notmuch_bool_t excluded;
854 notmuch_status_t status, res = NOTMUCH_STATUS_SUCCESS;
859 notmuch_messages_valid (messages);
860 notmuch_messages_move_to_next (messages))
864 message = notmuch_messages_get (messages);
866 match = notmuch_message_get_flag (message, NOTMUCH_MESSAGE_FLAG_MATCH);
867 excluded = notmuch_message_get_flag (message, NOTMUCH_MESSAGE_FLAG_EXCLUDED);
869 next_indent = indent;
871 if ((match && (!excluded || !params->omit_excluded)) || params->entire_thread) {
872 status = show_message (ctx, format, sp, message, indent, params);
875 next_indent = indent + 1;
880 status = show_messages (ctx,
882 notmuch_message_get_replies (message),
888 notmuch_message_destroy (message);
898 /* Formatted output of single message */
900 do_show_single (void *ctx,
901 notmuch_query_t *query,
902 const notmuch_show_format_t *format,
904 notmuch_show_params_t *params)
906 notmuch_messages_t *messages;
907 notmuch_message_t *message;
908 notmuch_status_t status;
911 status = notmuch_query_count_messages_st (query, &count);
912 if (print_status_query ("notmuch show", query, status))
916 fprintf (stderr, "Error: search term did not match precisely one message (matched %d messages).\n", count);
920 status = notmuch_query_search_messages_st (query, &messages);
921 if (print_status_query ("notmuch show", query, status))
924 message = notmuch_messages_get (messages);
926 if (message == NULL) {
927 fprintf (stderr, "Error: Cannot find matching message.\n");
931 notmuch_message_set_flag (message, NOTMUCH_MESSAGE_FLAG_MATCH, 1);
933 return show_message (ctx, format, sp, message, 0, params)
934 != NOTMUCH_STATUS_SUCCESS;
937 /* Formatted output of threads */
940 notmuch_query_t *query,
941 const notmuch_show_format_t *format,
943 notmuch_show_params_t *params)
945 notmuch_threads_t *threads;
946 notmuch_thread_t *thread;
947 notmuch_messages_t *messages;
948 notmuch_status_t status, res = NOTMUCH_STATUS_SUCCESS;
950 status= notmuch_query_search_threads_st (query, &threads);
951 if (print_status_query ("notmuch show", query, status))
957 notmuch_threads_valid (threads);
958 notmuch_threads_move_to_next (threads))
960 thread = notmuch_threads_get (threads);
962 messages = notmuch_thread_get_toplevel_messages (thread);
964 if (messages == NULL)
965 INTERNAL_ERROR ("Thread %s has no toplevel messages.\n",
966 notmuch_thread_get_thread_id (thread));
968 status = show_messages (ctx, format, sp, messages, 0, params);
972 notmuch_thread_destroy (thread);
978 return res != NOTMUCH_STATUS_SUCCESS;
982 NOTMUCH_FORMAT_NOT_SPECIFIED,
990 static const notmuch_show_format_t format_json = {
991 .new_sprinter = sprinter_json_create,
992 .part = format_part_sprinter_entry,
995 static const notmuch_show_format_t format_sexp = {
996 .new_sprinter = sprinter_sexp_create,
997 .part = format_part_sprinter_entry,
1000 static const notmuch_show_format_t format_text = {
1001 .new_sprinter = sprinter_text_create,
1002 .part = format_part_text,
1005 static const notmuch_show_format_t format_mbox = {
1006 .new_sprinter = sprinter_text_create,
1007 .part = format_part_mbox,
1010 static const notmuch_show_format_t format_raw = {
1011 .new_sprinter = sprinter_text_create,
1012 .part = format_part_raw,
1015 static const notmuch_show_format_t *formatters[] = {
1016 [NOTMUCH_FORMAT_JSON] = &format_json,
1017 [NOTMUCH_FORMAT_SEXP] = &format_sexp,
1018 [NOTMUCH_FORMAT_TEXT] = &format_text,
1019 [NOTMUCH_FORMAT_MBOX] = &format_mbox,
1020 [NOTMUCH_FORMAT_RAW] = &format_raw,
1024 ENTIRE_THREAD_DEFAULT = -1,
1025 ENTIRE_THREAD_FALSE = FALSE,
1026 ENTIRE_THREAD_TRUE = TRUE,
1029 /* The following is to allow future options to be added more easily */
1036 notmuch_show_command (notmuch_config_t *config, int argc, char *argv[])
1038 notmuch_database_t *notmuch;
1039 notmuch_query_t *query;
1042 const notmuch_show_format_t *formatter;
1043 sprinter_t *sprinter;
1044 notmuch_show_params_t params = {
1046 .omit_excluded = TRUE,
1047 .output_body = TRUE,
1049 int format = NOTMUCH_FORMAT_NOT_SPECIFIED;
1050 int exclude = EXCLUDE_TRUE;
1051 int entire_thread = ENTIRE_THREAD_DEFAULT;
1052 notmuch_bool_t single_message;
1054 notmuch_opt_desc_t options[] = {
1055 { NOTMUCH_OPT_KEYWORD, &format, "format", 'f',
1056 (notmuch_keyword_t []){ { "json", NOTMUCH_FORMAT_JSON },
1057 { "text", NOTMUCH_FORMAT_TEXT },
1058 { "sexp", NOTMUCH_FORMAT_SEXP },
1059 { "mbox", NOTMUCH_FORMAT_MBOX },
1060 { "raw", NOTMUCH_FORMAT_RAW },
1062 { NOTMUCH_OPT_INT, ¬much_format_version, "format-version", 0, 0 },
1063 { NOTMUCH_OPT_KEYWORD, &exclude, "exclude", 'x',
1064 (notmuch_keyword_t []){ { "true", EXCLUDE_TRUE },
1065 { "false", EXCLUDE_FALSE },
1067 { NOTMUCH_OPT_KEYWORD, &entire_thread, "entire-thread", 't',
1068 (notmuch_keyword_t []){ { "true", ENTIRE_THREAD_TRUE },
1069 { "false", ENTIRE_THREAD_FALSE },
1070 { "", ENTIRE_THREAD_TRUE },
1072 { NOTMUCH_OPT_INT, ¶ms.part, "part", 'p', 0 },
1073 { NOTMUCH_OPT_BOOLEAN, ¶ms.crypto.decrypt, "decrypt", 'd', 0 },
1074 { NOTMUCH_OPT_BOOLEAN, ¶ms.crypto.verify, "verify", 'v', 0 },
1075 { NOTMUCH_OPT_BOOLEAN, ¶ms.output_body, "body", 'b', 0 },
1076 { NOTMUCH_OPT_BOOLEAN, ¶ms.include_html, "include-html", 0, 0 },
1077 { NOTMUCH_OPT_INHERIT, (void *) ¬much_shared_options, NULL, 0, 0 },
1081 opt_index = parse_arguments (argc, argv, options, 1);
1083 return EXIT_FAILURE;
1085 notmuch_process_shared_options (argv[0]);
1087 /* decryption implies verification */
1088 if (params.crypto.decrypt)
1089 params.crypto.verify = TRUE;
1091 /* specifying a part implies single message display */
1092 single_message = params.part >= 0;
1094 if (format == NOTMUCH_FORMAT_NOT_SPECIFIED) {
1095 /* if part was requested and format was not specified, use format=raw */
1096 if (params.part >= 0)
1097 format = NOTMUCH_FORMAT_RAW;
1099 format = NOTMUCH_FORMAT_TEXT;
1102 if (format == NOTMUCH_FORMAT_MBOX) {
1103 if (params.part > 0) {
1104 fprintf (stderr, "Error: specifying parts is incompatible with mbox output format.\n");
1105 return EXIT_FAILURE;
1107 } else if (format == NOTMUCH_FORMAT_RAW) {
1108 /* raw format only supports single message display */
1109 single_message = TRUE;
1112 notmuch_exit_if_unsupported_format ();
1114 /* Default is entire-thread = FALSE except for format=json and
1116 if (entire_thread == ENTIRE_THREAD_DEFAULT) {
1117 if (format == NOTMUCH_FORMAT_JSON || format == NOTMUCH_FORMAT_SEXP)
1118 params.entire_thread = TRUE;
1120 params.entire_thread = FALSE;
1122 params.entire_thread = entire_thread;
1125 if (!params.output_body) {
1126 if (params.part > 0) {
1127 fprintf (stderr, "Warning: --body=false is incompatible with --part > 0. Disabling.\n");
1128 params.output_body = TRUE;
1130 if (format != NOTMUCH_FORMAT_JSON && format != NOTMUCH_FORMAT_SEXP)
1132 "Warning: --body=false only implemented for format=json and format=sexp\n");
1136 if (params.include_html &&
1137 (format != NOTMUCH_FORMAT_JSON && format != NOTMUCH_FORMAT_SEXP)) {
1138 fprintf (stderr, "Warning: --include-html only implemented for format=json and format=sexp\n");
1141 query_string = query_string_from_args (config, argc-opt_index, argv+opt_index);
1142 if (query_string == NULL) {
1143 fprintf (stderr, "Out of memory\n");
1144 return EXIT_FAILURE;
1147 if (*query_string == '\0') {
1148 fprintf (stderr, "Error: notmuch show requires at least one search term.\n");
1149 return EXIT_FAILURE;
1152 params.crypto.gpgpath = notmuch_config_get_crypto_gpg_path (config);
1154 if (notmuch_database_open (notmuch_config_get_database_path (config),
1155 NOTMUCH_DATABASE_MODE_READ_ONLY, ¬much))
1156 return EXIT_FAILURE;
1158 notmuch_exit_if_unmatched_db_uuid (notmuch);
1160 query = notmuch_query_create (notmuch, query_string);
1161 if (query == NULL) {
1162 fprintf (stderr, "Out of memory\n");
1163 return EXIT_FAILURE;
1166 /* Create structure printer. */
1167 formatter = formatters[format];
1168 sprinter = formatter->new_sprinter(config, stdout);
1170 /* If a single message is requested we do not use search_excludes. */
1171 if (single_message) {
1172 ret = do_show_single (config, query, formatter, sprinter, ¶ms);
1174 /* We always apply set the exclude flag. The
1175 * exclude=true|false option controls whether or not we return
1176 * threads that only match in an excluded message */
1177 const char **search_exclude_tags;
1178 size_t search_exclude_tags_length;
1181 search_exclude_tags = notmuch_config_get_search_exclude_tags
1182 (config, &search_exclude_tags_length);
1183 for (i = 0; i < search_exclude_tags_length; i++)
1184 notmuch_query_add_tag_exclude (query, search_exclude_tags[i]);
1186 if (exclude == EXCLUDE_FALSE) {
1187 notmuch_query_set_omit_excluded (query, FALSE);
1188 params.omit_excluded = FALSE;
1191 ret = do_show (config, query, formatter, sprinter, ¶ms);
1194 notmuch_crypto_cleanup (¶ms.crypto);
1195 notmuch_query_destroy (query);
1196 notmuch_database_destroy (notmuch);
1198 return ret ? EXIT_FAILURE : EXIT_SUCCESS;