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 https://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_sprinter_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_sprinter_entry,
43 static const notmuch_show_format_t format_sexp = {
44 .new_sprinter = sprinter_sexp_create,
45 .part = format_part_sprinter_entry,
48 static notmuch_status_t
49 format_part_mbox (const void *ctx, sprinter_t *sp, mime_node_t *node,
50 int indent, const notmuch_show_params_t *params);
52 static const notmuch_show_format_t format_mbox = {
53 .new_sprinter = sprinter_text_create,
54 .part = format_part_mbox,
57 static notmuch_status_t
58 format_part_raw (unused (const void *ctx), sprinter_t *sp, mime_node_t *node,
60 unused (const notmuch_show_params_t *params));
62 static const notmuch_show_format_t format_raw = {
63 .new_sprinter = sprinter_text_create,
64 .part = format_part_raw,
68 _get_tags_as_string (const void *ctx, notmuch_message_t *message)
75 result = talloc_strdup (ctx, "");
79 for (tags = notmuch_message_get_tags (message);
80 notmuch_tags_valid (tags);
81 notmuch_tags_move_to_next (tags))
83 tag = notmuch_tags_get (tags);
85 result = talloc_asprintf_append (result, "%s%s",
86 first ? "" : " ", tag);
93 /* Get a nice, single-line summary of message. */
95 _get_one_line_summary (const void *ctx, notmuch_message_t *message)
99 const char *relative_date;
102 from = notmuch_message_get_header (message, "from");
104 date = notmuch_message_get_date (message);
105 relative_date = notmuch_time_relative_date (ctx, date);
107 tags = _get_tags_as_string (ctx, message);
109 return talloc_asprintf (ctx, "%s (%s) (%s)",
110 from, relative_date, tags);
113 static const char *_get_disposition(GMimeObject *meta)
115 GMimeContentDisposition *disposition;
117 disposition = g_mime_object_get_content_disposition (meta);
121 return g_mime_content_disposition_get_disposition (disposition);
124 /* Emit a sequence of key/value pairs for the metadata of message.
125 * The caller should begin a map before calling this. */
127 format_message_sprinter (sprinter_t *sp, notmuch_message_t *message)
129 /* Any changes to the JSON or S-Expression format should be
130 * reflected in the file devel/schemata. */
132 void *local = talloc_new (NULL);
133 notmuch_tags_t *tags;
135 const char *relative_date;
137 sp->map_key (sp, "id");
138 sp->string (sp, notmuch_message_get_message_id (message));
140 sp->map_key (sp, "match");
141 sp->boolean (sp, notmuch_message_get_flag (message, NOTMUCH_MESSAGE_FLAG_MATCH));
143 sp->map_key (sp, "excluded");
144 sp->boolean (sp, notmuch_message_get_flag (message, NOTMUCH_MESSAGE_FLAG_EXCLUDED));
146 sp->map_key (sp, "filename");
147 if (notmuch_format_version >= 3) {
148 notmuch_filenames_t *filenames;
151 for (filenames = notmuch_message_get_filenames (message);
152 notmuch_filenames_valid (filenames);
153 notmuch_filenames_move_to_next (filenames)) {
154 sp->string (sp, notmuch_filenames_get (filenames));
156 notmuch_filenames_destroy (filenames);
159 sp->string (sp, notmuch_message_get_filename (message));
162 sp->map_key (sp, "timestamp");
163 date = notmuch_message_get_date (message);
164 sp->integer (sp, date);
166 sp->map_key (sp, "date_relative");
167 relative_date = notmuch_time_relative_date (local, date);
168 sp->string (sp, relative_date);
170 sp->map_key (sp, "tags");
172 for (tags = notmuch_message_get_tags (message);
173 notmuch_tags_valid (tags);
174 notmuch_tags_move_to_next (tags))
175 sp->string (sp, notmuch_tags_get (tags));
181 /* Extract just the email address from the contents of a From:
184 _extract_email_address (const void *ctx, const char *from)
186 InternetAddressList *addresses;
187 InternetAddress *address;
188 InternetAddressMailbox *mailbox;
189 const char *email = "MAILER-DAEMON";
191 addresses = internet_address_list_parse_string (from);
193 /* Bail if there is no address here. */
194 if (addresses == NULL || internet_address_list_length (addresses) < 1)
197 /* Otherwise, just use the first address. */
198 address = internet_address_list_get_address (addresses, 0);
200 /* The From header should never contain an address group rather
201 * than a mailbox. So bail if it does. */
202 if (! INTERNET_ADDRESS_IS_MAILBOX (address))
205 mailbox = INTERNET_ADDRESS_MAILBOX (address);
206 email = internet_address_mailbox_get_addr (mailbox);
207 email = talloc_strdup (ctx, email);
211 g_object_unref (addresses);
216 /* Return 1 if 'line' is an mbox From_ line---that is, a line
217 * beginning with zero or more '>' characters followed by the
218 * characters 'F', 'r', 'o', 'm', and space.
220 * Any characters at all may appear after that in the line.
223 _is_from_line (const char *line)
225 const char *s = line;
233 if (STRNCMP_LITERAL (s, "From ") == 0)
240 format_headers_sprinter (sprinter_t *sp, GMimeMessage *message,
241 notmuch_bool_t reply)
243 /* Any changes to the JSON or S-Expression format should be
244 * reflected in the file devel/schemata. */
246 InternetAddressList *recipients;
247 char *recipients_string;
248 const char *reply_to_string;
253 sp->map_key (sp, "Subject");
254 sp->string (sp, g_mime_message_get_subject (message));
256 sp->map_key (sp, "From");
257 sp->string (sp, g_mime_message_get_sender (message));
259 recipients = g_mime_message_get_recipients (message, GMIME_RECIPIENT_TYPE_TO);
260 recipients_string = internet_address_list_to_string (recipients, 0);
261 if (recipients_string) {
262 sp->map_key (sp, "To");
263 sp->string (sp, recipients_string);
264 g_free (recipients_string);
267 recipients = g_mime_message_get_recipients (message, GMIME_RECIPIENT_TYPE_CC);
268 recipients_string = internet_address_list_to_string (recipients, 0);
269 if (recipients_string) {
270 sp->map_key (sp, "Cc");
271 sp->string (sp, recipients_string);
272 g_free (recipients_string);
275 recipients = g_mime_message_get_recipients (message, GMIME_RECIPIENT_TYPE_BCC);
276 recipients_string = internet_address_list_to_string (recipients, 0);
277 if (recipients_string) {
278 sp->map_key (sp, "Bcc");
279 sp->string (sp, recipients_string);
280 g_free (recipients_string);
283 reply_to_string = g_mime_message_get_reply_to (message);
284 if (reply_to_string) {
285 sp->map_key (sp, "Reply-To");
286 sp->string (sp, reply_to_string);
290 sp->map_key (sp, "In-reply-to");
291 sp->string (sp, g_mime_object_get_header (GMIME_OBJECT (message), "In-reply-to"));
293 sp->map_key (sp, "References");
294 sp->string (sp, g_mime_object_get_header (GMIME_OBJECT (message), "References"));
296 sp->map_key (sp, "Date");
297 date_string = g_mime_message_get_date_as_string (message);
298 sp->string (sp, date_string);
299 g_free (date_string);
305 /* Write a MIME text part out to the given stream.
307 * If (flags & NOTMUCH_SHOW_TEXT_PART_REPLY), this prepends "> " to
310 * Both line-ending conversion (CRLF->LF) and charset conversion ( ->
311 * UTF-8) will be performed, so it is inappropriate to call this
312 * function with a non-text part. Doing so will trigger an internal
316 show_text_part_content (GMimeObject *part, GMimeStream *stream_out,
317 notmuch_show_text_part_flags flags)
319 GMimeContentType *content_type = g_mime_object_get_content_type (GMIME_OBJECT (part));
320 GMimeStream *stream_filter = NULL;
321 GMimeDataWrapper *wrapper;
324 if (! g_mime_content_type_is_type (content_type, "text", "*"))
325 INTERNAL_ERROR ("Illegal request to format non-text part (%s) as text.",
326 g_mime_content_type_to_string (content_type));
328 if (stream_out == NULL)
331 stream_filter = g_mime_stream_filter_new (stream_out);
332 g_mime_stream_filter_add(GMIME_STREAM_FILTER (stream_filter),
333 g_mime_filter_crlf_new (FALSE, FALSE));
335 charset = g_mime_object_get_content_type_parameter (part, "charset");
337 GMimeFilter *charset_filter;
338 charset_filter = g_mime_filter_charset_new (charset, "UTF-8");
339 /* This result can be NULL for things like "unknown-8bit".
340 * Don't set a NULL filter as that makes GMime print
341 * annoying assertion-failure messages on stderr. */
342 if (charset_filter) {
343 g_mime_stream_filter_add (GMIME_STREAM_FILTER (stream_filter),
345 g_object_unref (charset_filter);
350 if (flags & NOTMUCH_SHOW_TEXT_PART_REPLY) {
351 GMimeFilter *reply_filter;
352 reply_filter = g_mime_filter_reply_new (TRUE);
354 g_mime_stream_filter_add (GMIME_STREAM_FILTER (stream_filter),
356 g_object_unref (reply_filter);
360 wrapper = g_mime_part_get_content_object (GMIME_PART (part));
361 if (wrapper && stream_filter)
362 g_mime_data_wrapper_write_to_stream (wrapper, stream_filter);
364 g_object_unref(stream_filter);
367 /* Get signature status string (GMime 2.6) */
369 signature_status_to_string (GMimeSignatureStatus x)
372 case GMIME_SIGNATURE_STATUS_GOOD:
374 case GMIME_SIGNATURE_STATUS_BAD:
376 case GMIME_SIGNATURE_STATUS_ERROR:
382 /* Signature status sprinter (GMime 2.6) */
384 format_part_sigstatus_sprinter (sprinter_t *sp, mime_node_t *node)
386 /* Any changes to the JSON or S-Expression format should be
387 * reflected in the file devel/schemata. */
389 GMimeSignatureList *siglist = node->sig_list;
399 for (i = 0; i < g_mime_signature_list_length (siglist); i++) {
400 GMimeSignature *signature = g_mime_signature_list_get_signature (siglist, i);
405 GMimeSignatureStatus status = g_mime_signature_get_status (signature);
406 sp->map_key (sp, "status");
407 sp->string (sp, signature_status_to_string (status));
409 GMimeCertificate *certificate = g_mime_signature_get_certificate (signature);
410 if (status == GMIME_SIGNATURE_STATUS_GOOD) {
412 sp->map_key (sp, "fingerprint");
413 sp->string (sp, g_mime_certificate_get_fingerprint (certificate));
415 /* these dates are seconds since the epoch; should we
416 * provide a more human-readable format string? */
417 time_t created = g_mime_signature_get_created (signature);
419 sp->map_key (sp, "created");
420 sp->integer (sp, created);
422 time_t expires = g_mime_signature_get_expires (signature);
424 sp->map_key (sp, "expires");
425 sp->integer (sp, expires);
427 /* output user id only if validity is FULL or ULTIMATE. */
428 /* note that gmime is using the term "trust" here, which
429 * is WRONG. It's actually user id "validity". */
431 const char *name = g_mime_certificate_get_name (certificate);
432 GMimeCertificateTrust trust = g_mime_certificate_get_trust (certificate);
433 if (name && (trust == GMIME_CERTIFICATE_TRUST_FULLY || trust == GMIME_CERTIFICATE_TRUST_ULTIMATE)) {
434 sp->map_key (sp, "userid");
435 sp->string (sp, name);
438 } else if (certificate) {
439 const char *key_id = g_mime_certificate_get_key_id (certificate);
441 sp->map_key (sp, "keyid");
442 sp->string (sp, key_id);
446 GMimeSignatureError errors = g_mime_signature_get_errors (signature);
447 if (errors != GMIME_SIGNATURE_ERROR_NONE) {
448 sp->map_key (sp, "errors");
449 sp->integer (sp, errors);
458 static notmuch_status_t
459 format_part_text (const void *ctx, sprinter_t *sp, mime_node_t *node,
460 int indent, const notmuch_show_params_t *params)
462 /* The disposition and content-type metadata are associated with
463 * the envelope for message parts */
464 GMimeObject *meta = node->envelope_part ?
465 GMIME_OBJECT (node->envelope_part) : node->part;
466 GMimeContentType *content_type = g_mime_object_get_content_type (meta);
467 const notmuch_bool_t leaf = GMIME_IS_PART (node->part);
468 const char *part_type;
471 if (node->envelope_file) {
472 notmuch_message_t *message = node->envelope_file;
474 part_type = "message";
475 printf ("\f%s{ id:%s depth:%d match:%d excluded:%d filename:%s\n",
477 notmuch_message_get_message_id (message),
479 notmuch_message_get_flag (message, NOTMUCH_MESSAGE_FLAG_MATCH) ? 1 : 0,
480 notmuch_message_get_flag (message, NOTMUCH_MESSAGE_FLAG_EXCLUDED) ? 1 : 0,
481 notmuch_message_get_filename (message));
483 const char *disposition = _get_disposition (meta);
484 const char *cid = g_mime_object_get_content_id (meta);
485 const char *filename = leaf ?
486 g_mime_part_get_filename (GMIME_PART (node->part)) : NULL;
489 strcasecmp (disposition, GMIME_DISPOSITION_ATTACHMENT) == 0)
490 part_type = "attachment";
494 printf ("\f%s{ ID: %d", part_type, node->part_num);
496 printf (", Filename: %s", filename);
498 printf (", Content-id: %s", cid);
499 printf (", Content-type: %s\n", g_mime_content_type_to_string (content_type));
502 if (GMIME_IS_MESSAGE (node->part)) {
503 GMimeMessage *message = GMIME_MESSAGE (node->part);
504 InternetAddressList *recipients;
505 const char *recipients_string;
507 printf ("\fheader{\n");
508 if (node->envelope_file)
509 printf ("%s\n", _get_one_line_summary (ctx, node->envelope_file));
510 printf ("Subject: %s\n", g_mime_message_get_subject (message));
511 printf ("From: %s\n", g_mime_message_get_sender (message));
512 recipients = g_mime_message_get_recipients (message, GMIME_RECIPIENT_TYPE_TO);
513 recipients_string = internet_address_list_to_string (recipients, 0);
514 if (recipients_string)
515 printf ("To: %s\n", recipients_string);
516 recipients = g_mime_message_get_recipients (message, GMIME_RECIPIENT_TYPE_CC);
517 recipients_string = internet_address_list_to_string (recipients, 0);
518 if (recipients_string)
519 printf ("Cc: %s\n", recipients_string);
520 printf ("Date: %s\n", g_mime_message_get_date_as_string (message));
521 printf ("\fheader}\n");
523 printf ("\fbody{\n");
527 if (g_mime_content_type_is_type (content_type, "text", "*") &&
528 !g_mime_content_type_is_type (content_type, "text", "html"))
530 GMimeStream *stream_stdout = g_mime_stream_file_new (stdout);
531 g_mime_stream_file_set_owner (GMIME_STREAM_FILE (stream_stdout), FALSE);
532 show_text_part_content (node->part, stream_stdout, 0);
533 g_object_unref(stream_stdout);
535 printf ("Non-text part: %s\n",
536 g_mime_content_type_to_string (content_type));
540 for (i = 0; i < node->nchildren; i++)
541 format_part_text (ctx, sp, mime_node_child (node, i), indent, params);
543 if (GMIME_IS_MESSAGE (node->part))
544 printf ("\fbody}\n");
546 printf ("\f%s}\n", part_type);
548 return NOTMUCH_STATUS_SUCCESS;
552 format_omitted_part_meta_sprinter (sprinter_t *sp, GMimeObject *meta, GMimePart *part)
554 const char *content_charset = g_mime_object_get_content_type_parameter (meta, "charset");
555 const char *cte = g_mime_object_get_header (meta, "content-transfer-encoding");
556 GMimeDataWrapper *wrapper = g_mime_part_get_content_object (part);
557 GMimeStream *stream = g_mime_data_wrapper_get_stream (wrapper);
558 ssize_t content_length = g_mime_stream_length (stream);
560 if (content_charset != NULL) {
561 sp->map_key (sp, "content-charset");
562 sp->string (sp, content_charset);
565 sp->map_key (sp, "content-transfer-encoding");
566 sp->string (sp, cte);
568 if (content_length >= 0) {
569 sp->map_key (sp, "content-length");
570 sp->integer (sp, content_length);
575 format_part_sprinter (const void *ctx, sprinter_t *sp, mime_node_t *node,
576 notmuch_bool_t first, notmuch_bool_t output_body,
577 notmuch_bool_t include_html)
579 /* Any changes to the JSON or S-Expression format should be
580 * reflected in the file devel/schemata. */
582 if (node->envelope_file) {
584 format_message_sprinter (sp, node->envelope_file);
586 sp->map_key (sp, "headers");
587 format_headers_sprinter (sp, GMIME_MESSAGE (node->part), FALSE);
590 sp->map_key (sp, "body");
592 format_part_sprinter (ctx, sp, mime_node_child (node, 0), first, TRUE, include_html);
599 /* The disposition and content-type metadata are associated with
600 * the envelope for message parts */
601 GMimeObject *meta = node->envelope_part ?
602 GMIME_OBJECT (node->envelope_part) : node->part;
603 GMimeContentType *content_type = g_mime_object_get_content_type (meta);
604 const char *disposition = _get_disposition (meta);
605 const char *cid = g_mime_object_get_content_id (meta);
606 const char *filename = GMIME_IS_PART (node->part) ?
607 g_mime_part_get_filename (GMIME_PART (node->part)) : NULL;
613 sp->map_key (sp, "id");
614 sp->integer (sp, node->part_num);
616 if (node->decrypt_attempted) {
617 sp->map_key (sp, "encstatus");
620 sp->map_key (sp, "status");
621 sp->string (sp, node->decrypt_success ? "good" : "bad");
626 if (node->verify_attempted) {
627 sp->map_key (sp, "sigstatus");
628 format_part_sigstatus_sprinter (sp, node);
631 sp->map_key (sp, "content-type");
632 sp->string (sp, g_mime_content_type_to_string (content_type));
635 sp->map_key (sp, "content-disposition");
636 sp->string (sp, disposition);
640 sp->map_key (sp, "content-id");
641 sp->string (sp, cid);
645 sp->map_key (sp, "filename");
646 sp->string (sp, filename);
649 if (GMIME_IS_PART (node->part)) {
650 /* For non-HTML text parts, we include the content in the
651 * JSON. Since JSON must be Unicode, we handle charset
652 * decoding here and do not report a charset to the caller.
653 * For text/html parts, we do not include the content unless
654 * the --include-html option has been passed. If a html part
655 * is not included, it can be requested directly. This makes
656 * charset decoding the responsibility on the caller so we
657 * report the charset for text/html parts.
659 if (g_mime_content_type_is_type (content_type, "text", "*") &&
661 ! g_mime_content_type_is_type (content_type, "text", "html")))
663 GMimeStream *stream_memory = g_mime_stream_mem_new ();
664 GByteArray *part_content;
665 show_text_part_content (node->part, stream_memory, 0);
666 part_content = g_mime_stream_mem_get_byte_array (GMIME_STREAM_MEM (stream_memory));
667 sp->map_key (sp, "content");
668 sp->string_len (sp, (char *) part_content->data, part_content->len);
669 g_object_unref (stream_memory);
671 format_omitted_part_meta_sprinter (sp, meta, GMIME_PART (node->part));
673 } else if (GMIME_IS_MULTIPART (node->part)) {
674 sp->map_key (sp, "content");
677 } else if (GMIME_IS_MESSAGE (node->part)) {
678 sp->map_key (sp, "content");
682 sp->map_key (sp, "headers");
683 format_headers_sprinter (sp, GMIME_MESSAGE (node->part), FALSE);
685 sp->map_key (sp, "body");
690 for (i = 0; i < node->nchildren; i++)
691 format_part_sprinter (ctx, sp, mime_node_child (node, i), i == 0, TRUE, include_html);
693 /* Close content structures */
694 for (i = 0; i < nclose; i++)
700 static notmuch_status_t
701 format_part_sprinter_entry (const void *ctx, sprinter_t *sp,
702 mime_node_t *node, unused (int indent),
703 const notmuch_show_params_t *params)
705 format_part_sprinter (ctx, sp, node, TRUE, params->output_body, params->include_html);
707 return NOTMUCH_STATUS_SUCCESS;
710 /* Print a message in "mboxrd" format as documented, for example,
713 * http://qmail.org/qmail-manual-html/man5/mbox.html
715 static notmuch_status_t
716 format_part_mbox (const void *ctx, unused (sprinter_t *sp), mime_node_t *node,
718 unused (const notmuch_show_params_t *params))
720 notmuch_message_t *message = node->envelope_file;
722 const char *filename;
727 struct tm date_gmtime;
728 char date_asctime[26];
735 INTERNAL_ERROR ("format_part_mbox requires a root part");
737 filename = notmuch_message_get_filename (message);
738 file = fopen (filename, "r");
740 fprintf (stderr, "Failed to open %s: %s\n",
741 filename, strerror (errno));
742 return NOTMUCH_STATUS_FILE_ERROR;
745 from = notmuch_message_get_header (message, "from");
746 from = _extract_email_address (ctx, from);
748 date = notmuch_message_get_date (message);
749 gmtime_r (&date, &date_gmtime);
750 asctime_r (&date_gmtime, date_asctime);
752 printf ("From %s %s", from, date_asctime);
754 while ((line_len = getline (&line, &line_size, file)) != -1 ) {
755 if (_is_from_line (line))
764 return NOTMUCH_STATUS_SUCCESS;
767 static notmuch_status_t
768 format_part_raw (unused (const void *ctx), unused (sprinter_t *sp),
769 mime_node_t *node, unused (int indent),
770 unused (const notmuch_show_params_t *params))
772 if (node->envelope_file) {
773 /* Special case the entire message to avoid MIME parsing. */
774 const char *filename;
779 filename = notmuch_message_get_filename (node->envelope_file);
780 if (filename == NULL) {
781 fprintf (stderr, "Error: Cannot get message filename.\n");
782 return NOTMUCH_STATUS_FILE_ERROR;
785 file = fopen (filename, "r");
787 fprintf (stderr, "Error: Cannot open file %s: %s\n", filename, strerror (errno));
788 return NOTMUCH_STATUS_FILE_ERROR;
791 while (!feof (file)) {
792 size = fread (buf, 1, sizeof (buf), file);
794 fprintf (stderr, "Error: Read failed from %s\n", filename);
796 return NOTMUCH_STATUS_FILE_ERROR;
799 if (fwrite (buf, size, 1, stdout) != 1) {
800 fprintf (stderr, "Error: Write failed\n");
802 return NOTMUCH_STATUS_FILE_ERROR;
807 return NOTMUCH_STATUS_SUCCESS;
810 GMimeStream *stream_stdout;
811 GMimeStream *stream_filter = NULL;
813 stream_stdout = g_mime_stream_file_new (stdout);
814 g_mime_stream_file_set_owner (GMIME_STREAM_FILE (stream_stdout), FALSE);
816 stream_filter = g_mime_stream_filter_new (stream_stdout);
818 if (GMIME_IS_PART (node->part)) {
819 /* For leaf parts, we emit only the transfer-decoded
821 GMimeDataWrapper *wrapper;
822 wrapper = g_mime_part_get_content_object (GMIME_PART (node->part));
824 if (wrapper && stream_filter)
825 g_mime_data_wrapper_write_to_stream (wrapper, stream_filter);
827 /* Write out the whole part. For message parts (the root
828 * part and embedded message parts), this will be the
829 * message including its headers (but not the
830 * encapsulating part's headers). For multipart parts,
831 * this will include the headers. */
833 g_mime_object_write_to_stream (node->part, stream_filter);
837 g_object_unref (stream_filter);
840 g_object_unref(stream_stdout);
842 return NOTMUCH_STATUS_SUCCESS;
845 static notmuch_status_t
846 show_message (void *ctx,
847 const notmuch_show_format_t *format,
849 notmuch_message_t *message,
851 notmuch_show_params_t *params)
853 void *local = talloc_new (ctx);
854 mime_node_t *root, *part;
855 notmuch_status_t status;
857 status = mime_node_open (local, message, &(params->crypto), &root);
860 part = mime_node_seek_dfs (root, (params->part < 0 ? 0 : params->part));
862 status = format->part (local, sp, part, indent, params);
868 static notmuch_status_t
869 show_messages (void *ctx,
870 const notmuch_show_format_t *format,
872 notmuch_messages_t *messages,
874 notmuch_show_params_t *params)
876 notmuch_message_t *message;
877 notmuch_bool_t match;
878 notmuch_bool_t excluded;
880 notmuch_status_t status, res = NOTMUCH_STATUS_SUCCESS;
885 notmuch_messages_valid (messages);
886 notmuch_messages_move_to_next (messages))
890 message = notmuch_messages_get (messages);
892 match = notmuch_message_get_flag (message, NOTMUCH_MESSAGE_FLAG_MATCH);
893 excluded = notmuch_message_get_flag (message, NOTMUCH_MESSAGE_FLAG_EXCLUDED);
895 next_indent = indent;
897 if ((match && (!excluded || !params->omit_excluded)) || params->entire_thread) {
898 status = show_message (ctx, format, sp, message, indent, params);
901 next_indent = indent + 1;
906 status = show_messages (ctx,
908 notmuch_message_get_replies (message),
914 notmuch_message_destroy (message);
924 /* Formatted output of single message */
926 do_show_single (void *ctx,
927 notmuch_query_t *query,
928 const notmuch_show_format_t *format,
930 notmuch_show_params_t *params)
932 notmuch_messages_t *messages;
933 notmuch_message_t *message;
934 notmuch_status_t status;
937 status = notmuch_query_count_messages_st (query, &count);
938 if (print_status_query ("notmuch show", query, status))
942 fprintf (stderr, "Error: search term did not match precisely one message (matched %d messages).\n", count);
946 status = notmuch_query_search_messages_st (query, &messages);
947 if (print_status_query ("notmuch show", query, status))
950 message = notmuch_messages_get (messages);
952 if (message == NULL) {
953 fprintf (stderr, "Error: Cannot find matching message.\n");
957 notmuch_message_set_flag (message, NOTMUCH_MESSAGE_FLAG_MATCH, 1);
959 return show_message (ctx, format, sp, message, 0, params)
960 != NOTMUCH_STATUS_SUCCESS;
963 /* Formatted output of threads */
966 notmuch_query_t *query,
967 const notmuch_show_format_t *format,
969 notmuch_show_params_t *params)
971 notmuch_threads_t *threads;
972 notmuch_thread_t *thread;
973 notmuch_messages_t *messages;
974 notmuch_status_t status, res = NOTMUCH_STATUS_SUCCESS;
976 status= notmuch_query_search_threads_st (query, &threads);
977 if (print_status_query ("notmuch show", query, status))
983 notmuch_threads_valid (threads);
984 notmuch_threads_move_to_next (threads))
986 thread = notmuch_threads_get (threads);
988 messages = notmuch_thread_get_toplevel_messages (thread);
990 if (messages == NULL)
991 INTERNAL_ERROR ("Thread %s has no toplevel messages.\n",
992 notmuch_thread_get_thread_id (thread));
994 status = show_messages (ctx, format, sp, messages, 0, params);
998 notmuch_thread_destroy (thread);
1004 return res != NOTMUCH_STATUS_SUCCESS;
1008 NOTMUCH_FORMAT_NOT_SPECIFIED,
1009 NOTMUCH_FORMAT_JSON,
1010 NOTMUCH_FORMAT_SEXP,
1011 NOTMUCH_FORMAT_TEXT,
1012 NOTMUCH_FORMAT_MBOX,
1017 ENTIRE_THREAD_DEFAULT,
1019 ENTIRE_THREAD_FALSE,
1022 /* The following is to allow future options to be added more easily */
1029 notmuch_show_command (notmuch_config_t *config, int argc, char *argv[])
1031 notmuch_database_t *notmuch;
1032 notmuch_query_t *query;
1035 const notmuch_show_format_t *format = &format_text;
1036 sprinter_t *sprinter;
1037 notmuch_show_params_t params = {
1039 .omit_excluded = TRUE,
1040 .output_body = TRUE,
1046 .include_html = FALSE
1048 int format_sel = NOTMUCH_FORMAT_NOT_SPECIFIED;
1049 int exclude = EXCLUDE_TRUE;
1050 int entire_thread = ENTIRE_THREAD_DEFAULT;
1052 notmuch_opt_desc_t options[] = {
1053 { NOTMUCH_OPT_KEYWORD, &format_sel, "format", 'f',
1054 (notmuch_keyword_t []){ { "json", NOTMUCH_FORMAT_JSON },
1055 { "text", NOTMUCH_FORMAT_TEXT },
1056 { "sexp", NOTMUCH_FORMAT_SEXP },
1057 { "mbox", NOTMUCH_FORMAT_MBOX },
1058 { "raw", NOTMUCH_FORMAT_RAW },
1060 { NOTMUCH_OPT_INT, ¬much_format_version, "format-version", 0, 0 },
1061 { NOTMUCH_OPT_KEYWORD, &exclude, "exclude", 'x',
1062 (notmuch_keyword_t []){ { "true", EXCLUDE_TRUE },
1063 { "false", EXCLUDE_FALSE },
1065 { NOTMUCH_OPT_KEYWORD, &entire_thread, "entire-thread", 't',
1066 (notmuch_keyword_t []){ { "true", ENTIRE_THREAD_TRUE },
1067 { "false", ENTIRE_THREAD_FALSE },
1068 { "", ENTIRE_THREAD_TRUE },
1070 { NOTMUCH_OPT_INT, ¶ms.part, "part", 'p', 0 },
1071 { NOTMUCH_OPT_BOOLEAN, ¶ms.crypto.decrypt, "decrypt", 'd', 0 },
1072 { NOTMUCH_OPT_BOOLEAN, ¶ms.crypto.verify, "verify", 'v', 0 },
1073 { NOTMUCH_OPT_BOOLEAN, ¶ms.output_body, "body", 'b', 0 },
1074 { NOTMUCH_OPT_BOOLEAN, ¶ms.include_html, "include-html", 0, 0 },
1075 { NOTMUCH_OPT_INHERIT, (void *) ¬much_shared_options, NULL, 0, 0 },
1079 opt_index = parse_arguments (argc, argv, options, 1);
1081 return EXIT_FAILURE;
1083 notmuch_process_shared_options (argv[0]);
1085 /* decryption implies verification */
1086 if (params.crypto.decrypt)
1087 params.crypto.verify = TRUE;
1089 if (format_sel == NOTMUCH_FORMAT_NOT_SPECIFIED) {
1090 /* if part was requested and format was not specified, use format=raw */
1091 if (params.part >= 0)
1092 format_sel = NOTMUCH_FORMAT_RAW;
1094 format_sel = NOTMUCH_FORMAT_TEXT;
1097 switch (format_sel) {
1098 case NOTMUCH_FORMAT_JSON:
1099 format = &format_json;
1101 case NOTMUCH_FORMAT_TEXT:
1102 format = &format_text;
1104 case NOTMUCH_FORMAT_SEXP:
1105 format = &format_sexp;
1107 case NOTMUCH_FORMAT_MBOX:
1108 if (params.part > 0) {
1109 fprintf (stderr, "Error: specifying parts is incompatible with mbox output format.\n");
1110 return EXIT_FAILURE;
1113 format = &format_mbox;
1115 case NOTMUCH_FORMAT_RAW:
1116 format = &format_raw;
1117 /* If --format=raw specified without specifying part, we can only
1118 * output single message, so set part=0 */
1119 if (params.part < 0)
1125 notmuch_exit_if_unsupported_format ();
1127 /* Default is entire-thread = FALSE except for format=json and
1129 if (entire_thread == ENTIRE_THREAD_DEFAULT) {
1130 if (format == &format_json || format == &format_sexp)
1131 entire_thread = ENTIRE_THREAD_TRUE;
1133 entire_thread = ENTIRE_THREAD_FALSE;
1136 if (!params.output_body) {
1137 if (params.part > 0) {
1138 fprintf (stderr, "Warning: --body=false is incompatible with --part > 0. Disabling.\n");
1139 params.output_body = TRUE;
1141 if (format != &format_json && format != &format_sexp)
1143 "Warning: --body=false only implemented for format=json and format=sexp\n");
1147 if (params.include_html &&
1148 (format_sel != NOTMUCH_FORMAT_JSON && format_sel != NOTMUCH_FORMAT_SEXP)) {
1149 fprintf (stderr, "Warning: --include-html only implemented for format=json and format=sexp\n");
1152 if (entire_thread == ENTIRE_THREAD_TRUE)
1153 params.entire_thread = TRUE;
1155 params.entire_thread = FALSE;
1157 query_string = query_string_from_args (config, argc-opt_index, argv+opt_index);
1158 if (query_string == NULL) {
1159 fprintf (stderr, "Out of memory\n");
1160 return EXIT_FAILURE;
1163 if (*query_string == '\0') {
1164 fprintf (stderr, "Error: notmuch show requires at least one search term.\n");
1165 return EXIT_FAILURE;
1168 params.crypto.gpgpath = notmuch_config_get_crypto_gpg_path (config);
1170 if (notmuch_database_open (notmuch_config_get_database_path (config),
1171 NOTMUCH_DATABASE_MODE_READ_ONLY, ¬much))
1172 return EXIT_FAILURE;
1174 notmuch_exit_if_unmatched_db_uuid (notmuch);
1176 query = notmuch_query_create (notmuch, query_string);
1177 if (query == NULL) {
1178 fprintf (stderr, "Out of memory\n");
1179 return EXIT_FAILURE;
1182 /* Create structure printer. */
1183 sprinter = format->new_sprinter(config, stdout);
1185 /* If a single message is requested we do not use search_excludes. */
1186 if (params.part >= 0)
1187 ret = do_show_single (config, query, format, sprinter, ¶ms);
1189 /* We always apply set the exclude flag. The
1190 * exclude=true|false option controls whether or not we return
1191 * threads that only match in an excluded message */
1192 const char **search_exclude_tags;
1193 size_t search_exclude_tags_length;
1196 search_exclude_tags = notmuch_config_get_search_exclude_tags
1197 (config, &search_exclude_tags_length);
1198 for (i = 0; i < search_exclude_tags_length; i++)
1199 notmuch_query_add_tag_exclude (query, search_exclude_tags[i]);
1201 if (exclude == EXCLUDE_FALSE) {
1202 notmuch_query_set_omit_excluded (query, FALSE);
1203 params.omit_excluded = FALSE;
1206 ret = do_show (config, query, format, sprinter, ¶ms);
1209 notmuch_crypto_cleanup (¶ms.crypto);
1210 notmuch_query_destroy (query);
1211 notmuch_database_destroy (notmuch);
1213 return ret ? EXIT_FAILURE : EXIT_SUCCESS;