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,
35 #define OUTPUT_ADDRESS_FLAGS (OUTPUT_SENDER | OUTPUT_RECIPIENTS)
38 FILTER_BY_NAMEADDR = 0,
42 FILTER_BY_NAMEADDRFOLD,
47 notmuch_query_t *query;
53 filter_by_t filter_by;
61 /* Return two stable query strings that identify exactly the matched
62 * and unmatched messages currently in thread. If there are no
63 * matched or unmatched messages, the returned buffers will be
66 get_thread_query (notmuch_thread_t *thread,
67 char **matched_out, char **unmatched_out)
69 notmuch_messages_t *messages;
71 size_t escaped_len = 0;
73 *matched_out = *unmatched_out = NULL;
75 for (messages = notmuch_thread_get_messages (thread);
76 notmuch_messages_valid (messages);
77 notmuch_messages_move_to_next (messages))
79 notmuch_message_t *message = notmuch_messages_get (messages);
80 const char *mid = notmuch_message_get_message_id (message);
81 /* Determine which query buffer to extend */
82 char **buf = notmuch_message_get_flag (
83 message, NOTMUCH_MESSAGE_FLAG_MATCH) ? matched_out : unmatched_out;
84 /* Add this message's id: query. Since "id" is an exclusive
85 * prefix, it is implicitly 'or'd together, so we only need to
86 * join queries with a space. */
87 if (make_boolean_term (thread, "id", mid, &escaped, &escaped_len) < 0)
90 *buf = talloc_asprintf_append_buffer (*buf, " %s", escaped);
92 *buf = talloc_strdup (thread, escaped);
96 talloc_free (escaped);
101 do_search_threads (search_options_t *opt)
103 notmuch_thread_t *thread;
104 notmuch_threads_t *threads;
105 notmuch_tags_t *tags;
106 sprinter_t *format = opt->format;
110 if (opt->offset < 0) {
111 opt->offset += notmuch_query_count_threads (opt->query);
116 threads = notmuch_query_search_threads (opt->query);
120 format->begin_list (format);
123 notmuch_threads_valid (threads) && (opt->limit < 0 || i < opt->offset + opt->limit);
124 notmuch_threads_move_to_next (threads), i++)
126 thread = notmuch_threads_get (threads);
128 if (i < opt->offset) {
129 notmuch_thread_destroy (thread);
133 if (opt->output == OUTPUT_THREADS) {
134 format->set_prefix (format, "thread");
135 format->string (format,
136 notmuch_thread_get_thread_id (thread));
137 format->separator (format);
138 } else { /* output == OUTPUT_SUMMARY */
139 void *ctx_quote = talloc_new (thread);
140 const char *authors = notmuch_thread_get_authors (thread);
141 const char *subject = notmuch_thread_get_subject (thread);
142 const char *thread_id = notmuch_thread_get_thread_id (thread);
143 int matched = notmuch_thread_get_matched_messages (thread);
144 int total = notmuch_thread_get_total_messages (thread);
145 const char *relative_date = NULL;
146 notmuch_bool_t first_tag = TRUE;
148 format->begin_map (format);
150 if (opt->sort == NOTMUCH_SORT_OLDEST_FIRST)
151 date = notmuch_thread_get_oldest_date (thread);
153 date = notmuch_thread_get_newest_date (thread);
155 relative_date = notmuch_time_relative_date (ctx_quote, date);
157 if (format->is_text_printer) {
158 /* Special case for the text formatter */
159 printf ("thread:%s %12s [%d/%d] %s; %s (",
164 sanitize_string (ctx_quote, authors),
165 sanitize_string (ctx_quote, subject));
166 } else { /* Structured Output */
167 format->map_key (format, "thread");
168 format->string (format, thread_id);
169 format->map_key (format, "timestamp");
170 format->integer (format, date);
171 format->map_key (format, "date_relative");
172 format->string (format, relative_date);
173 format->map_key (format, "matched");
174 format->integer (format, matched);
175 format->map_key (format, "total");
176 format->integer (format, total);
177 format->map_key (format, "authors");
178 format->string (format, authors);
179 format->map_key (format, "subject");
180 format->string (format, subject);
181 if (notmuch_format_version >= 2) {
182 char *matched_query, *unmatched_query;
183 if (get_thread_query (thread, &matched_query,
184 &unmatched_query) < 0) {
185 fprintf (stderr, "Out of memory\n");
188 format->map_key (format, "query");
189 format->begin_list (format);
191 format->string (format, matched_query);
193 format->null (format);
195 format->string (format, unmatched_query);
197 format->null (format);
198 format->end (format);
202 talloc_free (ctx_quote);
204 format->map_key (format, "tags");
205 format->begin_list (format);
207 for (tags = notmuch_thread_get_tags (thread);
208 notmuch_tags_valid (tags);
209 notmuch_tags_move_to_next (tags))
211 const char *tag = notmuch_tags_get (tags);
213 if (format->is_text_printer) {
214 /* Special case for the text formatter */
220 } else { /* Structured Output */
221 format->string (format, tag);
225 if (format->is_text_printer)
228 format->end (format);
229 format->end (format);
230 format->separator (format);
233 notmuch_thread_destroy (thread);
236 format->end (format);
241 /* Returns TRUE iff name and/or addr is considered duplicite. */
242 static notmuch_bool_t
243 check_duplicite (const search_options_t *opt, GHashTable *addrs, const char *name, const char *addr)
245 notmuch_bool_t duplicite;
248 if (opt->filter_by == FILTER_BY_ADDRFOLD ||
249 opt->filter_by == FILTER_BY_NAMEADDRFOLD) {
250 gchar *folded = g_utf8_casefold (addr, -1);
251 addr = talloc_strdup (opt->format, folded);
254 switch (opt->filter_by) {
255 case FILTER_BY_NAMEADDR:
256 case FILTER_BY_NAMEADDRFOLD:
257 key = talloc_asprintf (opt->format, "%s <%s>", name, addr);
260 key = talloc_strdup (opt->format, name); /* !name results in !key */
263 case FILTER_BY_ADDRFOLD:
264 key = talloc_strdup (opt->format, addr);
267 INTERNAL_ERROR("invalid --filter-by flags");
270 if (opt->filter_by == FILTER_BY_ADDRFOLD ||
271 opt->filter_by == FILTER_BY_NAMEADDRFOLD)
272 talloc_free ((char*)addr);
277 duplicite = g_hash_table_lookup_extended (addrs, key, NULL, NULL);
280 g_hash_table_insert (addrs, key, NULL);
288 print_mailbox (const search_options_t *opt, const mailbox_t *mailbox)
290 const char *name = mailbox->name;
291 const char *addr = mailbox->addr;
293 if (opt->format->is_text_printer) {
297 mailbox_str = talloc_asprintf (opt->format, "%s <%s>", name, addr);
299 mailbox_str = talloc_strdup (opt->format, addr);
302 fprintf (stderr, "Error: out of memory\n");
305 opt->format->string (opt->format, mailbox_str);
306 opt->format->separator (opt->format);
308 talloc_free (mailbox_str);
310 opt->format->begin_map (opt->format);
311 opt->format->map_key (opt->format, "name");
312 opt->format->string (opt->format, name);
313 opt->format->map_key (opt->format, "address");
314 opt->format->string (opt->format, addr);
315 opt->format->end (opt->format);
316 opt->format->separator (opt->format);
321 process_address_list (const search_options_t *opt, GHashTable *addrs,
322 InternetAddressList *list)
324 InternetAddress *address;
327 for (i = 0; i < internet_address_list_length (list); i++) {
328 address = internet_address_list_get_address (list, i);
329 if (INTERNET_ADDRESS_IS_GROUP (address)) {
330 InternetAddressGroup *group;
331 InternetAddressList *group_list;
333 group = INTERNET_ADDRESS_GROUP (address);
334 group_list = internet_address_group_get_members (group);
335 if (group_list == NULL)
338 process_address_list (opt, addrs, group_list);
340 InternetAddressMailbox *mailbox = INTERNET_ADDRESS_MAILBOX (address);
342 .name = internet_address_get_name (address),
343 .addr = internet_address_mailbox_get_addr (mailbox),
346 if (check_duplicite (opt, addrs, mbx.name, mbx.addr))
349 print_mailbox (opt, &mbx);
355 process_address_header (const search_options_t *opt, GHashTable *addrs, const char *value)
357 InternetAddressList *list;
362 list = internet_address_list_parse_string (value);
366 process_address_list (opt, addrs, list);
370 _my_talloc_free_for_g_hash (void *ptr)
376 do_search_messages (search_options_t *opt)
378 notmuch_message_t *message;
379 notmuch_messages_t *messages;
380 notmuch_filenames_t *filenames;
381 sprinter_t *format = opt->format;
382 GHashTable *addresses = NULL;
385 if (opt->output & OUTPUT_ADDRESS_FLAGS)
386 addresses = g_hash_table_new_full (g_str_hash, g_str_equal,
387 _my_talloc_free_for_g_hash, NULL);
389 if (opt->offset < 0) {
390 opt->offset += notmuch_query_count_messages (opt->query);
395 messages = notmuch_query_search_messages (opt->query);
396 if (messages == NULL)
399 format->begin_list (format);
402 notmuch_messages_valid (messages) && (opt->limit < 0 || i < opt->offset + opt->limit);
403 notmuch_messages_move_to_next (messages), i++)
408 message = notmuch_messages_get (messages);
410 if (opt->output == OUTPUT_FILES) {
412 filenames = notmuch_message_get_filenames (message);
415 notmuch_filenames_valid (filenames);
416 notmuch_filenames_move_to_next (filenames), j++)
418 if (opt->dupe < 0 || opt->dupe == j) {
419 format->string (format, notmuch_filenames_get (filenames));
420 format->separator (format);
424 notmuch_filenames_destroy( filenames );
426 } else if (opt->output == OUTPUT_MESSAGES) {
427 format->set_prefix (format, "id");
428 format->string (format,
429 notmuch_message_get_message_id (message));
430 format->separator (format);
432 if (opt->output & OUTPUT_SENDER) {
435 addrs = notmuch_message_get_header (message, "from");
436 process_address_header (opt, addresses, addrs);
439 if (opt->output & OUTPUT_RECIPIENTS) {
440 const char *hdrs[] = { "to", "cc", "bcc" };
444 for (j = 0; j < ARRAY_SIZE (hdrs); j++) {
445 addrs = notmuch_message_get_header (message, hdrs[j]);
446 process_address_header (opt, addresses, addrs);
451 notmuch_message_destroy (message);
455 g_hash_table_unref (addresses);
457 notmuch_messages_destroy (messages);
459 format->end (format);
465 do_search_tags (notmuch_database_t *notmuch,
466 const search_options_t *opt)
468 notmuch_messages_t *messages = NULL;
469 notmuch_tags_t *tags;
471 sprinter_t *format = opt->format;
472 notmuch_query_t *query = opt->query;
474 /* should the following only special case if no excluded terms
477 /* Special-case query of "*" for better performance. */
478 if (strcmp (notmuch_query_get_query_string (query), "*") == 0) {
479 tags = notmuch_database_get_all_tags (notmuch);
481 messages = notmuch_query_search_messages (query);
482 if (messages == NULL)
485 tags = notmuch_messages_collect_tags (messages);
490 format->begin_list (format);
493 notmuch_tags_valid (tags);
494 notmuch_tags_move_to_next (tags))
496 tag = notmuch_tags_get (tags);
498 format->string (format, tag);
499 format->separator (format);
503 notmuch_tags_destroy (tags);
506 notmuch_messages_destroy (messages);
508 format->end (format);
514 notmuch_search_command (notmuch_config_t *config, int argc, char *argv[])
516 notmuch_database_t *notmuch;
517 search_options_t opt = {
518 .sort = NOTMUCH_SORT_NEWEST_FIRST,
521 .limit = -1, /* unlimited */
523 .filter_by = FILTER_BY_NAMEADDR,
527 notmuch_exclude_t exclude = NOTMUCH_EXCLUDE_TRUE;
533 NOTMUCH_FORMAT_TEXT0,
535 } format_sel = NOTMUCH_FORMAT_TEXT;
537 notmuch_opt_desc_t options[] = {
538 { NOTMUCH_OPT_KEYWORD, &opt.sort, "sort", 's',
539 (notmuch_keyword_t []){ { "oldest-first", NOTMUCH_SORT_OLDEST_FIRST },
540 { "newest-first", NOTMUCH_SORT_NEWEST_FIRST },
542 { NOTMUCH_OPT_KEYWORD, &format_sel, "format", 'f',
543 (notmuch_keyword_t []){ { "json", NOTMUCH_FORMAT_JSON },
544 { "sexp", NOTMUCH_FORMAT_SEXP },
545 { "text", NOTMUCH_FORMAT_TEXT },
546 { "text0", NOTMUCH_FORMAT_TEXT0 },
548 { NOTMUCH_OPT_INT, ¬much_format_version, "format-version", 0, 0 },
549 { NOTMUCH_OPT_KEYWORD_FLAGS, &opt.output, "output", 'o',
550 (notmuch_keyword_t []){ { "summary", OUTPUT_SUMMARY },
551 { "threads", OUTPUT_THREADS },
552 { "messages", OUTPUT_MESSAGES },
553 { "sender", OUTPUT_SENDER },
554 { "recipients", OUTPUT_RECIPIENTS },
555 { "files", OUTPUT_FILES },
556 { "tags", OUTPUT_TAGS },
558 { NOTMUCH_OPT_KEYWORD, &exclude, "exclude", 'x',
559 (notmuch_keyword_t []){ { "true", NOTMUCH_EXCLUDE_TRUE },
560 { "false", NOTMUCH_EXCLUDE_FALSE },
561 { "flag", NOTMUCH_EXCLUDE_FLAG },
562 { "all", NOTMUCH_EXCLUDE_ALL },
564 { NOTMUCH_OPT_INT, &opt.offset, "offset", 'O', 0 },
565 { NOTMUCH_OPT_INT, &opt.limit, "limit", 'L', 0 },
566 { NOTMUCH_OPT_INT, &opt.dupe, "duplicate", 'D', 0 },
567 { NOTMUCH_OPT_KEYWORD, &opt.filter_by, "filter-by", 'b',
568 (notmuch_keyword_t []){ { "nameaddr", FILTER_BY_NAMEADDR },
569 { "name", FILTER_BY_NAME },
570 { "addr", FILTER_BY_ADDR },
571 { "addrfold", FILTER_BY_ADDRFOLD },
572 { "nameaddrfold", FILTER_BY_NAMEADDRFOLD },
577 opt_index = parse_arguments (argc, argv, options, 1);
582 opt.output = OUTPUT_SUMMARY;
584 if (opt.filter_by && !(opt.output & OUTPUT_ADDRESS_FLAGS)) {
585 fprintf (stderr, "Error: --filter-by can only be used with address output.\n");
589 switch (format_sel) {
590 case NOTMUCH_FORMAT_TEXT:
591 opt.format = sprinter_text_create (config, stdout);
593 case NOTMUCH_FORMAT_TEXT0:
594 if (opt.output == OUTPUT_SUMMARY) {
595 fprintf (stderr, "Error: --format=text0 is not compatible with --output=summary.\n");
598 opt.format = sprinter_text0_create (config, stdout);
600 case NOTMUCH_FORMAT_JSON:
601 opt.format = sprinter_json_create (config, stdout);
603 case NOTMUCH_FORMAT_SEXP:
604 opt.format = sprinter_sexp_create (config, stdout);
607 /* this should never happen */
608 INTERNAL_ERROR("no output format selected");
611 notmuch_exit_if_unsupported_format ();
613 if (notmuch_database_open (notmuch_config_get_database_path (config),
614 NOTMUCH_DATABASE_MODE_READ_ONLY, ¬much))
617 query_str = query_string_from_args (notmuch, argc-opt_index, argv+opt_index);
618 if (query_str == NULL) {
619 fprintf (stderr, "Out of memory.\n");
622 if (*query_str == '\0') {
623 fprintf (stderr, "Error: notmuch search requires at least one search term.\n");
627 opt.query = notmuch_query_create (notmuch, query_str);
628 if (opt.query == NULL) {
629 fprintf (stderr, "Out of memory\n");
633 notmuch_query_set_sort (opt.query, opt.sort);
635 if (exclude == NOTMUCH_EXCLUDE_FLAG && opt.output != OUTPUT_SUMMARY) {
636 /* If we are not doing summary output there is nowhere to
637 * print the excluded flag so fall back on including the
638 * excluded messages. */
639 fprintf (stderr, "Warning: this output format cannot flag excluded messages.\n");
640 exclude = NOTMUCH_EXCLUDE_FALSE;
643 if (exclude != NOTMUCH_EXCLUDE_FALSE) {
644 const char **search_exclude_tags;
645 size_t search_exclude_tags_length;
647 search_exclude_tags = notmuch_config_get_search_exclude_tags
648 (config, &search_exclude_tags_length);
649 for (i = 0; i < search_exclude_tags_length; i++)
650 notmuch_query_add_tag_exclude (opt.query, search_exclude_tags[i]);
651 notmuch_query_set_omit_excluded (opt.query, exclude);
654 if (opt.output == OUTPUT_SUMMARY ||
655 opt.output == OUTPUT_THREADS)
656 ret = do_search_threads (&opt);
657 else if (opt.output == OUTPUT_MESSAGES ||
658 opt.output == OUTPUT_FILES ||
659 (opt.output & OUTPUT_ADDRESS_FLAGS && !(opt.output & ~OUTPUT_ADDRESS_FLAGS)))
660 ret = do_search_messages (&opt);
661 else if (opt.output == OUTPUT_TAGS)
662 ret = do_search_tags (notmuch, &opt);
664 fprintf (stderr, "Error: the combination of outputs is not supported.\n");
668 notmuch_query_destroy (opt.query);
669 notmuch_database_destroy (notmuch);
671 talloc_free (opt.format);
673 return ret ? EXIT_FAILURE : EXIT_SUCCESS;