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;
59 notmuch_query_t *query;
65 GHashTable *addresses;
75 /* Return two stable query strings that identify exactly the matched
76 * and unmatched messages currently in thread. If there are no
77 * matched or unmatched messages, the returned buffers will be
80 get_thread_query (notmuch_thread_t *thread,
81 char **matched_out, char **unmatched_out)
83 notmuch_messages_t *messages;
85 size_t escaped_len = 0;
87 *matched_out = *unmatched_out = NULL;
89 for (messages = notmuch_thread_get_messages (thread);
90 notmuch_messages_valid (messages);
91 notmuch_messages_move_to_next (messages)) {
92 notmuch_message_t *message = notmuch_messages_get (messages);
93 const char *mid = notmuch_message_get_message_id (message);
94 notmuch_bool_t is_set;
97 if (notmuch_message_get_flag_st (message, NOTMUCH_MESSAGE_FLAG_MATCH, &is_set))
99 /* Determine which query buffer to extend */
100 buf = is_set ? matched_out : unmatched_out;
101 /* Add this message's id: query. Since "id" is an exclusive
102 * prefix, it is implicitly 'or'd together, so we only need to
103 * join queries with a space. */
104 if (make_boolean_term (thread, "id", mid, &escaped, &escaped_len) < 0)
107 *buf = talloc_asprintf_append_buffer (*buf, " %s", escaped);
109 *buf = talloc_strdup (thread, escaped);
113 talloc_free (escaped);
118 do_search_threads (search_context_t *ctx)
120 notmuch_thread_t *thread;
121 notmuch_threads_t *threads;
122 notmuch_tags_t *tags;
123 sprinter_t *format = ctx->format;
126 notmuch_status_t status;
128 if (ctx->offset < 0) {
130 notmuch_status_t status;
131 status = notmuch_query_count_threads (ctx->query, &count);
132 if (print_status_query ("notmuch search", ctx->query, status))
135 ctx->offset += count;
140 status = notmuch_query_search_threads (ctx->query, &threads);
141 if (print_status_query ("notmuch search", ctx->query, status))
144 format->begin_list (format);
147 notmuch_threads_valid (threads) && (ctx->limit < 0 || i < ctx->offset + ctx->limit);
148 notmuch_threads_move_to_next (threads), i++) {
149 thread = notmuch_threads_get (threads);
151 if (i < ctx->offset) {
152 notmuch_thread_destroy (thread);
156 if (ctx->output == OUTPUT_THREADS) {
157 format->set_prefix (format, "thread");
158 format->string (format,
159 notmuch_thread_get_thread_id (thread));
160 format->separator (format);
161 } else { /* output == OUTPUT_SUMMARY */
162 void *ctx_quote = talloc_new (thread);
163 const char *authors = notmuch_thread_get_authors (thread);
164 const char *subject = notmuch_thread_get_subject (thread);
165 const char *thread_id = notmuch_thread_get_thread_id (thread);
166 int matched = notmuch_thread_get_matched_messages (thread);
167 int files = notmuch_thread_get_total_files (thread);
168 int total = notmuch_thread_get_total_messages (thread);
169 const char *relative_date = NULL;
170 bool first_tag = true;
172 format->begin_map (format);
174 if (ctx->sort == NOTMUCH_SORT_OLDEST_FIRST)
175 date = notmuch_thread_get_oldest_date (thread);
177 date = notmuch_thread_get_newest_date (thread);
179 relative_date = notmuch_time_relative_date (ctx_quote, date);
181 if (format->is_text_printer) {
182 /* Special case for the text formatter */
183 printf ("thread:%s %12s ",
187 printf ("[%d/%d] %s; %s (",
190 sanitize_string (ctx_quote, authors),
191 sanitize_string (ctx_quote, subject));
193 printf ("[%d/%d(%d)] %s; %s (",
197 sanitize_string (ctx_quote, authors),
198 sanitize_string (ctx_quote, subject));
200 } else { /* Structured Output */
201 format->map_key (format, "thread");
202 format->string (format, thread_id);
203 format->map_key (format, "timestamp");
204 format->integer (format, date);
205 format->map_key (format, "date_relative");
206 format->string (format, relative_date);
207 format->map_key (format, "matched");
208 format->integer (format, matched);
209 format->map_key (format, "total");
210 format->integer (format, total);
211 format->map_key (format, "authors");
212 format->string (format, authors);
213 format->map_key (format, "subject");
214 format->string (format, subject);
215 if (notmuch_format_version >= 2) {
216 char *matched_query, *unmatched_query;
217 if (get_thread_query (thread, &matched_query,
218 &unmatched_query) < 0) {
219 fprintf (stderr, "Out of memory\n");
222 format->map_key (format, "query");
223 format->begin_list (format);
225 format->string (format, matched_query);
227 format->null (format);
229 format->string (format, unmatched_query);
231 format->null (format);
232 format->end (format);
236 talloc_free (ctx_quote);
238 format->map_key (format, "tags");
239 format->begin_list (format);
241 for (tags = notmuch_thread_get_tags (thread);
242 notmuch_tags_valid (tags);
243 notmuch_tags_move_to_next (tags)) {
244 const char *tag = notmuch_tags_get (tags);
246 if (format->is_text_printer) {
247 /* Special case for the text formatter */
253 } else { /* Structured Output */
254 format->string (format, tag);
258 if (format->is_text_printer)
261 format->end (format);
262 format->end (format);
263 format->separator (format);
266 notmuch_thread_destroy (thread);
269 format->end (format);
275 new_mailbox (void *ctx, const char *name, const char *addr)
279 mailbox = talloc (ctx, mailbox_t);
283 mailbox->name = talloc_strdup (mailbox, name);
284 mailbox->addr = talloc_strdup (mailbox, addr);
291 mailbox_compare (const void *v1, const void *v2)
293 const mailbox_t *m1 = v1, *m2 = v2;
296 ret = strcmp_null (m1->name, m2->name);
298 ret = strcmp (m1->addr, m2->addr);
303 /* Returns true iff name and addr is duplicate. If not, stores the
304 * name/addr pair in order to detect subsequent duplicates. */
306 is_duplicate (const search_context_t *ctx, const char *name, const char *addr)
312 list = g_hash_table_lookup (ctx->addresses, addr);
319 l = g_list_find_custom (list, &find, mailbox_compare);
326 mailbox = new_mailbox (ctx->format, name, addr);
331 * XXX: It would be more efficient to prepend to the list, but
332 * then we'd have to store the changed list head back to the
333 * hash table. This check is here just to avoid the compiler
334 * warning for unused result.
336 if (list != g_list_append (list, mailbox))
337 INTERNAL_ERROR ("appending to list changed list head\n");
342 key = talloc_strdup (ctx->format, addr);
346 mailbox = new_mailbox (ctx->format, name, addr);
350 list = g_list_append (NULL, mailbox);
354 g_hash_table_insert (ctx->addresses, key, list);
360 print_mailbox (const search_context_t *ctx, const mailbox_t *mailbox)
362 const char *name = mailbox->name;
363 const char *addr = mailbox->addr;
364 int count = mailbox->count;
365 sprinter_t *format = ctx->format;
366 InternetAddress *ia = internet_address_mailbox_new (name, addr);
369 /* name_addr has the name part quoted if necessary. Compare
370 * 'John Doe <john@doe.com>' vs. '"Doe, John" <john@doe.com>' */
371 name_addr = internet_address_to_string (ia, NULL, false);
373 if (format->is_text_printer) {
374 if (ctx->output & OUTPUT_COUNT) {
375 format->integer (format, count);
376 format->string (format, "\t");
378 if (ctx->output & OUTPUT_ADDRESS)
379 format->string (format, addr);
381 format->string (format, name_addr);
382 format->separator (format);
384 format->begin_map (format);
385 format->map_key (format, "name");
386 format->string (format, name);
387 format->map_key (format, "address");
388 format->string (format, addr);
389 format->map_key (format, "name-addr");
390 format->string (format, name_addr);
391 if (ctx->output & OUTPUT_COUNT) {
392 format->map_key (format, "count");
393 format->integer (format, count);
395 format->end (format);
396 format->separator (format);
403 /* Print or prepare for printing addresses from InternetAddressList. */
405 process_address_list (const search_context_t *ctx,
406 InternetAddressList *list)
408 InternetAddress *address;
411 for (i = 0; i < internet_address_list_length (list); i++) {
412 address = internet_address_list_get_address (list, i);
413 if (INTERNET_ADDRESS_IS_GROUP (address)) {
414 InternetAddressGroup *group;
415 InternetAddressList *group_list;
417 group = INTERNET_ADDRESS_GROUP (address);
418 group_list = internet_address_group_get_members (group);
419 if (group_list == NULL)
422 process_address_list (ctx, group_list);
424 InternetAddressMailbox *mailbox = INTERNET_ADDRESS_MAILBOX (address);
426 .name = internet_address_get_name (address),
427 .addr = internet_address_mailbox_get_addr (mailbox),
430 /* OUTPUT_COUNT only works with deduplication */
431 if (ctx->dedup != DEDUP_NONE &&
432 is_duplicate (ctx, mbx.name, mbx.addr))
435 /* OUTPUT_COUNT and DEDUP_ADDRESS require a full pass. */
436 if (ctx->output & OUTPUT_COUNT || ctx->dedup == DEDUP_ADDRESS)
439 print_mailbox (ctx, &mbx);
444 /* Print or prepare for printing addresses from a message header. */
446 process_address_header (const search_context_t *ctx, const char *value)
448 InternetAddressList *list;
453 list = internet_address_list_parse (NULL, value);
457 process_address_list (ctx, list);
459 g_object_unref (list);
462 /* Destructor for talloc-allocated GHashTable keys and values. */
464 _talloc_free_for_g_hash (void *ptr)
470 _list_free_for_g_hash (void *ptr)
472 g_list_free_full (ptr, _talloc_free_for_g_hash);
475 /* Print the most common variant of a list of unique mailboxes, and
476 * conflate the counts. */
478 print_popular (const search_context_t *ctx, GList *list)
481 mailbox_t *mailbox = NULL, *m;
485 for (l = list; l; l = l->next) {
488 if (m->count > max) {
495 INTERNAL_ERROR ("Empty list in address hash table\n");
497 /* The original count is no longer needed, so overwrite. */
498 mailbox->count = total;
500 print_mailbox (ctx, mailbox);
504 print_list_value (void *mailbox, void *context)
506 print_mailbox (context, mailbox);
510 print_hash_value (unused (void *key), void *list, void *context)
512 const search_context_t *ctx = context;
514 if (ctx->dedup == DEDUP_ADDRESS)
515 print_popular (ctx, list);
517 g_list_foreach (list, print_list_value, context);
521 _count_filenames (notmuch_message_t *message)
523 notmuch_filenames_t *filenames;
526 filenames = notmuch_message_get_filenames (message);
528 while (notmuch_filenames_valid (filenames)) {
529 notmuch_filenames_move_to_next (filenames);
533 notmuch_filenames_destroy (filenames);
539 do_search_messages (search_context_t *ctx)
541 notmuch_message_t *message;
542 notmuch_messages_t *messages;
543 notmuch_filenames_t *filenames;
544 sprinter_t *format = ctx->format;
546 notmuch_status_t status;
548 if (ctx->offset < 0) {
550 notmuch_status_t status;
551 status = notmuch_query_count_messages (ctx->query, &count);
552 if (print_status_query ("notmuch search", ctx->query, status))
555 ctx->offset += count;
560 status = notmuch_query_search_messages (ctx->query, &messages);
561 if (print_status_query ("notmuch search", ctx->query, status))
564 format->begin_list (format);
567 notmuch_messages_valid (messages) && (ctx->limit < 0 || i < ctx->offset + ctx->limit);
568 notmuch_messages_move_to_next (messages), i++) {
572 message = notmuch_messages_get (messages);
574 if (ctx->output == OUTPUT_FILES) {
576 filenames = notmuch_message_get_filenames (message);
579 notmuch_filenames_valid (filenames);
580 notmuch_filenames_move_to_next (filenames), j++) {
581 if (ctx->dupe < 0 || ctx->dupe == j) {
582 format->string (format, notmuch_filenames_get (filenames));
583 format->separator (format);
587 notmuch_filenames_destroy ( filenames );
589 } else if (ctx->output == OUTPUT_MESSAGES) {
590 /* special case 1 for speed */
591 if (ctx->dupe <= 1 || ctx->dupe <= _count_filenames (message)) {
592 format->set_prefix (format, "id");
593 format->string (format,
594 notmuch_message_get_message_id (message));
595 format->separator (format);
598 if (ctx->output & OUTPUT_SENDER) {
601 addrs = notmuch_message_get_header (message, "from");
602 process_address_header (ctx, addrs);
605 if (ctx->output & OUTPUT_RECIPIENTS) {
606 const char *hdrs[] = { "to", "cc", "bcc" };
610 for (j = 0; j < ARRAY_SIZE (hdrs); j++) {
611 addrs = notmuch_message_get_header (message, hdrs[j]);
612 process_address_header (ctx, addrs);
617 notmuch_message_destroy (message);
620 if (ctx->addresses &&
621 (ctx->output & OUTPUT_COUNT || ctx->dedup == DEDUP_ADDRESS))
622 g_hash_table_foreach (ctx->addresses, print_hash_value, ctx);
624 notmuch_messages_destroy (messages);
626 format->end (format);
632 do_search_tags (const search_context_t *ctx)
634 notmuch_messages_t *messages = NULL;
635 notmuch_tags_t *tags;
637 sprinter_t *format = ctx->format;
638 notmuch_query_t *query = ctx->query;
639 notmuch_database_t *notmuch = ctx->notmuch;
641 /* should the following only special case if no excluded terms
644 /* Special-case query of "*" for better performance. */
645 if (strcmp (notmuch_query_get_query_string (query), "*") == 0) {
646 tags = notmuch_database_get_all_tags (notmuch);
648 notmuch_status_t status;
649 status = notmuch_query_search_messages (query, &messages);
650 if (print_status_query ("notmuch search", query, status))
653 tags = notmuch_messages_collect_tags (messages);
658 format->begin_list (format);
661 notmuch_tags_valid (tags);
662 notmuch_tags_move_to_next (tags)) {
663 tag = notmuch_tags_get (tags);
665 format->string (format, tag);
666 format->separator (format);
670 notmuch_tags_destroy (tags);
673 notmuch_messages_destroy (messages);
675 format->end (format);
681 _notmuch_search_prepare (search_context_t *ctx, int argc, char *argv[])
685 if (! ctx->talloc_ctx)
686 ctx->talloc_ctx = talloc_new (NULL);
688 switch (ctx->format_sel) {
689 case NOTMUCH_FORMAT_TEXT:
690 ctx->format = sprinter_text_create (ctx->talloc_ctx, stdout);
692 case NOTMUCH_FORMAT_TEXT0:
693 if (ctx->output == OUTPUT_SUMMARY) {
694 fprintf (stderr, "Error: --format=text0 is not compatible with --output=summary.\n");
697 ctx->format = sprinter_text0_create (ctx->talloc_ctx, stdout);
699 case NOTMUCH_FORMAT_JSON:
700 ctx->format = sprinter_json_create (ctx->talloc_ctx, stdout);
702 case NOTMUCH_FORMAT_SEXP:
703 ctx->format = sprinter_sexp_create (ctx->talloc_ctx, stdout);
706 /* this should never happen */
707 INTERNAL_ERROR ("no output format selected");
710 notmuch_exit_if_unsupported_format ();
712 notmuch_exit_if_unmatched_db_uuid (ctx->notmuch);
714 query_str = query_string_from_args (ctx->notmuch, argc, argv);
715 if (query_str == NULL) {
716 fprintf (stderr, "Out of memory.\n");
719 if (*query_str == '\0') {
720 fprintf (stderr, "Error: notmuch search requires at least one search term.\n");
724 ctx->query = notmuch_query_create (ctx->notmuch, query_str);
725 if (ctx->query == NULL) {
726 fprintf (stderr, "Out of memory\n");
730 notmuch_query_set_sort (ctx->query, ctx->sort);
732 if (ctx->exclude == NOTMUCH_EXCLUDE_FLAG && ctx->output != OUTPUT_SUMMARY) {
733 /* If we are not doing summary output there is nowhere to
734 * print the excluded flag so fall back on including the
735 * excluded messages. */
736 fprintf (stderr, "Warning: this output format cannot flag excluded messages.\n");
737 ctx->exclude = NOTMUCH_EXCLUDE_FALSE;
740 if (ctx->exclude != NOTMUCH_EXCLUDE_FALSE) {
741 notmuch_config_values_t *exclude_tags;
742 notmuch_status_t status;
744 for (exclude_tags = notmuch_config_get_values (ctx->notmuch, NOTMUCH_CONFIG_EXCLUDE_TAGS);
745 notmuch_config_values_valid (exclude_tags);
746 notmuch_config_values_move_to_next (exclude_tags)) {
748 status = notmuch_query_add_tag_exclude (ctx->query,
749 notmuch_config_values_get (exclude_tags));
750 if (status && status != NOTMUCH_STATUS_IGNORED) {
751 print_status_query ("notmuch search", ctx->query, status);
755 notmuch_query_set_omit_excluded (ctx->query, ctx->exclude);
762 _notmuch_search_cleanup (search_context_t *ctx)
764 notmuch_query_destroy (ctx->query);
765 notmuch_database_destroy (ctx->notmuch);
767 talloc_free (ctx->format);
770 static search_context_t search_context = {
771 .format_sel = NOTMUCH_FORMAT_TEXT,
772 .exclude = NOTMUCH_EXCLUDE_TRUE,
773 .sort = NOTMUCH_SORT_NEWEST_FIRST,
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 (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 (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;