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)
45 notmuch_database_t *notmuch;
46 format_sel_t format_sel;
48 notmuch_exclude_t exclude;
49 notmuch_query_t *query;
62 /* Return two stable query strings that identify exactly the matched
63 * and unmatched messages currently in thread. If there are no
64 * matched or unmatched messages, the returned buffers will be
67 get_thread_query (notmuch_thread_t *thread,
68 char **matched_out, char **unmatched_out)
70 notmuch_messages_t *messages;
72 size_t escaped_len = 0;
74 *matched_out = *unmatched_out = NULL;
76 for (messages = notmuch_thread_get_messages (thread);
77 notmuch_messages_valid (messages);
78 notmuch_messages_move_to_next (messages))
80 notmuch_message_t *message = notmuch_messages_get (messages);
81 const char *mid = notmuch_message_get_message_id (message);
82 /* Determine which query buffer to extend */
83 char **buf = notmuch_message_get_flag (
84 message, NOTMUCH_MESSAGE_FLAG_MATCH) ? matched_out : unmatched_out;
85 /* Add this message's id: query. Since "id" is an exclusive
86 * prefix, it is implicitly 'or'd together, so we only need to
87 * join queries with a space. */
88 if (make_boolean_term (thread, "id", mid, &escaped, &escaped_len) < 0)
91 *buf = talloc_asprintf_append_buffer (*buf, " %s", escaped);
93 *buf = talloc_strdup (thread, escaped);
97 talloc_free (escaped);
102 do_search_threads (search_context_t *ctx)
104 notmuch_thread_t *thread;
105 notmuch_threads_t *threads;
106 notmuch_tags_t *tags;
107 sprinter_t *format = ctx->format;
111 if (ctx->offset < 0) {
112 ctx->offset += notmuch_query_count_threads (ctx->query);
117 threads = notmuch_query_search_threads (ctx->query);
121 format->begin_list (format);
124 notmuch_threads_valid (threads) && (ctx->limit < 0 || i < ctx->offset + ctx->limit);
125 notmuch_threads_move_to_next (threads), i++)
127 thread = notmuch_threads_get (threads);
129 if (i < ctx->offset) {
130 notmuch_thread_destroy (thread);
134 if (ctx->output == OUTPUT_THREADS) {
135 format->set_prefix (format, "thread");
136 format->string (format,
137 notmuch_thread_get_thread_id (thread));
138 format->separator (format);
139 } else { /* output == OUTPUT_SUMMARY */
140 void *ctx_quote = talloc_new (thread);
141 const char *authors = notmuch_thread_get_authors (thread);
142 const char *subject = notmuch_thread_get_subject (thread);
143 const char *thread_id = notmuch_thread_get_thread_id (thread);
144 int matched = notmuch_thread_get_matched_messages (thread);
145 int total = notmuch_thread_get_total_messages (thread);
146 const char *relative_date = NULL;
147 notmuch_bool_t first_tag = TRUE;
149 format->begin_map (format);
151 if (ctx->sort == NOTMUCH_SORT_OLDEST_FIRST)
152 date = notmuch_thread_get_oldest_date (thread);
154 date = notmuch_thread_get_newest_date (thread);
156 relative_date = notmuch_time_relative_date (ctx_quote, date);
158 if (format->is_text_printer) {
159 /* Special case for the text formatter */
160 printf ("thread:%s %12s [%d/%d] %s; %s (",
165 sanitize_string (ctx_quote, authors),
166 sanitize_string (ctx_quote, subject));
167 } else { /* Structured Output */
168 format->map_key (format, "thread");
169 format->string (format, thread_id);
170 format->map_key (format, "timestamp");
171 format->integer (format, date);
172 format->map_key (format, "date_relative");
173 format->string (format, relative_date);
174 format->map_key (format, "matched");
175 format->integer (format, matched);
176 format->map_key (format, "total");
177 format->integer (format, total);
178 format->map_key (format, "authors");
179 format->string (format, authors);
180 format->map_key (format, "subject");
181 format->string (format, subject);
182 if (notmuch_format_version >= 2) {
183 char *matched_query, *unmatched_query;
184 if (get_thread_query (thread, &matched_query,
185 &unmatched_query) < 0) {
186 fprintf (stderr, "Out of memory\n");
189 format->map_key (format, "query");
190 format->begin_list (format);
192 format->string (format, matched_query);
194 format->null (format);
196 format->string (format, unmatched_query);
198 format->null (format);
199 format->end (format);
203 talloc_free (ctx_quote);
205 format->map_key (format, "tags");
206 format->begin_list (format);
208 for (tags = notmuch_thread_get_tags (thread);
209 notmuch_tags_valid (tags);
210 notmuch_tags_move_to_next (tags))
212 const char *tag = notmuch_tags_get (tags);
214 if (format->is_text_printer) {
215 /* Special case for the text formatter */
221 } else { /* Structured Output */
222 format->string (format, tag);
226 if (format->is_text_printer)
229 format->end (format);
230 format->end (format);
231 format->separator (format);
234 notmuch_thread_destroy (thread);
237 format->end (format);
243 print_mailbox (const search_context_t *ctx, const mailbox_t *mailbox)
245 const char *name = mailbox->name;
246 const char *addr = mailbox->addr;
247 sprinter_t *format = ctx->format;
248 InternetAddress *ia = internet_address_mailbox_new (name, addr);
251 /* name_addr has the name part quoted if necessary. Compare
252 * 'John Doe <john@doe.com>' vs. '"Doe, John" <john@doe.com>' */
253 name_addr = internet_address_to_string (ia, FALSE);
255 if (format->is_text_printer) {
256 format->string (format, name_addr);
257 format->separator (format);
259 format->begin_map (format);
260 format->map_key (format, "name");
261 format->string (format, name);
262 format->map_key (format, "address");
263 format->string (format, addr);
264 format->map_key (format, "name-addr");
265 format->string (format, name_addr);
266 format->end (format);
267 format->separator (format);
274 /* Print addresses from InternetAddressList. */
276 process_address_list (const search_context_t *ctx, InternetAddressList *list)
278 InternetAddress *address;
281 for (i = 0; i < internet_address_list_length (list); i++) {
282 address = internet_address_list_get_address (list, i);
283 if (INTERNET_ADDRESS_IS_GROUP (address)) {
284 InternetAddressGroup *group;
285 InternetAddressList *group_list;
287 group = INTERNET_ADDRESS_GROUP (address);
288 group_list = internet_address_group_get_members (group);
289 if (group_list == NULL)
292 process_address_list (ctx, group_list);
294 InternetAddressMailbox *mailbox = INTERNET_ADDRESS_MAILBOX (address);
296 .name = internet_address_get_name (address),
297 .addr = internet_address_mailbox_get_addr (mailbox),
300 print_mailbox (ctx, &mbx);
305 /* Print addresses from a message header. */
307 process_address_header (const search_context_t *ctx, const char *value)
309 InternetAddressList *list;
314 list = internet_address_list_parse_string (value);
318 process_address_list (ctx, list);
320 g_object_unref (list);
324 _count_filenames (notmuch_message_t *message)
326 notmuch_filenames_t *filenames;
329 filenames = notmuch_message_get_filenames (message);
331 while (notmuch_filenames_valid (filenames)) {
332 notmuch_filenames_move_to_next (filenames);
336 notmuch_filenames_destroy (filenames);
342 do_search_messages (search_context_t *ctx)
344 notmuch_message_t *message;
345 notmuch_messages_t *messages;
346 notmuch_filenames_t *filenames;
347 sprinter_t *format = ctx->format;
350 if (ctx->offset < 0) {
351 ctx->offset += notmuch_query_count_messages (ctx->query);
356 messages = notmuch_query_search_messages (ctx->query);
357 if (messages == NULL)
360 format->begin_list (format);
363 notmuch_messages_valid (messages) && (ctx->limit < 0 || i < ctx->offset + ctx->limit);
364 notmuch_messages_move_to_next (messages), i++)
369 message = notmuch_messages_get (messages);
371 if (ctx->output == OUTPUT_FILES) {
373 filenames = notmuch_message_get_filenames (message);
376 notmuch_filenames_valid (filenames);
377 notmuch_filenames_move_to_next (filenames), j++)
379 if (ctx->dupe < 0 || ctx->dupe == j) {
380 format->string (format, notmuch_filenames_get (filenames));
381 format->separator (format);
385 notmuch_filenames_destroy( filenames );
387 } else if (ctx->output == OUTPUT_MESSAGES) {
388 /* special case 1 for speed */
389 if (ctx->dupe <= 1 || ctx->dupe <= _count_filenames (message)) {
390 format->set_prefix (format, "id");
391 format->string (format,
392 notmuch_message_get_message_id (message));
393 format->separator (format);
396 if (ctx->output & OUTPUT_SENDER) {
399 addrs = notmuch_message_get_header (message, "from");
400 process_address_header (ctx, addrs);
403 if (ctx->output & OUTPUT_RECIPIENTS) {
404 const char *hdrs[] = { "to", "cc", "bcc" };
408 for (j = 0; j < ARRAY_SIZE (hdrs); j++) {
409 addrs = notmuch_message_get_header (message, hdrs[j]);
410 process_address_header (ctx, addrs);
415 notmuch_message_destroy (message);
418 notmuch_messages_destroy (messages);
420 format->end (format);
426 do_search_tags (const search_context_t *ctx)
428 notmuch_messages_t *messages = NULL;
429 notmuch_tags_t *tags;
431 sprinter_t *format = ctx->format;
432 notmuch_query_t *query = ctx->query;
433 notmuch_database_t *notmuch = ctx->notmuch;
435 /* should the following only special case if no excluded terms
438 /* Special-case query of "*" for better performance. */
439 if (strcmp (notmuch_query_get_query_string (query), "*") == 0) {
440 tags = notmuch_database_get_all_tags (notmuch);
442 messages = notmuch_query_search_messages (query);
443 if (messages == NULL)
446 tags = notmuch_messages_collect_tags (messages);
451 format->begin_list (format);
454 notmuch_tags_valid (tags);
455 notmuch_tags_move_to_next (tags))
457 tag = notmuch_tags_get (tags);
459 format->string (format, tag);
460 format->separator (format);
464 notmuch_tags_destroy (tags);
467 notmuch_messages_destroy (messages);
469 format->end (format);
475 _notmuch_search_prepare (search_context_t *ctx, notmuch_config_t *config, int argc, char *argv[])
480 switch (ctx->format_sel) {
481 case NOTMUCH_FORMAT_TEXT:
482 ctx->format = sprinter_text_create (config, stdout);
484 case NOTMUCH_FORMAT_TEXT0:
485 if (ctx->output == OUTPUT_SUMMARY) {
486 fprintf (stderr, "Error: --format=text0 is not compatible with --output=summary.\n");
489 ctx->format = sprinter_text0_create (config, stdout);
491 case NOTMUCH_FORMAT_JSON:
492 ctx->format = sprinter_json_create (config, stdout);
494 case NOTMUCH_FORMAT_SEXP:
495 ctx->format = sprinter_sexp_create (config, stdout);
498 /* this should never happen */
499 INTERNAL_ERROR("no output format selected");
502 notmuch_exit_if_unsupported_format ();
504 if (notmuch_database_open (notmuch_config_get_database_path (config),
505 NOTMUCH_DATABASE_MODE_READ_ONLY, &ctx->notmuch))
508 query_str = query_string_from_args (ctx->notmuch, argc, argv);
509 if (query_str == NULL) {
510 fprintf (stderr, "Out of memory.\n");
513 if (*query_str == '\0') {
514 fprintf (stderr, "Error: notmuch search requires at least one search term.\n");
518 ctx->query = notmuch_query_create (ctx->notmuch, query_str);
519 if (ctx->query == NULL) {
520 fprintf (stderr, "Out of memory\n");
524 notmuch_query_set_sort (ctx->query, ctx->sort);
526 if (ctx->exclude == NOTMUCH_EXCLUDE_FLAG && ctx->output != OUTPUT_SUMMARY) {
527 /* If we are not doing summary output there is nowhere to
528 * print the excluded flag so fall back on including the
529 * excluded messages. */
530 fprintf (stderr, "Warning: this output format cannot flag excluded messages.\n");
531 ctx->exclude = NOTMUCH_EXCLUDE_FALSE;
534 if (ctx->exclude != NOTMUCH_EXCLUDE_FALSE) {
535 const char **search_exclude_tags;
536 size_t search_exclude_tags_length;
538 search_exclude_tags = notmuch_config_get_search_exclude_tags
539 (config, &search_exclude_tags_length);
540 for (i = 0; i < search_exclude_tags_length; i++)
541 notmuch_query_add_tag_exclude (ctx->query, search_exclude_tags[i]);
542 notmuch_query_set_omit_excluded (ctx->query, ctx->exclude);
549 _notmuch_search_cleanup (search_context_t *ctx)
551 notmuch_query_destroy (ctx->query);
552 notmuch_database_destroy (ctx->notmuch);
554 talloc_free (ctx->format);
558 notmuch_search_command (notmuch_config_t *config, int argc, char *argv[])
560 search_context_t search_context = {
561 .format_sel = NOTMUCH_FORMAT_TEXT,
562 .exclude = NOTMUCH_EXCLUDE_TRUE,
563 .sort = NOTMUCH_SORT_NEWEST_FIRST,
566 .limit = -1, /* unlimited */
569 search_context_t *ctx = &search_context;
572 notmuch_opt_desc_t options[] = {
573 { NOTMUCH_OPT_KEYWORD, &ctx->sort, "sort", 's',
574 (notmuch_keyword_t []){ { "oldest-first", NOTMUCH_SORT_OLDEST_FIRST },
575 { "newest-first", NOTMUCH_SORT_NEWEST_FIRST },
577 { NOTMUCH_OPT_KEYWORD, &ctx->format_sel, "format", 'f',
578 (notmuch_keyword_t []){ { "json", NOTMUCH_FORMAT_JSON },
579 { "sexp", NOTMUCH_FORMAT_SEXP },
580 { "text", NOTMUCH_FORMAT_TEXT },
581 { "text0", NOTMUCH_FORMAT_TEXT0 },
583 { NOTMUCH_OPT_INT, ¬much_format_version, "format-version", 0, 0 },
584 { NOTMUCH_OPT_KEYWORD_FLAGS, &ctx->output, "output", 'o',
585 (notmuch_keyword_t []){ { "summary", OUTPUT_SUMMARY },
586 { "threads", OUTPUT_THREADS },
587 { "messages", OUTPUT_MESSAGES },
588 { "sender", OUTPUT_SENDER },
589 { "recipients", OUTPUT_RECIPIENTS },
590 { "files", OUTPUT_FILES },
591 { "tags", OUTPUT_TAGS },
593 { NOTMUCH_OPT_KEYWORD, &ctx->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, &ctx->offset, "offset", 'O', 0 },
600 { NOTMUCH_OPT_INT, &ctx->limit, "limit", 'L', 0 },
601 { NOTMUCH_OPT_INT, &ctx->dupe, "duplicate", 'D', 0 },
605 opt_index = parse_arguments (argc, argv, options, 1);
610 ctx->output = OUTPUT_SUMMARY;
612 if (ctx->output != OUTPUT_FILES && ctx->output != OUTPUT_MESSAGES &&
614 fprintf (stderr, "Error: --duplicate=N is only supported with --output=files and --output=messages.\n");
618 if (_notmuch_search_prepare (ctx, config,
619 argc - opt_index, argv + opt_index))
622 if (ctx->output == OUTPUT_SUMMARY ||
623 ctx->output == OUTPUT_THREADS)
624 ret = do_search_threads (ctx);
625 else if (ctx->output == OUTPUT_MESSAGES ||
626 ctx->output == OUTPUT_FILES ||
627 (ctx->output & OUTPUT_ADDRESS_FLAGS && !(ctx->output & ~OUTPUT_ADDRESS_FLAGS)))
628 ret = do_search_messages (ctx);
629 else if (ctx->output == OUTPUT_TAGS)
630 ret = do_search_tags (ctx);
632 fprintf (stderr, "Error: the combination of outputs is not supported.\n");
636 _notmuch_search_cleanup (ctx);
638 return ret ? EXIT_FAILURE : EXIT_SUCCESS;