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_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 /* Emit a sequence of key/value pairs for the metadata of message.
114 * The caller should begin a map before calling this. */
116 format_message_sprinter (sprinter_t *sp, notmuch_message_t *message)
118 /* Any changes to the JSON or S-Expression format should be
119 * reflected in the file devel/schemata. */
121 void *local = talloc_new (NULL);
122 notmuch_tags_t *tags;
124 const char *relative_date;
126 sp->map_key (sp, "id");
127 sp->string (sp, notmuch_message_get_message_id (message));
129 sp->map_key (sp, "match");
130 sp->boolean (sp, notmuch_message_get_flag (message, NOTMUCH_MESSAGE_FLAG_MATCH));
132 sp->map_key (sp, "excluded");
133 sp->boolean (sp, notmuch_message_get_flag (message, NOTMUCH_MESSAGE_FLAG_EXCLUDED));
135 sp->map_key (sp, "filename");
136 sp->string (sp, notmuch_message_get_filename (message));
138 sp->map_key (sp, "timestamp");
139 date = notmuch_message_get_date (message);
140 sp->integer (sp, date);
142 sp->map_key (sp, "date_relative");
143 relative_date = notmuch_time_relative_date (local, date);
144 sp->string (sp, relative_date);
146 sp->map_key (sp, "tags");
148 for (tags = notmuch_message_get_tags (message);
149 notmuch_tags_valid (tags);
150 notmuch_tags_move_to_next (tags))
151 sp->string (sp, notmuch_tags_get (tags));
157 /* Extract just the email address from the contents of a From:
160 _extract_email_address (const void *ctx, const char *from)
162 InternetAddressList *addresses;
163 InternetAddress *address;
164 InternetAddressMailbox *mailbox;
165 const char *email = "MAILER-DAEMON";
167 addresses = internet_address_list_parse_string (from);
169 /* Bail if there is no address here. */
170 if (addresses == NULL || internet_address_list_length (addresses) < 1)
173 /* Otherwise, just use the first address. */
174 address = internet_address_list_get_address (addresses, 0);
176 /* The From header should never contain an address group rather
177 * than a mailbox. So bail if it does. */
178 if (! INTERNET_ADDRESS_IS_MAILBOX (address))
181 mailbox = INTERNET_ADDRESS_MAILBOX (address);
182 email = internet_address_mailbox_get_addr (mailbox);
183 email = talloc_strdup (ctx, email);
187 g_object_unref (addresses);
192 /* Return 1 if 'line' is an mbox From_ line---that is, a line
193 * beginning with zero or more '>' characters followed by the
194 * characters 'F', 'r', 'o', 'm', and space.
196 * Any characters at all may appear after that in the line.
199 _is_from_line (const char *line)
201 const char *s = line;
209 if (STRNCMP_LITERAL (s, "From ") == 0)
216 format_headers_sprinter (sprinter_t *sp, GMimeMessage *message,
217 notmuch_bool_t reply)
219 /* Any changes to the JSON or S-Expression format should be
220 * reflected in the file devel/schemata. */
222 InternetAddressList *recipients;
223 const char *recipients_string;
224 const char *reply_to_string;
228 sp->map_key (sp, "Subject");
229 sp->string (sp, g_mime_message_get_subject (message));
231 sp->map_key (sp, "From");
232 sp->string (sp, g_mime_message_get_sender (message));
234 recipients = g_mime_message_get_recipients (message, GMIME_RECIPIENT_TYPE_TO);
235 recipients_string = internet_address_list_to_string (recipients, 0);
236 if (recipients_string) {
237 sp->map_key (sp, "To");
238 sp->string (sp, recipients_string);
241 recipients = g_mime_message_get_recipients (message, GMIME_RECIPIENT_TYPE_CC);
242 recipients_string = internet_address_list_to_string (recipients, 0);
243 if (recipients_string) {
244 sp->map_key (sp, "Cc");
245 sp->string (sp, recipients_string);
248 recipients = g_mime_message_get_recipients (message, GMIME_RECIPIENT_TYPE_BCC);
249 recipients_string = internet_address_list_to_string (recipients, 0);
250 if (recipients_string) {
251 sp->map_key (sp, "Bcc");
252 sp->string (sp, recipients_string);
255 reply_to_string = g_mime_message_get_reply_to (message);
256 if (reply_to_string) {
257 sp->map_key (sp, "Reply-To");
258 sp->string (sp, reply_to_string);
262 sp->map_key (sp, "In-reply-to");
263 sp->string (sp, g_mime_object_get_header (GMIME_OBJECT (message), "In-reply-to"));
265 sp->map_key (sp, "References");
266 sp->string (sp, g_mime_object_get_header (GMIME_OBJECT (message), "References"));
268 sp->map_key (sp, "Date");
269 sp->string (sp, g_mime_message_get_date_as_string (message));
275 /* Write a MIME text part out to the given stream.
277 * If (flags & NOTMUCH_SHOW_TEXT_PART_REPLY), this prepends "> " to
280 * Both line-ending conversion (CRLF->LF) and charset conversion ( ->
281 * UTF-8) will be performed, so it is inappropriate to call this
282 * function with a non-text part. Doing so will trigger an internal
286 show_text_part_content (GMimeObject *part, GMimeStream *stream_out,
287 notmuch_show_text_part_flags flags)
289 GMimeContentType *content_type = g_mime_object_get_content_type (GMIME_OBJECT (part));
290 GMimeStream *stream_filter = NULL;
291 GMimeDataWrapper *wrapper;
294 if (! g_mime_content_type_is_type (content_type, "text", "*"))
295 INTERNAL_ERROR ("Illegal request to format non-text part (%s) as text.",
296 g_mime_content_type_to_string (content_type));
298 if (stream_out == NULL)
301 stream_filter = g_mime_stream_filter_new (stream_out);
302 g_mime_stream_filter_add(GMIME_STREAM_FILTER (stream_filter),
303 g_mime_filter_crlf_new (FALSE, FALSE));
305 charset = g_mime_object_get_content_type_parameter (part, "charset");
307 GMimeFilter *charset_filter;
308 charset_filter = g_mime_filter_charset_new (charset, "UTF-8");
309 /* This result can be NULL for things like "unknown-8bit".
310 * Don't set a NULL filter as that makes GMime print
311 * annoying assertion-failure messages on stderr. */
312 if (charset_filter) {
313 g_mime_stream_filter_add (GMIME_STREAM_FILTER (stream_filter),
315 g_object_unref (charset_filter);
320 if (flags & NOTMUCH_SHOW_TEXT_PART_REPLY) {
321 GMimeFilter *reply_filter;
322 reply_filter = g_mime_filter_reply_new (TRUE);
324 g_mime_stream_filter_add (GMIME_STREAM_FILTER (stream_filter),
326 g_object_unref (reply_filter);
330 wrapper = g_mime_part_get_content_object (GMIME_PART (part));
331 if (wrapper && stream_filter)
332 g_mime_data_wrapper_write_to_stream (wrapper, stream_filter);
334 g_object_unref(stream_filter);
337 #ifdef GMIME_ATLEAST_26
339 /* Get signature status string (GMime 2.6) */
341 signature_status_to_string (GMimeSignatureStatus x)
344 case GMIME_SIGNATURE_STATUS_GOOD:
346 case GMIME_SIGNATURE_STATUS_BAD:
348 case GMIME_SIGNATURE_STATUS_ERROR:
354 /* Signature status sprinter (GMime 2.6) */
356 format_part_sigstatus_sprinter (sprinter_t *sp, mime_node_t *node)
358 /* Any changes to the JSON or S-Expression format should be
359 * reflected in the file devel/schemata. */
361 GMimeSignatureList *siglist = node->sig_list;
371 for (i = 0; i < g_mime_signature_list_length (siglist); i++) {
372 GMimeSignature *signature = g_mime_signature_list_get_signature (siglist, i);
377 GMimeSignatureStatus status = g_mime_signature_get_status (signature);
378 sp->map_key (sp, "status");
379 sp->string (sp, signature_status_to_string (status));
381 GMimeCertificate *certificate = g_mime_signature_get_certificate (signature);
382 if (status == GMIME_SIGNATURE_STATUS_GOOD) {
384 sp->map_key (sp, "fingerprint");
385 sp->string (sp, g_mime_certificate_get_fingerprint (certificate));
387 /* these dates are seconds since the epoch; should we
388 * provide a more human-readable format string? */
389 time_t created = g_mime_signature_get_created (signature);
391 sp->map_key (sp, "created");
392 sp->integer (sp, created);
394 time_t expires = g_mime_signature_get_expires (signature);
396 sp->map_key (sp, "expires");
397 sp->integer (sp, expires);
399 /* output user id only if validity is FULL or ULTIMATE. */
400 /* note that gmime is using the term "trust" here, which
401 * is WRONG. It's actually user id "validity". */
403 const char *name = g_mime_certificate_get_name (certificate);
404 GMimeCertificateTrust trust = g_mime_certificate_get_trust (certificate);
405 if (name && (trust == GMIME_CERTIFICATE_TRUST_FULLY || trust == GMIME_CERTIFICATE_TRUST_ULTIMATE)) {
406 sp->map_key (sp, "userid");
407 sp->string (sp, name);
410 } else if (certificate) {
411 const char *key_id = g_mime_certificate_get_key_id (certificate);
413 sp->map_key (sp, "keyid");
414 sp->string (sp, key_id);
418 GMimeSignatureError errors = g_mime_signature_get_errors (signature);
419 if (errors != GMIME_SIGNATURE_ERROR_NONE) {
420 sp->map_key (sp, "errors");
421 sp->integer (sp, errors);
430 #else /* GMIME_ATLEAST_26 */
432 /* Get signature status string (GMime 2.4) */
434 signer_status_to_string (GMimeSignerStatus x)
437 case GMIME_SIGNER_STATUS_NONE:
439 case GMIME_SIGNER_STATUS_GOOD:
441 case GMIME_SIGNER_STATUS_BAD:
443 case GMIME_SIGNER_STATUS_ERROR:
449 /* Signature status sprinter (GMime 2.4) */
451 format_part_sigstatus_sprinter (sprinter_t *sp, mime_node_t *node)
453 const GMimeSignatureValidity* validity = node->sig_validity;
462 const GMimeSigner *signer = g_mime_signature_validity_get_signers (validity);
467 sp->map_key (sp, "status");
468 sp->string (sp, signer_status_to_string (signer->status));
470 if (signer->status == GMIME_SIGNER_STATUS_GOOD)
472 if (signer->fingerprint) {
473 sp->map_key (sp, "fingerprint");
474 sp->string (sp, signer->fingerprint);
476 /* these dates are seconds since the epoch; should we
477 * provide a more human-readable format string? */
478 if (signer->created) {
479 sp->map_key (sp, "created");
480 sp->integer (sp, signer->created);
482 if (signer->expires) {
483 sp->map_key (sp, "expires");
484 sp->integer (sp, signer->expires);
486 /* output user id only if validity is FULL or ULTIMATE. */
487 /* note that gmime is using the term "trust" here, which
488 * is WRONG. It's actually user id "validity". */
489 if ((signer->name) && (signer->trust)) {
490 if ((signer->trust == GMIME_SIGNER_TRUST_FULLY) || (signer->trust == GMIME_SIGNER_TRUST_ULTIMATE)) {
491 sp->map_key (sp, "userid");
492 sp->string (sp, signer->name);
497 sp->map_key (sp, "keyid");
498 sp->string (sp, signer->keyid);
501 if (signer->errors != GMIME_SIGNER_ERROR_NONE) {
502 sp->map_key (sp, "errors");
503 sp->integer (sp, signer->errors);
507 signer = signer->next;
513 #endif /* GMIME_ATLEAST_26 */
515 static notmuch_status_t
516 format_part_text (const void *ctx, sprinter_t *sp, mime_node_t *node,
517 int indent, const notmuch_show_params_t *params)
519 /* The disposition and content-type metadata are associated with
520 * the envelope for message parts */
521 GMimeObject *meta = node->envelope_part ?
522 GMIME_OBJECT (node->envelope_part) : node->part;
523 GMimeContentType *content_type = g_mime_object_get_content_type (meta);
524 const notmuch_bool_t leaf = GMIME_IS_PART (node->part);
525 const char *part_type;
528 if (node->envelope_file) {
529 notmuch_message_t *message = node->envelope_file;
531 part_type = "message";
532 printf ("\f%s{ id:%s depth:%d match:%d excluded:%d filename:%s\n",
534 notmuch_message_get_message_id (message),
536 notmuch_message_get_flag (message, NOTMUCH_MESSAGE_FLAG_MATCH) ? 1 : 0,
537 notmuch_message_get_flag (message, NOTMUCH_MESSAGE_FLAG_EXCLUDED) ? 1 : 0,
538 notmuch_message_get_filename (message));
540 GMimeContentDisposition *disposition = g_mime_object_get_content_disposition (meta);
541 const char *cid = g_mime_object_get_content_id (meta);
542 const char *filename = leaf ?
543 g_mime_part_get_filename (GMIME_PART (node->part)) : NULL;
546 strcmp (disposition->disposition, GMIME_DISPOSITION_ATTACHMENT) == 0)
547 part_type = "attachment";
551 printf ("\f%s{ ID: %d", part_type, node->part_num);
553 printf (", Filename: %s", filename);
555 printf (", Content-id: %s", cid);
556 printf (", Content-type: %s\n", g_mime_content_type_to_string (content_type));
559 if (GMIME_IS_MESSAGE (node->part)) {
560 GMimeMessage *message = GMIME_MESSAGE (node->part);
561 InternetAddressList *recipients;
562 const char *recipients_string;
564 printf ("\fheader{\n");
565 if (node->envelope_file)
566 printf ("%s\n", _get_one_line_summary (ctx, node->envelope_file));
567 printf ("Subject: %s\n", g_mime_message_get_subject (message));
568 printf ("From: %s\n", g_mime_message_get_sender (message));
569 recipients = g_mime_message_get_recipients (message, GMIME_RECIPIENT_TYPE_TO);
570 recipients_string = internet_address_list_to_string (recipients, 0);
571 if (recipients_string)
572 printf ("To: %s\n", recipients_string);
573 recipients = g_mime_message_get_recipients (message, GMIME_RECIPIENT_TYPE_CC);
574 recipients_string = internet_address_list_to_string (recipients, 0);
575 if (recipients_string)
576 printf ("Cc: %s\n", recipients_string);
577 printf ("Date: %s\n", g_mime_message_get_date_as_string (message));
578 printf ("\fheader}\n");
580 printf ("\fbody{\n");
584 if (g_mime_content_type_is_type (content_type, "text", "*") &&
585 !g_mime_content_type_is_type (content_type, "text", "html"))
587 GMimeStream *stream_stdout = g_mime_stream_file_new (stdout);
588 g_mime_stream_file_set_owner (GMIME_STREAM_FILE (stream_stdout), FALSE);
589 show_text_part_content (node->part, stream_stdout, 0);
590 g_object_unref(stream_stdout);
592 printf ("Non-text part: %s\n",
593 g_mime_content_type_to_string (content_type));
597 for (i = 0; i < node->nchildren; i++)
598 format_part_text (ctx, sp, mime_node_child (node, i), indent, params);
600 if (GMIME_IS_MESSAGE (node->part))
601 printf ("\fbody}\n");
603 printf ("\f%s}\n", part_type);
605 return NOTMUCH_STATUS_SUCCESS;
609 format_omitted_part_meta_sprinter (sprinter_t *sp, GMimeObject *meta, GMimePart *part)
611 const char *content_charset = g_mime_object_get_content_type_parameter (meta, "charset");
612 const char *cte = g_mime_object_get_header (meta, "content-transfer-encoding");
613 GMimeDataWrapper *wrapper = g_mime_part_get_content_object (part);
614 GMimeStream *stream = g_mime_data_wrapper_get_stream (wrapper);
615 ssize_t content_length = g_mime_stream_length (stream);
617 if (content_charset != NULL) {
618 sp->map_key (sp, "content-charset");
619 sp->string (sp, content_charset);
622 sp->map_key (sp, "content-transfer-encoding");
623 sp->string (sp, cte);
625 if (content_length >= 0) {
626 sp->map_key (sp, "content-length");
627 sp->integer (sp, content_length);
632 format_part_sprinter (const void *ctx, sprinter_t *sp, mime_node_t *node,
633 notmuch_bool_t first, notmuch_bool_t output_body)
635 /* Any changes to the JSON or S-Expression format should be
636 * reflected in the file devel/schemata. */
638 if (node->envelope_file) {
640 format_message_sprinter (sp, node->envelope_file);
642 sp->map_key (sp, "headers");
643 format_headers_sprinter (sp, GMIME_MESSAGE (node->part), FALSE);
646 sp->map_key (sp, "body");
648 format_part_sprinter (ctx, sp, mime_node_child (node, 0), first, TRUE);
655 /* The disposition and content-type metadata are associated with
656 * the envelope for message parts */
657 GMimeObject *meta = node->envelope_part ?
658 GMIME_OBJECT (node->envelope_part) : node->part;
659 GMimeContentType *content_type = g_mime_object_get_content_type (meta);
660 const char *cid = g_mime_object_get_content_id (meta);
661 const char *filename = GMIME_IS_PART (node->part) ?
662 g_mime_part_get_filename (GMIME_PART (node->part)) : NULL;
668 sp->map_key (sp, "id");
669 sp->integer (sp, node->part_num);
671 if (node->decrypt_attempted) {
672 sp->map_key (sp, "encstatus");
675 sp->map_key (sp, "status");
676 sp->string (sp, node->decrypt_success ? "good" : "bad");
681 if (node->verify_attempted) {
682 sp->map_key (sp, "sigstatus");
683 format_part_sigstatus_sprinter (sp, node);
686 sp->map_key (sp, "content-type");
687 sp->string (sp, g_mime_content_type_to_string (content_type));
690 sp->map_key (sp, "content-id");
691 sp->string (sp, cid);
695 sp->map_key (sp, "filename");
696 sp->string (sp, filename);
699 if (GMIME_IS_PART (node->part)) {
700 /* For non-HTML text parts, we include the content in the
701 * JSON. Since JSON must be Unicode, we handle charset
702 * decoding here and do not report a charset to the caller.
703 * For text/html parts, we do not include the content. If a
704 * caller is interested in text/html parts, it should retrieve
705 * them separately and they will not be decoded. Since this
706 * makes charset decoding the responsibility on the caller, we
707 * report the charset for text/html parts.
709 if (g_mime_content_type_is_type (content_type, "text", "*") &&
710 ! g_mime_content_type_is_type (content_type, "text", "html"))
712 GMimeStream *stream_memory = g_mime_stream_mem_new ();
713 GByteArray *part_content;
714 show_text_part_content (node->part, stream_memory, 0);
715 part_content = g_mime_stream_mem_get_byte_array (GMIME_STREAM_MEM (stream_memory));
716 sp->map_key (sp, "content");
717 sp->string_len (sp, (char *) part_content->data, part_content->len);
718 g_object_unref (stream_memory);
720 format_omitted_part_meta_sprinter (sp, meta, GMIME_PART (node->part));
722 } else if (GMIME_IS_MULTIPART (node->part)) {
723 sp->map_key (sp, "content");
726 } else if (GMIME_IS_MESSAGE (node->part)) {
727 sp->map_key (sp, "content");
731 sp->map_key (sp, "headers");
732 format_headers_sprinter (sp, GMIME_MESSAGE (node->part), FALSE);
734 sp->map_key (sp, "body");
739 for (i = 0; i < node->nchildren; i++)
740 format_part_sprinter (ctx, sp, mime_node_child (node, i), i == 0, TRUE);
742 /* Close content structures */
743 for (i = 0; i < nclose; i++)
749 static notmuch_status_t
750 format_part_sprinter_entry (const void *ctx, sprinter_t *sp,
751 mime_node_t *node, unused (int indent),
752 const notmuch_show_params_t *params)
754 format_part_sprinter (ctx, sp, node, TRUE, params->output_body);
756 return NOTMUCH_STATUS_SUCCESS;
759 /* Print a message in "mboxrd" format as documented, for example,
762 * http://qmail.org/qmail-manual-html/man5/mbox.html
764 static notmuch_status_t
765 format_part_mbox (const void *ctx, unused (sprinter_t *sp), mime_node_t *node,
767 unused (const notmuch_show_params_t *params))
769 notmuch_message_t *message = node->envelope_file;
771 const char *filename;
776 struct tm date_gmtime;
777 char date_asctime[26];
784 INTERNAL_ERROR ("format_part_mbox requires a root part");
786 filename = notmuch_message_get_filename (message);
787 file = fopen (filename, "r");
789 fprintf (stderr, "Failed to open %s: %s\n",
790 filename, strerror (errno));
791 return NOTMUCH_STATUS_FILE_ERROR;
794 from = notmuch_message_get_header (message, "from");
795 from = _extract_email_address (ctx, from);
797 date = notmuch_message_get_date (message);
798 gmtime_r (&date, &date_gmtime);
799 asctime_r (&date_gmtime, date_asctime);
801 printf ("From %s %s", from, date_asctime);
803 while ((line_len = getline (&line, &line_size, file)) != -1 ) {
804 if (_is_from_line (line))
813 return NOTMUCH_STATUS_SUCCESS;
816 static notmuch_status_t
817 format_part_raw (unused (const void *ctx), unused (sprinter_t *sp),
818 mime_node_t *node, unused (int indent),
819 unused (const notmuch_show_params_t *params))
821 if (node->envelope_file) {
822 /* Special case the entire message to avoid MIME parsing. */
823 const char *filename;
828 filename = notmuch_message_get_filename (node->envelope_file);
829 if (filename == NULL) {
830 fprintf (stderr, "Error: Cannot get message filename.\n");
831 return NOTMUCH_STATUS_FILE_ERROR;
834 file = fopen (filename, "r");
836 fprintf (stderr, "Error: Cannot open file %s: %s\n", filename, strerror (errno));
837 return NOTMUCH_STATUS_FILE_ERROR;
840 while (!feof (file)) {
841 size = fread (buf, 1, sizeof (buf), file);
843 fprintf (stderr, "Error: Read failed from %s\n", filename);
845 return NOTMUCH_STATUS_FILE_ERROR;
848 if (fwrite (buf, size, 1, stdout) != 1) {
849 fprintf (stderr, "Error: Write failed\n");
851 return NOTMUCH_STATUS_FILE_ERROR;
856 return NOTMUCH_STATUS_SUCCESS;
859 GMimeStream *stream_stdout;
860 GMimeStream *stream_filter = NULL;
862 stream_stdout = g_mime_stream_file_new (stdout);
863 g_mime_stream_file_set_owner (GMIME_STREAM_FILE (stream_stdout), FALSE);
865 stream_filter = g_mime_stream_filter_new (stream_stdout);
867 if (GMIME_IS_PART (node->part)) {
868 /* For leaf parts, we emit only the transfer-decoded
870 GMimeDataWrapper *wrapper;
871 wrapper = g_mime_part_get_content_object (GMIME_PART (node->part));
873 if (wrapper && stream_filter)
874 g_mime_data_wrapper_write_to_stream (wrapper, stream_filter);
876 /* Write out the whole part. For message parts (the root
877 * part and embedded message parts), this will be the
878 * message including its headers (but not the
879 * encapsulating part's headers). For multipart parts,
880 * this will include the headers. */
882 g_mime_object_write_to_stream (node->part, stream_filter);
886 g_object_unref (stream_filter);
889 g_object_unref(stream_stdout);
891 return NOTMUCH_STATUS_SUCCESS;
894 static notmuch_status_t
895 show_message (void *ctx,
896 const notmuch_show_format_t *format,
898 notmuch_message_t *message,
900 notmuch_show_params_t *params)
902 void *local = talloc_new (ctx);
903 mime_node_t *root, *part;
904 notmuch_status_t status;
906 status = mime_node_open (local, message, &(params->crypto), &root);
909 part = mime_node_seek_dfs (root, (params->part < 0 ? 0 : params->part));
911 status = format->part (local, sp, part, indent, params);
917 static notmuch_status_t
918 show_messages (void *ctx,
919 const notmuch_show_format_t *format,
921 notmuch_messages_t *messages,
923 notmuch_show_params_t *params)
925 notmuch_message_t *message;
926 notmuch_bool_t match;
927 notmuch_bool_t excluded;
929 notmuch_status_t status, res = NOTMUCH_STATUS_SUCCESS;
934 notmuch_messages_valid (messages);
935 notmuch_messages_move_to_next (messages))
939 message = notmuch_messages_get (messages);
941 match = notmuch_message_get_flag (message, NOTMUCH_MESSAGE_FLAG_MATCH);
942 excluded = notmuch_message_get_flag (message, NOTMUCH_MESSAGE_FLAG_EXCLUDED);
944 next_indent = indent;
946 if ((match && (!excluded || !params->omit_excluded)) || params->entire_thread) {
947 status = show_message (ctx, format, sp, message, indent, params);
950 next_indent = indent + 1;
955 status = show_messages (ctx,
957 notmuch_message_get_replies (message),
963 notmuch_message_destroy (message);
973 /* Formatted output of single message */
975 do_show_single (void *ctx,
976 notmuch_query_t *query,
977 const notmuch_show_format_t *format,
979 notmuch_show_params_t *params)
981 notmuch_messages_t *messages;
982 notmuch_message_t *message;
984 if (notmuch_query_count_messages (query) != 1) {
985 fprintf (stderr, "Error: search term did not match precisely one message.\n");
989 messages = notmuch_query_search_messages (query);
990 message = notmuch_messages_get (messages);
992 if (message == NULL) {
993 fprintf (stderr, "Error: Cannot find matching message.\n");
997 notmuch_message_set_flag (message, NOTMUCH_MESSAGE_FLAG_MATCH, 1);
999 return show_message (ctx, format, sp, message, 0, params)
1000 != NOTMUCH_STATUS_SUCCESS;
1003 /* Formatted output of threads */
1006 notmuch_query_t *query,
1007 const notmuch_show_format_t *format,
1009 notmuch_show_params_t *params)
1011 notmuch_threads_t *threads;
1012 notmuch_thread_t *thread;
1013 notmuch_messages_t *messages;
1014 notmuch_status_t status, res = NOTMUCH_STATUS_SUCCESS;
1016 sp->begin_list (sp);
1018 for (threads = notmuch_query_search_threads (query);
1019 notmuch_threads_valid (threads);
1020 notmuch_threads_move_to_next (threads))
1022 thread = notmuch_threads_get (threads);
1024 messages = notmuch_thread_get_toplevel_messages (thread);
1026 if (messages == NULL)
1027 INTERNAL_ERROR ("Thread %s has no toplevel messages.\n",
1028 notmuch_thread_get_thread_id (thread));
1030 status = show_messages (ctx, format, sp, messages, 0, params);
1034 notmuch_thread_destroy (thread);
1040 return res != NOTMUCH_STATUS_SUCCESS;
1044 NOTMUCH_FORMAT_NOT_SPECIFIED,
1045 NOTMUCH_FORMAT_JSON,
1046 NOTMUCH_FORMAT_SEXP,
1047 NOTMUCH_FORMAT_TEXT,
1048 NOTMUCH_FORMAT_MBOX,
1053 ENTIRE_THREAD_DEFAULT,
1055 ENTIRE_THREAD_FALSE,
1058 /* The following is to allow future options to be added more easily */
1065 notmuch_show_command (notmuch_config_t *config, int argc, char *argv[])
1067 notmuch_database_t *notmuch;
1068 notmuch_query_t *query;
1071 const notmuch_show_format_t *format = &format_text;
1072 sprinter_t *sprinter;
1073 notmuch_show_params_t params = {
1075 .omit_excluded = TRUE,
1076 .output_body = TRUE,
1082 int format_sel = NOTMUCH_FORMAT_NOT_SPECIFIED;
1083 int exclude = EXCLUDE_TRUE;
1084 int entire_thread = ENTIRE_THREAD_DEFAULT;
1086 notmuch_opt_desc_t options[] = {
1087 { NOTMUCH_OPT_KEYWORD, &format_sel, "format", 'f',
1088 (notmuch_keyword_t []){ { "json", NOTMUCH_FORMAT_JSON },
1089 { "text", NOTMUCH_FORMAT_TEXT },
1090 { "sexp", NOTMUCH_FORMAT_SEXP },
1091 { "mbox", NOTMUCH_FORMAT_MBOX },
1092 { "raw", NOTMUCH_FORMAT_RAW },
1094 { NOTMUCH_OPT_INT, ¬much_format_version, "format-version", 0, 0 },
1095 { NOTMUCH_OPT_KEYWORD, &exclude, "exclude", 'x',
1096 (notmuch_keyword_t []){ { "true", EXCLUDE_TRUE },
1097 { "false", EXCLUDE_FALSE },
1099 { NOTMUCH_OPT_KEYWORD, &entire_thread, "entire-thread", 't',
1100 (notmuch_keyword_t []){ { "true", ENTIRE_THREAD_TRUE },
1101 { "false", ENTIRE_THREAD_FALSE },
1102 { "", ENTIRE_THREAD_TRUE },
1104 { NOTMUCH_OPT_INT, ¶ms.part, "part", 'p', 0 },
1105 { NOTMUCH_OPT_BOOLEAN, ¶ms.crypto.decrypt, "decrypt", 'd', 0 },
1106 { NOTMUCH_OPT_BOOLEAN, ¶ms.crypto.verify, "verify", 'v', 0 },
1107 { NOTMUCH_OPT_BOOLEAN, ¶ms.output_body, "body", 'b', 0 },
1111 opt_index = parse_arguments (argc, argv, options, 1);
1112 if (opt_index < 0) {
1113 /* diagnostics already printed */
1117 /* decryption implies verification */
1118 if (params.crypto.decrypt)
1119 params.crypto.verify = TRUE;
1121 if (format_sel == NOTMUCH_FORMAT_NOT_SPECIFIED) {
1122 /* if part was requested and format was not specified, use format=raw */
1123 if (params.part >= 0)
1124 format_sel = NOTMUCH_FORMAT_RAW;
1126 format_sel = NOTMUCH_FORMAT_TEXT;
1129 switch (format_sel) {
1130 case NOTMUCH_FORMAT_JSON:
1131 format = &format_json;
1133 case NOTMUCH_FORMAT_TEXT:
1134 format = &format_text;
1136 case NOTMUCH_FORMAT_SEXP:
1137 format = &format_sexp;
1139 case NOTMUCH_FORMAT_MBOX:
1140 if (params.part > 0) {
1141 fprintf (stderr, "Error: specifying parts is incompatible with mbox output format.\n");
1145 format = &format_mbox;
1147 case NOTMUCH_FORMAT_RAW:
1148 format = &format_raw;
1149 /* If --format=raw specified without specifying part, we can only
1150 * output single message, so set part=0 */
1151 if (params.part < 0)
1157 notmuch_exit_if_unsupported_format ();
1159 /* Default is entire-thread = FALSE except for format=json and
1161 if (entire_thread == ENTIRE_THREAD_DEFAULT) {
1162 if (format == &format_json || format == &format_sexp)
1163 entire_thread = ENTIRE_THREAD_TRUE;
1165 entire_thread = ENTIRE_THREAD_FALSE;
1168 if (!params.output_body) {
1169 if (params.part > 0) {
1170 fprintf (stderr, "Warning: --body=false is incompatible with --part > 0. Disabling.\n");
1171 params.output_body = TRUE;
1173 if (format != &format_json && format != &format_sexp)
1175 "Warning: --body=false only implemented for format=json and format=sexp\n");
1179 if (entire_thread == ENTIRE_THREAD_TRUE)
1180 params.entire_thread = TRUE;
1182 params.entire_thread = FALSE;
1184 query_string = query_string_from_args (config, argc-opt_index, argv+opt_index);
1185 if (query_string == NULL) {
1186 fprintf (stderr, "Out of memory\n");
1190 if (*query_string == '\0') {
1191 fprintf (stderr, "Error: notmuch show requires at least one search term.\n");
1195 if (notmuch_database_open (notmuch_config_get_database_path (config),
1196 NOTMUCH_DATABASE_MODE_READ_ONLY, ¬much))
1199 query = notmuch_query_create (notmuch, query_string);
1200 if (query == NULL) {
1201 fprintf (stderr, "Out of memory\n");
1205 /* Create structure printer. */
1206 sprinter = format->new_sprinter(config, stdout);
1208 /* If a single message is requested we do not use search_excludes. */
1209 if (params.part >= 0)
1210 ret = do_show_single (config, query, format, sprinter, ¶ms);
1212 /* We always apply set the exclude flag. The
1213 * exclude=true|false option controls whether or not we return
1214 * threads that only match in an excluded message */
1215 const char **search_exclude_tags;
1216 size_t search_exclude_tags_length;
1219 search_exclude_tags = notmuch_config_get_search_exclude_tags
1220 (config, &search_exclude_tags_length);
1221 for (i = 0; i < search_exclude_tags_length; i++)
1222 notmuch_query_add_tag_exclude (query, search_exclude_tags[i]);
1224 if (exclude == EXCLUDE_FALSE) {
1225 notmuch_query_set_omit_excluded (query, FALSE);
1226 params.omit_excluded = FALSE;
1229 ret = do_show (config, query, format, sprinter, ¶ms);
1232 notmuch_crypto_cleanup (¶ms.crypto);
1233 notmuch_query_destroy (query);
1234 notmuch_database_destroy (notmuch);