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"
35 notmuch_query_t *query;
43 /* Return two stable query strings that identify exactly the matched
44 * and unmatched messages currently in thread. If there are no
45 * matched or unmatched messages, the returned buffers will be
48 get_thread_query (notmuch_thread_t *thread,
49 char **matched_out, char **unmatched_out)
51 notmuch_messages_t *messages;
53 size_t escaped_len = 0;
55 *matched_out = *unmatched_out = NULL;
57 for (messages = notmuch_thread_get_messages (thread);
58 notmuch_messages_valid (messages);
59 notmuch_messages_move_to_next (messages))
61 notmuch_message_t *message = notmuch_messages_get (messages);
62 const char *mid = notmuch_message_get_message_id (message);
63 /* Determine which query buffer to extend */
64 char **buf = notmuch_message_get_flag (
65 message, NOTMUCH_MESSAGE_FLAG_MATCH) ? matched_out : unmatched_out;
66 /* Add this message's id: query. Since "id" is an exclusive
67 * prefix, it is implicitly 'or'd together, so we only need to
68 * join queries with a space. */
69 if (make_boolean_term (thread, "id", mid, &escaped, &escaped_len) < 0)
72 *buf = talloc_asprintf_append_buffer (*buf, " %s", escaped);
74 *buf = talloc_strdup (thread, escaped);
78 talloc_free (escaped);
83 do_search_threads (search_options_t *opt)
85 notmuch_thread_t *thread;
86 notmuch_threads_t *threads;
88 sprinter_t *format = opt->format;
92 if (opt->offset < 0) {
93 opt->offset += notmuch_query_count_threads (opt->query);
98 threads = notmuch_query_search_threads (opt->query);
102 format->begin_list (format);
105 notmuch_threads_valid (threads) && (opt->limit < 0 || i < opt->offset + opt->limit);
106 notmuch_threads_move_to_next (threads), i++)
108 thread = notmuch_threads_get (threads);
110 if (i < opt->offset) {
111 notmuch_thread_destroy (thread);
115 if (opt->output == OUTPUT_THREADS) {
116 format->set_prefix (format, "thread");
117 format->string (format,
118 notmuch_thread_get_thread_id (thread));
119 format->separator (format);
120 } else { /* output == OUTPUT_SUMMARY */
121 void *ctx_quote = talloc_new (thread);
122 const char *authors = notmuch_thread_get_authors (thread);
123 const char *subject = notmuch_thread_get_subject (thread);
124 const char *thread_id = notmuch_thread_get_thread_id (thread);
125 int matched = notmuch_thread_get_matched_messages (thread);
126 int total = notmuch_thread_get_total_messages (thread);
127 const char *relative_date = NULL;
128 notmuch_bool_t first_tag = TRUE;
130 format->begin_map (format);
132 if (opt->sort == NOTMUCH_SORT_OLDEST_FIRST)
133 date = notmuch_thread_get_oldest_date (thread);
135 date = notmuch_thread_get_newest_date (thread);
137 relative_date = notmuch_time_relative_date (ctx_quote, date);
139 if (format->is_text_printer) {
140 /* Special case for the text formatter */
141 printf ("thread:%s %12s [%d/%d] %s; %s (",
146 sanitize_string (ctx_quote, authors),
147 sanitize_string (ctx_quote, subject));
148 } else { /* Structured Output */
149 format->map_key (format, "thread");
150 format->string (format, thread_id);
151 format->map_key (format, "timestamp");
152 format->integer (format, date);
153 format->map_key (format, "date_relative");
154 format->string (format, relative_date);
155 format->map_key (format, "matched");
156 format->integer (format, matched);
157 format->map_key (format, "total");
158 format->integer (format, total);
159 format->map_key (format, "authors");
160 format->string (format, authors);
161 format->map_key (format, "subject");
162 format->string (format, subject);
163 if (notmuch_format_version >= 2) {
164 char *matched_query, *unmatched_query;
165 if (get_thread_query (thread, &matched_query,
166 &unmatched_query) < 0) {
167 fprintf (stderr, "Out of memory\n");
170 format->map_key (format, "query");
171 format->begin_list (format);
173 format->string (format, matched_query);
175 format->null (format);
177 format->string (format, unmatched_query);
179 format->null (format);
180 format->end (format);
184 talloc_free (ctx_quote);
186 format->map_key (format, "tags");
187 format->begin_list (format);
189 for (tags = notmuch_thread_get_tags (thread);
190 notmuch_tags_valid (tags);
191 notmuch_tags_move_to_next (tags))
193 const char *tag = notmuch_tags_get (tags);
195 if (format->is_text_printer) {
196 /* Special case for the text formatter */
202 } else { /* Structured Output */
203 format->string (format, tag);
207 if (format->is_text_printer)
210 format->end (format);
211 format->end (format);
212 format->separator (format);
215 notmuch_thread_destroy (thread);
218 format->end (format);
224 do_search_messages (search_options_t *opt)
226 notmuch_message_t *message;
227 notmuch_messages_t *messages;
228 notmuch_filenames_t *filenames;
229 sprinter_t *format = opt->format;
232 if (opt->offset < 0) {
233 opt->offset += notmuch_query_count_messages (opt->query);
238 messages = notmuch_query_search_messages (opt->query);
239 if (messages == NULL)
242 format->begin_list (format);
245 notmuch_messages_valid (messages) && (opt->limit < 0 || i < opt->offset + opt->limit);
246 notmuch_messages_move_to_next (messages), i++)
251 message = notmuch_messages_get (messages);
253 if (opt->output == OUTPUT_FILES) {
255 filenames = notmuch_message_get_filenames (message);
258 notmuch_filenames_valid (filenames);
259 notmuch_filenames_move_to_next (filenames), j++)
261 if (opt->dupe < 0 || opt->dupe == j) {
262 format->string (format, notmuch_filenames_get (filenames));
263 format->separator (format);
267 notmuch_filenames_destroy( filenames );
269 } else { /* output == OUTPUT_MESSAGES */
270 format->set_prefix (format, "id");
271 format->string (format,
272 notmuch_message_get_message_id (message));
273 format->separator (format);
276 notmuch_message_destroy (message);
279 notmuch_messages_destroy (messages);
281 format->end (format);
287 do_search_tags (notmuch_database_t *notmuch,
288 const search_options_t *opt)
290 notmuch_messages_t *messages = NULL;
291 notmuch_tags_t *tags;
293 sprinter_t *format = opt->format;
294 notmuch_query_t *query = opt->query;
296 /* should the following only special case if no excluded terms
299 /* Special-case query of "*" for better performance. */
300 if (strcmp (notmuch_query_get_query_string (query), "*") == 0) {
301 tags = notmuch_database_get_all_tags (notmuch);
303 messages = notmuch_query_search_messages (query);
304 if (messages == NULL)
307 tags = notmuch_messages_collect_tags (messages);
312 format->begin_list (format);
315 notmuch_tags_valid (tags);
316 notmuch_tags_move_to_next (tags))
318 tag = notmuch_tags_get (tags);
320 format->string (format, tag);
321 format->separator (format);
325 notmuch_tags_destroy (tags);
328 notmuch_messages_destroy (messages);
330 format->end (format);
336 notmuch_search_command (notmuch_config_t *config, int argc, char *argv[])
338 notmuch_database_t *notmuch;
339 search_options_t opt = {
340 .sort = NOTMUCH_SORT_NEWEST_FIRST,
341 .output = OUTPUT_SUMMARY,
343 .limit = -1, /* unlimited */
348 notmuch_exclude_t exclude = NOTMUCH_EXCLUDE_TRUE;
354 NOTMUCH_FORMAT_TEXT0,
356 } format_sel = NOTMUCH_FORMAT_TEXT;
358 notmuch_opt_desc_t options[] = {
359 { NOTMUCH_OPT_KEYWORD, &opt.sort, "sort", 's',
360 (notmuch_keyword_t []){ { "oldest-first", NOTMUCH_SORT_OLDEST_FIRST },
361 { "newest-first", NOTMUCH_SORT_NEWEST_FIRST },
363 { NOTMUCH_OPT_KEYWORD, &format_sel, "format", 'f',
364 (notmuch_keyword_t []){ { "json", NOTMUCH_FORMAT_JSON },
365 { "sexp", NOTMUCH_FORMAT_SEXP },
366 { "text", NOTMUCH_FORMAT_TEXT },
367 { "text0", NOTMUCH_FORMAT_TEXT0 },
369 { NOTMUCH_OPT_INT, ¬much_format_version, "format-version", 0, 0 },
370 { NOTMUCH_OPT_KEYWORD, &opt.output, "output", 'o',
371 (notmuch_keyword_t []){ { "summary", OUTPUT_SUMMARY },
372 { "threads", OUTPUT_THREADS },
373 { "messages", OUTPUT_MESSAGES },
374 { "files", OUTPUT_FILES },
375 { "tags", OUTPUT_TAGS },
377 { NOTMUCH_OPT_KEYWORD, &exclude, "exclude", 'x',
378 (notmuch_keyword_t []){ { "true", NOTMUCH_EXCLUDE_TRUE },
379 { "false", NOTMUCH_EXCLUDE_FALSE },
380 { "flag", NOTMUCH_EXCLUDE_FLAG },
381 { "all", NOTMUCH_EXCLUDE_ALL },
383 { NOTMUCH_OPT_INT, &opt.offset, "offset", 'O', 0 },
384 { NOTMUCH_OPT_INT, &opt.limit, "limit", 'L', 0 },
385 { NOTMUCH_OPT_INT, &opt.dupe, "duplicate", 'D', 0 },
389 opt_index = parse_arguments (argc, argv, options, 1);
393 switch (format_sel) {
394 case NOTMUCH_FORMAT_TEXT:
395 opt.format = sprinter_text_create (config, stdout);
397 case NOTMUCH_FORMAT_TEXT0:
398 if (opt.output == OUTPUT_SUMMARY) {
399 fprintf (stderr, "Error: --format=text0 is not compatible with --output=summary.\n");
402 opt.format = sprinter_text0_create (config, stdout);
404 case NOTMUCH_FORMAT_JSON:
405 opt.format = sprinter_json_create (config, stdout);
407 case NOTMUCH_FORMAT_SEXP:
408 opt.format = sprinter_sexp_create (config, stdout);
411 /* this should never happen */
412 INTERNAL_ERROR("no output format selected");
415 notmuch_exit_if_unsupported_format ();
417 if (notmuch_database_open (notmuch_config_get_database_path (config),
418 NOTMUCH_DATABASE_MODE_READ_ONLY, ¬much))
421 query_str = query_string_from_args (notmuch, argc-opt_index, argv+opt_index);
422 if (query_str == NULL) {
423 fprintf (stderr, "Out of memory.\n");
426 if (*query_str == '\0') {
427 fprintf (stderr, "Error: notmuch search requires at least one search term.\n");
431 opt.query = notmuch_query_create (notmuch, query_str);
432 if (opt.query == NULL) {
433 fprintf (stderr, "Out of memory\n");
437 notmuch_query_set_sort (opt.query, opt.sort);
439 if (exclude == NOTMUCH_EXCLUDE_FLAG && opt.output != OUTPUT_SUMMARY) {
440 /* If we are not doing summary output there is nowhere to
441 * print the excluded flag so fall back on including the
442 * excluded messages. */
443 fprintf (stderr, "Warning: this output format cannot flag excluded messages.\n");
444 exclude = NOTMUCH_EXCLUDE_FALSE;
447 if (exclude != NOTMUCH_EXCLUDE_FALSE) {
448 const char **search_exclude_tags;
449 size_t search_exclude_tags_length;
451 search_exclude_tags = notmuch_config_get_search_exclude_tags
452 (config, &search_exclude_tags_length);
453 for (i = 0; i < search_exclude_tags_length; i++)
454 notmuch_query_add_tag_exclude (opt.query, search_exclude_tags[i]);
455 notmuch_query_set_omit_excluded (opt.query, exclude);
458 switch (opt.output) {
462 ret = do_search_threads (&opt);
464 case OUTPUT_MESSAGES:
466 ret = do_search_messages (&opt);
469 ret = do_search_tags (notmuch, &opt);
473 notmuch_query_destroy (opt.query);
474 notmuch_database_destroy (notmuch);
476 talloc_free (opt.format);
478 return ret ? EXIT_FAILURE : EXIT_SUCCESS;