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 duplicite. */
244 static notmuch_bool_t
245 check_duplicite (const search_options_t *opt, GHashTable *addrs, const char *name, const char *addr)
247 notmuch_bool_t duplicite;
251 if (opt->filter_by == FILTER_BY_ADDRFOLD ||
252 opt->filter_by == FILTER_BY_NAMEADDRFOLD) {
253 gchar *folded = g_utf8_casefold (addr, -1);
254 addr = talloc_strdup (opt->format, folded);
257 switch (opt->filter_by) {
258 case FILTER_BY_NAMEADDR:
259 case FILTER_BY_NAMEADDRFOLD:
260 key = talloc_asprintf (opt->format, "%s <%s>", name, addr);
263 key = talloc_strdup (opt->format, name); /* !name results in !key */
266 case FILTER_BY_ADDRFOLD:
267 key = talloc_strdup (opt->format, addr);
270 INTERNAL_ERROR("invalid --filter-by flags");
273 if (opt->filter_by == FILTER_BY_ADDRFOLD ||
274 opt->filter_by == FILTER_BY_NAMEADDRFOLD)
275 talloc_free ((char*)addr);
280 duplicite = g_hash_table_lookup_extended (addrs, key, NULL, (gpointer)&mailbox);
283 mailbox = talloc (opt->format, mailbox_t);
284 mailbox->name = talloc_strdup (mailbox, name);
285 mailbox->addr = talloc_strdup (mailbox, addr);
287 g_hash_table_insert (addrs, key, mailbox);
297 print_mailbox (const search_options_t *opt, const mailbox_t *mailbox)
299 const char *name = mailbox->name;
300 const char *addr = mailbox->addr;
301 int count = mailbox->count;
303 if (opt->format->is_text_printer) {
307 mailbox_str = talloc_asprintf (opt->format, "%s <%s>", name, addr);
309 mailbox_str = talloc_strdup (opt->format, addr);
312 fprintf (stderr, "Error: out of memory\n");
316 opt->format->integer (opt->format, count);
317 opt->format->string (opt->format, "\t");
319 opt->format->string (opt->format, mailbox_str);
320 opt->format->separator (opt->format);
322 talloc_free (mailbox_str);
324 opt->format->begin_map (opt->format);
325 opt->format->map_key (opt->format, "name");
326 opt->format->string (opt->format, name);
327 opt->format->map_key (opt->format, "address");
328 opt->format->string (opt->format, addr);
330 opt->format->map_key (opt->format, "count");
331 opt->format->integer (opt->format, count);
333 opt->format->end (opt->format);
334 opt->format->separator (opt->format);
339 process_address_list (const search_options_t *opt, GHashTable *addrs,
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 (opt, addrs, 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 if (check_duplicite (opt, addrs, mbx.name, mbx.addr))
368 if (opt->output & OUTPUT_COUNT)
371 print_mailbox (opt, &mbx);
377 process_address_header (const search_options_t *opt, GHashTable *addrs, const char *value)
379 InternetAddressList *list;
384 list = internet_address_list_parse_string (value);
388 process_address_list (opt, addrs, list);
392 _my_talloc_free_for_g_hash (void *ptr)
398 print_hash_value (unused (gpointer key), gpointer value, gpointer user_data)
400 const mailbox_t *mailbox = value;
401 search_options_t *opt = user_data;
403 print_mailbox (opt, mailbox);
407 do_search_messages (search_options_t *opt)
409 notmuch_message_t *message;
410 notmuch_messages_t *messages;
411 notmuch_filenames_t *filenames;
412 sprinter_t *format = opt->format;
413 GHashTable *addresses = NULL;
416 if (opt->output & OUTPUT_ADDRESS_FLAGS)
417 addresses = g_hash_table_new_full (g_str_hash, g_str_equal,
418 _my_talloc_free_for_g_hash, _my_talloc_free_for_g_hash);
420 if (opt->offset < 0) {
421 opt->offset += notmuch_query_count_messages (opt->query);
426 messages = notmuch_query_search_messages (opt->query);
427 if (messages == NULL)
430 format->begin_list (format);
433 notmuch_messages_valid (messages) && (opt->limit < 0 || i < opt->offset + opt->limit);
434 notmuch_messages_move_to_next (messages), i++)
439 message = notmuch_messages_get (messages);
441 if (opt->output == OUTPUT_FILES) {
443 filenames = notmuch_message_get_filenames (message);
446 notmuch_filenames_valid (filenames);
447 notmuch_filenames_move_to_next (filenames), j++)
449 if (opt->dupe < 0 || opt->dupe == j) {
450 format->string (format, notmuch_filenames_get (filenames));
451 format->separator (format);
455 notmuch_filenames_destroy( filenames );
457 } else if (opt->output == OUTPUT_MESSAGES) {
458 format->set_prefix (format, "id");
459 format->string (format,
460 notmuch_message_get_message_id (message));
461 format->separator (format);
463 if (opt->output & OUTPUT_SENDER) {
466 addrs = notmuch_message_get_header (message, "from");
467 process_address_header (opt, addresses, addrs);
470 if (opt->output & OUTPUT_RECIPIENTS) {
471 const char *hdrs[] = { "to", "cc", "bcc" };
475 for (j = 0; j < ARRAY_SIZE (hdrs); j++) {
476 addrs = notmuch_message_get_header (message, hdrs[j]);
477 process_address_header (opt, addresses, addrs);
482 notmuch_message_destroy (message);
485 if (addresses && opt->output & OUTPUT_COUNT)
486 g_hash_table_foreach (addresses, print_hash_value, opt);
489 g_hash_table_unref (addresses);
491 notmuch_messages_destroy (messages);
493 format->end (format);
499 do_search_tags (notmuch_database_t *notmuch,
500 const search_options_t *opt)
502 notmuch_messages_t *messages = NULL;
503 notmuch_tags_t *tags;
505 sprinter_t *format = opt->format;
506 notmuch_query_t *query = opt->query;
508 /* should the following only special case if no excluded terms
511 /* Special-case query of "*" for better performance. */
512 if (strcmp (notmuch_query_get_query_string (query), "*") == 0) {
513 tags = notmuch_database_get_all_tags (notmuch);
515 messages = notmuch_query_search_messages (query);
516 if (messages == NULL)
519 tags = notmuch_messages_collect_tags (messages);
524 format->begin_list (format);
527 notmuch_tags_valid (tags);
528 notmuch_tags_move_to_next (tags))
530 tag = notmuch_tags_get (tags);
532 format->string (format, tag);
533 format->separator (format);
537 notmuch_tags_destroy (tags);
540 notmuch_messages_destroy (messages);
542 format->end (format);
548 notmuch_search_command (notmuch_config_t *config, int argc, char *argv[])
550 notmuch_database_t *notmuch;
551 search_options_t opt = {
552 .sort = NOTMUCH_SORT_NEWEST_FIRST,
555 .limit = -1, /* unlimited */
557 .filter_by = FILTER_BY_NAMEADDR,
561 notmuch_exclude_t exclude = NOTMUCH_EXCLUDE_TRUE;
567 NOTMUCH_FORMAT_TEXT0,
569 } format_sel = NOTMUCH_FORMAT_TEXT;
571 notmuch_opt_desc_t options[] = {
572 { NOTMUCH_OPT_KEYWORD, &opt.sort, "sort", 's',
573 (notmuch_keyword_t []){ { "oldest-first", NOTMUCH_SORT_OLDEST_FIRST },
574 { "newest-first", NOTMUCH_SORT_NEWEST_FIRST },
576 { NOTMUCH_OPT_KEYWORD, &format_sel, "format", 'f',
577 (notmuch_keyword_t []){ { "json", NOTMUCH_FORMAT_JSON },
578 { "sexp", NOTMUCH_FORMAT_SEXP },
579 { "text", NOTMUCH_FORMAT_TEXT },
580 { "text0", NOTMUCH_FORMAT_TEXT0 },
582 { NOTMUCH_OPT_INT, ¬much_format_version, "format-version", 0, 0 },
583 { NOTMUCH_OPT_KEYWORD_FLAGS, &opt.output, "output", 'o',
584 (notmuch_keyword_t []){ { "summary", OUTPUT_SUMMARY },
585 { "threads", OUTPUT_THREADS },
586 { "messages", OUTPUT_MESSAGES },
587 { "sender", OUTPUT_SENDER },
588 { "recipients", OUTPUT_RECIPIENTS },
589 { "files", OUTPUT_FILES },
590 { "tags", OUTPUT_TAGS },
591 { "count", OUTPUT_COUNT },
593 { NOTMUCH_OPT_KEYWORD, &exclude, "exclude", 'x',
594 (notmuch_keyword_t []){ { "true", NOTMUCH_EXCLUDE_TRUE },
595 { "false", NOTMUCH_EXCLUDE_FALSE },
596 { "flag", NOTMUCH_EXCLUDE_FLAG },
597 { "all", NOTMUCH_EXCLUDE_ALL },
599 { NOTMUCH_OPT_INT, &opt.offset, "offset", 'O', 0 },
600 { NOTMUCH_OPT_INT, &opt.limit, "limit", 'L', 0 },
601 { NOTMUCH_OPT_INT, &opt.dupe, "duplicate", 'D', 0 },
602 { NOTMUCH_OPT_KEYWORD, &opt.filter_by, "filter-by", 'b',
603 (notmuch_keyword_t []){ { "nameaddr", FILTER_BY_NAMEADDR },
604 { "name", FILTER_BY_NAME },
605 { "addr", FILTER_BY_ADDR },
606 { "addrfold", FILTER_BY_ADDRFOLD },
607 { "nameaddrfold", FILTER_BY_NAMEADDRFOLD },
612 opt_index = parse_arguments (argc, argv, options, 1);
617 opt.output = OUTPUT_SUMMARY;
619 if (opt.filter_by && !(opt.output & OUTPUT_ADDRESS_FLAGS)) {
620 fprintf (stderr, "Error: --filter-by can only be used with address output.\n");
624 switch (format_sel) {
625 case NOTMUCH_FORMAT_TEXT:
626 opt.format = sprinter_text_create (config, stdout);
628 case NOTMUCH_FORMAT_TEXT0:
629 if (opt.output == OUTPUT_SUMMARY) {
630 fprintf (stderr, "Error: --format=text0 is not compatible with --output=summary.\n");
633 opt.format = sprinter_text0_create (config, stdout);
635 case NOTMUCH_FORMAT_JSON:
636 opt.format = sprinter_json_create (config, stdout);
638 case NOTMUCH_FORMAT_SEXP:
639 opt.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 (notmuch_config_get_database_path (config),
649 NOTMUCH_DATABASE_MODE_READ_ONLY, ¬much))
652 query_str = query_string_from_args (notmuch, argc-opt_index, argv+opt_index);
653 if (query_str == NULL) {
654 fprintf (stderr, "Out of memory.\n");
657 if (*query_str == '\0') {
658 fprintf (stderr, "Error: notmuch search requires at least one search term.\n");
662 opt.query = notmuch_query_create (notmuch, query_str);
663 if (opt.query == NULL) {
664 fprintf (stderr, "Out of memory\n");
668 notmuch_query_set_sort (opt.query, opt.sort);
670 if (exclude == NOTMUCH_EXCLUDE_FLAG && opt.output != OUTPUT_SUMMARY) {
671 /* If we are not doing summary output there is nowhere to
672 * print the excluded flag so fall back on including the
673 * excluded messages. */
674 fprintf (stderr, "Warning: this output format cannot flag excluded messages.\n");
675 exclude = NOTMUCH_EXCLUDE_FALSE;
678 if (exclude != NOTMUCH_EXCLUDE_FALSE) {
679 const char **search_exclude_tags;
680 size_t search_exclude_tags_length;
682 search_exclude_tags = notmuch_config_get_search_exclude_tags
683 (config, &search_exclude_tags_length);
684 for (i = 0; i < search_exclude_tags_length; i++)
685 notmuch_query_add_tag_exclude (opt.query, search_exclude_tags[i]);
686 notmuch_query_set_omit_excluded (opt.query, exclude);
689 if (opt.output == OUTPUT_SUMMARY ||
690 opt.output == OUTPUT_THREADS)
691 ret = do_search_threads (&opt);
692 else if (opt.output == OUTPUT_MESSAGES ||
693 opt.output == OUTPUT_FILES ||
694 (opt.output & OUTPUT_ADDRESS_FLAGS && !(opt.output & ~OUTPUT_ADDRESS_FLAGS)))
695 ret = do_search_messages (&opt);
696 else if (opt.output == OUTPUT_TAGS)
697 ret = do_search_tags (notmuch, &opt);
699 fprintf (stderr, "Error: the combination of outputs is not supported.\n");
703 notmuch_query_destroy (opt.query);
704 notmuch_database_destroy (notmuch);
706 talloc_free (opt.format);
708 return ret ? EXIT_FAILURE : EXIT_SUCCESS;