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 https://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,
37 OUTPUT_ADDRESS = 1 << 8,
54 notmuch_database_t *notmuch;
60 notmuch_query_t *query;
66 GHashTable *addresses;
76 /* Return two stable query strings that identify exactly the matched
77 * and unmatched messages currently in thread. If there are no
78 * matched or unmatched messages, the returned buffers will be
81 get_thread_query (notmuch_thread_t *thread,
82 char **matched_out, char **unmatched_out)
84 notmuch_messages_t *messages;
86 size_t escaped_len = 0;
88 *matched_out = *unmatched_out = NULL;
90 for (messages = notmuch_thread_get_messages (thread);
91 notmuch_messages_valid (messages);
92 notmuch_messages_move_to_next (messages)) {
93 notmuch_message_t *message = notmuch_messages_get (messages);
94 const char *mid = notmuch_message_get_message_id (message);
95 notmuch_bool_t is_set;
98 if (notmuch_message_get_flag_st (message, NOTMUCH_MESSAGE_FLAG_MATCH, &is_set))
100 /* Determine which query buffer to extend */
101 buf = is_set ? matched_out : unmatched_out;
102 /* Add this message's id: query. Since "id" is an exclusive
103 * prefix, it is implicitly 'or'd together, so we only need to
104 * join queries with a space. */
105 if (make_boolean_term (thread, "id", mid, &escaped, &escaped_len) < 0)
108 *buf = talloc_asprintf_append_buffer (*buf, " %s", escaped);
110 *buf = talloc_strdup (thread, escaped);
114 talloc_free (escaped);
119 do_search_threads (search_context_t *ctx)
121 notmuch_thread_t *thread;
122 notmuch_threads_t *threads;
123 notmuch_tags_t *tags;
124 sprinter_t *format = ctx->format;
127 notmuch_status_t status;
129 if (ctx->offset < 0) {
131 notmuch_status_t status;
132 status = notmuch_query_count_threads (ctx->query, &count);
133 if (print_status_query ("notmuch search", ctx->query, status))
136 ctx->offset += count;
141 status = notmuch_query_search_threads (ctx->query, &threads);
142 if (print_status_query ("notmuch search", ctx->query, status))
145 format->begin_list (format);
148 notmuch_threads_valid (threads) && (ctx->limit < 0 || i < ctx->offset + ctx->limit);
149 notmuch_threads_move_to_next (threads), i++) {
150 thread = notmuch_threads_get (threads);
152 if (i < ctx->offset) {
153 notmuch_thread_destroy (thread);
157 if (ctx->output == OUTPUT_THREADS) {
158 format->set_prefix (format, "thread");
159 format->string (format,
160 notmuch_thread_get_thread_id (thread));
161 format->separator (format);
162 } else { /* output == OUTPUT_SUMMARY */
163 void *ctx_quote = talloc_new (thread);
164 const char *authors = notmuch_thread_get_authors (thread);
165 const char *subject = notmuch_thread_get_subject (thread);
166 const char *thread_id = notmuch_thread_get_thread_id (thread);
167 int matched = notmuch_thread_get_matched_messages (thread);
168 int files = notmuch_thread_get_total_files (thread);
169 int total = notmuch_thread_get_total_messages (thread);
170 const char *relative_date = NULL;
171 bool first_tag = true;
173 format->begin_map (format);
175 if (ctx->sort == NOTMUCH_SORT_OLDEST_FIRST)
176 date = notmuch_thread_get_oldest_date (thread);
178 date = notmuch_thread_get_newest_date (thread);
180 relative_date = notmuch_time_relative_date (ctx_quote, date);
182 if (format->is_text_printer) {
183 /* Special case for the text formatter */
184 printf ("thread:%s %12s ",
188 printf ("[%d/%d] %s; %s (",
191 sanitize_string (ctx_quote, authors),
192 sanitize_string (ctx_quote, subject));
194 printf ("[%d/%d(%d)] %s; %s (",
198 sanitize_string (ctx_quote, authors),
199 sanitize_string (ctx_quote, subject));
201 } else { /* Structured Output */
202 format->map_key (format, "thread");
203 format->string (format, thread_id);
204 format->map_key (format, "timestamp");
205 format->integer (format, date);
206 format->map_key (format, "date_relative");
207 format->string (format, relative_date);
208 format->map_key (format, "matched");
209 format->integer (format, matched);
210 format->map_key (format, "total");
211 format->integer (format, total);
212 format->map_key (format, "authors");
213 format->string (format, authors);
214 format->map_key (format, "subject");
215 format->string (format, subject);
216 if (notmuch_format_version >= 2) {
217 char *matched_query, *unmatched_query;
218 if (get_thread_query (thread, &matched_query,
219 &unmatched_query) < 0) {
220 fprintf (stderr, "Out of memory\n");
223 format->map_key (format, "query");
224 format->begin_list (format);
226 format->string (format, matched_query);
228 format->null (format);
230 format->string (format, unmatched_query);
232 format->null (format);
233 format->end (format);
237 talloc_free (ctx_quote);
239 format->map_key (format, "tags");
240 format->begin_list (format);
242 for (tags = notmuch_thread_get_tags (thread);
243 notmuch_tags_valid (tags);
244 notmuch_tags_move_to_next (tags)) {
245 const char *tag = notmuch_tags_get (tags);
247 if (format->is_text_printer) {
248 /* Special case for the text formatter */
254 } else { /* Structured Output */
255 format->string (format, tag);
259 if (format->is_text_printer)
262 format->end (format);
263 format->end (format);
264 format->separator (format);
267 notmuch_thread_destroy (thread);
270 format->end (format);
276 new_mailbox (void *ctx, const char *name, const char *addr)
280 mailbox = talloc (ctx, mailbox_t);
284 mailbox->name = talloc_strdup (mailbox, name);
285 mailbox->addr = talloc_strdup (mailbox, addr);
292 mailbox_compare (const void *v1, const void *v2)
294 const mailbox_t *m1 = v1, *m2 = v2;
297 ret = strcmp_null (m1->name, m2->name);
299 ret = strcmp (m1->addr, m2->addr);
304 /* Returns true iff name and addr is duplicate. If not, stores the
305 * name/addr pair in order to detect subsequent duplicates. */
307 is_duplicate (const search_context_t *ctx, const char *name, const char *addr)
313 list = g_hash_table_lookup (ctx->addresses, addr);
320 l = g_list_find_custom (list, &find, mailbox_compare);
327 mailbox = new_mailbox (ctx->format, name, addr);
332 * XXX: It would be more efficient to prepend to the list, but
333 * then we'd have to store the changed list head back to the
334 * hash table. This check is here just to avoid the compiler
335 * warning for unused result.
337 if (list != g_list_append (list, mailbox))
338 INTERNAL_ERROR ("appending to list changed list head\n");
343 key = talloc_strdup (ctx->format, addr);
347 mailbox = new_mailbox (ctx->format, name, addr);
351 list = g_list_append (NULL, mailbox);
355 g_hash_table_insert (ctx->addresses, key, list);
361 print_mailbox (const search_context_t *ctx, const mailbox_t *mailbox)
363 const char *name = mailbox->name;
364 const char *addr = mailbox->addr;
365 int count = mailbox->count;
366 sprinter_t *format = ctx->format;
367 InternetAddress *ia = internet_address_mailbox_new (name, addr);
370 /* name_addr has the name part quoted if necessary. Compare
371 * 'John Doe <john@doe.com>' vs. '"Doe, John" <john@doe.com>' */
372 name_addr = internet_address_to_string (ia, NULL, false);
374 if (format->is_text_printer) {
375 if (ctx->output & OUTPUT_COUNT) {
376 format->integer (format, count);
377 format->string (format, "\t");
379 if (ctx->output & OUTPUT_ADDRESS)
380 format->string (format, addr);
382 format->string (format, name_addr);
383 format->separator (format);
385 format->begin_map (format);
386 format->map_key (format, "name");
387 format->string (format, name);
388 format->map_key (format, "address");
389 format->string (format, addr);
390 format->map_key (format, "name-addr");
391 format->string (format, name_addr);
392 if (ctx->output & OUTPUT_COUNT) {
393 format->map_key (format, "count");
394 format->integer (format, count);
396 format->end (format);
397 format->separator (format);
404 /* Print or prepare for printing addresses from InternetAddressList. */
406 process_address_list (const search_context_t *ctx,
407 InternetAddressList *list)
409 InternetAddress *address;
412 for (i = 0; i < internet_address_list_length (list); i++) {
413 address = internet_address_list_get_address (list, i);
414 if (INTERNET_ADDRESS_IS_GROUP (address)) {
415 InternetAddressGroup *group;
416 InternetAddressList *group_list;
418 group = INTERNET_ADDRESS_GROUP (address);
419 group_list = internet_address_group_get_members (group);
420 if (group_list == NULL)
423 process_address_list (ctx, group_list);
425 InternetAddressMailbox *mailbox = INTERNET_ADDRESS_MAILBOX (address);
427 .name = internet_address_get_name (address),
428 .addr = internet_address_mailbox_get_addr (mailbox),
431 /* OUTPUT_COUNT only works with deduplication */
432 if (ctx->dedup != DEDUP_NONE &&
433 is_duplicate (ctx, mbx.name, mbx.addr))
436 /* OUTPUT_COUNT and DEDUP_ADDRESS require a full pass. */
437 if (ctx->output & OUTPUT_COUNT || ctx->dedup == DEDUP_ADDRESS)
440 print_mailbox (ctx, &mbx);
445 /* Print or prepare for printing addresses from a message header. */
447 process_address_header (const search_context_t *ctx, const char *value)
449 InternetAddressList *list;
454 list = internet_address_list_parse (NULL, value);
458 process_address_list (ctx, list);
460 g_object_unref (list);
463 /* Destructor for talloc-allocated GHashTable keys and values. */
465 _talloc_free_for_g_hash (void *ptr)
471 _list_free_for_g_hash (void *ptr)
473 g_list_free_full (ptr, _talloc_free_for_g_hash);
476 /* Print the most common variant of a list of unique mailboxes, and
477 * conflate the counts. */
479 print_popular (const search_context_t *ctx, GList *list)
482 mailbox_t *mailbox = NULL, *m;
486 for (l = list; l; l = l->next) {
489 if (m->count > max) {
496 INTERNAL_ERROR ("Empty list in address hash table\n");
498 /* The original count is no longer needed, so overwrite. */
499 mailbox->count = total;
501 print_mailbox (ctx, mailbox);
505 print_list_value (void *mailbox, void *context)
507 print_mailbox (context, mailbox);
511 print_hash_value (unused (void *key), void *list, void *context)
513 const search_context_t *ctx = context;
515 if (ctx->dedup == DEDUP_ADDRESS)
516 print_popular (ctx, list);
518 g_list_foreach (list, print_list_value, context);
522 _count_filenames (notmuch_message_t *message)
524 notmuch_filenames_t *filenames;
527 filenames = notmuch_message_get_filenames (message);
529 while (notmuch_filenames_valid (filenames)) {
530 notmuch_filenames_move_to_next (filenames);
534 notmuch_filenames_destroy (filenames);
540 do_search_messages (search_context_t *ctx)
542 notmuch_message_t *message;
543 notmuch_messages_t *messages;
544 notmuch_filenames_t *filenames;
545 sprinter_t *format = ctx->format;
547 notmuch_status_t status;
549 if (ctx->offset < 0) {
551 notmuch_status_t status;
552 status = notmuch_query_count_messages (ctx->query, &count);
553 if (print_status_query ("notmuch search", ctx->query, status))
556 ctx->offset += count;
561 status = notmuch_query_search_messages (ctx->query, &messages);
562 if (print_status_query ("notmuch search", ctx->query, status))
565 format->begin_list (format);
568 notmuch_messages_valid (messages) && (ctx->limit < 0 || i < ctx->offset + ctx->limit);
569 notmuch_messages_move_to_next (messages), i++) {
573 message = notmuch_messages_get (messages);
575 if (ctx->output == OUTPUT_FILES) {
577 filenames = notmuch_message_get_filenames (message);
580 notmuch_filenames_valid (filenames);
581 notmuch_filenames_move_to_next (filenames), j++) {
582 if (ctx->dupe < 0 || ctx->dupe == j) {
583 format->string (format, notmuch_filenames_get (filenames));
584 format->separator (format);
588 notmuch_filenames_destroy ( filenames );
590 } else if (ctx->output == OUTPUT_MESSAGES) {
591 /* special case 1 for speed */
592 if (ctx->dupe <= 1 || ctx->dupe <= _count_filenames (message)) {
593 format->set_prefix (format, "id");
594 format->string (format,
595 notmuch_message_get_message_id (message));
596 format->separator (format);
599 if (ctx->output & OUTPUT_SENDER) {
602 addrs = notmuch_message_get_header (message, "from");
603 process_address_header (ctx, addrs);
606 if (ctx->output & OUTPUT_RECIPIENTS) {
607 const char *hdrs[] = { "to", "cc", "bcc" };
611 for (j = 0; j < ARRAY_SIZE (hdrs); j++) {
612 addrs = notmuch_message_get_header (message, hdrs[j]);
613 process_address_header (ctx, addrs);
618 notmuch_message_destroy (message);
621 if (ctx->addresses &&
622 (ctx->output & OUTPUT_COUNT || ctx->dedup == DEDUP_ADDRESS))
623 g_hash_table_foreach (ctx->addresses, print_hash_value, ctx);
625 notmuch_messages_destroy (messages);
627 format->end (format);
633 do_search_tags (const search_context_t *ctx)
635 notmuch_messages_t *messages = NULL;
636 notmuch_tags_t *tags;
638 sprinter_t *format = ctx->format;
639 notmuch_query_t *query = ctx->query;
640 notmuch_database_t *notmuch = ctx->notmuch;
642 /* should the following only special case if no excluded terms
645 /* Special-case query of "*" for better performance. */
646 if (strcmp (notmuch_query_get_query_string (query), "*") == 0) {
647 tags = notmuch_database_get_all_tags (notmuch);
649 notmuch_status_t status;
650 status = notmuch_query_search_messages (query, &messages);
651 if (print_status_query ("notmuch search", query, status))
654 tags = notmuch_messages_collect_tags (messages);
659 format->begin_list (format);
662 notmuch_tags_valid (tags);
663 notmuch_tags_move_to_next (tags)) {
664 tag = notmuch_tags_get (tags);
666 format->string (format, tag);
667 format->separator (format);
671 notmuch_tags_destroy (tags);
674 notmuch_messages_destroy (messages);
676 format->end (format);
682 _notmuch_search_prepare (search_context_t *ctx, int argc, char *argv[])
686 if (! ctx->talloc_ctx)
687 ctx->talloc_ctx = talloc_new (NULL);
689 switch (ctx->format_sel) {
690 case NOTMUCH_FORMAT_TEXT:
691 ctx->format = sprinter_text_create (ctx->talloc_ctx, stdout);
693 case NOTMUCH_FORMAT_TEXT0:
694 if (ctx->output == OUTPUT_SUMMARY) {
695 fprintf (stderr, "Error: --format=text0 is not compatible with --output=summary.\n");
698 ctx->format = sprinter_text0_create (ctx->talloc_ctx, stdout);
700 case NOTMUCH_FORMAT_JSON:
701 ctx->format = sprinter_json_create (ctx->talloc_ctx, stdout);
703 case NOTMUCH_FORMAT_SEXP:
704 ctx->format = sprinter_sexp_create (ctx->talloc_ctx, stdout);
707 /* this should never happen */
708 INTERNAL_ERROR ("no output format selected");
711 notmuch_exit_if_unsupported_format ();
713 query_str = query_string_from_args (ctx->notmuch, argc, argv);
714 if (query_str == NULL) {
715 fprintf (stderr, "Out of memory.\n");
718 if (*query_str == '\0') {
719 fprintf (stderr, "Error: notmuch search requires at least one search term.\n");
723 if (print_status_database ("notmuch search", ctx->notmuch,
724 notmuch_query_create_with_syntax (ctx->notmuch, query_str,
725 shared_option_query_syntax (),
729 notmuch_query_set_sort (ctx->query, ctx->sort);
731 if (ctx->exclude == NOTMUCH_EXCLUDE_FLAG && ctx->output != OUTPUT_SUMMARY) {
732 /* If we are not doing summary output there is nowhere to
733 * print the excluded flag so fall back on including the
734 * excluded messages. */
735 fprintf (stderr, "Warning: this output format cannot flag excluded messages.\n");
736 ctx->exclude = NOTMUCH_EXCLUDE_FALSE;
739 if (ctx->exclude != NOTMUCH_EXCLUDE_FALSE) {
740 notmuch_config_values_t *exclude_tags;
741 notmuch_status_t status;
743 for (exclude_tags = notmuch_config_get_values (ctx->notmuch, NOTMUCH_CONFIG_EXCLUDE_TAGS);
744 notmuch_config_values_valid (exclude_tags);
745 notmuch_config_values_move_to_next (exclude_tags)) {
747 status = notmuch_query_add_tag_exclude (ctx->query,
748 notmuch_config_values_get (exclude_tags));
749 if (status && status != NOTMUCH_STATUS_IGNORED) {
750 print_status_query ("notmuch search", ctx->query, status);
754 notmuch_query_set_omit_excluded (ctx->query, ctx->exclude);
761 _notmuch_search_cleanup (search_context_t *ctx)
763 notmuch_query_destroy (ctx->query);
764 notmuch_database_destroy (ctx->notmuch);
766 talloc_free (ctx->format);
769 static search_context_t search_context = {
770 .format_sel = NOTMUCH_FORMAT_TEXT,
771 .exclude = NOTMUCH_EXCLUDE_TRUE,
772 .sort = NOTMUCH_SORT_NEWEST_FIRST,
773 .query_syntax = NOTMUCH_QUERY_SYNTAX_XAPIAN,
776 .limit = -1, /* unlimited */
778 .dedup = DEDUP_MAILBOX,
781 static const notmuch_opt_desc_t common_options[] = {
782 { .opt_keyword = &search_context.sort, .name = "sort", .keywords =
783 (notmuch_keyword_t []){ { "oldest-first", NOTMUCH_SORT_OLDEST_FIRST },
784 { "newest-first", NOTMUCH_SORT_NEWEST_FIRST },
786 { .opt_keyword = &search_context.format_sel, .name = "format", .keywords =
787 (notmuch_keyword_t []){ { "json", NOTMUCH_FORMAT_JSON },
788 { "sexp", NOTMUCH_FORMAT_SEXP },
789 { "text", NOTMUCH_FORMAT_TEXT },
790 { "text0", NOTMUCH_FORMAT_TEXT0 },
792 { .opt_int = ¬much_format_version, .name = "format-version" },
797 notmuch_search_command (notmuch_database_t *notmuch, int argc, char *argv[])
799 search_context_t *ctx = &search_context;
802 notmuch_opt_desc_t options[] = {
803 { .opt_keyword = &ctx->output, .name = "output", .keywords =
804 (notmuch_keyword_t []){ { "summary", OUTPUT_SUMMARY },
805 { "threads", OUTPUT_THREADS },
806 { "messages", OUTPUT_MESSAGES },
807 { "files", OUTPUT_FILES },
808 { "tags", OUTPUT_TAGS },
810 { .opt_keyword = &ctx->exclude, .name = "exclude", .keywords =
811 (notmuch_keyword_t []){ { "true", NOTMUCH_EXCLUDE_TRUE },
812 { "false", NOTMUCH_EXCLUDE_FALSE },
813 { "flag", NOTMUCH_EXCLUDE_FLAG },
814 { "all", NOTMUCH_EXCLUDE_ALL },
816 { .opt_int = &ctx->offset, .name = "offset" },
817 { .opt_int = &ctx->limit, .name = "limit" },
818 { .opt_int = &ctx->dupe, .name = "duplicate" },
819 { .opt_inherit = common_options },
820 { .opt_inherit = notmuch_shared_options },
824 ctx->notmuch = notmuch;
825 ctx->output = OUTPUT_SUMMARY;
826 opt_index = parse_arguments (argc, argv, options, 1);
830 notmuch_process_shared_options (notmuch, argv[0]);
832 if (ctx->output != OUTPUT_FILES && ctx->output != OUTPUT_MESSAGES &&
835 "Error: --duplicate=N is only supported with --output=files and --output=messages.\n");
839 if (_notmuch_search_prepare (ctx, argc - opt_index, argv + opt_index))
842 switch (ctx->output) {
845 ret = do_search_threads (ctx);
847 case OUTPUT_MESSAGES:
849 ret = do_search_messages (ctx);
852 ret = do_search_tags (ctx);
855 INTERNAL_ERROR ("Unexpected output");
858 _notmuch_search_cleanup (ctx);
860 return ret ? EXIT_FAILURE : EXIT_SUCCESS;
864 notmuch_address_command (notmuch_database_t *notmuch, int argc, char *argv[])
866 search_context_t *ctx = &search_context;
869 notmuch_opt_desc_t options[] = {
870 { .opt_flags = &ctx->output, .name = "output", .keywords =
871 (notmuch_keyword_t []){ { "sender", OUTPUT_SENDER },
872 { "recipients", OUTPUT_RECIPIENTS },
873 { "count", OUTPUT_COUNT },
874 { "address", OUTPUT_ADDRESS },
876 { .opt_keyword = &ctx->exclude, .name = "exclude", .keywords =
877 (notmuch_keyword_t []){ { "true", NOTMUCH_EXCLUDE_TRUE },
878 { "false", NOTMUCH_EXCLUDE_FALSE },
880 { .opt_keyword = &ctx->dedup, .name = "deduplicate", .keywords =
881 (notmuch_keyword_t []){ { "no", DEDUP_NONE },
882 { "mailbox", DEDUP_MAILBOX },
883 { "address", DEDUP_ADDRESS },
885 { .opt_inherit = common_options },
886 { .opt_inherit = notmuch_shared_options },
890 ctx->notmuch = notmuch;
892 opt_index = parse_arguments (argc, argv, options, 1);
896 notmuch_process_shared_options (notmuch, argv[0]);
898 if (! (ctx->output & (OUTPUT_SENDER | OUTPUT_RECIPIENTS)))
899 ctx->output |= OUTPUT_SENDER;
901 if (ctx->output & OUTPUT_COUNT && ctx->dedup == DEDUP_NONE) {
902 fprintf (stderr, "--output=count is not applicable with --deduplicate=no\n");
906 if (_notmuch_search_prepare (ctx, argc - opt_index, argv + opt_index))
909 ctx->addresses = g_hash_table_new_full (strcase_hash, strcase_equal,
910 _talloc_free_for_g_hash,
911 _list_free_for_g_hash);
913 /* The order is not guaranteed if a full pass is required, so go
915 if (ctx->output & OUTPUT_COUNT || ctx->dedup == DEDUP_ADDRESS)
916 notmuch_query_set_sort (ctx->query, NOTMUCH_SORT_UNSORTED);
918 ret = do_search_messages (ctx);
920 g_hash_table_unref (ctx->addresses);
923 _notmuch_search_cleanup (ctx);
925 return ret ? EXIT_FAILURE : EXIT_SUCCESS;