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);
415 printf ("Non-text part: %s\n",
416 g_mime_content_type_to_string (content_type));
419 printf ("\fpart}\n");
423 format_part_json (GMimeObject *part, int *part_count)
425 GMimeContentType *content_type;
426 GMimeContentDisposition *disposition;
427 void *ctx = talloc_new (NULL);
428 GMimeStream *stream_memory = g_mime_stream_mem_new ();
429 GByteArray *part_content;
431 content_type = g_mime_object_get_content_type (GMIME_OBJECT (part));
434 fputs (", ", stdout);
436 printf ("{\"id\": %d, \"content-type\": %s",
438 json_quote_str (ctx, g_mime_content_type_to_string (content_type)));
440 disposition = g_mime_object_get_content_disposition (part);
442 strcmp (disposition->disposition, GMIME_DISPOSITION_ATTACHMENT) == 0)
444 const char *filename = g_mime_part_get_filename (GMIME_PART (part));
446 printf (", \"filename\": %s", json_quote_str (ctx, filename));
449 if (g_mime_content_type_is_type (content_type, "text", "*") &&
450 !g_mime_content_type_is_type (content_type, "text", "html"))
452 show_part_content (part, stream_memory);
453 part_content = g_mime_stream_mem_get_byte_array (GMIME_STREAM_MEM (stream_memory));
455 printf (", \"content\": %s", json_quote_chararray (ctx, (char *) part_content->data, part_content->len));
462 g_object_unref (stream_memory);
466 show_message (void *ctx, const show_format_t *format, notmuch_message_t *message, int indent)
468 fputs (format->message_start, stdout);
470 format->message(ctx, message, indent);
472 fputs (format->header_start, stdout);
474 format->header(ctx, message);
475 fputs (format->header_end, stdout);
477 fputs (format->body_start, stdout);
479 show_message_body (notmuch_message_get_filename (message), format->part);
480 fputs (format->body_end, stdout);
482 fputs (format->message_end, stdout);
487 show_messages (void *ctx, const show_format_t *format, notmuch_messages_t *messages, int indent,
488 notmuch_bool_t entire_thread)
490 notmuch_message_t *message;
491 notmuch_bool_t match;
495 fputs (format->message_set_start, stdout);
498 notmuch_messages_valid (messages);
499 notmuch_messages_move_to_next (messages))
502 fputs (format->message_set_sep, stdout);
505 fputs (format->message_set_start, stdout);
507 message = notmuch_messages_get (messages);
509 match = notmuch_message_get_flag (message, NOTMUCH_MESSAGE_FLAG_MATCH);
511 next_indent = indent;
513 if (match || entire_thread) {
514 show_message (ctx, format, message, indent);
515 next_indent = indent + 1;
517 fputs (format->message_set_sep, stdout);
520 show_messages (ctx, format, notmuch_message_get_replies (message),
521 next_indent, entire_thread);
523 notmuch_message_destroy (message);
525 fputs (format->message_set_end, stdout);
528 fputs (format->message_set_end, stdout);
532 notmuch_show_command (void *ctx, unused (int argc), unused (char *argv[]))
534 notmuch_config_t *config;
535 notmuch_database_t *notmuch;
536 notmuch_query_t *query;
537 notmuch_threads_t *threads;
538 notmuch_thread_t *thread;
539 notmuch_messages_t *messages;
542 const show_format_t *format = &format_text;
543 int entire_thread = 0;
545 int first_toplevel = 1;
547 for (i = 0; i < argc && argv[i][0] == '-'; i++) {
548 if (strcmp (argv[i], "--") == 0) {
552 if (STRNCMP_LITERAL (argv[i], "--format=") == 0) {
553 opt = argv[i] + sizeof ("--format=") - 1;
554 if (strcmp (opt, "text") == 0) {
555 format = &format_text;
556 } else if (strcmp (opt, "json") == 0) {
557 format = &format_json;
559 } else if (strcmp (opt, "mbox") == 0) {
560 format = &format_mbox;
562 fprintf (stderr, "Invalid value for --format: %s\n", opt);
565 } else if (STRNCMP_LITERAL (argv[i], "--entire-thread") == 0) {
568 fprintf (stderr, "Unrecognized option: %s\n", argv[i]);
576 config = notmuch_config_open (ctx, NULL, NULL);
580 query_string = query_string_from_args (ctx, argc, argv);
581 if (query_string == NULL) {
582 fprintf (stderr, "Out of memory\n");
586 if (*query_string == '\0') {
587 fprintf (stderr, "Error: notmuch show requires at least one search term.\n");
591 notmuch = notmuch_database_open (notmuch_config_get_database_path (config),
592 NOTMUCH_DATABASE_MODE_READ_ONLY);
596 query = notmuch_query_create (notmuch, query_string);
598 fprintf (stderr, "Out of memory\n");
602 fputs (format->message_set_start, stdout);
604 for (threads = notmuch_query_search_threads (query);
605 notmuch_threads_valid (threads);
606 notmuch_threads_move_to_next (threads))
608 thread = notmuch_threads_get (threads);
610 messages = notmuch_thread_get_toplevel_messages (thread);
612 if (messages == NULL)
613 INTERNAL_ERROR ("Thread %s has no toplevel messages.\n",
614 notmuch_thread_get_thread_id (thread));
617 fputs (format->message_set_sep, stdout);
620 show_messages (ctx, format, messages, 0, entire_thread);
622 notmuch_thread_destroy (thread);
626 fputs (format->message_set_end, stdout);
628 notmuch_query_destroy (query);
629 notmuch_database_close (notmuch);
635 notmuch_part_command (void *ctx, unused (int argc), unused (char *argv[]))
637 notmuch_config_t *config;
638 notmuch_database_t *notmuch;
639 notmuch_query_t *query;
640 notmuch_messages_t *messages;
641 notmuch_message_t *message;
646 for (i = 0; i < argc && argv[i][0] == '-'; i++) {
647 if (strcmp (argv[i], "--") == 0) {
651 if (STRNCMP_LITERAL (argv[i], "--part=") == 0) {
652 part = atoi(argv[i] + sizeof ("--part=") - 1);
654 fprintf (stderr, "Unrecognized option: %s\n", argv[i]);
662 config = notmuch_config_open (ctx, NULL, NULL);
666 query_string = query_string_from_args (ctx, argc, argv);
667 if (query_string == NULL) {
668 fprintf (stderr, "Out of memory\n");
672 if (*query_string == '\0') {
673 fprintf (stderr, "Error: notmuch part requires at least one search term.\n");
677 notmuch = notmuch_database_open (notmuch_config_get_database_path (config),
678 NOTMUCH_DATABASE_MODE_READ_ONLY);
682 query = notmuch_query_create (notmuch, query_string);
684 fprintf (stderr, "Out of memory\n");
688 if (notmuch_query_count_messages (query) != 1) {
689 fprintf (stderr, "Error: search term did not match precisely one message.\n");
693 messages = notmuch_query_search_messages (query);
694 message = notmuch_messages_get (messages);
696 if (message == NULL) {
697 fprintf (stderr, "Error: cannot find matching message.\n");
701 show_one_part (notmuch_message_get_filename (message), part);
703 notmuch_query_destroy (query);
704 notmuch_database_close (notmuch);