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,
37 const char *message_end;
38 const char *message_set_sep;
39 const char *message_set_end;
43 format_message_text (unused (const void *ctx),
44 notmuch_message_t *message,
47 format_headers_text (const void *ctx,
48 notmuch_message_t *message);
50 format_part_text (GMimeObject *part,
52 static const show_format_t format_text = {
54 "\fmessage{ ", format_message_text,
55 "\fheader{\n", format_headers_text, "\fheader}\n",
56 "\fbody{\n", format_part_text, "\fbody}\n",
62 format_message_json (const void *ctx,
63 notmuch_message_t *message,
66 format_headers_json (const void *ctx,
67 notmuch_message_t *message);
69 format_part_json (GMimeObject *part,
71 static const show_format_t format_json = {
73 "{", format_message_json,
74 ", \"headers\": {", format_headers_json, "}",
75 ", \"body\": [", format_part_json, "]",
81 format_message_mbox (const void *ctx,
82 notmuch_message_t *message,
85 static const show_format_t format_mbox = {
87 "", format_message_mbox,
95 _get_tags_as_string (const void *ctx, notmuch_message_t *message)
102 result = talloc_strdup (ctx, "");
106 for (tags = notmuch_message_get_tags (message);
107 notmuch_tags_valid (tags);
108 notmuch_tags_move_to_next (tags))
110 tag = notmuch_tags_get (tags);
112 result = talloc_asprintf_append (result, "%s%s",
113 first ? "" : " ", tag);
120 /* Get a nice, single-line summary of message. */
122 _get_one_line_summary (const void *ctx, notmuch_message_t *message)
126 const char *relative_date;
129 from = notmuch_message_get_header (message, "from");
131 date = notmuch_message_get_date (message);
132 relative_date = notmuch_time_relative_date (ctx, date);
134 tags = _get_tags_as_string (ctx, message);
136 return talloc_asprintf (ctx, "%s (%s) (%s)",
137 from, relative_date, tags);
141 format_message_text (unused (const void *ctx), notmuch_message_t *message, int indent)
143 printf ("id:%s depth:%d match:%d filename:%s\n",
144 notmuch_message_get_message_id (message),
146 notmuch_message_get_flag (message, NOTMUCH_MESSAGE_FLAG_MATCH),
147 notmuch_message_get_filename (message));
151 format_message_json (const void *ctx, notmuch_message_t *message, unused (int indent))
153 notmuch_tags_t *tags;
155 void *ctx_quote = talloc_new (ctx);
157 const char *relative_date;
159 date = notmuch_message_get_date (message);
160 relative_date = notmuch_time_relative_date (ctx, date);
162 printf ("\"id\": %s, \"match\": %s, \"filename\": %s, \"timestamp\": %ld, \"date_relative\": \"%s\", \"tags\": [",
163 json_quote_str (ctx_quote, notmuch_message_get_message_id (message)),
164 notmuch_message_get_flag (message, NOTMUCH_MESSAGE_FLAG_MATCH) ? "true" : "false",
165 json_quote_str (ctx_quote, notmuch_message_get_filename (message)),
166 date, relative_date);
168 for (tags = notmuch_message_get_tags (message);
169 notmuch_tags_valid (tags);
170 notmuch_tags_move_to_next (tags))
172 printf("%s%s", first ? "" : ",",
173 json_quote_str (ctx_quote, notmuch_tags_get (tags)));
177 talloc_free (ctx_quote);
180 /* Extract just the email address from the contents of a From:
183 _extract_email_address (const void *ctx, const char *from)
185 InternetAddressList *addresses;
186 InternetAddress *address;
187 InternetAddressMailbox *mailbox;
188 const char *email = "MAILER-DAEMON";
190 addresses = internet_address_list_parse_string (from);
192 /* Bail if there is no address here. */
193 if (addresses == NULL || internet_address_list_length (addresses) < 1)
196 /* Otherwise, just use the first address. */
197 address = internet_address_list_get_address (addresses, 0);
199 /* The From header should never contain an address group rather
200 * than a mailbox. So bail if it does. */
201 if (! INTERNET_ADDRESS_IS_MAILBOX (address))
204 mailbox = INTERNET_ADDRESS_MAILBOX (address);
205 email = internet_address_mailbox_get_addr (mailbox);
206 email = talloc_strdup (ctx, email);
209 /* XXX: How to free addresses here? */
213 /* Return 1 if 'line' is an mbox From_ line---that is, a line
214 * beginning with zero or more '>' characters followed by the
215 * characters 'F', 'r', 'o', 'm', and space.
217 * Any characters at all may appear after that in the line.
220 _is_from_line (const char *line)
222 const char *s = line;
230 if (STRNCMP_LITERAL (s, "From ") == 0)
236 /* Print a message in "mboxrd" format as documented, for example,
239 * http://qmail.org/qmail-manual-html/man5/mbox.html
242 format_message_mbox (const void *ctx,
243 notmuch_message_t *message,
246 const char *filename;
251 struct tm date_gmtime;
252 char date_asctime[26];
258 filename = notmuch_message_get_filename (message);
259 file = fopen (filename, "r");
261 fprintf (stderr, "Failed to open %s: %s\n",
262 filename, strerror (errno));
266 from = notmuch_message_get_header (message, "from");
267 from = _extract_email_address (ctx, from);
269 date = notmuch_message_get_date (message);
270 gmtime_r (&date, &date_gmtime);
271 asctime_r (&date_gmtime, date_asctime);
273 printf ("From %s %s", from, date_asctime);
275 while ((line_len = getline (&line, &line_size, file)) != -1 ) {
276 if (_is_from_line (line))
287 format_headers_text (const void *ctx, notmuch_message_t *message)
289 const char *headers[] = {
290 "Subject", "From", "To", "Cc", "Bcc", "Date"
292 const char *name, *value;
295 printf ("%s\n", _get_one_line_summary (ctx, message));
297 for (i = 0; i < ARRAY_SIZE (headers); i++) {
299 value = notmuch_message_get_header (message, name);
300 if (value && strlen (value))
301 printf ("%s: %s\n", name, value);
306 format_headers_json (const void *ctx, notmuch_message_t *message)
308 const char *headers[] = {
309 "Subject", "From", "To", "Cc", "Bcc", "Date"
311 const char *name, *value;
313 int first_header = 1;
314 void *ctx_quote = talloc_new (ctx);
316 for (i = 0; i < ARRAY_SIZE (headers); i++) {
318 value = notmuch_message_get_header (message, name);
322 fputs (", ", stdout);
326 json_quote_str (ctx_quote, name),
327 json_quote_str (ctx_quote, value));
331 talloc_free (ctx_quote);
335 show_part_content (GMimeObject *part, GMimeStream *stream_out)
337 GMimeStream *stream_filter = NULL;
338 GMimeDataWrapper *wrapper;
341 charset = g_mime_object_get_content_type_parameter (part, "charset");
344 stream_filter = g_mime_stream_filter_new (stream_out);
345 g_mime_stream_filter_add(GMIME_STREAM_FILTER (stream_filter),
346 g_mime_filter_crlf_new (FALSE, FALSE));
348 GMimeFilter *charset_filter;
349 charset_filter = g_mime_filter_charset_new (charset, "UTF-8");
350 /* This result can be NULL for things like "unknown-8bit".
351 * Don't set a NULL filter as that makes GMime print
352 * annoying assertion-failure messages on stderr. */
354 g_mime_stream_filter_add (GMIME_STREAM_FILTER (stream_filter),
359 wrapper = g_mime_part_get_content_object (GMIME_PART (part));
360 if (wrapper && stream_filter)
361 g_mime_data_wrapper_write_to_stream (wrapper, stream_filter);
363 g_object_unref(stream_filter);
367 format_part_text (GMimeObject *part, int *part_count)
369 GMimeContentDisposition *disposition;
370 GMimeContentType *content_type;
372 disposition = g_mime_object_get_content_disposition (part);
374 strcmp (disposition->disposition, GMIME_DISPOSITION_ATTACHMENT) == 0)
376 const char *filename = g_mime_part_get_filename (GMIME_PART (part));
377 content_type = g_mime_object_get_content_type (GMIME_OBJECT (part));
379 printf ("\fattachment{ ID: %d, Content-type: %s\n",
381 g_mime_content_type_to_string (content_type));
382 printf ("Attachment: %s (%s)\n", filename,
383 g_mime_content_type_to_string (content_type));
385 if (g_mime_content_type_is_type (content_type, "text", "*") &&
386 !g_mime_content_type_is_type (content_type, "text", "html"))
388 GMimeStream *stream_stdout = g_mime_stream_file_new (stdout);
389 g_mime_stream_file_set_owner (GMIME_STREAM_FILE (stream_stdout), FALSE);
390 show_part_content (part, stream_stdout);
391 g_object_unref(stream_stdout);
394 printf ("\fattachment}\n");
399 content_type = g_mime_object_get_content_type (GMIME_OBJECT (part));
401 printf ("\fpart{ ID: %d, Content-type: %s\n",
403 g_mime_content_type_to_string (content_type));
405 if (g_mime_content_type_is_type (content_type, "text", "*") &&
406 !g_mime_content_type_is_type (content_type, "text", "html"))
408 GMimeStream *stream_stdout = g_mime_stream_file_new (stdout);
409 g_mime_stream_file_set_owner (GMIME_STREAM_FILE (stream_stdout), FALSE);
410 show_part_content (part, stream_stdout);
411 g_object_unref(stream_stdout);
413 else if (g_mime_content_type_is_type (content_type, "multipart", "*"))
415 /* Do nothing for multipart since its content will be printed
420 printf ("Non-text part: %s\n",
421 g_mime_content_type_to_string (content_type));
424 printf ("\fpart}\n");
428 format_part_json (GMimeObject *part, int *part_count)
430 GMimeContentType *content_type;
431 GMimeContentDisposition *disposition;
432 void *ctx = talloc_new (NULL);
433 GMimeStream *stream_memory = g_mime_stream_mem_new ();
434 GByteArray *part_content;
436 content_type = g_mime_object_get_content_type (GMIME_OBJECT (part));
439 fputs (", ", stdout);
441 printf ("{\"id\": %d, \"content-type\": %s",
443 json_quote_str (ctx, g_mime_content_type_to_string (content_type)));
445 disposition = g_mime_object_get_content_disposition (part);
447 strcmp (disposition->disposition, GMIME_DISPOSITION_ATTACHMENT) == 0)
449 const char *filename = g_mime_part_get_filename (GMIME_PART (part));
451 printf (", \"filename\": %s", json_quote_str (ctx, filename));
454 if (g_mime_content_type_is_type (content_type, "text", "*") &&
455 !g_mime_content_type_is_type (content_type, "text", "html"))
457 show_part_content (part, stream_memory);
458 part_content = g_mime_stream_mem_get_byte_array (GMIME_STREAM_MEM (stream_memory));
460 printf (", \"content\": %s", json_quote_chararray (ctx, (char *) part_content->data, part_content->len));
467 g_object_unref (stream_memory);
471 show_message (void *ctx, const show_format_t *format, notmuch_message_t *message, int indent)
473 fputs (format->message_start, stdout);
475 format->message(ctx, message, indent);
477 fputs (format->header_start, stdout);
479 format->header(ctx, message);
480 fputs (format->header_end, stdout);
482 fputs (format->body_start, stdout);
484 show_message_body (notmuch_message_get_filename (message), format->part);
485 fputs (format->body_end, stdout);
487 fputs (format->message_end, stdout);
492 show_messages (void *ctx, const show_format_t *format, notmuch_messages_t *messages, int indent,
493 notmuch_bool_t entire_thread)
495 notmuch_message_t *message;
496 notmuch_bool_t match;
500 fputs (format->message_set_start, stdout);
503 notmuch_messages_valid (messages);
504 notmuch_messages_move_to_next (messages))
507 fputs (format->message_set_sep, stdout);
510 fputs (format->message_set_start, stdout);
512 message = notmuch_messages_get (messages);
514 match = notmuch_message_get_flag (message, NOTMUCH_MESSAGE_FLAG_MATCH);
516 next_indent = indent;
518 if (match || entire_thread) {
519 show_message (ctx, format, message, indent);
520 next_indent = indent + 1;
522 fputs (format->message_set_sep, stdout);
525 show_messages (ctx, format, notmuch_message_get_replies (message),
526 next_indent, entire_thread);
528 notmuch_message_destroy (message);
530 fputs (format->message_set_end, stdout);
533 fputs (format->message_set_end, stdout);
536 /* Support for --format=raw */
538 do_show_raw (unused(void *ctx), notmuch_query_t *query)
540 notmuch_messages_t *messages;
541 notmuch_message_t *message;
542 const char *filename;
547 if (notmuch_query_count_messages (query) != 1) {
548 fprintf (stderr, "Error: search term did not match precisely one message.\n");
552 messages = notmuch_query_search_messages (query);
553 message = notmuch_messages_get (messages);
555 if (message == NULL) {
556 fprintf (stderr, "Error: Cannot find matching message.\n");
560 filename = notmuch_message_get_filename (message);
561 if (filename == NULL) {
562 fprintf (stderr, "Error: Cannot message filename.\n");
566 file = fopen (filename, "r");
568 fprintf (stderr, "Error: Cannot open file %s: %s\n", filename, strerror (errno));
572 while (!feof (file)) {
573 size = fread (buf, 1, sizeof (buf), file);
574 fwrite (buf, size, 1, stdout);
582 /* Support for --format=text|json|mbox */
585 notmuch_query_t *query,
586 const show_format_t *format,
589 notmuch_threads_t *threads;
590 notmuch_thread_t *thread;
591 notmuch_messages_t *messages;
592 int first_toplevel = 1;
594 fputs (format->message_set_start, stdout);
596 for (threads = notmuch_query_search_threads (query);
597 notmuch_threads_valid (threads);
598 notmuch_threads_move_to_next (threads))
600 thread = notmuch_threads_get (threads);
602 messages = notmuch_thread_get_toplevel_messages (thread);
604 if (messages == NULL)
605 INTERNAL_ERROR ("Thread %s has no toplevel messages.\n",
606 notmuch_thread_get_thread_id (thread));
609 fputs (format->message_set_sep, stdout);
612 show_messages (ctx, format, messages, 0, entire_thread);
614 notmuch_thread_destroy (thread);
618 fputs (format->message_set_end, stdout);
624 notmuch_show_command (void *ctx, unused (int argc), unused (char *argv[]))
626 notmuch_config_t *config;
627 notmuch_database_t *notmuch;
628 notmuch_query_t *query;
631 const show_format_t *format = &format_text;
632 int entire_thread = 0;
636 for (i = 0; i < argc && argv[i][0] == '-'; i++) {
637 if (strcmp (argv[i], "--") == 0) {
641 if (STRNCMP_LITERAL (argv[i], "--format=") == 0) {
642 opt = argv[i] + sizeof ("--format=") - 1;
643 if (strcmp (opt, "text") == 0) {
644 format = &format_text;
645 } else if (strcmp (opt, "json") == 0) {
646 format = &format_json;
648 } else if (strcmp (opt, "mbox") == 0) {
649 format = &format_mbox;
650 } else if (strcmp (opt, "raw") == 0) {
653 fprintf (stderr, "Invalid value for --format: %s\n", opt);
656 } else if (STRNCMP_LITERAL (argv[i], "--entire-thread") == 0) {
659 fprintf (stderr, "Unrecognized option: %s\n", argv[i]);
667 config = notmuch_config_open (ctx, NULL, NULL);
671 query_string = query_string_from_args (ctx, argc, argv);
672 if (query_string == NULL) {
673 fprintf (stderr, "Out of memory\n");
677 if (*query_string == '\0') {
678 fprintf (stderr, "Error: notmuch show requires at least one search term.\n");
682 notmuch = notmuch_database_open (notmuch_config_get_database_path (config),
683 NOTMUCH_DATABASE_MODE_READ_ONLY);
687 query = notmuch_query_create (notmuch, query_string);
689 fprintf (stderr, "Out of memory\n");
694 return do_show_raw (ctx, query);
696 return do_show (ctx, query, format, entire_thread);
698 notmuch_query_destroy (query);
699 notmuch_database_close (notmuch);
705 notmuch_part_command (void *ctx, unused (int argc), unused (char *argv[]))
707 notmuch_config_t *config;
708 notmuch_database_t *notmuch;
709 notmuch_query_t *query;
710 notmuch_messages_t *messages;
711 notmuch_message_t *message;
716 for (i = 0; i < argc && argv[i][0] == '-'; i++) {
717 if (strcmp (argv[i], "--") == 0) {
721 if (STRNCMP_LITERAL (argv[i], "--part=") == 0) {
722 part = atoi(argv[i] + sizeof ("--part=") - 1);
724 fprintf (stderr, "Unrecognized option: %s\n", argv[i]);
732 config = notmuch_config_open (ctx, NULL, NULL);
736 query_string = query_string_from_args (ctx, argc, argv);
737 if (query_string == NULL) {
738 fprintf (stderr, "Out of memory\n");
742 if (*query_string == '\0') {
743 fprintf (stderr, "Error: notmuch part requires at least one search term.\n");
747 notmuch = notmuch_database_open (notmuch_config_get_database_path (config),
748 NOTMUCH_DATABASE_MODE_READ_ONLY);
752 query = notmuch_query_create (notmuch, query_string);
754 fprintf (stderr, "Out of memory\n");
758 if (notmuch_query_count_messages (query) != 1) {
759 fprintf (stderr, "Error: search term did not match precisely one message.\n");
763 messages = notmuch_query_search_messages (query);
764 message = notmuch_messages_get (messages);
766 if (message == NULL) {
767 fprintf (stderr, "Error: cannot find matching message.\n");
771 show_one_part (notmuch_message_get_filename (message), part);
773 notmuch_query_destroy (query);
774 notmuch_database_close (notmuch);