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, sprinter_t *sp, 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, sprinter_t *sp, 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, sprinter_t *sp, 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), sprinter_t *sp, 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 (sprinter_t *sp, GMimeMessage *message,
203 notmuch_bool_t reply)
205 InternetAddressList *recipients;
206 const char *recipients_string;
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_sender (message));
216 recipients = g_mime_message_get_recipients (message, GMIME_RECIPIENT_TYPE_TO);
217 recipients_string = internet_address_list_to_string (recipients, 0);
218 if (recipients_string) {
219 sp->map_key (sp, "To");
220 sp->string (sp, recipients_string);
223 recipients = g_mime_message_get_recipients (message, GMIME_RECIPIENT_TYPE_CC);
224 recipients_string = internet_address_list_to_string (recipients, 0);
225 if (recipients_string) {
226 sp->map_key (sp, "Cc");
227 sp->string (sp, recipients_string);
231 sp->map_key (sp, "In-reply-to");
232 sp->string (sp, g_mime_object_get_header (GMIME_OBJECT (message), "In-reply-to"));
234 sp->map_key (sp, "References");
235 sp->string (sp, g_mime_object_get_header (GMIME_OBJECT (message), "References"));
237 sp->map_key (sp, "Date");
238 sp->string (sp, g_mime_message_get_date_as_string (message));
244 /* Write a MIME text part out to the given stream.
246 * If (flags & NOTMUCH_SHOW_TEXT_PART_REPLY), this prepends "> " to
249 * Both line-ending conversion (CRLF->LF) and charset conversion ( ->
250 * UTF-8) will be performed, so it is inappropriate to call this
251 * function with a non-text part. Doing so will trigger an internal
255 show_text_part_content (GMimeObject *part, GMimeStream *stream_out,
256 notmuch_show_text_part_flags flags)
258 GMimeContentType *content_type = g_mime_object_get_content_type (GMIME_OBJECT (part));
259 GMimeStream *stream_filter = NULL;
260 GMimeDataWrapper *wrapper;
263 if (! g_mime_content_type_is_type (content_type, "text", "*"))
264 INTERNAL_ERROR ("Illegal request to format non-text part (%s) as text.",
265 g_mime_content_type_to_string (content_type));
267 if (stream_out == NULL)
270 stream_filter = g_mime_stream_filter_new (stream_out);
271 g_mime_stream_filter_add(GMIME_STREAM_FILTER (stream_filter),
272 g_mime_filter_crlf_new (FALSE, FALSE));
274 charset = g_mime_object_get_content_type_parameter (part, "charset");
276 GMimeFilter *charset_filter;
277 charset_filter = g_mime_filter_charset_new (charset, "UTF-8");
278 /* This result can be NULL for things like "unknown-8bit".
279 * Don't set a NULL filter as that makes GMime print
280 * annoying assertion-failure messages on stderr. */
281 if (charset_filter) {
282 g_mime_stream_filter_add (GMIME_STREAM_FILTER (stream_filter),
284 g_object_unref (charset_filter);
289 if (flags & NOTMUCH_SHOW_TEXT_PART_REPLY) {
290 GMimeFilter *reply_filter;
291 reply_filter = g_mime_filter_reply_new (TRUE);
293 g_mime_stream_filter_add (GMIME_STREAM_FILTER (stream_filter),
295 g_object_unref (reply_filter);
299 wrapper = g_mime_part_get_content_object (GMIME_PART (part));
300 if (wrapper && stream_filter)
301 g_mime_data_wrapper_write_to_stream (wrapper, stream_filter);
303 g_object_unref(stream_filter);
306 #ifdef GMIME_ATLEAST_26
308 signature_status_to_string (GMimeSignatureStatus x)
311 case GMIME_SIGNATURE_STATUS_GOOD:
313 case GMIME_SIGNATURE_STATUS_BAD:
315 case GMIME_SIGNATURE_STATUS_ERROR:
322 signer_status_to_string (GMimeSignerStatus x)
325 case GMIME_SIGNER_STATUS_NONE:
327 case GMIME_SIGNER_STATUS_GOOD:
329 case GMIME_SIGNER_STATUS_BAD:
331 case GMIME_SIGNER_STATUS_ERROR:
338 #ifdef GMIME_ATLEAST_26
340 format_part_sigstatus_json (sprinter_t *sp, mime_node_t *node)
342 GMimeSignatureList *siglist = node->sig_list;
352 for (i = 0; i < g_mime_signature_list_length (siglist); i++) {
353 GMimeSignature *signature = g_mime_signature_list_get_signature (siglist, i);
358 GMimeSignatureStatus status = g_mime_signature_get_status (signature);
359 sp->map_key (sp, "status");
360 sp->string (sp, signature_status_to_string (status));
362 GMimeCertificate *certificate = g_mime_signature_get_certificate (signature);
363 if (status == GMIME_SIGNATURE_STATUS_GOOD) {
365 sp->map_key (sp, "fingerprint");
366 sp->string (sp, g_mime_certificate_get_fingerprint (certificate));
368 /* these dates are seconds since the epoch; should we
369 * provide a more human-readable format string? */
370 time_t created = g_mime_signature_get_created (signature);
372 sp->map_key (sp, "created");
373 sp->integer (sp, created);
375 time_t expires = g_mime_signature_get_expires (signature);
377 sp->map_key (sp, "expires");
378 sp->integer (sp, expires);
380 /* output user id only if validity is FULL or ULTIMATE. */
381 /* note that gmime is using the term "trust" here, which
382 * is WRONG. It's actually user id "validity". */
384 const char *name = g_mime_certificate_get_name (certificate);
385 GMimeCertificateTrust trust = g_mime_certificate_get_trust (certificate);
386 if (name && (trust == GMIME_CERTIFICATE_TRUST_FULLY || trust == GMIME_CERTIFICATE_TRUST_ULTIMATE)) {
387 sp->map_key (sp, "userid");
388 sp->string (sp, name);
391 } else if (certificate) {
392 const char *key_id = g_mime_certificate_get_key_id (certificate);
394 sp->map_key (sp, "keyid");
395 sp->string (sp, key_id);
399 GMimeSignatureError errors = g_mime_signature_get_errors (signature);
400 if (errors != GMIME_SIGNATURE_ERROR_NONE) {
401 sp->map_key (sp, "errors");
402 sp->integer (sp, errors);
412 format_part_sigstatus_json (sprinter_t *sp, mime_node_t *node)
414 const GMimeSignatureValidity* validity = node->sig_validity;
423 const GMimeSigner *signer = g_mime_signature_validity_get_signers (validity);
428 sp->map_key (sp, "status");
429 sp->string (sp, signer_status_to_string (signer->status));
431 if (signer->status == GMIME_SIGNER_STATUS_GOOD)
433 if (signer->fingerprint) {
434 sp->map_key (sp, "fingerprint");
435 sp->string (sp, signer->fingerprint);
437 /* these dates are seconds since the epoch; should we
438 * provide a more human-readable format string? */
439 if (signer->created) {
440 sp->map_key (sp, "created");
441 sp->integer (sp, signer->created);
443 if (signer->expires) {
444 sp->map_key (sp, "expires");
445 sp->integer (sp, signer->expires);
447 /* output user id only if validity is FULL or ULTIMATE. */
448 /* note that gmime is using the term "trust" here, which
449 * is WRONG. It's actually user id "validity". */
450 if ((signer->name) && (signer->trust)) {
451 if ((signer->trust == GMIME_SIGNER_TRUST_FULLY) || (signer->trust == GMIME_SIGNER_TRUST_ULTIMATE)) {
452 sp->map_key (sp, "userid");
453 sp->string (sp, signer->name);
458 sp->map_key (sp, "keyid");
459 sp->string (sp, signer->keyid);
462 if (signer->errors != GMIME_SIGNER_ERROR_NONE) {
463 sp->map_key (sp, "errors");
464 sp->integer (sp, signer->errors);
468 signer = signer->next;
475 static notmuch_status_t
476 format_part_text (const void *ctx, sprinter_t *sp, mime_node_t *node,
477 int indent, const notmuch_show_params_t *params)
479 /* The disposition and content-type metadata are associated with
480 * the envelope for message parts */
481 GMimeObject *meta = node->envelope_part ?
482 GMIME_OBJECT (node->envelope_part) : node->part;
483 GMimeContentType *content_type = g_mime_object_get_content_type (meta);
484 const notmuch_bool_t leaf = GMIME_IS_PART (node->part);
485 const char *part_type;
488 if (node->envelope_file) {
489 notmuch_message_t *message = node->envelope_file;
491 part_type = "message";
492 printf ("\f%s{ id:%s depth:%d match:%d excluded:%d filename:%s\n",
494 notmuch_message_get_message_id (message),
496 notmuch_message_get_flag (message, NOTMUCH_MESSAGE_FLAG_MATCH) ? 1 : 0,
497 notmuch_message_get_flag (message, NOTMUCH_MESSAGE_FLAG_EXCLUDED) ? 1 : 0,
498 notmuch_message_get_filename (message));
500 GMimeContentDisposition *disposition = g_mime_object_get_content_disposition (meta);
501 const char *cid = g_mime_object_get_content_id (meta);
502 const char *filename = leaf ?
503 g_mime_part_get_filename (GMIME_PART (node->part)) : NULL;
506 strcmp (disposition->disposition, GMIME_DISPOSITION_ATTACHMENT) == 0)
507 part_type = "attachment";
511 printf ("\f%s{ ID: %d", part_type, node->part_num);
513 printf (", Filename: %s", filename);
515 printf (", Content-id: %s", cid);
516 printf (", Content-type: %s\n", g_mime_content_type_to_string (content_type));
519 if (GMIME_IS_MESSAGE (node->part)) {
520 GMimeMessage *message = GMIME_MESSAGE (node->part);
521 InternetAddressList *recipients;
522 const char *recipients_string;
524 printf ("\fheader{\n");
525 if (node->envelope_file)
526 printf ("%s\n", _get_one_line_summary (ctx, node->envelope_file));
527 printf ("Subject: %s\n", g_mime_message_get_subject (message));
528 printf ("From: %s\n", g_mime_message_get_sender (message));
529 recipients = g_mime_message_get_recipients (message, GMIME_RECIPIENT_TYPE_TO);
530 recipients_string = internet_address_list_to_string (recipients, 0);
531 if (recipients_string)
532 printf ("To: %s\n", recipients_string);
533 recipients = g_mime_message_get_recipients (message, GMIME_RECIPIENT_TYPE_CC);
534 recipients_string = internet_address_list_to_string (recipients, 0);
535 if (recipients_string)
536 printf ("Cc: %s\n", recipients_string);
537 printf ("Date: %s\n", g_mime_message_get_date_as_string (message));
538 printf ("\fheader}\n");
540 printf ("\fbody{\n");
544 if (g_mime_content_type_is_type (content_type, "text", "*") &&
545 !g_mime_content_type_is_type (content_type, "text", "html"))
547 GMimeStream *stream_stdout = g_mime_stream_file_new (stdout);
548 g_mime_stream_file_set_owner (GMIME_STREAM_FILE (stream_stdout), FALSE);
549 show_text_part_content (node->part, stream_stdout, 0);
550 g_object_unref(stream_stdout);
552 printf ("Non-text part: %s\n",
553 g_mime_content_type_to_string (content_type));
557 for (i = 0; i < node->nchildren; i++)
558 format_part_text (ctx, sp, mime_node_child (node, i), indent, params);
560 if (GMIME_IS_MESSAGE (node->part))
561 printf ("\fbody}\n");
563 printf ("\f%s}\n", part_type);
565 return NOTMUCH_STATUS_SUCCESS;
569 format_part_json (const void *ctx, sprinter_t *sp, mime_node_t *node,
570 notmuch_bool_t first, notmuch_bool_t output_body)
572 /* Any changes to the JSON format should be reflected in the file
575 if (node->envelope_file) {
577 format_message_json (ctx, node->envelope_file);
579 printf ("\"headers\": ");
580 format_headers_json (sp, GMIME_MESSAGE (node->part), FALSE);
583 printf (", \"body\": [");
584 format_part_json (ctx, sp, mime_node_child (node, 0), first, TRUE);
591 void *local = talloc_new (ctx);
592 /* The disposition and content-type metadata are associated with
593 * the envelope for message parts */
594 GMimeObject *meta = node->envelope_part ?
595 GMIME_OBJECT (node->envelope_part) : node->part;
596 GMimeContentType *content_type = g_mime_object_get_content_type (meta);
597 const char *cid = g_mime_object_get_content_id (meta);
598 const char *filename = GMIME_IS_PART (node->part) ?
599 g_mime_part_get_filename (GMIME_PART (node->part)) : NULL;
600 const char *terminator = "";
606 printf ("{\"id\": %d", node->part_num);
608 if (node->decrypt_attempted)
609 printf (", \"encstatus\": [{\"status\": \"%s\"}]",
610 node->decrypt_success ? "good" : "bad");
612 if (node->verify_attempted) {
613 printf (", \"sigstatus\": ");
614 format_part_sigstatus_json (sp, node);
617 printf (", \"content-type\": %s",
618 json_quote_str (local, g_mime_content_type_to_string (content_type)));
621 printf (", \"content-id\": %s", json_quote_str (local, cid));
624 printf (", \"filename\": %s", json_quote_str (local, filename));
626 if (GMIME_IS_PART (node->part)) {
627 /* For non-HTML text parts, we include the content in the
628 * JSON. Since JSON must be Unicode, we handle charset
629 * decoding here and do not report a charset to the caller.
630 * For text/html parts, we do not include the content. If a
631 * caller is interested in text/html parts, it should retrieve
632 * them separately and they will not be decoded. Since this
633 * makes charset decoding the responsibility on the caller, we
634 * report the charset for text/html parts.
636 if (g_mime_content_type_is_type (content_type, "text", "html")) {
637 const char *content_charset = g_mime_object_get_content_type_parameter (meta, "charset");
639 if (content_charset != NULL)
640 printf (", \"content-charset\": %s", json_quote_str (local, content_charset));
641 } else if (g_mime_content_type_is_type (content_type, "text", "*")) {
642 GMimeStream *stream_memory = g_mime_stream_mem_new ();
643 GByteArray *part_content;
644 show_text_part_content (node->part, stream_memory, 0);
645 part_content = g_mime_stream_mem_get_byte_array (GMIME_STREAM_MEM (stream_memory));
647 printf (", \"content\": %s", json_quote_chararray (local, (char *) part_content->data, part_content->len));
648 g_object_unref (stream_memory);
650 } else if (GMIME_IS_MULTIPART (node->part)) {
651 printf (", \"content\": [");
653 } else if (GMIME_IS_MESSAGE (node->part)) {
654 printf (", \"content\": [{");
655 printf ("\"headers\": ");
656 format_headers_json (sp, GMIME_MESSAGE (node->part), FALSE);
658 printf (", \"body\": [");
664 for (i = 0; i < node->nchildren; i++)
665 format_part_json (ctx, sp, mime_node_child (node, i), i == 0, TRUE);
667 printf ("%s}", terminator);
670 static notmuch_status_t
671 format_part_json_entry (const void *ctx, sprinter_t *sp,
672 mime_node_t *node, unused (int indent),
673 const notmuch_show_params_t *params)
675 format_part_json (ctx, sp, node, TRUE, params->output_body);
677 return NOTMUCH_STATUS_SUCCESS;
680 /* Print a message in "mboxrd" format as documented, for example,
683 * http://qmail.org/qmail-manual-html/man5/mbox.html
685 static notmuch_status_t
686 format_part_mbox (const void *ctx, unused (sprinter_t *sp), mime_node_t *node,
688 unused (const notmuch_show_params_t *params))
690 notmuch_message_t *message = node->envelope_file;
692 const char *filename;
697 struct tm date_gmtime;
698 char date_asctime[26];
705 INTERNAL_ERROR ("format_part_mbox requires a root part");
707 filename = notmuch_message_get_filename (message);
708 file = fopen (filename, "r");
710 fprintf (stderr, "Failed to open %s: %s\n",
711 filename, strerror (errno));
712 return NOTMUCH_STATUS_FILE_ERROR;
715 from = notmuch_message_get_header (message, "from");
716 from = _extract_email_address (ctx, from);
718 date = notmuch_message_get_date (message);
719 gmtime_r (&date, &date_gmtime);
720 asctime_r (&date_gmtime, date_asctime);
722 printf ("From %s %s", from, date_asctime);
724 while ((line_len = getline (&line, &line_size, file)) != -1 ) {
725 if (_is_from_line (line))
734 return NOTMUCH_STATUS_SUCCESS;
737 static notmuch_status_t
738 format_part_raw (unused (const void *ctx), unused (sprinter_t *sp),
739 mime_node_t *node, unused (int indent),
740 unused (const notmuch_show_params_t *params))
742 if (node->envelope_file) {
743 /* Special case the entire message to avoid MIME parsing. */
744 const char *filename;
749 filename = notmuch_message_get_filename (node->envelope_file);
750 if (filename == NULL) {
751 fprintf (stderr, "Error: Cannot get message filename.\n");
752 return NOTMUCH_STATUS_FILE_ERROR;
755 file = fopen (filename, "r");
757 fprintf (stderr, "Error: Cannot open file %s: %s\n", filename, strerror (errno));
758 return NOTMUCH_STATUS_FILE_ERROR;
761 while (!feof (file)) {
762 size = fread (buf, 1, sizeof (buf), file);
764 fprintf (stderr, "Error: Read failed from %s\n", filename);
766 return NOTMUCH_STATUS_FILE_ERROR;
769 if (fwrite (buf, size, 1, stdout) != 1) {
770 fprintf (stderr, "Error: Write failed\n");
772 return NOTMUCH_STATUS_FILE_ERROR;
777 return NOTMUCH_STATUS_SUCCESS;
780 GMimeStream *stream_stdout;
781 GMimeStream *stream_filter = NULL;
783 stream_stdout = g_mime_stream_file_new (stdout);
784 g_mime_stream_file_set_owner (GMIME_STREAM_FILE (stream_stdout), FALSE);
786 stream_filter = g_mime_stream_filter_new (stream_stdout);
788 if (GMIME_IS_PART (node->part)) {
789 /* For leaf parts, we emit only the transfer-decoded
791 GMimeDataWrapper *wrapper;
792 wrapper = g_mime_part_get_content_object (GMIME_PART (node->part));
794 if (wrapper && stream_filter)
795 g_mime_data_wrapper_write_to_stream (wrapper, stream_filter);
797 /* Write out the whole part. For message parts (the root
798 * part and embedded message parts), this will be the
799 * message including its headers (but not the
800 * encapsulating part's headers). For multipart parts,
801 * this will include the headers. */
803 g_mime_object_write_to_stream (node->part, stream_filter);
807 g_object_unref (stream_filter);
810 g_object_unref(stream_stdout);
812 return NOTMUCH_STATUS_SUCCESS;
815 static notmuch_status_t
816 show_null_message (const notmuch_show_format_t *format)
818 /* Output a null message. Currently empty for all formats except Json */
819 if (format->null_message)
820 printf ("%s", format->null_message);
821 return NOTMUCH_STATUS_SUCCESS;
824 static notmuch_status_t
825 show_message (void *ctx,
826 const notmuch_show_format_t *format,
828 notmuch_message_t *message,
830 notmuch_show_params_t *params)
832 void *local = talloc_new (ctx);
833 mime_node_t *root, *part;
834 notmuch_status_t status;
836 status = mime_node_open (local, message, &(params->crypto), &root);
839 part = mime_node_seek_dfs (root, (params->part < 0 ? 0 : params->part));
841 status = format->part (local, sp, part, indent, params);
847 static notmuch_status_t
848 show_messages (void *ctx,
849 const notmuch_show_format_t *format,
851 notmuch_messages_t *messages,
853 notmuch_show_params_t *params)
855 notmuch_message_t *message;
856 notmuch_bool_t match;
857 notmuch_bool_t excluded;
860 notmuch_status_t status, res = NOTMUCH_STATUS_SUCCESS;
862 if (format->message_set_start)
863 fputs (format->message_set_start, stdout);
866 notmuch_messages_valid (messages);
867 notmuch_messages_move_to_next (messages))
869 if (!first_set && format->message_set_sep)
870 fputs (format->message_set_sep, stdout);
873 if (format->message_set_start)
874 fputs (format->message_set_start, stdout);
876 message = notmuch_messages_get (messages);
878 match = notmuch_message_get_flag (message, NOTMUCH_MESSAGE_FLAG_MATCH);
879 excluded = notmuch_message_get_flag (message, NOTMUCH_MESSAGE_FLAG_EXCLUDED);
881 next_indent = indent;
883 if ((match && (!excluded || !params->omit_excluded)) || params->entire_thread) {
884 status = show_message (ctx, format, sp, message, indent, params);
887 next_indent = indent + 1;
889 status = show_null_message (format);
892 if (!status && format->message_set_sep)
893 fputs (format->message_set_sep, stdout);
895 status = show_messages (ctx,
897 notmuch_message_get_replies (message),
903 notmuch_message_destroy (message);
905 if (format->message_set_end)
906 fputs (format->message_set_end, stdout);
909 if (format->message_set_end)
910 fputs (format->message_set_end, stdout);
915 /* Formatted output of single message */
917 do_show_single (void *ctx,
918 notmuch_query_t *query,
919 const notmuch_show_format_t *format,
921 notmuch_show_params_t *params)
923 notmuch_messages_t *messages;
924 notmuch_message_t *message;
926 if (notmuch_query_count_messages (query) != 1) {
927 fprintf (stderr, "Error: search term did not match precisely one message.\n");
931 messages = notmuch_query_search_messages (query);
932 message = notmuch_messages_get (messages);
934 if (message == NULL) {
935 fprintf (stderr, "Error: Cannot find matching message.\n");
939 notmuch_message_set_flag (message, NOTMUCH_MESSAGE_FLAG_MATCH, 1);
941 return show_message (ctx, format, sp, message, 0, params)
942 != NOTMUCH_STATUS_SUCCESS;
945 /* Formatted output of threads */
948 notmuch_query_t *query,
949 const notmuch_show_format_t *format,
951 notmuch_show_params_t *params)
953 notmuch_threads_t *threads;
954 notmuch_thread_t *thread;
955 notmuch_messages_t *messages;
956 int first_toplevel = 1;
957 notmuch_status_t status, res = NOTMUCH_STATUS_SUCCESS;
959 if (format->message_set_start)
960 fputs (format->message_set_start, stdout);
962 for (threads = notmuch_query_search_threads (query);
963 notmuch_threads_valid (threads);
964 notmuch_threads_move_to_next (threads))
966 thread = notmuch_threads_get (threads);
968 messages = notmuch_thread_get_toplevel_messages (thread);
970 if (messages == NULL)
971 INTERNAL_ERROR ("Thread %s has no toplevel messages.\n",
972 notmuch_thread_get_thread_id (thread));
974 if (!first_toplevel && format->message_set_sep)
975 fputs (format->message_set_sep, stdout);
978 status = show_messages (ctx, format, sp, messages, 0, params);
982 notmuch_thread_destroy (thread);
986 if (format->message_set_end)
987 fputs (format->message_set_end, stdout);
989 return res != NOTMUCH_STATUS_SUCCESS;
993 NOTMUCH_FORMAT_NOT_SPECIFIED,
1001 ENTIRE_THREAD_DEFAULT,
1003 ENTIRE_THREAD_FALSE,
1006 /* The following is to allow future options to be added more easily */
1013 notmuch_show_command (void *ctx, unused (int argc), unused (char *argv[]))
1015 notmuch_config_t *config;
1016 notmuch_database_t *notmuch;
1017 notmuch_query_t *query;
1020 const notmuch_show_format_t *format = &format_text;
1021 sprinter_t *sprinter;
1022 notmuch_show_params_t params = {
1024 .omit_excluded = TRUE,
1025 .output_body = TRUE,
1031 int format_sel = NOTMUCH_FORMAT_NOT_SPECIFIED;
1032 int exclude = EXCLUDE_TRUE;
1033 int entire_thread = ENTIRE_THREAD_DEFAULT;
1035 notmuch_opt_desc_t options[] = {
1036 { NOTMUCH_OPT_KEYWORD, &format_sel, "format", 'f',
1037 (notmuch_keyword_t []){ { "json", NOTMUCH_FORMAT_JSON },
1038 { "text", NOTMUCH_FORMAT_TEXT },
1039 { "mbox", NOTMUCH_FORMAT_MBOX },
1040 { "raw", NOTMUCH_FORMAT_RAW },
1042 { NOTMUCH_OPT_KEYWORD, &exclude, "exclude", 'x',
1043 (notmuch_keyword_t []){ { "true", EXCLUDE_TRUE },
1044 { "false", EXCLUDE_FALSE },
1046 { NOTMUCH_OPT_KEYWORD, &entire_thread, "entire-thread", 't',
1047 (notmuch_keyword_t []){ { "true", ENTIRE_THREAD_TRUE },
1048 { "false", ENTIRE_THREAD_FALSE },
1049 { "", ENTIRE_THREAD_TRUE },
1051 { NOTMUCH_OPT_INT, ¶ms.part, "part", 'p', 0 },
1052 { NOTMUCH_OPT_BOOLEAN, ¶ms.crypto.decrypt, "decrypt", 'd', 0 },
1053 { NOTMUCH_OPT_BOOLEAN, ¶ms.crypto.verify, "verify", 'v', 0 },
1054 { NOTMUCH_OPT_BOOLEAN, ¶ms.output_body, "body", 'b', 0 },
1058 opt_index = parse_arguments (argc, argv, options, 1);
1059 if (opt_index < 0) {
1060 /* diagnostics already printed */
1064 /* decryption implies verification */
1065 if (params.crypto.decrypt)
1066 params.crypto.verify = TRUE;
1068 if (format_sel == NOTMUCH_FORMAT_NOT_SPECIFIED) {
1069 /* if part was requested and format was not specified, use format=raw */
1070 if (params.part >= 0)
1071 format_sel = NOTMUCH_FORMAT_RAW;
1073 format_sel = NOTMUCH_FORMAT_TEXT;
1076 switch (format_sel) {
1077 case NOTMUCH_FORMAT_JSON:
1078 format = &format_json;
1080 case NOTMUCH_FORMAT_TEXT:
1081 format = &format_text;
1083 case NOTMUCH_FORMAT_MBOX:
1084 if (params.part > 0) {
1085 fprintf (stderr, "Error: specifying parts is incompatible with mbox output format.\n");
1089 format = &format_mbox;
1091 case NOTMUCH_FORMAT_RAW:
1092 format = &format_raw;
1093 /* If --format=raw specified without specifying part, we can only
1094 * output single message, so set part=0 */
1095 if (params.part < 0)
1101 /* Default is entire-thread = FALSE except for format=json. */
1102 if (entire_thread == ENTIRE_THREAD_DEFAULT) {
1103 if (format == &format_json)
1104 entire_thread = ENTIRE_THREAD_TRUE;
1106 entire_thread = ENTIRE_THREAD_FALSE;
1109 if (!params.output_body) {
1110 if (params.part > 0) {
1111 fprintf (stderr, "Warning: --body=false is incompatible with --part > 0. Disabling.\n");
1112 params.output_body = TRUE;
1114 if (format != &format_json)
1115 fprintf (stderr, "Warning: --body=false only implemented for format=json\n");
1119 if (entire_thread == ENTIRE_THREAD_TRUE)
1120 params.entire_thread = TRUE;
1122 params.entire_thread = FALSE;
1124 config = notmuch_config_open (ctx, NULL, NULL);
1128 query_string = query_string_from_args (ctx, argc-opt_index, argv+opt_index);
1129 if (query_string == NULL) {
1130 fprintf (stderr, "Out of memory\n");
1134 if (*query_string == '\0') {
1135 fprintf (stderr, "Error: notmuch show requires at least one search term.\n");
1139 if (notmuch_database_open (notmuch_config_get_database_path (config),
1140 NOTMUCH_DATABASE_MODE_READ_ONLY, ¬much))
1143 query = notmuch_query_create (notmuch, query_string);
1144 if (query == NULL) {
1145 fprintf (stderr, "Out of memory\n");
1149 /* Create structure printer. */
1150 sprinter = format->new_sprinter(ctx, stdout);
1152 /* If a single message is requested we do not use search_excludes. */
1153 if (params.part >= 0)
1154 ret = do_show_single (ctx, query, format, sprinter, ¶ms);
1156 /* We always apply set the exclude flag. The
1157 * exclude=true|false option controls whether or not we return
1158 * threads that only match in an excluded message */
1159 const char **search_exclude_tags;
1160 size_t search_exclude_tags_length;
1163 search_exclude_tags = notmuch_config_get_search_exclude_tags
1164 (config, &search_exclude_tags_length);
1165 for (i = 0; i < search_exclude_tags_length; i++)
1166 notmuch_query_add_tag_exclude (query, search_exclude_tags[i]);
1168 if (exclude == EXCLUDE_FALSE) {
1169 notmuch_query_set_omit_excluded (query, FALSE);
1170 params.omit_excluded = FALSE;
1173 ret = do_show (ctx, query, format, sprinter, ¶ms);
1176 notmuch_crypto_cleanup (¶ms.crypto);
1177 notmuch_query_destroy (query);
1178 notmuch_database_destroy (notmuch);