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);
26 static notmuch_status_t
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 = "",
37 static notmuch_status_t
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, \"excluded\": %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 notmuch_message_get_flag (message, NOTMUCH_MESSAGE_FLAG_EXCLUDED) ? "true" : "false",
150 json_quote_str (ctx_quote, notmuch_message_get_filename (message)),
151 date, relative_date);
153 for (tags = notmuch_message_get_tags (message);
154 notmuch_tags_valid (tags);
155 notmuch_tags_move_to_next (tags))
157 printf("%s%s", first ? "" : ",",
158 json_quote_str (ctx_quote, notmuch_tags_get (tags)));
162 talloc_free (ctx_quote);
165 /* Extract just the email address from the contents of a From:
168 _extract_email_address (const void *ctx, const char *from)
170 InternetAddressList *addresses;
171 InternetAddress *address;
172 InternetAddressMailbox *mailbox;
173 const char *email = "MAILER-DAEMON";
175 addresses = internet_address_list_parse_string (from);
177 /* Bail if there is no address here. */
178 if (addresses == NULL || internet_address_list_length (addresses) < 1)
181 /* Otherwise, just use the first address. */
182 address = internet_address_list_get_address (addresses, 0);
184 /* The From header should never contain an address group rather
185 * than a mailbox. So bail if it does. */
186 if (! INTERNET_ADDRESS_IS_MAILBOX (address))
189 mailbox = INTERNET_ADDRESS_MAILBOX (address);
190 email = internet_address_mailbox_get_addr (mailbox);
191 email = talloc_strdup (ctx, email);
195 g_object_unref (addresses);
200 /* Return 1 if 'line' is an mbox From_ line---that is, a line
201 * beginning with zero or more '>' characters followed by the
202 * characters 'F', 'r', 'o', 'm', and space.
204 * Any characters at all may appear after that in the line.
207 _is_from_line (const char *line)
209 const char *s = line;
217 if (STRNCMP_LITERAL (s, "From ") == 0)
224 format_headers_message_part_text (GMimeMessage *message)
226 InternetAddressList *recipients;
227 const char *recipients_string;
229 printf ("Subject: %s\n", g_mime_message_get_subject (message));
230 printf ("From: %s\n", g_mime_message_get_sender (message));
231 recipients = g_mime_message_get_recipients (message, GMIME_RECIPIENT_TYPE_TO);
232 recipients_string = internet_address_list_to_string (recipients, 0);
233 if (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)
241 printf ("Date: %s\n", g_mime_message_get_date_as_string (message));
245 format_headers_json (const void *ctx, GMimeMessage *message)
247 void *local = talloc_new (ctx);
248 InternetAddressList *recipients;
249 const char *recipients_string;
252 json_quote_str (local, "Subject"),
253 json_quote_str (local, g_mime_message_get_subject (message)));
255 json_quote_str (local, "From"),
256 json_quote_str (local, g_mime_message_get_sender (message)));
257 recipients = g_mime_message_get_recipients (message, GMIME_RECIPIENT_TYPE_TO);
258 recipients_string = internet_address_list_to_string (recipients, 0);
259 if (recipients_string)
261 json_quote_str (local, "To"),
262 json_quote_str (local, recipients_string));
263 recipients = g_mime_message_get_recipients (message, GMIME_RECIPIENT_TYPE_CC);
264 recipients_string = internet_address_list_to_string (recipients, 0);
265 if (recipients_string)
267 json_quote_str (local, "Cc"),
268 json_quote_str (local, recipients_string));
270 json_quote_str (local, "Date"),
271 json_quote_str (local, g_mime_message_get_date_as_string (message)));
276 /* Write a MIME text part out to the given stream.
278 * Both line-ending conversion (CRLF->LF) and charset conversion ( ->
279 * UTF-8) will be performed, so it is inappropriate to call this
280 * function with a non-text part. Doing so will trigger an internal
284 show_text_part_content (GMimeObject *part, GMimeStream *stream_out)
286 GMimeContentType *content_type = g_mime_object_get_content_type (GMIME_OBJECT (part));
287 GMimeStream *stream_filter = NULL;
288 GMimeDataWrapper *wrapper;
291 if (! g_mime_content_type_is_type (content_type, "text", "*"))
292 INTERNAL_ERROR ("Illegal request to format non-text part (%s) as text.",
293 g_mime_content_type_to_string (content_type));
295 if (stream_out == NULL)
298 stream_filter = g_mime_stream_filter_new (stream_out);
299 g_mime_stream_filter_add(GMIME_STREAM_FILTER (stream_filter),
300 g_mime_filter_crlf_new (FALSE, FALSE));
302 charset = g_mime_object_get_content_type_parameter (part, "charset");
304 GMimeFilter *charset_filter;
305 charset_filter = g_mime_filter_charset_new (charset, "UTF-8");
306 /* This result can be NULL for things like "unknown-8bit".
307 * Don't set a NULL filter as that makes GMime print
308 * annoying assertion-failure messages on stderr. */
309 if (charset_filter) {
310 g_mime_stream_filter_add (GMIME_STREAM_FILTER (stream_filter),
312 g_object_unref (charset_filter);
317 wrapper = g_mime_part_get_content_object (GMIME_PART (part));
318 if (wrapper && stream_filter)
319 g_mime_data_wrapper_write_to_stream (wrapper, stream_filter);
321 g_object_unref(stream_filter);
324 #ifdef GMIME_ATLEAST_26
326 signature_status_to_string (GMimeSignatureStatus x)
329 case GMIME_SIGNATURE_STATUS_GOOD:
331 case GMIME_SIGNATURE_STATUS_BAD:
333 case GMIME_SIGNATURE_STATUS_ERROR:
340 signer_status_to_string (GMimeSignerStatus x)
343 case GMIME_SIGNER_STATUS_NONE:
345 case GMIME_SIGNER_STATUS_GOOD:
347 case GMIME_SIGNER_STATUS_BAD:
349 case GMIME_SIGNER_STATUS_ERROR:
356 #ifdef GMIME_ATLEAST_26
358 format_part_sigstatus_json (mime_node_t *node)
360 GMimeSignatureList *siglist = node->sig_list;
369 void *ctx_quote = talloc_new (NULL);
371 for (i = 0; i < g_mime_signature_list_length (siglist); i++) {
372 GMimeSignature *signature = g_mime_signature_list_get_signature (siglist, i);
380 GMimeSignatureStatus status = g_mime_signature_get_status (signature);
381 printf ("\"status\": %s",
382 json_quote_str (ctx_quote,
383 signature_status_to_string (status)));
385 GMimeCertificate *certificate = g_mime_signature_get_certificate (signature);
386 if (status == GMIME_SIGNATURE_STATUS_GOOD) {
388 printf (", \"fingerprint\": %s", json_quote_str (ctx_quote, g_mime_certificate_get_fingerprint (certificate)));
389 /* these dates are seconds since the epoch; should we
390 * provide a more human-readable format string? */
391 time_t created = g_mime_signature_get_created (signature);
393 printf (", \"created\": %d", (int) created);
394 time_t expires = g_mime_signature_get_expires (signature);
396 printf (", \"expires\": %d", (int) expires);
397 /* output user id only if validity is FULL or ULTIMATE. */
398 /* note that gmime is using the term "trust" here, which
399 * is WRONG. It's actually user id "validity". */
401 const char *name = g_mime_certificate_get_name (certificate);
402 GMimeCertificateTrust trust = g_mime_certificate_get_trust (certificate);
403 if (name && (trust == GMIME_CERTIFICATE_TRUST_FULLY || trust == GMIME_CERTIFICATE_TRUST_ULTIMATE))
404 printf (", \"userid\": %s", json_quote_str (ctx_quote, name));
406 } else if (certificate) {
407 const char *key_id = g_mime_certificate_get_key_id (certificate);
409 printf (", \"keyid\": %s", json_quote_str (ctx_quote, key_id));
412 GMimeSignatureError errors = g_mime_signature_get_errors (signature);
413 if (errors != GMIME_SIGNATURE_ERROR_NONE) {
414 printf (", \"errors\": %d", errors);
422 talloc_free (ctx_quote);
426 format_part_sigstatus_json (mime_node_t *node)
428 const GMimeSignatureValidity* validity = node->sig_validity;
437 const GMimeSigner *signer = g_mime_signature_validity_get_signers (validity);
439 void *ctx_quote = talloc_new (NULL);
450 printf ("\"status\": %s",
451 json_quote_str (ctx_quote,
452 signer_status_to_string (signer->status)));
454 if (signer->status == GMIME_SIGNER_STATUS_GOOD)
456 if (signer->fingerprint)
457 printf (", \"fingerprint\": %s", json_quote_str (ctx_quote, signer->fingerprint));
458 /* these dates are seconds since the epoch; should we
459 * provide a more human-readable format string? */
461 printf (", \"created\": %d", (int) signer->created);
463 printf (", \"expires\": %d", (int) signer->expires);
464 /* output user id only if validity is FULL or ULTIMATE. */
465 /* note that gmime is using the term "trust" here, which
466 * is WRONG. It's actually user id "validity". */
467 if ((signer->name) && (signer->trust)) {
468 if ((signer->trust == GMIME_SIGNER_TRUST_FULLY) || (signer->trust == GMIME_SIGNER_TRUST_ULTIMATE))
469 printf (", \"userid\": %s", json_quote_str (ctx_quote, signer->name));
473 printf (", \"keyid\": %s", json_quote_str (ctx_quote, signer->keyid));
475 if (signer->errors != GMIME_SIGNER_ERROR_NONE) {
476 printf (", \"errors\": %d", signer->errors);
480 signer = signer->next;
485 talloc_free (ctx_quote);
490 format_part_content_raw (GMimeObject *part)
492 if (! GMIME_IS_PART (part))
495 GMimeStream *stream_stdout;
496 GMimeStream *stream_filter = NULL;
497 GMimeDataWrapper *wrapper;
499 stream_stdout = g_mime_stream_file_new (stdout);
500 g_mime_stream_file_set_owner (GMIME_STREAM_FILE (stream_stdout), FALSE);
502 stream_filter = g_mime_stream_filter_new (stream_stdout);
504 wrapper = g_mime_part_get_content_object (GMIME_PART (part));
506 if (wrapper && stream_filter)
507 g_mime_data_wrapper_write_to_stream (wrapper, stream_filter);
510 g_object_unref (stream_filter);
513 g_object_unref(stream_stdout);
516 static notmuch_status_t
517 format_part_text (const void *ctx, mime_node_t *node,
518 int indent, const notmuch_show_params_t *params)
520 /* The disposition and content-type metadata are associated with
521 * the envelope for message parts */
522 GMimeObject *meta = node->envelope_part ?
523 GMIME_OBJECT (node->envelope_part) : node->part;
524 GMimeContentType *content_type = g_mime_object_get_content_type (meta);
525 const notmuch_bool_t leaf = GMIME_IS_PART (node->part);
526 const char *part_type;
529 if (node->envelope_file) {
530 notmuch_message_t *message = node->envelope_file;
532 part_type = "message";
533 printf ("\f%s{ id:%s depth:%d match:%d excluded:%d filename:%s\n",
535 notmuch_message_get_message_id (message),
537 notmuch_message_get_flag (message, NOTMUCH_MESSAGE_FLAG_MATCH) ? 1 : 0,
538 notmuch_message_get_flag (message, NOTMUCH_MESSAGE_FLAG_EXCLUDED) ? 1 : 0,
539 notmuch_message_get_filename (message));
541 GMimeContentDisposition *disposition = g_mime_object_get_content_disposition (meta);
542 const char *cid = g_mime_object_get_content_id (meta);
543 const char *filename = leaf ?
544 g_mime_part_get_filename (GMIME_PART (node->part)) : NULL;
547 strcmp (disposition->disposition, GMIME_DISPOSITION_ATTACHMENT) == 0)
548 part_type = "attachment";
552 printf ("\f%s{ ID: %d", part_type, node->part_num);
554 printf (", Filename: %s", filename);
556 printf (", Content-id: %s", cid);
557 printf (", Content-type: %s\n", g_mime_content_type_to_string (content_type));
560 if (GMIME_IS_MESSAGE (node->part)) {
561 GMimeMessage *message = GMIME_MESSAGE (node->part);
562 InternetAddressList *recipients;
563 const char *recipients_string;
565 printf ("\fheader{\n");
566 if (node->envelope_file)
567 printf ("%s\n", _get_one_line_summary (ctx, node->envelope_file));
568 printf ("Subject: %s\n", g_mime_message_get_subject (message));
569 printf ("From: %s\n", g_mime_message_get_sender (message));
570 recipients = g_mime_message_get_recipients (message, GMIME_RECIPIENT_TYPE_TO);
571 recipients_string = internet_address_list_to_string (recipients, 0);
572 if (recipients_string)
573 printf ("To: %s\n", recipients_string);
574 recipients = g_mime_message_get_recipients (message, GMIME_RECIPIENT_TYPE_CC);
575 recipients_string = internet_address_list_to_string (recipients, 0);
576 if (recipients_string)
577 printf ("Cc: %s\n", recipients_string);
578 printf ("Date: %s\n", g_mime_message_get_date_as_string (message));
579 printf ("\fheader}\n");
581 printf ("\fbody{\n");
585 if (g_mime_content_type_is_type (content_type, "text", "*") &&
586 !g_mime_content_type_is_type (content_type, "text", "html"))
588 GMimeStream *stream_stdout = g_mime_stream_file_new (stdout);
589 g_mime_stream_file_set_owner (GMIME_STREAM_FILE (stream_stdout), FALSE);
590 show_text_part_content (node->part, stream_stdout);
591 g_object_unref(stream_stdout);
593 printf ("Non-text part: %s\n",
594 g_mime_content_type_to_string (content_type));
598 for (i = 0; i < node->nchildren; i++)
599 format_part_text (ctx, mime_node_child (node, i), indent, params);
601 if (GMIME_IS_MESSAGE (node->part))
602 printf ("\fbody}\n");
604 printf ("\f%s}\n", part_type);
606 return NOTMUCH_STATUS_SUCCESS;
610 format_part_json (const void *ctx, mime_node_t *node, notmuch_bool_t first)
612 /* Any changes to the JSON format should be reflected in the file
615 if (node->envelope_file) {
617 format_message_json (ctx, node->envelope_file);
619 printf ("\"headers\": ");
620 format_headers_json (ctx, GMIME_MESSAGE (node->part));
622 printf (", \"body\": [");
623 format_part_json (ctx, mime_node_child (node, 0), first);
629 void *local = talloc_new (ctx);
630 /* The disposition and content-type metadata are associated with
631 * the envelope for message parts */
632 GMimeObject *meta = node->envelope_part ?
633 GMIME_OBJECT (node->envelope_part) : node->part;
634 GMimeContentType *content_type = g_mime_object_get_content_type (meta);
635 const char *cid = g_mime_object_get_content_id (meta);
636 const char *filename = GMIME_IS_PART (node->part) ?
637 g_mime_part_get_filename (GMIME_PART (node->part)) : NULL;
638 const char *terminator = "";
644 printf ("{\"id\": %d", node->part_num);
646 if (node->decrypt_attempted)
647 printf (", \"encstatus\": [{\"status\": \"%s\"}]",
648 node->decrypt_success ? "good" : "bad");
650 if (node->verify_attempted) {
651 printf (", \"sigstatus\": ");
652 format_part_sigstatus_json (node);
655 printf (", \"content-type\": %s",
656 json_quote_str (local, g_mime_content_type_to_string (content_type)));
659 printf (", \"content-id\": %s", json_quote_str (local, cid));
662 printf (", \"filename\": %s", json_quote_str (local, filename));
664 if (GMIME_IS_PART (node->part)) {
665 /* For non-HTML text parts, we include the content in the
666 * JSON. Since JSON must be Unicode, we handle charset
667 * decoding here and do not report a charset to the caller.
668 * For text/html parts, we do not include the content. If a
669 * caller is interested in text/html parts, it should retrieve
670 * them separately and they will not be decoded. Since this
671 * makes charset decoding the responsibility on the caller, we
672 * report the charset for text/html parts.
674 if (g_mime_content_type_is_type (content_type, "text", "html")) {
675 const char *content_charset = g_mime_object_get_content_type_parameter (meta, "charset");
677 if (content_charset != NULL)
678 printf (", \"content-charset\": %s", json_quote_str (local, content_charset));
679 } else if (g_mime_content_type_is_type (content_type, "text", "*")) {
680 GMimeStream *stream_memory = g_mime_stream_mem_new ();
681 GByteArray *part_content;
682 show_text_part_content (node->part, stream_memory);
683 part_content = g_mime_stream_mem_get_byte_array (GMIME_STREAM_MEM (stream_memory));
685 printf (", \"content\": %s", json_quote_chararray (local, (char *) part_content->data, part_content->len));
686 g_object_unref (stream_memory);
688 } else if (GMIME_IS_MULTIPART (node->part)) {
689 printf (", \"content\": [");
691 } else if (GMIME_IS_MESSAGE (node->part)) {
692 printf (", \"content\": [{");
693 printf ("\"headers\": ");
694 format_headers_json (local, GMIME_MESSAGE (node->part));
696 printf (", \"body\": [");
702 for (i = 0; i < node->nchildren; i++)
703 format_part_json (ctx, mime_node_child (node, i), i == 0);
705 printf ("%s}", terminator);
708 static notmuch_status_t
709 format_part_json_entry (const void *ctx, mime_node_t *node, unused (int indent),
710 unused (const notmuch_show_params_t *params))
712 format_part_json (ctx, node, TRUE);
714 return NOTMUCH_STATUS_SUCCESS;
717 /* Print a message in "mboxrd" format as documented, for example,
720 * http://qmail.org/qmail-manual-html/man5/mbox.html
723 format_message_mbox (const void *ctx,
724 notmuch_message_t *message,
727 const char *filename;
732 struct tm date_gmtime;
733 char date_asctime[26];
739 filename = notmuch_message_get_filename (message);
740 file = fopen (filename, "r");
742 fprintf (stderr, "Failed to open %s: %s\n",
743 filename, strerror (errno));
747 from = notmuch_message_get_header (message, "from");
748 from = _extract_email_address (ctx, from);
750 date = notmuch_message_get_date (message);
751 gmtime_r (&date, &date_gmtime);
752 asctime_r (&date_gmtime, date_asctime);
754 printf ("From %s %s", from, date_asctime);
756 while ((line_len = getline (&line, &line_size, file)) != -1 ) {
757 if (_is_from_line (line))
767 static notmuch_status_t
768 show_message (void *ctx,
769 const notmuch_show_format_t *format,
770 notmuch_message_t *message,
772 notmuch_show_params_t *params)
775 void *local = talloc_new (ctx);
776 mime_node_t *root, *part;
777 notmuch_status_t status;
779 status = mime_node_open (local, message, params->cryptoctx,
780 params->decrypt, &root);
783 part = mime_node_seek_dfs (root, (params->part < 0 ? 0 : params->part));
785 status = format->part (local, part, indent, params);
791 if (params->part <= 0) {
792 fputs (format->message_start, stdout);
794 format->message(ctx, message, indent);
796 fputs (format->header_start, stdout);
798 format->header(ctx, message);
799 fputs (format->header_end, stdout);
801 fputs (format->body_start, stdout);
804 if (format->part_content)
805 show_message_body (message, format, params);
807 if (params->part <= 0) {
808 fputs (format->body_end, stdout);
810 fputs (format->message_end, stdout);
813 return NOTMUCH_STATUS_SUCCESS;
816 static notmuch_status_t
817 show_messages (void *ctx,
818 const notmuch_show_format_t *format,
819 notmuch_messages_t *messages,
821 notmuch_show_params_t *params)
823 notmuch_message_t *message;
824 notmuch_bool_t match;
827 notmuch_status_t status, res = NOTMUCH_STATUS_SUCCESS;
829 fputs (format->message_set_start, stdout);
832 notmuch_messages_valid (messages);
833 notmuch_messages_move_to_next (messages))
836 fputs (format->message_set_sep, stdout);
839 fputs (format->message_set_start, stdout);
841 message = notmuch_messages_get (messages);
843 match = notmuch_message_get_flag (message, NOTMUCH_MESSAGE_FLAG_MATCH);
845 next_indent = indent;
847 if (match || params->entire_thread) {
848 status = show_message (ctx, format, message, indent, params);
851 next_indent = indent + 1;
854 fputs (format->message_set_sep, stdout);
857 status = show_messages (ctx,
859 notmuch_message_get_replies (message),
865 notmuch_message_destroy (message);
867 fputs (format->message_set_end, stdout);
870 fputs (format->message_set_end, stdout);
875 /* Formatted output of single message */
877 do_show_single (void *ctx,
878 notmuch_query_t *query,
879 const notmuch_show_format_t *format,
880 notmuch_show_params_t *params)
882 notmuch_messages_t *messages;
883 notmuch_message_t *message;
885 if (notmuch_query_count_messages (query) != 1) {
886 fprintf (stderr, "Error: search term did not match precisely one message.\n");
890 messages = notmuch_query_search_messages (query);
891 message = notmuch_messages_get (messages);
893 if (message == NULL) {
894 fprintf (stderr, "Error: Cannot find matching message.\n");
898 notmuch_message_set_flag (message, NOTMUCH_MESSAGE_FLAG_MATCH, 1);
900 /* Special case for --format=raw of full single message, just cat out file */
901 if (params->raw && 0 == params->part) {
903 const char *filename;
908 filename = notmuch_message_get_filename (message);
909 if (filename == NULL) {
910 fprintf (stderr, "Error: Cannot message filename.\n");
914 file = fopen (filename, "r");
916 fprintf (stderr, "Error: Cannot open file %s: %s\n", filename, strerror (errno));
920 while (!feof (file)) {
921 size = fread (buf, 1, sizeof (buf), file);
923 fprintf (stderr, "Error: Read failed from %s\n", filename);
928 if (fwrite (buf, size, 1, stdout) != 1) {
929 fprintf (stderr, "Error: Write failed\n");
941 return show_message (ctx, format, message, 0, params) != NOTMUCH_STATUS_SUCCESS;
946 /* Formatted output of threads */
949 notmuch_query_t *query,
950 const notmuch_show_format_t *format,
951 notmuch_show_params_t *params)
953 notmuch_threads_t *threads;
954 notmuch_thread_t *thread;
955 notmuch_messages_t *messages;
956 int first_toplevel = 1;
957 notmuch_status_t status, res = NOTMUCH_STATUS_SUCCESS;
959 fputs (format->message_set_start, stdout);
961 for (threads = notmuch_query_search_threads (query);
962 notmuch_threads_valid (threads);
963 notmuch_threads_move_to_next (threads))
965 thread = notmuch_threads_get (threads);
967 messages = notmuch_thread_get_toplevel_messages (thread);
969 if (messages == NULL)
970 INTERNAL_ERROR ("Thread %s has no toplevel messages.\n",
971 notmuch_thread_get_thread_id (thread));
974 fputs (format->message_set_sep, stdout);
977 status = show_messages (ctx, format, messages, 0, params);
981 notmuch_thread_destroy (thread);
985 fputs (format->message_set_end, stdout);
987 return res != NOTMUCH_STATUS_SUCCESS;
991 NOTMUCH_FORMAT_NOT_SPECIFIED,
999 notmuch_show_command (void *ctx, unused (int argc), unused (char *argv[]))
1001 notmuch_config_t *config;
1002 notmuch_database_t *notmuch;
1003 notmuch_query_t *query;
1006 const notmuch_show_format_t *format = &format_text;
1007 notmuch_show_params_t params = { .part = -1 };
1008 int format_sel = NOTMUCH_FORMAT_NOT_SPECIFIED;
1009 notmuch_bool_t verify = FALSE;
1010 notmuch_bool_t no_exclude = FALSE;
1012 notmuch_opt_desc_t options[] = {
1013 { NOTMUCH_OPT_KEYWORD, &format_sel, "format", 'f',
1014 (notmuch_keyword_t []){ { "json", NOTMUCH_FORMAT_JSON },
1015 { "text", NOTMUCH_FORMAT_TEXT },
1016 { "mbox", NOTMUCH_FORMAT_MBOX },
1017 { "raw", NOTMUCH_FORMAT_RAW },
1019 { NOTMUCH_OPT_INT, ¶ms.part, "part", 'p', 0 },
1020 { NOTMUCH_OPT_BOOLEAN, ¶ms.entire_thread, "entire-thread", 't', 0 },
1021 { NOTMUCH_OPT_BOOLEAN, ¶ms.decrypt, "decrypt", 'd', 0 },
1022 { NOTMUCH_OPT_BOOLEAN, &verify, "verify", 'v', 0 },
1023 { NOTMUCH_OPT_BOOLEAN, &no_exclude, "no-exclude", 'n', 0 },
1027 opt_index = parse_arguments (argc, argv, options, 1);
1028 if (opt_index < 0) {
1029 /* diagnostics already printed */
1033 if (format_sel == NOTMUCH_FORMAT_NOT_SPECIFIED) {
1034 /* if part was requested and format was not specified, use format=raw */
1035 if (params.part >= 0)
1036 format_sel = NOTMUCH_FORMAT_RAW;
1038 format_sel = NOTMUCH_FORMAT_TEXT;
1041 switch (format_sel) {
1042 case NOTMUCH_FORMAT_JSON:
1043 format = &format_json;
1044 params.entire_thread = TRUE;
1046 case NOTMUCH_FORMAT_TEXT:
1047 format = &format_text;
1049 case NOTMUCH_FORMAT_MBOX:
1050 if (params.part > 0) {
1051 fprintf (stderr, "Error: specifying parts is incompatible with mbox output format.\n");
1055 format = &format_mbox;
1057 case NOTMUCH_FORMAT_RAW:
1058 format = &format_raw;
1059 /* If --format=raw specified without specifying part, we can only
1060 * output single message, so set part=0 */
1061 if (params.part < 0)
1067 if (params.decrypt || verify) {
1068 #ifdef GMIME_ATLEAST_26
1069 /* TODO: GMimePasswordRequestFunc */
1070 params.cryptoctx = g_mime_gpg_context_new (NULL, "gpg");
1072 GMimeSession* session = g_object_new (g_mime_session_get_type(), NULL);
1073 params.cryptoctx = g_mime_gpg_context_new (session, "gpg");
1075 if (params.cryptoctx) {
1076 g_mime_gpg_context_set_always_trust ((GMimeGpgContext*) params.cryptoctx, FALSE);
1078 params.decrypt = FALSE;
1079 fprintf (stderr, "Failed to construct gpg context.\n");
1081 #ifndef GMIME_ATLEAST_26
1082 g_object_unref (session);
1086 config = notmuch_config_open (ctx, NULL, NULL);
1090 query_string = query_string_from_args (ctx, argc-opt_index, argv+opt_index);
1091 if (query_string == NULL) {
1092 fprintf (stderr, "Out of memory\n");
1096 if (*query_string == '\0') {
1097 fprintf (stderr, "Error: notmuch show requires at least one search term.\n");
1101 notmuch = notmuch_database_open (notmuch_config_get_database_path (config),
1102 NOTMUCH_DATABASE_MODE_READ_ONLY);
1103 if (notmuch == NULL)
1106 query = notmuch_query_create (notmuch, query_string);
1107 if (query == NULL) {
1108 fprintf (stderr, "Out of memory\n");
1112 /* if format=mbox then we can not output excluded messages as
1113 * there is no way to make the exclude flag available */
1114 if (format_sel == NOTMUCH_FORMAT_MBOX)
1115 notmuch_query_set_omit_excluded_messages (query, TRUE);
1117 /* If a single message is requested we do not use search_excludes. */
1118 if (params.part >= 0)
1119 ret = do_show_single (ctx, query, format, ¶ms);
1122 const char **search_exclude_tags;
1123 size_t search_exclude_tags_length;
1126 search_exclude_tags = notmuch_config_get_search_exclude_tags
1127 (config, &search_exclude_tags_length);
1128 for (i = 0; i < search_exclude_tags_length; i++)
1129 notmuch_query_add_tag_exclude (query, search_exclude_tags[i]);
1131 ret = do_show (ctx, query, format, ¶ms);
1135 notmuch_query_destroy (query);
1136 notmuch_database_close (notmuch);
1138 if (params.cryptoctx)
1139 g_object_unref(params.cryptoctx);