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 format_part_raw (GMimeObject *part,
87 unused (int *part_count));
89 static const notmuch_show_format_t format_raw = {
93 "", format_part_raw, NULL, "", "",
99 _get_tags_as_string (const void *ctx, notmuch_message_t *message)
101 notmuch_tags_t *tags;
106 result = talloc_strdup (ctx, "");
110 for (tags = notmuch_message_get_tags (message);
111 notmuch_tags_valid (tags);
112 notmuch_tags_move_to_next (tags))
114 tag = notmuch_tags_get (tags);
116 result = talloc_asprintf_append (result, "%s%s",
117 first ? "" : " ", tag);
124 /* Get a nice, single-line summary of message. */
126 _get_one_line_summary (const void *ctx, notmuch_message_t *message)
130 const char *relative_date;
133 from = notmuch_message_get_header (message, "from");
135 date = notmuch_message_get_date (message);
136 relative_date = notmuch_time_relative_date (ctx, date);
138 tags = _get_tags_as_string (ctx, message);
140 return talloc_asprintf (ctx, "%s (%s) (%s)",
141 from, relative_date, tags);
145 format_message_text (unused (const void *ctx), notmuch_message_t *message, int indent)
147 printf ("id:%s depth:%d match:%d filename:%s\n",
148 notmuch_message_get_message_id (message),
150 notmuch_message_get_flag (message, NOTMUCH_MESSAGE_FLAG_MATCH),
151 notmuch_message_get_filename (message));
155 format_message_json (const void *ctx, notmuch_message_t *message, unused (int indent))
157 notmuch_tags_t *tags;
159 void *ctx_quote = talloc_new (ctx);
161 const char *relative_date;
163 date = notmuch_message_get_date (message);
164 relative_date = notmuch_time_relative_date (ctx, date);
166 printf ("\"id\": %s, \"match\": %s, \"filename\": %s, \"timestamp\": %ld, \"date_relative\": \"%s\", \"tags\": [",
167 json_quote_str (ctx_quote, notmuch_message_get_message_id (message)),
168 notmuch_message_get_flag (message, NOTMUCH_MESSAGE_FLAG_MATCH) ? "true" : "false",
169 json_quote_str (ctx_quote, notmuch_message_get_filename (message)),
170 date, relative_date);
172 for (tags = notmuch_message_get_tags (message);
173 notmuch_tags_valid (tags);
174 notmuch_tags_move_to_next (tags))
176 printf("%s%s", first ? "" : ",",
177 json_quote_str (ctx_quote, notmuch_tags_get (tags)));
181 talloc_free (ctx_quote);
184 /* Extract just the email address from the contents of a From:
187 _extract_email_address (const void *ctx, const char *from)
189 InternetAddressList *addresses;
190 InternetAddress *address;
191 InternetAddressMailbox *mailbox;
192 const char *email = "MAILER-DAEMON";
194 addresses = internet_address_list_parse_string (from);
196 /* Bail if there is no address here. */
197 if (addresses == NULL || internet_address_list_length (addresses) < 1)
200 /* Otherwise, just use the first address. */
201 address = internet_address_list_get_address (addresses, 0);
203 /* The From header should never contain an address group rather
204 * than a mailbox. So bail if it does. */
205 if (! INTERNET_ADDRESS_IS_MAILBOX (address))
208 mailbox = INTERNET_ADDRESS_MAILBOX (address);
209 email = internet_address_mailbox_get_addr (mailbox);
210 email = talloc_strdup (ctx, email);
213 /* XXX: How to free addresses here? */
217 /* Return 1 if 'line' is an mbox From_ line---that is, a line
218 * beginning with zero or more '>' characters followed by the
219 * characters 'F', 'r', 'o', 'm', and space.
221 * Any characters at all may appear after that in the line.
224 _is_from_line (const char *line)
226 const char *s = line;
234 if (STRNCMP_LITERAL (s, "From ") == 0)
240 /* Print a message in "mboxrd" format as documented, for example,
243 * http://qmail.org/qmail-manual-html/man5/mbox.html
246 format_message_mbox (const void *ctx,
247 notmuch_message_t *message,
250 const char *filename;
255 struct tm date_gmtime;
256 char date_asctime[26];
262 filename = notmuch_message_get_filename (message);
263 file = fopen (filename, "r");
265 fprintf (stderr, "Failed to open %s: %s\n",
266 filename, strerror (errno));
270 from = notmuch_message_get_header (message, "from");
271 from = _extract_email_address (ctx, from);
273 date = notmuch_message_get_date (message);
274 gmtime_r (&date, &date_gmtime);
275 asctime_r (&date_gmtime, date_asctime);
277 printf ("From %s %s", from, date_asctime);
279 while ((line_len = getline (&line, &line_size, file)) != -1 ) {
280 if (_is_from_line (line))
291 format_headers_text (const void *ctx, notmuch_message_t *message)
293 const char *headers[] = {
294 "Subject", "From", "To", "Cc", "Bcc", "Date"
296 const char *name, *value;
299 printf ("%s\n", _get_one_line_summary (ctx, message));
301 for (i = 0; i < ARRAY_SIZE (headers); i++) {
303 value = notmuch_message_get_header (message, name);
304 if (value && strlen (value))
305 printf ("%s: %s\n", name, value);
310 format_headers_json (const void *ctx, notmuch_message_t *message)
312 const char *headers[] = {
313 "Subject", "From", "To", "Cc", "Bcc", "Date"
315 const char *name, *value;
317 int first_header = 1;
318 void *ctx_quote = talloc_new (ctx);
320 for (i = 0; i < ARRAY_SIZE (headers); i++) {
322 value = notmuch_message_get_header (message, name);
326 fputs (", ", stdout);
330 json_quote_str (ctx_quote, name),
331 json_quote_str (ctx_quote, value));
335 talloc_free (ctx_quote);
339 show_part_content (GMimeObject *part, GMimeStream *stream_out)
341 GMimeStream *stream_filter = NULL;
342 GMimeDataWrapper *wrapper;
345 /* do nothing if this is a multipart */
346 if (GMIME_IS_MULTIPART (part) || GMIME_IS_MESSAGE_PART (part))
349 charset = g_mime_object_get_content_type_parameter (part, "charset");
352 stream_filter = g_mime_stream_filter_new (stream_out);
353 g_mime_stream_filter_add(GMIME_STREAM_FILTER (stream_filter),
354 g_mime_filter_crlf_new (FALSE, FALSE));
356 GMimeFilter *charset_filter;
357 charset_filter = g_mime_filter_charset_new (charset, "UTF-8");
358 /* This result can be NULL for things like "unknown-8bit".
359 * Don't set a NULL filter as that makes GMime print
360 * annoying assertion-failure messages on stderr. */
362 g_mime_stream_filter_add (GMIME_STREAM_FILTER (stream_filter),
367 wrapper = g_mime_part_get_content_object (GMIME_PART (part));
368 if (wrapper && stream_filter)
369 g_mime_data_wrapper_write_to_stream (wrapper, stream_filter);
371 g_object_unref(stream_filter);
375 format_part_text (GMimeObject *part, int *part_count)
377 GMimeContentDisposition *disposition;
378 GMimeContentType *content_type;
380 disposition = g_mime_object_get_content_disposition (part);
382 strcmp (disposition->disposition, GMIME_DISPOSITION_ATTACHMENT) == 0)
384 const char *filename = g_mime_part_get_filename (GMIME_PART (part));
385 content_type = g_mime_object_get_content_type (GMIME_OBJECT (part));
387 printf ("\fattachment{ ID: %d, Content-type: %s\n",
389 g_mime_content_type_to_string (content_type));
390 printf ("Attachment: %s (%s)\n", filename,
391 g_mime_content_type_to_string (content_type));
393 if (g_mime_content_type_is_type (content_type, "text", "*") &&
394 !g_mime_content_type_is_type (content_type, "text", "html"))
396 GMimeStream *stream_stdout = g_mime_stream_file_new (stdout);
397 g_mime_stream_file_set_owner (GMIME_STREAM_FILE (stream_stdout), FALSE);
398 show_part_content (part, stream_stdout);
399 g_object_unref(stream_stdout);
405 content_type = g_mime_object_get_content_type (GMIME_OBJECT (part));
407 printf ("\fpart{ ID: %d, Content-type: %s\n",
409 g_mime_content_type_to_string (content_type));
411 if (g_mime_content_type_is_type (content_type, "text", "*") &&
412 !g_mime_content_type_is_type (content_type, "text", "html"))
414 GMimeStream *stream_stdout = g_mime_stream_file_new (stdout);
415 g_mime_stream_file_set_owner (GMIME_STREAM_FILE (stream_stdout), FALSE);
416 show_part_content (part, stream_stdout);
417 g_object_unref(stream_stdout);
419 else if (g_mime_content_type_is_type (content_type, "multipart", "*") ||
420 g_mime_content_type_is_type (content_type, "message", "rfc822"))
422 /* Do nothing for multipart since its content will be printed
427 printf ("Non-text part: %s\n",
428 g_mime_content_type_to_string (content_type));
433 format_part_end_text (GMimeObject *part)
435 GMimeContentDisposition *disposition;
437 disposition = g_mime_object_get_content_disposition (part);
439 strcmp (disposition->disposition, GMIME_DISPOSITION_ATTACHMENT) == 0)
441 printf ("\fattachment}\n");
445 printf ("\fpart}\n");
450 format_part_json (GMimeObject *part, int *part_count)
452 GMimeContentType *content_type;
453 GMimeContentDisposition *disposition;
454 void *ctx = talloc_new (NULL);
455 GMimeStream *stream_memory = g_mime_stream_mem_new ();
456 GByteArray *part_content;
459 content_type = g_mime_object_get_content_type (GMIME_OBJECT (part));
461 printf ("{\"id\": %d, \"content-type\": %s",
463 json_quote_str (ctx, g_mime_content_type_to_string (content_type)));
465 cid = g_mime_object_get_content_id (part);
467 printf(", \"content-id\": %s",
468 json_quote_str (ctx, cid));
470 disposition = g_mime_object_get_content_disposition (part);
472 strcmp (disposition->disposition, GMIME_DISPOSITION_ATTACHMENT) == 0)
474 const char *filename = g_mime_part_get_filename (GMIME_PART (part));
476 printf (", \"filename\": %s", json_quote_str (ctx, filename));
479 if (g_mime_content_type_is_type (content_type, "text", "*") &&
480 !g_mime_content_type_is_type (content_type, "text", "html"))
482 show_part_content (part, stream_memory);
483 part_content = g_mime_stream_mem_get_byte_array (GMIME_STREAM_MEM (stream_memory));
485 printf (", \"content\": %s", json_quote_chararray (ctx, (char *) part_content->data, part_content->len));
487 else if (g_mime_content_type_is_type (content_type, "multipart", "*") ||
488 g_mime_content_type_is_type (content_type, "message", "rfc822"))
490 printf (", \"content\": [");
495 g_object_unref (stream_memory);
499 format_part_end_json (GMimeObject *part)
501 GMimeContentType *content_type;
503 content_type = g_mime_object_get_content_type (GMIME_OBJECT (part));
505 if (g_mime_content_type_is_type (content_type, "multipart", "*") ||
506 g_mime_content_type_is_type (content_type, "message", "rfc822"))
513 format_part_raw (GMimeObject *part, unused (int *part_count))
515 GMimeStream *stream_stdout = g_mime_stream_file_new (stdout);
516 g_mime_stream_file_set_owner (GMIME_STREAM_FILE (stream_stdout), FALSE);
517 show_part_content (part, stream_stdout);
518 g_object_unref(stream_stdout);
522 show_message (void *ctx,
523 const notmuch_show_format_t *format,
524 notmuch_message_t *message,
526 notmuch_show_params_t *params)
528 if (params->part <= 0) {
529 fputs (format->message_start, stdout);
531 format->message(ctx, message, indent);
533 fputs (format->header_start, stdout);
535 format->header(ctx, message);
536 fputs (format->header_end, stdout);
538 fputs (format->body_start, stdout);
542 show_message_body (notmuch_message_get_filename (message),
545 if (params->part <= 0) {
546 fputs (format->body_end, stdout);
548 fputs (format->message_end, stdout);
553 show_messages (void *ctx,
554 const notmuch_show_format_t *format,
555 notmuch_messages_t *messages,
557 notmuch_show_params_t *params)
559 notmuch_message_t *message;
560 notmuch_bool_t match;
564 fputs (format->message_set_start, stdout);
567 notmuch_messages_valid (messages);
568 notmuch_messages_move_to_next (messages))
571 fputs (format->message_set_sep, stdout);
574 fputs (format->message_set_start, stdout);
576 message = notmuch_messages_get (messages);
578 match = notmuch_message_get_flag (message, NOTMUCH_MESSAGE_FLAG_MATCH);
580 next_indent = indent;
582 if (match || params->entire_thread) {
583 show_message (ctx, format, message, indent, params);
584 next_indent = indent + 1;
586 fputs (format->message_set_sep, stdout);
591 notmuch_message_get_replies (message),
595 notmuch_message_destroy (message);
597 fputs (format->message_set_end, stdout);
600 fputs (format->message_set_end, stdout);
603 /* Formatted output of single message */
605 do_show_single (void *ctx,
606 notmuch_query_t *query,
607 const notmuch_show_format_t *format,
608 notmuch_show_params_t *params)
610 notmuch_messages_t *messages;
611 notmuch_message_t *message;
613 if (notmuch_query_count_messages (query) != 1) {
614 fprintf (stderr, "Error: search term did not match precisely one message.\n");
618 messages = notmuch_query_search_messages (query);
619 message = notmuch_messages_get (messages);
621 if (message == NULL) {
622 fprintf (stderr, "Error: Cannot find matching message.\n");
626 notmuch_message_set_flag (message, NOTMUCH_MESSAGE_FLAG_MATCH, 1);
628 /* Special case for --format=raw of full single message, just cat out file */
629 if (params->raw && 0 == params->part) {
631 const char *filename;
636 filename = notmuch_message_get_filename (message);
637 if (filename == NULL) {
638 fprintf (stderr, "Error: Cannot message filename.\n");
642 file = fopen (filename, "r");
644 fprintf (stderr, "Error: Cannot open file %s: %s\n", filename, strerror (errno));
648 while (!feof (file)) {
649 size = fread (buf, 1, sizeof (buf), file);
650 fwrite (buf, size, 1, stdout);
657 show_message (ctx, format, message, 0, params);
664 /* Formatted output of threads */
667 notmuch_query_t *query,
668 const notmuch_show_format_t *format,
669 notmuch_show_params_t *params)
671 notmuch_threads_t *threads;
672 notmuch_thread_t *thread;
673 notmuch_messages_t *messages;
674 int first_toplevel = 1;
676 fputs (format->message_set_start, stdout);
678 for (threads = notmuch_query_search_threads (query);
679 notmuch_threads_valid (threads);
680 notmuch_threads_move_to_next (threads))
682 thread = notmuch_threads_get (threads);
684 messages = notmuch_thread_get_toplevel_messages (thread);
686 if (messages == NULL)
687 INTERNAL_ERROR ("Thread %s has no toplevel messages.\n",
688 notmuch_thread_get_thread_id (thread));
691 fputs (format->message_set_sep, stdout);
694 show_messages (ctx, format, messages, 0, params);
696 notmuch_thread_destroy (thread);
700 fputs (format->message_set_end, stdout);
706 notmuch_show_command (void *ctx, unused (int argc), unused (char *argv[]))
708 notmuch_config_t *config;
709 notmuch_database_t *notmuch;
710 notmuch_query_t *query;
713 const notmuch_show_format_t *format = &format_text;
714 notmuch_show_params_t params;
716 int format_specified = 0;
719 params.entire_thread = 0;
723 for (i = 0; i < argc && argv[i][0] == '-'; i++) {
724 if (strcmp (argv[i], "--") == 0) {
728 if (STRNCMP_LITERAL (argv[i], "--format=") == 0) {
729 opt = argv[i] + sizeof ("--format=") - 1;
730 if (strcmp (opt, "text") == 0) {
731 format = &format_text;
732 } else if (strcmp (opt, "json") == 0) {
733 format = &format_json;
734 params.entire_thread = 1;
735 } else if (strcmp (opt, "mbox") == 0) {
736 format = &format_mbox;
738 } else if (strcmp (opt, "raw") == 0) {
739 format = &format_raw;
742 fprintf (stderr, "Invalid value for --format: %s\n", opt);
745 format_specified = 1;
746 } else if (STRNCMP_LITERAL (argv[i], "--part=") == 0) {
747 params.part = atoi(argv[i] + sizeof ("--part=") - 1);
748 } else if (STRNCMP_LITERAL (argv[i], "--entire-thread") == 0) {
749 params.entire_thread = 1;
751 fprintf (stderr, "Unrecognized option: %s\n", argv[i]);
759 config = notmuch_config_open (ctx, NULL, NULL);
763 query_string = query_string_from_args (ctx, argc, argv);
764 if (query_string == NULL) {
765 fprintf (stderr, "Out of memory\n");
769 if (mbox && params.part > 0) {
770 fprintf (stderr, "Error: specifying parts is incompatible with mbox output format.\n");
774 if (*query_string == '\0') {
775 fprintf (stderr, "Error: notmuch show requires at least one search term.\n");
779 notmuch = notmuch_database_open (notmuch_config_get_database_path (config),
780 NOTMUCH_DATABASE_MODE_READ_ONLY);
784 query = notmuch_query_create (notmuch, query_string);
786 fprintf (stderr, "Out of memory\n");
790 /* if part was requested and format was not specified, use format=raw */
791 if (params.part >= 0 && !format_specified)
792 format = &format_raw;
794 /* If --format=raw specified without specifying part, we can only
795 * output single message, so set part=0 */
796 if (params.raw && params.part < 0)
799 if (params.part >= 0)
800 return do_show_single (ctx, query, format, ¶ms);
802 return do_show (ctx, query, format, ¶ms);
804 notmuch_query_destroy (query);
805 notmuch_database_close (notmuch);