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"
26 OUTPUT_SUMMARY = 1 << 0,
27 OUTPUT_THREADS = 1 << 1,
28 OUTPUT_MESSAGES = 1 << 2,
29 OUTPUT_FILES = 1 << 3,
31 OUTPUT_SENDER = 1 << 5,
32 OUTPUT_RECIPIENTS = 1 << 6,
33 OUTPUT_COUNT = 1 << 7,
36 #define OUTPUT_ADDRESS_FLAGS (OUTPUT_SENDER | OUTPUT_RECIPIENTS | OUTPUT_COUNT)
39 FILTER_BY_NAMEADDR = 0,
43 FILTER_BY_NAMEADDRFOLD,
48 notmuch_query_t *query;
54 filter_by_t filter_by;
63 /* Return two stable query strings that identify exactly the matched
64 * and unmatched messages currently in thread. If there are no
65 * matched or unmatched messages, the returned buffers will be
68 get_thread_query (notmuch_thread_t *thread,
69 char **matched_out, char **unmatched_out)
71 notmuch_messages_t *messages;
73 size_t escaped_len = 0;
75 *matched_out = *unmatched_out = NULL;
77 for (messages = notmuch_thread_get_messages (thread);
78 notmuch_messages_valid (messages);
79 notmuch_messages_move_to_next (messages))
81 notmuch_message_t *message = notmuch_messages_get (messages);
82 const char *mid = notmuch_message_get_message_id (message);
83 /* Determine which query buffer to extend */
84 char **buf = notmuch_message_get_flag (
85 message, NOTMUCH_MESSAGE_FLAG_MATCH) ? matched_out : unmatched_out;
86 /* Add this message's id: query. Since "id" is an exclusive
87 * prefix, it is implicitly 'or'd together, so we only need to
88 * join queries with a space. */
89 if (make_boolean_term (thread, "id", mid, &escaped, &escaped_len) < 0)
92 *buf = talloc_asprintf_append_buffer (*buf, " %s", escaped);
94 *buf = talloc_strdup (thread, escaped);
98 talloc_free (escaped);
103 do_search_threads (search_options_t *opt)
105 notmuch_thread_t *thread;
106 notmuch_threads_t *threads;
107 notmuch_tags_t *tags;
108 sprinter_t *format = opt->format;
112 if (opt->offset < 0) {
113 opt->offset += notmuch_query_count_threads (opt->query);
118 threads = notmuch_query_search_threads (opt->query);
122 format->begin_list (format);
125 notmuch_threads_valid (threads) && (opt->limit < 0 || i < opt->offset + opt->limit);
126 notmuch_threads_move_to_next (threads), i++)
128 thread = notmuch_threads_get (threads);
130 if (i < opt->offset) {
131 notmuch_thread_destroy (thread);
135 if (opt->output == OUTPUT_THREADS) {
136 format->set_prefix (format, "thread");
137 format->string (format,
138 notmuch_thread_get_thread_id (thread));
139 format->separator (format);
140 } else { /* output == OUTPUT_SUMMARY */
141 void *ctx_quote = talloc_new (thread);
142 const char *authors = notmuch_thread_get_authors (thread);
143 const char *subject = notmuch_thread_get_subject (thread);
144 const char *thread_id = notmuch_thread_get_thread_id (thread);
145 int matched = notmuch_thread_get_matched_messages (thread);
146 int total = notmuch_thread_get_total_messages (thread);
147 const char *relative_date = NULL;
148 notmuch_bool_t first_tag = TRUE;
150 format->begin_map (format);
152 if (opt->sort == NOTMUCH_SORT_OLDEST_FIRST)
153 date = notmuch_thread_get_oldest_date (thread);
155 date = notmuch_thread_get_newest_date (thread);
157 relative_date = notmuch_time_relative_date (ctx_quote, date);
159 if (format->is_text_printer) {
160 /* Special case for the text formatter */
161 printf ("thread:%s %12s [%d/%d] %s; %s (",
166 sanitize_string (ctx_quote, authors),
167 sanitize_string (ctx_quote, subject));
168 } else { /* Structured Output */
169 format->map_key (format, "thread");
170 format->string (format, thread_id);
171 format->map_key (format, "timestamp");
172 format->integer (format, date);
173 format->map_key (format, "date_relative");
174 format->string (format, relative_date);
175 format->map_key (format, "matched");
176 format->integer (format, matched);
177 format->map_key (format, "total");
178 format->integer (format, total);
179 format->map_key (format, "authors");
180 format->string (format, authors);
181 format->map_key (format, "subject");
182 format->string (format, subject);
183 if (notmuch_format_version >= 2) {
184 char *matched_query, *unmatched_query;
185 if (get_thread_query (thread, &matched_query,
186 &unmatched_query) < 0) {
187 fprintf (stderr, "Out of memory\n");
190 format->map_key (format, "query");
191 format->begin_list (format);
193 format->string (format, matched_query);
195 format->null (format);
197 format->string (format, unmatched_query);
199 format->null (format);
200 format->end (format);
204 talloc_free (ctx_quote);
206 format->map_key (format, "tags");
207 format->begin_list (format);
209 for (tags = notmuch_thread_get_tags (thread);
210 notmuch_tags_valid (tags);
211 notmuch_tags_move_to_next (tags))
213 const char *tag = notmuch_tags_get (tags);
215 if (format->is_text_printer) {
216 /* Special case for the text formatter */
222 } else { /* Structured Output */
223 format->string (format, tag);
227 if (format->is_text_printer)
230 format->end (format);
231 format->end (format);
232 format->separator (format);
235 notmuch_thread_destroy (thread);
238 format->end (format);
243 /* Returns TRUE iff name and/or addr is considered duplicate. */
244 static notmuch_bool_t
245 is_duplicate (const search_options_t *opt, GHashTable *addrs, const char *name, const char *addr)
247 notmuch_bool_t duplicate;
249 gchar *addrfold = NULL;
252 if (opt->filter_by == FILTER_BY_ADDRFOLD ||
253 opt->filter_by == FILTER_BY_NAMEADDRFOLD)
254 addrfold = g_utf8_casefold (addr, -1);
256 switch (opt->filter_by) {
257 case FILTER_BY_NAMEADDR:
258 key = talloc_asprintf (opt->format, "%s <%s>", name, addr);
260 case FILTER_BY_NAMEADDRFOLD:
261 key = talloc_asprintf (opt->format, "%s <%s>", name, addrfold);
264 key = talloc_strdup (opt->format, name); /* !name results in !key */
267 key = talloc_strdup (opt->format, addr);
269 case FILTER_BY_ADDRFOLD:
270 key = talloc_strdup (opt->format, addrfold);
273 INTERNAL_ERROR("invalid --filter-by flags");
282 duplicate = g_hash_table_lookup_extended (addrs, key, NULL, (gpointer)&mailbox);
285 mailbox = talloc (opt->format, mailbox_t);
286 mailbox->name = talloc_strdup (mailbox, name);
287 mailbox->addr = talloc_strdup (mailbox, addr);
289 g_hash_table_insert (addrs, key, mailbox);
299 print_mailbox (const search_options_t *opt, const mailbox_t *mailbox)
301 const char *name = mailbox->name;
302 const char *addr = mailbox->addr;
303 int count = mailbox->count;
304 sprinter_t *format = opt->format;
305 InternetAddress *ia = internet_address_mailbox_new (name, addr);
308 /* name_addr has the name part quoted if necessary. Compare
309 * 'John Doe <john@doe.com>' vs. '"Doe, John" <john@doe.com>' */
310 name_addr = internet_address_to_string (ia, FALSE);
312 if (format->is_text_printer) {
314 format->integer (format, count);
315 format->string (format, "\t");
317 format->string (format, name_addr);
318 format->separator (format);
320 format->begin_map (format);
321 format->map_key (format, "name");
322 format->string (format, name);
323 format->map_key (format, "address");
324 format->string (format, addr);
325 format->map_key (format, "name-addr");
326 format->string (format, name_addr);
328 format->map_key (format, "count");
329 format->integer (format, count);
331 format->end (format);
332 format->separator (format);
339 /* Print or prepare for printing addresses from InternetAddressList. */
341 process_address_list (const search_options_t *opt, GHashTable *addrs,
342 InternetAddressList *list)
344 InternetAddress *address;
347 for (i = 0; i < internet_address_list_length (list); i++) {
348 address = internet_address_list_get_address (list, i);
349 if (INTERNET_ADDRESS_IS_GROUP (address)) {
350 InternetAddressGroup *group;
351 InternetAddressList *group_list;
353 group = INTERNET_ADDRESS_GROUP (address);
354 group_list = internet_address_group_get_members (group);
355 if (group_list == NULL)
358 process_address_list (opt, addrs, group_list);
360 InternetAddressMailbox *mailbox = INTERNET_ADDRESS_MAILBOX (address);
362 .name = internet_address_get_name (address),
363 .addr = internet_address_mailbox_get_addr (mailbox),
367 if (is_duplicate (opt, addrs, mbx.name, mbx.addr))
370 if (opt->output & OUTPUT_COUNT)
373 print_mailbox (opt, &mbx);
378 /* Print or prepare for printing addresses from a message header. */
380 process_address_header (const search_options_t *opt, GHashTable *addrs, const char *value)
382 InternetAddressList *list;
387 list = internet_address_list_parse_string (value);
391 process_address_list (opt, addrs, list);
393 g_object_unref (list);
397 _my_talloc_free_for_g_hash (void *ptr)
403 print_hash_value (unused (gpointer key), gpointer value, gpointer user_data)
405 const mailbox_t *mailbox = value;
406 search_options_t *opt = user_data;
408 print_mailbox (opt, mailbox);
412 do_search_messages (search_options_t *opt)
414 notmuch_message_t *message;
415 notmuch_messages_t *messages;
416 notmuch_filenames_t *filenames;
417 sprinter_t *format = opt->format;
418 GHashTable *addresses = NULL;
421 if (opt->output & OUTPUT_ADDRESS_FLAGS)
422 addresses = g_hash_table_new_full (g_str_hash, g_str_equal,
423 _my_talloc_free_for_g_hash, _my_talloc_free_for_g_hash);
425 if (opt->offset < 0) {
426 opt->offset += notmuch_query_count_messages (opt->query);
431 messages = notmuch_query_search_messages (opt->query);
432 if (messages == NULL)
435 format->begin_list (format);
438 notmuch_messages_valid (messages) && (opt->limit < 0 || i < opt->offset + opt->limit);
439 notmuch_messages_move_to_next (messages), i++)
444 message = notmuch_messages_get (messages);
446 if (opt->output == OUTPUT_FILES) {
448 filenames = notmuch_message_get_filenames (message);
451 notmuch_filenames_valid (filenames);
452 notmuch_filenames_move_to_next (filenames), j++)
454 if (opt->dupe < 0 || opt->dupe == j) {
455 format->string (format, notmuch_filenames_get (filenames));
456 format->separator (format);
460 notmuch_filenames_destroy( filenames );
462 } else if (opt->output == OUTPUT_MESSAGES) {
463 format->set_prefix (format, "id");
464 format->string (format,
465 notmuch_message_get_message_id (message));
466 format->separator (format);
468 if (opt->output & OUTPUT_SENDER) {
471 addrs = notmuch_message_get_header (message, "from");
472 process_address_header (opt, addresses, addrs);
475 if (opt->output & OUTPUT_RECIPIENTS) {
476 const char *hdrs[] = { "to", "cc", "bcc" };
480 for (j = 0; j < ARRAY_SIZE (hdrs); j++) {
481 addrs = notmuch_message_get_header (message, hdrs[j]);
482 process_address_header (opt, addresses, addrs);
487 notmuch_message_destroy (message);
490 if (addresses && opt->output & OUTPUT_COUNT)
491 g_hash_table_foreach (addresses, print_hash_value, opt);
494 g_hash_table_unref (addresses);
496 notmuch_messages_destroy (messages);
498 format->end (format);
504 do_search_tags (notmuch_database_t *notmuch,
505 const search_options_t *opt)
507 notmuch_messages_t *messages = NULL;
508 notmuch_tags_t *tags;
510 sprinter_t *format = opt->format;
511 notmuch_query_t *query = opt->query;
513 /* should the following only special case if no excluded terms
516 /* Special-case query of "*" for better performance. */
517 if (strcmp (notmuch_query_get_query_string (query), "*") == 0) {
518 tags = notmuch_database_get_all_tags (notmuch);
520 messages = notmuch_query_search_messages (query);
521 if (messages == NULL)
524 tags = notmuch_messages_collect_tags (messages);
529 format->begin_list (format);
532 notmuch_tags_valid (tags);
533 notmuch_tags_move_to_next (tags))
535 tag = notmuch_tags_get (tags);
537 format->string (format, tag);
538 format->separator (format);
542 notmuch_tags_destroy (tags);
545 notmuch_messages_destroy (messages);
547 format->end (format);
553 notmuch_search_command (notmuch_config_t *config, int argc, char *argv[])
555 notmuch_database_t *notmuch;
556 search_options_t opt = {
557 .sort = NOTMUCH_SORT_NEWEST_FIRST,
560 .limit = -1, /* unlimited */
562 .filter_by = FILTER_BY_NAMEADDR,
566 notmuch_exclude_t exclude = NOTMUCH_EXCLUDE_TRUE;
572 NOTMUCH_FORMAT_TEXT0,
574 } format_sel = NOTMUCH_FORMAT_TEXT;
576 notmuch_opt_desc_t options[] = {
577 { NOTMUCH_OPT_KEYWORD, &opt.sort, "sort", 's',
578 (notmuch_keyword_t []){ { "oldest-first", NOTMUCH_SORT_OLDEST_FIRST },
579 { "newest-first", NOTMUCH_SORT_NEWEST_FIRST },
581 { NOTMUCH_OPT_KEYWORD, &format_sel, "format", 'f',
582 (notmuch_keyword_t []){ { "json", NOTMUCH_FORMAT_JSON },
583 { "sexp", NOTMUCH_FORMAT_SEXP },
584 { "text", NOTMUCH_FORMAT_TEXT },
585 { "text0", NOTMUCH_FORMAT_TEXT0 },
587 { NOTMUCH_OPT_INT, ¬much_format_version, "format-version", 0, 0 },
588 { NOTMUCH_OPT_KEYWORD_FLAGS, &opt.output, "output", 'o',
589 (notmuch_keyword_t []){ { "summary", OUTPUT_SUMMARY },
590 { "threads", OUTPUT_THREADS },
591 { "messages", OUTPUT_MESSAGES },
592 { "sender", OUTPUT_SENDER },
593 { "recipients", OUTPUT_RECIPIENTS },
594 { "files", OUTPUT_FILES },
595 { "tags", OUTPUT_TAGS },
596 { "count", OUTPUT_COUNT },
598 { NOTMUCH_OPT_KEYWORD, &exclude, "exclude", 'x',
599 (notmuch_keyword_t []){ { "true", NOTMUCH_EXCLUDE_TRUE },
600 { "false", NOTMUCH_EXCLUDE_FALSE },
601 { "flag", NOTMUCH_EXCLUDE_FLAG },
602 { "all", NOTMUCH_EXCLUDE_ALL },
604 { NOTMUCH_OPT_INT, &opt.offset, "offset", 'O', 0 },
605 { NOTMUCH_OPT_INT, &opt.limit, "limit", 'L', 0 },
606 { NOTMUCH_OPT_INT, &opt.dupe, "duplicate", 'D', 0 },
607 { NOTMUCH_OPT_KEYWORD, &opt.filter_by, "filter-by", 'b',
608 (notmuch_keyword_t []){ { "nameaddr", FILTER_BY_NAMEADDR },
609 { "name", FILTER_BY_NAME },
610 { "addr", FILTER_BY_ADDR },
611 { "addrfold", FILTER_BY_ADDRFOLD },
612 { "nameaddrfold", FILTER_BY_NAMEADDRFOLD },
617 opt_index = parse_arguments (argc, argv, options, 1);
622 opt.output = OUTPUT_SUMMARY;
624 if (opt.filter_by && !(opt.output & OUTPUT_ADDRESS_FLAGS)) {
625 fprintf (stderr, "Error: --filter-by can only be used with address output.\n");
629 switch (format_sel) {
630 case NOTMUCH_FORMAT_TEXT:
631 opt.format = sprinter_text_create (config, stdout);
633 case NOTMUCH_FORMAT_TEXT0:
634 if (opt.output == OUTPUT_SUMMARY) {
635 fprintf (stderr, "Error: --format=text0 is not compatible with --output=summary.\n");
638 opt.format = sprinter_text0_create (config, stdout);
640 case NOTMUCH_FORMAT_JSON:
641 opt.format = sprinter_json_create (config, stdout);
643 case NOTMUCH_FORMAT_SEXP:
644 opt.format = sprinter_sexp_create (config, stdout);
647 /* this should never happen */
648 INTERNAL_ERROR("no output format selected");
651 notmuch_exit_if_unsupported_format ();
653 if (notmuch_database_open (notmuch_config_get_database_path (config),
654 NOTMUCH_DATABASE_MODE_READ_ONLY, ¬much))
657 query_str = query_string_from_args (notmuch, argc-opt_index, argv+opt_index);
658 if (query_str == NULL) {
659 fprintf (stderr, "Out of memory.\n");
662 if (*query_str == '\0') {
663 fprintf (stderr, "Error: notmuch search requires at least one search term.\n");
667 opt.query = notmuch_query_create (notmuch, query_str);
668 if (opt.query == NULL) {
669 fprintf (stderr, "Out of memory\n");
673 notmuch_query_set_sort (opt.query, opt.sort);
675 if (exclude == NOTMUCH_EXCLUDE_FLAG && opt.output != OUTPUT_SUMMARY) {
676 /* If we are not doing summary output there is nowhere to
677 * print the excluded flag so fall back on including the
678 * excluded messages. */
679 fprintf (stderr, "Warning: this output format cannot flag excluded messages.\n");
680 exclude = NOTMUCH_EXCLUDE_FALSE;
683 if (exclude != NOTMUCH_EXCLUDE_FALSE) {
684 const char **search_exclude_tags;
685 size_t search_exclude_tags_length;
687 search_exclude_tags = notmuch_config_get_search_exclude_tags
688 (config, &search_exclude_tags_length);
689 for (i = 0; i < search_exclude_tags_length; i++)
690 notmuch_query_add_tag_exclude (opt.query, search_exclude_tags[i]);
691 notmuch_query_set_omit_excluded (opt.query, exclude);
694 if (opt.output == OUTPUT_SUMMARY ||
695 opt.output == OUTPUT_THREADS)
696 ret = do_search_threads (&opt);
697 else if (opt.output == OUTPUT_MESSAGES ||
698 opt.output == OUTPUT_FILES ||
699 (opt.output & OUTPUT_ADDRESS_FLAGS && !(opt.output & ~OUTPUT_ADDRESS_FLAGS)))
700 ret = do_search_messages (&opt);
701 else if (opt.output == OUTPUT_TAGS)
702 ret = do_search_tags (notmuch, &opt);
704 fprintf (stderr, "Error: the combination of outputs is not supported.\n");
708 notmuch_query_destroy (opt.query);
709 notmuch_database_destroy (notmuch);
711 talloc_free (opt.format);
713 return ret ? EXIT_FAILURE : EXIT_SUCCESS;