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 (void *ctx, int argc, char *argv[])
295 notmuch_config_t *config;
296 notmuch_database_t *notmuch;
297 notmuch_query_t *query;
299 notmuch_sort_t sort = NOTMUCH_SORT_NEWEST_FIRST;
300 sprinter_t *format = NULL;
302 output_t output = OUTPUT_SUMMARY;
304 int limit = -1; /* unlimited */
305 int exclude = EXCLUDE_TRUE;
311 NOTMUCH_FORMAT_TEXT0,
313 } format_sel = NOTMUCH_FORMAT_TEXT;
315 notmuch_opt_desc_t options[] = {
316 { NOTMUCH_OPT_KEYWORD, &sort, "sort", 's',
317 (notmuch_keyword_t []){ { "oldest-first", NOTMUCH_SORT_OLDEST_FIRST },
318 { "newest-first", NOTMUCH_SORT_NEWEST_FIRST },
320 { NOTMUCH_OPT_KEYWORD, &format_sel, "format", 'f',
321 (notmuch_keyword_t []){ { "json", NOTMUCH_FORMAT_JSON },
322 { "sexp", NOTMUCH_FORMAT_SEXP },
323 { "text", NOTMUCH_FORMAT_TEXT },
324 { "text0", NOTMUCH_FORMAT_TEXT0 },
326 { NOTMUCH_OPT_INT, ¬much_format_version, "format-version", 0, 0 },
327 { NOTMUCH_OPT_KEYWORD, &output, "output", 'o',
328 (notmuch_keyword_t []){ { "summary", OUTPUT_SUMMARY },
329 { "threads", OUTPUT_THREADS },
330 { "messages", OUTPUT_MESSAGES },
331 { "files", OUTPUT_FILES },
332 { "tags", OUTPUT_TAGS },
334 { NOTMUCH_OPT_KEYWORD, &exclude, "exclude", 'x',
335 (notmuch_keyword_t []){ { "true", EXCLUDE_TRUE },
336 { "false", EXCLUDE_FALSE },
337 { "flag", EXCLUDE_FLAG },
339 { NOTMUCH_OPT_INT, &offset, "offset", 'O', 0 },
340 { NOTMUCH_OPT_INT, &limit, "limit", 'L', 0 },
344 opt_index = parse_arguments (argc, argv, options, 1);
350 switch (format_sel) {
351 case NOTMUCH_FORMAT_TEXT:
352 format = sprinter_text_create (ctx, stdout);
354 case NOTMUCH_FORMAT_TEXT0:
355 if (output == OUTPUT_SUMMARY) {
356 fprintf (stderr, "Error: --format=text0 is not compatible with --output=summary.\n");
359 format = sprinter_text0_create (ctx, stdout);
361 case NOTMUCH_FORMAT_JSON:
362 format = sprinter_json_create (ctx, stdout);
364 case NOTMUCH_FORMAT_SEXP:
365 format = sprinter_sexp_create (ctx, stdout);
368 /* this should never happen */
369 INTERNAL_ERROR("no output format selected");
372 notmuch_exit_if_unsupported_format ();
374 config = notmuch_config_open (ctx, NULL, NULL);
378 if (notmuch_database_open (notmuch_config_get_database_path (config),
379 NOTMUCH_DATABASE_MODE_READ_ONLY, ¬much))
382 query_str = query_string_from_args (notmuch, argc-opt_index, argv+opt_index);
383 if (query_str == NULL) {
384 fprintf (stderr, "Out of memory.\n");
387 if (*query_str == '\0') {
388 fprintf (stderr, "Error: notmuch search requires at least one search term.\n");
392 query = notmuch_query_create (notmuch, query_str);
394 fprintf (stderr, "Out of memory\n");
398 notmuch_query_set_sort (query, sort);
400 if (exclude == EXCLUDE_FLAG && output != OUTPUT_SUMMARY) {
401 /* If we are not doing summary output there is nowhere to
402 * print the excluded flag so fall back on including the
403 * excluded messages. */
404 fprintf (stderr, "Warning: this output format cannot flag excluded messages.\n");
405 exclude = EXCLUDE_FALSE;
408 if (exclude == EXCLUDE_TRUE || exclude == EXCLUDE_FLAG) {
409 const char **search_exclude_tags;
410 size_t search_exclude_tags_length;
412 search_exclude_tags = notmuch_config_get_search_exclude_tags
413 (config, &search_exclude_tags_length);
414 for (i = 0; i < search_exclude_tags_length; i++)
415 notmuch_query_add_tag_exclude (query, search_exclude_tags[i]);
416 if (exclude == EXCLUDE_FLAG)
417 notmuch_query_set_omit_excluded (query, FALSE);
424 ret = do_search_threads (format, query, sort, output, offset, limit);
426 case OUTPUT_MESSAGES:
428 ret = do_search_messages (format, query, output, offset, limit);
431 ret = do_search_tags (notmuch, format, query);
435 notmuch_query_destroy (query);
436 notmuch_database_destroy (notmuch);
438 talloc_free (format);