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"
24 format_headers_message_part_text (GMimeMessage *message);
27 format_part_text (const void *ctx, mime_node_t *node,
28 int indent, const notmuch_show_params_t *params);
30 static const notmuch_show_format_t format_text = {
31 .message_set_start = "",
32 .part = format_part_text,
33 .message_set_sep = "",
38 format_part_json_entry (const void *ctx, mime_node_t *node,
39 int indent, const notmuch_show_params_t *params);
41 static const notmuch_show_format_t format_json = {
42 .message_set_start = "[",
43 .part = format_part_json_entry,
44 .message_set_sep = ", ",
45 .message_set_end = "]"
49 format_message_mbox (const void *ctx,
50 notmuch_message_t *message,
53 static const notmuch_show_format_t format_mbox = {
55 "", format_message_mbox,
70 format_part_content_raw (GMimeObject *part);
72 static const notmuch_show_format_t format_raw = {
75 "", NULL, format_headers_message_part_text, "\n",
80 format_part_content_raw,
89 _get_tags_as_string (const void *ctx, notmuch_message_t *message)
96 result = talloc_strdup (ctx, "");
100 for (tags = notmuch_message_get_tags (message);
101 notmuch_tags_valid (tags);
102 notmuch_tags_move_to_next (tags))
104 tag = notmuch_tags_get (tags);
106 result = talloc_asprintf_append (result, "%s%s",
107 first ? "" : " ", tag);
114 /* Get a nice, single-line summary of message. */
116 _get_one_line_summary (const void *ctx, notmuch_message_t *message)
120 const char *relative_date;
123 from = notmuch_message_get_header (message, "from");
125 date = notmuch_message_get_date (message);
126 relative_date = notmuch_time_relative_date (ctx, date);
128 tags = _get_tags_as_string (ctx, message);
130 return talloc_asprintf (ctx, "%s (%s) (%s)",
131 from, relative_date, tags);
135 format_message_json (const void *ctx, notmuch_message_t *message)
137 notmuch_tags_t *tags;
139 void *ctx_quote = talloc_new (ctx);
141 const char *relative_date;
143 date = notmuch_message_get_date (message);
144 relative_date = notmuch_time_relative_date (ctx, date);
146 printf ("\"id\": %s, \"match\": %s, \"filename\": %s, \"timestamp\": %ld, \"date_relative\": \"%s\", \"tags\": [",
147 json_quote_str (ctx_quote, notmuch_message_get_message_id (message)),
148 notmuch_message_get_flag (message, NOTMUCH_MESSAGE_FLAG_MATCH) ? "true" : "false",
149 json_quote_str (ctx_quote, notmuch_message_get_filename (message)),
150 date, relative_date);
152 for (tags = notmuch_message_get_tags (message);
153 notmuch_tags_valid (tags);
154 notmuch_tags_move_to_next (tags))
156 printf("%s%s", first ? "" : ",",
157 json_quote_str (ctx_quote, notmuch_tags_get (tags)));
161 talloc_free (ctx_quote);
164 /* Extract just the email address from the contents of a From:
167 _extract_email_address (const void *ctx, const char *from)
169 InternetAddressList *addresses;
170 InternetAddress *address;
171 InternetAddressMailbox *mailbox;
172 const char *email = "MAILER-DAEMON";
174 addresses = internet_address_list_parse_string (from);
176 /* Bail if there is no address here. */
177 if (addresses == NULL || internet_address_list_length (addresses) < 1)
180 /* Otherwise, just use the first address. */
181 address = internet_address_list_get_address (addresses, 0);
183 /* The From header should never contain an address group rather
184 * than a mailbox. So bail if it does. */
185 if (! INTERNET_ADDRESS_IS_MAILBOX (address))
188 mailbox = INTERNET_ADDRESS_MAILBOX (address);
189 email = internet_address_mailbox_get_addr (mailbox);
190 email = talloc_strdup (ctx, email);
194 g_object_unref (addresses);
199 /* Return 1 if 'line' is an mbox From_ line---that is, a line
200 * beginning with zero or more '>' characters followed by the
201 * characters 'F', 'r', 'o', 'm', and space.
203 * Any characters at all may appear after that in the line.
206 _is_from_line (const char *line)
208 const char *s = line;
216 if (STRNCMP_LITERAL (s, "From ") == 0)
222 /* Print a message in "mboxrd" format as documented, for example,
225 * http://qmail.org/qmail-manual-html/man5/mbox.html
228 format_message_mbox (const void *ctx,
229 notmuch_message_t *message,
232 const char *filename;
237 struct tm date_gmtime;
238 char date_asctime[26];
244 filename = notmuch_message_get_filename (message);
245 file = fopen (filename, "r");
247 fprintf (stderr, "Failed to open %s: %s\n",
248 filename, strerror (errno));
252 from = notmuch_message_get_header (message, "from");
253 from = _extract_email_address (ctx, from);
255 date = notmuch_message_get_date (message);
256 gmtime_r (&date, &date_gmtime);
257 asctime_r (&date_gmtime, date_asctime);
259 printf ("From %s %s", from, date_asctime);
261 while ((line_len = getline (&line, &line_size, file)) != -1 ) {
262 if (_is_from_line (line))
273 format_headers_message_part_text (GMimeMessage *message)
275 InternetAddressList *recipients;
276 const char *recipients_string;
278 printf ("Subject: %s\n", g_mime_message_get_subject (message));
279 printf ("From: %s\n", g_mime_message_get_sender (message));
280 recipients = g_mime_message_get_recipients (message, GMIME_RECIPIENT_TYPE_TO);
281 recipients_string = internet_address_list_to_string (recipients, 0);
282 if (recipients_string)
285 recipients = g_mime_message_get_recipients (message, GMIME_RECIPIENT_TYPE_CC);
286 recipients_string = internet_address_list_to_string (recipients, 0);
287 if (recipients_string)
290 printf ("Date: %s\n", g_mime_message_get_date_as_string (message));
294 format_headers_json (const void *ctx, notmuch_message_t *message)
296 const char *headers[] = {
297 "Subject", "From", "To", "Cc", "Bcc", "Date"
299 const char *name, *value;
301 int first_header = 1;
302 void *ctx_quote = talloc_new (ctx);
304 for (i = 0; i < ARRAY_SIZE (headers); i++) {
306 value = notmuch_message_get_header (message, name);
310 fputs (", ", stdout);
314 json_quote_str (ctx_quote, name),
315 json_quote_str (ctx_quote, value));
319 talloc_free (ctx_quote);
323 format_headers_message_part_json (GMimeMessage *message)
325 void *ctx = talloc_new (NULL);
326 void *ctx_quote = talloc_new (ctx);
327 InternetAddressList *recipients;
328 const char *recipients_string;
331 json_quote_str (ctx_quote, "Subject"),
332 json_quote_str (ctx_quote, g_mime_message_get_subject (message)));
334 json_quote_str (ctx_quote, "From"),
335 json_quote_str (ctx_quote, g_mime_message_get_sender (message)));
336 recipients = g_mime_message_get_recipients (message, GMIME_RECIPIENT_TYPE_TO);
337 recipients_string = internet_address_list_to_string (recipients, 0);
338 if (recipients_string)
340 json_quote_str (ctx_quote, "To"),
341 json_quote_str (ctx_quote, recipients_string));
342 recipients = g_mime_message_get_recipients (message, GMIME_RECIPIENT_TYPE_CC);
343 recipients_string = internet_address_list_to_string (recipients, 0);
344 if (recipients_string)
346 json_quote_str (ctx_quote, "Cc"),
347 json_quote_str (ctx_quote, recipients_string));
349 json_quote_str (ctx_quote, "Date"),
350 json_quote_str (ctx_quote, g_mime_message_get_date_as_string (message)));
352 talloc_free (ctx_quote);
356 /* Write a MIME text part out to the given stream.
358 * Both line-ending conversion (CRLF->LF) and charset conversion ( ->
359 * UTF-8) will be performed, so it is inappropriate to call this
360 * function with a non-text part. Doing so will trigger an internal
364 show_text_part_content (GMimeObject *part, GMimeStream *stream_out)
366 GMimeContentType *content_type = g_mime_object_get_content_type (GMIME_OBJECT (part));
367 GMimeStream *stream_filter = NULL;
368 GMimeDataWrapper *wrapper;
371 if (! g_mime_content_type_is_type (content_type, "text", "*"))
372 INTERNAL_ERROR ("Illegal request to format non-text part (%s) as text.",
373 g_mime_content_type_to_string (content_type));
375 if (stream_out == NULL)
378 stream_filter = g_mime_stream_filter_new (stream_out);
379 g_mime_stream_filter_add(GMIME_STREAM_FILTER (stream_filter),
380 g_mime_filter_crlf_new (FALSE, FALSE));
382 charset = g_mime_object_get_content_type_parameter (part, "charset");
384 GMimeFilter *charset_filter;
385 charset_filter = g_mime_filter_charset_new (charset, "UTF-8");
386 /* This result can be NULL for things like "unknown-8bit".
387 * Don't set a NULL filter as that makes GMime print
388 * annoying assertion-failure messages on stderr. */
389 if (charset_filter) {
390 g_mime_stream_filter_add (GMIME_STREAM_FILTER (stream_filter),
392 g_object_unref (charset_filter);
397 wrapper = g_mime_part_get_content_object (GMIME_PART (part));
398 if (wrapper && stream_filter)
399 g_mime_data_wrapper_write_to_stream (wrapper, stream_filter);
401 g_object_unref(stream_filter);
404 #ifdef GMIME_ATLEAST_26
406 signature_status_to_string (GMimeSignatureStatus x)
409 case GMIME_SIGNATURE_STATUS_GOOD:
411 case GMIME_SIGNATURE_STATUS_BAD:
413 case GMIME_SIGNATURE_STATUS_ERROR:
420 signer_status_to_string (GMimeSignerStatus x)
423 case GMIME_SIGNER_STATUS_NONE:
425 case GMIME_SIGNER_STATUS_GOOD:
427 case GMIME_SIGNER_STATUS_BAD:
429 case GMIME_SIGNER_STATUS_ERROR:
436 #ifdef GMIME_ATLEAST_26
438 format_part_sigstatus_json (GMimeSignatureList *siglist)
440 printf (", \"sigstatus\": [");
447 void *ctx_quote = talloc_new (NULL);
449 for (i = 0; i < g_mime_signature_list_length (siglist); i++) {
450 GMimeSignature *signature = g_mime_signature_list_get_signature (siglist, i);
458 GMimeSignatureStatus status = g_mime_signature_get_status (signature);
459 printf ("\"status\": %s",
460 json_quote_str (ctx_quote,
461 signature_status_to_string (status)));
463 GMimeCertificate *certificate = g_mime_signature_get_certificate (signature);
464 if (status == GMIME_SIGNATURE_STATUS_GOOD) {
466 printf (", \"fingerprint\": %s", json_quote_str (ctx_quote, g_mime_certificate_get_fingerprint (certificate)));
467 /* these dates are seconds since the epoch; should we
468 * provide a more human-readable format string? */
469 time_t created = g_mime_signature_get_created (signature);
471 printf (", \"created\": %d", (int) created);
472 time_t expires = g_mime_signature_get_expires (signature);
474 printf (", \"expires\": %d", (int) expires);
475 /* output user id only if validity is FULL or ULTIMATE. */
476 /* note that gmime is using the term "trust" here, which
477 * is WRONG. It's actually user id "validity". */
479 const char *name = g_mime_certificate_get_name (certificate);
480 GMimeCertificateTrust trust = g_mime_certificate_get_trust (certificate);
481 if (name && (trust == GMIME_CERTIFICATE_TRUST_FULLY || trust == GMIME_CERTIFICATE_TRUST_ULTIMATE))
482 printf (", \"userid\": %s", json_quote_str (ctx_quote, name));
484 } else if (certificate) {
485 const char *key_id = g_mime_certificate_get_key_id (certificate);
487 printf (", \"keyid\": %s", json_quote_str (ctx_quote, key_id));
490 GMimeSignatureError errors = g_mime_signature_get_errors (signature);
491 if (errors != GMIME_SIGNATURE_ERROR_NONE) {
492 printf (", \"errors\": %d", errors);
500 talloc_free (ctx_quote);
504 format_part_sigstatus_json (const GMimeSignatureValidity* validity)
506 printf (", \"sigstatus\": [");
513 const GMimeSigner *signer = g_mime_signature_validity_get_signers (validity);
515 void *ctx_quote = talloc_new (NULL);
526 printf ("\"status\": %s",
527 json_quote_str (ctx_quote,
528 signer_status_to_string (signer->status)));
530 if (signer->status == GMIME_SIGNER_STATUS_GOOD)
532 if (signer->fingerprint)
533 printf (", \"fingerprint\": %s", json_quote_str (ctx_quote, signer->fingerprint));
534 /* these dates are seconds since the epoch; should we
535 * provide a more human-readable format string? */
537 printf (", \"created\": %d", (int) signer->created);
539 printf (", \"expires\": %d", (int) signer->expires);
540 /* output user id only if validity is FULL or ULTIMATE. */
541 /* note that gmime is using the term "trust" here, which
542 * is WRONG. It's actually user id "validity". */
543 if ((signer->name) && (signer->trust)) {
544 if ((signer->trust == GMIME_SIGNER_TRUST_FULLY) || (signer->trust == GMIME_SIGNER_TRUST_ULTIMATE))
545 printf (", \"userid\": %s", json_quote_str (ctx_quote, signer->name));
549 printf (", \"keyid\": %s", json_quote_str (ctx_quote, signer->keyid));
551 if (signer->errors != GMIME_SIGNER_ERROR_NONE) {
552 printf (", \"errors\": %d", signer->errors);
556 signer = signer->next;
561 talloc_free (ctx_quote);
566 format_part_content_raw (GMimeObject *part)
568 if (! GMIME_IS_PART (part))
571 GMimeStream *stream_stdout;
572 GMimeStream *stream_filter = NULL;
573 GMimeDataWrapper *wrapper;
575 stream_stdout = g_mime_stream_file_new (stdout);
576 g_mime_stream_file_set_owner (GMIME_STREAM_FILE (stream_stdout), FALSE);
578 stream_filter = g_mime_stream_filter_new (stream_stdout);
580 wrapper = g_mime_part_get_content_object (GMIME_PART (part));
582 if (wrapper && stream_filter)
583 g_mime_data_wrapper_write_to_stream (wrapper, stream_filter);
586 g_object_unref (stream_filter);
589 g_object_unref(stream_stdout);
593 format_part_text (const void *ctx, mime_node_t *node,
594 int indent, const notmuch_show_params_t *params)
596 /* The disposition and content-type metadata are associated with
597 * the envelope for message parts */
598 GMimeObject *meta = node->envelope_part ?
599 GMIME_OBJECT (node->envelope_part) : node->part;
600 GMimeContentType *content_type = g_mime_object_get_content_type (meta);
601 const notmuch_bool_t leaf = GMIME_IS_PART (node->part);
602 const char *part_type;
605 if (node->envelope_file) {
606 notmuch_message_t *message = node->envelope_file;
608 part_type = "message";
609 printf ("\f%s{ id:%s depth:%d match:%d filename:%s\n",
611 notmuch_message_get_message_id (message),
613 notmuch_message_get_flag (message, NOTMUCH_MESSAGE_FLAG_MATCH),
614 notmuch_message_get_filename (message));
616 GMimeContentDisposition *disposition = g_mime_object_get_content_disposition (meta);
617 const char *cid = g_mime_object_get_content_id (meta);
618 const char *filename = leaf ?
619 g_mime_part_get_filename (GMIME_PART (node->part)) : NULL;
622 strcmp (disposition->disposition, GMIME_DISPOSITION_ATTACHMENT) == 0)
623 part_type = "attachment";
627 printf ("\f%s{ ID: %d", part_type, node->part_num);
629 printf (", Filename: %s", filename);
631 printf (", Content-id: %s", cid);
632 printf (", Content-type: %s\n", g_mime_content_type_to_string (content_type));
635 if (GMIME_IS_MESSAGE (node->part)) {
636 GMimeMessage *message = GMIME_MESSAGE (node->part);
637 InternetAddressList *recipients;
638 const char *recipients_string;
640 printf ("\fheader{\n");
641 if (node->envelope_file)
642 printf ("%s\n", _get_one_line_summary (ctx, node->envelope_file));
643 printf ("Subject: %s\n", g_mime_message_get_subject (message));
644 printf ("From: %s\n", g_mime_message_get_sender (message));
645 recipients = g_mime_message_get_recipients (message, GMIME_RECIPIENT_TYPE_TO);
646 recipients_string = internet_address_list_to_string (recipients, 0);
647 if (recipients_string)
648 printf ("To: %s\n", recipients_string);
649 recipients = g_mime_message_get_recipients (message, GMIME_RECIPIENT_TYPE_CC);
650 recipients_string = internet_address_list_to_string (recipients, 0);
651 if (recipients_string)
652 printf ("Cc: %s\n", recipients_string);
653 printf ("Date: %s\n", g_mime_message_get_date_as_string (message));
654 printf ("\fheader}\n");
656 printf ("\fbody{\n");
660 if (g_mime_content_type_is_type (content_type, "text", "*") &&
661 !g_mime_content_type_is_type (content_type, "text", "html"))
663 GMimeStream *stream_stdout = g_mime_stream_file_new (stdout);
664 g_mime_stream_file_set_owner (GMIME_STREAM_FILE (stream_stdout), FALSE);
665 show_text_part_content (node->part, stream_stdout);
666 g_object_unref(stream_stdout);
668 printf ("Non-text part: %s\n",
669 g_mime_content_type_to_string (content_type));
673 for (i = 0; i < node->nchildren; i++)
674 format_part_text (ctx, mime_node_child (node, i), indent, params);
676 if (GMIME_IS_MESSAGE (node->part))
677 printf ("\fbody}\n");
679 printf ("\f%s}\n", part_type);
683 format_part_json (const void *ctx, mime_node_t *node, notmuch_bool_t first)
685 /* Any changes to the JSON format should be reflected in the file
688 if (node->envelope_file) {
690 format_message_json (ctx, node->envelope_file);
692 printf ("\"headers\": {");
693 format_headers_json (ctx, node->envelope_file);
696 printf (", \"body\": [");
697 format_part_json (ctx, mime_node_child (node, 0), first);
703 void *local = talloc_new (ctx);
704 /* The disposition and content-type metadata are associated with
705 * the envelope for message parts */
706 GMimeObject *meta = node->envelope_part ?
707 GMIME_OBJECT (node->envelope_part) : node->part;
708 GMimeContentType *content_type = g_mime_object_get_content_type (meta);
709 GMimeStream *stream_memory = g_mime_stream_mem_new ();
710 const char *cid = g_mime_object_get_content_id (meta);
711 GByteArray *part_content;
717 printf ("{\"id\": %d", node->part_num);
719 if (node->decrypt_attempted) {
720 printf (", \"encstatus\": [{\"status\": ");
721 if (node->decrypt_success) {
729 if (node->verify_attempted) {
730 #ifdef GMIME_ATLEAST_26
731 format_part_sigstatus_json (node->sig_list);
733 format_part_sigstatus_json (node->sig_validity);
737 printf (", \"content-type\": %s",
738 json_quote_str (local, g_mime_content_type_to_string (content_type)));
741 printf(", \"content-id\": %s", json_quote_str (local, cid));
743 if (GMIME_IS_PART (node->part)) {
744 const char *filename = g_mime_part_get_filename (GMIME_PART (node->part));
746 printf (", \"filename\": %s", json_quote_str (local, filename));
749 if (g_mime_content_type_is_type (content_type, "text", "*")) {
750 /* For non-HTML text parts, we include the content in the
751 * JSON. Since JSON must be Unicode, we handle charset
752 * decoding here and do not report a charset to the caller.
753 * For text/html parts, we do not include the content. If a
754 * caller is interested in text/html parts, it should retrieve
755 * them separately and they will not be decoded. Since this
756 * makes charset decoding the responsibility on the caller, we
757 * report the charset for text/html parts.
759 if (g_mime_content_type_is_type (content_type, "text", "html")) {
760 const char *content_charset = g_mime_object_get_content_type_parameter (meta, "charset");
762 if (content_charset != NULL)
763 printf (", \"content-charset\": %s", json_quote_str (local, content_charset));
765 show_text_part_content (node->part, stream_memory);
766 part_content = g_mime_stream_mem_get_byte_array (GMIME_STREAM_MEM (stream_memory));
768 printf (", \"content\": %s", json_quote_chararray (local, (char *) part_content->data, part_content->len));
770 } else if (g_mime_content_type_is_type (content_type, "multipart", "*")) {
771 printf (", \"content\": [");
772 } else if (g_mime_content_type_is_type (content_type, "message", "rfc822")) {
773 printf (", \"content\": [{");
777 g_object_unref (stream_memory);
779 if (GMIME_IS_MESSAGE (node->part)) {
780 printf ("\"headers\": {");
781 format_headers_message_part_json (GMIME_MESSAGE (node->part));
784 printf (", \"body\": [");
787 for (i = 0; i < node->nchildren; i++)
788 format_part_json (ctx, mime_node_child (node, i), i == 0);
790 if (GMIME_IS_MESSAGE (node->part))
793 if (g_mime_content_type_is_type (content_type, "multipart", "*"))
795 else if (g_mime_content_type_is_type (content_type, "message", "rfc822"))
804 format_part_json_entry (const void *ctx, mime_node_t *node, unused (int indent),
805 unused (const notmuch_show_params_t *params))
807 format_part_json (ctx, node, TRUE);
811 show_message (void *ctx,
812 const notmuch_show_format_t *format,
813 notmuch_message_t *message,
815 notmuch_show_params_t *params)
818 void *local = talloc_new (ctx);
819 mime_node_t *root, *part;
821 if (mime_node_open (local, message, params->cryptoctx, params->decrypt,
822 &root) == NOTMUCH_STATUS_SUCCESS &&
823 (part = mime_node_seek_dfs (root, (params->part < 0 ?
825 format->part (local, part, indent, params);
830 if (params->part <= 0) {
831 fputs (format->message_start, stdout);
833 format->message(ctx, message, indent);
835 fputs (format->header_start, stdout);
837 format->header(ctx, message);
838 fputs (format->header_end, stdout);
840 fputs (format->body_start, stdout);
843 if (format->part_content)
844 show_message_body (message, format, params);
846 if (params->part <= 0) {
847 fputs (format->body_end, stdout);
849 fputs (format->message_end, stdout);
854 show_messages (void *ctx,
855 const notmuch_show_format_t *format,
856 notmuch_messages_t *messages,
858 notmuch_show_params_t *params)
860 notmuch_message_t *message;
861 notmuch_bool_t match;
865 fputs (format->message_set_start, stdout);
868 notmuch_messages_valid (messages);
869 notmuch_messages_move_to_next (messages))
872 fputs (format->message_set_sep, stdout);
875 fputs (format->message_set_start, stdout);
877 message = notmuch_messages_get (messages);
879 match = notmuch_message_get_flag (message, NOTMUCH_MESSAGE_FLAG_MATCH);
881 next_indent = indent;
883 if (match || params->entire_thread) {
884 show_message (ctx, format, message, indent, params);
885 next_indent = indent + 1;
887 fputs (format->message_set_sep, stdout);
892 notmuch_message_get_replies (message),
896 notmuch_message_destroy (message);
898 fputs (format->message_set_end, stdout);
901 fputs (format->message_set_end, stdout);
904 /* Formatted output of single message */
906 do_show_single (void *ctx,
907 notmuch_query_t *query,
908 const notmuch_show_format_t *format,
909 notmuch_show_params_t *params)
911 notmuch_messages_t *messages;
912 notmuch_message_t *message;
914 if (notmuch_query_count_messages (query) != 1) {
915 fprintf (stderr, "Error: search term did not match precisely one message.\n");
919 messages = notmuch_query_search_messages (query);
920 message = notmuch_messages_get (messages);
922 if (message == NULL) {
923 fprintf (stderr, "Error: Cannot find matching message.\n");
927 notmuch_message_set_flag (message, NOTMUCH_MESSAGE_FLAG_MATCH, 1);
929 /* Special case for --format=raw of full single message, just cat out file */
930 if (params->raw && 0 == params->part) {
932 const char *filename;
937 filename = notmuch_message_get_filename (message);
938 if (filename == NULL) {
939 fprintf (stderr, "Error: Cannot message filename.\n");
943 file = fopen (filename, "r");
945 fprintf (stderr, "Error: Cannot open file %s: %s\n", filename, strerror (errno));
949 while (!feof (file)) {
950 size = fread (buf, 1, sizeof (buf), file);
952 fprintf (stderr, "Error: Read failed from %s\n", filename);
957 if (fwrite (buf, size, 1, stdout) != 1) {
958 fprintf (stderr, "Error: Write failed\n");
968 show_message (ctx, format, message, 0, params);
975 /* Formatted output of threads */
978 notmuch_query_t *query,
979 const notmuch_show_format_t *format,
980 notmuch_show_params_t *params)
982 notmuch_threads_t *threads;
983 notmuch_thread_t *thread;
984 notmuch_messages_t *messages;
985 int first_toplevel = 1;
987 fputs (format->message_set_start, stdout);
989 for (threads = notmuch_query_search_threads (query);
990 notmuch_threads_valid (threads);
991 notmuch_threads_move_to_next (threads))
993 thread = notmuch_threads_get (threads);
995 messages = notmuch_thread_get_toplevel_messages (thread);
997 if (messages == NULL)
998 INTERNAL_ERROR ("Thread %s has no toplevel messages.\n",
999 notmuch_thread_get_thread_id (thread));
1001 if (!first_toplevel)
1002 fputs (format->message_set_sep, stdout);
1005 show_messages (ctx, format, messages, 0, params);
1007 notmuch_thread_destroy (thread);
1011 fputs (format->message_set_end, stdout);
1017 NOTMUCH_FORMAT_NOT_SPECIFIED,
1018 NOTMUCH_FORMAT_JSON,
1019 NOTMUCH_FORMAT_TEXT,
1020 NOTMUCH_FORMAT_MBOX,
1025 notmuch_show_command (void *ctx, unused (int argc), unused (char *argv[]))
1027 notmuch_config_t *config;
1028 notmuch_database_t *notmuch;
1029 notmuch_query_t *query;
1032 const notmuch_show_format_t *format = &format_text;
1033 notmuch_show_params_t params = { .part = -1 };
1034 int format_sel = NOTMUCH_FORMAT_NOT_SPECIFIED;
1035 notmuch_bool_t verify = FALSE;
1037 notmuch_opt_desc_t options[] = {
1038 { NOTMUCH_OPT_KEYWORD, &format_sel, "format", 'f',
1039 (notmuch_keyword_t []){ { "json", NOTMUCH_FORMAT_JSON },
1040 { "text", NOTMUCH_FORMAT_TEXT },
1041 { "mbox", NOTMUCH_FORMAT_MBOX },
1042 { "raw", NOTMUCH_FORMAT_RAW },
1044 { NOTMUCH_OPT_INT, ¶ms.part, "part", 'p', 0 },
1045 { NOTMUCH_OPT_BOOLEAN, ¶ms.entire_thread, "entire-thread", 't', 0 },
1046 { NOTMUCH_OPT_BOOLEAN, ¶ms.decrypt, "decrypt", 'd', 0 },
1047 { NOTMUCH_OPT_BOOLEAN, &verify, "verify", 'v', 0 },
1051 opt_index = parse_arguments (argc, argv, options, 1);
1052 if (opt_index < 0) {
1053 /* diagnostics already printed */
1057 if (format_sel == NOTMUCH_FORMAT_NOT_SPECIFIED) {
1058 /* if part was requested and format was not specified, use format=raw */
1059 if (params.part >= 0)
1060 format_sel = NOTMUCH_FORMAT_RAW;
1062 format_sel = NOTMUCH_FORMAT_TEXT;
1065 switch (format_sel) {
1066 case NOTMUCH_FORMAT_JSON:
1067 format = &format_json;
1068 params.entire_thread = TRUE;
1070 case NOTMUCH_FORMAT_TEXT:
1071 format = &format_text;
1073 case NOTMUCH_FORMAT_MBOX:
1074 if (params.part > 0) {
1075 fprintf (stderr, "Error: specifying parts is incompatible with mbox output format.\n");
1078 format = &format_mbox;
1080 case NOTMUCH_FORMAT_RAW:
1081 format = &format_raw;
1082 /* If --format=raw specified without specifying part, we can only
1083 * output single message, so set part=0 */
1084 if (params.part < 0)
1090 if (params.decrypt || verify) {
1091 #ifdef GMIME_ATLEAST_26
1092 /* TODO: GMimePasswordRequestFunc */
1093 params.cryptoctx = g_mime_gpg_context_new (NULL, "gpg");
1095 GMimeSession* session = g_object_new (g_mime_session_get_type(), NULL);
1096 params.cryptoctx = g_mime_gpg_context_new (session, "gpg");
1098 if (params.cryptoctx) {
1099 g_mime_gpg_context_set_always_trust ((GMimeGpgContext*) params.cryptoctx, FALSE);
1101 params.decrypt = FALSE;
1102 fprintf (stderr, "Failed to construct gpg context.\n");
1104 #ifndef GMIME_ATLEAST_26
1105 g_object_unref (session);
1109 config = notmuch_config_open (ctx, NULL, NULL);
1113 query_string = query_string_from_args (ctx, argc-opt_index, argv+opt_index);
1114 if (query_string == NULL) {
1115 fprintf (stderr, "Out of memory\n");
1119 if (*query_string == '\0') {
1120 fprintf (stderr, "Error: notmuch show requires at least one search term.\n");
1124 notmuch = notmuch_database_open (notmuch_config_get_database_path (config),
1125 NOTMUCH_DATABASE_MODE_READ_ONLY);
1126 if (notmuch == NULL)
1129 query = notmuch_query_create (notmuch, query_string);
1130 if (query == NULL) {
1131 fprintf (stderr, "Out of memory\n");
1135 if (params.part >= 0)
1136 ret = do_show_single (ctx, query, format, ¶ms);
1138 ret = do_show (ctx, query, format, ¶ms);
1140 notmuch_query_destroy (query);
1141 notmuch_database_close (notmuch);
1143 if (params.cryptoctx)
1144 g_object_unref(params.cryptoctx);