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 /* Support for --format=raw */
570 do_show_raw (unused(void *ctx), notmuch_query_t *query)
572 notmuch_messages_t *messages;
573 notmuch_message_t *message;
574 const char *filename;
579 if (notmuch_query_count_messages (query) != 1) {
580 fprintf (stderr, "Error: search term did not match precisely one message.\n");
584 messages = notmuch_query_search_messages (query);
585 message = notmuch_messages_get (messages);
587 if (message == NULL) {
588 fprintf (stderr, "Error: Cannot find matching message.\n");
592 filename = notmuch_message_get_filename (message);
593 if (filename == NULL) {
594 fprintf (stderr, "Error: Cannot message filename.\n");
598 file = fopen (filename, "r");
600 fprintf (stderr, "Error: Cannot open file %s: %s\n", filename, strerror (errno));
604 while (!feof (file)) {
605 size = fread (buf, 1, sizeof (buf), file);
606 fwrite (buf, size, 1, stdout);
614 /* Support for --format=text|json|mbox */
617 notmuch_query_t *query,
618 const notmuch_show_format_t *format,
619 notmuch_show_params_t *params)
621 notmuch_threads_t *threads;
622 notmuch_thread_t *thread;
623 notmuch_messages_t *messages;
624 int first_toplevel = 1;
626 fputs (format->message_set_start, stdout);
628 for (threads = notmuch_query_search_threads (query);
629 notmuch_threads_valid (threads);
630 notmuch_threads_move_to_next (threads))
632 thread = notmuch_threads_get (threads);
634 messages = notmuch_thread_get_toplevel_messages (thread);
636 if (messages == NULL)
637 INTERNAL_ERROR ("Thread %s has no toplevel messages.\n",
638 notmuch_thread_get_thread_id (thread));
641 fputs (format->message_set_sep, stdout);
644 show_messages (ctx, format, messages, 0, params);
646 notmuch_thread_destroy (thread);
650 fputs (format->message_set_end, stdout);
656 notmuch_show_command (void *ctx, unused (int argc), unused (char *argv[]))
658 notmuch_config_t *config;
659 notmuch_database_t *notmuch;
660 notmuch_query_t *query;
663 const notmuch_show_format_t *format = &format_text;
664 notmuch_show_params_t params;
668 params.entire_thread = 0;
670 for (i = 0; i < argc && argv[i][0] == '-'; i++) {
671 if (strcmp (argv[i], "--") == 0) {
675 if (STRNCMP_LITERAL (argv[i], "--format=") == 0) {
676 opt = argv[i] + sizeof ("--format=") - 1;
677 if (strcmp (opt, "text") == 0) {
678 format = &format_text;
679 } else if (strcmp (opt, "json") == 0) {
680 format = &format_json;
681 params.entire_thread = 1;
682 } else if (strcmp (opt, "mbox") == 0) {
683 format = &format_mbox;
684 } else if (strcmp (opt, "raw") == 0) {
687 fprintf (stderr, "Invalid value for --format: %s\n", opt);
690 } else if (STRNCMP_LITERAL (argv[i], "--entire-thread") == 0) {
691 params.entire_thread = 1;
693 fprintf (stderr, "Unrecognized option: %s\n", argv[i]);
701 config = notmuch_config_open (ctx, NULL, NULL);
705 query_string = query_string_from_args (ctx, argc, argv);
706 if (query_string == NULL) {
707 fprintf (stderr, "Out of memory\n");
711 if (*query_string == '\0') {
712 fprintf (stderr, "Error: notmuch show requires at least one search term.\n");
716 notmuch = notmuch_database_open (notmuch_config_get_database_path (config),
717 NOTMUCH_DATABASE_MODE_READ_ONLY);
721 query = notmuch_query_create (notmuch, query_string);
723 fprintf (stderr, "Out of memory\n");
728 return do_show_raw (ctx, query);
730 return do_show (ctx, query, format, ¶ms);
732 notmuch_query_destroy (query);
733 notmuch_database_close (notmuch);
739 notmuch_part_command (void *ctx, unused (int argc), unused (char *argv[]))
741 notmuch_config_t *config;
742 notmuch_database_t *notmuch;
743 notmuch_query_t *query;
744 notmuch_messages_t *messages;
745 notmuch_message_t *message;
750 for (i = 0; i < argc && argv[i][0] == '-'; i++) {
751 if (strcmp (argv[i], "--") == 0) {
755 if (STRNCMP_LITERAL (argv[i], "--part=") == 0) {
756 part = atoi(argv[i] + sizeof ("--part=") - 1);
758 fprintf (stderr, "Unrecognized option: %s\n", argv[i]);
766 config = notmuch_config_open (ctx, NULL, NULL);
770 query_string = query_string_from_args (ctx, argc, argv);
771 if (query_string == NULL) {
772 fprintf (stderr, "Out of memory\n");
776 if (*query_string == '\0') {
777 fprintf (stderr, "Error: notmuch part requires at least one search term.\n");
781 notmuch = notmuch_database_open (notmuch_config_get_database_path (config),
782 NOTMUCH_DATABASE_MODE_READ_ONLY);
786 query = notmuch_query_create (notmuch, query_string);
788 fprintf (stderr, "Out of memory\n");
792 if (notmuch_query_count_messages (query) != 1) {
793 fprintf (stderr, "Error: search term did not match precisely one message.\n");
797 messages = notmuch_query_search_messages (query);
798 message = notmuch_messages_get (messages);
800 if (message == NULL) {
801 fprintf (stderr, "Error: cannot find matching message.\n");
805 show_one_part (notmuch_message_get_filename (message), part);
807 notmuch_query_destroy (query);
808 notmuch_database_close (notmuch);