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,
37 format_part_end_text (GMimeObject *part);
39 static const notmuch_show_format_t format_text = {
41 "\fmessage{ ", format_message_text,
42 "\fheader{\n", format_headers_text, "\fheader}\n",
43 "\fbody{\n", format_part_text, format_part_end_text, "\fbody}\n",
49 format_message_json (const void *ctx,
50 notmuch_message_t *message,
53 format_headers_json (const void *ctx,
54 notmuch_message_t *message);
57 format_part_json (GMimeObject *part,
62 format_part_end_json (GMimeObject *part);
64 static const notmuch_show_format_t format_json = {
66 "{", format_message_json,
67 ", \"headers\": {", format_headers_json, "}",
68 ", \"body\": [", format_part_json, format_part_end_json, "]",
74 format_message_mbox (const void *ctx,
75 notmuch_message_t *message,
78 static const notmuch_show_format_t format_mbox = {
80 "", format_message_mbox,
88 _get_tags_as_string (const void *ctx, notmuch_message_t *message)
95 result = talloc_strdup (ctx, "");
99 for (tags = notmuch_message_get_tags (message);
100 notmuch_tags_valid (tags);
101 notmuch_tags_move_to_next (tags))
103 tag = notmuch_tags_get (tags);
105 result = talloc_asprintf_append (result, "%s%s",
106 first ? "" : " ", tag);
113 /* Get a nice, single-line summary of message. */
115 _get_one_line_summary (const void *ctx, notmuch_message_t *message)
119 const char *relative_date;
122 from = notmuch_message_get_header (message, "from");
124 date = notmuch_message_get_date (message);
125 relative_date = notmuch_time_relative_date (ctx, date);
127 tags = _get_tags_as_string (ctx, message);
129 return talloc_asprintf (ctx, "%s (%s) (%s)",
130 from, relative_date, tags);
134 format_message_text (unused (const void *ctx), notmuch_message_t *message, int indent)
136 printf ("id:%s depth:%d match:%d filename:%s\n",
137 notmuch_message_get_message_id (message),
139 notmuch_message_get_flag (message, NOTMUCH_MESSAGE_FLAG_MATCH),
140 notmuch_message_get_filename (message));
144 format_message_json (const void *ctx, notmuch_message_t *message, unused (int indent))
146 notmuch_tags_t *tags;
148 void *ctx_quote = talloc_new (ctx);
150 const char *relative_date;
152 date = notmuch_message_get_date (message);
153 relative_date = notmuch_time_relative_date (ctx, date);
155 printf ("\"id\": %s, \"match\": %s, \"filename\": %s, \"timestamp\": %ld, \"date_relative\": \"%s\", \"tags\": [",
156 json_quote_str (ctx_quote, notmuch_message_get_message_id (message)),
157 notmuch_message_get_flag (message, NOTMUCH_MESSAGE_FLAG_MATCH) ? "true" : "false",
158 json_quote_str (ctx_quote, notmuch_message_get_filename (message)),
159 date, relative_date);
161 for (tags = notmuch_message_get_tags (message);
162 notmuch_tags_valid (tags);
163 notmuch_tags_move_to_next (tags))
165 printf("%s%s", first ? "" : ",",
166 json_quote_str (ctx_quote, notmuch_tags_get (tags)));
170 talloc_free (ctx_quote);
173 /* Extract just the email address from the contents of a From:
176 _extract_email_address (const void *ctx, const char *from)
178 InternetAddressList *addresses;
179 InternetAddress *address;
180 InternetAddressMailbox *mailbox;
181 const char *email = "MAILER-DAEMON";
183 addresses = internet_address_list_parse_string (from);
185 /* Bail if there is no address here. */
186 if (addresses == NULL || internet_address_list_length (addresses) < 1)
189 /* Otherwise, just use the first address. */
190 address = internet_address_list_get_address (addresses, 0);
192 /* The From header should never contain an address group rather
193 * than a mailbox. So bail if it does. */
194 if (! INTERNET_ADDRESS_IS_MAILBOX (address))
197 mailbox = INTERNET_ADDRESS_MAILBOX (address);
198 email = internet_address_mailbox_get_addr (mailbox);
199 email = talloc_strdup (ctx, email);
202 /* XXX: How to free addresses here? */
206 /* Return 1 if 'line' is an mbox From_ line---that is, a line
207 * beginning with zero or more '>' characters followed by the
208 * characters 'F', 'r', 'o', 'm', and space.
210 * Any characters at all may appear after that in the line.
213 _is_from_line (const char *line)
215 const char *s = line;
223 if (STRNCMP_LITERAL (s, "From ") == 0)
229 /* Print a message in "mboxrd" format as documented, for example,
232 * http://qmail.org/qmail-manual-html/man5/mbox.html
235 format_message_mbox (const void *ctx,
236 notmuch_message_t *message,
239 const char *filename;
244 struct tm date_gmtime;
245 char date_asctime[26];
251 filename = notmuch_message_get_filename (message);
252 file = fopen (filename, "r");
254 fprintf (stderr, "Failed to open %s: %s\n",
255 filename, strerror (errno));
259 from = notmuch_message_get_header (message, "from");
260 from = _extract_email_address (ctx, from);
262 date = notmuch_message_get_date (message);
263 gmtime_r (&date, &date_gmtime);
264 asctime_r (&date_gmtime, date_asctime);
266 printf ("From %s %s", from, date_asctime);
268 while ((line_len = getline (&line, &line_size, file)) != -1 ) {
269 if (_is_from_line (line))
280 format_headers_text (const void *ctx, notmuch_message_t *message)
282 const char *headers[] = {
283 "Subject", "From", "To", "Cc", "Bcc", "Date"
285 const char *name, *value;
288 printf ("%s\n", _get_one_line_summary (ctx, message));
290 for (i = 0; i < ARRAY_SIZE (headers); i++) {
292 value = notmuch_message_get_header (message, name);
293 if (value && strlen (value))
294 printf ("%s: %s\n", name, value);
299 format_headers_json (const void *ctx, notmuch_message_t *message)
301 const char *headers[] = {
302 "Subject", "From", "To", "Cc", "Bcc", "Date"
304 const char *name, *value;
306 int first_header = 1;
307 void *ctx_quote = talloc_new (ctx);
309 for (i = 0; i < ARRAY_SIZE (headers); i++) {
311 value = notmuch_message_get_header (message, name);
315 fputs (", ", stdout);
319 json_quote_str (ctx_quote, name),
320 json_quote_str (ctx_quote, value));
324 talloc_free (ctx_quote);
328 show_part_content (GMimeObject *part, GMimeStream *stream_out)
330 GMimeStream *stream_filter = NULL;
331 GMimeDataWrapper *wrapper;
334 charset = g_mime_object_get_content_type_parameter (part, "charset");
337 stream_filter = g_mime_stream_filter_new (stream_out);
338 g_mime_stream_filter_add(GMIME_STREAM_FILTER (stream_filter),
339 g_mime_filter_crlf_new (FALSE, FALSE));
341 GMimeFilter *charset_filter;
342 charset_filter = g_mime_filter_charset_new (charset, "UTF-8");
343 /* This result can be NULL for things like "unknown-8bit".
344 * Don't set a NULL filter as that makes GMime print
345 * annoying assertion-failure messages on stderr. */
347 g_mime_stream_filter_add (GMIME_STREAM_FILTER (stream_filter),
352 wrapper = g_mime_part_get_content_object (GMIME_PART (part));
353 if (wrapper && stream_filter)
354 g_mime_data_wrapper_write_to_stream (wrapper, stream_filter);
356 g_object_unref(stream_filter);
360 format_part_text (GMimeObject *part, int *part_count, unused (int first))
362 GMimeContentDisposition *disposition;
363 GMimeContentType *content_type;
365 disposition = g_mime_object_get_content_disposition (part);
367 strcmp (disposition->disposition, GMIME_DISPOSITION_ATTACHMENT) == 0)
369 const char *filename = g_mime_part_get_filename (GMIME_PART (part));
370 content_type = g_mime_object_get_content_type (GMIME_OBJECT (part));
372 printf ("\fattachment{ ID: %d, Content-type: %s\n",
374 g_mime_content_type_to_string (content_type));
375 printf ("Attachment: %s (%s)\n", filename,
376 g_mime_content_type_to_string (content_type));
378 if (g_mime_content_type_is_type (content_type, "text", "*") &&
379 !g_mime_content_type_is_type (content_type, "text", "html"))
381 GMimeStream *stream_stdout = g_mime_stream_file_new (stdout);
382 g_mime_stream_file_set_owner (GMIME_STREAM_FILE (stream_stdout), FALSE);
383 show_part_content (part, stream_stdout);
384 g_object_unref(stream_stdout);
390 content_type = g_mime_object_get_content_type (GMIME_OBJECT (part));
392 printf ("\fpart{ ID: %d, Content-type: %s\n",
394 g_mime_content_type_to_string (content_type));
396 if (g_mime_content_type_is_type (content_type, "text", "*") &&
397 !g_mime_content_type_is_type (content_type, "text", "html"))
399 GMimeStream *stream_stdout = g_mime_stream_file_new (stdout);
400 g_mime_stream_file_set_owner (GMIME_STREAM_FILE (stream_stdout), FALSE);
401 show_part_content (part, stream_stdout);
402 g_object_unref(stream_stdout);
404 else if (g_mime_content_type_is_type (content_type, "multipart", "*"))
406 /* Do nothing for multipart since its content will be printed
411 printf ("Non-text part: %s\n",
412 g_mime_content_type_to_string (content_type));
417 format_part_end_text (GMimeObject *part)
419 GMimeContentDisposition *disposition;
421 disposition = g_mime_object_get_content_disposition (part);
423 strcmp (disposition->disposition, GMIME_DISPOSITION_ATTACHMENT) == 0)
425 printf ("\fattachment}\n");
429 printf ("\fpart}\n");
434 format_part_json (GMimeObject *part, int *part_count, int first)
436 GMimeContentType *content_type;
437 GMimeContentDisposition *disposition;
438 void *ctx = talloc_new (NULL);
439 GMimeStream *stream_memory = g_mime_stream_mem_new ();
440 GByteArray *part_content;
443 content_type = g_mime_object_get_content_type (GMIME_OBJECT (part));
446 fputs (", ", stdout);
448 printf ("{\"id\": %d, \"content-type\": %s",
450 json_quote_str (ctx, g_mime_content_type_to_string (content_type)));
452 cid = g_mime_object_get_content_id (part);
454 printf(", \"content-id\": %s",
455 json_quote_str (ctx, cid));
457 disposition = g_mime_object_get_content_disposition (part);
459 strcmp (disposition->disposition, GMIME_DISPOSITION_ATTACHMENT) == 0)
461 const char *filename = g_mime_part_get_filename (GMIME_PART (part));
463 printf (", \"filename\": %s", json_quote_str (ctx, filename));
466 if (g_mime_content_type_is_type (content_type, "text", "*") &&
467 !g_mime_content_type_is_type (content_type, "text", "html"))
469 show_part_content (part, stream_memory);
470 part_content = g_mime_stream_mem_get_byte_array (GMIME_STREAM_MEM (stream_memory));
472 printf (", \"content\": %s", json_quote_chararray (ctx, (char *) part_content->data, part_content->len));
474 else if (g_mime_content_type_is_type (content_type, "multipart", "*"))
476 printf (", \"content\": [");
481 g_object_unref (stream_memory);
485 format_part_end_json (GMimeObject *part)
487 GMimeContentType *content_type;
489 content_type = g_mime_object_get_content_type (GMIME_OBJECT (part));
491 if (g_mime_content_type_is_type (content_type, "multipart", "*"))
498 show_message (void *ctx,
499 const notmuch_show_format_t *format,
500 notmuch_message_t *message,
503 fputs (format->message_start, stdout);
505 format->message(ctx, message, indent);
507 fputs (format->header_start, stdout);
509 format->header(ctx, message);
510 fputs (format->header_end, stdout);
512 fputs (format->body_start, stdout);
514 show_message_body (notmuch_message_get_filename (message),
516 fputs (format->body_end, stdout);
518 fputs (format->message_end, stdout);
523 show_messages (void *ctx,
524 const notmuch_show_format_t *format,
525 notmuch_messages_t *messages,
527 notmuch_bool_t entire_thread)
529 notmuch_message_t *message;
530 notmuch_bool_t match;
534 fputs (format->message_set_start, stdout);
537 notmuch_messages_valid (messages);
538 notmuch_messages_move_to_next (messages))
541 fputs (format->message_set_sep, stdout);
544 fputs (format->message_set_start, stdout);
546 message = notmuch_messages_get (messages);
548 match = notmuch_message_get_flag (message, NOTMUCH_MESSAGE_FLAG_MATCH);
550 next_indent = indent;
552 if (match || entire_thread) {
553 show_message (ctx, format, message, indent);
554 next_indent = indent + 1;
556 fputs (format->message_set_sep, stdout);
559 show_messages (ctx, format, notmuch_message_get_replies (message),
560 next_indent, entire_thread);
562 notmuch_message_destroy (message);
564 fputs (format->message_set_end, stdout);
567 fputs (format->message_set_end, stdout);
570 /* Support for --format=raw */
572 do_show_raw (unused(void *ctx), notmuch_query_t *query)
574 notmuch_messages_t *messages;
575 notmuch_message_t *message;
576 const char *filename;
581 if (notmuch_query_count_messages (query) != 1) {
582 fprintf (stderr, "Error: search term did not match precisely one message.\n");
586 messages = notmuch_query_search_messages (query);
587 message = notmuch_messages_get (messages);
589 if (message == NULL) {
590 fprintf (stderr, "Error: Cannot find matching message.\n");
594 filename = notmuch_message_get_filename (message);
595 if (filename == NULL) {
596 fprintf (stderr, "Error: Cannot message filename.\n");
600 file = fopen (filename, "r");
602 fprintf (stderr, "Error: Cannot open file %s: %s\n", filename, strerror (errno));
606 while (!feof (file)) {
607 size = fread (buf, 1, sizeof (buf), file);
608 fwrite (buf, size, 1, stdout);
616 /* Support for --format=text|json|mbox */
619 notmuch_query_t *query,
620 const notmuch_show_format_t *format,
623 notmuch_threads_t *threads;
624 notmuch_thread_t *thread;
625 notmuch_messages_t *messages;
626 int first_toplevel = 1;
628 fputs (format->message_set_start, stdout);
630 for (threads = notmuch_query_search_threads (query);
631 notmuch_threads_valid (threads);
632 notmuch_threads_move_to_next (threads))
634 thread = notmuch_threads_get (threads);
636 messages = notmuch_thread_get_toplevel_messages (thread);
638 if (messages == NULL)
639 INTERNAL_ERROR ("Thread %s has no toplevel messages.\n",
640 notmuch_thread_get_thread_id (thread));
643 fputs (format->message_set_sep, stdout);
646 show_messages (ctx, format, messages, 0, entire_thread);
648 notmuch_thread_destroy (thread);
652 fputs (format->message_set_end, stdout);
658 notmuch_show_command (void *ctx, unused (int argc), unused (char *argv[]))
660 notmuch_config_t *config;
661 notmuch_database_t *notmuch;
662 notmuch_query_t *query;
665 const notmuch_show_format_t *format = &format_text;
666 int 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;
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) {
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, entire_thread);
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);