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"
33 sanitize_string (const void *ctx, const char *str)
40 loop = out = talloc_strdup (ctx, str);
42 for (; *loop; loop++) {
43 if ((unsigned char)(*loop) < 32)
50 do_search_threads (sprinter_t *format,
51 notmuch_query_t *query,
57 notmuch_thread_t *thread;
58 notmuch_threads_t *threads;
64 offset += notmuch_query_count_threads (query);
69 threads = notmuch_query_search_threads (query);
73 format->begin_list (format);
76 notmuch_threads_valid (threads) && (limit < 0 || i < offset + limit);
77 notmuch_threads_move_to_next (threads), i++)
79 thread = notmuch_threads_get (threads);
82 notmuch_thread_destroy (thread);
86 if (output == OUTPUT_THREADS) {
87 format->set_prefix (format, "thread");
88 format->string (format,
89 notmuch_thread_get_thread_id (thread));
90 format->separator (format);
91 } else { /* output == OUTPUT_SUMMARY */
92 void *ctx_quote = talloc_new (thread);
93 const char *authors = notmuch_thread_get_authors (thread);
94 const char *subject = notmuch_thread_get_subject (thread);
95 const char *thread_id = notmuch_thread_get_thread_id (thread);
96 int matched = notmuch_thread_get_matched_messages (thread);
97 int total = notmuch_thread_get_total_messages (thread);
98 const char *relative_date = NULL;
99 notmuch_bool_t first_tag = TRUE;
101 format->begin_map (format);
103 if (sort == NOTMUCH_SORT_OLDEST_FIRST)
104 date = notmuch_thread_get_oldest_date (thread);
106 date = notmuch_thread_get_newest_date (thread);
108 relative_date = notmuch_time_relative_date (ctx_quote, date);
110 if (format->is_text_printer) {
111 /* Special case for the text formatter */
112 printf ("thread:%s %12s [%d/%d] %s; %s (",
117 sanitize_string (ctx_quote, authors),
118 sanitize_string (ctx_quote, subject));
119 } else { /* Structured Output */
120 format->map_key (format, "thread");
121 format->string (format, thread_id);
122 format->map_key (format, "timestamp");
123 format->integer (format, date);
124 format->map_key (format, "date_relative");
125 format->string (format, relative_date);
126 format->map_key (format, "matched");
127 format->integer (format, matched);
128 format->map_key (format, "total");
129 format->integer (format, total);
130 format->map_key (format, "authors");
131 format->string (format, authors);
132 format->map_key (format, "subject");
133 format->string (format, subject);
136 talloc_free (ctx_quote);
138 format->map_key (format, "tags");
139 format->begin_list (format);
141 for (tags = notmuch_thread_get_tags (thread);
142 notmuch_tags_valid (tags);
143 notmuch_tags_move_to_next (tags))
145 const char *tag = notmuch_tags_get (tags);
147 if (format->is_text_printer) {
148 /* Special case for the text formatter */
154 } else { /* Structured Output */
155 format->string (format, tag);
159 if (format->is_text_printer)
162 format->end (format);
163 format->end (format);
164 format->separator (format);
167 notmuch_thread_destroy (thread);
170 format->end (format);
176 do_search_messages (sprinter_t *format,
177 notmuch_query_t *query,
182 notmuch_message_t *message;
183 notmuch_messages_t *messages;
184 notmuch_filenames_t *filenames;
188 offset += notmuch_query_count_messages (query);
193 messages = notmuch_query_search_messages (query);
194 if (messages == NULL)
197 format->begin_list (format);
200 notmuch_messages_valid (messages) && (limit < 0 || i < offset + limit);
201 notmuch_messages_move_to_next (messages), i++)
206 message = notmuch_messages_get (messages);
208 if (output == OUTPUT_FILES) {
209 filenames = notmuch_message_get_filenames (message);
212 notmuch_filenames_valid (filenames);
213 notmuch_filenames_move_to_next (filenames))
215 format->string (format, notmuch_filenames_get (filenames));
216 format->separator (format);
219 notmuch_filenames_destroy( filenames );
221 } else { /* output == OUTPUT_MESSAGES */
222 format->set_prefix (format, "id");
223 format->string (format,
224 notmuch_message_get_message_id (message));
225 format->separator (format);
228 notmuch_message_destroy (message);
231 notmuch_messages_destroy (messages);
233 format->end (format);
239 do_search_tags (notmuch_database_t *notmuch,
241 notmuch_query_t *query)
243 notmuch_messages_t *messages = NULL;
244 notmuch_tags_t *tags;
247 /* should the following only special case if no excluded terms
250 /* Special-case query of "*" for better performance. */
251 if (strcmp (notmuch_query_get_query_string (query), "*") == 0) {
252 tags = notmuch_database_get_all_tags (notmuch);
254 messages = notmuch_query_search_messages (query);
255 if (messages == NULL)
258 tags = notmuch_messages_collect_tags (messages);
263 format->begin_list (format);
266 notmuch_tags_valid (tags);
267 notmuch_tags_move_to_next (tags))
269 tag = notmuch_tags_get (tags);
271 format->string (format, tag);
272 format->separator (format);
276 notmuch_tags_destroy (tags);
279 notmuch_messages_destroy (messages);
281 format->end (format);
293 notmuch_search_command (notmuch_config_t *config, int argc, char *argv[])
295 notmuch_database_t *notmuch;
296 notmuch_query_t *query;
298 notmuch_sort_t sort = NOTMUCH_SORT_NEWEST_FIRST;
299 sprinter_t *format = NULL;
301 output_t output = OUTPUT_SUMMARY;
303 int limit = -1; /* unlimited */
304 int exclude = EXCLUDE_TRUE;
310 NOTMUCH_FORMAT_TEXT0,
312 } format_sel = NOTMUCH_FORMAT_TEXT;
314 notmuch_opt_desc_t options[] = {
315 { NOTMUCH_OPT_KEYWORD, &sort, "sort", 's',
316 (notmuch_keyword_t []){ { "oldest-first", NOTMUCH_SORT_OLDEST_FIRST },
317 { "newest-first", NOTMUCH_SORT_NEWEST_FIRST },
319 { NOTMUCH_OPT_KEYWORD, &format_sel, "format", 'f',
320 (notmuch_keyword_t []){ { "json", NOTMUCH_FORMAT_JSON },
321 { "sexp", NOTMUCH_FORMAT_SEXP },
322 { "text", NOTMUCH_FORMAT_TEXT },
323 { "text0", NOTMUCH_FORMAT_TEXT0 },
325 { NOTMUCH_OPT_INT, ¬much_format_version, "format-version", 0, 0 },
326 { NOTMUCH_OPT_KEYWORD, &output, "output", 'o',
327 (notmuch_keyword_t []){ { "summary", OUTPUT_SUMMARY },
328 { "threads", OUTPUT_THREADS },
329 { "messages", OUTPUT_MESSAGES },
330 { "files", OUTPUT_FILES },
331 { "tags", OUTPUT_TAGS },
333 { NOTMUCH_OPT_KEYWORD, &exclude, "exclude", 'x',
334 (notmuch_keyword_t []){ { "true", EXCLUDE_TRUE },
335 { "false", EXCLUDE_FALSE },
336 { "flag", EXCLUDE_FLAG },
338 { NOTMUCH_OPT_INT, &offset, "offset", 'O', 0 },
339 { NOTMUCH_OPT_INT, &limit, "limit", 'L', 0 },
343 opt_index = parse_arguments (argc, argv, options, 1);
349 switch (format_sel) {
350 case NOTMUCH_FORMAT_TEXT:
351 format = sprinter_text_create (config, stdout);
353 case NOTMUCH_FORMAT_TEXT0:
354 if (output == OUTPUT_SUMMARY) {
355 fprintf (stderr, "Error: --format=text0 is not compatible with --output=summary.\n");
358 format = sprinter_text0_create (config, stdout);
360 case NOTMUCH_FORMAT_JSON:
361 format = sprinter_json_create (config, stdout);
363 case NOTMUCH_FORMAT_SEXP:
364 format = sprinter_sexp_create (config, stdout);
367 /* this should never happen */
368 INTERNAL_ERROR("no output format selected");
371 notmuch_exit_if_unsupported_format ();
373 if (notmuch_database_open (notmuch_config_get_database_path (config),
374 NOTMUCH_DATABASE_MODE_READ_ONLY, ¬much))
377 query_str = query_string_from_args (notmuch, argc-opt_index, argv+opt_index);
378 if (query_str == NULL) {
379 fprintf (stderr, "Out of memory.\n");
382 if (*query_str == '\0') {
383 fprintf (stderr, "Error: notmuch search requires at least one search term.\n");
387 query = notmuch_query_create (notmuch, query_str);
389 fprintf (stderr, "Out of memory\n");
393 notmuch_query_set_sort (query, sort);
395 if (exclude == EXCLUDE_FLAG && output != OUTPUT_SUMMARY) {
396 /* If we are not doing summary output there is nowhere to
397 * print the excluded flag so fall back on including the
398 * excluded messages. */
399 fprintf (stderr, "Warning: this output format cannot flag excluded messages.\n");
400 exclude = EXCLUDE_FALSE;
403 if (exclude == EXCLUDE_TRUE || exclude == EXCLUDE_FLAG) {
404 const char **search_exclude_tags;
405 size_t search_exclude_tags_length;
407 search_exclude_tags = notmuch_config_get_search_exclude_tags
408 (config, &search_exclude_tags_length);
409 for (i = 0; i < search_exclude_tags_length; i++)
410 notmuch_query_add_tag_exclude (query, search_exclude_tags[i]);
411 if (exclude == EXCLUDE_FLAG)
412 notmuch_query_set_omit_excluded (query, FALSE);
419 ret = do_search_threads (format, query, sort, output, offset, limit);
421 case OUTPUT_MESSAGES:
423 ret = do_search_messages (format, query, output, offset, limit);
426 ret = do_search_tags (notmuch, format, query);
430 notmuch_query_destroy (query);
431 notmuch_database_destroy (notmuch);
433 talloc_free (format);