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 http://www.gnu.org/licenses/ .
18 * Author: Carl Worth <cworth@cworth.org>
21 #include "notmuch-client.h"
22 #include "gmime-filter-reply.h"
25 static notmuch_status_t
26 format_part_text (const void *ctx, mime_node_t *node,
27 int indent, const notmuch_show_params_t *params);
29 static const notmuch_show_format_t format_text = {
30 .new_sprinter = sprinter_text_create,
31 .part = format_part_text,
34 static notmuch_status_t
35 format_part_json_entry (const void *ctx, mime_node_t *node,
36 int indent, const notmuch_show_params_t *params);
38 static const notmuch_show_format_t format_json = {
39 .new_sprinter = sprinter_json_create,
40 .message_set_start = "[",
41 .part = format_part_json_entry,
42 .message_set_sep = ", ",
43 .message_set_end = "]",
44 .null_message = "null"
47 static notmuch_status_t
48 format_part_mbox (const void *ctx, mime_node_t *node,
49 int indent, const notmuch_show_params_t *params);
51 static const notmuch_show_format_t format_mbox = {
52 .new_sprinter = sprinter_text_create,
53 .part = format_part_mbox,
56 static notmuch_status_t
57 format_part_raw (unused (const void *ctx), mime_node_t *node,
59 unused (const notmuch_show_params_t *params));
61 static const notmuch_show_format_t format_raw = {
62 .new_sprinter = sprinter_text_create,
63 .part = format_part_raw,
67 _get_tags_as_string (const void *ctx, notmuch_message_t *message)
74 result = talloc_strdup (ctx, "");
78 for (tags = notmuch_message_get_tags (message);
79 notmuch_tags_valid (tags);
80 notmuch_tags_move_to_next (tags))
82 tag = notmuch_tags_get (tags);
84 result = talloc_asprintf_append (result, "%s%s",
85 first ? "" : " ", tag);
92 /* Get a nice, single-line summary of message. */
94 _get_one_line_summary (const void *ctx, notmuch_message_t *message)
98 const char *relative_date;
101 from = notmuch_message_get_header (message, "from");
103 date = notmuch_message_get_date (message);
104 relative_date = notmuch_time_relative_date (ctx, date);
106 tags = _get_tags_as_string (ctx, message);
108 return talloc_asprintf (ctx, "%s (%s) (%s)",
109 from, relative_date, tags);
113 format_message_json (const void *ctx, notmuch_message_t *message)
115 notmuch_tags_t *tags;
117 void *ctx_quote = talloc_new (ctx);
119 const char *relative_date;
121 date = notmuch_message_get_date (message);
122 relative_date = notmuch_time_relative_date (ctx, date);
124 printf ("\"id\": %s, \"match\": %s, \"excluded\": %s, \"filename\": %s, \"timestamp\": %ld, \"date_relative\": \"%s\", \"tags\": [",
125 json_quote_str (ctx_quote, notmuch_message_get_message_id (message)),
126 notmuch_message_get_flag (message, NOTMUCH_MESSAGE_FLAG_MATCH) ? "true" : "false",
127 notmuch_message_get_flag (message, NOTMUCH_MESSAGE_FLAG_EXCLUDED) ? "true" : "false",
128 json_quote_str (ctx_quote, notmuch_message_get_filename (message)),
129 date, relative_date);
131 for (tags = notmuch_message_get_tags (message);
132 notmuch_tags_valid (tags);
133 notmuch_tags_move_to_next (tags))
135 printf("%s%s", first ? "" : ",",
136 json_quote_str (ctx_quote, notmuch_tags_get (tags)));
140 talloc_free (ctx_quote);
143 /* Extract just the email address from the contents of a From:
146 _extract_email_address (const void *ctx, const char *from)
148 InternetAddressList *addresses;
149 InternetAddress *address;
150 InternetAddressMailbox *mailbox;
151 const char *email = "MAILER-DAEMON";
153 addresses = internet_address_list_parse_string (from);
155 /* Bail if there is no address here. */
156 if (addresses == NULL || internet_address_list_length (addresses) < 1)
159 /* Otherwise, just use the first address. */
160 address = internet_address_list_get_address (addresses, 0);
162 /* The From header should never contain an address group rather
163 * than a mailbox. So bail if it does. */
164 if (! INTERNET_ADDRESS_IS_MAILBOX (address))
167 mailbox = INTERNET_ADDRESS_MAILBOX (address);
168 email = internet_address_mailbox_get_addr (mailbox);
169 email = talloc_strdup (ctx, email);
173 g_object_unref (addresses);
178 /* Return 1 if 'line' is an mbox From_ line---that is, a line
179 * beginning with zero or more '>' characters followed by the
180 * characters 'F', 'r', 'o', 'm', and space.
182 * Any characters at all may appear after that in the line.
185 _is_from_line (const char *line)
187 const char *s = line;
195 if (STRNCMP_LITERAL (s, "From ") == 0)
202 format_headers_json (const void *ctx, GMimeMessage *message, notmuch_bool_t reply)
204 void *local = talloc_new (ctx);
205 InternetAddressList *recipients;
206 const char *recipients_string;
209 json_quote_str (local, "Subject"),
210 json_quote_str (local, g_mime_message_get_subject (message)));
212 json_quote_str (local, "From"),
213 json_quote_str (local, g_mime_message_get_sender (message)));
214 recipients = g_mime_message_get_recipients (message, GMIME_RECIPIENT_TYPE_TO);
215 recipients_string = internet_address_list_to_string (recipients, 0);
216 if (recipients_string)
218 json_quote_str (local, "To"),
219 json_quote_str (local, recipients_string));
220 recipients = g_mime_message_get_recipients (message, GMIME_RECIPIENT_TYPE_CC);
221 recipients_string = internet_address_list_to_string (recipients, 0);
222 if (recipients_string)
224 json_quote_str (local, "Cc"),
225 json_quote_str (local, recipients_string));
229 json_quote_str (local, "In-reply-to"),
230 json_quote_str (local, g_mime_object_get_header (GMIME_OBJECT (message), "In-reply-to")));
233 json_quote_str (local, "References"),
234 json_quote_str (local, g_mime_object_get_header (GMIME_OBJECT (message), "References")));
237 json_quote_str (local, "Date"),
238 json_quote_str (local, g_mime_message_get_date_as_string (message)));
246 /* Write a MIME text part out to the given stream.
248 * If (flags & NOTMUCH_SHOW_TEXT_PART_REPLY), this prepends "> " to
251 * Both line-ending conversion (CRLF->LF) and charset conversion ( ->
252 * UTF-8) will be performed, so it is inappropriate to call this
253 * function with a non-text part. Doing so will trigger an internal
257 show_text_part_content (GMimeObject *part, GMimeStream *stream_out,
258 notmuch_show_text_part_flags flags)
260 GMimeContentType *content_type = g_mime_object_get_content_type (GMIME_OBJECT (part));
261 GMimeStream *stream_filter = NULL;
262 GMimeDataWrapper *wrapper;
265 if (! g_mime_content_type_is_type (content_type, "text", "*"))
266 INTERNAL_ERROR ("Illegal request to format non-text part (%s) as text.",
267 g_mime_content_type_to_string (content_type));
269 if (stream_out == NULL)
272 stream_filter = g_mime_stream_filter_new (stream_out);
273 g_mime_stream_filter_add(GMIME_STREAM_FILTER (stream_filter),
274 g_mime_filter_crlf_new (FALSE, FALSE));
276 charset = g_mime_object_get_content_type_parameter (part, "charset");
278 GMimeFilter *charset_filter;
279 charset_filter = g_mime_filter_charset_new (charset, "UTF-8");
280 /* This result can be NULL for things like "unknown-8bit".
281 * Don't set a NULL filter as that makes GMime print
282 * annoying assertion-failure messages on stderr. */
283 if (charset_filter) {
284 g_mime_stream_filter_add (GMIME_STREAM_FILTER (stream_filter),
286 g_object_unref (charset_filter);
291 if (flags & NOTMUCH_SHOW_TEXT_PART_REPLY) {
292 GMimeFilter *reply_filter;
293 reply_filter = g_mime_filter_reply_new (TRUE);
295 g_mime_stream_filter_add (GMIME_STREAM_FILTER (stream_filter),
297 g_object_unref (reply_filter);
301 wrapper = g_mime_part_get_content_object (GMIME_PART (part));
302 if (wrapper && stream_filter)
303 g_mime_data_wrapper_write_to_stream (wrapper, stream_filter);
305 g_object_unref(stream_filter);
308 #ifdef GMIME_ATLEAST_26
310 signature_status_to_string (GMimeSignatureStatus x)
313 case GMIME_SIGNATURE_STATUS_GOOD:
315 case GMIME_SIGNATURE_STATUS_BAD:
317 case GMIME_SIGNATURE_STATUS_ERROR:
324 signer_status_to_string (GMimeSignerStatus x)
327 case GMIME_SIGNER_STATUS_NONE:
329 case GMIME_SIGNER_STATUS_GOOD:
331 case GMIME_SIGNER_STATUS_BAD:
333 case GMIME_SIGNER_STATUS_ERROR:
340 #ifdef GMIME_ATLEAST_26
342 format_part_sigstatus_json (mime_node_t *node)
344 GMimeSignatureList *siglist = node->sig_list;
353 void *ctx_quote = talloc_new (NULL);
355 for (i = 0; i < g_mime_signature_list_length (siglist); i++) {
356 GMimeSignature *signature = g_mime_signature_list_get_signature (siglist, i);
364 GMimeSignatureStatus status = g_mime_signature_get_status (signature);
365 printf ("\"status\": %s",
366 json_quote_str (ctx_quote,
367 signature_status_to_string (status)));
369 GMimeCertificate *certificate = g_mime_signature_get_certificate (signature);
370 if (status == GMIME_SIGNATURE_STATUS_GOOD) {
372 printf (", \"fingerprint\": %s", json_quote_str (ctx_quote, g_mime_certificate_get_fingerprint (certificate)));
373 /* these dates are seconds since the epoch; should we
374 * provide a more human-readable format string? */
375 time_t created = g_mime_signature_get_created (signature);
377 printf (", \"created\": %d", (int) created);
378 time_t expires = g_mime_signature_get_expires (signature);
380 printf (", \"expires\": %d", (int) expires);
381 /* output user id only if validity is FULL or ULTIMATE. */
382 /* note that gmime is using the term "trust" here, which
383 * is WRONG. It's actually user id "validity". */
385 const char *name = g_mime_certificate_get_name (certificate);
386 GMimeCertificateTrust trust = g_mime_certificate_get_trust (certificate);
387 if (name && (trust == GMIME_CERTIFICATE_TRUST_FULLY || trust == GMIME_CERTIFICATE_TRUST_ULTIMATE))
388 printf (", \"userid\": %s", json_quote_str (ctx_quote, name));
390 } else if (certificate) {
391 const char *key_id = g_mime_certificate_get_key_id (certificate);
393 printf (", \"keyid\": %s", json_quote_str (ctx_quote, key_id));
396 GMimeSignatureError errors = g_mime_signature_get_errors (signature);
397 if (errors != GMIME_SIGNATURE_ERROR_NONE) {
398 printf (", \"errors\": %d", errors);
406 talloc_free (ctx_quote);
410 format_part_sigstatus_json (mime_node_t *node)
412 const GMimeSignatureValidity* validity = node->sig_validity;
421 const GMimeSigner *signer = g_mime_signature_validity_get_signers (validity);
423 void *ctx_quote = talloc_new (NULL);
434 printf ("\"status\": %s",
435 json_quote_str (ctx_quote,
436 signer_status_to_string (signer->status)));
438 if (signer->status == GMIME_SIGNER_STATUS_GOOD)
440 if (signer->fingerprint)
441 printf (", \"fingerprint\": %s", json_quote_str (ctx_quote, signer->fingerprint));
442 /* these dates are seconds since the epoch; should we
443 * provide a more human-readable format string? */
445 printf (", \"created\": %d", (int) signer->created);
447 printf (", \"expires\": %d", (int) signer->expires);
448 /* output user id only if validity is FULL or ULTIMATE. */
449 /* note that gmime is using the term "trust" here, which
450 * is WRONG. It's actually user id "validity". */
451 if ((signer->name) && (signer->trust)) {
452 if ((signer->trust == GMIME_SIGNER_TRUST_FULLY) || (signer->trust == GMIME_SIGNER_TRUST_ULTIMATE))
453 printf (", \"userid\": %s", json_quote_str (ctx_quote, signer->name));
457 printf (", \"keyid\": %s", json_quote_str (ctx_quote, signer->keyid));
459 if (signer->errors != GMIME_SIGNER_ERROR_NONE) {
460 printf (", \"errors\": %d", signer->errors);
464 signer = signer->next;
469 talloc_free (ctx_quote);
473 static notmuch_status_t
474 format_part_text (const void *ctx, mime_node_t *node,
475 int indent, const notmuch_show_params_t *params)
477 /* The disposition and content-type metadata are associated with
478 * the envelope for message parts */
479 GMimeObject *meta = node->envelope_part ?
480 GMIME_OBJECT (node->envelope_part) : node->part;
481 GMimeContentType *content_type = g_mime_object_get_content_type (meta);
482 const notmuch_bool_t leaf = GMIME_IS_PART (node->part);
483 const char *part_type;
486 if (node->envelope_file) {
487 notmuch_message_t *message = node->envelope_file;
489 part_type = "message";
490 printf ("\f%s{ id:%s depth:%d match:%d excluded:%d filename:%s\n",
492 notmuch_message_get_message_id (message),
494 notmuch_message_get_flag (message, NOTMUCH_MESSAGE_FLAG_MATCH) ? 1 : 0,
495 notmuch_message_get_flag (message, NOTMUCH_MESSAGE_FLAG_EXCLUDED) ? 1 : 0,
496 notmuch_message_get_filename (message));
498 GMimeContentDisposition *disposition = g_mime_object_get_content_disposition (meta);
499 const char *cid = g_mime_object_get_content_id (meta);
500 const char *filename = leaf ?
501 g_mime_part_get_filename (GMIME_PART (node->part)) : NULL;
504 strcmp (disposition->disposition, GMIME_DISPOSITION_ATTACHMENT) == 0)
505 part_type = "attachment";
509 printf ("\f%s{ ID: %d", part_type, node->part_num);
511 printf (", Filename: %s", filename);
513 printf (", Content-id: %s", cid);
514 printf (", Content-type: %s\n", g_mime_content_type_to_string (content_type));
517 if (GMIME_IS_MESSAGE (node->part)) {
518 GMimeMessage *message = GMIME_MESSAGE (node->part);
519 InternetAddressList *recipients;
520 const char *recipients_string;
522 printf ("\fheader{\n");
523 if (node->envelope_file)
524 printf ("%s\n", _get_one_line_summary (ctx, node->envelope_file));
525 printf ("Subject: %s\n", g_mime_message_get_subject (message));
526 printf ("From: %s\n", g_mime_message_get_sender (message));
527 recipients = g_mime_message_get_recipients (message, GMIME_RECIPIENT_TYPE_TO);
528 recipients_string = internet_address_list_to_string (recipients, 0);
529 if (recipients_string)
530 printf ("To: %s\n", recipients_string);
531 recipients = g_mime_message_get_recipients (message, GMIME_RECIPIENT_TYPE_CC);
532 recipients_string = internet_address_list_to_string (recipients, 0);
533 if (recipients_string)
534 printf ("Cc: %s\n", recipients_string);
535 printf ("Date: %s\n", g_mime_message_get_date_as_string (message));
536 printf ("\fheader}\n");
538 printf ("\fbody{\n");
542 if (g_mime_content_type_is_type (content_type, "text", "*") &&
543 !g_mime_content_type_is_type (content_type, "text", "html"))
545 GMimeStream *stream_stdout = g_mime_stream_file_new (stdout);
546 g_mime_stream_file_set_owner (GMIME_STREAM_FILE (stream_stdout), FALSE);
547 show_text_part_content (node->part, stream_stdout, 0);
548 g_object_unref(stream_stdout);
550 printf ("Non-text part: %s\n",
551 g_mime_content_type_to_string (content_type));
555 for (i = 0; i < node->nchildren; i++)
556 format_part_text (ctx, mime_node_child (node, i), indent, params);
558 if (GMIME_IS_MESSAGE (node->part))
559 printf ("\fbody}\n");
561 printf ("\f%s}\n", part_type);
563 return NOTMUCH_STATUS_SUCCESS;
567 format_part_json (const void *ctx, mime_node_t *node, notmuch_bool_t first, notmuch_bool_t output_body)
569 /* Any changes to the JSON format should be reflected in the file
572 if (node->envelope_file) {
574 format_message_json (ctx, node->envelope_file);
576 printf ("\"headers\": ");
577 format_headers_json (ctx, GMIME_MESSAGE (node->part), FALSE);
580 printf (", \"body\": [");
581 format_part_json (ctx, mime_node_child (node, 0), first, TRUE);
588 void *local = talloc_new (ctx);
589 /* The disposition and content-type metadata are associated with
590 * the envelope for message parts */
591 GMimeObject *meta = node->envelope_part ?
592 GMIME_OBJECT (node->envelope_part) : node->part;
593 GMimeContentType *content_type = g_mime_object_get_content_type (meta);
594 const char *cid = g_mime_object_get_content_id (meta);
595 const char *filename = GMIME_IS_PART (node->part) ?
596 g_mime_part_get_filename (GMIME_PART (node->part)) : NULL;
597 const char *terminator = "";
603 printf ("{\"id\": %d", node->part_num);
605 if (node->decrypt_attempted)
606 printf (", \"encstatus\": [{\"status\": \"%s\"}]",
607 node->decrypt_success ? "good" : "bad");
609 if (node->verify_attempted) {
610 printf (", \"sigstatus\": ");
611 format_part_sigstatus_json (node);
614 printf (", \"content-type\": %s",
615 json_quote_str (local, g_mime_content_type_to_string (content_type)));
618 printf (", \"content-id\": %s", json_quote_str (local, cid));
621 printf (", \"filename\": %s", json_quote_str (local, 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. If a
628 * caller is interested in text/html parts, it should retrieve
629 * them separately and they will not be decoded. Since this
630 * makes charset decoding the responsibility on the caller, we
631 * report the charset for text/html parts.
633 if (g_mime_content_type_is_type (content_type, "text", "html")) {
634 const char *content_charset = g_mime_object_get_content_type_parameter (meta, "charset");
636 if (content_charset != NULL)
637 printf (", \"content-charset\": %s", json_quote_str (local, content_charset));
638 } else if (g_mime_content_type_is_type (content_type, "text", "*")) {
639 GMimeStream *stream_memory = g_mime_stream_mem_new ();
640 GByteArray *part_content;
641 show_text_part_content (node->part, stream_memory, 0);
642 part_content = g_mime_stream_mem_get_byte_array (GMIME_STREAM_MEM (stream_memory));
644 printf (", \"content\": %s", json_quote_chararray (local, (char *) part_content->data, part_content->len));
645 g_object_unref (stream_memory);
647 } else if (GMIME_IS_MULTIPART (node->part)) {
648 printf (", \"content\": [");
650 } else if (GMIME_IS_MESSAGE (node->part)) {
651 printf (", \"content\": [{");
652 printf ("\"headers\": ");
653 format_headers_json (local, GMIME_MESSAGE (node->part), FALSE);
655 printf (", \"body\": [");
661 for (i = 0; i < node->nchildren; i++)
662 format_part_json (ctx, mime_node_child (node, i), i == 0, TRUE);
664 printf ("%s}", terminator);
667 static notmuch_status_t
668 format_part_json_entry (const void *ctx, mime_node_t *node, unused (int indent),
669 const notmuch_show_params_t *params)
671 format_part_json (ctx, node, TRUE, params->output_body);
673 return NOTMUCH_STATUS_SUCCESS;
676 /* Print a message in "mboxrd" format as documented, for example,
679 * http://qmail.org/qmail-manual-html/man5/mbox.html
681 static notmuch_status_t
682 format_part_mbox (const void *ctx, mime_node_t *node, unused (int indent),
683 unused (const notmuch_show_params_t *params))
685 notmuch_message_t *message = node->envelope_file;
687 const char *filename;
692 struct tm date_gmtime;
693 char date_asctime[26];
700 INTERNAL_ERROR ("format_part_mbox requires a root part");
702 filename = notmuch_message_get_filename (message);
703 file = fopen (filename, "r");
705 fprintf (stderr, "Failed to open %s: %s\n",
706 filename, strerror (errno));
707 return NOTMUCH_STATUS_FILE_ERROR;
710 from = notmuch_message_get_header (message, "from");
711 from = _extract_email_address (ctx, from);
713 date = notmuch_message_get_date (message);
714 gmtime_r (&date, &date_gmtime);
715 asctime_r (&date_gmtime, date_asctime);
717 printf ("From %s %s", from, date_asctime);
719 while ((line_len = getline (&line, &line_size, file)) != -1 ) {
720 if (_is_from_line (line))
729 return NOTMUCH_STATUS_SUCCESS;
732 static notmuch_status_t
733 format_part_raw (unused (const void *ctx), mime_node_t *node,
735 unused (const notmuch_show_params_t *params))
737 if (node->envelope_file) {
738 /* Special case the entire message to avoid MIME parsing. */
739 const char *filename;
744 filename = notmuch_message_get_filename (node->envelope_file);
745 if (filename == NULL) {
746 fprintf (stderr, "Error: Cannot get message filename.\n");
747 return NOTMUCH_STATUS_FILE_ERROR;
750 file = fopen (filename, "r");
752 fprintf (stderr, "Error: Cannot open file %s: %s\n", filename, strerror (errno));
753 return NOTMUCH_STATUS_FILE_ERROR;
756 while (!feof (file)) {
757 size = fread (buf, 1, sizeof (buf), file);
759 fprintf (stderr, "Error: Read failed from %s\n", filename);
761 return NOTMUCH_STATUS_FILE_ERROR;
764 if (fwrite (buf, size, 1, stdout) != 1) {
765 fprintf (stderr, "Error: Write failed\n");
767 return NOTMUCH_STATUS_FILE_ERROR;
772 return NOTMUCH_STATUS_SUCCESS;
775 GMimeStream *stream_stdout;
776 GMimeStream *stream_filter = NULL;
778 stream_stdout = g_mime_stream_file_new (stdout);
779 g_mime_stream_file_set_owner (GMIME_STREAM_FILE (stream_stdout), FALSE);
781 stream_filter = g_mime_stream_filter_new (stream_stdout);
783 if (GMIME_IS_PART (node->part)) {
784 /* For leaf parts, we emit only the transfer-decoded
786 GMimeDataWrapper *wrapper;
787 wrapper = g_mime_part_get_content_object (GMIME_PART (node->part));
789 if (wrapper && stream_filter)
790 g_mime_data_wrapper_write_to_stream (wrapper, stream_filter);
792 /* Write out the whole part. For message parts (the root
793 * part and embedded message parts), this will be the
794 * message including its headers (but not the
795 * encapsulating part's headers). For multipart parts,
796 * this will include the headers. */
798 g_mime_object_write_to_stream (node->part, stream_filter);
802 g_object_unref (stream_filter);
805 g_object_unref(stream_stdout);
807 return NOTMUCH_STATUS_SUCCESS;
810 static notmuch_status_t
811 show_null_message (const notmuch_show_format_t *format)
813 /* Output a null message. Currently empty for all formats except Json */
814 if (format->null_message)
815 printf ("%s", format->null_message);
816 return NOTMUCH_STATUS_SUCCESS;
819 static notmuch_status_t
820 show_message (void *ctx,
821 const notmuch_show_format_t *format,
822 notmuch_message_t *message,
824 notmuch_show_params_t *params)
826 void *local = talloc_new (ctx);
827 mime_node_t *root, *part;
828 notmuch_status_t status;
830 status = mime_node_open (local, message, &(params->crypto), &root);
833 part = mime_node_seek_dfs (root, (params->part < 0 ? 0 : params->part));
835 status = format->part (local, part, indent, params);
841 static notmuch_status_t
842 show_messages (void *ctx,
843 const notmuch_show_format_t *format,
844 notmuch_messages_t *messages,
846 notmuch_show_params_t *params)
848 notmuch_message_t *message;
849 notmuch_bool_t match;
850 notmuch_bool_t excluded;
853 notmuch_status_t status, res = NOTMUCH_STATUS_SUCCESS;
855 if (format->message_set_start)
856 fputs (format->message_set_start, stdout);
859 notmuch_messages_valid (messages);
860 notmuch_messages_move_to_next (messages))
862 if (!first_set && format->message_set_sep)
863 fputs (format->message_set_sep, stdout);
866 if (format->message_set_start)
867 fputs (format->message_set_start, stdout);
869 message = notmuch_messages_get (messages);
871 match = notmuch_message_get_flag (message, NOTMUCH_MESSAGE_FLAG_MATCH);
872 excluded = notmuch_message_get_flag (message, NOTMUCH_MESSAGE_FLAG_EXCLUDED);
874 next_indent = indent;
876 if ((match && (!excluded || !params->omit_excluded)) || params->entire_thread) {
877 status = show_message (ctx, format, message, indent, params);
880 next_indent = indent + 1;
882 status = show_null_message (format);
885 if (!status && format->message_set_sep)
886 fputs (format->message_set_sep, stdout);
888 status = show_messages (ctx,
890 notmuch_message_get_replies (message),
896 notmuch_message_destroy (message);
898 if (format->message_set_end)
899 fputs (format->message_set_end, stdout);
902 if (format->message_set_end)
903 fputs (format->message_set_end, stdout);
908 /* Formatted output of single message */
910 do_show_single (void *ctx,
911 notmuch_query_t *query,
912 const notmuch_show_format_t *format,
913 notmuch_show_params_t *params)
915 notmuch_messages_t *messages;
916 notmuch_message_t *message;
918 if (notmuch_query_count_messages (query) != 1) {
919 fprintf (stderr, "Error: search term did not match precisely one message.\n");
923 messages = notmuch_query_search_messages (query);
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, message, 0, params) != NOTMUCH_STATUS_SUCCESS;
936 /* Formatted output of threads */
939 notmuch_query_t *query,
940 const notmuch_show_format_t *format,
941 notmuch_show_params_t *params)
943 notmuch_threads_t *threads;
944 notmuch_thread_t *thread;
945 notmuch_messages_t *messages;
946 int first_toplevel = 1;
947 notmuch_status_t status, res = NOTMUCH_STATUS_SUCCESS;
949 if (format->message_set_start)
950 fputs (format->message_set_start, stdout);
952 for (threads = notmuch_query_search_threads (query);
953 notmuch_threads_valid (threads);
954 notmuch_threads_move_to_next (threads))
956 thread = notmuch_threads_get (threads);
958 messages = notmuch_thread_get_toplevel_messages (thread);
960 if (messages == NULL)
961 INTERNAL_ERROR ("Thread %s has no toplevel messages.\n",
962 notmuch_thread_get_thread_id (thread));
964 if (!first_toplevel && format->message_set_sep)
965 fputs (format->message_set_sep, stdout);
968 status = show_messages (ctx, format, messages, 0, params);
972 notmuch_thread_destroy (thread);
976 if (format->message_set_end)
977 fputs (format->message_set_end, stdout);
979 return res != NOTMUCH_STATUS_SUCCESS;
983 NOTMUCH_FORMAT_NOT_SPECIFIED,
991 ENTIRE_THREAD_DEFAULT,
996 /* The following is to allow future options to be added more easily */
1003 notmuch_show_command (void *ctx, unused (int argc), unused (char *argv[]))
1005 notmuch_config_t *config;
1006 notmuch_database_t *notmuch;
1007 notmuch_query_t *query;
1010 const notmuch_show_format_t *format = &format_text;
1011 sprinter_t *sprinter;
1012 notmuch_show_params_t params = {
1014 .omit_excluded = TRUE,
1015 .output_body = TRUE,
1021 int format_sel = NOTMUCH_FORMAT_NOT_SPECIFIED;
1022 int exclude = EXCLUDE_TRUE;
1023 int entire_thread = ENTIRE_THREAD_DEFAULT;
1025 notmuch_opt_desc_t options[] = {
1026 { NOTMUCH_OPT_KEYWORD, &format_sel, "format", 'f',
1027 (notmuch_keyword_t []){ { "json", NOTMUCH_FORMAT_JSON },
1028 { "text", NOTMUCH_FORMAT_TEXT },
1029 { "mbox", NOTMUCH_FORMAT_MBOX },
1030 { "raw", NOTMUCH_FORMAT_RAW },
1032 { NOTMUCH_OPT_KEYWORD, &exclude, "exclude", 'x',
1033 (notmuch_keyword_t []){ { "true", EXCLUDE_TRUE },
1034 { "false", EXCLUDE_FALSE },
1036 { NOTMUCH_OPT_KEYWORD, &entire_thread, "entire-thread", 't',
1037 (notmuch_keyword_t []){ { "true", ENTIRE_THREAD_TRUE },
1038 { "false", ENTIRE_THREAD_FALSE },
1039 { "", ENTIRE_THREAD_TRUE },
1041 { NOTMUCH_OPT_INT, ¶ms.part, "part", 'p', 0 },
1042 { NOTMUCH_OPT_BOOLEAN, ¶ms.crypto.decrypt, "decrypt", 'd', 0 },
1043 { NOTMUCH_OPT_BOOLEAN, ¶ms.crypto.verify, "verify", 'v', 0 },
1044 { NOTMUCH_OPT_BOOLEAN, ¶ms.output_body, "body", 'b', 0 },
1048 opt_index = parse_arguments (argc, argv, options, 1);
1049 if (opt_index < 0) {
1050 /* diagnostics already printed */
1054 /* decryption implies verification */
1055 if (params.crypto.decrypt)
1056 params.crypto.verify = TRUE;
1058 if (format_sel == NOTMUCH_FORMAT_NOT_SPECIFIED) {
1059 /* if part was requested and format was not specified, use format=raw */
1060 if (params.part >= 0)
1061 format_sel = NOTMUCH_FORMAT_RAW;
1063 format_sel = NOTMUCH_FORMAT_TEXT;
1066 switch (format_sel) {
1067 case NOTMUCH_FORMAT_JSON:
1068 format = &format_json;
1070 case NOTMUCH_FORMAT_TEXT:
1071 format = &format_text;
1073 case NOTMUCH_FORMAT_MBOX:
1074 if (params.part > 0) {
1075 fprintf (stderr, "Error: specifying parts is incompatible with mbox output format.\n");
1079 format = &format_mbox;
1081 case NOTMUCH_FORMAT_RAW:
1082 format = &format_raw;
1083 /* If --format=raw specified without specifying part, we can only
1084 * output single message, so set part=0 */
1085 if (params.part < 0)
1091 /* Default is entire-thread = FALSE except for format=json. */
1092 if (entire_thread == ENTIRE_THREAD_DEFAULT) {
1093 if (format == &format_json)
1094 entire_thread = ENTIRE_THREAD_TRUE;
1096 entire_thread = ENTIRE_THREAD_FALSE;
1099 if (!params.output_body) {
1100 if (params.part > 0) {
1101 fprintf (stderr, "Warning: --body=false is incompatible with --part > 0. Disabling.\n");
1102 params.output_body = TRUE;
1104 if (format != &format_json)
1105 fprintf (stderr, "Warning: --body=false only implemented for format=json\n");
1109 if (entire_thread == ENTIRE_THREAD_TRUE)
1110 params.entire_thread = TRUE;
1112 params.entire_thread = FALSE;
1114 config = notmuch_config_open (ctx, NULL, NULL);
1118 query_string = query_string_from_args (ctx, argc-opt_index, argv+opt_index);
1119 if (query_string == NULL) {
1120 fprintf (stderr, "Out of memory\n");
1124 if (*query_string == '\0') {
1125 fprintf (stderr, "Error: notmuch show requires at least one search term.\n");
1129 if (notmuch_database_open (notmuch_config_get_database_path (config),
1130 NOTMUCH_DATABASE_MODE_READ_ONLY, ¬much))
1133 query = notmuch_query_create (notmuch, query_string);
1134 if (query == NULL) {
1135 fprintf (stderr, "Out of memory\n");
1139 /* Create structure printer. */
1140 sprinter = format->new_sprinter(ctx, stdout);
1142 /* If a single message is requested we do not use search_excludes. */
1143 if (params.part >= 0)
1144 ret = do_show_single (ctx, query, format, ¶ms);
1146 /* We always apply set the exclude flag. The
1147 * exclude=true|false option controls whether or not we return
1148 * threads that only match in an excluded message */
1149 const char **search_exclude_tags;
1150 size_t search_exclude_tags_length;
1153 search_exclude_tags = notmuch_config_get_search_exclude_tags
1154 (config, &search_exclude_tags_length);
1155 for (i = 0; i < search_exclude_tags_length; i++)
1156 notmuch_query_add_tag_exclude (query, search_exclude_tags[i]);
1158 if (exclude == EXCLUDE_FALSE) {
1159 notmuch_query_set_omit_excluded (query, FALSE);
1160 params.omit_excluded = FALSE;
1163 ret = do_show (ctx, query, format, ¶ms);
1166 notmuch_crypto_cleanup (¶ms.crypto);
1167 notmuch_query_destroy (query);
1168 notmuch_database_destroy (notmuch);