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 .part = format_part_json_entry,
43 static notmuch_status_t
44 format_part_mbox (const void *ctx, sprinter_t *sp, mime_node_t *node,
45 int indent, const notmuch_show_params_t *params);
47 static const notmuch_show_format_t format_mbox = {
48 .new_sprinter = sprinter_text_create,
49 .part = format_part_mbox,
52 static notmuch_status_t
53 format_part_raw (unused (const void *ctx), sprinter_t *sp, mime_node_t *node,
55 unused (const notmuch_show_params_t *params));
57 static const notmuch_show_format_t format_raw = {
58 .new_sprinter = sprinter_text_create,
59 .part = format_part_raw,
63 _get_tags_as_string (const void *ctx, notmuch_message_t *message)
70 result = talloc_strdup (ctx, "");
74 for (tags = notmuch_message_get_tags (message);
75 notmuch_tags_valid (tags);
76 notmuch_tags_move_to_next (tags))
78 tag = notmuch_tags_get (tags);
80 result = talloc_asprintf_append (result, "%s%s",
81 first ? "" : " ", tag);
88 /* Get a nice, single-line summary of message. */
90 _get_one_line_summary (const void *ctx, notmuch_message_t *message)
94 const char *relative_date;
97 from = notmuch_message_get_header (message, "from");
99 date = notmuch_message_get_date (message);
100 relative_date = notmuch_time_relative_date (ctx, date);
102 tags = _get_tags_as_string (ctx, message);
104 return talloc_asprintf (ctx, "%s (%s) (%s)",
105 from, relative_date, tags);
108 /* Emit a sequence of key/value pairs for the metadata of message.
109 * The caller should begin a map before calling this. */
111 format_message_json (sprinter_t *sp, notmuch_message_t *message)
113 /* Any changes to the JSON format should be reflected in the file
116 void *local = talloc_new (NULL);
117 notmuch_tags_t *tags;
119 const char *relative_date;
121 sp->map_key (sp, "id");
122 sp->string (sp, notmuch_message_get_message_id (message));
124 sp->map_key (sp, "match");
125 sp->boolean (sp, notmuch_message_get_flag (message, NOTMUCH_MESSAGE_FLAG_MATCH));
127 sp->map_key (sp, "excluded");
128 sp->boolean (sp, notmuch_message_get_flag (message, NOTMUCH_MESSAGE_FLAG_EXCLUDED));
130 sp->map_key (sp, "filename");
131 sp->string (sp, notmuch_message_get_filename (message));
133 sp->map_key (sp, "timestamp");
134 date = notmuch_message_get_date (message);
135 sp->integer (sp, date);
137 sp->map_key (sp, "date_relative");
138 relative_date = notmuch_time_relative_date (local, date);
139 sp->string (sp, relative_date);
141 sp->map_key (sp, "tags");
143 for (tags = notmuch_message_get_tags (message);
144 notmuch_tags_valid (tags);
145 notmuch_tags_move_to_next (tags))
146 sp->string (sp, notmuch_tags_get (tags));
152 /* Extract just the email address from the contents of a From:
155 _extract_email_address (const void *ctx, const char *from)
157 InternetAddressList *addresses;
158 InternetAddress *address;
159 InternetAddressMailbox *mailbox;
160 const char *email = "MAILER-DAEMON";
162 addresses = internet_address_list_parse_string (from);
164 /* Bail if there is no address here. */
165 if (addresses == NULL || internet_address_list_length (addresses) < 1)
168 /* Otherwise, just use the first address. */
169 address = internet_address_list_get_address (addresses, 0);
171 /* The From header should never contain an address group rather
172 * than a mailbox. So bail if it does. */
173 if (! INTERNET_ADDRESS_IS_MAILBOX (address))
176 mailbox = INTERNET_ADDRESS_MAILBOX (address);
177 email = internet_address_mailbox_get_addr (mailbox);
178 email = talloc_strdup (ctx, email);
182 g_object_unref (addresses);
187 /* Return 1 if 'line' is an mbox From_ line---that is, a line
188 * beginning with zero or more '>' characters followed by the
189 * characters 'F', 'r', 'o', 'm', and space.
191 * Any characters at all may appear after that in the line.
194 _is_from_line (const char *line)
196 const char *s = line;
204 if (STRNCMP_LITERAL (s, "From ") == 0)
211 format_headers_json (sprinter_t *sp, GMimeMessage *message,
212 notmuch_bool_t reply)
214 /* Any changes to the JSON format should be reflected in the file
217 InternetAddressList *recipients;
218 const char *recipients_string;
219 const char *reply_to_string;
223 sp->map_key (sp, "Subject");
224 sp->string (sp, g_mime_message_get_subject (message));
226 sp->map_key (sp, "From");
227 sp->string (sp, g_mime_message_get_sender (message));
229 recipients = g_mime_message_get_recipients (message, GMIME_RECIPIENT_TYPE_TO);
230 recipients_string = internet_address_list_to_string (recipients, 0);
231 if (recipients_string) {
232 sp->map_key (sp, "To");
233 sp->string (sp, recipients_string);
236 recipients = g_mime_message_get_recipients (message, GMIME_RECIPIENT_TYPE_CC);
237 recipients_string = internet_address_list_to_string (recipients, 0);
238 if (recipients_string) {
239 sp->map_key (sp, "Cc");
240 sp->string (sp, recipients_string);
243 recipients = g_mime_message_get_recipients (message, GMIME_RECIPIENT_TYPE_BCC);
244 recipients_string = internet_address_list_to_string (recipients, 0);
245 if (recipients_string) {
246 sp->map_key (sp, "Bcc");
247 sp->string (sp, recipients_string);
250 reply_to_string = g_mime_message_get_reply_to (message);
251 if (reply_to_string) {
252 sp->map_key (sp, "Reply-To");
253 sp->string (sp, reply_to_string);
257 sp->map_key (sp, "In-reply-to");
258 sp->string (sp, g_mime_object_get_header (GMIME_OBJECT (message), "In-reply-to"));
260 sp->map_key (sp, "References");
261 sp->string (sp, g_mime_object_get_header (GMIME_OBJECT (message), "References"));
263 sp->map_key (sp, "Date");
264 sp->string (sp, g_mime_message_get_date_as_string (message));
270 /* Write a MIME text part out to the given stream.
272 * If (flags & NOTMUCH_SHOW_TEXT_PART_REPLY), this prepends "> " to
275 * Both line-ending conversion (CRLF->LF) and charset conversion ( ->
276 * UTF-8) will be performed, so it is inappropriate to call this
277 * function with a non-text part. Doing so will trigger an internal
281 show_text_part_content (GMimeObject *part, GMimeStream *stream_out,
282 notmuch_show_text_part_flags flags)
284 GMimeContentType *content_type = g_mime_object_get_content_type (GMIME_OBJECT (part));
285 GMimeStream *stream_filter = NULL;
286 GMimeDataWrapper *wrapper;
289 if (! g_mime_content_type_is_type (content_type, "text", "*"))
290 INTERNAL_ERROR ("Illegal request to format non-text part (%s) as text.",
291 g_mime_content_type_to_string (content_type));
293 if (stream_out == NULL)
296 stream_filter = g_mime_stream_filter_new (stream_out);
297 g_mime_stream_filter_add(GMIME_STREAM_FILTER (stream_filter),
298 g_mime_filter_crlf_new (FALSE, FALSE));
300 charset = g_mime_object_get_content_type_parameter (part, "charset");
302 GMimeFilter *charset_filter;
303 charset_filter = g_mime_filter_charset_new (charset, "UTF-8");
304 /* This result can be NULL for things like "unknown-8bit".
305 * Don't set a NULL filter as that makes GMime print
306 * annoying assertion-failure messages on stderr. */
307 if (charset_filter) {
308 g_mime_stream_filter_add (GMIME_STREAM_FILTER (stream_filter),
310 g_object_unref (charset_filter);
315 if (flags & NOTMUCH_SHOW_TEXT_PART_REPLY) {
316 GMimeFilter *reply_filter;
317 reply_filter = g_mime_filter_reply_new (TRUE);
319 g_mime_stream_filter_add (GMIME_STREAM_FILTER (stream_filter),
321 g_object_unref (reply_filter);
325 wrapper = g_mime_part_get_content_object (GMIME_PART (part));
326 if (wrapper && stream_filter)
327 g_mime_data_wrapper_write_to_stream (wrapper, stream_filter);
329 g_object_unref(stream_filter);
332 #ifdef GMIME_ATLEAST_26
334 signature_status_to_string (GMimeSignatureStatus x)
337 case GMIME_SIGNATURE_STATUS_GOOD:
339 case GMIME_SIGNATURE_STATUS_BAD:
341 case GMIME_SIGNATURE_STATUS_ERROR:
348 signer_status_to_string (GMimeSignerStatus x)
351 case GMIME_SIGNER_STATUS_NONE:
353 case GMIME_SIGNER_STATUS_GOOD:
355 case GMIME_SIGNER_STATUS_BAD:
357 case GMIME_SIGNER_STATUS_ERROR:
364 #ifdef GMIME_ATLEAST_26
366 format_part_sigstatus_json (sprinter_t *sp, mime_node_t *node)
368 /* Any changes to the JSON format should be reflected in the file
371 GMimeSignatureList *siglist = node->sig_list;
381 for (i = 0; i < g_mime_signature_list_length (siglist); i++) {
382 GMimeSignature *signature = g_mime_signature_list_get_signature (siglist, i);
387 GMimeSignatureStatus status = g_mime_signature_get_status (signature);
388 sp->map_key (sp, "status");
389 sp->string (sp, signature_status_to_string (status));
391 GMimeCertificate *certificate = g_mime_signature_get_certificate (signature);
392 if (status == GMIME_SIGNATURE_STATUS_GOOD) {
394 sp->map_key (sp, "fingerprint");
395 sp->string (sp, g_mime_certificate_get_fingerprint (certificate));
397 /* these dates are seconds since the epoch; should we
398 * provide a more human-readable format string? */
399 time_t created = g_mime_signature_get_created (signature);
401 sp->map_key (sp, "created");
402 sp->integer (sp, created);
404 time_t expires = g_mime_signature_get_expires (signature);
406 sp->map_key (sp, "expires");
407 sp->integer (sp, expires);
409 /* output user id only if validity is FULL or ULTIMATE. */
410 /* note that gmime is using the term "trust" here, which
411 * is WRONG. It's actually user id "validity". */
413 const char *name = g_mime_certificate_get_name (certificate);
414 GMimeCertificateTrust trust = g_mime_certificate_get_trust (certificate);
415 if (name && (trust == GMIME_CERTIFICATE_TRUST_FULLY || trust == GMIME_CERTIFICATE_TRUST_ULTIMATE)) {
416 sp->map_key (sp, "userid");
417 sp->string (sp, name);
420 } else if (certificate) {
421 const char *key_id = g_mime_certificate_get_key_id (certificate);
423 sp->map_key (sp, "keyid");
424 sp->string (sp, key_id);
428 GMimeSignatureError errors = g_mime_signature_get_errors (signature);
429 if (errors != GMIME_SIGNATURE_ERROR_NONE) {
430 sp->map_key (sp, "errors");
431 sp->integer (sp, errors);
441 format_part_sigstatus_json (sprinter_t *sp, mime_node_t *node)
443 const GMimeSignatureValidity* validity = node->sig_validity;
452 const GMimeSigner *signer = g_mime_signature_validity_get_signers (validity);
457 sp->map_key (sp, "status");
458 sp->string (sp, signer_status_to_string (signer->status));
460 if (signer->status == GMIME_SIGNER_STATUS_GOOD)
462 if (signer->fingerprint) {
463 sp->map_key (sp, "fingerprint");
464 sp->string (sp, signer->fingerprint);
466 /* these dates are seconds since the epoch; should we
467 * provide a more human-readable format string? */
468 if (signer->created) {
469 sp->map_key (sp, "created");
470 sp->integer (sp, signer->created);
472 if (signer->expires) {
473 sp->map_key (sp, "expires");
474 sp->integer (sp, signer->expires);
476 /* output user id only if validity is FULL or ULTIMATE. */
477 /* note that gmime is using the term "trust" here, which
478 * is WRONG. It's actually user id "validity". */
479 if ((signer->name) && (signer->trust)) {
480 if ((signer->trust == GMIME_SIGNER_TRUST_FULLY) || (signer->trust == GMIME_SIGNER_TRUST_ULTIMATE)) {
481 sp->map_key (sp, "userid");
482 sp->string (sp, signer->name);
487 sp->map_key (sp, "keyid");
488 sp->string (sp, signer->keyid);
491 if (signer->errors != GMIME_SIGNER_ERROR_NONE) {
492 sp->map_key (sp, "errors");
493 sp->integer (sp, signer->errors);
497 signer = signer->next;
504 static notmuch_status_t
505 format_part_text (const void *ctx, sprinter_t *sp, mime_node_t *node,
506 int indent, const notmuch_show_params_t *params)
508 /* The disposition and content-type metadata are associated with
509 * the envelope for message parts */
510 GMimeObject *meta = node->envelope_part ?
511 GMIME_OBJECT (node->envelope_part) : node->part;
512 GMimeContentType *content_type = g_mime_object_get_content_type (meta);
513 const notmuch_bool_t leaf = GMIME_IS_PART (node->part);
514 const char *part_type;
517 if (node->envelope_file) {
518 notmuch_message_t *message = node->envelope_file;
520 part_type = "message";
521 printf ("\f%s{ id:%s depth:%d match:%d excluded:%d filename:%s\n",
523 notmuch_message_get_message_id (message),
525 notmuch_message_get_flag (message, NOTMUCH_MESSAGE_FLAG_MATCH) ? 1 : 0,
526 notmuch_message_get_flag (message, NOTMUCH_MESSAGE_FLAG_EXCLUDED) ? 1 : 0,
527 notmuch_message_get_filename (message));
529 GMimeContentDisposition *disposition = g_mime_object_get_content_disposition (meta);
530 const char *cid = g_mime_object_get_content_id (meta);
531 const char *filename = leaf ?
532 g_mime_part_get_filename (GMIME_PART (node->part)) : NULL;
535 strcmp (disposition->disposition, GMIME_DISPOSITION_ATTACHMENT) == 0)
536 part_type = "attachment";
540 printf ("\f%s{ ID: %d", part_type, node->part_num);
542 printf (", Filename: %s", filename);
544 printf (", Content-id: %s", cid);
545 printf (", Content-type: %s\n", g_mime_content_type_to_string (content_type));
548 if (GMIME_IS_MESSAGE (node->part)) {
549 GMimeMessage *message = GMIME_MESSAGE (node->part);
550 InternetAddressList *recipients;
551 const char *recipients_string;
553 printf ("\fheader{\n");
554 if (node->envelope_file)
555 printf ("%s\n", _get_one_line_summary (ctx, node->envelope_file));
556 printf ("Subject: %s\n", g_mime_message_get_subject (message));
557 printf ("From: %s\n", g_mime_message_get_sender (message));
558 recipients = g_mime_message_get_recipients (message, GMIME_RECIPIENT_TYPE_TO);
559 recipients_string = internet_address_list_to_string (recipients, 0);
560 if (recipients_string)
561 printf ("To: %s\n", recipients_string);
562 recipients = g_mime_message_get_recipients (message, GMIME_RECIPIENT_TYPE_CC);
563 recipients_string = internet_address_list_to_string (recipients, 0);
564 if (recipients_string)
565 printf ("Cc: %s\n", recipients_string);
566 printf ("Date: %s\n", g_mime_message_get_date_as_string (message));
567 printf ("\fheader}\n");
569 printf ("\fbody{\n");
573 if (g_mime_content_type_is_type (content_type, "text", "*") &&
574 !g_mime_content_type_is_type (content_type, "text", "html"))
576 GMimeStream *stream_stdout = g_mime_stream_file_new (stdout);
577 g_mime_stream_file_set_owner (GMIME_STREAM_FILE (stream_stdout), FALSE);
578 show_text_part_content (node->part, stream_stdout, 0);
579 g_object_unref(stream_stdout);
581 printf ("Non-text part: %s\n",
582 g_mime_content_type_to_string (content_type));
586 for (i = 0; i < node->nchildren; i++)
587 format_part_text (ctx, sp, mime_node_child (node, i), indent, params);
589 if (GMIME_IS_MESSAGE (node->part))
590 printf ("\fbody}\n");
592 printf ("\f%s}\n", part_type);
594 return NOTMUCH_STATUS_SUCCESS;
598 format_part_json (const void *ctx, sprinter_t *sp, mime_node_t *node,
599 notmuch_bool_t first, notmuch_bool_t output_body)
601 /* Any changes to the JSON format should be reflected in the file
604 if (node->envelope_file) {
606 format_message_json (sp, node->envelope_file);
608 sp->map_key (sp, "headers");
609 format_headers_json (sp, GMIME_MESSAGE (node->part), FALSE);
612 sp->map_key (sp, "body");
614 format_part_json (ctx, sp, mime_node_child (node, 0), first, TRUE);
621 /* The disposition and content-type metadata are associated with
622 * the envelope for message parts */
623 GMimeObject *meta = node->envelope_part ?
624 GMIME_OBJECT (node->envelope_part) : node->part;
625 GMimeContentType *content_type = g_mime_object_get_content_type (meta);
626 const char *cid = g_mime_object_get_content_id (meta);
627 const char *filename = GMIME_IS_PART (node->part) ?
628 g_mime_part_get_filename (GMIME_PART (node->part)) : NULL;
634 sp->map_key (sp, "id");
635 sp->integer (sp, node->part_num);
637 if (node->decrypt_attempted) {
638 sp->map_key (sp, "encstatus");
641 sp->map_key (sp, "status");
642 sp->string (sp, node->decrypt_success ? "good" : "bad");
647 if (node->verify_attempted) {
648 sp->map_key (sp, "sigstatus");
649 format_part_sigstatus_json (sp, node);
652 sp->map_key (sp, "content-type");
653 sp->string (sp, g_mime_content_type_to_string (content_type));
656 sp->map_key (sp, "content-id");
657 sp->string (sp, cid);
661 sp->map_key (sp, "filename");
662 sp->string (sp, filename);
665 if (GMIME_IS_PART (node->part)) {
666 /* For non-HTML text parts, we include the content in the
667 * JSON. Since JSON must be Unicode, we handle charset
668 * decoding here and do not report a charset to the caller.
669 * For text/html parts, we do not include the content. If a
670 * caller is interested in text/html parts, it should retrieve
671 * them separately and they will not be decoded. Since this
672 * makes charset decoding the responsibility on the caller, we
673 * report the charset for text/html parts.
675 if (g_mime_content_type_is_type (content_type, "text", "html")) {
676 const char *content_charset = g_mime_object_get_content_type_parameter (meta, "charset");
678 if (content_charset != NULL) {
679 sp->map_key (sp, "content-charset");
680 sp->string (sp, content_charset);
682 } else if (g_mime_content_type_is_type (content_type, "text", "*")) {
683 GMimeStream *stream_memory = g_mime_stream_mem_new ();
684 GByteArray *part_content;
685 show_text_part_content (node->part, stream_memory, 0);
686 part_content = g_mime_stream_mem_get_byte_array (GMIME_STREAM_MEM (stream_memory));
687 sp->map_key (sp, "content");
688 sp->string_len (sp, (char *) part_content->data, part_content->len);
689 g_object_unref (stream_memory);
691 } else if (GMIME_IS_MULTIPART (node->part)) {
692 sp->map_key (sp, "content");
695 } else if (GMIME_IS_MESSAGE (node->part)) {
696 sp->map_key (sp, "content");
700 sp->map_key (sp, "headers");
701 format_headers_json (sp, GMIME_MESSAGE (node->part), FALSE);
703 sp->map_key (sp, "body");
708 for (i = 0; i < node->nchildren; i++)
709 format_part_json (ctx, sp, mime_node_child (node, i), i == 0, TRUE);
711 /* Close content structures */
712 for (i = 0; i < nclose; i++)
718 static notmuch_status_t
719 format_part_json_entry (const void *ctx, sprinter_t *sp,
720 mime_node_t *node, unused (int indent),
721 const notmuch_show_params_t *params)
723 format_part_json (ctx, sp, node, TRUE, params->output_body);
725 return NOTMUCH_STATUS_SUCCESS;
728 /* Print a message in "mboxrd" format as documented, for example,
731 * http://qmail.org/qmail-manual-html/man5/mbox.html
733 static notmuch_status_t
734 format_part_mbox (const void *ctx, unused (sprinter_t *sp), mime_node_t *node,
736 unused (const notmuch_show_params_t *params))
738 notmuch_message_t *message = node->envelope_file;
740 const char *filename;
745 struct tm date_gmtime;
746 char date_asctime[26];
753 INTERNAL_ERROR ("format_part_mbox requires a root part");
755 filename = notmuch_message_get_filename (message);
756 file = fopen (filename, "r");
758 fprintf (stderr, "Failed to open %s: %s\n",
759 filename, strerror (errno));
760 return NOTMUCH_STATUS_FILE_ERROR;
763 from = notmuch_message_get_header (message, "from");
764 from = _extract_email_address (ctx, from);
766 date = notmuch_message_get_date (message);
767 gmtime_r (&date, &date_gmtime);
768 asctime_r (&date_gmtime, date_asctime);
770 printf ("From %s %s", from, date_asctime);
772 while ((line_len = getline (&line, &line_size, file)) != -1 ) {
773 if (_is_from_line (line))
782 return NOTMUCH_STATUS_SUCCESS;
785 static notmuch_status_t
786 format_part_raw (unused (const void *ctx), unused (sprinter_t *sp),
787 mime_node_t *node, unused (int indent),
788 unused (const notmuch_show_params_t *params))
790 if (node->envelope_file) {
791 /* Special case the entire message to avoid MIME parsing. */
792 const char *filename;
797 filename = notmuch_message_get_filename (node->envelope_file);
798 if (filename == NULL) {
799 fprintf (stderr, "Error: Cannot get message filename.\n");
800 return NOTMUCH_STATUS_FILE_ERROR;
803 file = fopen (filename, "r");
805 fprintf (stderr, "Error: Cannot open file %s: %s\n", filename, strerror (errno));
806 return NOTMUCH_STATUS_FILE_ERROR;
809 while (!feof (file)) {
810 size = fread (buf, 1, sizeof (buf), file);
812 fprintf (stderr, "Error: Read failed from %s\n", filename);
814 return NOTMUCH_STATUS_FILE_ERROR;
817 if (fwrite (buf, size, 1, stdout) != 1) {
818 fprintf (stderr, "Error: Write failed\n");
820 return NOTMUCH_STATUS_FILE_ERROR;
825 return NOTMUCH_STATUS_SUCCESS;
828 GMimeStream *stream_stdout;
829 GMimeStream *stream_filter = NULL;
831 stream_stdout = g_mime_stream_file_new (stdout);
832 g_mime_stream_file_set_owner (GMIME_STREAM_FILE (stream_stdout), FALSE);
834 stream_filter = g_mime_stream_filter_new (stream_stdout);
836 if (GMIME_IS_PART (node->part)) {
837 /* For leaf parts, we emit only the transfer-decoded
839 GMimeDataWrapper *wrapper;
840 wrapper = g_mime_part_get_content_object (GMIME_PART (node->part));
842 if (wrapper && stream_filter)
843 g_mime_data_wrapper_write_to_stream (wrapper, stream_filter);
845 /* Write out the whole part. For message parts (the root
846 * part and embedded message parts), this will be the
847 * message including its headers (but not the
848 * encapsulating part's headers). For multipart parts,
849 * this will include the headers. */
851 g_mime_object_write_to_stream (node->part, stream_filter);
855 g_object_unref (stream_filter);
858 g_object_unref(stream_stdout);
860 return NOTMUCH_STATUS_SUCCESS;
863 static notmuch_status_t
864 show_message (void *ctx,
865 const notmuch_show_format_t *format,
867 notmuch_message_t *message,
869 notmuch_show_params_t *params)
871 void *local = talloc_new (ctx);
872 mime_node_t *root, *part;
873 notmuch_status_t status;
875 status = mime_node_open (local, message, &(params->crypto), &root);
878 part = mime_node_seek_dfs (root, (params->part < 0 ? 0 : params->part));
880 status = format->part (local, sp, part, indent, params);
886 static notmuch_status_t
887 show_messages (void *ctx,
888 const notmuch_show_format_t *format,
890 notmuch_messages_t *messages,
892 notmuch_show_params_t *params)
894 notmuch_message_t *message;
895 notmuch_bool_t match;
896 notmuch_bool_t excluded;
898 notmuch_status_t status, res = NOTMUCH_STATUS_SUCCESS;
903 notmuch_messages_valid (messages);
904 notmuch_messages_move_to_next (messages))
908 message = notmuch_messages_get (messages);
910 match = notmuch_message_get_flag (message, NOTMUCH_MESSAGE_FLAG_MATCH);
911 excluded = notmuch_message_get_flag (message, NOTMUCH_MESSAGE_FLAG_EXCLUDED);
913 next_indent = indent;
915 if ((match && (!excluded || !params->omit_excluded)) || params->entire_thread) {
916 status = show_message (ctx, format, sp, message, indent, params);
919 next_indent = indent + 1;
924 status = show_messages (ctx,
926 notmuch_message_get_replies (message),
932 notmuch_message_destroy (message);
942 /* Formatted output of single message */
944 do_show_single (void *ctx,
945 notmuch_query_t *query,
946 const notmuch_show_format_t *format,
948 notmuch_show_params_t *params)
950 notmuch_messages_t *messages;
951 notmuch_message_t *message;
953 if (notmuch_query_count_messages (query) != 1) {
954 fprintf (stderr, "Error: search term did not match precisely one message.\n");
958 messages = notmuch_query_search_messages (query);
959 message = notmuch_messages_get (messages);
961 if (message == NULL) {
962 fprintf (stderr, "Error: Cannot find matching message.\n");
966 notmuch_message_set_flag (message, NOTMUCH_MESSAGE_FLAG_MATCH, 1);
968 return show_message (ctx, format, sp, message, 0, params)
969 != NOTMUCH_STATUS_SUCCESS;
972 /* Formatted output of threads */
975 notmuch_query_t *query,
976 const notmuch_show_format_t *format,
978 notmuch_show_params_t *params)
980 notmuch_threads_t *threads;
981 notmuch_thread_t *thread;
982 notmuch_messages_t *messages;
983 notmuch_status_t status, res = NOTMUCH_STATUS_SUCCESS;
987 for (threads = notmuch_query_search_threads (query);
988 notmuch_threads_valid (threads);
989 notmuch_threads_move_to_next (threads))
991 thread = notmuch_threads_get (threads);
993 messages = notmuch_thread_get_toplevel_messages (thread);
995 if (messages == NULL)
996 INTERNAL_ERROR ("Thread %s has no toplevel messages.\n",
997 notmuch_thread_get_thread_id (thread));
999 status = show_messages (ctx, format, sp, messages, 0, params);
1003 notmuch_thread_destroy (thread);
1009 return res != NOTMUCH_STATUS_SUCCESS;
1013 NOTMUCH_FORMAT_NOT_SPECIFIED,
1014 NOTMUCH_FORMAT_JSON,
1015 NOTMUCH_FORMAT_TEXT,
1016 NOTMUCH_FORMAT_MBOX,
1021 ENTIRE_THREAD_DEFAULT,
1023 ENTIRE_THREAD_FALSE,
1026 /* The following is to allow future options to be added more easily */
1033 notmuch_show_command (void *ctx, unused (int argc), unused (char *argv[]))
1035 notmuch_config_t *config;
1036 notmuch_database_t *notmuch;
1037 notmuch_query_t *query;
1040 const notmuch_show_format_t *format = &format_text;
1041 sprinter_t *sprinter;
1042 notmuch_show_params_t params = {
1044 .omit_excluded = TRUE,
1045 .output_body = TRUE,
1051 int format_sel = NOTMUCH_FORMAT_NOT_SPECIFIED;
1052 int exclude = EXCLUDE_TRUE;
1053 int entire_thread = ENTIRE_THREAD_DEFAULT;
1055 notmuch_opt_desc_t options[] = {
1056 { NOTMUCH_OPT_KEYWORD, &format_sel, "format", 'f',
1057 (notmuch_keyword_t []){ { "json", NOTMUCH_FORMAT_JSON },
1058 { "text", NOTMUCH_FORMAT_TEXT },
1059 { "mbox", NOTMUCH_FORMAT_MBOX },
1060 { "raw", NOTMUCH_FORMAT_RAW },
1062 { NOTMUCH_OPT_KEYWORD, &exclude, "exclude", 'x',
1063 (notmuch_keyword_t []){ { "true", EXCLUDE_TRUE },
1064 { "false", EXCLUDE_FALSE },
1066 { NOTMUCH_OPT_KEYWORD, &entire_thread, "entire-thread", 't',
1067 (notmuch_keyword_t []){ { "true", ENTIRE_THREAD_TRUE },
1068 { "false", ENTIRE_THREAD_FALSE },
1069 { "", ENTIRE_THREAD_TRUE },
1071 { NOTMUCH_OPT_INT, ¶ms.part, "part", 'p', 0 },
1072 { NOTMUCH_OPT_BOOLEAN, ¶ms.crypto.decrypt, "decrypt", 'd', 0 },
1073 { NOTMUCH_OPT_BOOLEAN, ¶ms.crypto.verify, "verify", 'v', 0 },
1074 { NOTMUCH_OPT_BOOLEAN, ¶ms.output_body, "body", 'b', 0 },
1078 opt_index = parse_arguments (argc, argv, options, 1);
1079 if (opt_index < 0) {
1080 /* diagnostics already printed */
1084 /* decryption implies verification */
1085 if (params.crypto.decrypt)
1086 params.crypto.verify = TRUE;
1088 if (format_sel == NOTMUCH_FORMAT_NOT_SPECIFIED) {
1089 /* if part was requested and format was not specified, use format=raw */
1090 if (params.part >= 0)
1091 format_sel = NOTMUCH_FORMAT_RAW;
1093 format_sel = NOTMUCH_FORMAT_TEXT;
1096 switch (format_sel) {
1097 case NOTMUCH_FORMAT_JSON:
1098 format = &format_json;
1100 case NOTMUCH_FORMAT_TEXT:
1101 format = &format_text;
1103 case NOTMUCH_FORMAT_MBOX:
1104 if (params.part > 0) {
1105 fprintf (stderr, "Error: specifying parts is incompatible with mbox output format.\n");
1109 format = &format_mbox;
1111 case NOTMUCH_FORMAT_RAW:
1112 format = &format_raw;
1113 /* If --format=raw specified without specifying part, we can only
1114 * output single message, so set part=0 */
1115 if (params.part < 0)
1121 /* Default is entire-thread = FALSE except for format=json. */
1122 if (entire_thread == ENTIRE_THREAD_DEFAULT) {
1123 if (format == &format_json)
1124 entire_thread = ENTIRE_THREAD_TRUE;
1126 entire_thread = ENTIRE_THREAD_FALSE;
1129 if (!params.output_body) {
1130 if (params.part > 0) {
1131 fprintf (stderr, "Warning: --body=false is incompatible with --part > 0. Disabling.\n");
1132 params.output_body = TRUE;
1134 if (format != &format_json)
1135 fprintf (stderr, "Warning: --body=false only implemented for format=json\n");
1139 if (entire_thread == ENTIRE_THREAD_TRUE)
1140 params.entire_thread = TRUE;
1142 params.entire_thread = FALSE;
1144 config = notmuch_config_open (ctx, NULL, NULL);
1148 query_string = query_string_from_args (ctx, argc-opt_index, argv+opt_index);
1149 if (query_string == NULL) {
1150 fprintf (stderr, "Out of memory\n");
1154 if (*query_string == '\0') {
1155 fprintf (stderr, "Error: notmuch show requires at least one search term.\n");
1159 if (notmuch_database_open (notmuch_config_get_database_path (config),
1160 NOTMUCH_DATABASE_MODE_READ_ONLY, ¬much))
1163 query = notmuch_query_create (notmuch, query_string);
1164 if (query == NULL) {
1165 fprintf (stderr, "Out of memory\n");
1169 /* Create structure printer. */
1170 sprinter = format->new_sprinter(ctx, stdout);
1172 /* If a single message is requested we do not use search_excludes. */
1173 if (params.part >= 0)
1174 ret = do_show_single (ctx, query, format, sprinter, ¶ms);
1176 /* We always apply set the exclude flag. The
1177 * exclude=true|false option controls whether or not we return
1178 * threads that only match in an excluded message */
1179 const char **search_exclude_tags;
1180 size_t search_exclude_tags_length;
1183 search_exclude_tags = notmuch_config_get_search_exclude_tags
1184 (config, &search_exclude_tags_length);
1185 for (i = 0; i < search_exclude_tags_length; i++)
1186 notmuch_query_add_tag_exclude (query, search_exclude_tags[i]);
1188 if (exclude == EXCLUDE_FALSE) {
1189 notmuch_query_set_omit_excluded (query, FALSE);
1190 params.omit_excluded = FALSE;
1193 ret = do_show (ctx, query, format, sprinter, ¶ms);
1196 notmuch_crypto_cleanup (¶ms.crypto);
1197 notmuch_query_destroy (query);
1198 notmuch_database_destroy (notmuch);