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,
93 static const search_format_t format_json = {
107 format_item_id_text (unused (const void *ctx),
108 const char *item_type,
111 printf ("%s%s", item_type, item_id);
115 sanitize_string (const void *ctx, const char *str)
122 loop = out = talloc_strdup (ctx, str);
124 for (; *loop; loop++) {
125 if ((unsigned char)(*loop) < 32)
132 format_thread_text (const void *ctx,
133 const char *thread_id,
140 void *ctx_quote = talloc_new (ctx);
142 printf ("thread:%s %12s [%d/%d] %s; %s",
144 notmuch_time_relative_date (ctx, date),
147 sanitize_string (ctx_quote, authors),
148 sanitize_string (ctx_quote, subject));
150 talloc_free (ctx_quote);
154 format_item_id_json (const void *ctx,
155 unused (const char *item_type),
158 void *ctx_quote = talloc_new (ctx);
160 printf ("%s", json_quote_str (ctx_quote, item_id));
162 talloc_free (ctx_quote);
167 format_thread_json (const void *ctx,
168 const char *thread_id,
175 void *ctx_quote = talloc_new (ctx);
177 printf ("\"thread\": %s,\n"
178 "\"timestamp\": %ld,\n"
182 "\"subject\": %s,\n",
183 json_quote_str (ctx_quote, thread_id),
187 json_quote_str (ctx_quote, authors),
188 json_quote_str (ctx_quote, subject));
190 talloc_free (ctx_quote);
194 do_search_threads (const search_format_t *format,
195 notmuch_query_t *query,
201 notmuch_thread_t *thread;
202 notmuch_threads_t *threads;
203 notmuch_tags_t *tags;
205 int first_thread = 1;
209 offset += notmuch_query_count_threads (query);
214 threads = notmuch_query_search_threads (query);
218 fputs (format->results_start, stdout);
221 notmuch_threads_valid (threads) && (limit < 0 || i < offset + limit);
222 notmuch_threads_move_to_next (threads), i++)
226 thread = notmuch_threads_get (threads);
229 notmuch_thread_destroy (thread);
234 fputs (format->item_sep, stdout);
236 if (output == OUTPUT_THREADS) {
237 format->item_id (thread, "thread:",
238 notmuch_thread_get_thread_id (thread));
239 } else { /* output == OUTPUT_SUMMARY */
240 fputs (format->item_start, stdout);
242 if (sort == NOTMUCH_SORT_OLDEST_FIRST)
243 date = notmuch_thread_get_oldest_date (thread);
245 date = notmuch_thread_get_newest_date (thread);
247 format->thread_summary (thread,
248 notmuch_thread_get_thread_id (thread),
250 notmuch_thread_get_matched_messages (thread),
251 notmuch_thread_get_total_messages (thread),
252 notmuch_thread_get_authors (thread),
253 notmuch_thread_get_subject (thread));
255 fputs (format->tag_start, stdout);
257 for (tags = notmuch_thread_get_tags (thread);
258 notmuch_tags_valid (tags);
259 notmuch_tags_move_to_next (tags))
262 fputs (format->tag_sep, stdout);
263 printf (format->tag, notmuch_tags_get (tags));
267 fputs (format->tag_end, stdout);
269 fputs (format->item_end, stdout);
274 notmuch_thread_destroy (thread);
278 fputs (format->results_null, stdout);
280 fputs (format->results_end, stdout);
286 do_search_messages (const search_format_t *format,
287 notmuch_query_t *query,
292 notmuch_message_t *message;
293 notmuch_messages_t *messages;
294 notmuch_filenames_t *filenames;
295 int first_message = 1;
299 offset += notmuch_query_count_messages (query);
304 messages = notmuch_query_search_messages (query);
305 if (messages == NULL)
308 fputs (format->results_start, stdout);
311 notmuch_messages_valid (messages) && (limit < 0 || i < offset + limit);
312 notmuch_messages_move_to_next (messages), i++)
317 message = notmuch_messages_get (messages);
319 if (output == OUTPUT_FILES) {
320 filenames = notmuch_message_get_filenames (message);
323 notmuch_filenames_valid (filenames);
324 notmuch_filenames_move_to_next (filenames))
327 fputs (format->item_sep, stdout);
329 format->item_id (message, "",
330 notmuch_filenames_get (filenames));
335 notmuch_filenames_destroy( filenames );
337 } else { /* output == OUTPUT_MESSAGES */
339 fputs (format->item_sep, stdout);
341 format->item_id (message, "id:",
342 notmuch_message_get_message_id (message));
346 notmuch_message_destroy (message);
349 notmuch_messages_destroy (messages);
352 fputs (format->results_null, stdout);
354 fputs (format->results_end, stdout);
360 do_search_tags (notmuch_database_t *notmuch,
361 const search_format_t *format,
362 notmuch_query_t *query)
364 notmuch_messages_t *messages = NULL;
365 notmuch_tags_t *tags;
369 /* Special-case query of "*" for better performance. */
370 if (strcmp (notmuch_query_get_query_string (query), "*") == 0) {
371 tags = notmuch_database_get_all_tags (notmuch);
373 messages = notmuch_query_search_messages (query);
374 if (messages == NULL)
377 tags = notmuch_messages_collect_tags (messages);
382 fputs (format->results_start, stdout);
385 notmuch_tags_valid (tags);
386 notmuch_tags_move_to_next (tags))
388 tag = notmuch_tags_get (tags);
391 fputs (format->item_sep, stdout);
393 format->item_id (tags, "", tag);
398 notmuch_tags_destroy (tags);
401 notmuch_messages_destroy (messages);
404 fputs (format->results_null, stdout);
406 fputs (format->results_end, stdout);
412 notmuch_search_command (void *ctx, int argc, char *argv[])
414 notmuch_config_t *config;
415 notmuch_database_t *notmuch;
416 notmuch_query_t *query;
419 notmuch_sort_t sort = NOTMUCH_SORT_NEWEST_FIRST;
420 const search_format_t *format = &format_text;
422 output_t output = OUTPUT_SUMMARY;
424 int limit = -1; /* unlimited */
426 argc--; argv++; /* skip subcommand argument */
428 for (i = 0; i < argc && argv[i][0] == '-'; i++) {
429 if (strcmp (argv[i], "--") == 0) {
433 if (STRNCMP_LITERAL (argv[i], "--sort=") == 0) {
434 opt = argv[i] + sizeof ("--sort=") - 1;
435 if (strcmp (opt, "oldest-first") == 0) {
436 sort = NOTMUCH_SORT_OLDEST_FIRST;
437 } else if (strcmp (opt, "newest-first") == 0) {
438 sort = NOTMUCH_SORT_NEWEST_FIRST;
440 fprintf (stderr, "Invalid value for --sort: %s\n", opt);
443 } else if (STRNCMP_LITERAL (argv[i], "--offset=") == 0) {
445 opt = argv[i] + sizeof ("--offset=") - 1;
446 offset = strtol (opt, &p, 10);
447 if (*opt == '\0' || p == opt || *p != '\0') {
448 fprintf (stderr, "Invalid value for --offset: %s\n", opt);
451 } else if (STRNCMP_LITERAL (argv[i], "--limit=") == 0) {
453 opt = argv[i] + sizeof ("--limit=") - 1;
454 limit = strtoul (opt, &p, 10);
455 if (*opt == '\0' || p == opt || *p != '\0') {
456 fprintf (stderr, "Invalid value for --limit: %s\n", opt);
459 } else if (STRNCMP_LITERAL (argv[i], "--format=") == 0) {
460 opt = argv[i] + sizeof ("--format=") - 1;
461 if (strcmp (opt, "text") == 0) {
462 format = &format_text;
463 } else if (strcmp (opt, "json") == 0) {
464 format = &format_json;
466 fprintf (stderr, "Invalid value for --format: %s\n", opt);
469 } else if (STRNCMP_LITERAL (argv[i], "--output=") == 0) {
470 opt = argv[i] + sizeof ("--output=") - 1;
471 if (strcmp (opt, "summary") == 0) {
472 output = OUTPUT_SUMMARY;
473 } else if (strcmp (opt, "threads") == 0) {
474 output = OUTPUT_THREADS;
475 } else if (strcmp (opt, "messages") == 0) {
476 output = OUTPUT_MESSAGES;
477 } else if (strcmp (opt, "files") == 0) {
478 output = OUTPUT_FILES;
479 } else if (strcmp (opt, "tags") == 0) {
480 output = OUTPUT_TAGS;
482 fprintf (stderr, "Invalid value for --output: %s\n", opt);
486 fprintf (stderr, "Unrecognized option: %s\n", argv[i]);
494 config = notmuch_config_open (ctx, NULL, NULL);
498 notmuch = notmuch_database_open (notmuch_config_get_database_path (config),
499 NOTMUCH_DATABASE_MODE_READ_ONLY);
503 query_str = query_string_from_args (notmuch, argc, argv);
504 if (query_str == NULL) {
505 fprintf (stderr, "Out of memory.\n");
508 if (*query_str == '\0') {
509 fprintf (stderr, "Error: notmuch search requires at least one search term.\n");
513 query = notmuch_query_create (notmuch, query_str);
515 fprintf (stderr, "Out of memory\n");
519 notmuch_query_set_sort (query, sort);
525 ret = do_search_threads (format, query, sort, output, offset, limit);
527 case OUTPUT_MESSAGES:
529 ret = do_search_messages (format, query, output, offset, limit);
532 ret = do_search_tags (notmuch, format, query);
536 notmuch_query_destroy (query);
537 notmuch_database_close (notmuch);