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_message_text (unused (const void *ctx),
25 notmuch_message_t *message,
28 format_headers_text (const void *ctx,
29 notmuch_message_t *message);
32 format_part_start_text (GMimeObject *part,
36 format_part_content_text (GMimeObject *part);
39 format_part_end_text (GMimeObject *part);
41 static const notmuch_show_format_t format_text = {
43 "\fmessage{ ", format_message_text,
44 "\fheader{\n", format_headers_text, "\fheader}\n",
46 format_part_start_text,
49 format_part_content_text,
58 format_message_json (const void *ctx,
59 notmuch_message_t *message,
62 format_headers_json (const void *ctx,
63 notmuch_message_t *message);
66 format_part_start_json (unused (GMimeObject *part),
70 format_part_encstatus_json (int status);
73 format_part_sigstatus_json (const GMimeSignatureValidity* validity);
76 format_part_content_json (GMimeObject *part);
79 format_part_end_json (GMimeObject *part);
81 static const notmuch_show_format_t format_json = {
83 "{", format_message_json,
84 ", \"headers\": {", format_headers_json, "}",
86 format_part_start_json,
87 format_part_encstatus_json,
88 format_part_sigstatus_json,
89 format_part_content_json,
98 format_message_mbox (const void *ctx,
99 notmuch_message_t *message,
100 unused (int indent));
102 static const notmuch_show_format_t format_mbox = {
104 "", format_message_mbox,
119 format_part_content_raw (GMimeObject *part);
121 static const notmuch_show_format_t format_raw = {
129 format_part_content_raw,
138 _get_tags_as_string (const void *ctx, notmuch_message_t *message)
140 notmuch_tags_t *tags;
145 result = talloc_strdup (ctx, "");
149 for (tags = notmuch_message_get_tags (message);
150 notmuch_tags_valid (tags);
151 notmuch_tags_move_to_next (tags))
153 tag = notmuch_tags_get (tags);
155 result = talloc_asprintf_append (result, "%s%s",
156 first ? "" : " ", tag);
163 /* Get a nice, single-line summary of message. */
165 _get_one_line_summary (const void *ctx, notmuch_message_t *message)
169 const char *relative_date;
172 from = notmuch_message_get_header (message, "from");
174 date = notmuch_message_get_date (message);
175 relative_date = notmuch_time_relative_date (ctx, date);
177 tags = _get_tags_as_string (ctx, message);
179 return talloc_asprintf (ctx, "%s (%s) (%s)",
180 from, relative_date, tags);
184 format_message_text (unused (const void *ctx), notmuch_message_t *message, int indent)
186 printf ("id:%s depth:%d match:%d filename:%s\n",
187 notmuch_message_get_message_id (message),
189 notmuch_message_get_flag (message, NOTMUCH_MESSAGE_FLAG_MATCH),
190 notmuch_message_get_filename (message));
194 format_message_json (const void *ctx, notmuch_message_t *message, unused (int indent))
196 notmuch_tags_t *tags;
198 void *ctx_quote = talloc_new (ctx);
200 const char *relative_date;
202 date = notmuch_message_get_date (message);
203 relative_date = notmuch_time_relative_date (ctx, date);
205 printf ("\"id\": %s, \"match\": %s, \"filename\": %s, \"timestamp\": %ld, \"date_relative\": \"%s\", \"tags\": [",
206 json_quote_str (ctx_quote, notmuch_message_get_message_id (message)),
207 notmuch_message_get_flag (message, NOTMUCH_MESSAGE_FLAG_MATCH) ? "true" : "false",
208 json_quote_str (ctx_quote, notmuch_message_get_filename (message)),
209 date, relative_date);
211 for (tags = notmuch_message_get_tags (message);
212 notmuch_tags_valid (tags);
213 notmuch_tags_move_to_next (tags))
215 printf("%s%s", first ? "" : ",",
216 json_quote_str (ctx_quote, notmuch_tags_get (tags)));
220 talloc_free (ctx_quote);
223 /* Extract just the email address from the contents of a From:
226 _extract_email_address (const void *ctx, const char *from)
228 InternetAddressList *addresses;
229 InternetAddress *address;
230 InternetAddressMailbox *mailbox;
231 const char *email = "MAILER-DAEMON";
233 addresses = internet_address_list_parse_string (from);
235 /* Bail if there is no address here. */
236 if (addresses == NULL || internet_address_list_length (addresses) < 1)
239 /* Otherwise, just use the first address. */
240 address = internet_address_list_get_address (addresses, 0);
242 /* The From header should never contain an address group rather
243 * than a mailbox. So bail if it does. */
244 if (! INTERNET_ADDRESS_IS_MAILBOX (address))
247 mailbox = INTERNET_ADDRESS_MAILBOX (address);
248 email = internet_address_mailbox_get_addr (mailbox);
249 email = talloc_strdup (ctx, email);
252 /* XXX: How to free addresses here? */
256 /* Return 1 if 'line' is an mbox From_ line---that is, a line
257 * beginning with zero or more '>' characters followed by the
258 * characters 'F', 'r', 'o', 'm', and space.
260 * Any characters at all may appear after that in the line.
263 _is_from_line (const char *line)
265 const char *s = line;
273 if (STRNCMP_LITERAL (s, "From ") == 0)
279 /* Print a message in "mboxrd" format as documented, for example,
282 * http://qmail.org/qmail-manual-html/man5/mbox.html
285 format_message_mbox (const void *ctx,
286 notmuch_message_t *message,
289 const char *filename;
294 struct tm date_gmtime;
295 char date_asctime[26];
301 filename = notmuch_message_get_filename (message);
302 file = fopen (filename, "r");
304 fprintf (stderr, "Failed to open %s: %s\n",
305 filename, strerror (errno));
309 from = notmuch_message_get_header (message, "from");
310 from = _extract_email_address (ctx, from);
312 date = notmuch_message_get_date (message);
313 gmtime_r (&date, &date_gmtime);
314 asctime_r (&date_gmtime, date_asctime);
316 printf ("From %s %s", from, date_asctime);
318 while ((line_len = getline (&line, &line_size, file)) != -1 ) {
319 if (_is_from_line (line))
330 format_headers_text (const void *ctx, notmuch_message_t *message)
332 const char *headers[] = {
333 "Subject", "From", "To", "Cc", "Bcc", "Date"
335 const char *name, *value;
338 printf ("%s\n", _get_one_line_summary (ctx, message));
340 for (i = 0; i < ARRAY_SIZE (headers); i++) {
342 value = notmuch_message_get_header (message, name);
343 if (value && strlen (value))
344 printf ("%s: %s\n", name, value);
349 format_headers_json (const void *ctx, notmuch_message_t *message)
351 const char *headers[] = {
352 "Subject", "From", "To", "Cc", "Bcc", "Date"
354 const char *name, *value;
356 int first_header = 1;
357 void *ctx_quote = talloc_new (ctx);
359 for (i = 0; i < ARRAY_SIZE (headers); i++) {
361 value = notmuch_message_get_header (message, name);
365 fputs (", ", stdout);
369 json_quote_str (ctx_quote, name),
370 json_quote_str (ctx_quote, value));
374 talloc_free (ctx_quote);
377 /* Write a MIME text part out to the given stream.
379 * Both line-ending conversion (CRLF->LF) and charset conversion ( ->
380 * UTF-8) will be performed, so it is inappropriate to call this
381 * function with a non-text part. Doing so will trigger an internal
385 show_text_part_content (GMimeObject *part, GMimeStream *stream_out)
387 GMimeContentType *content_type = g_mime_object_get_content_type (GMIME_OBJECT (part));
388 GMimeStream *stream_filter = NULL;
389 GMimeDataWrapper *wrapper;
392 if (! g_mime_content_type_is_type (content_type, "text", "*"))
393 INTERNAL_ERROR ("Illegal request to format non-text part (%s) as text.",
394 g_mime_content_type_to_string (content_type));
396 if (stream_out == NULL)
399 stream_filter = g_mime_stream_filter_new (stream_out);
400 g_mime_stream_filter_add(GMIME_STREAM_FILTER (stream_filter),
401 g_mime_filter_crlf_new (FALSE, FALSE));
403 charset = g_mime_object_get_content_type_parameter (part, "charset");
405 GMimeFilter *charset_filter;
406 charset_filter = g_mime_filter_charset_new (charset, "UTF-8");
407 /* This result can be NULL for things like "unknown-8bit".
408 * Don't set a NULL filter as that makes GMime print
409 * annoying assertion-failure messages on stderr. */
411 g_mime_stream_filter_add (GMIME_STREAM_FILTER (stream_filter),
415 wrapper = g_mime_part_get_content_object (GMIME_PART (part));
416 if (wrapper && stream_filter)
417 g_mime_data_wrapper_write_to_stream (wrapper, stream_filter);
419 g_object_unref(stream_filter);
423 signerstatustostring (GMimeSignerStatus x)
426 case GMIME_SIGNER_STATUS_NONE:
428 case GMIME_SIGNER_STATUS_GOOD:
430 case GMIME_SIGNER_STATUS_BAD:
432 case GMIME_SIGNER_STATUS_ERROR:
439 format_part_start_text (GMimeObject *part, int *part_count)
441 GMimeContentDisposition *disposition = g_mime_object_get_content_disposition (part);
444 strcmp (disposition->disposition, GMIME_DISPOSITION_ATTACHMENT) == 0)
446 printf ("\fattachment{ ID: %d", *part_count);
450 printf ("\fpart{ ID: %d", *part_count);
455 format_part_content_text (GMimeObject *part)
457 GMimeContentDisposition *disposition = g_mime_object_get_content_disposition (part);
458 GMimeContentType *content_type = g_mime_object_get_content_type (GMIME_OBJECT (part));
459 GMimeStream *stream_stdout = g_mime_stream_file_new (stdout);
461 printf (", Content-type: %s\n", g_mime_content_type_to_string (content_type));
464 strcmp (disposition->disposition, GMIME_DISPOSITION_ATTACHMENT) == 0)
466 const char *filename = g_mime_part_get_filename (GMIME_PART (part));
467 printf ("Attachment: %s (%s)\n", filename,
468 g_mime_content_type_to_string (content_type));
471 if (g_mime_content_type_is_type (content_type, "text", "*") &&
472 !g_mime_content_type_is_type (content_type, "text", "html"))
474 g_mime_stream_file_set_owner (GMIME_STREAM_FILE (stream_stdout), FALSE);
475 show_text_part_content (part, stream_stdout);
476 g_object_unref(stream_stdout);
478 else if (g_mime_content_type_is_type (content_type, "multipart", "*") ||
479 g_mime_content_type_is_type (content_type, "message", "rfc822"))
481 /* Do nothing for multipart since its content will be printed
486 printf ("Non-text part: %s\n",
487 g_mime_content_type_to_string (content_type));
492 format_part_end_text (GMimeObject *part)
494 GMimeContentDisposition *disposition;
496 disposition = g_mime_object_get_content_disposition (part);
498 strcmp (disposition->disposition, GMIME_DISPOSITION_ATTACHMENT) == 0)
500 printf ("\fattachment}\n");
504 printf ("\fpart}\n");
509 format_part_start_json (unused (GMimeObject *part), int *part_count)
511 printf ("{\"id\": %d", *part_count);
515 format_part_encstatus_json (int status)
517 printf (", \"encstatus\": [{\"status\": ");
527 format_part_sigstatus_json (const GMimeSignatureValidity* validity)
529 printf (", \"sigstatus\": [");
536 const GMimeSigner *signer = g_mime_signature_validity_get_signers (validity);
538 void *ctx_quote = talloc_new (NULL);
549 printf ("\"status\": %s", json_quote_str (ctx_quote, signerstatustostring(signer->status)));
551 if (signer->status == GMIME_SIGNER_STATUS_GOOD)
553 if (signer->fingerprint)
554 printf (", \"fingerprint\": %s", json_quote_str (ctx_quote, signer->fingerprint));
555 /* these dates are seconds since the epoch; should we
556 * provide a more human-readable format string? */
558 printf (", \"created\": %d", (int) signer->created);
560 printf (", \"expires\": %d", (int) signer->expires);
561 /* output user id only if validity is FULL or ULTIMATE. */
562 /* note that gmime is using the term "trust" here, which
563 * is WRONG. It's actually user id "validity". */
564 if ((signer->name) && (signer->trust)) {
565 if ((signer->trust == GMIME_SIGNER_TRUST_FULLY) || (signer->trust == GMIME_SIGNER_TRUST_ULTIMATE))
566 printf (", \"userid\": %s", json_quote_str (ctx_quote, signer->name));
570 printf (", \"keyid\": %s", json_quote_str (ctx_quote, signer->keyid));
572 if (signer->errors != GMIME_SIGNER_ERROR_NONE) {
573 printf (", \"errors\": %x", signer->errors);
577 signer = signer->next;
582 talloc_free (ctx_quote);
586 format_part_content_json (GMimeObject *part)
588 GMimeContentType *content_type = g_mime_object_get_content_type (GMIME_OBJECT (part));
589 GMimeStream *stream_memory = g_mime_stream_mem_new ();
590 const char *cid = g_mime_object_get_content_id (part);
591 void *ctx = talloc_new (NULL);
592 GMimeContentDisposition *disposition = g_mime_object_get_content_disposition (part);
593 GByteArray *part_content;
595 printf (", \"content-type\": %s",
596 json_quote_str (ctx, g_mime_content_type_to_string (content_type)));
599 printf(", \"content-id\": %s", json_quote_str (ctx, cid));
602 strcmp (disposition->disposition, GMIME_DISPOSITION_ATTACHMENT) == 0)
604 const char *filename = g_mime_part_get_filename (GMIME_PART (part));
606 printf (", \"filename\": %s", json_quote_str (ctx, filename));
609 if (g_mime_content_type_is_type (content_type, "text", "*") &&
610 !g_mime_content_type_is_type (content_type, "text", "html"))
612 show_text_part_content (part, stream_memory);
613 part_content = g_mime_stream_mem_get_byte_array (GMIME_STREAM_MEM (stream_memory));
615 printf (", \"content\": %s", json_quote_chararray (ctx, (char *) part_content->data, part_content->len));
617 else if (g_mime_content_type_is_type (content_type, "multipart", "*") ||
618 g_mime_content_type_is_type (content_type, "message", "rfc822"))
620 printf (", \"content\": [");
625 g_object_unref (stream_memory);
629 format_part_end_json (GMimeObject *part)
631 GMimeContentType *content_type;
633 content_type = g_mime_object_get_content_type (GMIME_OBJECT (part));
635 if (g_mime_content_type_is_type (content_type, "multipart", "*") ||
636 g_mime_content_type_is_type (content_type, "message", "rfc822"))
643 format_part_content_raw (GMimeObject *part)
645 GMimeStream *stream_stdout;
646 GMimeStream *stream_filter = NULL;
647 GMimeDataWrapper *wrapper;
649 stream_stdout = g_mime_stream_file_new (stdout);
650 g_mime_stream_file_set_owner (GMIME_STREAM_FILE (stream_stdout), FALSE);
652 stream_filter = g_mime_stream_filter_new (stream_stdout);
654 wrapper = g_mime_part_get_content_object (GMIME_PART (part));
656 if (wrapper && stream_filter)
657 g_mime_data_wrapper_write_to_stream (wrapper, stream_filter);
660 g_object_unref (stream_filter);
663 g_object_unref(stream_stdout);
667 show_message (void *ctx,
668 const notmuch_show_format_t *format,
669 notmuch_message_t *message,
671 notmuch_show_params_t *params)
673 if (params->part <= 0) {
674 fputs (format->message_start, stdout);
676 format->message(ctx, message, indent);
678 fputs (format->header_start, stdout);
680 format->header(ctx, message);
681 fputs (format->header_end, stdout);
683 fputs (format->body_start, stdout);
686 if (format->part_content)
687 show_message_body (notmuch_message_get_filename (message),
690 if (params->part <= 0) {
691 fputs (format->body_end, stdout);
693 fputs (format->message_end, stdout);
698 show_messages (void *ctx,
699 const notmuch_show_format_t *format,
700 notmuch_messages_t *messages,
702 notmuch_show_params_t *params)
704 notmuch_message_t *message;
705 notmuch_bool_t match;
709 fputs (format->message_set_start, stdout);
712 notmuch_messages_valid (messages);
713 notmuch_messages_move_to_next (messages))
716 fputs (format->message_set_sep, stdout);
719 fputs (format->message_set_start, stdout);
721 message = notmuch_messages_get (messages);
723 match = notmuch_message_get_flag (message, NOTMUCH_MESSAGE_FLAG_MATCH);
725 next_indent = indent;
727 if (match || params->entire_thread) {
728 show_message (ctx, format, message, indent, params);
729 next_indent = indent + 1;
731 fputs (format->message_set_sep, stdout);
736 notmuch_message_get_replies (message),
740 notmuch_message_destroy (message);
742 fputs (format->message_set_end, stdout);
745 fputs (format->message_set_end, stdout);
748 /* Formatted output of single message */
750 do_show_single (void *ctx,
751 notmuch_query_t *query,
752 const notmuch_show_format_t *format,
753 notmuch_show_params_t *params)
755 notmuch_messages_t *messages;
756 notmuch_message_t *message;
758 if (notmuch_query_count_messages (query) != 1) {
759 fprintf (stderr, "Error: search term did not match precisely one message.\n");
763 messages = notmuch_query_search_messages (query);
764 message = notmuch_messages_get (messages);
766 if (message == NULL) {
767 fprintf (stderr, "Error: Cannot find matching message.\n");
771 notmuch_message_set_flag (message, NOTMUCH_MESSAGE_FLAG_MATCH, 1);
773 /* Special case for --format=raw of full single message, just cat out file */
774 if (params->raw && 0 == params->part) {
776 const char *filename;
781 filename = notmuch_message_get_filename (message);
782 if (filename == NULL) {
783 fprintf (stderr, "Error: Cannot message filename.\n");
787 file = fopen (filename, "r");
789 fprintf (stderr, "Error: Cannot open file %s: %s\n", filename, strerror (errno));
793 while (!feof (file)) {
794 size = fread (buf, 1, sizeof (buf), file);
795 fwrite (buf, size, 1, stdout);
802 show_message (ctx, format, message, 0, params);
809 /* Formatted output of threads */
812 notmuch_query_t *query,
813 const notmuch_show_format_t *format,
814 notmuch_show_params_t *params)
816 notmuch_threads_t *threads;
817 notmuch_thread_t *thread;
818 notmuch_messages_t *messages;
819 int first_toplevel = 1;
821 fputs (format->message_set_start, stdout);
823 for (threads = notmuch_query_search_threads (query);
824 notmuch_threads_valid (threads);
825 notmuch_threads_move_to_next (threads))
827 thread = notmuch_threads_get (threads);
829 messages = notmuch_thread_get_toplevel_messages (thread);
831 if (messages == NULL)
832 INTERNAL_ERROR ("Thread %s has no toplevel messages.\n",
833 notmuch_thread_get_thread_id (thread));
836 fputs (format->message_set_sep, stdout);
839 show_messages (ctx, format, messages, 0, params);
841 notmuch_thread_destroy (thread);
845 fputs (format->message_set_end, stdout);
851 notmuch_show_command (void *ctx, unused (int argc), unused (char *argv[]))
853 notmuch_config_t *config;
854 notmuch_database_t *notmuch;
855 notmuch_query_t *query;
858 const notmuch_show_format_t *format = &format_text;
859 notmuch_show_params_t params;
861 int format_specified = 0;
864 params.entire_thread = 0;
867 params.cryptoctx = NULL;
870 for (i = 0; i < argc && argv[i][0] == '-'; i++) {
871 if (strcmp (argv[i], "--") == 0) {
875 if (STRNCMP_LITERAL (argv[i], "--format=") == 0) {
876 opt = argv[i] + sizeof ("--format=") - 1;
877 if (strcmp (opt, "text") == 0) {
878 format = &format_text;
879 } else if (strcmp (opt, "json") == 0) {
880 format = &format_json;
881 params.entire_thread = 1;
882 } else if (strcmp (opt, "mbox") == 0) {
883 format = &format_mbox;
885 } else if (strcmp (opt, "raw") == 0) {
886 format = &format_raw;
889 fprintf (stderr, "Invalid value for --format: %s\n", opt);
892 format_specified = 1;
893 } else if (STRNCMP_LITERAL (argv[i], "--part=") == 0) {
894 params.part = atoi(argv[i] + sizeof ("--part=") - 1);
895 } else if (STRNCMP_LITERAL (argv[i], "--entire-thread") == 0) {
896 params.entire_thread = 1;
897 } else if ((STRNCMP_LITERAL (argv[i], "--verify") == 0) ||
898 (STRNCMP_LITERAL (argv[i], "--decrypt") == 0)) {
899 if (params.cryptoctx == NULL) {
900 GMimeSession* session = g_object_new(notmuch_gmime_session_get_type(), NULL);
901 if (NULL == (params.cryptoctx = g_mime_gpg_context_new(session, "gpg")))
902 fprintf (stderr, "Failed to construct gpg context.\n");
904 g_mime_gpg_context_set_always_trust((GMimeGpgContext*)params.cryptoctx, FALSE);
905 g_object_unref (session);
908 if (STRNCMP_LITERAL (argv[i], "--decrypt") == 0)
911 fprintf (stderr, "Unrecognized option: %s\n", argv[i]);
919 config = notmuch_config_open (ctx, NULL, NULL);
923 query_string = query_string_from_args (ctx, argc, argv);
924 if (query_string == NULL) {
925 fprintf (stderr, "Out of memory\n");
929 if (mbox && params.part > 0) {
930 fprintf (stderr, "Error: specifying parts is incompatible with mbox output format.\n");
934 if (*query_string == '\0') {
935 fprintf (stderr, "Error: notmuch show requires at least one search term.\n");
939 notmuch = notmuch_database_open (notmuch_config_get_database_path (config),
940 NOTMUCH_DATABASE_MODE_READ_ONLY);
944 query = notmuch_query_create (notmuch, query_string);
946 fprintf (stderr, "Out of memory\n");
950 /* if part was requested and format was not specified, use format=raw */
951 if (params.part >= 0 && !format_specified)
952 format = &format_raw;
954 /* If --format=raw specified without specifying part, we can only
955 * output single message, so set part=0 */
956 if (params.raw && params.part < 0)
959 if (params.part >= 0)
960 return do_show_single (ctx, query, format, ¶ms);
962 return do_show (ctx, query, format, ¶ms);
964 notmuch_query_destroy (query);
965 notmuch_database_close (notmuch);
967 if (params.cryptoctx)
968 g_object_unref(params.cryptoctx);