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"
34 sanitize_string (const void *ctx, const char *str)
41 loop = out = talloc_strdup (ctx, str);
43 for (; *loop; loop++) {
44 if (*loop == '\t' || *loop == '\n')
46 else if ((unsigned char)(*loop) < 32)
52 /* Return two stable query strings that identify exactly the matched
53 * and unmatched messages currently in thread. If there are no
54 * matched or unmatched messages, the returned buffers will be
57 get_thread_query (notmuch_thread_t *thread,
58 char **matched_out, char **unmatched_out)
60 notmuch_messages_t *messages;
62 size_t escaped_len = 0;
64 *matched_out = *unmatched_out = NULL;
66 for (messages = notmuch_thread_get_messages (thread);
67 notmuch_messages_valid (messages);
68 notmuch_messages_move_to_next (messages))
70 notmuch_message_t *message = notmuch_messages_get (messages);
71 const char *mid = notmuch_message_get_message_id (message);
72 /* Determine which query buffer to extend */
73 char **buf = notmuch_message_get_flag (
74 message, NOTMUCH_MESSAGE_FLAG_MATCH) ? matched_out : unmatched_out;
75 /* Add this message's id: query. Since "id" is an exclusive
76 * prefix, it is implicitly 'or'd together, so we only need to
77 * join queries with a space. */
78 if (make_boolean_term (thread, "id", mid, &escaped, &escaped_len) < 0)
81 *buf = talloc_asprintf_append_buffer (*buf, " %s", escaped);
83 *buf = talloc_strdup (thread, escaped);
87 talloc_free (escaped);
92 do_search_threads (sprinter_t *format,
93 notmuch_query_t *query,
99 notmuch_thread_t *thread;
100 notmuch_threads_t *threads;
101 notmuch_tags_t *tags;
106 offset += notmuch_query_count_threads (query);
111 threads = notmuch_query_search_threads (query);
115 format->begin_list (format);
118 notmuch_threads_valid (threads) && (limit < 0 || i < offset + limit);
119 notmuch_threads_move_to_next (threads), i++)
121 thread = notmuch_threads_get (threads);
124 notmuch_thread_destroy (thread);
128 if (output == OUTPUT_THREADS) {
129 format->set_prefix (format, "thread");
130 format->string (format,
131 notmuch_thread_get_thread_id (thread));
132 format->separator (format);
133 } else { /* output == OUTPUT_SUMMARY */
134 void *ctx_quote = talloc_new (thread);
135 const char *authors = notmuch_thread_get_authors (thread);
136 const char *subject = notmuch_thread_get_subject (thread);
137 const char *thread_id = notmuch_thread_get_thread_id (thread);
138 int matched = notmuch_thread_get_matched_messages (thread);
139 int total = notmuch_thread_get_total_messages (thread);
140 const char *relative_date = NULL;
141 notmuch_bool_t first_tag = TRUE;
143 format->begin_map (format);
145 if (sort == NOTMUCH_SORT_OLDEST_FIRST)
146 date = notmuch_thread_get_oldest_date (thread);
148 date = notmuch_thread_get_newest_date (thread);
150 relative_date = notmuch_time_relative_date (ctx_quote, date);
152 if (format->is_text_printer) {
153 /* Special case for the text formatter */
154 printf ("thread:%s %12s [%d/%d] %s; %s (",
159 sanitize_string (ctx_quote, authors),
160 sanitize_string (ctx_quote, subject));
161 } else { /* Structured Output */
162 format->map_key (format, "thread");
163 format->string (format, thread_id);
164 format->map_key (format, "timestamp");
165 format->integer (format, date);
166 format->map_key (format, "date_relative");
167 format->string (format, relative_date);
168 format->map_key (format, "matched");
169 format->integer (format, matched);
170 format->map_key (format, "total");
171 format->integer (format, total);
172 format->map_key (format, "authors");
173 format->string (format, authors);
174 format->map_key (format, "subject");
175 format->string (format, subject);
176 if (notmuch_format_version >= 2) {
177 char *matched_query, *unmatched_query;
178 if (get_thread_query (thread, &matched_query,
179 &unmatched_query) < 0) {
180 fprintf (stderr, "Out of memory\n");
183 format->map_key (format, "query");
184 format->begin_list (format);
186 format->string (format, matched_query);
188 format->null (format);
190 format->string (format, unmatched_query);
192 format->null (format);
193 format->end (format);
197 talloc_free (ctx_quote);
199 format->map_key (format, "tags");
200 format->begin_list (format);
202 for (tags = notmuch_thread_get_tags (thread);
203 notmuch_tags_valid (tags);
204 notmuch_tags_move_to_next (tags))
206 const char *tag = notmuch_tags_get (tags);
208 if (format->is_text_printer) {
209 /* Special case for the text formatter */
215 } else { /* Structured Output */
216 format->string (format, tag);
220 if (format->is_text_printer)
223 format->end (format);
224 format->end (format);
225 format->separator (format);
228 notmuch_thread_destroy (thread);
231 format->end (format);
237 do_search_messages (sprinter_t *format,
238 notmuch_query_t *query,
244 notmuch_message_t *message;
245 notmuch_messages_t *messages;
246 notmuch_filenames_t *filenames;
250 offset += notmuch_query_count_messages (query);
255 messages = notmuch_query_search_messages (query);
256 if (messages == NULL)
259 format->begin_list (format);
262 notmuch_messages_valid (messages) && (limit < 0 || i < offset + limit);
263 notmuch_messages_move_to_next (messages), i++)
268 message = notmuch_messages_get (messages);
270 if (output == OUTPUT_FILES) {
272 filenames = notmuch_message_get_filenames (message);
275 notmuch_filenames_valid (filenames);
276 notmuch_filenames_move_to_next (filenames), j++)
278 if (dupe < 0 || dupe == j) {
279 format->string (format, notmuch_filenames_get (filenames));
280 format->separator (format);
284 notmuch_filenames_destroy( filenames );
286 } else { /* output == OUTPUT_MESSAGES */
287 format->set_prefix (format, "id");
288 format->string (format,
289 notmuch_message_get_message_id (message));
290 format->separator (format);
293 notmuch_message_destroy (message);
296 notmuch_messages_destroy (messages);
298 format->end (format);
304 do_search_tags (notmuch_database_t *notmuch,
306 notmuch_query_t *query)
308 notmuch_messages_t *messages = NULL;
309 notmuch_tags_t *tags;
312 /* should the following only special case if no excluded terms
315 /* Special-case query of "*" for better performance. */
316 if (strcmp (notmuch_query_get_query_string (query), "*") == 0) {
317 tags = notmuch_database_get_all_tags (notmuch);
319 messages = notmuch_query_search_messages (query);
320 if (messages == NULL)
323 tags = notmuch_messages_collect_tags (messages);
328 format->begin_list (format);
331 notmuch_tags_valid (tags);
332 notmuch_tags_move_to_next (tags))
334 tag = notmuch_tags_get (tags);
336 format->string (format, tag);
337 format->separator (format);
341 notmuch_tags_destroy (tags);
344 notmuch_messages_destroy (messages);
346 format->end (format);
352 notmuch_search_command (notmuch_config_t *config, int argc, char *argv[])
354 notmuch_database_t *notmuch;
355 notmuch_query_t *query;
357 notmuch_sort_t sort = NOTMUCH_SORT_NEWEST_FIRST;
358 sprinter_t *format = NULL;
360 output_t output = OUTPUT_SUMMARY;
362 int limit = -1; /* unlimited */
363 notmuch_exclude_t exclude = NOTMUCH_EXCLUDE_TRUE;
370 NOTMUCH_FORMAT_TEXT0,
372 } format_sel = NOTMUCH_FORMAT_TEXT;
374 notmuch_opt_desc_t options[] = {
375 { NOTMUCH_OPT_KEYWORD, &sort, "sort", 's',
376 (notmuch_keyword_t []){ { "oldest-first", NOTMUCH_SORT_OLDEST_FIRST },
377 { "newest-first", NOTMUCH_SORT_NEWEST_FIRST },
379 { NOTMUCH_OPT_KEYWORD, &format_sel, "format", 'f',
380 (notmuch_keyword_t []){ { "json", NOTMUCH_FORMAT_JSON },
381 { "sexp", NOTMUCH_FORMAT_SEXP },
382 { "text", NOTMUCH_FORMAT_TEXT },
383 { "text0", NOTMUCH_FORMAT_TEXT0 },
385 { NOTMUCH_OPT_INT, ¬much_format_version, "format-version", 0, 0 },
386 { NOTMUCH_OPT_KEYWORD, &output, "output", 'o',
387 (notmuch_keyword_t []){ { "summary", OUTPUT_SUMMARY },
388 { "threads", OUTPUT_THREADS },
389 { "messages", OUTPUT_MESSAGES },
390 { "files", OUTPUT_FILES },
391 { "tags", OUTPUT_TAGS },
393 { NOTMUCH_OPT_KEYWORD, &exclude, "exclude", 'x',
394 (notmuch_keyword_t []){ { "true", NOTMUCH_EXCLUDE_TRUE },
395 { "false", NOTMUCH_EXCLUDE_FALSE },
396 { "flag", NOTMUCH_EXCLUDE_FLAG },
397 { "all", NOTMUCH_EXCLUDE_ALL },
399 { NOTMUCH_OPT_INT, &offset, "offset", 'O', 0 },
400 { NOTMUCH_OPT_INT, &limit, "limit", 'L', 0 },
401 { NOTMUCH_OPT_INT, &dupe, "duplicate", 'D', 0 },
405 opt_index = parse_arguments (argc, argv, options, 1);
409 switch (format_sel) {
410 case NOTMUCH_FORMAT_TEXT:
411 format = sprinter_text_create (config, stdout);
413 case NOTMUCH_FORMAT_TEXT0:
414 if (output == OUTPUT_SUMMARY) {
415 fprintf (stderr, "Error: --format=text0 is not compatible with --output=summary.\n");
418 format = sprinter_text0_create (config, stdout);
420 case NOTMUCH_FORMAT_JSON:
421 format = sprinter_json_create (config, stdout);
423 case NOTMUCH_FORMAT_SEXP:
424 format = sprinter_sexp_create (config, stdout);
427 /* this should never happen */
428 INTERNAL_ERROR("no output format selected");
431 notmuch_exit_if_unsupported_format ();
433 if (notmuch_database_open (notmuch_config_get_database_path (config),
434 NOTMUCH_DATABASE_MODE_READ_ONLY, ¬much))
437 query_str = query_string_from_args (notmuch, argc-opt_index, argv+opt_index);
438 if (query_str == NULL) {
439 fprintf (stderr, "Out of memory.\n");
442 if (*query_str == '\0') {
443 fprintf (stderr, "Error: notmuch search requires at least one search term.\n");
447 query = notmuch_query_create (notmuch, query_str);
449 fprintf (stderr, "Out of memory\n");
453 notmuch_query_set_sort (query, sort);
455 if (exclude == NOTMUCH_EXCLUDE_FLAG && output != OUTPUT_SUMMARY) {
456 /* If we are not doing summary output there is nowhere to
457 * print the excluded flag so fall back on including the
458 * excluded messages. */
459 fprintf (stderr, "Warning: this output format cannot flag excluded messages.\n");
460 exclude = NOTMUCH_EXCLUDE_FALSE;
463 if (exclude != NOTMUCH_EXCLUDE_FALSE) {
464 const char **search_exclude_tags;
465 size_t search_exclude_tags_length;
467 search_exclude_tags = notmuch_config_get_search_exclude_tags
468 (config, &search_exclude_tags_length);
469 for (i = 0; i < search_exclude_tags_length; i++)
470 notmuch_query_add_tag_exclude (query, search_exclude_tags[i]);
471 notmuch_query_set_omit_excluded (query, exclude);
478 ret = do_search_threads (format, query, sort, output, offset, limit);
480 case OUTPUT_MESSAGES:
482 ret = do_search_messages (format, query, output, offset, limit, dupe);
485 ret = do_search_tags (notmuch, format, query);
489 notmuch_query_destroy (query);
490 notmuch_database_destroy (notmuch);
492 talloc_free (format);
494 return ret ? EXIT_FAILURE : EXIT_SUCCESS;