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"
23 typedef struct show_format {
24 const char *message_set_start;
25 const char *message_start;
26 void (*message) (const void *ctx,
27 notmuch_message_t *message,
29 const char *header_start;
30 void (*header) (const void *ctx,
31 notmuch_message_t *message);
32 const char *header_end;
33 const char *body_start;
34 void (*part) (GMimeObject *part,
35 int *part_count, int first);
36 void (*part_end) (GMimeObject *part);
38 const char *message_end;
39 const char *message_set_sep;
40 const char *message_set_end;
44 format_message_text (unused (const void *ctx),
45 notmuch_message_t *message,
48 format_headers_text (const void *ctx,
49 notmuch_message_t *message);
52 format_part_text (GMimeObject *part,
57 format_part_end_text (GMimeObject *part);
59 static const show_format_t format_text = {
61 "\fmessage{ ", format_message_text,
62 "\fheader{\n", format_headers_text, "\fheader}\n",
63 "\fbody{\n", format_part_text, format_part_end_text, "\fbody}\n",
69 format_message_json (const void *ctx,
70 notmuch_message_t *message,
73 format_headers_json (const void *ctx,
74 notmuch_message_t *message);
77 format_part_json (GMimeObject *part,
82 format_part_end_json (GMimeObject *part);
84 static const show_format_t format_json = {
86 "{", format_message_json,
87 ", \"headers\": {", format_headers_json, "}",
88 ", \"body\": [", format_part_json, format_part_end_json, "]",
94 format_message_mbox (const void *ctx,
95 notmuch_message_t *message,
98 static const show_format_t format_mbox = {
100 "", format_message_mbox,
108 _get_tags_as_string (const void *ctx, notmuch_message_t *message)
110 notmuch_tags_t *tags;
115 result = talloc_strdup (ctx, "");
119 for (tags = notmuch_message_get_tags (message);
120 notmuch_tags_valid (tags);
121 notmuch_tags_move_to_next (tags))
123 tag = notmuch_tags_get (tags);
125 result = talloc_asprintf_append (result, "%s%s",
126 first ? "" : " ", tag);
133 /* Get a nice, single-line summary of message. */
135 _get_one_line_summary (const void *ctx, notmuch_message_t *message)
139 const char *relative_date;
142 from = notmuch_message_get_header (message, "from");
144 date = notmuch_message_get_date (message);
145 relative_date = notmuch_time_relative_date (ctx, date);
147 tags = _get_tags_as_string (ctx, message);
149 return talloc_asprintf (ctx, "%s (%s) (%s)",
150 from, relative_date, tags);
154 format_message_text (unused (const void *ctx), notmuch_message_t *message, int indent)
156 printf ("id:%s depth:%d match:%d filename:%s\n",
157 notmuch_message_get_message_id (message),
159 notmuch_message_get_flag (message, NOTMUCH_MESSAGE_FLAG_MATCH),
160 notmuch_message_get_filename (message));
164 format_message_json (const void *ctx, notmuch_message_t *message, unused (int indent))
166 notmuch_tags_t *tags;
168 void *ctx_quote = talloc_new (ctx);
170 const char *relative_date;
172 date = notmuch_message_get_date (message);
173 relative_date = notmuch_time_relative_date (ctx, date);
175 printf ("\"id\": %s, \"match\": %s, \"filename\": %s, \"timestamp\": %ld, \"date_relative\": \"%s\", \"tags\": [",
176 json_quote_str (ctx_quote, notmuch_message_get_message_id (message)),
177 notmuch_message_get_flag (message, NOTMUCH_MESSAGE_FLAG_MATCH) ? "true" : "false",
178 json_quote_str (ctx_quote, notmuch_message_get_filename (message)),
179 date, relative_date);
181 for (tags = notmuch_message_get_tags (message);
182 notmuch_tags_valid (tags);
183 notmuch_tags_move_to_next (tags))
185 printf("%s%s", first ? "" : ",",
186 json_quote_str (ctx_quote, notmuch_tags_get (tags)));
190 talloc_free (ctx_quote);
193 /* Extract just the email address from the contents of a From:
196 _extract_email_address (const void *ctx, const char *from)
198 InternetAddressList *addresses;
199 InternetAddress *address;
200 InternetAddressMailbox *mailbox;
201 const char *email = "MAILER-DAEMON";
203 addresses = internet_address_list_parse_string (from);
205 /* Bail if there is no address here. */
206 if (addresses == NULL || internet_address_list_length (addresses) < 1)
209 /* Otherwise, just use the first address. */
210 address = internet_address_list_get_address (addresses, 0);
212 /* The From header should never contain an address group rather
213 * than a mailbox. So bail if it does. */
214 if (! INTERNET_ADDRESS_IS_MAILBOX (address))
217 mailbox = INTERNET_ADDRESS_MAILBOX (address);
218 email = internet_address_mailbox_get_addr (mailbox);
219 email = talloc_strdup (ctx, email);
222 /* XXX: How to free addresses here? */
226 /* Return 1 if 'line' is an mbox From_ line---that is, a line
227 * beginning with zero or more '>' characters followed by the
228 * characters 'F', 'r', 'o', 'm', and space.
230 * Any characters at all may appear after that in the line.
233 _is_from_line (const char *line)
235 const char *s = line;
243 if (STRNCMP_LITERAL (s, "From ") == 0)
249 /* Print a message in "mboxrd" format as documented, for example,
252 * http://qmail.org/qmail-manual-html/man5/mbox.html
255 format_message_mbox (const void *ctx,
256 notmuch_message_t *message,
259 const char *filename;
264 struct tm date_gmtime;
265 char date_asctime[26];
271 filename = notmuch_message_get_filename (message);
272 file = fopen (filename, "r");
274 fprintf (stderr, "Failed to open %s: %s\n",
275 filename, strerror (errno));
279 from = notmuch_message_get_header (message, "from");
280 from = _extract_email_address (ctx, from);
282 date = notmuch_message_get_date (message);
283 gmtime_r (&date, &date_gmtime);
284 asctime_r (&date_gmtime, date_asctime);
286 printf ("From %s %s", from, date_asctime);
288 while ((line_len = getline (&line, &line_size, file)) != -1 ) {
289 if (_is_from_line (line))
300 format_headers_text (const void *ctx, notmuch_message_t *message)
302 const char *headers[] = {
303 "Subject", "From", "To", "Cc", "Bcc", "Date"
305 const char *name, *value;
308 printf ("%s\n", _get_one_line_summary (ctx, message));
310 for (i = 0; i < ARRAY_SIZE (headers); i++) {
312 value = notmuch_message_get_header (message, name);
313 if (value && strlen (value))
314 printf ("%s: %s\n", name, value);
319 format_headers_json (const void *ctx, notmuch_message_t *message)
321 const char *headers[] = {
322 "Subject", "From", "To", "Cc", "Bcc", "Date"
324 const char *name, *value;
326 int first_header = 1;
327 void *ctx_quote = talloc_new (ctx);
329 for (i = 0; i < ARRAY_SIZE (headers); i++) {
331 value = notmuch_message_get_header (message, name);
335 fputs (", ", stdout);
339 json_quote_str (ctx_quote, name),
340 json_quote_str (ctx_quote, value));
344 talloc_free (ctx_quote);
348 show_part_content (GMimeObject *part, GMimeStream *stream_out)
350 GMimeStream *stream_filter = NULL;
351 GMimeDataWrapper *wrapper;
354 charset = g_mime_object_get_content_type_parameter (part, "charset");
357 stream_filter = g_mime_stream_filter_new (stream_out);
358 g_mime_stream_filter_add(GMIME_STREAM_FILTER (stream_filter),
359 g_mime_filter_crlf_new (FALSE, FALSE));
361 GMimeFilter *charset_filter;
362 charset_filter = g_mime_filter_charset_new (charset, "UTF-8");
363 /* This result can be NULL for things like "unknown-8bit".
364 * Don't set a NULL filter as that makes GMime print
365 * annoying assertion-failure messages on stderr. */
367 g_mime_stream_filter_add (GMIME_STREAM_FILTER (stream_filter),
372 wrapper = g_mime_part_get_content_object (GMIME_PART (part));
373 if (wrapper && stream_filter)
374 g_mime_data_wrapper_write_to_stream (wrapper, stream_filter);
376 g_object_unref(stream_filter);
380 format_part_text (GMimeObject *part, int *part_count, unused (int first))
382 GMimeContentDisposition *disposition;
383 GMimeContentType *content_type;
385 disposition = g_mime_object_get_content_disposition (part);
387 strcmp (disposition->disposition, GMIME_DISPOSITION_ATTACHMENT) == 0)
389 const char *filename = g_mime_part_get_filename (GMIME_PART (part));
390 content_type = g_mime_object_get_content_type (GMIME_OBJECT (part));
392 printf ("\fattachment{ ID: %d, Content-type: %s\n",
394 g_mime_content_type_to_string (content_type));
395 printf ("Attachment: %s (%s)\n", filename,
396 g_mime_content_type_to_string (content_type));
398 if (g_mime_content_type_is_type (content_type, "text", "*") &&
399 !g_mime_content_type_is_type (content_type, "text", "html"))
401 GMimeStream *stream_stdout = g_mime_stream_file_new (stdout);
402 g_mime_stream_file_set_owner (GMIME_STREAM_FILE (stream_stdout), FALSE);
403 show_part_content (part, stream_stdout);
404 g_object_unref(stream_stdout);
410 content_type = g_mime_object_get_content_type (GMIME_OBJECT (part));
412 printf ("\fpart{ ID: %d, Content-type: %s\n",
414 g_mime_content_type_to_string (content_type));
416 if (g_mime_content_type_is_type (content_type, "text", "*") &&
417 !g_mime_content_type_is_type (content_type, "text", "html"))
419 GMimeStream *stream_stdout = g_mime_stream_file_new (stdout);
420 g_mime_stream_file_set_owner (GMIME_STREAM_FILE (stream_stdout), FALSE);
421 show_part_content (part, stream_stdout);
422 g_object_unref(stream_stdout);
424 else if (g_mime_content_type_is_type (content_type, "multipart", "*"))
426 /* Do nothing for multipart since its content will be printed
431 printf ("Non-text part: %s\n",
432 g_mime_content_type_to_string (content_type));
437 format_part_end_text (GMimeObject *part)
439 GMimeContentDisposition *disposition;
441 disposition = g_mime_object_get_content_disposition (part);
443 strcmp (disposition->disposition, GMIME_DISPOSITION_ATTACHMENT) == 0)
445 printf ("\fattachment}\n");
449 printf ("\fpart}\n");
454 format_part_json (GMimeObject *part, int *part_count, int first)
456 GMimeContentType *content_type;
457 GMimeContentDisposition *disposition;
458 void *ctx = talloc_new (NULL);
459 GMimeStream *stream_memory = g_mime_stream_mem_new ();
460 GByteArray *part_content;
463 content_type = g_mime_object_get_content_type (GMIME_OBJECT (part));
466 fputs (", ", stdout);
468 printf ("{\"id\": %d, \"content-type\": %s",
470 json_quote_str (ctx, g_mime_content_type_to_string (content_type)));
472 cid = g_mime_object_get_content_id (part);
474 printf(", \"content-id\": %s",
475 json_quote_str (ctx, cid));
477 disposition = g_mime_object_get_content_disposition (part);
479 strcmp (disposition->disposition, GMIME_DISPOSITION_ATTACHMENT) == 0)
481 const char *filename = g_mime_part_get_filename (GMIME_PART (part));
483 printf (", \"filename\": %s", json_quote_str (ctx, filename));
486 if (g_mime_content_type_is_type (content_type, "text", "*") &&
487 !g_mime_content_type_is_type (content_type, "text", "html"))
489 show_part_content (part, stream_memory);
490 part_content = g_mime_stream_mem_get_byte_array (GMIME_STREAM_MEM (stream_memory));
492 printf (", \"content\": %s", json_quote_chararray (ctx, (char *) part_content->data, part_content->len));
494 else if (g_mime_content_type_is_type (content_type, "multipart", "*"))
496 printf (", \"content\": [");
501 g_object_unref (stream_memory);
505 format_part_end_json (GMimeObject *part)
507 GMimeContentType *content_type;
509 content_type = g_mime_object_get_content_type (GMIME_OBJECT (part));
511 if (g_mime_content_type_is_type (content_type, "multipart", "*"))
518 show_message (void *ctx, const show_format_t *format, notmuch_message_t *message, int indent)
520 fputs (format->message_start, stdout);
522 format->message(ctx, message, indent);
524 fputs (format->header_start, stdout);
526 format->header(ctx, message);
527 fputs (format->header_end, stdout);
529 fputs (format->body_start, stdout);
531 show_message_body (notmuch_message_get_filename (message),
532 format->part, format->part_end);
533 fputs (format->body_end, stdout);
535 fputs (format->message_end, stdout);
540 show_messages (void *ctx, const show_format_t *format, notmuch_messages_t *messages, int indent,
541 notmuch_bool_t entire_thread)
543 notmuch_message_t *message;
544 notmuch_bool_t match;
548 fputs (format->message_set_start, stdout);
551 notmuch_messages_valid (messages);
552 notmuch_messages_move_to_next (messages))
555 fputs (format->message_set_sep, stdout);
558 fputs (format->message_set_start, stdout);
560 message = notmuch_messages_get (messages);
562 match = notmuch_message_get_flag (message, NOTMUCH_MESSAGE_FLAG_MATCH);
564 next_indent = indent;
566 if (match || entire_thread) {
567 show_message (ctx, format, message, indent);
568 next_indent = indent + 1;
570 fputs (format->message_set_sep, stdout);
573 show_messages (ctx, format, notmuch_message_get_replies (message),
574 next_indent, entire_thread);
576 notmuch_message_destroy (message);
578 fputs (format->message_set_end, stdout);
581 fputs (format->message_set_end, stdout);
584 /* Support for --format=raw */
586 do_show_raw (unused(void *ctx), notmuch_query_t *query)
588 notmuch_messages_t *messages;
589 notmuch_message_t *message;
590 const char *filename;
595 if (notmuch_query_count_messages (query) != 1) {
596 fprintf (stderr, "Error: search term did not match precisely one message.\n");
600 messages = notmuch_query_search_messages (query);
601 message = notmuch_messages_get (messages);
603 if (message == NULL) {
604 fprintf (stderr, "Error: Cannot find matching message.\n");
608 filename = notmuch_message_get_filename (message);
609 if (filename == NULL) {
610 fprintf (stderr, "Error: Cannot message filename.\n");
614 file = fopen (filename, "r");
616 fprintf (stderr, "Error: Cannot open file %s: %s\n", filename, strerror (errno));
620 while (!feof (file)) {
621 size = fread (buf, 1, sizeof (buf), file);
622 fwrite (buf, size, 1, stdout);
630 /* Support for --format=text|json|mbox */
633 notmuch_query_t *query,
634 const show_format_t *format,
637 notmuch_threads_t *threads;
638 notmuch_thread_t *thread;
639 notmuch_messages_t *messages;
640 int first_toplevel = 1;
642 fputs (format->message_set_start, stdout);
644 for (threads = notmuch_query_search_threads (query);
645 notmuch_threads_valid (threads);
646 notmuch_threads_move_to_next (threads))
648 thread = notmuch_threads_get (threads);
650 messages = notmuch_thread_get_toplevel_messages (thread);
652 if (messages == NULL)
653 INTERNAL_ERROR ("Thread %s has no toplevel messages.\n",
654 notmuch_thread_get_thread_id (thread));
657 fputs (format->message_set_sep, stdout);
660 show_messages (ctx, format, messages, 0, entire_thread);
662 notmuch_thread_destroy (thread);
666 fputs (format->message_set_end, stdout);
672 notmuch_show_command (void *ctx, unused (int argc), unused (char *argv[]))
674 notmuch_config_t *config;
675 notmuch_database_t *notmuch;
676 notmuch_query_t *query;
679 const show_format_t *format = &format_text;
680 int entire_thread = 0;
684 for (i = 0; i < argc && argv[i][0] == '-'; i++) {
685 if (strcmp (argv[i], "--") == 0) {
689 if (STRNCMP_LITERAL (argv[i], "--format=") == 0) {
690 opt = argv[i] + sizeof ("--format=") - 1;
691 if (strcmp (opt, "text") == 0) {
692 format = &format_text;
693 } else if (strcmp (opt, "json") == 0) {
694 format = &format_json;
696 } else if (strcmp (opt, "mbox") == 0) {
697 format = &format_mbox;
698 } else if (strcmp (opt, "raw") == 0) {
701 fprintf (stderr, "Invalid value for --format: %s\n", opt);
704 } else if (STRNCMP_LITERAL (argv[i], "--entire-thread") == 0) {
707 fprintf (stderr, "Unrecognized option: %s\n", argv[i]);
715 config = notmuch_config_open (ctx, NULL, NULL);
719 query_string = query_string_from_args (ctx, argc, argv);
720 if (query_string == NULL) {
721 fprintf (stderr, "Out of memory\n");
725 if (*query_string == '\0') {
726 fprintf (stderr, "Error: notmuch show requires at least one search term.\n");
730 notmuch = notmuch_database_open (notmuch_config_get_database_path (config),
731 NOTMUCH_DATABASE_MODE_READ_ONLY);
735 query = notmuch_query_create (notmuch, query_string);
737 fprintf (stderr, "Out of memory\n");
742 return do_show_raw (ctx, query);
744 return do_show (ctx, query, format, entire_thread);
746 notmuch_query_destroy (query);
747 notmuch_database_close (notmuch);
753 notmuch_part_command (void *ctx, unused (int argc), unused (char *argv[]))
755 notmuch_config_t *config;
756 notmuch_database_t *notmuch;
757 notmuch_query_t *query;
758 notmuch_messages_t *messages;
759 notmuch_message_t *message;
764 for (i = 0; i < argc && argv[i][0] == '-'; i++) {
765 if (strcmp (argv[i], "--") == 0) {
769 if (STRNCMP_LITERAL (argv[i], "--part=") == 0) {
770 part = atoi(argv[i] + sizeof ("--part=") - 1);
772 fprintf (stderr, "Unrecognized option: %s\n", argv[i]);
780 config = notmuch_config_open (ctx, NULL, NULL);
784 query_string = query_string_from_args (ctx, argc, argv);
785 if (query_string == NULL) {
786 fprintf (stderr, "Out of memory\n");
790 if (*query_string == '\0') {
791 fprintf (stderr, "Error: notmuch part requires at least one search term.\n");
795 notmuch = notmuch_database_open (notmuch_config_get_database_path (config),
796 NOTMUCH_DATABASE_MODE_READ_ONLY);
800 query = notmuch_query_create (notmuch, query_string);
802 fprintf (stderr, "Out of memory\n");
806 if (notmuch_query_count_messages (query) != 1) {
807 fprintf (stderr, "Error: search term did not match precisely one message.\n");
811 messages = notmuch_query_search_messages (query);
812 message = notmuch_messages_get (messages);
814 if (message == NULL) {
815 fprintf (stderr, "Error: cannot find matching message.\n");
819 show_one_part (notmuch_message_get_filename (message), part);
821 notmuch_query_destroy (query);
822 notmuch_database_close (notmuch);