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,
47 FILTER_BY_NAMEADDR = 0,
51 FILTER_BY_NAMEADDRFOLD,
55 notmuch_database_t *notmuch;
56 format_sel_t format_sel;
58 notmuch_exclude_t exclude;
59 notmuch_query_t *query;
65 GHashTable *addresses;
66 filter_by_t filter_by;
75 /* Return two stable query strings that identify exactly the matched
76 * and unmatched messages currently in thread. If there are no
77 * matched or unmatched messages, the returned buffers will be
80 get_thread_query (notmuch_thread_t *thread,
81 char **matched_out, char **unmatched_out)
83 notmuch_messages_t *messages;
85 size_t escaped_len = 0;
87 *matched_out = *unmatched_out = NULL;
89 for (messages = notmuch_thread_get_messages (thread);
90 notmuch_messages_valid (messages);
91 notmuch_messages_move_to_next (messages))
93 notmuch_message_t *message = notmuch_messages_get (messages);
94 const char *mid = notmuch_message_get_message_id (message);
95 /* Determine which query buffer to extend */
96 char **buf = notmuch_message_get_flag (
97 message, NOTMUCH_MESSAGE_FLAG_MATCH) ? matched_out : unmatched_out;
98 /* Add this message's id: query. Since "id" is an exclusive
99 * prefix, it is implicitly 'or'd together, so we only need to
100 * join queries with a space. */
101 if (make_boolean_term (thread, "id", mid, &escaped, &escaped_len) < 0)
104 *buf = talloc_asprintf_append_buffer (*buf, " %s", escaped);
106 *buf = talloc_strdup (thread, escaped);
110 talloc_free (escaped);
115 do_search_threads (search_context_t *ctx)
117 notmuch_thread_t *thread;
118 notmuch_threads_t *threads;
119 notmuch_tags_t *tags;
120 sprinter_t *format = ctx->format;
124 if (ctx->offset < 0) {
125 ctx->offset += notmuch_query_count_threads (ctx->query);
130 threads = notmuch_query_search_threads (ctx->query);
134 format->begin_list (format);
137 notmuch_threads_valid (threads) && (ctx->limit < 0 || i < ctx->offset + ctx->limit);
138 notmuch_threads_move_to_next (threads), i++)
140 thread = notmuch_threads_get (threads);
142 if (i < ctx->offset) {
143 notmuch_thread_destroy (thread);
147 if (ctx->output == OUTPUT_THREADS) {
148 format->set_prefix (format, "thread");
149 format->string (format,
150 notmuch_thread_get_thread_id (thread));
151 format->separator (format);
152 } else { /* output == OUTPUT_SUMMARY */
153 void *ctx_quote = talloc_new (thread);
154 const char *authors = notmuch_thread_get_authors (thread);
155 const char *subject = notmuch_thread_get_subject (thread);
156 const char *thread_id = notmuch_thread_get_thread_id (thread);
157 int matched = notmuch_thread_get_matched_messages (thread);
158 int total = notmuch_thread_get_total_messages (thread);
159 const char *relative_date = NULL;
160 notmuch_bool_t first_tag = TRUE;
162 format->begin_map (format);
164 if (ctx->sort == NOTMUCH_SORT_OLDEST_FIRST)
165 date = notmuch_thread_get_oldest_date (thread);
167 date = notmuch_thread_get_newest_date (thread);
169 relative_date = notmuch_time_relative_date (ctx_quote, date);
171 if (format->is_text_printer) {
172 /* Special case for the text formatter */
173 printf ("thread:%s %12s [%d/%d] %s; %s (",
178 sanitize_string (ctx_quote, authors),
179 sanitize_string (ctx_quote, subject));
180 } else { /* Structured Output */
181 format->map_key (format, "thread");
182 format->string (format, thread_id);
183 format->map_key (format, "timestamp");
184 format->integer (format, date);
185 format->map_key (format, "date_relative");
186 format->string (format, relative_date);
187 format->map_key (format, "matched");
188 format->integer (format, matched);
189 format->map_key (format, "total");
190 format->integer (format, total);
191 format->map_key (format, "authors");
192 format->string (format, authors);
193 format->map_key (format, "subject");
194 format->string (format, subject);
195 if (notmuch_format_version >= 2) {
196 char *matched_query, *unmatched_query;
197 if (get_thread_query (thread, &matched_query,
198 &unmatched_query) < 0) {
199 fprintf (stderr, "Out of memory\n");
202 format->map_key (format, "query");
203 format->begin_list (format);
205 format->string (format, matched_query);
207 format->null (format);
209 format->string (format, unmatched_query);
211 format->null (format);
212 format->end (format);
216 talloc_free (ctx_quote);
218 format->map_key (format, "tags");
219 format->begin_list (format);
221 for (tags = notmuch_thread_get_tags (thread);
222 notmuch_tags_valid (tags);
223 notmuch_tags_move_to_next (tags))
225 const char *tag = notmuch_tags_get (tags);
227 if (format->is_text_printer) {
228 /* Special case for the text formatter */
234 } else { /* Structured Output */
235 format->string (format, tag);
239 if (format->is_text_printer)
242 format->end (format);
243 format->end (format);
244 format->separator (format);
247 notmuch_thread_destroy (thread);
250 format->end (format);
255 /* Returns TRUE iff name and/or addr is considered duplicate. */
256 static notmuch_bool_t
257 is_duplicate (const search_context_t *ctx, const char *name, const char *addr)
259 notmuch_bool_t duplicate;
261 gchar *addrfold = NULL;
264 if (ctx->filter_by == FILTER_BY_ADDRFOLD ||
265 ctx->filter_by == FILTER_BY_NAMEADDRFOLD)
266 addrfold = g_utf8_casefold (addr, -1);
268 switch (ctx->filter_by) {
269 case FILTER_BY_NAMEADDR:
270 key = talloc_asprintf (ctx->format, "%s <%s>", name, addr);
272 case FILTER_BY_NAMEADDRFOLD:
273 key = talloc_asprintf (ctx->format, "%s <%s>", name, addrfold);
276 key = talloc_strdup (ctx->format, name); /* !name results in !key */
279 key = talloc_strdup (ctx->format, addr);
281 case FILTER_BY_ADDRFOLD:
282 key = talloc_strdup (ctx->format, addrfold);
285 INTERNAL_ERROR("invalid --filter-by flags");
294 duplicate = g_hash_table_lookup_extended (ctx->addresses, key, NULL, (gpointer)&mailbox);
297 mailbox = talloc (ctx->format, mailbox_t);
298 mailbox->name = talloc_strdup (mailbox, name);
299 mailbox->addr = talloc_strdup (mailbox, addr);
301 g_hash_table_insert (ctx->addresses, key, mailbox);
311 print_mailbox (const search_context_t *ctx, const mailbox_t *mailbox)
313 const char *name = mailbox->name;
314 const char *addr = mailbox->addr;
315 int count = mailbox->count;
316 sprinter_t *format = ctx->format;
317 InternetAddress *ia = internet_address_mailbox_new (name, addr);
320 /* name_addr has the name part quoted if necessary. Compare
321 * 'John Doe <john@doe.com>' vs. '"Doe, John" <john@doe.com>' */
322 name_addr = internet_address_to_string (ia, FALSE);
324 if (format->is_text_printer) {
326 format->integer (format, count);
327 format->string (format, "\t");
329 format->string (format, name_addr);
330 format->separator (format);
332 format->begin_map (format);
333 format->map_key (format, "name");
334 format->string (format, name);
335 format->map_key (format, "address");
336 format->string (format, addr);
337 format->map_key (format, "name-addr");
338 format->string (format, name_addr);
340 format->map_key (format, "count");
341 format->integer (format, count);
343 format->end (format);
344 format->separator (format);
351 /* Print or prepare for printing addresses from InternetAddressList. */
353 process_address_list (const search_context_t *ctx,
354 InternetAddressList *list)
356 InternetAddress *address;
359 for (i = 0; i < internet_address_list_length (list); i++) {
360 address = internet_address_list_get_address (list, i);
361 if (INTERNET_ADDRESS_IS_GROUP (address)) {
362 InternetAddressGroup *group;
363 InternetAddressList *group_list;
365 group = INTERNET_ADDRESS_GROUP (address);
366 group_list = internet_address_group_get_members (group);
367 if (group_list == NULL)
370 process_address_list (ctx, group_list);
372 InternetAddressMailbox *mailbox = INTERNET_ADDRESS_MAILBOX (address);
374 .name = internet_address_get_name (address),
375 .addr = internet_address_mailbox_get_addr (mailbox),
379 if (is_duplicate (ctx, mbx.name, mbx.addr))
382 if (ctx->output & OUTPUT_COUNT)
385 print_mailbox (ctx, &mbx);
390 /* Print or prepare for printing addresses from a message header. */
392 process_address_header (const search_context_t *ctx, const char *value)
394 InternetAddressList *list;
399 list = internet_address_list_parse_string (value);
403 process_address_list (ctx, list);
405 g_object_unref (list);
409 _my_talloc_free_for_g_hash (void *ptr)
415 print_hash_value (unused (gpointer key), gpointer value, gpointer user_data)
417 const mailbox_t *mailbox = value;
418 search_context_t *ctx = user_data;
420 print_mailbox (ctx, mailbox);
424 _count_filenames (notmuch_message_t *message)
426 notmuch_filenames_t *filenames;
429 filenames = notmuch_message_get_filenames (message);
431 while (notmuch_filenames_valid (filenames)) {
432 notmuch_filenames_move_to_next (filenames);
436 notmuch_filenames_destroy (filenames);
442 do_search_messages (search_context_t *ctx)
444 notmuch_message_t *message;
445 notmuch_messages_t *messages;
446 notmuch_filenames_t *filenames;
447 sprinter_t *format = ctx->format;
450 if (ctx->offset < 0) {
451 ctx->offset += notmuch_query_count_messages (ctx->query);
456 messages = notmuch_query_search_messages (ctx->query);
457 if (messages == NULL)
460 format->begin_list (format);
463 notmuch_messages_valid (messages) && (ctx->limit < 0 || i < ctx->offset + ctx->limit);
464 notmuch_messages_move_to_next (messages), i++)
469 message = notmuch_messages_get (messages);
471 if (ctx->output == OUTPUT_FILES) {
473 filenames = notmuch_message_get_filenames (message);
476 notmuch_filenames_valid (filenames);
477 notmuch_filenames_move_to_next (filenames), j++)
479 if (ctx->dupe < 0 || ctx->dupe == j) {
480 format->string (format, notmuch_filenames_get (filenames));
481 format->separator (format);
485 notmuch_filenames_destroy( filenames );
487 } else if (ctx->output == OUTPUT_MESSAGES) {
488 /* special case 1 for speed */
489 if (ctx->dupe <= 1 || ctx->dupe <= _count_filenames (message)) {
490 format->set_prefix (format, "id");
491 format->string (format,
492 notmuch_message_get_message_id (message));
493 format->separator (format);
496 if (ctx->output & OUTPUT_SENDER) {
499 addrs = notmuch_message_get_header (message, "from");
500 process_address_header (ctx, addrs);
503 if (ctx->output & OUTPUT_RECIPIENTS) {
504 const char *hdrs[] = { "to", "cc", "bcc" };
508 for (j = 0; j < ARRAY_SIZE (hdrs); j++) {
509 addrs = notmuch_message_get_header (message, hdrs[j]);
510 process_address_header (ctx, addrs);
515 notmuch_message_destroy (message);
518 if (ctx->addresses && ctx->output & OUTPUT_COUNT)
519 g_hash_table_foreach (ctx->addresses, print_hash_value, ctx);
521 notmuch_messages_destroy (messages);
523 format->end (format);
529 do_search_tags (const search_context_t *ctx)
531 notmuch_messages_t *messages = NULL;
532 notmuch_tags_t *tags;
534 sprinter_t *format = ctx->format;
535 notmuch_query_t *query = ctx->query;
536 notmuch_database_t *notmuch = ctx->notmuch;
538 /* should the following only special case if no excluded terms
541 /* Special-case query of "*" for better performance. */
542 if (strcmp (notmuch_query_get_query_string (query), "*") == 0) {
543 tags = notmuch_database_get_all_tags (notmuch);
545 messages = notmuch_query_search_messages (query);
546 if (messages == NULL)
549 tags = notmuch_messages_collect_tags (messages);
554 format->begin_list (format);
557 notmuch_tags_valid (tags);
558 notmuch_tags_move_to_next (tags))
560 tag = notmuch_tags_get (tags);
562 format->string (format, tag);
563 format->separator (format);
567 notmuch_tags_destroy (tags);
570 notmuch_messages_destroy (messages);
572 format->end (format);
578 _notmuch_search_prepare (search_context_t *ctx, notmuch_config_t *config, int argc, char *argv[])
583 switch (ctx->format_sel) {
584 case NOTMUCH_FORMAT_TEXT:
585 ctx->format = sprinter_text_create (config, stdout);
587 case NOTMUCH_FORMAT_TEXT0:
588 if (ctx->output == OUTPUT_SUMMARY) {
589 fprintf (stderr, "Error: --format=text0 is not compatible with --output=summary.\n");
592 ctx->format = sprinter_text0_create (config, stdout);
594 case NOTMUCH_FORMAT_JSON:
595 ctx->format = sprinter_json_create (config, stdout);
597 case NOTMUCH_FORMAT_SEXP:
598 ctx->format = sprinter_sexp_create (config, stdout);
601 /* this should never happen */
602 INTERNAL_ERROR("no output format selected");
605 notmuch_exit_if_unsupported_format ();
607 if (notmuch_database_open (notmuch_config_get_database_path (config),
608 NOTMUCH_DATABASE_MODE_READ_ONLY, &ctx->notmuch))
611 query_str = query_string_from_args (ctx->notmuch, argc, argv);
612 if (query_str == NULL) {
613 fprintf (stderr, "Out of memory.\n");
616 if (*query_str == '\0') {
617 fprintf (stderr, "Error: notmuch search requires at least one search term.\n");
621 ctx->query = notmuch_query_create (ctx->notmuch, query_str);
622 if (ctx->query == NULL) {
623 fprintf (stderr, "Out of memory\n");
627 notmuch_query_set_sort (ctx->query, ctx->sort);
629 if (ctx->exclude == NOTMUCH_EXCLUDE_FLAG && ctx->output != OUTPUT_SUMMARY) {
630 /* If we are not doing summary output there is nowhere to
631 * print the excluded flag so fall back on including the
632 * excluded messages. */
633 fprintf (stderr, "Warning: this output format cannot flag excluded messages.\n");
634 ctx->exclude = NOTMUCH_EXCLUDE_FALSE;
637 if (ctx->exclude != NOTMUCH_EXCLUDE_FALSE) {
638 const char **search_exclude_tags;
639 size_t search_exclude_tags_length;
641 search_exclude_tags = notmuch_config_get_search_exclude_tags
642 (config, &search_exclude_tags_length);
643 for (i = 0; i < search_exclude_tags_length; i++)
644 notmuch_query_add_tag_exclude (ctx->query, search_exclude_tags[i]);
645 notmuch_query_set_omit_excluded (ctx->query, ctx->exclude);
652 _notmuch_search_cleanup (search_context_t *ctx)
654 notmuch_query_destroy (ctx->query);
655 notmuch_database_destroy (ctx->notmuch);
657 talloc_free (ctx->format);
660 static search_context_t search_context = {
661 .format_sel = NOTMUCH_FORMAT_TEXT,
662 .exclude = NOTMUCH_EXCLUDE_TRUE,
663 .sort = NOTMUCH_SORT_NEWEST_FIRST,
666 .limit = -1, /* unlimited */
670 static const notmuch_opt_desc_t common_options[] = {
671 { NOTMUCH_OPT_KEYWORD, &search_context.sort, "sort", 's',
672 (notmuch_keyword_t []){ { "oldest-first", NOTMUCH_SORT_OLDEST_FIRST },
673 { "newest-first", NOTMUCH_SORT_NEWEST_FIRST },
675 { NOTMUCH_OPT_KEYWORD, &search_context.format_sel, "format", 'f',
676 (notmuch_keyword_t []){ { "json", NOTMUCH_FORMAT_JSON },
677 { "sexp", NOTMUCH_FORMAT_SEXP },
678 { "text", NOTMUCH_FORMAT_TEXT },
679 { "text0", NOTMUCH_FORMAT_TEXT0 },
681 { NOTMUCH_OPT_INT, ¬much_format_version, "format-version", 0, 0 },
682 { NOTMUCH_OPT_KEYWORD, &search_context.exclude, "exclude", 'x',
683 (notmuch_keyword_t []){ { "true", NOTMUCH_EXCLUDE_TRUE },
684 { "false", NOTMUCH_EXCLUDE_FALSE },
685 { "flag", NOTMUCH_EXCLUDE_FLAG },
686 { "all", NOTMUCH_EXCLUDE_ALL },
692 notmuch_search_command (notmuch_config_t *config, int argc, char *argv[])
694 search_context_t *ctx = &search_context;
697 notmuch_opt_desc_t options[] = {
698 { NOTMUCH_OPT_KEYWORD, &ctx->output, "output", 'o',
699 (notmuch_keyword_t []){ { "summary", OUTPUT_SUMMARY },
700 { "threads", OUTPUT_THREADS },
701 { "messages", OUTPUT_MESSAGES },
702 { "files", OUTPUT_FILES },
703 { "tags", OUTPUT_TAGS },
705 { NOTMUCH_OPT_INT, &ctx->offset, "offset", 'O', 0 },
706 { NOTMUCH_OPT_INT, &ctx->limit, "limit", 'L', 0 },
707 { NOTMUCH_OPT_INT, &ctx->dupe, "duplicate", 'D', 0 },
708 { NOTMUCH_OPT_INHERIT, &common_options, NULL, 0, 0 },
712 ctx->output = OUTPUT_SUMMARY;
713 opt_index = parse_arguments (argc, argv, options, 1);
717 if (ctx->output != OUTPUT_FILES && ctx->output != OUTPUT_MESSAGES &&
719 fprintf (stderr, "Error: --duplicate=N is only supported with --output=files and --output=messages.\n");
723 if (_notmuch_search_prepare (ctx, config,
724 argc - opt_index, argv + opt_index))
727 switch (ctx->output) {
730 ret = do_search_threads (ctx);
732 case OUTPUT_MESSAGES:
734 ret = do_search_messages (ctx);
737 ret = do_search_tags (ctx);
740 INTERNAL_ERROR ("Unexpected output");
743 _notmuch_search_cleanup (ctx);
745 return ret ? EXIT_FAILURE : EXIT_SUCCESS;
749 notmuch_address_command (notmuch_config_t *config, int argc, char *argv[])
751 search_context_t *ctx = &search_context;
754 notmuch_opt_desc_t options[] = {
755 { NOTMUCH_OPT_KEYWORD_FLAGS, &ctx->output, "output", 'o',
756 (notmuch_keyword_t []){ { "sender", OUTPUT_SENDER },
757 { "recipients", OUTPUT_RECIPIENTS },
758 { "count", OUTPUT_COUNT },
760 { NOTMUCH_OPT_KEYWORD, &ctx->filter_by, "filter-by", 'b',
761 (notmuch_keyword_t []){ { "nameaddr", FILTER_BY_NAMEADDR },
762 { "name", FILTER_BY_NAME },
763 { "addr", FILTER_BY_ADDR },
764 { "addrfold", FILTER_BY_ADDRFOLD },
765 { "nameaddrfold", FILTER_BY_NAMEADDRFOLD },
767 { NOTMUCH_OPT_INHERIT, &common_options, NULL, 0, 0 },
771 ctx->filter_by = FILTER_BY_NAMEADDR,
772 opt_index = parse_arguments (argc, argv, options, 1);
777 search_context.output = OUTPUT_SENDER | OUTPUT_RECIPIENTS;
779 if (_notmuch_search_prepare (ctx, config,
780 argc - opt_index, argv + opt_index))
783 ctx->addresses = g_hash_table_new_full (g_str_hash, g_str_equal,
784 _my_talloc_free_for_g_hash, _my_talloc_free_for_g_hash);
786 ret = do_search_messages (ctx);
788 g_hash_table_unref (ctx->addresses);
791 _notmuch_search_cleanup (ctx);
793 return ret ? EXIT_FAILURE : EXIT_SUCCESS;