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", "*"))
421 /* Do nothing for multipart since its content will be printed
426 printf ("Non-text part: %s\n",
427 g_mime_content_type_to_string (content_type));
432 format_part_end_text (GMimeObject *part)
434 GMimeContentDisposition *disposition;
436 disposition = g_mime_object_get_content_disposition (part);
438 strcmp (disposition->disposition, GMIME_DISPOSITION_ATTACHMENT) == 0)
440 printf ("\fattachment}\n");
444 printf ("\fpart}\n");
449 format_part_json (GMimeObject *part, int *part_count)
451 GMimeContentType *content_type;
452 GMimeContentDisposition *disposition;
453 void *ctx = talloc_new (NULL);
454 GMimeStream *stream_memory = g_mime_stream_mem_new ();
455 GByteArray *part_content;
458 content_type = g_mime_object_get_content_type (GMIME_OBJECT (part));
460 printf ("{\"id\": %d, \"content-type\": %s",
462 json_quote_str (ctx, g_mime_content_type_to_string (content_type)));
464 cid = g_mime_object_get_content_id (part);
466 printf(", \"content-id\": %s",
467 json_quote_str (ctx, cid));
469 disposition = g_mime_object_get_content_disposition (part);
471 strcmp (disposition->disposition, GMIME_DISPOSITION_ATTACHMENT) == 0)
473 const char *filename = g_mime_part_get_filename (GMIME_PART (part));
475 printf (", \"filename\": %s", json_quote_str (ctx, filename));
478 if (g_mime_content_type_is_type (content_type, "text", "*") &&
479 !g_mime_content_type_is_type (content_type, "text", "html"))
481 show_part_content (part, stream_memory);
482 part_content = g_mime_stream_mem_get_byte_array (GMIME_STREAM_MEM (stream_memory));
484 printf (", \"content\": %s", json_quote_chararray (ctx, (char *) part_content->data, part_content->len));
486 else if (g_mime_content_type_is_type (content_type, "multipart", "*"))
488 printf (", \"content\": [");
493 g_object_unref (stream_memory);
497 format_part_end_json (GMimeObject *part)
499 GMimeContentType *content_type;
501 content_type = g_mime_object_get_content_type (GMIME_OBJECT (part));
503 if (g_mime_content_type_is_type (content_type, "multipart", "*"))
510 format_part_raw (GMimeObject *part, unused (int *part_count))
512 GMimeStream *stream_stdout = g_mime_stream_file_new (stdout);
513 g_mime_stream_file_set_owner (GMIME_STREAM_FILE (stream_stdout), FALSE);
514 show_part_content (part, stream_stdout);
515 g_object_unref(stream_stdout);
519 show_message (void *ctx,
520 const notmuch_show_format_t *format,
521 notmuch_message_t *message,
523 notmuch_show_params_t *params)
525 if (params->part <= 0) {
526 fputs (format->message_start, stdout);
528 format->message(ctx, message, indent);
530 fputs (format->header_start, stdout);
532 format->header(ctx, message);
533 fputs (format->header_end, stdout);
535 fputs (format->body_start, stdout);
539 show_message_body (notmuch_message_get_filename (message),
542 if (params->part <= 0) {
543 fputs (format->body_end, stdout);
545 fputs (format->message_end, stdout);
550 show_messages (void *ctx,
551 const notmuch_show_format_t *format,
552 notmuch_messages_t *messages,
554 notmuch_show_params_t *params)
556 notmuch_message_t *message;
557 notmuch_bool_t match;
561 fputs (format->message_set_start, stdout);
564 notmuch_messages_valid (messages);
565 notmuch_messages_move_to_next (messages))
568 fputs (format->message_set_sep, stdout);
571 fputs (format->message_set_start, stdout);
573 message = notmuch_messages_get (messages);
575 match = notmuch_message_get_flag (message, NOTMUCH_MESSAGE_FLAG_MATCH);
577 next_indent = indent;
579 if (match || params->entire_thread) {
580 show_message (ctx, format, message, indent, params);
581 next_indent = indent + 1;
583 fputs (format->message_set_sep, stdout);
588 notmuch_message_get_replies (message),
592 notmuch_message_destroy (message);
594 fputs (format->message_set_end, stdout);
597 fputs (format->message_set_end, stdout);
600 /* Formatted output of single message */
602 do_show_single (void *ctx,
603 notmuch_query_t *query,
604 const notmuch_show_format_t *format,
605 notmuch_show_params_t *params)
607 notmuch_messages_t *messages;
608 notmuch_message_t *message;
610 if (notmuch_query_count_messages (query) != 1) {
611 fprintf (stderr, "Error: search term did not match precisely one message.\n");
615 messages = notmuch_query_search_messages (query);
616 message = notmuch_messages_get (messages);
618 if (message == NULL) {
619 fprintf (stderr, "Error: Cannot find matching message.\n");
623 notmuch_message_set_flag (message, NOTMUCH_MESSAGE_FLAG_MATCH, 1);
625 /* Special case for --format=raw of full single message, just cat out file */
626 if (params->raw && 0 == params->part) {
628 const char *filename;
633 filename = notmuch_message_get_filename (message);
634 if (filename == NULL) {
635 fprintf (stderr, "Error: Cannot message filename.\n");
639 file = fopen (filename, "r");
641 fprintf (stderr, "Error: Cannot open file %s: %s\n", filename, strerror (errno));
645 while (!feof (file)) {
646 size = fread (buf, 1, sizeof (buf), file);
647 fwrite (buf, size, 1, stdout);
654 show_message (ctx, format, message, 0, params);
661 /* Formatted output of threads */
664 notmuch_query_t *query,
665 const notmuch_show_format_t *format,
666 notmuch_show_params_t *params)
668 notmuch_threads_t *threads;
669 notmuch_thread_t *thread;
670 notmuch_messages_t *messages;
671 int first_toplevel = 1;
673 fputs (format->message_set_start, stdout);
675 for (threads = notmuch_query_search_threads (query);
676 notmuch_threads_valid (threads);
677 notmuch_threads_move_to_next (threads))
679 thread = notmuch_threads_get (threads);
681 messages = notmuch_thread_get_toplevel_messages (thread);
683 if (messages == NULL)
684 INTERNAL_ERROR ("Thread %s has no toplevel messages.\n",
685 notmuch_thread_get_thread_id (thread));
688 fputs (format->message_set_sep, stdout);
691 show_messages (ctx, format, messages, 0, params);
693 notmuch_thread_destroy (thread);
697 fputs (format->message_set_end, stdout);
703 notmuch_show_command (void *ctx, unused (int argc), unused (char *argv[]))
705 notmuch_config_t *config;
706 notmuch_database_t *notmuch;
707 notmuch_query_t *query;
710 const notmuch_show_format_t *format = &format_text;
711 notmuch_show_params_t params;
715 params.entire_thread = 0;
719 for (i = 0; i < argc && argv[i][0] == '-'; i++) {
720 if (strcmp (argv[i], "--") == 0) {
724 if (STRNCMP_LITERAL (argv[i], "--format=") == 0) {
725 opt = argv[i] + sizeof ("--format=") - 1;
726 if (strcmp (opt, "text") == 0) {
727 format = &format_text;
728 } else if (strcmp (opt, "json") == 0) {
729 format = &format_json;
730 params.entire_thread = 1;
731 } else if (strcmp (opt, "mbox") == 0) {
732 format = &format_mbox;
734 } else if (strcmp (opt, "raw") == 0) {
735 format = &format_raw;
738 fprintf (stderr, "Invalid value for --format: %s\n", opt);
741 } else if (STRNCMP_LITERAL (argv[i], "--part=") == 0) {
742 params.part = atoi(argv[i] + sizeof ("--part=") - 1);
743 } else if (STRNCMP_LITERAL (argv[i], "--entire-thread") == 0) {
744 params.entire_thread = 1;
746 fprintf (stderr, "Unrecognized option: %s\n", argv[i]);
754 config = notmuch_config_open (ctx, NULL, NULL);
758 query_string = query_string_from_args (ctx, argc, argv);
759 if (query_string == NULL) {
760 fprintf (stderr, "Out of memory\n");
764 if (mbox && params.part > 0) {
765 fprintf (stderr, "Error: specifying parts is incompatible with mbox output format.\n");
769 if (*query_string == '\0') {
770 fprintf (stderr, "Error: notmuch show requires at least one search term.\n");
774 notmuch = notmuch_database_open (notmuch_config_get_database_path (config),
775 NOTMUCH_DATABASE_MODE_READ_ONLY);
779 query = notmuch_query_create (notmuch, query_string);
781 fprintf (stderr, "Out of memory\n");
785 /* If --format=raw specified without specifying part, we can only
786 * output single message, so set part=0 */
787 if (params.raw && params.part < 0)
790 if (params.part >= 0)
791 return do_show_single (ctx, query, format, ¶ms);
793 return do_show (ctx, query, format, ¶ms);
795 notmuch_query_destroy (query);
796 notmuch_database_close (notmuch);