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_start_text (GMimeObject *part,
36 format_part_content_text (GMimeObject *part);
39 format_part_end_text (GMimeObject *part);
41 static const notmuch_show_format_t format_text = {
43 "\fmessage{ ", format_message_text,
44 "\fheader{\n", format_headers_text, "\fheader}\n",
46 format_part_start_text,
47 format_part_content_text,
56 format_message_json (const void *ctx,
57 notmuch_message_t *message,
60 format_headers_json (const void *ctx,
61 notmuch_message_t *message);
64 format_part_start_json (unused (GMimeObject *part),
68 format_part_content_json (GMimeObject *part);
71 format_part_end_json (GMimeObject *part);
73 static const notmuch_show_format_t format_json = {
75 "{", format_message_json,
76 ", \"headers\": {", format_headers_json, "}",
78 format_part_start_json,
79 format_part_content_json,
88 format_message_mbox (const void *ctx,
89 notmuch_message_t *message,
92 static const notmuch_show_format_t format_mbox = {
94 "", format_message_mbox,
107 format_part_content_raw (GMimeObject *part);
109 static const notmuch_show_format_t format_raw = {
115 format_part_content_raw,
124 _get_tags_as_string (const void *ctx, notmuch_message_t *message)
126 notmuch_tags_t *tags;
131 result = talloc_strdup (ctx, "");
135 for (tags = notmuch_message_get_tags (message);
136 notmuch_tags_valid (tags);
137 notmuch_tags_move_to_next (tags))
139 tag = notmuch_tags_get (tags);
141 result = talloc_asprintf_append (result, "%s%s",
142 first ? "" : " ", tag);
149 /* Get a nice, single-line summary of message. */
151 _get_one_line_summary (const void *ctx, notmuch_message_t *message)
155 const char *relative_date;
158 from = notmuch_message_get_header (message, "from");
160 date = notmuch_message_get_date (message);
161 relative_date = notmuch_time_relative_date (ctx, date);
163 tags = _get_tags_as_string (ctx, message);
165 return talloc_asprintf (ctx, "%s (%s) (%s)",
166 from, relative_date, tags);
170 format_message_text (unused (const void *ctx), notmuch_message_t *message, int indent)
172 printf ("id:%s depth:%d match:%d filename:%s\n",
173 notmuch_message_get_message_id (message),
175 notmuch_message_get_flag (message, NOTMUCH_MESSAGE_FLAG_MATCH),
176 notmuch_message_get_filename (message));
180 format_message_json (const void *ctx, notmuch_message_t *message, unused (int indent))
182 notmuch_tags_t *tags;
184 void *ctx_quote = talloc_new (ctx);
186 const char *relative_date;
188 date = notmuch_message_get_date (message);
189 relative_date = notmuch_time_relative_date (ctx, date);
191 printf ("\"id\": %s, \"match\": %s, \"filename\": %s, \"timestamp\": %ld, \"date_relative\": \"%s\", \"tags\": [",
192 json_quote_str (ctx_quote, notmuch_message_get_message_id (message)),
193 notmuch_message_get_flag (message, NOTMUCH_MESSAGE_FLAG_MATCH) ? "true" : "false",
194 json_quote_str (ctx_quote, notmuch_message_get_filename (message)),
195 date, relative_date);
197 for (tags = notmuch_message_get_tags (message);
198 notmuch_tags_valid (tags);
199 notmuch_tags_move_to_next (tags))
201 printf("%s%s", first ? "" : ",",
202 json_quote_str (ctx_quote, notmuch_tags_get (tags)));
206 talloc_free (ctx_quote);
209 /* Extract just the email address from the contents of a From:
212 _extract_email_address (const void *ctx, const char *from)
214 InternetAddressList *addresses;
215 InternetAddress *address;
216 InternetAddressMailbox *mailbox;
217 const char *email = "MAILER-DAEMON";
219 addresses = internet_address_list_parse_string (from);
221 /* Bail if there is no address here. */
222 if (addresses == NULL || internet_address_list_length (addresses) < 1)
225 /* Otherwise, just use the first address. */
226 address = internet_address_list_get_address (addresses, 0);
228 /* The From header should never contain an address group rather
229 * than a mailbox. So bail if it does. */
230 if (! INTERNET_ADDRESS_IS_MAILBOX (address))
233 mailbox = INTERNET_ADDRESS_MAILBOX (address);
234 email = internet_address_mailbox_get_addr (mailbox);
235 email = talloc_strdup (ctx, email);
238 /* XXX: How to free addresses here? */
242 /* Return 1 if 'line' is an mbox From_ line---that is, a line
243 * beginning with zero or more '>' characters followed by the
244 * characters 'F', 'r', 'o', 'm', and space.
246 * Any characters at all may appear after that in the line.
249 _is_from_line (const char *line)
251 const char *s = line;
259 if (STRNCMP_LITERAL (s, "From ") == 0)
265 /* Print a message in "mboxrd" format as documented, for example,
268 * http://qmail.org/qmail-manual-html/man5/mbox.html
271 format_message_mbox (const void *ctx,
272 notmuch_message_t *message,
275 const char *filename;
280 struct tm date_gmtime;
281 char date_asctime[26];
287 filename = notmuch_message_get_filename (message);
288 file = fopen (filename, "r");
290 fprintf (stderr, "Failed to open %s: %s\n",
291 filename, strerror (errno));
295 from = notmuch_message_get_header (message, "from");
296 from = _extract_email_address (ctx, from);
298 date = notmuch_message_get_date (message);
299 gmtime_r (&date, &date_gmtime);
300 asctime_r (&date_gmtime, date_asctime);
302 printf ("From %s %s", from, date_asctime);
304 while ((line_len = getline (&line, &line_size, file)) != -1 ) {
305 if (_is_from_line (line))
316 format_headers_text (const void *ctx, notmuch_message_t *message)
318 const char *headers[] = {
319 "Subject", "From", "To", "Cc", "Bcc", "Date"
321 const char *name, *value;
324 printf ("%s\n", _get_one_line_summary (ctx, message));
326 for (i = 0; i < ARRAY_SIZE (headers); i++) {
328 value = notmuch_message_get_header (message, name);
329 if (value && strlen (value))
330 printf ("%s: %s\n", name, value);
335 format_headers_json (const void *ctx, notmuch_message_t *message)
337 const char *headers[] = {
338 "Subject", "From", "To", "Cc", "Bcc", "Date"
340 const char *name, *value;
342 int first_header = 1;
343 void *ctx_quote = talloc_new (ctx);
345 for (i = 0; i < ARRAY_SIZE (headers); i++) {
347 value = notmuch_message_get_header (message, name);
351 fputs (", ", stdout);
355 json_quote_str (ctx_quote, name),
356 json_quote_str (ctx_quote, value));
360 talloc_free (ctx_quote);
364 show_part_content (GMimeObject *part, GMimeStream *stream_out)
366 GMimeStream *stream_filter = NULL;
367 GMimeDataWrapper *wrapper;
370 /* do nothing if this is a multipart */
371 if (GMIME_IS_MULTIPART (part) || GMIME_IS_MESSAGE_PART (part))
374 charset = g_mime_object_get_content_type_parameter (part, "charset");
377 stream_filter = g_mime_stream_filter_new (stream_out);
378 g_mime_stream_filter_add(GMIME_STREAM_FILTER (stream_filter),
379 g_mime_filter_crlf_new (FALSE, FALSE));
381 GMimeFilter *charset_filter;
382 charset_filter = g_mime_filter_charset_new (charset, "UTF-8");
383 /* This result can be NULL for things like "unknown-8bit".
384 * Don't set a NULL filter as that makes GMime print
385 * annoying assertion-failure messages on stderr. */
387 g_mime_stream_filter_add (GMIME_STREAM_FILTER (stream_filter),
392 wrapper = g_mime_part_get_content_object (GMIME_PART (part));
393 if (wrapper && stream_filter)
394 g_mime_data_wrapper_write_to_stream (wrapper, stream_filter);
396 g_object_unref(stream_filter);
400 format_part_start_text (GMimeObject *part, int *part_count)
402 GMimeContentDisposition *disposition = g_mime_object_get_content_disposition (part);
405 strcmp (disposition->disposition, GMIME_DISPOSITION_ATTACHMENT) == 0)
407 printf ("\fattachment{ ID: %d", *part_count);
411 printf ("\fpart{ ID: %d", *part_count);
416 format_part_content_text (GMimeObject *part)
418 GMimeContentDisposition *disposition = g_mime_object_get_content_disposition (part);
419 GMimeContentType *content_type = g_mime_object_get_content_type (GMIME_OBJECT (part));
420 GMimeStream *stream_stdout = g_mime_stream_file_new (stdout);
422 printf (", Content-type: %s\n", g_mime_content_type_to_string (content_type));
425 strcmp (disposition->disposition, GMIME_DISPOSITION_ATTACHMENT) == 0)
427 const char *filename = g_mime_part_get_filename (GMIME_PART (part));
428 printf ("Attachment: %s (%s)\n", filename,
429 g_mime_content_type_to_string (content_type));
432 if (g_mime_content_type_is_type (content_type, "text", "*") &&
433 !g_mime_content_type_is_type (content_type, "text", "html"))
435 g_mime_stream_file_set_owner (GMIME_STREAM_FILE (stream_stdout), FALSE);
436 show_part_content (part, stream_stdout);
437 g_object_unref(stream_stdout);
439 else if (g_mime_content_type_is_type (content_type, "multipart", "*") ||
440 g_mime_content_type_is_type (content_type, "message", "rfc822"))
442 /* Do nothing for multipart since its content will be printed
447 printf ("Non-text part: %s\n",
448 g_mime_content_type_to_string (content_type));
453 format_part_end_text (GMimeObject *part)
455 GMimeContentDisposition *disposition;
457 disposition = g_mime_object_get_content_disposition (part);
459 strcmp (disposition->disposition, GMIME_DISPOSITION_ATTACHMENT) == 0)
461 printf ("\fattachment}\n");
465 printf ("\fpart}\n");
470 format_part_start_json (unused (GMimeObject *part), int *part_count)
472 printf ("{\"id\": %d", *part_count);
476 format_part_content_json (GMimeObject *part)
478 GMimeContentType *content_type = g_mime_object_get_content_type (GMIME_OBJECT (part));
479 GMimeStream *stream_memory = g_mime_stream_mem_new ();
480 const char *cid = g_mime_object_get_content_id (part);
481 void *ctx = talloc_new (NULL);
482 GMimeContentDisposition *disposition = g_mime_object_get_content_disposition (part);
483 GByteArray *part_content;
485 printf (", \"content-type\": %s",
486 json_quote_str (ctx, g_mime_content_type_to_string (content_type)));
489 printf(", \"content-id\": %s", json_quote_str (ctx, cid));
492 strcmp (disposition->disposition, GMIME_DISPOSITION_ATTACHMENT) == 0)
494 const char *filename = g_mime_part_get_filename (GMIME_PART (part));
496 printf (", \"filename\": %s", json_quote_str (ctx, filename));
499 if (g_mime_content_type_is_type (content_type, "text", "*") &&
500 !g_mime_content_type_is_type (content_type, "text", "html"))
502 show_part_content (part, stream_memory);
503 part_content = g_mime_stream_mem_get_byte_array (GMIME_STREAM_MEM (stream_memory));
505 printf (", \"content\": %s", json_quote_chararray (ctx, (char *) part_content->data, part_content->len));
507 else if (g_mime_content_type_is_type (content_type, "multipart", "*") ||
508 g_mime_content_type_is_type (content_type, "message", "rfc822"))
510 printf (", \"content\": [");
515 g_object_unref (stream_memory);
519 format_part_end_json (GMimeObject *part)
521 GMimeContentType *content_type;
523 content_type = g_mime_object_get_content_type (GMIME_OBJECT (part));
525 if (g_mime_content_type_is_type (content_type, "multipart", "*") ||
526 g_mime_content_type_is_type (content_type, "message", "rfc822"))
533 format_part_content_raw (GMimeObject *part)
535 GMimeStream *stream_stdout = g_mime_stream_file_new (stdout);
536 g_mime_stream_file_set_owner (GMIME_STREAM_FILE (stream_stdout), FALSE);
537 show_part_content (part, stream_stdout);
538 g_object_unref(stream_stdout);
542 show_message (void *ctx,
543 const notmuch_show_format_t *format,
544 notmuch_message_t *message,
546 notmuch_show_params_t *params)
548 if (params->part <= 0) {
549 fputs (format->message_start, stdout);
551 format->message(ctx, message, indent);
553 fputs (format->header_start, stdout);
555 format->header(ctx, message);
556 fputs (format->header_end, stdout);
558 fputs (format->body_start, stdout);
561 if (format->part_content)
562 show_message_body (notmuch_message_get_filename (message),
565 if (params->part <= 0) {
566 fputs (format->body_end, stdout);
568 fputs (format->message_end, stdout);
573 show_messages (void *ctx,
574 const notmuch_show_format_t *format,
575 notmuch_messages_t *messages,
577 notmuch_show_params_t *params)
579 notmuch_message_t *message;
580 notmuch_bool_t match;
584 fputs (format->message_set_start, stdout);
587 notmuch_messages_valid (messages);
588 notmuch_messages_move_to_next (messages))
591 fputs (format->message_set_sep, stdout);
594 fputs (format->message_set_start, stdout);
596 message = notmuch_messages_get (messages);
598 match = notmuch_message_get_flag (message, NOTMUCH_MESSAGE_FLAG_MATCH);
600 next_indent = indent;
602 if (match || params->entire_thread) {
603 show_message (ctx, format, message, indent, params);
604 next_indent = indent + 1;
606 fputs (format->message_set_sep, stdout);
611 notmuch_message_get_replies (message),
615 notmuch_message_destroy (message);
617 fputs (format->message_set_end, stdout);
620 fputs (format->message_set_end, stdout);
623 /* Formatted output of single message */
625 do_show_single (void *ctx,
626 notmuch_query_t *query,
627 const notmuch_show_format_t *format,
628 notmuch_show_params_t *params)
630 notmuch_messages_t *messages;
631 notmuch_message_t *message;
633 if (notmuch_query_count_messages (query) != 1) {
634 fprintf (stderr, "Error: search term did not match precisely one message.\n");
638 messages = notmuch_query_search_messages (query);
639 message = notmuch_messages_get (messages);
641 if (message == NULL) {
642 fprintf (stderr, "Error: Cannot find matching message.\n");
646 notmuch_message_set_flag (message, NOTMUCH_MESSAGE_FLAG_MATCH, 1);
648 /* Special case for --format=raw of full single message, just cat out file */
649 if (params->raw && 0 == params->part) {
651 const char *filename;
656 filename = notmuch_message_get_filename (message);
657 if (filename == NULL) {
658 fprintf (stderr, "Error: Cannot message filename.\n");
662 file = fopen (filename, "r");
664 fprintf (stderr, "Error: Cannot open file %s: %s\n", filename, strerror (errno));
668 while (!feof (file)) {
669 size = fread (buf, 1, sizeof (buf), file);
670 fwrite (buf, size, 1, stdout);
677 show_message (ctx, format, message, 0, params);
684 /* Formatted output of threads */
687 notmuch_query_t *query,
688 const notmuch_show_format_t *format,
689 notmuch_show_params_t *params)
691 notmuch_threads_t *threads;
692 notmuch_thread_t *thread;
693 notmuch_messages_t *messages;
694 int first_toplevel = 1;
696 fputs (format->message_set_start, stdout);
698 for (threads = notmuch_query_search_threads (query);
699 notmuch_threads_valid (threads);
700 notmuch_threads_move_to_next (threads))
702 thread = notmuch_threads_get (threads);
704 messages = notmuch_thread_get_toplevel_messages (thread);
706 if (messages == NULL)
707 INTERNAL_ERROR ("Thread %s has no toplevel messages.\n",
708 notmuch_thread_get_thread_id (thread));
711 fputs (format->message_set_sep, stdout);
714 show_messages (ctx, format, messages, 0, params);
716 notmuch_thread_destroy (thread);
720 fputs (format->message_set_end, stdout);
726 notmuch_show_command (void *ctx, unused (int argc), unused (char *argv[]))
728 notmuch_config_t *config;
729 notmuch_database_t *notmuch;
730 notmuch_query_t *query;
733 const notmuch_show_format_t *format = &format_text;
734 notmuch_show_params_t params;
736 int format_specified = 0;
739 params.entire_thread = 0;
743 for (i = 0; i < argc && argv[i][0] == '-'; i++) {
744 if (strcmp (argv[i], "--") == 0) {
748 if (STRNCMP_LITERAL (argv[i], "--format=") == 0) {
749 opt = argv[i] + sizeof ("--format=") - 1;
750 if (strcmp (opt, "text") == 0) {
751 format = &format_text;
752 } else if (strcmp (opt, "json") == 0) {
753 format = &format_json;
754 params.entire_thread = 1;
755 } else if (strcmp (opt, "mbox") == 0) {
756 format = &format_mbox;
758 } else if (strcmp (opt, "raw") == 0) {
759 format = &format_raw;
762 fprintf (stderr, "Invalid value for --format: %s\n", opt);
765 format_specified = 1;
766 } else if (STRNCMP_LITERAL (argv[i], "--part=") == 0) {
767 params.part = atoi(argv[i] + sizeof ("--part=") - 1);
768 } else if (STRNCMP_LITERAL (argv[i], "--entire-thread") == 0) {
769 params.entire_thread = 1;
771 fprintf (stderr, "Unrecognized option: %s\n", argv[i]);
779 config = notmuch_config_open (ctx, NULL, NULL);
783 query_string = query_string_from_args (ctx, argc, argv);
784 if (query_string == NULL) {
785 fprintf (stderr, "Out of memory\n");
789 if (mbox && params.part > 0) {
790 fprintf (stderr, "Error: specifying parts is incompatible with mbox output format.\n");
794 if (*query_string == '\0') {
795 fprintf (stderr, "Error: notmuch show requires at least one search term.\n");
799 notmuch = notmuch_database_open (notmuch_config_get_database_path (config),
800 NOTMUCH_DATABASE_MODE_READ_ONLY);
804 query = notmuch_query_create (notmuch, query_string);
806 fprintf (stderr, "Out of memory\n");
810 /* if part was requested and format was not specified, use format=raw */
811 if (params.part >= 0 && !format_specified)
812 format = &format_raw;
814 /* If --format=raw specified without specifying part, we can only
815 * output single message, so set part=0 */
816 if (params.raw && params.part < 0)
819 if (params.part >= 0)
820 return do_show_single (ctx, query, format, ¶ms);
822 return do_show (ctx, query, format, ¶ms);
824 notmuch_query_destroy (query);
825 notmuch_database_close (notmuch);