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 /* Returns TRUE iff name and addr is duplicate. If not, stores the
269 * name/addr pair in order to detect subsequent duplicates. */
270 static notmuch_bool_t
271 is_duplicate (const search_context_t *ctx, const char *name, const char *addr)
276 key = talloc_asprintf (ctx->format, "%s <%s>", name, addr);
280 mailbox = g_hash_table_lookup (ctx->addresses, key);
287 mailbox = new_mailbox (ctx->format, name, addr);
291 g_hash_table_insert (ctx->addresses, key, mailbox);
297 print_mailbox (const search_context_t *ctx, const mailbox_t *mailbox)
299 const char *name = mailbox->name;
300 const char *addr = mailbox->addr;
301 int count = mailbox->count;
302 sprinter_t *format = ctx->format;
303 InternetAddress *ia = internet_address_mailbox_new (name, addr);
306 /* name_addr has the name part quoted if necessary. Compare
307 * 'John Doe <john@doe.com>' vs. '"Doe, John" <john@doe.com>' */
308 name_addr = internet_address_to_string (ia, FALSE);
310 if (format->is_text_printer) {
312 format->integer (format, count);
313 format->string (format, "\t");
315 format->string (format, name_addr);
316 format->separator (format);
318 format->begin_map (format);
319 format->map_key (format, "name");
320 format->string (format, name);
321 format->map_key (format, "address");
322 format->string (format, addr);
323 format->map_key (format, "name-addr");
324 format->string (format, name_addr);
326 format->map_key (format, "count");
327 format->integer (format, count);
329 format->end (format);
330 format->separator (format);
337 /* Print or prepare for printing addresses from InternetAddressList. */
339 process_address_list (const search_context_t *ctx,
340 InternetAddressList *list)
342 InternetAddress *address;
345 for (i = 0; i < internet_address_list_length (list); i++) {
346 address = internet_address_list_get_address (list, i);
347 if (INTERNET_ADDRESS_IS_GROUP (address)) {
348 InternetAddressGroup *group;
349 InternetAddressList *group_list;
351 group = INTERNET_ADDRESS_GROUP (address);
352 group_list = internet_address_group_get_members (group);
353 if (group_list == NULL)
356 process_address_list (ctx, group_list);
358 InternetAddressMailbox *mailbox = INTERNET_ADDRESS_MAILBOX (address);
360 .name = internet_address_get_name (address),
361 .addr = internet_address_mailbox_get_addr (mailbox),
365 /* OUTPUT_COUNT only works with deduplication */
366 if (ctx->dedup != DEDUP_NONE &&
367 is_duplicate (ctx, mbx.name, mbx.addr))
370 if (ctx->output & OUTPUT_COUNT)
373 print_mailbox (ctx, &mbx);
378 /* Print or prepare for printing addresses from a message header. */
380 process_address_header (const search_context_t *ctx, const char *value)
382 InternetAddressList *list;
387 list = internet_address_list_parse_string (value);
391 process_address_list (ctx, list);
393 g_object_unref (list);
396 /* Destructor for talloc-allocated GHashTable keys and values. */
398 _talloc_free_for_g_hash (void *ptr)
404 print_hash_value (unused (gpointer key), gpointer value, gpointer user_data)
406 const mailbox_t *mailbox = value;
407 search_context_t *ctx = user_data;
409 print_mailbox (ctx, mailbox);
413 _count_filenames (notmuch_message_t *message)
415 notmuch_filenames_t *filenames;
418 filenames = notmuch_message_get_filenames (message);
420 while (notmuch_filenames_valid (filenames)) {
421 notmuch_filenames_move_to_next (filenames);
425 notmuch_filenames_destroy (filenames);
431 do_search_messages (search_context_t *ctx)
433 notmuch_message_t *message;
434 notmuch_messages_t *messages;
435 notmuch_filenames_t *filenames;
436 sprinter_t *format = ctx->format;
438 notmuch_status_t status;
440 if (ctx->offset < 0) {
441 ctx->offset += notmuch_query_count_messages (ctx->query);
446 status = notmuch_query_search_messages_st (ctx->query, &messages);
447 if (print_status_query ("notmuch search", ctx->query, status))
450 format->begin_list (format);
453 notmuch_messages_valid (messages) && (ctx->limit < 0 || i < ctx->offset + ctx->limit);
454 notmuch_messages_move_to_next (messages), i++)
459 message = notmuch_messages_get (messages);
461 if (ctx->output == OUTPUT_FILES) {
463 filenames = notmuch_message_get_filenames (message);
466 notmuch_filenames_valid (filenames);
467 notmuch_filenames_move_to_next (filenames), j++)
469 if (ctx->dupe < 0 || ctx->dupe == j) {
470 format->string (format, notmuch_filenames_get (filenames));
471 format->separator (format);
475 notmuch_filenames_destroy( filenames );
477 } else if (ctx->output == OUTPUT_MESSAGES) {
478 /* special case 1 for speed */
479 if (ctx->dupe <= 1 || ctx->dupe <= _count_filenames (message)) {
480 format->set_prefix (format, "id");
481 format->string (format,
482 notmuch_message_get_message_id (message));
483 format->separator (format);
486 if (ctx->output & OUTPUT_SENDER) {
489 addrs = notmuch_message_get_header (message, "from");
490 process_address_header (ctx, addrs);
493 if (ctx->output & OUTPUT_RECIPIENTS) {
494 const char *hdrs[] = { "to", "cc", "bcc" };
498 for (j = 0; j < ARRAY_SIZE (hdrs); j++) {
499 addrs = notmuch_message_get_header (message, hdrs[j]);
500 process_address_header (ctx, addrs);
505 notmuch_message_destroy (message);
508 if (ctx->addresses && ctx->output & OUTPUT_COUNT)
509 g_hash_table_foreach (ctx->addresses, print_hash_value, ctx);
511 notmuch_messages_destroy (messages);
513 format->end (format);
519 do_search_tags (const search_context_t *ctx)
521 notmuch_messages_t *messages = NULL;
522 notmuch_tags_t *tags;
524 sprinter_t *format = ctx->format;
525 notmuch_query_t *query = ctx->query;
526 notmuch_database_t *notmuch = ctx->notmuch;
528 /* should the following only special case if no excluded terms
531 /* Special-case query of "*" for better performance. */
532 if (strcmp (notmuch_query_get_query_string (query), "*") == 0) {
533 tags = notmuch_database_get_all_tags (notmuch);
535 notmuch_status_t status;
536 status = notmuch_query_search_messages_st (query, &messages);
537 if (print_status_query ("notmuch search", query, status))
540 tags = notmuch_messages_collect_tags (messages);
545 format->begin_list (format);
548 notmuch_tags_valid (tags);
549 notmuch_tags_move_to_next (tags))
551 tag = notmuch_tags_get (tags);
553 format->string (format, tag);
554 format->separator (format);
558 notmuch_tags_destroy (tags);
561 notmuch_messages_destroy (messages);
563 format->end (format);
569 _notmuch_search_prepare (search_context_t *ctx, notmuch_config_t *config, int argc, char *argv[])
573 char *status_string = NULL;
575 switch (ctx->format_sel) {
576 case NOTMUCH_FORMAT_TEXT:
577 ctx->format = sprinter_text_create (config, stdout);
579 case NOTMUCH_FORMAT_TEXT0:
580 if (ctx->output == OUTPUT_SUMMARY) {
581 fprintf (stderr, "Error: --format=text0 is not compatible with --output=summary.\n");
584 ctx->format = sprinter_text0_create (config, stdout);
586 case NOTMUCH_FORMAT_JSON:
587 ctx->format = sprinter_json_create (config, stdout);
589 case NOTMUCH_FORMAT_SEXP:
590 ctx->format = sprinter_sexp_create (config, stdout);
593 /* this should never happen */
594 INTERNAL_ERROR("no output format selected");
597 notmuch_exit_if_unsupported_format ();
599 if (notmuch_database_open_verbose (
600 notmuch_config_get_database_path (config),
601 NOTMUCH_DATABASE_MODE_READ_ONLY, &ctx->notmuch, &status_string)) {
604 fputs (status_string, stderr);
605 free (status_string);
611 notmuch_exit_if_unmatched_db_uuid (ctx->notmuch);
613 query_str = query_string_from_args (ctx->notmuch, argc, argv);
614 if (query_str == NULL) {
615 fprintf (stderr, "Out of memory.\n");
618 if (*query_str == '\0') {
619 fprintf (stderr, "Error: notmuch search requires at least one search term.\n");
623 ctx->query = notmuch_query_create (ctx->notmuch, query_str);
624 if (ctx->query == NULL) {
625 fprintf (stderr, "Out of memory\n");
629 notmuch_query_set_sort (ctx->query, ctx->sort);
631 if (ctx->exclude == NOTMUCH_EXCLUDE_FLAG && ctx->output != OUTPUT_SUMMARY) {
632 /* If we are not doing summary output there is nowhere to
633 * print the excluded flag so fall back on including the
634 * excluded messages. */
635 fprintf (stderr, "Warning: this output format cannot flag excluded messages.\n");
636 ctx->exclude = NOTMUCH_EXCLUDE_FALSE;
639 if (ctx->exclude != NOTMUCH_EXCLUDE_FALSE) {
640 const char **search_exclude_tags;
641 size_t search_exclude_tags_length;
643 search_exclude_tags = notmuch_config_get_search_exclude_tags
644 (config, &search_exclude_tags_length);
645 for (i = 0; i < search_exclude_tags_length; i++)
646 notmuch_query_add_tag_exclude (ctx->query, search_exclude_tags[i]);
647 notmuch_query_set_omit_excluded (ctx->query, ctx->exclude);
654 _notmuch_search_cleanup (search_context_t *ctx)
656 notmuch_query_destroy (ctx->query);
657 notmuch_database_destroy (ctx->notmuch);
659 talloc_free (ctx->format);
662 static search_context_t search_context = {
663 .format_sel = NOTMUCH_FORMAT_TEXT,
664 .exclude = NOTMUCH_EXCLUDE_TRUE,
665 .sort = NOTMUCH_SORT_NEWEST_FIRST,
668 .limit = -1, /* unlimited */
670 .dedup = DEDUP_MAILBOX,
673 static const notmuch_opt_desc_t common_options[] = {
674 { NOTMUCH_OPT_KEYWORD, &search_context.sort, "sort", 's',
675 (notmuch_keyword_t []){ { "oldest-first", NOTMUCH_SORT_OLDEST_FIRST },
676 { "newest-first", NOTMUCH_SORT_NEWEST_FIRST },
678 { NOTMUCH_OPT_KEYWORD, &search_context.format_sel, "format", 'f',
679 (notmuch_keyword_t []){ { "json", NOTMUCH_FORMAT_JSON },
680 { "sexp", NOTMUCH_FORMAT_SEXP },
681 { "text", NOTMUCH_FORMAT_TEXT },
682 { "text0", NOTMUCH_FORMAT_TEXT0 },
684 { NOTMUCH_OPT_INT, ¬much_format_version, "format-version", 0, 0 },
689 notmuch_search_command (notmuch_config_t *config, int argc, char *argv[])
691 search_context_t *ctx = &search_context;
694 notmuch_opt_desc_t options[] = {
695 { NOTMUCH_OPT_KEYWORD, &ctx->output, "output", 'o',
696 (notmuch_keyword_t []){ { "summary", OUTPUT_SUMMARY },
697 { "threads", OUTPUT_THREADS },
698 { "messages", OUTPUT_MESSAGES },
699 { "files", OUTPUT_FILES },
700 { "tags", OUTPUT_TAGS },
702 { NOTMUCH_OPT_KEYWORD, &ctx->exclude, "exclude", 'x',
703 (notmuch_keyword_t []){ { "true", NOTMUCH_EXCLUDE_TRUE },
704 { "false", NOTMUCH_EXCLUDE_FALSE },
705 { "flag", NOTMUCH_EXCLUDE_FLAG },
706 { "all", NOTMUCH_EXCLUDE_ALL },
708 { NOTMUCH_OPT_INT, &ctx->offset, "offset", 'O', 0 },
709 { NOTMUCH_OPT_INT, &ctx->limit, "limit", 'L', 0 },
710 { NOTMUCH_OPT_INT, &ctx->dupe, "duplicate", 'D', 0 },
711 { NOTMUCH_OPT_INHERIT, (void *) &common_options, NULL, 0, 0 },
712 { NOTMUCH_OPT_INHERIT, (void *) ¬much_shared_options, NULL, 0, 0 },
716 ctx->output = OUTPUT_SUMMARY;
717 opt_index = parse_arguments (argc, argv, options, 1);
721 notmuch_process_shared_options (argv[0]);
723 if (ctx->output != OUTPUT_FILES && ctx->output != OUTPUT_MESSAGES &&
725 fprintf (stderr, "Error: --duplicate=N is only supported with --output=files and --output=messages.\n");
729 if (_notmuch_search_prepare (ctx, config,
730 argc - opt_index, argv + opt_index))
733 switch (ctx->output) {
736 ret = do_search_threads (ctx);
738 case OUTPUT_MESSAGES:
740 ret = do_search_messages (ctx);
743 ret = do_search_tags (ctx);
746 INTERNAL_ERROR ("Unexpected output");
749 _notmuch_search_cleanup (ctx);
751 return ret ? EXIT_FAILURE : EXIT_SUCCESS;
755 notmuch_address_command (notmuch_config_t *config, int argc, char *argv[])
757 search_context_t *ctx = &search_context;
760 notmuch_opt_desc_t options[] = {
761 { NOTMUCH_OPT_KEYWORD_FLAGS, &ctx->output, "output", 'o',
762 (notmuch_keyword_t []){ { "sender", OUTPUT_SENDER },
763 { "recipients", OUTPUT_RECIPIENTS },
764 { "count", OUTPUT_COUNT },
766 { NOTMUCH_OPT_KEYWORD, &ctx->exclude, "exclude", 'x',
767 (notmuch_keyword_t []){ { "true", NOTMUCH_EXCLUDE_TRUE },
768 { "false", NOTMUCH_EXCLUDE_FALSE },
770 { NOTMUCH_OPT_KEYWORD, &ctx->dedup, "deduplicate", 'D',
771 (notmuch_keyword_t []){ { "no", DEDUP_NONE },
772 { "mailbox", DEDUP_MAILBOX },
774 { NOTMUCH_OPT_INHERIT, (void *) &common_options, NULL, 0, 0 },
775 { NOTMUCH_OPT_INHERIT, (void *) ¬much_shared_options, NULL, 0, 0 },
779 opt_index = parse_arguments (argc, argv, options, 1);
783 notmuch_process_shared_options (argv[0]);
785 if (! (ctx->output & (OUTPUT_SENDER | OUTPUT_RECIPIENTS)))
786 ctx->output |= OUTPUT_SENDER;
788 if (ctx->output & OUTPUT_COUNT && ctx->dedup == DEDUP_NONE) {
789 fprintf (stderr, "--output=count is not applicable with --deduplicate=no\n");
793 if (_notmuch_search_prepare (ctx, config,
794 argc - opt_index, argv + opt_index))
797 ctx->addresses = g_hash_table_new_full (g_str_hash, g_str_equal,
798 _talloc_free_for_g_hash, _talloc_free_for_g_hash);
800 ret = do_search_messages (ctx);
802 g_hash_table_unref (ctx->addresses);
805 _notmuch_search_cleanup (ctx);
807 return ret ? EXIT_FAILURE : EXIT_SUCCESS;