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 notmuch_database_t *notmuch;
48 format_sel_t format_sel;
50 notmuch_exclude_t exclude;
51 notmuch_query_t *query;
57 GHashTable *addresses;
66 /* Return two stable query strings that identify exactly the matched
67 * and unmatched messages currently in thread. If there are no
68 * matched or unmatched messages, the returned buffers will be
71 get_thread_query (notmuch_thread_t *thread,
72 char **matched_out, char **unmatched_out)
74 notmuch_messages_t *messages;
76 size_t escaped_len = 0;
78 *matched_out = *unmatched_out = NULL;
80 for (messages = notmuch_thread_get_messages (thread);
81 notmuch_messages_valid (messages);
82 notmuch_messages_move_to_next (messages))
84 notmuch_message_t *message = notmuch_messages_get (messages);
85 const char *mid = notmuch_message_get_message_id (message);
86 /* Determine which query buffer to extend */
87 char **buf = notmuch_message_get_flag (
88 message, NOTMUCH_MESSAGE_FLAG_MATCH) ? matched_out : unmatched_out;
89 /* Add this message's id: query. Since "id" is an exclusive
90 * prefix, it is implicitly 'or'd together, so we only need to
91 * join queries with a space. */
92 if (make_boolean_term (thread, "id", mid, &escaped, &escaped_len) < 0)
95 *buf = talloc_asprintf_append_buffer (*buf, " %s", escaped);
97 *buf = talloc_strdup (thread, escaped);
101 talloc_free (escaped);
106 do_search_threads (search_context_t *ctx)
108 notmuch_thread_t *thread;
109 notmuch_threads_t *threads;
110 notmuch_tags_t *tags;
111 sprinter_t *format = ctx->format;
115 if (ctx->offset < 0) {
116 ctx->offset += notmuch_query_count_threads (ctx->query);
121 threads = notmuch_query_search_threads (ctx->query);
125 format->begin_list (format);
128 notmuch_threads_valid (threads) && (ctx->limit < 0 || i < ctx->offset + ctx->limit);
129 notmuch_threads_move_to_next (threads), i++)
131 thread = notmuch_threads_get (threads);
133 if (i < ctx->offset) {
134 notmuch_thread_destroy (thread);
138 if (ctx->output == OUTPUT_THREADS) {
139 format->set_prefix (format, "thread");
140 format->string (format,
141 notmuch_thread_get_thread_id (thread));
142 format->separator (format);
143 } else { /* output == OUTPUT_SUMMARY */
144 void *ctx_quote = talloc_new (thread);
145 const char *authors = notmuch_thread_get_authors (thread);
146 const char *subject = notmuch_thread_get_subject (thread);
147 const char *thread_id = notmuch_thread_get_thread_id (thread);
148 int matched = notmuch_thread_get_matched_messages (thread);
149 int total = notmuch_thread_get_total_messages (thread);
150 const char *relative_date = NULL;
151 notmuch_bool_t first_tag = TRUE;
153 format->begin_map (format);
155 if (ctx->sort == NOTMUCH_SORT_OLDEST_FIRST)
156 date = notmuch_thread_get_oldest_date (thread);
158 date = notmuch_thread_get_newest_date (thread);
160 relative_date = notmuch_time_relative_date (ctx_quote, date);
162 if (format->is_text_printer) {
163 /* Special case for the text formatter */
164 printf ("thread:%s %12s [%d/%d] %s; %s (",
169 sanitize_string (ctx_quote, authors),
170 sanitize_string (ctx_quote, subject));
171 } else { /* Structured Output */
172 format->map_key (format, "thread");
173 format->string (format, thread_id);
174 format->map_key (format, "timestamp");
175 format->integer (format, date);
176 format->map_key (format, "date_relative");
177 format->string (format, relative_date);
178 format->map_key (format, "matched");
179 format->integer (format, matched);
180 format->map_key (format, "total");
181 format->integer (format, total);
182 format->map_key (format, "authors");
183 format->string (format, authors);
184 format->map_key (format, "subject");
185 format->string (format, subject);
186 if (notmuch_format_version >= 2) {
187 char *matched_query, *unmatched_query;
188 if (get_thread_query (thread, &matched_query,
189 &unmatched_query) < 0) {
190 fprintf (stderr, "Out of memory\n");
193 format->map_key (format, "query");
194 format->begin_list (format);
196 format->string (format, matched_query);
198 format->null (format);
200 format->string (format, unmatched_query);
202 format->null (format);
203 format->end (format);
207 talloc_free (ctx_quote);
209 format->map_key (format, "tags");
210 format->begin_list (format);
212 for (tags = notmuch_thread_get_tags (thread);
213 notmuch_tags_valid (tags);
214 notmuch_tags_move_to_next (tags))
216 const char *tag = notmuch_tags_get (tags);
218 if (format->is_text_printer) {
219 /* Special case for the text formatter */
225 } else { /* Structured Output */
226 format->string (format, tag);
230 if (format->is_text_printer)
233 format->end (format);
234 format->end (format);
235 format->separator (format);
238 notmuch_thread_destroy (thread);
241 format->end (format);
246 /* Returns TRUE iff name and addr is duplicate. If not, stores the
247 * name/addr pair in order to detect subsequent duplicates. */
248 static notmuch_bool_t
249 is_duplicate (const search_context_t *ctx, const char *name, const char *addr)
254 key = talloc_asprintf (ctx->format, "%s <%s>", name, addr);
258 mailbox = g_hash_table_lookup (ctx->addresses, key);
265 mailbox = talloc (ctx->format, mailbox_t);
266 mailbox->name = talloc_strdup (mailbox, name);
267 mailbox->addr = talloc_strdup (mailbox, addr);
269 g_hash_table_insert (ctx->addresses, key, mailbox);
275 print_mailbox (const search_context_t *ctx, const mailbox_t *mailbox)
277 const char *name = mailbox->name;
278 const char *addr = mailbox->addr;
279 int count = mailbox->count;
280 sprinter_t *format = ctx->format;
281 InternetAddress *ia = internet_address_mailbox_new (name, addr);
284 /* name_addr has the name part quoted if necessary. Compare
285 * 'John Doe <john@doe.com>' vs. '"Doe, John" <john@doe.com>' */
286 name_addr = internet_address_to_string (ia, FALSE);
288 if (format->is_text_printer) {
290 format->integer (format, count);
291 format->string (format, "\t");
293 format->string (format, name_addr);
294 format->separator (format);
296 format->begin_map (format);
297 format->map_key (format, "name");
298 format->string (format, name);
299 format->map_key (format, "address");
300 format->string (format, addr);
301 format->map_key (format, "name-addr");
302 format->string (format, name_addr);
304 format->map_key (format, "count");
305 format->integer (format, count);
307 format->end (format);
308 format->separator (format);
315 /* Print or prepare for printing addresses from InternetAddressList. */
317 process_address_list (const search_context_t *ctx,
318 InternetAddressList *list)
320 InternetAddress *address;
323 for (i = 0; i < internet_address_list_length (list); i++) {
324 address = internet_address_list_get_address (list, i);
325 if (INTERNET_ADDRESS_IS_GROUP (address)) {
326 InternetAddressGroup *group;
327 InternetAddressList *group_list;
329 group = INTERNET_ADDRESS_GROUP (address);
330 group_list = internet_address_group_get_members (group);
331 if (group_list == NULL)
334 process_address_list (ctx, group_list);
336 InternetAddressMailbox *mailbox = INTERNET_ADDRESS_MAILBOX (address);
338 .name = internet_address_get_name (address),
339 .addr = internet_address_mailbox_get_addr (mailbox),
343 if (is_duplicate (ctx, mbx.name, mbx.addr))
346 if (ctx->output & OUTPUT_COUNT)
349 print_mailbox (ctx, &mbx);
354 /* Print or prepare for printing addresses from a message header. */
356 process_address_header (const search_context_t *ctx, const char *value)
358 InternetAddressList *list;
363 list = internet_address_list_parse_string (value);
367 process_address_list (ctx, list);
369 g_object_unref (list);
372 /* Destructor for talloc-allocated GHashTable keys and values. */
374 _talloc_free_for_g_hash (void *ptr)
380 print_hash_value (unused (gpointer key), gpointer value, gpointer user_data)
382 const mailbox_t *mailbox = value;
383 search_context_t *ctx = user_data;
385 print_mailbox (ctx, mailbox);
389 _count_filenames (notmuch_message_t *message)
391 notmuch_filenames_t *filenames;
394 filenames = notmuch_message_get_filenames (message);
396 while (notmuch_filenames_valid (filenames)) {
397 notmuch_filenames_move_to_next (filenames);
401 notmuch_filenames_destroy (filenames);
407 do_search_messages (search_context_t *ctx)
409 notmuch_message_t *message;
410 notmuch_messages_t *messages;
411 notmuch_filenames_t *filenames;
412 sprinter_t *format = ctx->format;
415 if (ctx->offset < 0) {
416 ctx->offset += notmuch_query_count_messages (ctx->query);
421 messages = notmuch_query_search_messages (ctx->query);
422 if (messages == NULL)
425 format->begin_list (format);
428 notmuch_messages_valid (messages) && (ctx->limit < 0 || i < ctx->offset + ctx->limit);
429 notmuch_messages_move_to_next (messages), i++)
434 message = notmuch_messages_get (messages);
436 if (ctx->output == OUTPUT_FILES) {
438 filenames = notmuch_message_get_filenames (message);
441 notmuch_filenames_valid (filenames);
442 notmuch_filenames_move_to_next (filenames), j++)
444 if (ctx->dupe < 0 || ctx->dupe == j) {
445 format->string (format, notmuch_filenames_get (filenames));
446 format->separator (format);
450 notmuch_filenames_destroy( filenames );
452 } else if (ctx->output == OUTPUT_MESSAGES) {
453 /* special case 1 for speed */
454 if (ctx->dupe <= 1 || ctx->dupe <= _count_filenames (message)) {
455 format->set_prefix (format, "id");
456 format->string (format,
457 notmuch_message_get_message_id (message));
458 format->separator (format);
461 if (ctx->output & OUTPUT_SENDER) {
464 addrs = notmuch_message_get_header (message, "from");
465 process_address_header (ctx, addrs);
468 if (ctx->output & OUTPUT_RECIPIENTS) {
469 const char *hdrs[] = { "to", "cc", "bcc" };
473 for (j = 0; j < ARRAY_SIZE (hdrs); j++) {
474 addrs = notmuch_message_get_header (message, hdrs[j]);
475 process_address_header (ctx, addrs);
480 notmuch_message_destroy (message);
483 if (ctx->addresses && ctx->output & OUTPUT_COUNT)
484 g_hash_table_foreach (ctx->addresses, print_hash_value, ctx);
486 notmuch_messages_destroy (messages);
488 format->end (format);
494 do_search_tags (const search_context_t *ctx)
496 notmuch_messages_t *messages = NULL;
497 notmuch_tags_t *tags;
499 sprinter_t *format = ctx->format;
500 notmuch_query_t *query = ctx->query;
501 notmuch_database_t *notmuch = ctx->notmuch;
503 /* should the following only special case if no excluded terms
506 /* Special-case query of "*" for better performance. */
507 if (strcmp (notmuch_query_get_query_string (query), "*") == 0) {
508 tags = notmuch_database_get_all_tags (notmuch);
510 messages = notmuch_query_search_messages (query);
511 if (messages == NULL)
514 tags = notmuch_messages_collect_tags (messages);
519 format->begin_list (format);
522 notmuch_tags_valid (tags);
523 notmuch_tags_move_to_next (tags))
525 tag = notmuch_tags_get (tags);
527 format->string (format, tag);
528 format->separator (format);
532 notmuch_tags_destroy (tags);
535 notmuch_messages_destroy (messages);
537 format->end (format);
543 _notmuch_search_prepare (search_context_t *ctx, notmuch_config_t *config, int argc, char *argv[])
547 char *status_string = NULL;
549 switch (ctx->format_sel) {
550 case NOTMUCH_FORMAT_TEXT:
551 ctx->format = sprinter_text_create (config, stdout);
553 case NOTMUCH_FORMAT_TEXT0:
554 if (ctx->output == OUTPUT_SUMMARY) {
555 fprintf (stderr, "Error: --format=text0 is not compatible with --output=summary.\n");
558 ctx->format = sprinter_text0_create (config, stdout);
560 case NOTMUCH_FORMAT_JSON:
561 ctx->format = sprinter_json_create (config, stdout);
563 case NOTMUCH_FORMAT_SEXP:
564 ctx->format = sprinter_sexp_create (config, stdout);
567 /* this should never happen */
568 INTERNAL_ERROR("no output format selected");
571 notmuch_exit_if_unsupported_format ();
573 if (notmuch_database_open_verbose (
574 notmuch_config_get_database_path (config),
575 NOTMUCH_DATABASE_MODE_READ_ONLY, &ctx->notmuch, &status_string)) {
578 fputs (status_string, stderr);
579 free (status_string);
585 notmuch_exit_if_unmatched_db_uuid (ctx->notmuch);
587 query_str = query_string_from_args (ctx->notmuch, argc, argv);
588 if (query_str == NULL) {
589 fprintf (stderr, "Out of memory.\n");
592 if (*query_str == '\0') {
593 fprintf (stderr, "Error: notmuch search requires at least one search term.\n");
597 ctx->query = notmuch_query_create (ctx->notmuch, query_str);
598 if (ctx->query == NULL) {
599 fprintf (stderr, "Out of memory\n");
603 notmuch_query_set_sort (ctx->query, ctx->sort);
605 if (ctx->exclude == NOTMUCH_EXCLUDE_FLAG && ctx->output != OUTPUT_SUMMARY) {
606 /* If we are not doing summary output there is nowhere to
607 * print the excluded flag so fall back on including the
608 * excluded messages. */
609 fprintf (stderr, "Warning: this output format cannot flag excluded messages.\n");
610 ctx->exclude = NOTMUCH_EXCLUDE_FALSE;
613 if (ctx->exclude != NOTMUCH_EXCLUDE_FALSE) {
614 const char **search_exclude_tags;
615 size_t search_exclude_tags_length;
617 search_exclude_tags = notmuch_config_get_search_exclude_tags
618 (config, &search_exclude_tags_length);
619 for (i = 0; i < search_exclude_tags_length; i++)
620 notmuch_query_add_tag_exclude (ctx->query, search_exclude_tags[i]);
621 notmuch_query_set_omit_excluded (ctx->query, ctx->exclude);
628 _notmuch_search_cleanup (search_context_t *ctx)
630 notmuch_query_destroy (ctx->query);
631 notmuch_database_destroy (ctx->notmuch);
633 talloc_free (ctx->format);
636 static search_context_t search_context = {
637 .format_sel = NOTMUCH_FORMAT_TEXT,
638 .exclude = NOTMUCH_EXCLUDE_TRUE,
639 .sort = NOTMUCH_SORT_NEWEST_FIRST,
642 .limit = -1, /* unlimited */
646 static const notmuch_opt_desc_t common_options[] = {
647 { NOTMUCH_OPT_KEYWORD, &search_context.sort, "sort", 's',
648 (notmuch_keyword_t []){ { "oldest-first", NOTMUCH_SORT_OLDEST_FIRST },
649 { "newest-first", NOTMUCH_SORT_NEWEST_FIRST },
651 { NOTMUCH_OPT_KEYWORD, &search_context.format_sel, "format", 'f',
652 (notmuch_keyword_t []){ { "json", NOTMUCH_FORMAT_JSON },
653 { "sexp", NOTMUCH_FORMAT_SEXP },
654 { "text", NOTMUCH_FORMAT_TEXT },
655 { "text0", NOTMUCH_FORMAT_TEXT0 },
657 { NOTMUCH_OPT_INT, ¬much_format_version, "format-version", 0, 0 },
662 notmuch_search_command (notmuch_config_t *config, int argc, char *argv[])
664 search_context_t *ctx = &search_context;
667 notmuch_opt_desc_t options[] = {
668 { NOTMUCH_OPT_KEYWORD, &ctx->output, "output", 'o',
669 (notmuch_keyword_t []){ { "summary", OUTPUT_SUMMARY },
670 { "threads", OUTPUT_THREADS },
671 { "messages", OUTPUT_MESSAGES },
672 { "files", OUTPUT_FILES },
673 { "tags", OUTPUT_TAGS },
675 { NOTMUCH_OPT_KEYWORD, &ctx->exclude, "exclude", 'x',
676 (notmuch_keyword_t []){ { "true", NOTMUCH_EXCLUDE_TRUE },
677 { "false", NOTMUCH_EXCLUDE_FALSE },
678 { "flag", NOTMUCH_EXCLUDE_FLAG },
679 { "all", NOTMUCH_EXCLUDE_ALL },
681 { NOTMUCH_OPT_INT, &ctx->offset, "offset", 'O', 0 },
682 { NOTMUCH_OPT_INT, &ctx->limit, "limit", 'L', 0 },
683 { NOTMUCH_OPT_INT, &ctx->dupe, "duplicate", 'D', 0 },
684 { NOTMUCH_OPT_INHERIT, (void *) &common_options, NULL, 0, 0 },
685 { NOTMUCH_OPT_INHERIT, (void *) ¬much_shared_options, NULL, 0, 0 },
689 ctx->output = OUTPUT_SUMMARY;
690 opt_index = parse_arguments (argc, argv, options, 1);
694 notmuch_process_shared_options (argv[0]);
696 if (ctx->output != OUTPUT_FILES && ctx->output != OUTPUT_MESSAGES &&
698 fprintf (stderr, "Error: --duplicate=N is only supported with --output=files and --output=messages.\n");
702 if (_notmuch_search_prepare (ctx, config,
703 argc - opt_index, argv + opt_index))
706 switch (ctx->output) {
709 ret = do_search_threads (ctx);
711 case OUTPUT_MESSAGES:
713 ret = do_search_messages (ctx);
716 ret = do_search_tags (ctx);
719 INTERNAL_ERROR ("Unexpected output");
722 _notmuch_search_cleanup (ctx);
724 return ret ? EXIT_FAILURE : EXIT_SUCCESS;
728 notmuch_address_command (notmuch_config_t *config, int argc, char *argv[])
730 search_context_t *ctx = &search_context;
733 notmuch_opt_desc_t options[] = {
734 { NOTMUCH_OPT_KEYWORD_FLAGS, &ctx->output, "output", 'o',
735 (notmuch_keyword_t []){ { "sender", OUTPUT_SENDER },
736 { "recipients", OUTPUT_RECIPIENTS },
737 { "count", OUTPUT_COUNT },
739 { NOTMUCH_OPT_KEYWORD, &ctx->exclude, "exclude", 'x',
740 (notmuch_keyword_t []){ { "true", NOTMUCH_EXCLUDE_TRUE },
741 { "false", NOTMUCH_EXCLUDE_FALSE },
743 { NOTMUCH_OPT_INHERIT, (void *) &common_options, NULL, 0, 0 },
744 { NOTMUCH_OPT_INHERIT, (void *) ¬much_shared_options, NULL, 0, 0 },
748 opt_index = parse_arguments (argc, argv, options, 1);
752 notmuch_process_shared_options (argv[0]);
754 if (! (ctx->output & (OUTPUT_SENDER | OUTPUT_RECIPIENTS)))
755 ctx->output |= OUTPUT_SENDER;
757 if (_notmuch_search_prepare (ctx, config,
758 argc - opt_index, argv + opt_index))
761 ctx->addresses = g_hash_table_new_full (g_str_hash, g_str_equal,
762 _talloc_free_for_g_hash, _talloc_free_for_g_hash);
764 ret = do_search_messages (ctx);
766 g_hash_table_unref (ctx->addresses);
769 _notmuch_search_cleanup (ctx);
771 return ret ? EXIT_FAILURE : EXIT_SUCCESS;