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 .message_set_start = "",
30 .part = format_part_text,
31 .message_set_sep = "",
35 static notmuch_status_t
36 format_part_json_entry (const void *ctx, mime_node_t *node,
37 int indent, const notmuch_show_params_t *params);
39 static const notmuch_show_format_t format_json = {
40 .message_set_start = "[",
41 .part = format_part_json_entry,
42 .message_set_sep = ", ",
43 .message_set_end = "]"
46 static notmuch_status_t
47 format_part_mbox (const void *ctx, mime_node_t *node,
48 int indent, const notmuch_show_params_t *params);
50 static const notmuch_show_format_t format_mbox = {
51 .message_set_start = "",
52 .part = format_part_mbox,
53 .message_set_sep = "",
57 static notmuch_status_t
58 format_part_raw (unused (const void *ctx), mime_node_t *node,
60 unused (const notmuch_show_params_t *params));
62 static const notmuch_show_format_t format_raw = {
63 .message_set_start = "",
64 .part = format_part_raw,
65 .message_set_sep = "",
70 _get_tags_as_string (const void *ctx, notmuch_message_t *message)
77 result = talloc_strdup (ctx, "");
81 for (tags = notmuch_message_get_tags (message);
82 notmuch_tags_valid (tags);
83 notmuch_tags_move_to_next (tags))
85 tag = notmuch_tags_get (tags);
87 result = talloc_asprintf_append (result, "%s%s",
88 first ? "" : " ", tag);
95 /* Get a nice, single-line summary of message. */
97 _get_one_line_summary (const void *ctx, notmuch_message_t *message)
101 const char *relative_date;
104 from = notmuch_message_get_header (message, "from");
106 date = notmuch_message_get_date (message);
107 relative_date = notmuch_time_relative_date (ctx, date);
109 tags = _get_tags_as_string (ctx, message);
111 return talloc_asprintf (ctx, "%s (%s) (%s)",
112 from, relative_date, tags);
116 format_message_json (const void *ctx, notmuch_message_t *message)
118 notmuch_tags_t *tags;
120 void *ctx_quote = talloc_new (ctx);
122 const char *relative_date;
124 date = notmuch_message_get_date (message);
125 relative_date = notmuch_time_relative_date (ctx, date);
127 printf ("\"id\": %s, \"match\": %s, \"excluded\": %s, \"filename\": %s, \"timestamp\": %ld, \"date_relative\": \"%s\", \"tags\": [",
128 json_quote_str (ctx_quote, notmuch_message_get_message_id (message)),
129 notmuch_message_get_flag (message, NOTMUCH_MESSAGE_FLAG_MATCH) ? "true" : "false",
130 notmuch_message_get_flag (message, NOTMUCH_MESSAGE_FLAG_EXCLUDED) ? "true" : "false",
131 json_quote_str (ctx_quote, notmuch_message_get_filename (message)),
132 date, relative_date);
134 for (tags = notmuch_message_get_tags (message);
135 notmuch_tags_valid (tags);
136 notmuch_tags_move_to_next (tags))
138 printf("%s%s", first ? "" : ",",
139 json_quote_str (ctx_quote, notmuch_tags_get (tags)));
143 talloc_free (ctx_quote);
146 /* Extract just the email address from the contents of a From:
149 _extract_email_address (const void *ctx, const char *from)
151 InternetAddressList *addresses;
152 InternetAddress *address;
153 InternetAddressMailbox *mailbox;
154 const char *email = "MAILER-DAEMON";
156 addresses = internet_address_list_parse_string (from);
158 /* Bail if there is no address here. */
159 if (addresses == NULL || internet_address_list_length (addresses) < 1)
162 /* Otherwise, just use the first address. */
163 address = internet_address_list_get_address (addresses, 0);
165 /* The From header should never contain an address group rather
166 * than a mailbox. So bail if it does. */
167 if (! INTERNET_ADDRESS_IS_MAILBOX (address))
170 mailbox = INTERNET_ADDRESS_MAILBOX (address);
171 email = internet_address_mailbox_get_addr (mailbox);
172 email = talloc_strdup (ctx, email);
176 g_object_unref (addresses);
181 /* Return 1 if 'line' is an mbox From_ line---that is, a line
182 * beginning with zero or more '>' characters followed by the
183 * characters 'F', 'r', 'o', 'm', and space.
185 * Any characters at all may appear after that in the line.
188 _is_from_line (const char *line)
190 const char *s = line;
198 if (STRNCMP_LITERAL (s, "From ") == 0)
205 format_headers_json (const void *ctx, GMimeMessage *message, notmuch_bool_t reply)
207 void *local = talloc_new (ctx);
208 InternetAddressList *recipients;
209 const char *recipients_string;
212 json_quote_str (local, "Subject"),
213 json_quote_str (local, g_mime_message_get_subject (message)));
215 json_quote_str (local, "From"),
216 json_quote_str (local, 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)
221 json_quote_str (local, "To"),
222 json_quote_str (local, 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)
227 json_quote_str (local, "Cc"),
228 json_quote_str (local, recipients_string));
232 json_quote_str (local, "In-reply-to"),
233 json_quote_str (local, g_mime_object_get_header (GMIME_OBJECT (message), "In-reply-to")));
236 json_quote_str (local, "References"),
237 json_quote_str (local, g_mime_object_get_header (GMIME_OBJECT (message), "References")));
240 json_quote_str (local, "Date"),
241 json_quote_str (local, g_mime_message_get_date_as_string (message)));
249 /* Write a MIME text part out to the given stream.
251 * If (flags & NOTMUCH_SHOW_TEXT_PART_REPLY), this prepends "> " to
254 * Both line-ending conversion (CRLF->LF) and charset conversion ( ->
255 * UTF-8) will be performed, so it is inappropriate to call this
256 * function with a non-text part. Doing so will trigger an internal
260 show_text_part_content (GMimeObject *part, GMimeStream *stream_out,
261 notmuch_show_text_part_flags flags)
263 GMimeContentType *content_type = g_mime_object_get_content_type (GMIME_OBJECT (part));
264 GMimeStream *stream_filter = NULL;
265 GMimeDataWrapper *wrapper;
268 if (! g_mime_content_type_is_type (content_type, "text", "*"))
269 INTERNAL_ERROR ("Illegal request to format non-text part (%s) as text.",
270 g_mime_content_type_to_string (content_type));
272 if (stream_out == NULL)
275 stream_filter = g_mime_stream_filter_new (stream_out);
276 g_mime_stream_filter_add(GMIME_STREAM_FILTER (stream_filter),
277 g_mime_filter_crlf_new (FALSE, FALSE));
279 charset = g_mime_object_get_content_type_parameter (part, "charset");
281 GMimeFilter *charset_filter;
282 charset_filter = g_mime_filter_charset_new (charset, "UTF-8");
283 /* This result can be NULL for things like "unknown-8bit".
284 * Don't set a NULL filter as that makes GMime print
285 * annoying assertion-failure messages on stderr. */
286 if (charset_filter) {
287 g_mime_stream_filter_add (GMIME_STREAM_FILTER (stream_filter),
289 g_object_unref (charset_filter);
294 if (flags & NOTMUCH_SHOW_TEXT_PART_REPLY) {
295 GMimeFilter *reply_filter;
296 reply_filter = g_mime_filter_reply_new (TRUE);
298 g_mime_stream_filter_add (GMIME_STREAM_FILTER (stream_filter),
300 g_object_unref (reply_filter);
304 wrapper = g_mime_part_get_content_object (GMIME_PART (part));
305 if (wrapper && stream_filter)
306 g_mime_data_wrapper_write_to_stream (wrapper, stream_filter);
308 g_object_unref(stream_filter);
311 #ifdef GMIME_ATLEAST_26
313 signature_status_to_string (GMimeSignatureStatus x)
316 case GMIME_SIGNATURE_STATUS_GOOD:
318 case GMIME_SIGNATURE_STATUS_BAD:
320 case GMIME_SIGNATURE_STATUS_ERROR:
327 signer_status_to_string (GMimeSignerStatus x)
330 case GMIME_SIGNER_STATUS_NONE:
332 case GMIME_SIGNER_STATUS_GOOD:
334 case GMIME_SIGNER_STATUS_BAD:
336 case GMIME_SIGNER_STATUS_ERROR:
343 #ifdef GMIME_ATLEAST_26
345 format_part_sigstatus_json (mime_node_t *node)
347 GMimeSignatureList *siglist = node->sig_list;
356 void *ctx_quote = talloc_new (NULL);
358 for (i = 0; i < g_mime_signature_list_length (siglist); i++) {
359 GMimeSignature *signature = g_mime_signature_list_get_signature (siglist, i);
367 GMimeSignatureStatus status = g_mime_signature_get_status (signature);
368 printf ("\"status\": %s",
369 json_quote_str (ctx_quote,
370 signature_status_to_string (status)));
372 GMimeCertificate *certificate = g_mime_signature_get_certificate (signature);
373 if (status == GMIME_SIGNATURE_STATUS_GOOD) {
375 printf (", \"fingerprint\": %s", json_quote_str (ctx_quote, 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 printf (", \"created\": %d", (int) created);
381 time_t expires = g_mime_signature_get_expires (signature);
383 printf (", \"expires\": %d", (int) expires);
384 /* output user id only if validity is FULL or ULTIMATE. */
385 /* note that gmime is using the term "trust" here, which
386 * is WRONG. It's actually user id "validity". */
388 const char *name = g_mime_certificate_get_name (certificate);
389 GMimeCertificateTrust trust = g_mime_certificate_get_trust (certificate);
390 if (name && (trust == GMIME_CERTIFICATE_TRUST_FULLY || trust == GMIME_CERTIFICATE_TRUST_ULTIMATE))
391 printf (", \"userid\": %s", json_quote_str (ctx_quote, name));
393 } else if (certificate) {
394 const char *key_id = g_mime_certificate_get_key_id (certificate);
396 printf (", \"keyid\": %s", json_quote_str (ctx_quote, key_id));
399 GMimeSignatureError errors = g_mime_signature_get_errors (signature);
400 if (errors != GMIME_SIGNATURE_ERROR_NONE) {
401 printf (", \"errors\": %d", errors);
409 talloc_free (ctx_quote);
413 format_part_sigstatus_json (mime_node_t *node)
415 const GMimeSignatureValidity* validity = node->sig_validity;
424 const GMimeSigner *signer = g_mime_signature_validity_get_signers (validity);
426 void *ctx_quote = talloc_new (NULL);
437 printf ("\"status\": %s",
438 json_quote_str (ctx_quote,
439 signer_status_to_string (signer->status)));
441 if (signer->status == GMIME_SIGNER_STATUS_GOOD)
443 if (signer->fingerprint)
444 printf (", \"fingerprint\": %s", json_quote_str (ctx_quote, signer->fingerprint));
445 /* these dates are seconds since the epoch; should we
446 * provide a more human-readable format string? */
448 printf (", \"created\": %d", (int) signer->created);
450 printf (", \"expires\": %d", (int) signer->expires);
451 /* output user id only if validity is FULL or ULTIMATE. */
452 /* note that gmime is using the term "trust" here, which
453 * is WRONG. It's actually user id "validity". */
454 if ((signer->name) && (signer->trust)) {
455 if ((signer->trust == GMIME_SIGNER_TRUST_FULLY) || (signer->trust == GMIME_SIGNER_TRUST_ULTIMATE))
456 printf (", \"userid\": %s", json_quote_str (ctx_quote, signer->name));
460 printf (", \"keyid\": %s", json_quote_str (ctx_quote, signer->keyid));
462 if (signer->errors != GMIME_SIGNER_ERROR_NONE) {
463 printf (", \"errors\": %d", signer->errors);
467 signer = signer->next;
472 talloc_free (ctx_quote);
476 static notmuch_status_t
477 format_part_text (const void *ctx, mime_node_t *node,
478 int indent, const notmuch_show_params_t *params)
480 /* The disposition and content-type metadata are associated with
481 * the envelope for message parts */
482 GMimeObject *meta = node->envelope_part ?
483 GMIME_OBJECT (node->envelope_part) : node->part;
484 GMimeContentType *content_type = g_mime_object_get_content_type (meta);
485 const notmuch_bool_t leaf = GMIME_IS_PART (node->part);
486 const char *part_type;
489 if (node->envelope_file) {
490 notmuch_message_t *message = node->envelope_file;
492 part_type = "message";
493 printf ("\f%s{ id:%s depth:%d match:%d excluded:%d filename:%s\n",
495 notmuch_message_get_message_id (message),
497 notmuch_message_get_flag (message, NOTMUCH_MESSAGE_FLAG_MATCH) ? 1 : 0,
498 notmuch_message_get_flag (message, NOTMUCH_MESSAGE_FLAG_EXCLUDED) ? 1 : 0,
499 notmuch_message_get_filename (message));
501 GMimeContentDisposition *disposition = g_mime_object_get_content_disposition (meta);
502 const char *cid = g_mime_object_get_content_id (meta);
503 const char *filename = leaf ?
504 g_mime_part_get_filename (GMIME_PART (node->part)) : NULL;
507 strcmp (disposition->disposition, GMIME_DISPOSITION_ATTACHMENT) == 0)
508 part_type = "attachment";
512 printf ("\f%s{ ID: %d", part_type, node->part_num);
514 printf (", Filename: %s", filename);
516 printf (", Content-id: %s", cid);
517 printf (", Content-type: %s\n", g_mime_content_type_to_string (content_type));
520 if (GMIME_IS_MESSAGE (node->part)) {
521 GMimeMessage *message = GMIME_MESSAGE (node->part);
522 InternetAddressList *recipients;
523 const char *recipients_string;
525 printf ("\fheader{\n");
526 if (node->envelope_file)
527 printf ("%s\n", _get_one_line_summary (ctx, node->envelope_file));
528 printf ("Subject: %s\n", g_mime_message_get_subject (message));
529 printf ("From: %s\n", g_mime_message_get_sender (message));
530 recipients = g_mime_message_get_recipients (message, GMIME_RECIPIENT_TYPE_TO);
531 recipients_string = internet_address_list_to_string (recipients, 0);
532 if (recipients_string)
533 printf ("To: %s\n", recipients_string);
534 recipients = g_mime_message_get_recipients (message, GMIME_RECIPIENT_TYPE_CC);
535 recipients_string = internet_address_list_to_string (recipients, 0);
536 if (recipients_string)
537 printf ("Cc: %s\n", recipients_string);
538 printf ("Date: %s\n", g_mime_message_get_date_as_string (message));
539 printf ("\fheader}\n");
541 printf ("\fbody{\n");
545 if (g_mime_content_type_is_type (content_type, "text", "*") &&
546 !g_mime_content_type_is_type (content_type, "text", "html"))
548 GMimeStream *stream_stdout = g_mime_stream_file_new (stdout);
549 g_mime_stream_file_set_owner (GMIME_STREAM_FILE (stream_stdout), FALSE);
550 show_text_part_content (node->part, stream_stdout, 0);
551 g_object_unref(stream_stdout);
553 printf ("Non-text part: %s\n",
554 g_mime_content_type_to_string (content_type));
558 for (i = 0; i < node->nchildren; i++)
559 format_part_text (ctx, mime_node_child (node, i), indent, params);
561 if (GMIME_IS_MESSAGE (node->part))
562 printf ("\fbody}\n");
564 printf ("\f%s}\n", part_type);
566 return NOTMUCH_STATUS_SUCCESS;
570 format_part_json (const void *ctx, mime_node_t *node, notmuch_bool_t first)
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 (ctx, GMIME_MESSAGE (node->part), FALSE);
582 printf (", \"body\": [");
583 format_part_json (ctx, mime_node_child (node, 0), first);
589 void *local = talloc_new (ctx);
590 /* The disposition and content-type metadata are associated with
591 * the envelope for message parts */
592 GMimeObject *meta = node->envelope_part ?
593 GMIME_OBJECT (node->envelope_part) : node->part;
594 GMimeContentType *content_type = g_mime_object_get_content_type (meta);
595 const char *cid = g_mime_object_get_content_id (meta);
596 const char *filename = GMIME_IS_PART (node->part) ?
597 g_mime_part_get_filename (GMIME_PART (node->part)) : NULL;
598 const char *terminator = "";
604 printf ("{\"id\": %d", node->part_num);
606 if (node->decrypt_attempted)
607 printf (", \"encstatus\": [{\"status\": \"%s\"}]",
608 node->decrypt_success ? "good" : "bad");
610 if (node->verify_attempted) {
611 printf (", \"sigstatus\": ");
612 format_part_sigstatus_json (node);
615 printf (", \"content-type\": %s",
616 json_quote_str (local, g_mime_content_type_to_string (content_type)));
619 printf (", \"content-id\": %s", json_quote_str (local, cid));
622 printf (", \"filename\": %s", json_quote_str (local, filename));
624 if (GMIME_IS_PART (node->part)) {
625 /* For non-HTML text parts, we include the content in the
626 * JSON. Since JSON must be Unicode, we handle charset
627 * decoding here and do not report a charset to the caller.
628 * For text/html parts, we do not include the content. If a
629 * caller is interested in text/html parts, it should retrieve
630 * them separately and they will not be decoded. Since this
631 * makes charset decoding the responsibility on the caller, we
632 * report the charset for text/html parts.
634 if (g_mime_content_type_is_type (content_type, "text", "html")) {
635 const char *content_charset = g_mime_object_get_content_type_parameter (meta, "charset");
637 if (content_charset != NULL)
638 printf (", \"content-charset\": %s", json_quote_str (local, content_charset));
639 } else if (g_mime_content_type_is_type (content_type, "text", "*")) {
640 GMimeStream *stream_memory = g_mime_stream_mem_new ();
641 GByteArray *part_content;
642 show_text_part_content (node->part, stream_memory, 0);
643 part_content = g_mime_stream_mem_get_byte_array (GMIME_STREAM_MEM (stream_memory));
645 printf (", \"content\": %s", json_quote_chararray (local, (char *) part_content->data, part_content->len));
646 g_object_unref (stream_memory);
648 } else if (GMIME_IS_MULTIPART (node->part)) {
649 printf (", \"content\": [");
651 } else if (GMIME_IS_MESSAGE (node->part)) {
652 printf (", \"content\": [{");
653 printf ("\"headers\": ");
654 format_headers_json (local, GMIME_MESSAGE (node->part), FALSE);
656 printf (", \"body\": [");
662 for (i = 0; i < node->nchildren; i++)
663 format_part_json (ctx, mime_node_child (node, i), i == 0);
665 printf ("%s}", terminator);
668 static notmuch_status_t
669 format_part_json_entry (const void *ctx, mime_node_t *node, unused (int indent),
670 unused (const notmuch_show_params_t *params))
672 format_part_json (ctx, node, TRUE);
674 return NOTMUCH_STATUS_SUCCESS;
677 /* Print a message in "mboxrd" format as documented, for example,
680 * http://qmail.org/qmail-manual-html/man5/mbox.html
682 static notmuch_status_t
683 format_part_mbox (const void *ctx, mime_node_t *node, unused (int indent),
684 unused (const notmuch_show_params_t *params))
686 notmuch_message_t *message = node->envelope_file;
688 const char *filename;
693 struct tm date_gmtime;
694 char date_asctime[26];
701 INTERNAL_ERROR ("format_part_mbox requires a root part");
703 filename = notmuch_message_get_filename (message);
704 file = fopen (filename, "r");
706 fprintf (stderr, "Failed to open %s: %s\n",
707 filename, strerror (errno));
708 return NOTMUCH_STATUS_FILE_ERROR;
711 from = notmuch_message_get_header (message, "from");
712 from = _extract_email_address (ctx, from);
714 date = notmuch_message_get_date (message);
715 gmtime_r (&date, &date_gmtime);
716 asctime_r (&date_gmtime, date_asctime);
718 printf ("From %s %s", from, date_asctime);
720 while ((line_len = getline (&line, &line_size, file)) != -1 ) {
721 if (_is_from_line (line))
730 return NOTMUCH_STATUS_SUCCESS;
733 static notmuch_status_t
734 format_part_raw (unused (const void *ctx), mime_node_t *node,
736 unused (const notmuch_show_params_t *params))
738 if (node->envelope_file) {
739 /* Special case the entire message to avoid MIME parsing. */
740 const char *filename;
745 filename = notmuch_message_get_filename (node->envelope_file);
746 if (filename == NULL) {
747 fprintf (stderr, "Error: Cannot get message filename.\n");
748 return NOTMUCH_STATUS_FILE_ERROR;
751 file = fopen (filename, "r");
753 fprintf (stderr, "Error: Cannot open file %s: %s\n", filename, strerror (errno));
754 return NOTMUCH_STATUS_FILE_ERROR;
757 while (!feof (file)) {
758 size = fread (buf, 1, sizeof (buf), file);
760 fprintf (stderr, "Error: Read failed from %s\n", filename);
762 return NOTMUCH_STATUS_FILE_ERROR;
765 if (fwrite (buf, size, 1, stdout) != 1) {
766 fprintf (stderr, "Error: Write failed\n");
768 return NOTMUCH_STATUS_FILE_ERROR;
773 return NOTMUCH_STATUS_SUCCESS;
776 GMimeStream *stream_stdout;
777 GMimeStream *stream_filter = NULL;
779 stream_stdout = g_mime_stream_file_new (stdout);
780 g_mime_stream_file_set_owner (GMIME_STREAM_FILE (stream_stdout), FALSE);
782 stream_filter = g_mime_stream_filter_new (stream_stdout);
784 if (GMIME_IS_PART (node->part)) {
785 /* For leaf parts, we emit only the transfer-decoded
787 GMimeDataWrapper *wrapper;
788 wrapper = g_mime_part_get_content_object (GMIME_PART (node->part));
790 if (wrapper && stream_filter)
791 g_mime_data_wrapper_write_to_stream (wrapper, stream_filter);
793 /* Write out the whole part. For message parts (the root
794 * part and embedded message parts), this will be the
795 * message including its headers (but not the
796 * encapsulating part's headers). For multipart parts,
797 * this will include the headers. */
799 g_mime_object_write_to_stream (node->part, stream_filter);
803 g_object_unref (stream_filter);
806 g_object_unref(stream_stdout);
808 return NOTMUCH_STATUS_SUCCESS;
811 static notmuch_status_t
812 show_message (void *ctx,
813 const notmuch_show_format_t *format,
814 notmuch_message_t *message,
816 notmuch_show_params_t *params)
818 void *local = talloc_new (ctx);
819 mime_node_t *root, *part;
820 notmuch_status_t status;
822 status = mime_node_open (local, message, params->cryptoctx,
823 params->decrypt, &root);
826 part = mime_node_seek_dfs (root, (params->part < 0 ? 0 : params->part));
828 status = format->part (local, part, indent, params);
834 static notmuch_status_t
835 show_messages (void *ctx,
836 const notmuch_show_format_t *format,
837 notmuch_messages_t *messages,
839 notmuch_show_params_t *params)
841 notmuch_message_t *message;
842 notmuch_bool_t match;
843 notmuch_bool_t excluded;
846 notmuch_status_t status, res = NOTMUCH_STATUS_SUCCESS;
848 fputs (format->message_set_start, stdout);
851 notmuch_messages_valid (messages);
852 notmuch_messages_move_to_next (messages))
855 fputs (format->message_set_sep, stdout);
858 fputs (format->message_set_start, stdout);
860 message = notmuch_messages_get (messages);
862 match = notmuch_message_get_flag (message, NOTMUCH_MESSAGE_FLAG_MATCH);
863 excluded = notmuch_message_get_flag (message, NOTMUCH_MESSAGE_FLAG_EXCLUDED);
865 next_indent = indent;
867 if ((match && (!excluded || !params->omit_excluded)) || params->entire_thread) {
868 status = show_message (ctx, format, message, indent, params);
871 next_indent = indent + 1;
874 fputs (format->message_set_sep, stdout);
877 status = show_messages (ctx,
879 notmuch_message_get_replies (message),
885 notmuch_message_destroy (message);
887 fputs (format->message_set_end, stdout);
890 fputs (format->message_set_end, stdout);
895 /* Formatted output of single message */
897 do_show_single (void *ctx,
898 notmuch_query_t *query,
899 const notmuch_show_format_t *format,
900 notmuch_show_params_t *params)
902 notmuch_messages_t *messages;
903 notmuch_message_t *message;
905 if (notmuch_query_count_messages (query) != 1) {
906 fprintf (stderr, "Error: search term did not match precisely one message.\n");
910 messages = notmuch_query_search_messages (query);
911 message = notmuch_messages_get (messages);
913 if (message == NULL) {
914 fprintf (stderr, "Error: Cannot find matching message.\n");
918 notmuch_message_set_flag (message, NOTMUCH_MESSAGE_FLAG_MATCH, 1);
920 return show_message (ctx, format, message, 0, params) != NOTMUCH_STATUS_SUCCESS;
923 /* Formatted output of threads */
926 notmuch_query_t *query,
927 const notmuch_show_format_t *format,
928 notmuch_show_params_t *params)
930 notmuch_threads_t *threads;
931 notmuch_thread_t *thread;
932 notmuch_messages_t *messages;
933 int first_toplevel = 1;
934 notmuch_status_t status, res = NOTMUCH_STATUS_SUCCESS;
936 fputs (format->message_set_start, stdout);
938 for (threads = notmuch_query_search_threads (query);
939 notmuch_threads_valid (threads);
940 notmuch_threads_move_to_next (threads))
942 thread = notmuch_threads_get (threads);
944 messages = notmuch_thread_get_toplevel_messages (thread);
946 if (messages == NULL)
947 INTERNAL_ERROR ("Thread %s has no toplevel messages.\n",
948 notmuch_thread_get_thread_id (thread));
951 fputs (format->message_set_sep, stdout);
954 status = show_messages (ctx, format, messages, 0, params);
958 notmuch_thread_destroy (thread);
962 fputs (format->message_set_end, stdout);
964 return res != NOTMUCH_STATUS_SUCCESS;
968 NOTMUCH_FORMAT_NOT_SPECIFIED,
975 /* The following is to allow future options to be added more easily */
982 notmuch_show_command (void *ctx, unused (int argc), unused (char *argv[]))
984 notmuch_config_t *config;
985 notmuch_database_t *notmuch;
986 notmuch_query_t *query;
989 const notmuch_show_format_t *format = &format_text;
990 notmuch_show_params_t params = { .part = -1, .omit_excluded = TRUE };
991 int format_sel = NOTMUCH_FORMAT_NOT_SPECIFIED;
992 notmuch_bool_t verify = FALSE;
993 int exclude = EXCLUDE_TRUE;
995 notmuch_opt_desc_t options[] = {
996 { NOTMUCH_OPT_KEYWORD, &format_sel, "format", 'f',
997 (notmuch_keyword_t []){ { "json", NOTMUCH_FORMAT_JSON },
998 { "text", NOTMUCH_FORMAT_TEXT },
999 { "mbox", NOTMUCH_FORMAT_MBOX },
1000 { "raw", NOTMUCH_FORMAT_RAW },
1002 { NOTMUCH_OPT_KEYWORD, &exclude, "exclude", 'x',
1003 (notmuch_keyword_t []){ { "true", EXCLUDE_TRUE },
1004 { "false", EXCLUDE_FALSE },
1006 { NOTMUCH_OPT_INT, ¶ms.part, "part", 'p', 0 },
1007 { NOTMUCH_OPT_BOOLEAN, ¶ms.entire_thread, "entire-thread", 't', 0 },
1008 { NOTMUCH_OPT_BOOLEAN, ¶ms.decrypt, "decrypt", 'd', 0 },
1009 { NOTMUCH_OPT_BOOLEAN, &verify, "verify", 'v', 0 },
1013 opt_index = parse_arguments (argc, argv, options, 1);
1014 if (opt_index < 0) {
1015 /* diagnostics already printed */
1019 if (format_sel == NOTMUCH_FORMAT_NOT_SPECIFIED) {
1020 /* if part was requested and format was not specified, use format=raw */
1021 if (params.part >= 0)
1022 format_sel = NOTMUCH_FORMAT_RAW;
1024 format_sel = NOTMUCH_FORMAT_TEXT;
1027 switch (format_sel) {
1028 case NOTMUCH_FORMAT_JSON:
1029 format = &format_json;
1030 params.entire_thread = TRUE;
1032 case NOTMUCH_FORMAT_TEXT:
1033 format = &format_text;
1035 case NOTMUCH_FORMAT_MBOX:
1036 if (params.part > 0) {
1037 fprintf (stderr, "Error: specifying parts is incompatible with mbox output format.\n");
1041 format = &format_mbox;
1043 case NOTMUCH_FORMAT_RAW:
1044 format = &format_raw;
1045 /* If --format=raw specified without specifying part, we can only
1046 * output single message, so set part=0 */
1047 if (params.part < 0)
1053 if (params.decrypt || verify) {
1054 #ifdef GMIME_ATLEAST_26
1055 /* TODO: GMimePasswordRequestFunc */
1056 params.cryptoctx = g_mime_gpg_context_new (NULL, "gpg");
1058 GMimeSession* session = g_object_new (g_mime_session_get_type(), NULL);
1059 params.cryptoctx = g_mime_gpg_context_new (session, "gpg");
1061 if (params.cryptoctx) {
1062 g_mime_gpg_context_set_always_trust ((GMimeGpgContext*) params.cryptoctx, FALSE);
1064 params.decrypt = FALSE;
1065 fprintf (stderr, "Failed to construct gpg context.\n");
1067 #ifndef GMIME_ATLEAST_26
1068 g_object_unref (session);
1072 config = notmuch_config_open (ctx, NULL, NULL);
1076 query_string = query_string_from_args (ctx, argc-opt_index, argv+opt_index);
1077 if (query_string == NULL) {
1078 fprintf (stderr, "Out of memory\n");
1082 if (*query_string == '\0') {
1083 fprintf (stderr, "Error: notmuch show requires at least one search term.\n");
1087 notmuch = notmuch_database_open (notmuch_config_get_database_path (config),
1088 NOTMUCH_DATABASE_MODE_READ_ONLY);
1089 if (notmuch == NULL)
1092 query = notmuch_query_create (notmuch, query_string);
1093 if (query == NULL) {
1094 fprintf (stderr, "Out of memory\n");
1098 /* If a single message is requested we do not use search_excludes. */
1099 if (params.part >= 0)
1100 ret = do_show_single (ctx, query, format, ¶ms);
1102 /* We always apply set the exclude flag. The
1103 * exclude=true|false option controls whether or not we return
1104 * threads that only match in an excluded message */
1105 const char **search_exclude_tags;
1106 size_t search_exclude_tags_length;
1109 search_exclude_tags = notmuch_config_get_search_exclude_tags
1110 (config, &search_exclude_tags_length);
1111 for (i = 0; i < search_exclude_tags_length; i++)
1112 notmuch_query_add_tag_exclude (query, search_exclude_tags[i]);
1114 if (exclude == EXCLUDE_FALSE) {
1115 notmuch_query_set_omit_excluded (query, FALSE);
1116 params.omit_excluded = FALSE;
1119 ret = do_show (ctx, query, format, ¶ms);
1122 notmuch_query_destroy (query);
1123 notmuch_database_close (notmuch);
1125 if (params.cryptoctx)
1126 g_object_unref(params.cryptoctx);