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"
31 typedef struct search_format {
32 const char *results_start;
33 const char *item_start;
34 void (*item_id) (const void *ctx,
35 const char *item_type,
37 void (*thread_summary) (const void *ctx,
38 const char *thread_id,
44 const char *tag_start;
50 const char *results_end;
51 const char *results_null;
55 format_item_id_text (const void *ctx,
56 const char *item_type,
60 format_thread_text (const void *ctx,
61 const char *thread_id,
67 static const search_format_t format_text = {
81 format_item_id_json (const void *ctx,
82 const char *item_type,
86 format_thread_json (const void *ctx,
87 const char *thread_id,
94 /* Any changes to the JSON format should be reflected in the file
96 static const search_format_t format_json = {
110 format_item_id_text (unused (const void *ctx),
111 const char *item_type,
114 printf ("%s%s", item_type, item_id);
118 sanitize_string (const void *ctx, const char *str)
125 loop = out = talloc_strdup (ctx, str);
127 for (; *loop; loop++) {
128 if ((unsigned char)(*loop) < 32)
135 format_thread_text (const void *ctx,
136 const char *thread_id,
143 void *ctx_quote = talloc_new (ctx);
145 printf ("thread:%s %12s [%d/%d] %s; %s",
147 notmuch_time_relative_date (ctx, date),
150 sanitize_string (ctx_quote, authors),
151 sanitize_string (ctx_quote, subject));
153 talloc_free (ctx_quote);
157 format_item_id_json (const void *ctx,
158 unused (const char *item_type),
161 void *ctx_quote = talloc_new (ctx);
163 printf ("%s", json_quote_str (ctx_quote, item_id));
165 talloc_free (ctx_quote);
170 format_thread_json (const void *ctx,
171 const char *thread_id,
178 void *ctx_quote = talloc_new (ctx);
180 printf ("\"thread\": %s,\n"
181 "\"timestamp\": %ld,\n"
182 "\"date_relative\": \"%s\",\n"
186 "\"subject\": %s,\n",
187 json_quote_str (ctx_quote, thread_id),
189 notmuch_time_relative_date (ctx, date),
192 json_quote_str (ctx_quote, authors),
193 json_quote_str (ctx_quote, subject));
195 talloc_free (ctx_quote);
199 do_search_threads (const search_format_t *format,
200 notmuch_query_t *query,
206 notmuch_thread_t *thread;
207 notmuch_threads_t *threads;
208 notmuch_tags_t *tags;
210 int first_thread = 1;
213 if (output == OUTPUT_THREADS)
214 notmuch_query_set_omit_excluded_messages (query, TRUE);
217 offset += notmuch_query_count_threads (query);
222 threads = notmuch_query_search_threads (query);
226 fputs (format->results_start, stdout);
229 notmuch_threads_valid (threads) && (limit < 0 || i < offset + limit);
230 notmuch_threads_move_to_next (threads), i++)
234 thread = notmuch_threads_get (threads);
237 notmuch_thread_destroy (thread);
242 fputs (format->item_sep, stdout);
244 if (output == OUTPUT_THREADS) {
245 format->item_id (thread, "thread:",
246 notmuch_thread_get_thread_id (thread));
247 } else { /* output == OUTPUT_SUMMARY */
248 fputs (format->item_start, stdout);
250 if (sort == NOTMUCH_SORT_OLDEST_FIRST)
251 date = notmuch_thread_get_oldest_date (thread);
253 date = notmuch_thread_get_newest_date (thread);
255 format->thread_summary (thread,
256 notmuch_thread_get_thread_id (thread),
258 notmuch_thread_get_matched_messages (thread),
259 notmuch_thread_get_total_messages (thread),
260 notmuch_thread_get_authors (thread),
261 notmuch_thread_get_subject (thread));
263 fputs (format->tag_start, stdout);
265 for (tags = notmuch_thread_get_tags (thread);
266 notmuch_tags_valid (tags);
267 notmuch_tags_move_to_next (tags))
270 fputs (format->tag_sep, stdout);
271 printf (format->tag, notmuch_tags_get (tags));
275 fputs (format->tag_end, stdout);
277 fputs (format->item_end, stdout);
282 notmuch_thread_destroy (thread);
286 fputs (format->results_null, stdout);
288 fputs (format->results_end, stdout);
294 do_search_messages (const search_format_t *format,
295 notmuch_query_t *query,
300 notmuch_message_t *message;
301 notmuch_messages_t *messages;
302 notmuch_filenames_t *filenames;
303 int first_message = 1;
306 notmuch_query_set_omit_excluded_messages (query, TRUE);
309 offset += notmuch_query_count_messages (query);
314 messages = notmuch_query_search_messages (query);
315 if (messages == NULL)
318 fputs (format->results_start, stdout);
321 notmuch_messages_valid (messages) && (limit < 0 || i < offset + limit);
322 notmuch_messages_move_to_next (messages), i++)
327 message = notmuch_messages_get (messages);
329 if (output == OUTPUT_FILES) {
330 filenames = notmuch_message_get_filenames (message);
333 notmuch_filenames_valid (filenames);
334 notmuch_filenames_move_to_next (filenames))
337 fputs (format->item_sep, stdout);
339 format->item_id (message, "",
340 notmuch_filenames_get (filenames));
345 notmuch_filenames_destroy( filenames );
347 } else { /* output == OUTPUT_MESSAGES */
349 fputs (format->item_sep, stdout);
351 format->item_id (message, "id:",
352 notmuch_message_get_message_id (message));
356 notmuch_message_destroy (message);
359 notmuch_messages_destroy (messages);
362 fputs (format->results_null, stdout);
364 fputs (format->results_end, stdout);
370 do_search_tags (notmuch_database_t *notmuch,
371 const search_format_t *format,
372 notmuch_query_t *query)
374 notmuch_messages_t *messages = NULL;
375 notmuch_tags_t *tags;
379 notmuch_query_set_omit_excluded_messages (query, TRUE);
380 /* should the following only special case if no excluded terms
383 /* Special-case query of "*" for better performance. */
384 if (strcmp (notmuch_query_get_query_string (query), "*") == 0) {
385 tags = notmuch_database_get_all_tags (notmuch);
387 messages = notmuch_query_search_messages (query);
388 if (messages == NULL)
391 tags = notmuch_messages_collect_tags (messages);
396 fputs (format->results_start, stdout);
399 notmuch_tags_valid (tags);
400 notmuch_tags_move_to_next (tags))
402 tag = notmuch_tags_get (tags);
405 fputs (format->item_sep, stdout);
407 format->item_id (tags, "", tag);
412 notmuch_tags_destroy (tags);
415 notmuch_messages_destroy (messages);
418 fputs (format->results_null, stdout);
420 fputs (format->results_end, stdout);
426 notmuch_search_command (void *ctx, int argc, char *argv[])
428 notmuch_config_t *config;
429 notmuch_database_t *notmuch;
430 notmuch_query_t *query;
432 notmuch_sort_t sort = NOTMUCH_SORT_NEWEST_FIRST;
433 const search_format_t *format = &format_text;
435 output_t output = OUTPUT_SUMMARY;
437 int limit = -1; /* unlimited */
438 notmuch_bool_t no_exclude = FALSE;
441 enum { NOTMUCH_FORMAT_JSON, NOTMUCH_FORMAT_TEXT }
442 format_sel = NOTMUCH_FORMAT_TEXT;
444 notmuch_opt_desc_t options[] = {
445 { NOTMUCH_OPT_KEYWORD, &sort, "sort", 's',
446 (notmuch_keyword_t []){ { "oldest-first", NOTMUCH_SORT_OLDEST_FIRST },
447 { "newest-first", NOTMUCH_SORT_NEWEST_FIRST },
449 { NOTMUCH_OPT_KEYWORD, &format_sel, "format", 'f',
450 (notmuch_keyword_t []){ { "json", NOTMUCH_FORMAT_JSON },
451 { "text", NOTMUCH_FORMAT_TEXT },
453 { NOTMUCH_OPT_KEYWORD, &output, "output", 'o',
454 (notmuch_keyword_t []){ { "summary", OUTPUT_SUMMARY },
455 { "threads", OUTPUT_THREADS },
456 { "messages", OUTPUT_MESSAGES },
457 { "files", OUTPUT_FILES },
458 { "tags", OUTPUT_TAGS },
460 { NOTMUCH_OPT_BOOLEAN, &no_exclude, "no-exclude", 'd', 0 },
461 { NOTMUCH_OPT_INT, &offset, "offset", 'O', 0 },
462 { NOTMUCH_OPT_INT, &limit, "limit", 'L', 0 },
466 opt_index = parse_arguments (argc, argv, options, 1);
472 switch (format_sel) {
473 case NOTMUCH_FORMAT_TEXT:
474 format = &format_text;
476 case NOTMUCH_FORMAT_JSON:
477 format = &format_json;
481 config = notmuch_config_open (ctx, NULL, NULL);
485 notmuch = notmuch_database_open (notmuch_config_get_database_path (config),
486 NOTMUCH_DATABASE_MODE_READ_ONLY);
490 query_str = query_string_from_args (notmuch, argc-opt_index, argv+opt_index);
491 if (query_str == NULL) {
492 fprintf (stderr, "Out of memory.\n");
495 if (*query_str == '\0') {
496 fprintf (stderr, "Error: notmuch search requires at least one search term.\n");
500 query = notmuch_query_create (notmuch, query_str);
502 fprintf (stderr, "Out of memory\n");
506 notmuch_query_set_sort (query, sort);
509 const char **search_exclude_tags;
510 size_t search_exclude_tags_length;
512 search_exclude_tags = notmuch_config_get_search_exclude_tags
513 (config, &search_exclude_tags_length);
514 for (i = 0; i < search_exclude_tags_length; i++)
515 notmuch_query_add_tag_exclude (query, search_exclude_tags[i]);
522 ret = do_search_threads (format, query, sort, output, offset, limit);
524 case OUTPUT_MESSAGES:
526 ret = do_search_messages (format, query, output, offset, limit);
529 ret = do_search_tags (notmuch, format, query);
533 notmuch_query_destroy (query);
534 notmuch_database_close (notmuch);