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,
48 format_part_content_text,
57 format_message_json (const void *ctx,
58 notmuch_message_t *message,
61 format_headers_json (const void *ctx,
62 notmuch_message_t *message);
65 format_part_start_json (unused (GMimeObject *part),
69 format_part_sigstatus_json (const GMimeSignatureValidity* validity);
72 format_part_content_json (GMimeObject *part);
75 format_part_end_json (GMimeObject *part);
77 static const notmuch_show_format_t format_json = {
79 "{", format_message_json,
80 ", \"headers\": {", format_headers_json, "}",
82 format_part_start_json,
83 format_part_sigstatus_json,
84 format_part_content_json,
93 format_message_mbox (const void *ctx,
94 notmuch_message_t *message,
97 static const notmuch_show_format_t format_mbox = {
99 "", format_message_mbox,
113 format_part_content_raw (GMimeObject *part);
115 static const notmuch_show_format_t format_raw = {
122 format_part_content_raw,
131 _get_tags_as_string (const void *ctx, notmuch_message_t *message)
133 notmuch_tags_t *tags;
138 result = talloc_strdup (ctx, "");
142 for (tags = notmuch_message_get_tags (message);
143 notmuch_tags_valid (tags);
144 notmuch_tags_move_to_next (tags))
146 tag = notmuch_tags_get (tags);
148 result = talloc_asprintf_append (result, "%s%s",
149 first ? "" : " ", tag);
156 /* Get a nice, single-line summary of message. */
158 _get_one_line_summary (const void *ctx, notmuch_message_t *message)
162 const char *relative_date;
165 from = notmuch_message_get_header (message, "from");
167 date = notmuch_message_get_date (message);
168 relative_date = notmuch_time_relative_date (ctx, date);
170 tags = _get_tags_as_string (ctx, message);
172 return talloc_asprintf (ctx, "%s (%s) (%s)",
173 from, relative_date, tags);
177 format_message_text (unused (const void *ctx), notmuch_message_t *message, int indent)
179 printf ("id:%s depth:%d match:%d filename:%s\n",
180 notmuch_message_get_message_id (message),
182 notmuch_message_get_flag (message, NOTMUCH_MESSAGE_FLAG_MATCH),
183 notmuch_message_get_filename (message));
187 format_message_json (const void *ctx, notmuch_message_t *message, unused (int indent))
189 notmuch_tags_t *tags;
191 void *ctx_quote = talloc_new (ctx);
193 const char *relative_date;
195 date = notmuch_message_get_date (message);
196 relative_date = notmuch_time_relative_date (ctx, date);
198 printf ("\"id\": %s, \"match\": %s, \"filename\": %s, \"timestamp\": %ld, \"date_relative\": \"%s\", \"tags\": [",
199 json_quote_str (ctx_quote, notmuch_message_get_message_id (message)),
200 notmuch_message_get_flag (message, NOTMUCH_MESSAGE_FLAG_MATCH) ? "true" : "false",
201 json_quote_str (ctx_quote, notmuch_message_get_filename (message)),
202 date, relative_date);
204 for (tags = notmuch_message_get_tags (message);
205 notmuch_tags_valid (tags);
206 notmuch_tags_move_to_next (tags))
208 printf("%s%s", first ? "" : ",",
209 json_quote_str (ctx_quote, notmuch_tags_get (tags)));
213 talloc_free (ctx_quote);
216 /* Extract just the email address from the contents of a From:
219 _extract_email_address (const void *ctx, const char *from)
221 InternetAddressList *addresses;
222 InternetAddress *address;
223 InternetAddressMailbox *mailbox;
224 const char *email = "MAILER-DAEMON";
226 addresses = internet_address_list_parse_string (from);
228 /* Bail if there is no address here. */
229 if (addresses == NULL || internet_address_list_length (addresses) < 1)
232 /* Otherwise, just use the first address. */
233 address = internet_address_list_get_address (addresses, 0);
235 /* The From header should never contain an address group rather
236 * than a mailbox. So bail if it does. */
237 if (! INTERNET_ADDRESS_IS_MAILBOX (address))
240 mailbox = INTERNET_ADDRESS_MAILBOX (address);
241 email = internet_address_mailbox_get_addr (mailbox);
242 email = talloc_strdup (ctx, email);
245 /* XXX: How to free addresses here? */
249 /* Return 1 if 'line' is an mbox From_ line---that is, a line
250 * beginning with zero or more '>' characters followed by the
251 * characters 'F', 'r', 'o', 'm', and space.
253 * Any characters at all may appear after that in the line.
256 _is_from_line (const char *line)
258 const char *s = line;
266 if (STRNCMP_LITERAL (s, "From ") == 0)
272 /* Print a message in "mboxrd" format as documented, for example,
275 * http://qmail.org/qmail-manual-html/man5/mbox.html
278 format_message_mbox (const void *ctx,
279 notmuch_message_t *message,
282 const char *filename;
287 struct tm date_gmtime;
288 char date_asctime[26];
294 filename = notmuch_message_get_filename (message);
295 file = fopen (filename, "r");
297 fprintf (stderr, "Failed to open %s: %s\n",
298 filename, strerror (errno));
302 from = notmuch_message_get_header (message, "from");
303 from = _extract_email_address (ctx, from);
305 date = notmuch_message_get_date (message);
306 gmtime_r (&date, &date_gmtime);
307 asctime_r (&date_gmtime, date_asctime);
309 printf ("From %s %s", from, date_asctime);
311 while ((line_len = getline (&line, &line_size, file)) != -1 ) {
312 if (_is_from_line (line))
323 format_headers_text (const void *ctx, notmuch_message_t *message)
325 const char *headers[] = {
326 "Subject", "From", "To", "Cc", "Bcc", "Date"
328 const char *name, *value;
331 printf ("%s\n", _get_one_line_summary (ctx, message));
333 for (i = 0; i < ARRAY_SIZE (headers); i++) {
335 value = notmuch_message_get_header (message, name);
336 if (value && strlen (value))
337 printf ("%s: %s\n", name, value);
342 format_headers_json (const void *ctx, notmuch_message_t *message)
344 const char *headers[] = {
345 "Subject", "From", "To", "Cc", "Bcc", "Date"
347 const char *name, *value;
349 int first_header = 1;
350 void *ctx_quote = talloc_new (ctx);
352 for (i = 0; i < ARRAY_SIZE (headers); i++) {
354 value = notmuch_message_get_header (message, name);
358 fputs (", ", stdout);
362 json_quote_str (ctx_quote, name),
363 json_quote_str (ctx_quote, value));
367 talloc_free (ctx_quote);
371 show_part_content (GMimeObject *part, GMimeStream *stream_out)
373 GMimeStream *stream_filter = NULL;
374 GMimeDataWrapper *wrapper;
377 /* do nothing if this is a multipart */
378 if (GMIME_IS_MULTIPART (part) || GMIME_IS_MESSAGE_PART (part))
381 charset = g_mime_object_get_content_type_parameter (part, "charset");
384 stream_filter = g_mime_stream_filter_new (stream_out);
385 g_mime_stream_filter_add(GMIME_STREAM_FILTER (stream_filter),
386 g_mime_filter_crlf_new (FALSE, FALSE));
388 GMimeFilter *charset_filter;
389 charset_filter = g_mime_filter_charset_new (charset, "UTF-8");
390 /* This result can be NULL for things like "unknown-8bit".
391 * Don't set a NULL filter as that makes GMime print
392 * annoying assertion-failure messages on stderr. */
394 g_mime_stream_filter_add (GMIME_STREAM_FILTER (stream_filter),
399 wrapper = g_mime_part_get_content_object (GMIME_PART (part));
400 if (wrapper && stream_filter)
401 g_mime_data_wrapper_write_to_stream (wrapper, stream_filter);
403 g_object_unref(stream_filter);
407 signerstatustostring (GMimeSignerStatus x)
410 case GMIME_SIGNER_STATUS_NONE:
412 case GMIME_SIGNER_STATUS_GOOD:
414 case GMIME_SIGNER_STATUS_BAD:
416 case GMIME_SIGNER_STATUS_ERROR:
423 format_part_start_text (GMimeObject *part, int *part_count)
425 GMimeContentDisposition *disposition = g_mime_object_get_content_disposition (part);
428 strcmp (disposition->disposition, GMIME_DISPOSITION_ATTACHMENT) == 0)
430 printf ("\fattachment{ ID: %d", *part_count);
434 printf ("\fpart{ ID: %d", *part_count);
439 format_part_content_text (GMimeObject *part)
441 GMimeContentDisposition *disposition = g_mime_object_get_content_disposition (part);
442 GMimeContentType *content_type = g_mime_object_get_content_type (GMIME_OBJECT (part));
443 GMimeStream *stream_stdout = g_mime_stream_file_new (stdout);
445 printf (", Content-type: %s\n", g_mime_content_type_to_string (content_type));
448 strcmp (disposition->disposition, GMIME_DISPOSITION_ATTACHMENT) == 0)
450 const char *filename = g_mime_part_get_filename (GMIME_PART (part));
451 printf ("Attachment: %s (%s)\n", filename,
452 g_mime_content_type_to_string (content_type));
455 if (g_mime_content_type_is_type (content_type, "text", "*") &&
456 !g_mime_content_type_is_type (content_type, "text", "html"))
458 g_mime_stream_file_set_owner (GMIME_STREAM_FILE (stream_stdout), FALSE);
459 show_part_content (part, stream_stdout);
460 g_object_unref(stream_stdout);
462 else if (g_mime_content_type_is_type (content_type, "multipart", "*") ||
463 g_mime_content_type_is_type (content_type, "message", "rfc822"))
465 /* Do nothing for multipart since its content will be printed
470 printf ("Non-text part: %s\n",
471 g_mime_content_type_to_string (content_type));
476 format_part_end_text (GMimeObject *part)
478 GMimeContentDisposition *disposition;
480 disposition = g_mime_object_get_content_disposition (part);
482 strcmp (disposition->disposition, GMIME_DISPOSITION_ATTACHMENT) == 0)
484 printf ("\fattachment}\n");
488 printf ("\fpart}\n");
493 format_part_start_json (unused (GMimeObject *part), int *part_count)
495 printf ("{\"id\": %d", *part_count);
499 format_part_sigstatus_json (const GMimeSignatureValidity* validity)
501 printf (", \"sigstatus\": [");
508 const GMimeSigner *signer = g_mime_signature_validity_get_signers (validity);
510 void *ctx_quote = talloc_new (NULL);
521 printf ("\"status\": %s", json_quote_str (ctx_quote, signerstatustostring(signer->status)));
523 if (signer->status == GMIME_SIGNER_STATUS_GOOD)
525 if (signer->fingerprint)
526 printf (", \"fingerprint\": %s", json_quote_str (ctx_quote, signer->fingerprint));
527 /* these dates are seconds since the epoch; should we
528 * provide a more human-readable format string? */
530 printf (", \"created\": %d", (int) signer->created);
532 printf (", \"expires\": %d", (int) signer->expires);
533 /* output user id only if validity is FULL or ULTIMATE. */
534 /* note that gmime is using the term "trust" here, which
535 * is WRONG. It's actually user id "validity". */
536 if ((signer->name) && (signer->trust)) {
537 if ((signer->trust == GMIME_SIGNER_TRUST_FULLY) || (signer->trust == GMIME_SIGNER_TRUST_ULTIMATE))
538 printf (", \"userid\": %s", json_quote_str (ctx_quote, signer->name));
542 printf (", \"keyid\": %s", json_quote_str (ctx_quote, signer->keyid));
544 if (signer->errors != GMIME_SIGNER_ERROR_NONE) {
545 printf (", \"errors\": %x", signer->errors);
549 signer = signer->next;
554 talloc_free (ctx_quote);
558 format_part_content_json (GMimeObject *part)
560 GMimeContentType *content_type = g_mime_object_get_content_type (GMIME_OBJECT (part));
561 GMimeStream *stream_memory = g_mime_stream_mem_new ();
562 const char *cid = g_mime_object_get_content_id (part);
563 void *ctx = talloc_new (NULL);
564 GMimeContentDisposition *disposition = g_mime_object_get_content_disposition (part);
565 GByteArray *part_content;
567 printf (", \"content-type\": %s",
568 json_quote_str (ctx, g_mime_content_type_to_string (content_type)));
571 printf(", \"content-id\": %s", json_quote_str (ctx, cid));
574 strcmp (disposition->disposition, GMIME_DISPOSITION_ATTACHMENT) == 0)
576 const char *filename = g_mime_part_get_filename (GMIME_PART (part));
578 printf (", \"filename\": %s", json_quote_str (ctx, filename));
581 if (g_mime_content_type_is_type (content_type, "text", "*") &&
582 !g_mime_content_type_is_type (content_type, "text", "html"))
584 show_part_content (part, stream_memory);
585 part_content = g_mime_stream_mem_get_byte_array (GMIME_STREAM_MEM (stream_memory));
587 printf (", \"content\": %s", json_quote_chararray (ctx, (char *) part_content->data, part_content->len));
589 else if (g_mime_content_type_is_type (content_type, "multipart", "*") ||
590 g_mime_content_type_is_type (content_type, "message", "rfc822"))
592 printf (", \"content\": [");
597 g_object_unref (stream_memory);
601 format_part_end_json (GMimeObject *part)
603 GMimeContentType *content_type;
605 content_type = g_mime_object_get_content_type (GMIME_OBJECT (part));
607 if (g_mime_content_type_is_type (content_type, "multipart", "*") ||
608 g_mime_content_type_is_type (content_type, "message", "rfc822"))
615 format_part_content_raw (GMimeObject *part)
617 GMimeStream *stream_stdout = g_mime_stream_file_new (stdout);
618 g_mime_stream_file_set_owner (GMIME_STREAM_FILE (stream_stdout), FALSE);
619 show_part_content (part, stream_stdout);
620 g_object_unref(stream_stdout);
624 show_message (void *ctx,
625 const notmuch_show_format_t *format,
626 notmuch_message_t *message,
628 notmuch_show_params_t *params)
630 if (params->part <= 0) {
631 fputs (format->message_start, stdout);
633 format->message(ctx, message, indent);
635 fputs (format->header_start, stdout);
637 format->header(ctx, message);
638 fputs (format->header_end, stdout);
640 fputs (format->body_start, stdout);
643 if (format->part_content)
644 show_message_body (notmuch_message_get_filename (message),
647 if (params->part <= 0) {
648 fputs (format->body_end, stdout);
650 fputs (format->message_end, stdout);
655 show_messages (void *ctx,
656 const notmuch_show_format_t *format,
657 notmuch_messages_t *messages,
659 notmuch_show_params_t *params)
661 notmuch_message_t *message;
662 notmuch_bool_t match;
666 fputs (format->message_set_start, stdout);
669 notmuch_messages_valid (messages);
670 notmuch_messages_move_to_next (messages))
673 fputs (format->message_set_sep, stdout);
676 fputs (format->message_set_start, stdout);
678 message = notmuch_messages_get (messages);
680 match = notmuch_message_get_flag (message, NOTMUCH_MESSAGE_FLAG_MATCH);
682 next_indent = indent;
684 if (match || params->entire_thread) {
685 show_message (ctx, format, message, indent, params);
686 next_indent = indent + 1;
688 fputs (format->message_set_sep, stdout);
693 notmuch_message_get_replies (message),
697 notmuch_message_destroy (message);
699 fputs (format->message_set_end, stdout);
702 fputs (format->message_set_end, stdout);
705 /* Formatted output of single message */
707 do_show_single (void *ctx,
708 notmuch_query_t *query,
709 const notmuch_show_format_t *format,
710 notmuch_show_params_t *params)
712 notmuch_messages_t *messages;
713 notmuch_message_t *message;
715 if (notmuch_query_count_messages (query) != 1) {
716 fprintf (stderr, "Error: search term did not match precisely one message.\n");
720 messages = notmuch_query_search_messages (query);
721 message = notmuch_messages_get (messages);
723 if (message == NULL) {
724 fprintf (stderr, "Error: Cannot find matching message.\n");
728 notmuch_message_set_flag (message, NOTMUCH_MESSAGE_FLAG_MATCH, 1);
730 /* Special case for --format=raw of full single message, just cat out file */
731 if (params->raw && 0 == params->part) {
733 const char *filename;
738 filename = notmuch_message_get_filename (message);
739 if (filename == NULL) {
740 fprintf (stderr, "Error: Cannot message filename.\n");
744 file = fopen (filename, "r");
746 fprintf (stderr, "Error: Cannot open file %s: %s\n", filename, strerror (errno));
750 while (!feof (file)) {
751 size = fread (buf, 1, sizeof (buf), file);
752 fwrite (buf, size, 1, stdout);
759 show_message (ctx, format, message, 0, params);
766 /* Formatted output of threads */
769 notmuch_query_t *query,
770 const notmuch_show_format_t *format,
771 notmuch_show_params_t *params)
773 notmuch_threads_t *threads;
774 notmuch_thread_t *thread;
775 notmuch_messages_t *messages;
776 int first_toplevel = 1;
778 fputs (format->message_set_start, stdout);
780 for (threads = notmuch_query_search_threads (query);
781 notmuch_threads_valid (threads);
782 notmuch_threads_move_to_next (threads))
784 thread = notmuch_threads_get (threads);
786 messages = notmuch_thread_get_toplevel_messages (thread);
788 if (messages == NULL)
789 INTERNAL_ERROR ("Thread %s has no toplevel messages.\n",
790 notmuch_thread_get_thread_id (thread));
793 fputs (format->message_set_sep, stdout);
796 show_messages (ctx, format, messages, 0, params);
798 notmuch_thread_destroy (thread);
802 fputs (format->message_set_end, stdout);
808 notmuch_show_command (void *ctx, unused (int argc), unused (char *argv[]))
810 notmuch_config_t *config;
811 notmuch_database_t *notmuch;
812 notmuch_query_t *query;
815 const notmuch_show_format_t *format = &format_text;
816 notmuch_show_params_t params;
818 int format_specified = 0;
821 params.entire_thread = 0;
824 params.cryptoctx = NULL;
826 for (i = 0; i < argc && argv[i][0] == '-'; i++) {
827 if (strcmp (argv[i], "--") == 0) {
831 if (STRNCMP_LITERAL (argv[i], "--format=") == 0) {
832 opt = argv[i] + sizeof ("--format=") - 1;
833 if (strcmp (opt, "text") == 0) {
834 format = &format_text;
835 } else if (strcmp (opt, "json") == 0) {
836 format = &format_json;
837 params.entire_thread = 1;
838 } else if (strcmp (opt, "mbox") == 0) {
839 format = &format_mbox;
841 } else if (strcmp (opt, "raw") == 0) {
842 format = &format_raw;
845 fprintf (stderr, "Invalid value for --format: %s\n", opt);
848 format_specified = 1;
849 } else if (STRNCMP_LITERAL (argv[i], "--part=") == 0) {
850 params.part = atoi(argv[i] + sizeof ("--part=") - 1);
851 } else if (STRNCMP_LITERAL (argv[i], "--entire-thread") == 0) {
852 params.entire_thread = 1;
853 } else if (STRNCMP_LITERAL (argv[i], "--verify") == 0) {
854 if (params.cryptoctx == NULL) {
855 GMimeSession* session = g_object_new(notmuch_gmime_session_get_type(), NULL);
856 if (NULL == (params.cryptoctx = g_mime_gpg_context_new(session, "gpg")))
857 fprintf (stderr, "Failed to construct gpg context.\n");
859 g_mime_gpg_context_set_always_trust((GMimeGpgContext*)params.cryptoctx, FALSE);
860 g_object_unref (session);
864 fprintf (stderr, "Unrecognized option: %s\n", argv[i]);
872 config = notmuch_config_open (ctx, NULL, NULL);
876 query_string = query_string_from_args (ctx, argc, argv);
877 if (query_string == NULL) {
878 fprintf (stderr, "Out of memory\n");
882 if (mbox && params.part > 0) {
883 fprintf (stderr, "Error: specifying parts is incompatible with mbox output format.\n");
887 if (*query_string == '\0') {
888 fprintf (stderr, "Error: notmuch show requires at least one search term.\n");
892 notmuch = notmuch_database_open (notmuch_config_get_database_path (config),
893 NOTMUCH_DATABASE_MODE_READ_ONLY);
897 query = notmuch_query_create (notmuch, query_string);
899 fprintf (stderr, "Out of memory\n");
903 /* if part was requested and format was not specified, use format=raw */
904 if (params.part >= 0 && !format_specified)
905 format = &format_raw;
907 /* If --format=raw specified without specifying part, we can only
908 * output single message, so set part=0 */
909 if (params.raw && params.part < 0)
912 if (params.part >= 0)
913 return do_show_single (ctx, query, format, ¶ms);
915 return do_show (ctx, query, format, ¶ms);
917 notmuch_query_destroy (query);
918 notmuch_database_close (notmuch);
920 if (params.cryptoctx)
921 g_object_unref(params.cryptoctx);