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;
58 notmuch_query_t *query;
64 GHashTable *addresses;
74 /* Return two stable query strings that identify exactly the matched
75 * and unmatched messages currently in thread. If there are no
76 * matched or unmatched messages, the returned buffers will be
79 get_thread_query (notmuch_thread_t *thread,
80 char **matched_out, char **unmatched_out)
82 notmuch_messages_t *messages;
84 size_t escaped_len = 0;
86 *matched_out = *unmatched_out = NULL;
88 for (messages = notmuch_thread_get_messages (thread);
89 notmuch_messages_valid (messages);
90 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 /* Determine which query buffer to extend */
95 char **buf = notmuch_message_get_flag (
96 message, NOTMUCH_MESSAGE_FLAG_MATCH) ? matched_out : unmatched_out;
97 /* Add this message's id: query. Since "id" is an exclusive
98 * prefix, it is implicitly 'or'd together, so we only need to
99 * join queries with a space. */
100 if (make_boolean_term (thread, "id", mid, &escaped, &escaped_len) < 0)
103 *buf = talloc_asprintf_append_buffer (*buf, " %s", escaped);
105 *buf = talloc_strdup (thread, escaped);
109 talloc_free (escaped);
114 do_search_threads (search_context_t *ctx)
116 notmuch_thread_t *thread;
117 notmuch_threads_t *threads;
118 notmuch_tags_t *tags;
119 sprinter_t *format = ctx->format;
122 notmuch_status_t status;
124 if (ctx->offset < 0) {
126 notmuch_status_t status;
127 status = notmuch_query_count_threads (ctx->query, &count);
128 if (print_status_query ("notmuch search", ctx->query, status))
131 ctx->offset += count;
136 status = notmuch_query_search_threads (ctx->query, &threads);
137 if (print_status_query("notmuch search", ctx->query, status))
140 format->begin_list (format);
143 notmuch_threads_valid (threads) && (ctx->limit < 0 || i < ctx->offset + ctx->limit);
144 notmuch_threads_move_to_next (threads), i++)
146 thread = notmuch_threads_get (threads);
148 if (i < ctx->offset) {
149 notmuch_thread_destroy (thread);
153 if (ctx->output == OUTPUT_THREADS) {
154 format->set_prefix (format, "thread");
155 format->string (format,
156 notmuch_thread_get_thread_id (thread));
157 format->separator (format);
158 } else { /* output == OUTPUT_SUMMARY */
159 void *ctx_quote = talloc_new (thread);
160 const char *authors = notmuch_thread_get_authors (thread);
161 const char *subject = notmuch_thread_get_subject (thread);
162 const char *thread_id = notmuch_thread_get_thread_id (thread);
163 int matched = notmuch_thread_get_matched_messages (thread);
164 int files = notmuch_thread_get_total_files (thread);
165 int total = notmuch_thread_get_total_messages (thread);
166 const char *relative_date = NULL;
167 bool first_tag = true;
169 format->begin_map (format);
171 if (ctx->sort == NOTMUCH_SORT_OLDEST_FIRST)
172 date = notmuch_thread_get_oldest_date (thread);
174 date = notmuch_thread_get_newest_date (thread);
176 relative_date = notmuch_time_relative_date (ctx_quote, date);
178 if (format->is_text_printer) {
179 /* Special case for the text formatter */
180 printf ("thread:%s %12s ",
184 printf ("[%d/%d] %s; %s (",
187 sanitize_string (ctx_quote, authors),
188 sanitize_string (ctx_quote, subject));
190 printf ("[%d/%d(%d)] %s; %s (",
194 sanitize_string (ctx_quote, authors),
195 sanitize_string (ctx_quote, subject));
197 } else { /* Structured Output */
198 format->map_key (format, "thread");
199 format->string (format, thread_id);
200 format->map_key (format, "timestamp");
201 format->integer (format, date);
202 format->map_key (format, "date_relative");
203 format->string (format, relative_date);
204 format->map_key (format, "matched");
205 format->integer (format, matched);
206 format->map_key (format, "total");
207 format->integer (format, total);
208 format->map_key (format, "authors");
209 format->string (format, authors);
210 format->map_key (format, "subject");
211 format->string (format, subject);
212 if (notmuch_format_version >= 2) {
213 char *matched_query, *unmatched_query;
214 if (get_thread_query (thread, &matched_query,
215 &unmatched_query) < 0) {
216 fprintf (stderr, "Out of memory\n");
219 format->map_key (format, "query");
220 format->begin_list (format);
222 format->string (format, matched_query);
224 format->null (format);
226 format->string (format, unmatched_query);
228 format->null (format);
229 format->end (format);
233 talloc_free (ctx_quote);
235 format->map_key (format, "tags");
236 format->begin_list (format);
238 for (tags = notmuch_thread_get_tags (thread);
239 notmuch_tags_valid (tags);
240 notmuch_tags_move_to_next (tags))
242 const char *tag = notmuch_tags_get (tags);
244 if (format->is_text_printer) {
245 /* Special case for the text formatter */
251 } else { /* Structured Output */
252 format->string (format, tag);
256 if (format->is_text_printer)
259 format->end (format);
260 format->end (format);
261 format->separator (format);
264 notmuch_thread_destroy (thread);
267 format->end (format);
272 static mailbox_t *new_mailbox (void *ctx, const char *name, const char *addr)
276 mailbox = talloc (ctx, mailbox_t);
280 mailbox->name = talloc_strdup (mailbox, name);
281 mailbox->addr = talloc_strdup (mailbox, addr);
287 static int mailbox_compare (const void *v1, const void *v2)
289 const mailbox_t *m1 = v1, *m2 = v2;
292 ret = strcmp_null (m1->name, m2->name);
294 ret = strcmp (m1->addr, m2->addr);
299 /* Returns true iff name and addr is duplicate. If not, stores the
300 * name/addr pair in order to detect subsequent duplicates. */
302 is_duplicate (const search_context_t *ctx, const char *name, const char *addr)
308 list = g_hash_table_lookup (ctx->addresses, addr);
315 l = g_list_find_custom (list, &find, mailbox_compare);
322 mailbox = new_mailbox (ctx->format, name, addr);
327 * XXX: It would be more efficient to prepend to the list, but
328 * then we'd have to store the changed list head back to the
329 * hash table. This check is here just to avoid the compiler
330 * warning for unused result.
332 if (list != g_list_append (list, mailbox))
333 INTERNAL_ERROR ("appending to list changed list head\n");
338 key = talloc_strdup (ctx->format, addr);
342 mailbox = new_mailbox (ctx->format, name, addr);
346 list = g_list_append (NULL, mailbox);
350 g_hash_table_insert (ctx->addresses, key, list);
356 print_mailbox (const search_context_t *ctx, const mailbox_t *mailbox)
358 const char *name = mailbox->name;
359 const char *addr = mailbox->addr;
360 int count = mailbox->count;
361 sprinter_t *format = ctx->format;
362 InternetAddress *ia = internet_address_mailbox_new (name, addr);
365 /* name_addr has the name part quoted if necessary. Compare
366 * 'John Doe <john@doe.com>' vs. '"Doe, John" <john@doe.com>' */
367 name_addr = internet_address_to_string (ia, NULL, false);
369 if (format->is_text_printer) {
370 if (ctx->output & OUTPUT_COUNT) {
371 format->integer (format, count);
372 format->string (format, "\t");
374 if (ctx->output & OUTPUT_ADDRESS)
375 format->string (format, addr);
377 format->string (format, name_addr);
378 format->separator (format);
380 format->begin_map (format);
381 format->map_key (format, "name");
382 format->string (format, name);
383 format->map_key (format, "address");
384 format->string (format, addr);
385 format->map_key (format, "name-addr");
386 format->string (format, name_addr);
387 if (ctx->output & OUTPUT_COUNT) {
388 format->map_key (format, "count");
389 format->integer (format, count);
391 format->end (format);
392 format->separator (format);
399 /* Print or prepare for printing addresses from InternetAddressList. */
401 process_address_list (const search_context_t *ctx,
402 InternetAddressList *list)
404 InternetAddress *address;
407 for (i = 0; i < internet_address_list_length (list); i++) {
408 address = internet_address_list_get_address (list, i);
409 if (INTERNET_ADDRESS_IS_GROUP (address)) {
410 InternetAddressGroup *group;
411 InternetAddressList *group_list;
413 group = INTERNET_ADDRESS_GROUP (address);
414 group_list = internet_address_group_get_members (group);
415 if (group_list == NULL)
418 process_address_list (ctx, group_list);
420 InternetAddressMailbox *mailbox = INTERNET_ADDRESS_MAILBOX (address);
422 .name = internet_address_get_name (address),
423 .addr = internet_address_mailbox_get_addr (mailbox),
426 /* OUTPUT_COUNT only works with deduplication */
427 if (ctx->dedup != DEDUP_NONE &&
428 is_duplicate (ctx, mbx.name, mbx.addr))
431 /* OUTPUT_COUNT and DEDUP_ADDRESS require a full pass. */
432 if (ctx->output & OUTPUT_COUNT || ctx->dedup == DEDUP_ADDRESS)
435 print_mailbox (ctx, &mbx);
440 /* Print or prepare for printing addresses from a message header. */
442 process_address_header (const search_context_t *ctx, const char *value)
444 InternetAddressList *list;
449 list = internet_address_list_parse (NULL, value);
453 process_address_list (ctx, list);
455 g_object_unref (list);
458 /* Destructor for talloc-allocated GHashTable keys and values. */
460 _talloc_free_for_g_hash (void *ptr)
466 _list_free_for_g_hash (void *ptr)
468 g_list_free_full (ptr, _talloc_free_for_g_hash);
471 /* Print the most common variant of a list of unique mailboxes, and
472 * conflate the counts. */
474 print_popular (const search_context_t *ctx, GList *list)
477 mailbox_t *mailbox = NULL, *m;
481 for (l = list; l; l = l->next) {
484 if (m->count > max) {
491 INTERNAL_ERROR("Empty list in address hash table\n");
493 /* The original count is no longer needed, so overwrite. */
494 mailbox->count = total;
496 print_mailbox (ctx, mailbox);
500 print_list_value (void *mailbox, void *context)
502 print_mailbox (context, mailbox);
506 print_hash_value (unused (void *key), void *list, void *context)
508 const search_context_t *ctx = context;
510 if (ctx->dedup == DEDUP_ADDRESS)
511 print_popular (ctx, list);
513 g_list_foreach (list, print_list_value, context);
517 _count_filenames (notmuch_message_t *message)
519 notmuch_filenames_t *filenames;
522 filenames = notmuch_message_get_filenames (message);
524 while (notmuch_filenames_valid (filenames)) {
525 notmuch_filenames_move_to_next (filenames);
529 notmuch_filenames_destroy (filenames);
535 do_search_messages (search_context_t *ctx)
537 notmuch_message_t *message;
538 notmuch_messages_t *messages;
539 notmuch_filenames_t *filenames;
540 sprinter_t *format = ctx->format;
542 notmuch_status_t status;
544 if (ctx->offset < 0) {
546 notmuch_status_t status;
547 status = notmuch_query_count_messages (ctx->query, &count);
548 if (print_status_query ("notmuch search", ctx->query, status))
551 ctx->offset += count;
556 status = notmuch_query_search_messages (ctx->query, &messages);
557 if (print_status_query ("notmuch search", ctx->query, status))
560 format->begin_list (format);
563 notmuch_messages_valid (messages) && (ctx->limit < 0 || i < ctx->offset + ctx->limit);
564 notmuch_messages_move_to_next (messages), i++)
569 message = notmuch_messages_get (messages);
571 if (ctx->output == OUTPUT_FILES) {
573 filenames = notmuch_message_get_filenames (message);
576 notmuch_filenames_valid (filenames);
577 notmuch_filenames_move_to_next (filenames), j++)
579 if (ctx->dupe < 0 || ctx->dupe == j) {
580 format->string (format, notmuch_filenames_get (filenames));
581 format->separator (format);
585 notmuch_filenames_destroy( filenames );
587 } else if (ctx->output == OUTPUT_MESSAGES) {
588 /* special case 1 for speed */
589 if (ctx->dupe <= 1 || ctx->dupe <= _count_filenames (message)) {
590 format->set_prefix (format, "id");
591 format->string (format,
592 notmuch_message_get_message_id (message));
593 format->separator (format);
596 if (ctx->output & OUTPUT_SENDER) {
599 addrs = notmuch_message_get_header (message, "from");
600 process_address_header (ctx, addrs);
603 if (ctx->output & OUTPUT_RECIPIENTS) {
604 const char *hdrs[] = { "to", "cc", "bcc" };
608 for (j = 0; j < ARRAY_SIZE (hdrs); j++) {
609 addrs = notmuch_message_get_header (message, hdrs[j]);
610 process_address_header (ctx, addrs);
615 notmuch_message_destroy (message);
618 if (ctx->addresses &&
619 (ctx->output & OUTPUT_COUNT || ctx->dedup == DEDUP_ADDRESS))
620 g_hash_table_foreach (ctx->addresses, print_hash_value, ctx);
622 notmuch_messages_destroy (messages);
624 format->end (format);
630 do_search_tags (const search_context_t *ctx)
632 notmuch_messages_t *messages = NULL;
633 notmuch_tags_t *tags;
635 sprinter_t *format = ctx->format;
636 notmuch_query_t *query = ctx->query;
637 notmuch_database_t *notmuch = ctx->notmuch;
639 /* should the following only special case if no excluded terms
642 /* Special-case query of "*" for better performance. */
643 if (strcmp (notmuch_query_get_query_string (query), "*") == 0) {
644 tags = notmuch_database_get_all_tags (notmuch);
646 notmuch_status_t status;
647 status = notmuch_query_search_messages (query, &messages);
648 if (print_status_query ("notmuch search", query, status))
651 tags = notmuch_messages_collect_tags (messages);
656 format->begin_list (format);
659 notmuch_tags_valid (tags);
660 notmuch_tags_move_to_next (tags))
662 tag = notmuch_tags_get (tags);
664 format->string (format, tag);
665 format->separator (format);
669 notmuch_tags_destroy (tags);
672 notmuch_messages_destroy (messages);
674 format->end (format);
680 _notmuch_search_prepare (search_context_t *ctx, notmuch_config_t *config, int argc, char *argv[])
684 char *status_string = NULL;
686 switch (ctx->format_sel) {
687 case NOTMUCH_FORMAT_TEXT:
688 ctx->format = sprinter_text_create (config, stdout);
690 case NOTMUCH_FORMAT_TEXT0:
691 if (ctx->output == OUTPUT_SUMMARY) {
692 fprintf (stderr, "Error: --format=text0 is not compatible with --output=summary.\n");
695 ctx->format = sprinter_text0_create (config, stdout);
697 case NOTMUCH_FORMAT_JSON:
698 ctx->format = sprinter_json_create (config, stdout);
700 case NOTMUCH_FORMAT_SEXP:
701 ctx->format = sprinter_sexp_create (config, stdout);
704 /* this should never happen */
705 INTERNAL_ERROR("no output format selected");
708 notmuch_exit_if_unsupported_format ();
710 if (notmuch_database_open_verbose (
711 notmuch_config_get_database_path (config),
712 NOTMUCH_DATABASE_MODE_READ_ONLY, &ctx->notmuch, &status_string)) {
715 fputs (status_string, stderr);
716 free (status_string);
722 notmuch_exit_if_unmatched_db_uuid (ctx->notmuch);
724 query_str = query_string_from_args (ctx->notmuch, argc, argv);
725 if (query_str == NULL) {
726 fprintf (stderr, "Out of memory.\n");
729 if (*query_str == '\0') {
730 fprintf (stderr, "Error: notmuch search requires at least one search term.\n");
734 ctx->query = notmuch_query_create (ctx->notmuch, query_str);
735 if (ctx->query == NULL) {
736 fprintf (stderr, "Out of memory\n");
740 notmuch_query_set_sort (ctx->query, ctx->sort);
742 if (ctx->exclude == NOTMUCH_EXCLUDE_FLAG && ctx->output != OUTPUT_SUMMARY) {
743 /* If we are not doing summary output there is nowhere to
744 * print the excluded flag so fall back on including the
745 * excluded messages. */
746 fprintf (stderr, "Warning: this output format cannot flag excluded messages.\n");
747 ctx->exclude = NOTMUCH_EXCLUDE_FALSE;
750 if (ctx->exclude != NOTMUCH_EXCLUDE_FALSE) {
751 const char **search_exclude_tags;
752 size_t search_exclude_tags_length;
753 notmuch_status_t status;
755 search_exclude_tags = notmuch_config_get_search_exclude_tags
756 (config, &search_exclude_tags_length);
758 for (i = 0; i < search_exclude_tags_length; i++) {
759 status = notmuch_query_add_tag_exclude (ctx->query, search_exclude_tags[i]);
760 if (status && status != NOTMUCH_STATUS_IGNORED) {
761 print_status_query ("notmuch search", ctx->query, status);
766 notmuch_query_set_omit_excluded (ctx->query, ctx->exclude);
773 _notmuch_search_cleanup (search_context_t *ctx)
775 notmuch_query_destroy (ctx->query);
776 notmuch_database_destroy (ctx->notmuch);
778 talloc_free (ctx->format);
781 static search_context_t search_context = {
782 .format_sel = NOTMUCH_FORMAT_TEXT,
783 .exclude = NOTMUCH_EXCLUDE_TRUE,
784 .sort = NOTMUCH_SORT_NEWEST_FIRST,
787 .limit = -1, /* unlimited */
789 .dedup = DEDUP_MAILBOX,
792 static const notmuch_opt_desc_t common_options[] = {
793 { .opt_keyword = &search_context.sort, .name = "sort", .keywords =
794 (notmuch_keyword_t []){ { "oldest-first", NOTMUCH_SORT_OLDEST_FIRST },
795 { "newest-first", NOTMUCH_SORT_NEWEST_FIRST },
797 { .opt_keyword = &search_context.format_sel, .name = "format", .keywords =
798 (notmuch_keyword_t []){ { "json", NOTMUCH_FORMAT_JSON },
799 { "sexp", NOTMUCH_FORMAT_SEXP },
800 { "text", NOTMUCH_FORMAT_TEXT },
801 { "text0", NOTMUCH_FORMAT_TEXT0 },
803 { .opt_int = ¬much_format_version, .name = "format-version" },
808 notmuch_search_command (notmuch_config_t *config, int argc, char *argv[])
810 search_context_t *ctx = &search_context;
813 notmuch_opt_desc_t options[] = {
814 { .opt_keyword = &ctx->output, .name = "output", .keywords =
815 (notmuch_keyword_t []){ { "summary", OUTPUT_SUMMARY },
816 { "threads", OUTPUT_THREADS },
817 { "messages", OUTPUT_MESSAGES },
818 { "files", OUTPUT_FILES },
819 { "tags", OUTPUT_TAGS },
821 { .opt_keyword = &ctx->exclude, .name = "exclude", .keywords =
822 (notmuch_keyword_t []){ { "true", NOTMUCH_EXCLUDE_TRUE },
823 { "false", NOTMUCH_EXCLUDE_FALSE },
824 { "flag", NOTMUCH_EXCLUDE_FLAG },
825 { "all", NOTMUCH_EXCLUDE_ALL },
827 { .opt_int = &ctx->offset, .name = "offset" },
828 { .opt_int = &ctx->limit, .name = "limit" },
829 { .opt_int = &ctx->dupe, .name = "duplicate" },
830 { .opt_inherit = common_options },
831 { .opt_inherit = notmuch_shared_options },
835 ctx->output = OUTPUT_SUMMARY;
836 opt_index = parse_arguments (argc, argv, options, 1);
840 notmuch_process_shared_options (argv[0]);
842 if (ctx->output != OUTPUT_FILES && ctx->output != OUTPUT_MESSAGES &&
844 fprintf (stderr, "Error: --duplicate=N is only supported with --output=files and --output=messages.\n");
848 if (_notmuch_search_prepare (ctx, config,
849 argc - opt_index, argv + opt_index))
852 switch (ctx->output) {
855 ret = do_search_threads (ctx);
857 case OUTPUT_MESSAGES:
859 ret = do_search_messages (ctx);
862 ret = do_search_tags (ctx);
865 INTERNAL_ERROR ("Unexpected output");
868 _notmuch_search_cleanup (ctx);
870 return ret ? EXIT_FAILURE : EXIT_SUCCESS;
874 notmuch_address_command (notmuch_config_t *config, int argc, char *argv[])
876 search_context_t *ctx = &search_context;
879 notmuch_opt_desc_t options[] = {
880 { .opt_flags = &ctx->output, .name = "output", .keywords =
881 (notmuch_keyword_t []){ { "sender", OUTPUT_SENDER },
882 { "recipients", OUTPUT_RECIPIENTS },
883 { "count", OUTPUT_COUNT },
884 { "address", OUTPUT_ADDRESS },
886 { .opt_keyword = &ctx->exclude, .name = "exclude", .keywords =
887 (notmuch_keyword_t []){ { "true", NOTMUCH_EXCLUDE_TRUE },
888 { "false", NOTMUCH_EXCLUDE_FALSE },
890 { .opt_keyword = &ctx->dedup, .name = "deduplicate", .keywords =
891 (notmuch_keyword_t []){ { "no", DEDUP_NONE },
892 { "mailbox", DEDUP_MAILBOX },
893 { "address", DEDUP_ADDRESS },
895 { .opt_inherit = common_options },
896 { .opt_inherit = notmuch_shared_options },
900 opt_index = parse_arguments (argc, argv, options, 1);
904 notmuch_process_shared_options (argv[0]);
906 if (! (ctx->output & (OUTPUT_SENDER | OUTPUT_RECIPIENTS)))
907 ctx->output |= OUTPUT_SENDER;
909 if (ctx->output & OUTPUT_COUNT && ctx->dedup == DEDUP_NONE) {
910 fprintf (stderr, "--output=count is not applicable with --deduplicate=no\n");
914 if (_notmuch_search_prepare (ctx, config,
915 argc - opt_index, argv + opt_index))
918 ctx->addresses = g_hash_table_new_full (strcase_hash, strcase_equal,
919 _talloc_free_for_g_hash,
920 _list_free_for_g_hash);
922 /* The order is not guaranteed if a full pass is required, so go
924 if (ctx->output & OUTPUT_COUNT || ctx->dedup == DEDUP_ADDRESS)
925 notmuch_query_set_sort (ctx->query, NOTMUCH_SORT_UNSORTED);
927 ret = do_search_messages (ctx);
929 g_hash_table_unref (ctx->addresses);
932 _notmuch_search_cleanup (ctx);
934 return ret ? EXIT_FAILURE : EXIT_SUCCESS;