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_text (GMimeObject *part,
36 format_part_end_text (GMimeObject *part);
38 static const notmuch_show_format_t format_text = {
40 "\fmessage{ ", format_message_text,
41 "\fheader{\n", format_headers_text, "\fheader}\n",
42 "\fbody{\n", format_part_text, format_part_end_text, "", "\fbody}\n",
48 format_message_json (const void *ctx,
49 notmuch_message_t *message,
52 format_headers_json (const void *ctx,
53 notmuch_message_t *message);
56 format_part_json (GMimeObject *part,
60 format_part_end_json (GMimeObject *part);
62 static const notmuch_show_format_t format_json = {
64 "{", format_message_json,
65 ", \"headers\": {", format_headers_json, "}",
66 ", \"body\": [", format_part_json, format_part_end_json, ", ", "]",
72 format_message_mbox (const void *ctx,
73 notmuch_message_t *message,
76 static const notmuch_show_format_t format_mbox = {
78 "", format_message_mbox,
80 "", NULL, NULL, "", "",
86 _get_tags_as_string (const void *ctx, notmuch_message_t *message)
93 result = talloc_strdup (ctx, "");
97 for (tags = notmuch_message_get_tags (message);
98 notmuch_tags_valid (tags);
99 notmuch_tags_move_to_next (tags))
101 tag = notmuch_tags_get (tags);
103 result = talloc_asprintf_append (result, "%s%s",
104 first ? "" : " ", tag);
111 /* Get a nice, single-line summary of message. */
113 _get_one_line_summary (const void *ctx, notmuch_message_t *message)
117 const char *relative_date;
120 from = notmuch_message_get_header (message, "from");
122 date = notmuch_message_get_date (message);
123 relative_date = notmuch_time_relative_date (ctx, date);
125 tags = _get_tags_as_string (ctx, message);
127 return talloc_asprintf (ctx, "%s (%s) (%s)",
128 from, relative_date, tags);
132 format_message_text (unused (const void *ctx), notmuch_message_t *message, int indent)
134 printf ("id:%s depth:%d match:%d filename:%s\n",
135 notmuch_message_get_message_id (message),
137 notmuch_message_get_flag (message, NOTMUCH_MESSAGE_FLAG_MATCH),
138 notmuch_message_get_filename (message));
142 format_message_json (const void *ctx, notmuch_message_t *message, unused (int indent))
144 notmuch_tags_t *tags;
146 void *ctx_quote = talloc_new (ctx);
148 const char *relative_date;
150 date = notmuch_message_get_date (message);
151 relative_date = notmuch_time_relative_date (ctx, date);
153 printf ("\"id\": %s, \"match\": %s, \"filename\": %s, \"timestamp\": %ld, \"date_relative\": \"%s\", \"tags\": [",
154 json_quote_str (ctx_quote, notmuch_message_get_message_id (message)),
155 notmuch_message_get_flag (message, NOTMUCH_MESSAGE_FLAG_MATCH) ? "true" : "false",
156 json_quote_str (ctx_quote, notmuch_message_get_filename (message)),
157 date, relative_date);
159 for (tags = notmuch_message_get_tags (message);
160 notmuch_tags_valid (tags);
161 notmuch_tags_move_to_next (tags))
163 printf("%s%s", first ? "" : ",",
164 json_quote_str (ctx_quote, notmuch_tags_get (tags)));
168 talloc_free (ctx_quote);
171 /* Extract just the email address from the contents of a From:
174 _extract_email_address (const void *ctx, const char *from)
176 InternetAddressList *addresses;
177 InternetAddress *address;
178 InternetAddressMailbox *mailbox;
179 const char *email = "MAILER-DAEMON";
181 addresses = internet_address_list_parse_string (from);
183 /* Bail if there is no address here. */
184 if (addresses == NULL || internet_address_list_length (addresses) < 1)
187 /* Otherwise, just use the first address. */
188 address = internet_address_list_get_address (addresses, 0);
190 /* The From header should never contain an address group rather
191 * than a mailbox. So bail if it does. */
192 if (! INTERNET_ADDRESS_IS_MAILBOX (address))
195 mailbox = INTERNET_ADDRESS_MAILBOX (address);
196 email = internet_address_mailbox_get_addr (mailbox);
197 email = talloc_strdup (ctx, email);
200 /* XXX: How to free addresses here? */
204 /* Return 1 if 'line' is an mbox From_ line---that is, a line
205 * beginning with zero or more '>' characters followed by the
206 * characters 'F', 'r', 'o', 'm', and space.
208 * Any characters at all may appear after that in the line.
211 _is_from_line (const char *line)
213 const char *s = line;
221 if (STRNCMP_LITERAL (s, "From ") == 0)
227 /* Print a message in "mboxrd" format as documented, for example,
230 * http://qmail.org/qmail-manual-html/man5/mbox.html
233 format_message_mbox (const void *ctx,
234 notmuch_message_t *message,
237 const char *filename;
242 struct tm date_gmtime;
243 char date_asctime[26];
249 filename = notmuch_message_get_filename (message);
250 file = fopen (filename, "r");
252 fprintf (stderr, "Failed to open %s: %s\n",
253 filename, strerror (errno));
257 from = notmuch_message_get_header (message, "from");
258 from = _extract_email_address (ctx, from);
260 date = notmuch_message_get_date (message);
261 gmtime_r (&date, &date_gmtime);
262 asctime_r (&date_gmtime, date_asctime);
264 printf ("From %s %s", from, date_asctime);
266 while ((line_len = getline (&line, &line_size, file)) != -1 ) {
267 if (_is_from_line (line))
278 format_headers_text (const void *ctx, notmuch_message_t *message)
280 const char *headers[] = {
281 "Subject", "From", "To", "Cc", "Bcc", "Date"
283 const char *name, *value;
286 printf ("%s\n", _get_one_line_summary (ctx, message));
288 for (i = 0; i < ARRAY_SIZE (headers); i++) {
290 value = notmuch_message_get_header (message, name);
291 if (value && strlen (value))
292 printf ("%s: %s\n", name, value);
297 format_headers_json (const void *ctx, notmuch_message_t *message)
299 const char *headers[] = {
300 "Subject", "From", "To", "Cc", "Bcc", "Date"
302 const char *name, *value;
304 int first_header = 1;
305 void *ctx_quote = talloc_new (ctx);
307 for (i = 0; i < ARRAY_SIZE (headers); i++) {
309 value = notmuch_message_get_header (message, name);
313 fputs (", ", stdout);
317 json_quote_str (ctx_quote, name),
318 json_quote_str (ctx_quote, value));
322 talloc_free (ctx_quote);
326 show_part_content (GMimeObject *part, GMimeStream *stream_out)
328 GMimeStream *stream_filter = NULL;
329 GMimeDataWrapper *wrapper;
332 charset = g_mime_object_get_content_type_parameter (part, "charset");
335 stream_filter = g_mime_stream_filter_new (stream_out);
336 g_mime_stream_filter_add(GMIME_STREAM_FILTER (stream_filter),
337 g_mime_filter_crlf_new (FALSE, FALSE));
339 GMimeFilter *charset_filter;
340 charset_filter = g_mime_filter_charset_new (charset, "UTF-8");
341 /* This result can be NULL for things like "unknown-8bit".
342 * Don't set a NULL filter as that makes GMime print
343 * annoying assertion-failure messages on stderr. */
345 g_mime_stream_filter_add (GMIME_STREAM_FILTER (stream_filter),
350 wrapper = g_mime_part_get_content_object (GMIME_PART (part));
351 if (wrapper && stream_filter)
352 g_mime_data_wrapper_write_to_stream (wrapper, stream_filter);
354 g_object_unref(stream_filter);
358 format_part_text (GMimeObject *part, int *part_count)
360 GMimeContentDisposition *disposition;
361 GMimeContentType *content_type;
363 disposition = g_mime_object_get_content_disposition (part);
365 strcmp (disposition->disposition, GMIME_DISPOSITION_ATTACHMENT) == 0)
367 const char *filename = g_mime_part_get_filename (GMIME_PART (part));
368 content_type = g_mime_object_get_content_type (GMIME_OBJECT (part));
370 printf ("\fattachment{ ID: %d, Content-type: %s\n",
372 g_mime_content_type_to_string (content_type));
373 printf ("Attachment: %s (%s)\n", filename,
374 g_mime_content_type_to_string (content_type));
376 if (g_mime_content_type_is_type (content_type, "text", "*") &&
377 !g_mime_content_type_is_type (content_type, "text", "html"))
379 GMimeStream *stream_stdout = g_mime_stream_file_new (stdout);
380 g_mime_stream_file_set_owner (GMIME_STREAM_FILE (stream_stdout), FALSE);
381 show_part_content (part, stream_stdout);
382 g_object_unref(stream_stdout);
388 content_type = g_mime_object_get_content_type (GMIME_OBJECT (part));
390 printf ("\fpart{ ID: %d, Content-type: %s\n",
392 g_mime_content_type_to_string (content_type));
394 if (g_mime_content_type_is_type (content_type, "text", "*") &&
395 !g_mime_content_type_is_type (content_type, "text", "html"))
397 GMimeStream *stream_stdout = g_mime_stream_file_new (stdout);
398 g_mime_stream_file_set_owner (GMIME_STREAM_FILE (stream_stdout), FALSE);
399 show_part_content (part, stream_stdout);
400 g_object_unref(stream_stdout);
402 else if (g_mime_content_type_is_type (content_type, "multipart", "*"))
404 /* Do nothing for multipart since its content will be printed
409 printf ("Non-text part: %s\n",
410 g_mime_content_type_to_string (content_type));
415 format_part_end_text (GMimeObject *part)
417 GMimeContentDisposition *disposition;
419 disposition = g_mime_object_get_content_disposition (part);
421 strcmp (disposition->disposition, GMIME_DISPOSITION_ATTACHMENT) == 0)
423 printf ("\fattachment}\n");
427 printf ("\fpart}\n");
432 format_part_json (GMimeObject *part, int *part_count)
434 GMimeContentType *content_type;
435 GMimeContentDisposition *disposition;
436 void *ctx = talloc_new (NULL);
437 GMimeStream *stream_memory = g_mime_stream_mem_new ();
438 GByteArray *part_content;
441 content_type = g_mime_object_get_content_type (GMIME_OBJECT (part));
443 printf ("{\"id\": %d, \"content-type\": %s",
445 json_quote_str (ctx, g_mime_content_type_to_string (content_type)));
447 cid = g_mime_object_get_content_id (part);
449 printf(", \"content-id\": %s",
450 json_quote_str (ctx, cid));
452 disposition = g_mime_object_get_content_disposition (part);
454 strcmp (disposition->disposition, GMIME_DISPOSITION_ATTACHMENT) == 0)
456 const char *filename = g_mime_part_get_filename (GMIME_PART (part));
458 printf (", \"filename\": %s", json_quote_str (ctx, filename));
461 if (g_mime_content_type_is_type (content_type, "text", "*") &&
462 !g_mime_content_type_is_type (content_type, "text", "html"))
464 show_part_content (part, stream_memory);
465 part_content = g_mime_stream_mem_get_byte_array (GMIME_STREAM_MEM (stream_memory));
467 printf (", \"content\": %s", json_quote_chararray (ctx, (char *) part_content->data, part_content->len));
469 else if (g_mime_content_type_is_type (content_type, "multipart", "*"))
471 printf (", \"content\": [");
476 g_object_unref (stream_memory);
480 format_part_end_json (GMimeObject *part)
482 GMimeContentType *content_type;
484 content_type = g_mime_object_get_content_type (GMIME_OBJECT (part));
486 if (g_mime_content_type_is_type (content_type, "multipart", "*"))
493 show_message (void *ctx,
494 const notmuch_show_format_t *format,
495 notmuch_message_t *message,
498 fputs (format->message_start, stdout);
500 format->message(ctx, message, indent);
502 fputs (format->header_start, stdout);
504 format->header(ctx, message);
505 fputs (format->header_end, stdout);
507 fputs (format->body_start, stdout);
509 show_message_body (notmuch_message_get_filename (message),
511 fputs (format->body_end, stdout);
513 fputs (format->message_end, stdout);
518 show_messages (void *ctx,
519 const notmuch_show_format_t *format,
520 notmuch_messages_t *messages,
522 notmuch_show_params_t *params)
524 notmuch_message_t *message;
525 notmuch_bool_t match;
529 fputs (format->message_set_start, stdout);
532 notmuch_messages_valid (messages);
533 notmuch_messages_move_to_next (messages))
536 fputs (format->message_set_sep, stdout);
539 fputs (format->message_set_start, stdout);
541 message = notmuch_messages_get (messages);
543 match = notmuch_message_get_flag (message, NOTMUCH_MESSAGE_FLAG_MATCH);
545 next_indent = indent;
547 if (match || params->entire_thread) {
548 show_message (ctx, format, message, indent);
549 next_indent = indent + 1;
551 fputs (format->message_set_sep, stdout);
556 notmuch_message_get_replies (message),
560 notmuch_message_destroy (message);
562 fputs (format->message_set_end, stdout);
565 fputs (format->message_set_end, stdout);
568 /* Formatted output of single message */
570 do_show_single (void *ctx,
571 notmuch_query_t *query,
572 const notmuch_show_format_t *format,
573 notmuch_show_params_t *params)
575 notmuch_messages_t *messages;
576 notmuch_message_t *message;
578 if (notmuch_query_count_messages (query) != 1) {
579 fprintf (stderr, "Error: search term did not match precisely one message.\n");
583 messages = notmuch_query_search_messages (query);
584 message = notmuch_messages_get (messages);
586 if (message == NULL) {
587 fprintf (stderr, "Error: Cannot find matching message.\n");
591 /* Special case for --format=raw of full single message, just cat out file */
594 const char *filename;
599 filename = notmuch_message_get_filename (message);
600 if (filename == NULL) {
601 fprintf (stderr, "Error: Cannot message filename.\n");
605 file = fopen (filename, "r");
607 fprintf (stderr, "Error: Cannot open file %s: %s\n", filename, strerror (errno));
611 while (!feof (file)) {
612 size = fread (buf, 1, sizeof (buf), file);
613 fwrite (buf, size, 1, stdout);
623 /* Formatted output of threads */
626 notmuch_query_t *query,
627 const notmuch_show_format_t *format,
628 notmuch_show_params_t *params)
630 notmuch_threads_t *threads;
631 notmuch_thread_t *thread;
632 notmuch_messages_t *messages;
633 int first_toplevel = 1;
635 fputs (format->message_set_start, stdout);
637 for (threads = notmuch_query_search_threads (query);
638 notmuch_threads_valid (threads);
639 notmuch_threads_move_to_next (threads))
641 thread = notmuch_threads_get (threads);
643 messages = notmuch_thread_get_toplevel_messages (thread);
645 if (messages == NULL)
646 INTERNAL_ERROR ("Thread %s has no toplevel messages.\n",
647 notmuch_thread_get_thread_id (thread));
650 fputs (format->message_set_sep, stdout);
653 show_messages (ctx, format, messages, 0, params);
655 notmuch_thread_destroy (thread);
659 fputs (format->message_set_end, stdout);
665 notmuch_show_command (void *ctx, unused (int argc), unused (char *argv[]))
667 notmuch_config_t *config;
668 notmuch_database_t *notmuch;
669 notmuch_query_t *query;
672 const notmuch_show_format_t *format = &format_text;
673 notmuch_show_params_t params;
676 params.entire_thread = 0;
679 for (i = 0; i < argc && argv[i][0] == '-'; i++) {
680 if (strcmp (argv[i], "--") == 0) {
684 if (STRNCMP_LITERAL (argv[i], "--format=") == 0) {
685 opt = argv[i] + sizeof ("--format=") - 1;
686 if (strcmp (opt, "text") == 0) {
687 format = &format_text;
688 } else if (strcmp (opt, "json") == 0) {
689 format = &format_json;
690 params.entire_thread = 1;
691 } else if (strcmp (opt, "mbox") == 0) {
692 format = &format_mbox;
693 } else if (strcmp (opt, "raw") == 0) {
696 fprintf (stderr, "Invalid value for --format: %s\n", opt);
699 } else if (STRNCMP_LITERAL (argv[i], "--entire-thread") == 0) {
700 params.entire_thread = 1;
702 fprintf (stderr, "Unrecognized option: %s\n", argv[i]);
710 config = notmuch_config_open (ctx, NULL, NULL);
714 query_string = query_string_from_args (ctx, argc, argv);
715 if (query_string == NULL) {
716 fprintf (stderr, "Out of memory\n");
720 if (*query_string == '\0') {
721 fprintf (stderr, "Error: notmuch show requires at least one search term.\n");
725 notmuch = notmuch_database_open (notmuch_config_get_database_path (config),
726 NOTMUCH_DATABASE_MODE_READ_ONLY);
730 query = notmuch_query_create (notmuch, query_string);
732 fprintf (stderr, "Out of memory\n");
736 /* If --format=raw specified without specifying part, we can only
737 * output single message, so set part=0 */
739 return do_show_single (ctx, query, format, ¶ms);
741 return do_show (ctx, query, format, ¶ms);
743 notmuch_query_destroy (query);
744 notmuch_database_close (notmuch);
750 notmuch_part_command (void *ctx, unused (int argc), unused (char *argv[]))
752 notmuch_config_t *config;
753 notmuch_database_t *notmuch;
754 notmuch_query_t *query;
755 notmuch_messages_t *messages;
756 notmuch_message_t *message;
761 for (i = 0; i < argc && argv[i][0] == '-'; i++) {
762 if (strcmp (argv[i], "--") == 0) {
766 if (STRNCMP_LITERAL (argv[i], "--part=") == 0) {
767 part = atoi(argv[i] + sizeof ("--part=") - 1);
769 fprintf (stderr, "Unrecognized option: %s\n", argv[i]);
777 config = notmuch_config_open (ctx, NULL, NULL);
781 query_string = query_string_from_args (ctx, argc, argv);
782 if (query_string == NULL) {
783 fprintf (stderr, "Out of memory\n");
787 if (*query_string == '\0') {
788 fprintf (stderr, "Error: notmuch part requires at least one search term.\n");
792 notmuch = notmuch_database_open (notmuch_config_get_database_path (config),
793 NOTMUCH_DATABASE_MODE_READ_ONLY);
797 query = notmuch_query_create (notmuch, query_string);
799 fprintf (stderr, "Out of memory\n");
803 if (notmuch_query_count_messages (query) != 1) {
804 fprintf (stderr, "Error: search term did not match precisely one message.\n");
808 messages = notmuch_query_search_messages (query);
809 message = notmuch_messages_get (messages);
811 if (message == NULL) {
812 fprintf (stderr, "Error: cannot find matching message.\n");
816 show_one_part (notmuch_message_get_filename (message), part);
818 notmuch_query_destroy (query);
819 notmuch_database_close (notmuch);