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 #include "string-util.h"
27 OUTPUT_SUMMARY = 1 << 0,
28 OUTPUT_THREADS = 1 << 1,
29 OUTPUT_MESSAGES = 1 << 2,
30 OUTPUT_FILES = 1 << 3,
34 OUTPUT_SENDER = 1 << 5,
35 OUTPUT_RECIPIENTS = 1 << 6,
36 OUTPUT_COUNT = 1 << 7,
47 notmuch_database_t *notmuch;
48 format_sel_t format_sel;
50 notmuch_exclude_t exclude;
51 notmuch_query_t *query;
57 GHashTable *addresses;
66 /* Return two stable query strings that identify exactly the matched
67 * and unmatched messages currently in thread. If there are no
68 * matched or unmatched messages, the returned buffers will be
71 get_thread_query (notmuch_thread_t *thread,
72 char **matched_out, char **unmatched_out)
74 notmuch_messages_t *messages;
76 size_t escaped_len = 0;
78 *matched_out = *unmatched_out = NULL;
80 for (messages = notmuch_thread_get_messages (thread);
81 notmuch_messages_valid (messages);
82 notmuch_messages_move_to_next (messages))
84 notmuch_message_t *message = notmuch_messages_get (messages);
85 const char *mid = notmuch_message_get_message_id (message);
86 /* Determine which query buffer to extend */
87 char **buf = notmuch_message_get_flag (
88 message, NOTMUCH_MESSAGE_FLAG_MATCH) ? matched_out : unmatched_out;
89 /* Add this message's id: query. Since "id" is an exclusive
90 * prefix, it is implicitly 'or'd together, so we only need to
91 * join queries with a space. */
92 if (make_boolean_term (thread, "id", mid, &escaped, &escaped_len) < 0)
95 *buf = talloc_asprintf_append_buffer (*buf, " %s", escaped);
97 *buf = talloc_strdup (thread, escaped);
101 talloc_free (escaped);
106 do_search_threads (search_context_t *ctx)
108 notmuch_thread_t *thread;
109 notmuch_threads_t *threads;
110 notmuch_tags_t *tags;
111 sprinter_t *format = ctx->format;
114 notmuch_status_t status;
116 if (ctx->offset < 0) {
117 ctx->offset += notmuch_query_count_threads (ctx->query);
122 status = notmuch_query_search_threads_st (ctx->query, &threads);
123 if (print_status_query("notmuch search", ctx->query, status))
126 format->begin_list (format);
129 notmuch_threads_valid (threads) && (ctx->limit < 0 || i < ctx->offset + ctx->limit);
130 notmuch_threads_move_to_next (threads), i++)
132 thread = notmuch_threads_get (threads);
134 if (i < ctx->offset) {
135 notmuch_thread_destroy (thread);
139 if (ctx->output == OUTPUT_THREADS) {
140 format->set_prefix (format, "thread");
141 format->string (format,
142 notmuch_thread_get_thread_id (thread));
143 format->separator (format);
144 } else { /* output == OUTPUT_SUMMARY */
145 void *ctx_quote = talloc_new (thread);
146 const char *authors = notmuch_thread_get_authors (thread);
147 const char *subject = notmuch_thread_get_subject (thread);
148 const char *thread_id = notmuch_thread_get_thread_id (thread);
149 int matched = notmuch_thread_get_matched_messages (thread);
150 int total = notmuch_thread_get_total_messages (thread);
151 const char *relative_date = NULL;
152 notmuch_bool_t first_tag = TRUE;
154 format->begin_map (format);
156 if (ctx->sort == NOTMUCH_SORT_OLDEST_FIRST)
157 date = notmuch_thread_get_oldest_date (thread);
159 date = notmuch_thread_get_newest_date (thread);
161 relative_date = notmuch_time_relative_date (ctx_quote, date);
163 if (format->is_text_printer) {
164 /* Special case for the text formatter */
165 printf ("thread:%s %12s [%d/%d] %s; %s (",
170 sanitize_string (ctx_quote, authors),
171 sanitize_string (ctx_quote, subject));
172 } else { /* Structured Output */
173 format->map_key (format, "thread");
174 format->string (format, thread_id);
175 format->map_key (format, "timestamp");
176 format->integer (format, date);
177 format->map_key (format, "date_relative");
178 format->string (format, relative_date);
179 format->map_key (format, "matched");
180 format->integer (format, matched);
181 format->map_key (format, "total");
182 format->integer (format, total);
183 format->map_key (format, "authors");
184 format->string (format, authors);
185 format->map_key (format, "subject");
186 format->string (format, subject);
187 if (notmuch_format_version >= 2) {
188 char *matched_query, *unmatched_query;
189 if (get_thread_query (thread, &matched_query,
190 &unmatched_query) < 0) {
191 fprintf (stderr, "Out of memory\n");
194 format->map_key (format, "query");
195 format->begin_list (format);
197 format->string (format, matched_query);
199 format->null (format);
201 format->string (format, unmatched_query);
203 format->null (format);
204 format->end (format);
208 talloc_free (ctx_quote);
210 format->map_key (format, "tags");
211 format->begin_list (format);
213 for (tags = notmuch_thread_get_tags (thread);
214 notmuch_tags_valid (tags);
215 notmuch_tags_move_to_next (tags))
217 const char *tag = notmuch_tags_get (tags);
219 if (format->is_text_printer) {
220 /* Special case for the text formatter */
226 } else { /* Structured Output */
227 format->string (format, tag);
231 if (format->is_text_printer)
234 format->end (format);
235 format->end (format);
236 format->separator (format);
239 notmuch_thread_destroy (thread);
242 format->end (format);
247 static mailbox_t *new_mailbox (void *ctx, const char *name, const char *addr)
251 mailbox = talloc (ctx, mailbox_t);
255 mailbox->name = talloc_strdup (mailbox, name);
256 mailbox->addr = talloc_strdup (mailbox, addr);
262 /* Returns TRUE iff name and addr is duplicate. If not, stores the
263 * name/addr pair in order to detect subsequent duplicates. */
264 static notmuch_bool_t
265 is_duplicate (const search_context_t *ctx, const char *name, const char *addr)
270 key = talloc_asprintf (ctx->format, "%s <%s>", name, addr);
274 mailbox = g_hash_table_lookup (ctx->addresses, key);
281 mailbox = new_mailbox (ctx->format, name, addr);
285 g_hash_table_insert (ctx->addresses, key, mailbox);
291 print_mailbox (const search_context_t *ctx, const mailbox_t *mailbox)
293 const char *name = mailbox->name;
294 const char *addr = mailbox->addr;
295 int count = mailbox->count;
296 sprinter_t *format = ctx->format;
297 InternetAddress *ia = internet_address_mailbox_new (name, addr);
300 /* name_addr has the name part quoted if necessary. Compare
301 * 'John Doe <john@doe.com>' vs. '"Doe, John" <john@doe.com>' */
302 name_addr = internet_address_to_string (ia, FALSE);
304 if (format->is_text_printer) {
306 format->integer (format, count);
307 format->string (format, "\t");
309 format->string (format, name_addr);
310 format->separator (format);
312 format->begin_map (format);
313 format->map_key (format, "name");
314 format->string (format, name);
315 format->map_key (format, "address");
316 format->string (format, addr);
317 format->map_key (format, "name-addr");
318 format->string (format, name_addr);
320 format->map_key (format, "count");
321 format->integer (format, count);
323 format->end (format);
324 format->separator (format);
331 /* Print or prepare for printing addresses from InternetAddressList. */
333 process_address_list (const search_context_t *ctx,
334 InternetAddressList *list)
336 InternetAddress *address;
339 for (i = 0; i < internet_address_list_length (list); i++) {
340 address = internet_address_list_get_address (list, i);
341 if (INTERNET_ADDRESS_IS_GROUP (address)) {
342 InternetAddressGroup *group;
343 InternetAddressList *group_list;
345 group = INTERNET_ADDRESS_GROUP (address);
346 group_list = internet_address_group_get_members (group);
347 if (group_list == NULL)
350 process_address_list (ctx, group_list);
352 InternetAddressMailbox *mailbox = INTERNET_ADDRESS_MAILBOX (address);
354 .name = internet_address_get_name (address),
355 .addr = internet_address_mailbox_get_addr (mailbox),
359 if (is_duplicate (ctx, mbx.name, mbx.addr))
362 if (ctx->output & OUTPUT_COUNT)
365 print_mailbox (ctx, &mbx);
370 /* Print or prepare for printing addresses from a message header. */
372 process_address_header (const search_context_t *ctx, const char *value)
374 InternetAddressList *list;
379 list = internet_address_list_parse_string (value);
383 process_address_list (ctx, list);
385 g_object_unref (list);
388 /* Destructor for talloc-allocated GHashTable keys and values. */
390 _talloc_free_for_g_hash (void *ptr)
396 print_hash_value (unused (gpointer key), gpointer value, gpointer user_data)
398 const mailbox_t *mailbox = value;
399 search_context_t *ctx = user_data;
401 print_mailbox (ctx, mailbox);
405 _count_filenames (notmuch_message_t *message)
407 notmuch_filenames_t *filenames;
410 filenames = notmuch_message_get_filenames (message);
412 while (notmuch_filenames_valid (filenames)) {
413 notmuch_filenames_move_to_next (filenames);
417 notmuch_filenames_destroy (filenames);
423 do_search_messages (search_context_t *ctx)
425 notmuch_message_t *message;
426 notmuch_messages_t *messages;
427 notmuch_filenames_t *filenames;
428 sprinter_t *format = ctx->format;
430 notmuch_status_t status;
432 if (ctx->offset < 0) {
433 ctx->offset += notmuch_query_count_messages (ctx->query);
438 status = notmuch_query_search_messages_st (ctx->query, &messages);
439 if (print_status_query ("notmuch search", ctx->query, status))
442 format->begin_list (format);
445 notmuch_messages_valid (messages) && (ctx->limit < 0 || i < ctx->offset + ctx->limit);
446 notmuch_messages_move_to_next (messages), i++)
451 message = notmuch_messages_get (messages);
453 if (ctx->output == OUTPUT_FILES) {
455 filenames = notmuch_message_get_filenames (message);
458 notmuch_filenames_valid (filenames);
459 notmuch_filenames_move_to_next (filenames), j++)
461 if (ctx->dupe < 0 || ctx->dupe == j) {
462 format->string (format, notmuch_filenames_get (filenames));
463 format->separator (format);
467 notmuch_filenames_destroy( filenames );
469 } else if (ctx->output == OUTPUT_MESSAGES) {
470 /* special case 1 for speed */
471 if (ctx->dupe <= 1 || ctx->dupe <= _count_filenames (message)) {
472 format->set_prefix (format, "id");
473 format->string (format,
474 notmuch_message_get_message_id (message));
475 format->separator (format);
478 if (ctx->output & OUTPUT_SENDER) {
481 addrs = notmuch_message_get_header (message, "from");
482 process_address_header (ctx, addrs);
485 if (ctx->output & OUTPUT_RECIPIENTS) {
486 const char *hdrs[] = { "to", "cc", "bcc" };
490 for (j = 0; j < ARRAY_SIZE (hdrs); j++) {
491 addrs = notmuch_message_get_header (message, hdrs[j]);
492 process_address_header (ctx, addrs);
497 notmuch_message_destroy (message);
500 if (ctx->addresses && ctx->output & OUTPUT_COUNT)
501 g_hash_table_foreach (ctx->addresses, print_hash_value, ctx);
503 notmuch_messages_destroy (messages);
505 format->end (format);
511 do_search_tags (const search_context_t *ctx)
513 notmuch_messages_t *messages = NULL;
514 notmuch_tags_t *tags;
516 sprinter_t *format = ctx->format;
517 notmuch_query_t *query = ctx->query;
518 notmuch_database_t *notmuch = ctx->notmuch;
520 /* should the following only special case if no excluded terms
523 /* Special-case query of "*" for better performance. */
524 if (strcmp (notmuch_query_get_query_string (query), "*") == 0) {
525 tags = notmuch_database_get_all_tags (notmuch);
527 notmuch_status_t status;
528 status = notmuch_query_search_messages_st (query, &messages);
529 if (print_status_query ("notmuch search", query, status))
532 tags = notmuch_messages_collect_tags (messages);
537 format->begin_list (format);
540 notmuch_tags_valid (tags);
541 notmuch_tags_move_to_next (tags))
543 tag = notmuch_tags_get (tags);
545 format->string (format, tag);
546 format->separator (format);
550 notmuch_tags_destroy (tags);
553 notmuch_messages_destroy (messages);
555 format->end (format);
561 _notmuch_search_prepare (search_context_t *ctx, notmuch_config_t *config, int argc, char *argv[])
565 char *status_string = NULL;
567 switch (ctx->format_sel) {
568 case NOTMUCH_FORMAT_TEXT:
569 ctx->format = sprinter_text_create (config, stdout);
571 case NOTMUCH_FORMAT_TEXT0:
572 if (ctx->output == OUTPUT_SUMMARY) {
573 fprintf (stderr, "Error: --format=text0 is not compatible with --output=summary.\n");
576 ctx->format = sprinter_text0_create (config, stdout);
578 case NOTMUCH_FORMAT_JSON:
579 ctx->format = sprinter_json_create (config, stdout);
581 case NOTMUCH_FORMAT_SEXP:
582 ctx->format = sprinter_sexp_create (config, stdout);
585 /* this should never happen */
586 INTERNAL_ERROR("no output format selected");
589 notmuch_exit_if_unsupported_format ();
591 if (notmuch_database_open_verbose (
592 notmuch_config_get_database_path (config),
593 NOTMUCH_DATABASE_MODE_READ_ONLY, &ctx->notmuch, &status_string)) {
596 fputs (status_string, stderr);
597 free (status_string);
603 notmuch_exit_if_unmatched_db_uuid (ctx->notmuch);
605 query_str = query_string_from_args (ctx->notmuch, argc, argv);
606 if (query_str == NULL) {
607 fprintf (stderr, "Out of memory.\n");
610 if (*query_str == '\0') {
611 fprintf (stderr, "Error: notmuch search requires at least one search term.\n");
615 ctx->query = notmuch_query_create (ctx->notmuch, query_str);
616 if (ctx->query == NULL) {
617 fprintf (stderr, "Out of memory\n");
621 notmuch_query_set_sort (ctx->query, ctx->sort);
623 if (ctx->exclude == NOTMUCH_EXCLUDE_FLAG && ctx->output != OUTPUT_SUMMARY) {
624 /* If we are not doing summary output there is nowhere to
625 * print the excluded flag so fall back on including the
626 * excluded messages. */
627 fprintf (stderr, "Warning: this output format cannot flag excluded messages.\n");
628 ctx->exclude = NOTMUCH_EXCLUDE_FALSE;
631 if (ctx->exclude != NOTMUCH_EXCLUDE_FALSE) {
632 const char **search_exclude_tags;
633 size_t search_exclude_tags_length;
635 search_exclude_tags = notmuch_config_get_search_exclude_tags
636 (config, &search_exclude_tags_length);
637 for (i = 0; i < search_exclude_tags_length; i++)
638 notmuch_query_add_tag_exclude (ctx->query, search_exclude_tags[i]);
639 notmuch_query_set_omit_excluded (ctx->query, ctx->exclude);
646 _notmuch_search_cleanup (search_context_t *ctx)
648 notmuch_query_destroy (ctx->query);
649 notmuch_database_destroy (ctx->notmuch);
651 talloc_free (ctx->format);
654 static search_context_t search_context = {
655 .format_sel = NOTMUCH_FORMAT_TEXT,
656 .exclude = NOTMUCH_EXCLUDE_TRUE,
657 .sort = NOTMUCH_SORT_NEWEST_FIRST,
660 .limit = -1, /* unlimited */
664 static const notmuch_opt_desc_t common_options[] = {
665 { NOTMUCH_OPT_KEYWORD, &search_context.sort, "sort", 's',
666 (notmuch_keyword_t []){ { "oldest-first", NOTMUCH_SORT_OLDEST_FIRST },
667 { "newest-first", NOTMUCH_SORT_NEWEST_FIRST },
669 { NOTMUCH_OPT_KEYWORD, &search_context.format_sel, "format", 'f',
670 (notmuch_keyword_t []){ { "json", NOTMUCH_FORMAT_JSON },
671 { "sexp", NOTMUCH_FORMAT_SEXP },
672 { "text", NOTMUCH_FORMAT_TEXT },
673 { "text0", NOTMUCH_FORMAT_TEXT0 },
675 { NOTMUCH_OPT_INT, ¬much_format_version, "format-version", 0, 0 },
680 notmuch_search_command (notmuch_config_t *config, int argc, char *argv[])
682 search_context_t *ctx = &search_context;
685 notmuch_opt_desc_t options[] = {
686 { NOTMUCH_OPT_KEYWORD, &ctx->output, "output", 'o',
687 (notmuch_keyword_t []){ { "summary", OUTPUT_SUMMARY },
688 { "threads", OUTPUT_THREADS },
689 { "messages", OUTPUT_MESSAGES },
690 { "files", OUTPUT_FILES },
691 { "tags", OUTPUT_TAGS },
693 { NOTMUCH_OPT_KEYWORD, &ctx->exclude, "exclude", 'x',
694 (notmuch_keyword_t []){ { "true", NOTMUCH_EXCLUDE_TRUE },
695 { "false", NOTMUCH_EXCLUDE_FALSE },
696 { "flag", NOTMUCH_EXCLUDE_FLAG },
697 { "all", NOTMUCH_EXCLUDE_ALL },
699 { NOTMUCH_OPT_INT, &ctx->offset, "offset", 'O', 0 },
700 { NOTMUCH_OPT_INT, &ctx->limit, "limit", 'L', 0 },
701 { NOTMUCH_OPT_INT, &ctx->dupe, "duplicate", 'D', 0 },
702 { NOTMUCH_OPT_INHERIT, (void *) &common_options, NULL, 0, 0 },
703 { NOTMUCH_OPT_INHERIT, (void *) ¬much_shared_options, NULL, 0, 0 },
707 ctx->output = OUTPUT_SUMMARY;
708 opt_index = parse_arguments (argc, argv, options, 1);
712 notmuch_process_shared_options (argv[0]);
714 if (ctx->output != OUTPUT_FILES && ctx->output != OUTPUT_MESSAGES &&
716 fprintf (stderr, "Error: --duplicate=N is only supported with --output=files and --output=messages.\n");
720 if (_notmuch_search_prepare (ctx, config,
721 argc - opt_index, argv + opt_index))
724 switch (ctx->output) {
727 ret = do_search_threads (ctx);
729 case OUTPUT_MESSAGES:
731 ret = do_search_messages (ctx);
734 ret = do_search_tags (ctx);
737 INTERNAL_ERROR ("Unexpected output");
740 _notmuch_search_cleanup (ctx);
742 return ret ? EXIT_FAILURE : EXIT_SUCCESS;
746 notmuch_address_command (notmuch_config_t *config, int argc, char *argv[])
748 search_context_t *ctx = &search_context;
751 notmuch_opt_desc_t options[] = {
752 { NOTMUCH_OPT_KEYWORD_FLAGS, &ctx->output, "output", 'o',
753 (notmuch_keyword_t []){ { "sender", OUTPUT_SENDER },
754 { "recipients", OUTPUT_RECIPIENTS },
755 { "count", OUTPUT_COUNT },
757 { NOTMUCH_OPT_KEYWORD, &ctx->exclude, "exclude", 'x',
758 (notmuch_keyword_t []){ { "true", NOTMUCH_EXCLUDE_TRUE },
759 { "false", NOTMUCH_EXCLUDE_FALSE },
761 { NOTMUCH_OPT_INHERIT, (void *) &common_options, NULL, 0, 0 },
762 { NOTMUCH_OPT_INHERIT, (void *) ¬much_shared_options, NULL, 0, 0 },
766 opt_index = parse_arguments (argc, argv, options, 1);
770 notmuch_process_shared_options (argv[0]);
772 if (! (ctx->output & (OUTPUT_SENDER | OUTPUT_RECIPIENTS)))
773 ctx->output |= OUTPUT_SENDER;
775 if (_notmuch_search_prepare (ctx, config,
776 argc - opt_index, argv + opt_index))
779 ctx->addresses = g_hash_table_new_full (g_str_hash, g_str_equal,
780 _talloc_free_for_g_hash, _talloc_free_for_g_hash);
782 ret = do_search_messages (ctx);
784 g_hash_table_unref (ctx->addresses);
787 _notmuch_search_cleanup (ctx);
789 return ret ? EXIT_FAILURE : EXIT_SUCCESS;