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;
308 enum { NOTMUCH_FORMAT_JSON, NOTMUCH_FORMAT_TEXT, NOTMUCH_FORMAT_SEXP }
309 format_sel = NOTMUCH_FORMAT_TEXT;
311 notmuch_opt_desc_t options[] = {
312 { NOTMUCH_OPT_KEYWORD, &sort, "sort", 's',
313 (notmuch_keyword_t []){ { "oldest-first", NOTMUCH_SORT_OLDEST_FIRST },
314 { "newest-first", NOTMUCH_SORT_NEWEST_FIRST },
316 { NOTMUCH_OPT_KEYWORD, &format_sel, "format", 'f',
317 (notmuch_keyword_t []){ { "json", NOTMUCH_FORMAT_JSON },
318 { "sexp", NOTMUCH_FORMAT_SEXP },
319 { "text", NOTMUCH_FORMAT_TEXT },
321 { NOTMUCH_OPT_INT, ¬much_format_version, "format-version", 0, 0 },
322 { NOTMUCH_OPT_KEYWORD, &output, "output", 'o',
323 (notmuch_keyword_t []){ { "summary", OUTPUT_SUMMARY },
324 { "threads", OUTPUT_THREADS },
325 { "messages", OUTPUT_MESSAGES },
326 { "files", OUTPUT_FILES },
327 { "tags", OUTPUT_TAGS },
329 { NOTMUCH_OPT_KEYWORD, &exclude, "exclude", 'x',
330 (notmuch_keyword_t []){ { "true", EXCLUDE_TRUE },
331 { "false", EXCLUDE_FALSE },
332 { "flag", EXCLUDE_FLAG },
334 { NOTMUCH_OPT_INT, &offset, "offset", 'O', 0 },
335 { NOTMUCH_OPT_INT, &limit, "limit", 'L', 0 },
339 opt_index = parse_arguments (argc, argv, options, 1);
345 switch (format_sel) {
346 case NOTMUCH_FORMAT_TEXT:
347 format = sprinter_text_create (ctx, stdout);
349 case NOTMUCH_FORMAT_JSON:
350 format = sprinter_json_create (ctx, stdout);
352 case NOTMUCH_FORMAT_SEXP:
353 format = sprinter_sexp_create (ctx, stdout);
356 /* this should never happen */
357 INTERNAL_ERROR("no output format selected");
360 notmuch_exit_if_unsupported_format ();
362 config = notmuch_config_open (ctx, NULL, NULL);
366 if (notmuch_database_open (notmuch_config_get_database_path (config),
367 NOTMUCH_DATABASE_MODE_READ_ONLY, ¬much))
370 query_str = query_string_from_args (notmuch, argc-opt_index, argv+opt_index);
371 if (query_str == NULL) {
372 fprintf (stderr, "Out of memory.\n");
375 if (*query_str == '\0') {
376 fprintf (stderr, "Error: notmuch search requires at least one search term.\n");
380 query = notmuch_query_create (notmuch, query_str);
382 fprintf (stderr, "Out of memory\n");
386 notmuch_query_set_sort (query, sort);
388 if (exclude == EXCLUDE_FLAG && output != OUTPUT_SUMMARY) {
389 /* If we are not doing summary output there is nowhere to
390 * print the excluded flag so fall back on including the
391 * excluded messages. */
392 fprintf (stderr, "Warning: this output format cannot flag excluded messages.\n");
393 exclude = EXCLUDE_FALSE;
396 if (exclude == EXCLUDE_TRUE || exclude == EXCLUDE_FLAG) {
397 const char **search_exclude_tags;
398 size_t search_exclude_tags_length;
400 search_exclude_tags = notmuch_config_get_search_exclude_tags
401 (config, &search_exclude_tags_length);
402 for (i = 0; i < search_exclude_tags_length; i++)
403 notmuch_query_add_tag_exclude (query, search_exclude_tags[i]);
404 if (exclude == EXCLUDE_FLAG)
405 notmuch_query_set_omit_excluded (query, FALSE);
412 ret = do_search_threads (format, query, sort, output, offset, limit);
414 case OUTPUT_MESSAGES:
416 ret = do_search_messages (format, query, output, offset, limit);
419 ret = do_search_tags (notmuch, format, query);
423 notmuch_query_destroy (query);
424 notmuch_database_destroy (notmuch);
426 talloc_free (format);