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,
52 notmuch_database_t *notmuch;
53 format_sel_t format_sel;
55 notmuch_exclude_t exclude;
56 notmuch_query_t *query;
62 GHashTable *addresses;
72 /* Return two stable query strings that identify exactly the matched
73 * and unmatched messages currently in thread. If there are no
74 * matched or unmatched messages, the returned buffers will be
77 get_thread_query (notmuch_thread_t *thread,
78 char **matched_out, char **unmatched_out)
80 notmuch_messages_t *messages;
82 size_t escaped_len = 0;
84 *matched_out = *unmatched_out = NULL;
86 for (messages = notmuch_thread_get_messages (thread);
87 notmuch_messages_valid (messages);
88 notmuch_messages_move_to_next (messages))
90 notmuch_message_t *message = notmuch_messages_get (messages);
91 const char *mid = notmuch_message_get_message_id (message);
92 /* Determine which query buffer to extend */
93 char **buf = notmuch_message_get_flag (
94 message, NOTMUCH_MESSAGE_FLAG_MATCH) ? matched_out : unmatched_out;
95 /* Add this message's id: query. Since "id" is an exclusive
96 * prefix, it is implicitly 'or'd together, so we only need to
97 * join queries with a space. */
98 if (make_boolean_term (thread, "id", mid, &escaped, &escaped_len) < 0)
101 *buf = talloc_asprintf_append_buffer (*buf, " %s", escaped);
103 *buf = talloc_strdup (thread, escaped);
107 talloc_free (escaped);
112 do_search_threads (search_context_t *ctx)
114 notmuch_thread_t *thread;
115 notmuch_threads_t *threads;
116 notmuch_tags_t *tags;
117 sprinter_t *format = ctx->format;
120 notmuch_status_t status;
122 if (ctx->offset < 0) {
123 ctx->offset += notmuch_query_count_threads (ctx->query);
128 status = notmuch_query_search_threads_st (ctx->query, &threads);
129 if (print_status_query("notmuch search", ctx->query, status))
132 format->begin_list (format);
135 notmuch_threads_valid (threads) && (ctx->limit < 0 || i < ctx->offset + ctx->limit);
136 notmuch_threads_move_to_next (threads), i++)
138 thread = notmuch_threads_get (threads);
140 if (i < ctx->offset) {
141 notmuch_thread_destroy (thread);
145 if (ctx->output == OUTPUT_THREADS) {
146 format->set_prefix (format, "thread");
147 format->string (format,
148 notmuch_thread_get_thread_id (thread));
149 format->separator (format);
150 } else { /* output == OUTPUT_SUMMARY */
151 void *ctx_quote = talloc_new (thread);
152 const char *authors = notmuch_thread_get_authors (thread);
153 const char *subject = notmuch_thread_get_subject (thread);
154 const char *thread_id = notmuch_thread_get_thread_id (thread);
155 int matched = notmuch_thread_get_matched_messages (thread);
156 int total = notmuch_thread_get_total_messages (thread);
157 const char *relative_date = NULL;
158 notmuch_bool_t first_tag = TRUE;
160 format->begin_map (format);
162 if (ctx->sort == NOTMUCH_SORT_OLDEST_FIRST)
163 date = notmuch_thread_get_oldest_date (thread);
165 date = notmuch_thread_get_newest_date (thread);
167 relative_date = notmuch_time_relative_date (ctx_quote, date);
169 if (format->is_text_printer) {
170 /* Special case for the text formatter */
171 printf ("thread:%s %12s [%d/%d] %s; %s (",
176 sanitize_string (ctx_quote, authors),
177 sanitize_string (ctx_quote, subject));
178 } else { /* Structured Output */
179 format->map_key (format, "thread");
180 format->string (format, thread_id);
181 format->map_key (format, "timestamp");
182 format->integer (format, date);
183 format->map_key (format, "date_relative");
184 format->string (format, relative_date);
185 format->map_key (format, "matched");
186 format->integer (format, matched);
187 format->map_key (format, "total");
188 format->integer (format, total);
189 format->map_key (format, "authors");
190 format->string (format, authors);
191 format->map_key (format, "subject");
192 format->string (format, subject);
193 if (notmuch_format_version >= 2) {
194 char *matched_query, *unmatched_query;
195 if (get_thread_query (thread, &matched_query,
196 &unmatched_query) < 0) {
197 fprintf (stderr, "Out of memory\n");
200 format->map_key (format, "query");
201 format->begin_list (format);
203 format->string (format, matched_query);
205 format->null (format);
207 format->string (format, unmatched_query);
209 format->null (format);
210 format->end (format);
214 talloc_free (ctx_quote);
216 format->map_key (format, "tags");
217 format->begin_list (format);
219 for (tags = notmuch_thread_get_tags (thread);
220 notmuch_tags_valid (tags);
221 notmuch_tags_move_to_next (tags))
223 const char *tag = notmuch_tags_get (tags);
225 if (format->is_text_printer) {
226 /* Special case for the text formatter */
232 } else { /* Structured Output */
233 format->string (format, tag);
237 if (format->is_text_printer)
240 format->end (format);
241 format->end (format);
242 format->separator (format);
245 notmuch_thread_destroy (thread);
248 format->end (format);
253 static mailbox_t *new_mailbox (void *ctx, const char *name, const char *addr)
257 mailbox = talloc (ctx, mailbox_t);
261 mailbox->name = talloc_strdup (mailbox, name);
262 mailbox->addr = talloc_strdup (mailbox, addr);
268 static int mailbox_compare (const void *v1, const void *v2)
270 const mailbox_t *m1 = v1, *m2 = v2;
273 ret = strcmp_null (m1->name, m2->name);
275 ret = strcmp (m1->addr, m2->addr);
280 /* Returns TRUE iff name and addr is duplicate. If not, stores the
281 * name/addr pair in order to detect subsequent duplicates. */
282 static notmuch_bool_t
283 is_duplicate (const search_context_t *ctx, const char *name, const char *addr)
289 list = g_hash_table_lookup (ctx->addresses, addr);
296 l = g_list_find_custom (list, &find, mailbox_compare);
303 mailbox = new_mailbox (ctx->format, name, addr);
308 * XXX: It would be more efficient to prepend to the list, but
309 * then we'd have to store the changed list head back to the
310 * hash table. This check is here just to avoid the compiler
311 * warning for unused result.
313 if (list != g_list_append (list, mailbox))
314 INTERNAL_ERROR ("appending to list changed list head\n");
319 key = talloc_strdup (ctx->format, addr);
323 mailbox = new_mailbox (ctx->format, name, addr);
327 list = g_list_append (NULL, mailbox);
331 g_hash_table_insert (ctx->addresses, key, list);
337 print_mailbox (const search_context_t *ctx, const mailbox_t *mailbox)
339 const char *name = mailbox->name;
340 const char *addr = mailbox->addr;
341 int count = mailbox->count;
342 sprinter_t *format = ctx->format;
343 InternetAddress *ia = internet_address_mailbox_new (name, addr);
346 /* name_addr has the name part quoted if necessary. Compare
347 * 'John Doe <john@doe.com>' vs. '"Doe, John" <john@doe.com>' */
348 name_addr = internet_address_to_string (ia, FALSE);
350 if (format->is_text_printer) {
352 format->integer (format, count);
353 format->string (format, "\t");
355 format->string (format, name_addr);
356 format->separator (format);
358 format->begin_map (format);
359 format->map_key (format, "name");
360 format->string (format, name);
361 format->map_key (format, "address");
362 format->string (format, addr);
363 format->map_key (format, "name-addr");
364 format->string (format, name_addr);
366 format->map_key (format, "count");
367 format->integer (format, count);
369 format->end (format);
370 format->separator (format);
377 /* Print or prepare for printing addresses from InternetAddressList. */
379 process_address_list (const search_context_t *ctx,
380 InternetAddressList *list)
382 InternetAddress *address;
385 for (i = 0; i < internet_address_list_length (list); i++) {
386 address = internet_address_list_get_address (list, i);
387 if (INTERNET_ADDRESS_IS_GROUP (address)) {
388 InternetAddressGroup *group;
389 InternetAddressList *group_list;
391 group = INTERNET_ADDRESS_GROUP (address);
392 group_list = internet_address_group_get_members (group);
393 if (group_list == NULL)
396 process_address_list (ctx, group_list);
398 InternetAddressMailbox *mailbox = INTERNET_ADDRESS_MAILBOX (address);
400 .name = internet_address_get_name (address),
401 .addr = internet_address_mailbox_get_addr (mailbox),
405 /* OUTPUT_COUNT only works with deduplication */
406 if (ctx->dedup != DEDUP_NONE &&
407 is_duplicate (ctx, mbx.name, mbx.addr))
410 if (ctx->output & OUTPUT_COUNT)
413 print_mailbox (ctx, &mbx);
418 /* Print or prepare for printing addresses from a message header. */
420 process_address_header (const search_context_t *ctx, const char *value)
422 InternetAddressList *list;
427 list = internet_address_list_parse_string (value);
431 process_address_list (ctx, list);
433 g_object_unref (list);
436 /* Destructor for talloc-allocated GHashTable keys and values. */
438 _talloc_free_for_g_hash (void *ptr)
444 _list_free_for_g_hash (void *ptr)
446 g_list_free_full (ptr, _talloc_free_for_g_hash);
450 print_list_value (void *mailbox, void *context)
452 print_mailbox (context, mailbox);
456 print_hash_value (unused (void *key), void *list, void *context)
458 g_list_foreach (list, print_list_value, context);
462 _count_filenames (notmuch_message_t *message)
464 notmuch_filenames_t *filenames;
467 filenames = notmuch_message_get_filenames (message);
469 while (notmuch_filenames_valid (filenames)) {
470 notmuch_filenames_move_to_next (filenames);
474 notmuch_filenames_destroy (filenames);
480 do_search_messages (search_context_t *ctx)
482 notmuch_message_t *message;
483 notmuch_messages_t *messages;
484 notmuch_filenames_t *filenames;
485 sprinter_t *format = ctx->format;
487 notmuch_status_t status;
489 if (ctx->offset < 0) {
490 ctx->offset += notmuch_query_count_messages (ctx->query);
495 status = notmuch_query_search_messages_st (ctx->query, &messages);
496 if (print_status_query ("notmuch search", ctx->query, status))
499 format->begin_list (format);
502 notmuch_messages_valid (messages) && (ctx->limit < 0 || i < ctx->offset + ctx->limit);
503 notmuch_messages_move_to_next (messages), i++)
508 message = notmuch_messages_get (messages);
510 if (ctx->output == OUTPUT_FILES) {
512 filenames = notmuch_message_get_filenames (message);
515 notmuch_filenames_valid (filenames);
516 notmuch_filenames_move_to_next (filenames), j++)
518 if (ctx->dupe < 0 || ctx->dupe == j) {
519 format->string (format, notmuch_filenames_get (filenames));
520 format->separator (format);
524 notmuch_filenames_destroy( filenames );
526 } else if (ctx->output == OUTPUT_MESSAGES) {
527 /* special case 1 for speed */
528 if (ctx->dupe <= 1 || ctx->dupe <= _count_filenames (message)) {
529 format->set_prefix (format, "id");
530 format->string (format,
531 notmuch_message_get_message_id (message));
532 format->separator (format);
535 if (ctx->output & OUTPUT_SENDER) {
538 addrs = notmuch_message_get_header (message, "from");
539 process_address_header (ctx, addrs);
542 if (ctx->output & OUTPUT_RECIPIENTS) {
543 const char *hdrs[] = { "to", "cc", "bcc" };
547 for (j = 0; j < ARRAY_SIZE (hdrs); j++) {
548 addrs = notmuch_message_get_header (message, hdrs[j]);
549 process_address_header (ctx, addrs);
554 notmuch_message_destroy (message);
557 if (ctx->addresses && ctx->output & OUTPUT_COUNT)
558 g_hash_table_foreach (ctx->addresses, print_hash_value, ctx);
560 notmuch_messages_destroy (messages);
562 format->end (format);
568 do_search_tags (const search_context_t *ctx)
570 notmuch_messages_t *messages = NULL;
571 notmuch_tags_t *tags;
573 sprinter_t *format = ctx->format;
574 notmuch_query_t *query = ctx->query;
575 notmuch_database_t *notmuch = ctx->notmuch;
577 /* should the following only special case if no excluded terms
580 /* Special-case query of "*" for better performance. */
581 if (strcmp (notmuch_query_get_query_string (query), "*") == 0) {
582 tags = notmuch_database_get_all_tags (notmuch);
584 notmuch_status_t status;
585 status = notmuch_query_search_messages_st (query, &messages);
586 if (print_status_query ("notmuch search", query, status))
589 tags = notmuch_messages_collect_tags (messages);
594 format->begin_list (format);
597 notmuch_tags_valid (tags);
598 notmuch_tags_move_to_next (tags))
600 tag = notmuch_tags_get (tags);
602 format->string (format, tag);
603 format->separator (format);
607 notmuch_tags_destroy (tags);
610 notmuch_messages_destroy (messages);
612 format->end (format);
618 _notmuch_search_prepare (search_context_t *ctx, notmuch_config_t *config, int argc, char *argv[])
622 char *status_string = NULL;
624 switch (ctx->format_sel) {
625 case NOTMUCH_FORMAT_TEXT:
626 ctx->format = sprinter_text_create (config, stdout);
628 case NOTMUCH_FORMAT_TEXT0:
629 if (ctx->output == OUTPUT_SUMMARY) {
630 fprintf (stderr, "Error: --format=text0 is not compatible with --output=summary.\n");
633 ctx->format = sprinter_text0_create (config, stdout);
635 case NOTMUCH_FORMAT_JSON:
636 ctx->format = sprinter_json_create (config, stdout);
638 case NOTMUCH_FORMAT_SEXP:
639 ctx->format = sprinter_sexp_create (config, stdout);
642 /* this should never happen */
643 INTERNAL_ERROR("no output format selected");
646 notmuch_exit_if_unsupported_format ();
648 if (notmuch_database_open_verbose (
649 notmuch_config_get_database_path (config),
650 NOTMUCH_DATABASE_MODE_READ_ONLY, &ctx->notmuch, &status_string)) {
653 fputs (status_string, stderr);
654 free (status_string);
660 notmuch_exit_if_unmatched_db_uuid (ctx->notmuch);
662 query_str = query_string_from_args (ctx->notmuch, argc, argv);
663 if (query_str == NULL) {
664 fprintf (stderr, "Out of memory.\n");
667 if (*query_str == '\0') {
668 fprintf (stderr, "Error: notmuch search requires at least one search term.\n");
672 ctx->query = notmuch_query_create (ctx->notmuch, query_str);
673 if (ctx->query == NULL) {
674 fprintf (stderr, "Out of memory\n");
678 notmuch_query_set_sort (ctx->query, ctx->sort);
680 if (ctx->exclude == NOTMUCH_EXCLUDE_FLAG && ctx->output != OUTPUT_SUMMARY) {
681 /* If we are not doing summary output there is nowhere to
682 * print the excluded flag so fall back on including the
683 * excluded messages. */
684 fprintf (stderr, "Warning: this output format cannot flag excluded messages.\n");
685 ctx->exclude = NOTMUCH_EXCLUDE_FALSE;
688 if (ctx->exclude != NOTMUCH_EXCLUDE_FALSE) {
689 const char **search_exclude_tags;
690 size_t search_exclude_tags_length;
692 search_exclude_tags = notmuch_config_get_search_exclude_tags
693 (config, &search_exclude_tags_length);
694 for (i = 0; i < search_exclude_tags_length; i++)
695 notmuch_query_add_tag_exclude (ctx->query, search_exclude_tags[i]);
696 notmuch_query_set_omit_excluded (ctx->query, ctx->exclude);
703 _notmuch_search_cleanup (search_context_t *ctx)
705 notmuch_query_destroy (ctx->query);
706 notmuch_database_destroy (ctx->notmuch);
708 talloc_free (ctx->format);
711 static search_context_t search_context = {
712 .format_sel = NOTMUCH_FORMAT_TEXT,
713 .exclude = NOTMUCH_EXCLUDE_TRUE,
714 .sort = NOTMUCH_SORT_NEWEST_FIRST,
717 .limit = -1, /* unlimited */
719 .dedup = DEDUP_MAILBOX,
722 static const notmuch_opt_desc_t common_options[] = {
723 { NOTMUCH_OPT_KEYWORD, &search_context.sort, "sort", 's',
724 (notmuch_keyword_t []){ { "oldest-first", NOTMUCH_SORT_OLDEST_FIRST },
725 { "newest-first", NOTMUCH_SORT_NEWEST_FIRST },
727 { NOTMUCH_OPT_KEYWORD, &search_context.format_sel, "format", 'f',
728 (notmuch_keyword_t []){ { "json", NOTMUCH_FORMAT_JSON },
729 { "sexp", NOTMUCH_FORMAT_SEXP },
730 { "text", NOTMUCH_FORMAT_TEXT },
731 { "text0", NOTMUCH_FORMAT_TEXT0 },
733 { NOTMUCH_OPT_INT, ¬much_format_version, "format-version", 0, 0 },
738 notmuch_search_command (notmuch_config_t *config, int argc, char *argv[])
740 search_context_t *ctx = &search_context;
743 notmuch_opt_desc_t options[] = {
744 { NOTMUCH_OPT_KEYWORD, &ctx->output, "output", 'o',
745 (notmuch_keyword_t []){ { "summary", OUTPUT_SUMMARY },
746 { "threads", OUTPUT_THREADS },
747 { "messages", OUTPUT_MESSAGES },
748 { "files", OUTPUT_FILES },
749 { "tags", OUTPUT_TAGS },
751 { NOTMUCH_OPT_KEYWORD, &ctx->exclude, "exclude", 'x',
752 (notmuch_keyword_t []){ { "true", NOTMUCH_EXCLUDE_TRUE },
753 { "false", NOTMUCH_EXCLUDE_FALSE },
754 { "flag", NOTMUCH_EXCLUDE_FLAG },
755 { "all", NOTMUCH_EXCLUDE_ALL },
757 { NOTMUCH_OPT_INT, &ctx->offset, "offset", 'O', 0 },
758 { NOTMUCH_OPT_INT, &ctx->limit, "limit", 'L', 0 },
759 { NOTMUCH_OPT_INT, &ctx->dupe, "duplicate", 'D', 0 },
760 { NOTMUCH_OPT_INHERIT, (void *) &common_options, NULL, 0, 0 },
761 { NOTMUCH_OPT_INHERIT, (void *) ¬much_shared_options, NULL, 0, 0 },
765 ctx->output = OUTPUT_SUMMARY;
766 opt_index = parse_arguments (argc, argv, options, 1);
770 notmuch_process_shared_options (argv[0]);
772 if (ctx->output != OUTPUT_FILES && ctx->output != OUTPUT_MESSAGES &&
774 fprintf (stderr, "Error: --duplicate=N is only supported with --output=files and --output=messages.\n");
778 if (_notmuch_search_prepare (ctx, config,
779 argc - opt_index, argv + opt_index))
782 switch (ctx->output) {
785 ret = do_search_threads (ctx);
787 case OUTPUT_MESSAGES:
789 ret = do_search_messages (ctx);
792 ret = do_search_tags (ctx);
795 INTERNAL_ERROR ("Unexpected output");
798 _notmuch_search_cleanup (ctx);
800 return ret ? EXIT_FAILURE : EXIT_SUCCESS;
804 notmuch_address_command (notmuch_config_t *config, int argc, char *argv[])
806 search_context_t *ctx = &search_context;
809 notmuch_opt_desc_t options[] = {
810 { NOTMUCH_OPT_KEYWORD_FLAGS, &ctx->output, "output", 'o',
811 (notmuch_keyword_t []){ { "sender", OUTPUT_SENDER },
812 { "recipients", OUTPUT_RECIPIENTS },
813 { "count", OUTPUT_COUNT },
815 { NOTMUCH_OPT_KEYWORD, &ctx->exclude, "exclude", 'x',
816 (notmuch_keyword_t []){ { "true", NOTMUCH_EXCLUDE_TRUE },
817 { "false", NOTMUCH_EXCLUDE_FALSE },
819 { NOTMUCH_OPT_KEYWORD, &ctx->dedup, "deduplicate", 'D',
820 (notmuch_keyword_t []){ { "no", DEDUP_NONE },
821 { "mailbox", DEDUP_MAILBOX },
823 { NOTMUCH_OPT_INHERIT, (void *) &common_options, NULL, 0, 0 },
824 { NOTMUCH_OPT_INHERIT, (void *) ¬much_shared_options, NULL, 0, 0 },
828 opt_index = parse_arguments (argc, argv, options, 1);
832 notmuch_process_shared_options (argv[0]);
834 if (! (ctx->output & (OUTPUT_SENDER | OUTPUT_RECIPIENTS)))
835 ctx->output |= OUTPUT_SENDER;
837 if (ctx->output & OUTPUT_COUNT && ctx->dedup == DEDUP_NONE) {
838 fprintf (stderr, "--output=count is not applicable with --deduplicate=no\n");
842 if (_notmuch_search_prepare (ctx, config,
843 argc - opt_index, argv + opt_index))
846 ctx->addresses = g_hash_table_new_full (strcase_hash, strcase_equal,
847 _talloc_free_for_g_hash,
848 _list_free_for_g_hash);
850 ret = do_search_messages (ctx);
852 g_hash_table_unref (ctx->addresses);
855 _notmuch_search_cleanup (ctx);
857 return ret ? EXIT_FAILURE : EXIT_SUCCESS;