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"
24 static notmuch_status_t
25 format_part_text (const void *ctx, mime_node_t *node,
26 int indent, const notmuch_show_params_t *params);
28 static const notmuch_show_format_t format_text = {
29 .part = format_part_text,
32 static notmuch_status_t
33 format_part_json_entry (const void *ctx, mime_node_t *node,
34 int indent, const notmuch_show_params_t *params);
36 static const notmuch_show_format_t format_json = {
37 .message_set_start = "[",
38 .part = format_part_json_entry,
39 .message_set_sep = ", ",
40 .message_set_end = "]"
43 static notmuch_status_t
44 format_part_mbox (const void *ctx, mime_node_t *node,
45 int indent, const notmuch_show_params_t *params);
47 static const notmuch_show_format_t format_mbox = {
48 .part = format_part_mbox,
51 static notmuch_status_t
52 format_part_raw (unused (const void *ctx), mime_node_t *node,
54 unused (const notmuch_show_params_t *params));
56 static const notmuch_show_format_t format_raw = {
57 .part = format_part_raw,
61 _get_tags_as_string (const void *ctx, notmuch_message_t *message)
68 result = talloc_strdup (ctx, "");
72 for (tags = notmuch_message_get_tags (message);
73 notmuch_tags_valid (tags);
74 notmuch_tags_move_to_next (tags))
76 tag = notmuch_tags_get (tags);
78 result = talloc_asprintf_append (result, "%s%s",
79 first ? "" : " ", tag);
86 /* Get a nice, single-line summary of message. */
88 _get_one_line_summary (const void *ctx, notmuch_message_t *message)
92 const char *relative_date;
95 from = notmuch_message_get_header (message, "from");
97 date = notmuch_message_get_date (message);
98 relative_date = notmuch_time_relative_date (ctx, date);
100 tags = _get_tags_as_string (ctx, message);
102 return talloc_asprintf (ctx, "%s (%s) (%s)",
103 from, relative_date, tags);
107 format_message_json (const void *ctx, notmuch_message_t *message)
109 notmuch_tags_t *tags;
111 void *ctx_quote = talloc_new (ctx);
113 const char *relative_date;
115 date = notmuch_message_get_date (message);
116 relative_date = notmuch_time_relative_date (ctx, date);
118 printf ("\"id\": %s, \"match\": %s, \"excluded\": %s, \"filename\": %s, \"timestamp\": %ld, \"date_relative\": \"%s\", \"tags\": [",
119 json_quote_str (ctx_quote, notmuch_message_get_message_id (message)),
120 notmuch_message_get_flag (message, NOTMUCH_MESSAGE_FLAG_MATCH) ? "true" : "false",
121 notmuch_message_get_flag (message, NOTMUCH_MESSAGE_FLAG_EXCLUDED) ? "true" : "false",
122 json_quote_str (ctx_quote, notmuch_message_get_filename (message)),
123 date, relative_date);
125 for (tags = notmuch_message_get_tags (message);
126 notmuch_tags_valid (tags);
127 notmuch_tags_move_to_next (tags))
129 printf("%s%s", first ? "" : ",",
130 json_quote_str (ctx_quote, notmuch_tags_get (tags)));
134 talloc_free (ctx_quote);
137 /* Extract just the email address from the contents of a From:
140 _extract_email_address (const void *ctx, const char *from)
142 InternetAddressList *addresses;
143 InternetAddress *address;
144 InternetAddressMailbox *mailbox;
145 const char *email = "MAILER-DAEMON";
147 addresses = internet_address_list_parse_string (from);
149 /* Bail if there is no address here. */
150 if (addresses == NULL || internet_address_list_length (addresses) < 1)
153 /* Otherwise, just use the first address. */
154 address = internet_address_list_get_address (addresses, 0);
156 /* The From header should never contain an address group rather
157 * than a mailbox. So bail if it does. */
158 if (! INTERNET_ADDRESS_IS_MAILBOX (address))
161 mailbox = INTERNET_ADDRESS_MAILBOX (address);
162 email = internet_address_mailbox_get_addr (mailbox);
163 email = talloc_strdup (ctx, email);
167 g_object_unref (addresses);
172 /* Return 1 if 'line' is an mbox From_ line---that is, a line
173 * beginning with zero or more '>' characters followed by the
174 * characters 'F', 'r', 'o', 'm', and space.
176 * Any characters at all may appear after that in the line.
179 _is_from_line (const char *line)
181 const char *s = line;
189 if (STRNCMP_LITERAL (s, "From ") == 0)
196 format_headers_json (const void *ctx, GMimeMessage *message, notmuch_bool_t reply)
198 void *local = talloc_new (ctx);
199 InternetAddressList *recipients;
200 const char *recipients_string;
203 json_quote_str (local, "Subject"),
204 json_quote_str (local, g_mime_message_get_subject (message)));
206 json_quote_str (local, "From"),
207 json_quote_str (local, g_mime_message_get_sender (message)));
208 recipients = g_mime_message_get_recipients (message, GMIME_RECIPIENT_TYPE_TO);
209 recipients_string = internet_address_list_to_string (recipients, 0);
210 if (recipients_string)
212 json_quote_str (local, "To"),
213 json_quote_str (local, recipients_string));
214 recipients = g_mime_message_get_recipients (message, GMIME_RECIPIENT_TYPE_CC);
215 recipients_string = internet_address_list_to_string (recipients, 0);
216 if (recipients_string)
218 json_quote_str (local, "Cc"),
219 json_quote_str (local, recipients_string));
223 json_quote_str (local, "In-reply-to"),
224 json_quote_str (local, g_mime_object_get_header (GMIME_OBJECT (message), "In-reply-to")));
227 json_quote_str (local, "References"),
228 json_quote_str (local, g_mime_object_get_header (GMIME_OBJECT (message), "References")));
231 json_quote_str (local, "Date"),
232 json_quote_str (local, g_mime_message_get_date_as_string (message)));
240 /* Write a MIME text part out to the given stream.
242 * If (flags & NOTMUCH_SHOW_TEXT_PART_REPLY), this prepends "> " to
245 * Both line-ending conversion (CRLF->LF) and charset conversion ( ->
246 * UTF-8) will be performed, so it is inappropriate to call this
247 * function with a non-text part. Doing so will trigger an internal
251 show_text_part_content (GMimeObject *part, GMimeStream *stream_out,
252 notmuch_show_text_part_flags flags)
254 GMimeContentType *content_type = g_mime_object_get_content_type (GMIME_OBJECT (part));
255 GMimeStream *stream_filter = NULL;
256 GMimeDataWrapper *wrapper;
259 if (! g_mime_content_type_is_type (content_type, "text", "*"))
260 INTERNAL_ERROR ("Illegal request to format non-text part (%s) as text.",
261 g_mime_content_type_to_string (content_type));
263 if (stream_out == NULL)
266 stream_filter = g_mime_stream_filter_new (stream_out);
267 g_mime_stream_filter_add(GMIME_STREAM_FILTER (stream_filter),
268 g_mime_filter_crlf_new (FALSE, FALSE));
270 charset = g_mime_object_get_content_type_parameter (part, "charset");
272 GMimeFilter *charset_filter;
273 charset_filter = g_mime_filter_charset_new (charset, "UTF-8");
274 /* This result can be NULL for things like "unknown-8bit".
275 * Don't set a NULL filter as that makes GMime print
276 * annoying assertion-failure messages on stderr. */
277 if (charset_filter) {
278 g_mime_stream_filter_add (GMIME_STREAM_FILTER (stream_filter),
280 g_object_unref (charset_filter);
285 if (flags & NOTMUCH_SHOW_TEXT_PART_REPLY) {
286 GMimeFilter *reply_filter;
287 reply_filter = g_mime_filter_reply_new (TRUE);
289 g_mime_stream_filter_add (GMIME_STREAM_FILTER (stream_filter),
291 g_object_unref (reply_filter);
295 wrapper = g_mime_part_get_content_object (GMIME_PART (part));
296 if (wrapper && stream_filter)
297 g_mime_data_wrapper_write_to_stream (wrapper, stream_filter);
299 g_object_unref(stream_filter);
302 #ifdef GMIME_ATLEAST_26
304 signature_status_to_string (GMimeSignatureStatus x)
307 case GMIME_SIGNATURE_STATUS_GOOD:
309 case GMIME_SIGNATURE_STATUS_BAD:
311 case GMIME_SIGNATURE_STATUS_ERROR:
318 signer_status_to_string (GMimeSignerStatus x)
321 case GMIME_SIGNER_STATUS_NONE:
323 case GMIME_SIGNER_STATUS_GOOD:
325 case GMIME_SIGNER_STATUS_BAD:
327 case GMIME_SIGNER_STATUS_ERROR:
334 #ifdef GMIME_ATLEAST_26
336 format_part_sigstatus_json (mime_node_t *node)
338 GMimeSignatureList *siglist = node->sig_list;
347 void *ctx_quote = talloc_new (NULL);
349 for (i = 0; i < g_mime_signature_list_length (siglist); i++) {
350 GMimeSignature *signature = g_mime_signature_list_get_signature (siglist, i);
358 GMimeSignatureStatus status = g_mime_signature_get_status (signature);
359 printf ("\"status\": %s",
360 json_quote_str (ctx_quote,
361 signature_status_to_string (status)));
363 GMimeCertificate *certificate = g_mime_signature_get_certificate (signature);
364 if (status == GMIME_SIGNATURE_STATUS_GOOD) {
366 printf (", \"fingerprint\": %s", json_quote_str (ctx_quote, g_mime_certificate_get_fingerprint (certificate)));
367 /* these dates are seconds since the epoch; should we
368 * provide a more human-readable format string? */
369 time_t created = g_mime_signature_get_created (signature);
371 printf (", \"created\": %d", (int) created);
372 time_t expires = g_mime_signature_get_expires (signature);
374 printf (", \"expires\": %d", (int) expires);
375 /* output user id only if validity is FULL or ULTIMATE. */
376 /* note that gmime is using the term "trust" here, which
377 * is WRONG. It's actually user id "validity". */
379 const char *name = g_mime_certificate_get_name (certificate);
380 GMimeCertificateTrust trust = g_mime_certificate_get_trust (certificate);
381 if (name && (trust == GMIME_CERTIFICATE_TRUST_FULLY || trust == GMIME_CERTIFICATE_TRUST_ULTIMATE))
382 printf (", \"userid\": %s", json_quote_str (ctx_quote, name));
384 } else if (certificate) {
385 const char *key_id = g_mime_certificate_get_key_id (certificate);
387 printf (", \"keyid\": %s", json_quote_str (ctx_quote, key_id));
390 GMimeSignatureError errors = g_mime_signature_get_errors (signature);
391 if (errors != GMIME_SIGNATURE_ERROR_NONE) {
392 printf (", \"errors\": %d", errors);
400 talloc_free (ctx_quote);
404 format_part_sigstatus_json (mime_node_t *node)
406 const GMimeSignatureValidity* validity = node->sig_validity;
415 const GMimeSigner *signer = g_mime_signature_validity_get_signers (validity);
417 void *ctx_quote = talloc_new (NULL);
428 printf ("\"status\": %s",
429 json_quote_str (ctx_quote,
430 signer_status_to_string (signer->status)));
432 if (signer->status == GMIME_SIGNER_STATUS_GOOD)
434 if (signer->fingerprint)
435 printf (", \"fingerprint\": %s", json_quote_str (ctx_quote, signer->fingerprint));
436 /* these dates are seconds since the epoch; should we
437 * provide a more human-readable format string? */
439 printf (", \"created\": %d", (int) signer->created);
441 printf (", \"expires\": %d", (int) signer->expires);
442 /* output user id only if validity is FULL or ULTIMATE. */
443 /* note that gmime is using the term "trust" here, which
444 * is WRONG. It's actually user id "validity". */
445 if ((signer->name) && (signer->trust)) {
446 if ((signer->trust == GMIME_SIGNER_TRUST_FULLY) || (signer->trust == GMIME_SIGNER_TRUST_ULTIMATE))
447 printf (", \"userid\": %s", json_quote_str (ctx_quote, signer->name));
451 printf (", \"keyid\": %s", json_quote_str (ctx_quote, signer->keyid));
453 if (signer->errors != GMIME_SIGNER_ERROR_NONE) {
454 printf (", \"errors\": %d", signer->errors);
458 signer = signer->next;
463 talloc_free (ctx_quote);
467 static notmuch_status_t
468 format_part_text (const void *ctx, mime_node_t *node,
469 int indent, const notmuch_show_params_t *params)
471 /* The disposition and content-type metadata are associated with
472 * the envelope for message parts */
473 GMimeObject *meta = node->envelope_part ?
474 GMIME_OBJECT (node->envelope_part) : node->part;
475 GMimeContentType *content_type = g_mime_object_get_content_type (meta);
476 const notmuch_bool_t leaf = GMIME_IS_PART (node->part);
477 const char *part_type;
480 if (node->envelope_file) {
481 notmuch_message_t *message = node->envelope_file;
483 part_type = "message";
484 printf ("\f%s{ id:%s depth:%d match:%d excluded:%d filename:%s\n",
486 notmuch_message_get_message_id (message),
488 notmuch_message_get_flag (message, NOTMUCH_MESSAGE_FLAG_MATCH) ? 1 : 0,
489 notmuch_message_get_flag (message, NOTMUCH_MESSAGE_FLAG_EXCLUDED) ? 1 : 0,
490 notmuch_message_get_filename (message));
492 GMimeContentDisposition *disposition = g_mime_object_get_content_disposition (meta);
493 const char *cid = g_mime_object_get_content_id (meta);
494 const char *filename = leaf ?
495 g_mime_part_get_filename (GMIME_PART (node->part)) : NULL;
498 strcmp (disposition->disposition, GMIME_DISPOSITION_ATTACHMENT) == 0)
499 part_type = "attachment";
503 printf ("\f%s{ ID: %d", part_type, node->part_num);
505 printf (", Filename: %s", filename);
507 printf (", Content-id: %s", cid);
508 printf (", Content-type: %s\n", g_mime_content_type_to_string (content_type));
511 if (GMIME_IS_MESSAGE (node->part)) {
512 GMimeMessage *message = GMIME_MESSAGE (node->part);
513 InternetAddressList *recipients;
514 const char *recipients_string;
516 printf ("\fheader{\n");
517 if (node->envelope_file)
518 printf ("%s\n", _get_one_line_summary (ctx, node->envelope_file));
519 printf ("Subject: %s\n", g_mime_message_get_subject (message));
520 printf ("From: %s\n", g_mime_message_get_sender (message));
521 recipients = g_mime_message_get_recipients (message, GMIME_RECIPIENT_TYPE_TO);
522 recipients_string = internet_address_list_to_string (recipients, 0);
523 if (recipients_string)
524 printf ("To: %s\n", recipients_string);
525 recipients = g_mime_message_get_recipients (message, GMIME_RECIPIENT_TYPE_CC);
526 recipients_string = internet_address_list_to_string (recipients, 0);
527 if (recipients_string)
528 printf ("Cc: %s\n", recipients_string);
529 printf ("Date: %s\n", g_mime_message_get_date_as_string (message));
530 printf ("\fheader}\n");
532 printf ("\fbody{\n");
536 if (g_mime_content_type_is_type (content_type, "text", "*") &&
537 !g_mime_content_type_is_type (content_type, "text", "html"))
539 GMimeStream *stream_stdout = g_mime_stream_file_new (stdout);
540 g_mime_stream_file_set_owner (GMIME_STREAM_FILE (stream_stdout), FALSE);
541 show_text_part_content (node->part, stream_stdout, 0);
542 g_object_unref(stream_stdout);
544 printf ("Non-text part: %s\n",
545 g_mime_content_type_to_string (content_type));
549 for (i = 0; i < node->nchildren; i++)
550 format_part_text (ctx, mime_node_child (node, i), indent, params);
552 if (GMIME_IS_MESSAGE (node->part))
553 printf ("\fbody}\n");
555 printf ("\f%s}\n", part_type);
557 return NOTMUCH_STATUS_SUCCESS;
561 format_part_json (const void *ctx, mime_node_t *node, notmuch_bool_t first)
563 /* Any changes to the JSON format should be reflected in the file
566 if (node->envelope_file) {
568 format_message_json (ctx, node->envelope_file);
570 printf ("\"headers\": ");
571 format_headers_json (ctx, GMIME_MESSAGE (node->part), FALSE);
573 printf (", \"body\": [");
574 format_part_json (ctx, mime_node_child (node, 0), first);
580 void *local = talloc_new (ctx);
581 /* The disposition and content-type metadata are associated with
582 * the envelope for message parts */
583 GMimeObject *meta = node->envelope_part ?
584 GMIME_OBJECT (node->envelope_part) : node->part;
585 GMimeContentType *content_type = g_mime_object_get_content_type (meta);
586 const char *cid = g_mime_object_get_content_id (meta);
587 const char *filename = GMIME_IS_PART (node->part) ?
588 g_mime_part_get_filename (GMIME_PART (node->part)) : NULL;
589 const char *terminator = "";
595 printf ("{\"id\": %d", node->part_num);
597 if (node->decrypt_attempted)
598 printf (", \"encstatus\": [{\"status\": \"%s\"}]",
599 node->decrypt_success ? "good" : "bad");
601 if (node->verify_attempted) {
602 printf (", \"sigstatus\": ");
603 format_part_sigstatus_json (node);
606 printf (", \"content-type\": %s",
607 json_quote_str (local, g_mime_content_type_to_string (content_type)));
610 printf (", \"content-id\": %s", json_quote_str (local, cid));
613 printf (", \"filename\": %s", json_quote_str (local, filename));
615 if (GMIME_IS_PART (node->part)) {
616 /* For non-HTML text parts, we include the content in the
617 * JSON. Since JSON must be Unicode, we handle charset
618 * decoding here and do not report a charset to the caller.
619 * For text/html parts, we do not include the content. If a
620 * caller is interested in text/html parts, it should retrieve
621 * them separately and they will not be decoded. Since this
622 * makes charset decoding the responsibility on the caller, we
623 * report the charset for text/html parts.
625 if (g_mime_content_type_is_type (content_type, "text", "html")) {
626 const char *content_charset = g_mime_object_get_content_type_parameter (meta, "charset");
628 if (content_charset != NULL)
629 printf (", \"content-charset\": %s", json_quote_str (local, content_charset));
630 } else if (g_mime_content_type_is_type (content_type, "text", "*")) {
631 GMimeStream *stream_memory = g_mime_stream_mem_new ();
632 GByteArray *part_content;
633 show_text_part_content (node->part, stream_memory, 0);
634 part_content = g_mime_stream_mem_get_byte_array (GMIME_STREAM_MEM (stream_memory));
636 printf (", \"content\": %s", json_quote_chararray (local, (char *) part_content->data, part_content->len));
637 g_object_unref (stream_memory);
639 } else if (GMIME_IS_MULTIPART (node->part)) {
640 printf (", \"content\": [");
642 } else if (GMIME_IS_MESSAGE (node->part)) {
643 printf (", \"content\": [{");
644 printf ("\"headers\": ");
645 format_headers_json (local, GMIME_MESSAGE (node->part), FALSE);
647 printf (", \"body\": [");
653 for (i = 0; i < node->nchildren; i++)
654 format_part_json (ctx, mime_node_child (node, i), i == 0);
656 printf ("%s}", terminator);
659 static notmuch_status_t
660 format_part_json_entry (const void *ctx, mime_node_t *node, unused (int indent),
661 unused (const notmuch_show_params_t *params))
663 format_part_json (ctx, node, TRUE);
665 return NOTMUCH_STATUS_SUCCESS;
668 /* Print a message in "mboxrd" format as documented, for example,
671 * http://qmail.org/qmail-manual-html/man5/mbox.html
673 static notmuch_status_t
674 format_part_mbox (const void *ctx, mime_node_t *node, unused (int indent),
675 unused (const notmuch_show_params_t *params))
677 notmuch_message_t *message = node->envelope_file;
679 const char *filename;
684 struct tm date_gmtime;
685 char date_asctime[26];
692 INTERNAL_ERROR ("format_part_mbox requires a root part");
694 filename = notmuch_message_get_filename (message);
695 file = fopen (filename, "r");
697 fprintf (stderr, "Failed to open %s: %s\n",
698 filename, strerror (errno));
699 return NOTMUCH_STATUS_FILE_ERROR;
702 from = notmuch_message_get_header (message, "from");
703 from = _extract_email_address (ctx, from);
705 date = notmuch_message_get_date (message);
706 gmtime_r (&date, &date_gmtime);
707 asctime_r (&date_gmtime, date_asctime);
709 printf ("From %s %s", from, date_asctime);
711 while ((line_len = getline (&line, &line_size, file)) != -1 ) {
712 if (_is_from_line (line))
721 return NOTMUCH_STATUS_SUCCESS;
724 static notmuch_status_t
725 format_part_raw (unused (const void *ctx), mime_node_t *node,
727 unused (const notmuch_show_params_t *params))
729 if (node->envelope_file) {
730 /* Special case the entire message to avoid MIME parsing. */
731 const char *filename;
736 filename = notmuch_message_get_filename (node->envelope_file);
737 if (filename == NULL) {
738 fprintf (stderr, "Error: Cannot get message filename.\n");
739 return NOTMUCH_STATUS_FILE_ERROR;
742 file = fopen (filename, "r");
744 fprintf (stderr, "Error: Cannot open file %s: %s\n", filename, strerror (errno));
745 return NOTMUCH_STATUS_FILE_ERROR;
748 while (!feof (file)) {
749 size = fread (buf, 1, sizeof (buf), file);
751 fprintf (stderr, "Error: Read failed from %s\n", filename);
753 return NOTMUCH_STATUS_FILE_ERROR;
756 if (fwrite (buf, size, 1, stdout) != 1) {
757 fprintf (stderr, "Error: Write failed\n");
759 return NOTMUCH_STATUS_FILE_ERROR;
764 return NOTMUCH_STATUS_SUCCESS;
767 GMimeStream *stream_stdout;
768 GMimeStream *stream_filter = NULL;
770 stream_stdout = g_mime_stream_file_new (stdout);
771 g_mime_stream_file_set_owner (GMIME_STREAM_FILE (stream_stdout), FALSE);
773 stream_filter = g_mime_stream_filter_new (stream_stdout);
775 if (GMIME_IS_PART (node->part)) {
776 /* For leaf parts, we emit only the transfer-decoded
778 GMimeDataWrapper *wrapper;
779 wrapper = g_mime_part_get_content_object (GMIME_PART (node->part));
781 if (wrapper && stream_filter)
782 g_mime_data_wrapper_write_to_stream (wrapper, stream_filter);
784 /* Write out the whole part. For message parts (the root
785 * part and embedded message parts), this will be the
786 * message including its headers (but not the
787 * encapsulating part's headers). For multipart parts,
788 * this will include the headers. */
790 g_mime_object_write_to_stream (node->part, stream_filter);
794 g_object_unref (stream_filter);
797 g_object_unref(stream_stdout);
799 return NOTMUCH_STATUS_SUCCESS;
802 static notmuch_status_t
803 show_message (void *ctx,
804 const notmuch_show_format_t *format,
805 notmuch_message_t *message,
807 notmuch_show_params_t *params)
809 void *local = talloc_new (ctx);
810 mime_node_t *root, *part;
811 notmuch_status_t status;
813 status = mime_node_open (local, message, &(params->crypto), &root);
816 part = mime_node_seek_dfs (root, (params->part < 0 ? 0 : params->part));
818 status = format->part (local, part, indent, params);
824 static notmuch_status_t
825 show_messages (void *ctx,
826 const notmuch_show_format_t *format,
827 notmuch_messages_t *messages,
829 notmuch_show_params_t *params)
831 notmuch_message_t *message;
832 notmuch_bool_t match;
833 notmuch_bool_t excluded;
836 notmuch_status_t status, res = NOTMUCH_STATUS_SUCCESS;
838 if (format->message_set_start)
839 fputs (format->message_set_start, stdout);
842 notmuch_messages_valid (messages);
843 notmuch_messages_move_to_next (messages))
845 if (!first_set && format->message_set_sep)
846 fputs (format->message_set_sep, stdout);
849 if (format->message_set_start)
850 fputs (format->message_set_start, stdout);
852 message = notmuch_messages_get (messages);
854 match = notmuch_message_get_flag (message, NOTMUCH_MESSAGE_FLAG_MATCH);
855 excluded = notmuch_message_get_flag (message, NOTMUCH_MESSAGE_FLAG_EXCLUDED);
857 next_indent = indent;
859 if ((match && (!excluded || !params->omit_excluded)) || params->entire_thread) {
860 status = show_message (ctx, format, message, indent, params);
863 next_indent = indent + 1;
865 if (!status && format->message_set_sep)
866 fputs (format->message_set_sep, stdout);
869 status = show_messages (ctx,
871 notmuch_message_get_replies (message),
877 notmuch_message_destroy (message);
879 if (format->message_set_end)
880 fputs (format->message_set_end, stdout);
883 if (format->message_set_end)
884 fputs (format->message_set_end, stdout);
889 /* Formatted output of single message */
891 do_show_single (void *ctx,
892 notmuch_query_t *query,
893 const notmuch_show_format_t *format,
894 notmuch_show_params_t *params)
896 notmuch_messages_t *messages;
897 notmuch_message_t *message;
899 if (notmuch_query_count_messages (query) != 1) {
900 fprintf (stderr, "Error: search term did not match precisely one message.\n");
904 messages = notmuch_query_search_messages (query);
905 message = notmuch_messages_get (messages);
907 if (message == NULL) {
908 fprintf (stderr, "Error: Cannot find matching message.\n");
912 notmuch_message_set_flag (message, NOTMUCH_MESSAGE_FLAG_MATCH, 1);
914 return show_message (ctx, format, message, 0, params) != NOTMUCH_STATUS_SUCCESS;
917 /* Formatted output of threads */
920 notmuch_query_t *query,
921 const notmuch_show_format_t *format,
922 notmuch_show_params_t *params)
924 notmuch_threads_t *threads;
925 notmuch_thread_t *thread;
926 notmuch_messages_t *messages;
927 int first_toplevel = 1;
928 notmuch_status_t status, res = NOTMUCH_STATUS_SUCCESS;
930 if (format->message_set_start)
931 fputs (format->message_set_start, stdout);
933 for (threads = notmuch_query_search_threads (query);
934 notmuch_threads_valid (threads);
935 notmuch_threads_move_to_next (threads))
937 thread = notmuch_threads_get (threads);
939 messages = notmuch_thread_get_toplevel_messages (thread);
941 if (messages == NULL)
942 INTERNAL_ERROR ("Thread %s has no toplevel messages.\n",
943 notmuch_thread_get_thread_id (thread));
945 if (!first_toplevel && format->message_set_sep)
946 fputs (format->message_set_sep, stdout);
949 status = show_messages (ctx, format, messages, 0, params);
953 notmuch_thread_destroy (thread);
957 if (format->message_set_end)
958 fputs (format->message_set_end, stdout);
960 return res != NOTMUCH_STATUS_SUCCESS;
964 NOTMUCH_FORMAT_NOT_SPECIFIED,
971 /* The following is to allow future options to be added more easily */
978 notmuch_show_command (void *ctx, unused (int argc), unused (char *argv[]))
980 notmuch_config_t *config;
981 notmuch_database_t *notmuch;
982 notmuch_query_t *query;
985 const notmuch_show_format_t *format = &format_text;
986 notmuch_show_params_t params = {
988 .omit_excluded = TRUE,
993 int format_sel = NOTMUCH_FORMAT_NOT_SPECIFIED;
994 notmuch_bool_t verify = FALSE;
995 int exclude = EXCLUDE_TRUE;
997 notmuch_opt_desc_t options[] = {
998 { NOTMUCH_OPT_KEYWORD, &format_sel, "format", 'f',
999 (notmuch_keyword_t []){ { "json", NOTMUCH_FORMAT_JSON },
1000 { "text", NOTMUCH_FORMAT_TEXT },
1001 { "mbox", NOTMUCH_FORMAT_MBOX },
1002 { "raw", NOTMUCH_FORMAT_RAW },
1004 { NOTMUCH_OPT_KEYWORD, &exclude, "exclude", 'x',
1005 (notmuch_keyword_t []){ { "true", EXCLUDE_TRUE },
1006 { "false", EXCLUDE_FALSE },
1008 { NOTMUCH_OPT_INT, ¶ms.part, "part", 'p', 0 },
1009 { NOTMUCH_OPT_BOOLEAN, ¶ms.entire_thread, "entire-thread", 't', 0 },
1010 { NOTMUCH_OPT_BOOLEAN, ¶ms.crypto.decrypt, "decrypt", 'd', 0 },
1011 { NOTMUCH_OPT_BOOLEAN, &verify, "verify", 'v', 0 },
1015 opt_index = parse_arguments (argc, argv, options, 1);
1016 if (opt_index < 0) {
1017 /* diagnostics already printed */
1021 if (format_sel == NOTMUCH_FORMAT_NOT_SPECIFIED) {
1022 /* if part was requested and format was not specified, use format=raw */
1023 if (params.part >= 0)
1024 format_sel = NOTMUCH_FORMAT_RAW;
1026 format_sel = NOTMUCH_FORMAT_TEXT;
1029 switch (format_sel) {
1030 case NOTMUCH_FORMAT_JSON:
1031 format = &format_json;
1032 params.entire_thread = TRUE;
1034 case NOTMUCH_FORMAT_TEXT:
1035 format = &format_text;
1037 case NOTMUCH_FORMAT_MBOX:
1038 if (params.part > 0) {
1039 fprintf (stderr, "Error: specifying parts is incompatible with mbox output format.\n");
1043 format = &format_mbox;
1045 case NOTMUCH_FORMAT_RAW:
1046 format = &format_raw;
1047 /* If --format=raw specified without specifying part, we can only
1048 * output single message, so set part=0 */
1049 if (params.part < 0)
1055 if (params.crypto.decrypt || verify) {
1056 #ifdef GMIME_ATLEAST_26
1057 /* TODO: GMimePasswordRequestFunc */
1058 params.crypto.gpgctx = g_mime_gpg_context_new (NULL, "gpg");
1060 GMimeSession* session = g_object_new (g_mime_session_get_type(), NULL);
1061 params.crypto.gpgctx = g_mime_gpg_context_new (session, "gpg");
1063 if (params.crypto.gpgctx) {
1064 g_mime_gpg_context_set_always_trust ((GMimeGpgContext*) params.crypto.gpgctx, FALSE);
1066 params.crypto.decrypt = FALSE;
1067 fprintf (stderr, "Failed to construct gpg context.\n");
1069 #ifndef GMIME_ATLEAST_26
1070 g_object_unref (session);
1074 config = notmuch_config_open (ctx, NULL, NULL);
1078 query_string = query_string_from_args (ctx, argc-opt_index, argv+opt_index);
1079 if (query_string == NULL) {
1080 fprintf (stderr, "Out of memory\n");
1084 if (*query_string == '\0') {
1085 fprintf (stderr, "Error: notmuch show requires at least one search term.\n");
1089 if (notmuch_database_open (notmuch_config_get_database_path (config),
1090 NOTMUCH_DATABASE_MODE_READ_ONLY, ¬much))
1093 query = notmuch_query_create (notmuch, query_string);
1094 if (query == NULL) {
1095 fprintf (stderr, "Out of memory\n");
1099 /* If a single message is requested we do not use search_excludes. */
1100 if (params.part >= 0)
1101 ret = do_show_single (ctx, query, format, ¶ms);
1103 /* We always apply set the exclude flag. The
1104 * exclude=true|false option controls whether or not we return
1105 * threads that only match in an excluded message */
1106 const char **search_exclude_tags;
1107 size_t search_exclude_tags_length;
1110 search_exclude_tags = notmuch_config_get_search_exclude_tags
1111 (config, &search_exclude_tags_length);
1112 for (i = 0; i < search_exclude_tags_length; i++)
1113 notmuch_query_add_tag_exclude (query, search_exclude_tags[i]);
1115 if (exclude == EXCLUDE_FALSE) {
1116 notmuch_query_set_omit_excluded (query, FALSE);
1117 params.omit_excluded = FALSE;
1120 ret = do_show (ctx, query, format, ¶ms);
1123 notmuch_crypto_cleanup (¶ms.crypto);
1124 notmuch_query_destroy (query);
1125 notmuch_database_destroy (notmuch);