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;
54 format_item_id_text (const void *ctx,
55 const char *item_type,
59 format_thread_text (const void *ctx,
60 const char *thread_id,
66 static const search_format_t format_text = {
79 format_item_id_json (const void *ctx,
80 const char *item_type,
84 format_thread_json (const void *ctx,
85 const char *thread_id,
91 static const search_format_t format_json = {
104 format_item_id_text (unused (const void *ctx),
105 const char *item_type,
108 printf ("%s%s", item_type, item_id);
112 format_thread_text (const void *ctx,
113 const char *thread_id,
120 printf ("thread:%s %12s [%d/%d] %s; %s",
122 notmuch_time_relative_date (ctx, date),
130 format_item_id_json (const void *ctx,
131 unused (const char *item_type),
134 void *ctx_quote = talloc_new (ctx);
136 printf ("%s", json_quote_str (ctx_quote, item_id));
138 talloc_free (ctx_quote);
143 format_thread_json (const void *ctx,
144 const char *thread_id,
151 void *ctx_quote = talloc_new (ctx);
153 printf ("\"thread\": %s,\n"
154 "\"timestamp\": %ld,\n"
158 "\"subject\": %s,\n",
159 json_quote_str (ctx_quote, thread_id),
163 json_quote_str (ctx_quote, authors),
164 json_quote_str (ctx_quote, subject));
166 talloc_free (ctx_quote);
170 do_search_threads (const void *ctx,
171 const search_format_t *format,
172 notmuch_query_t *query,
176 notmuch_thread_t *thread;
177 notmuch_threads_t *threads;
178 notmuch_tags_t *tags;
180 int first_thread = 1;
182 threads = notmuch_query_search_threads (query);
187 notmuch_threads_valid (threads);
188 notmuch_threads_move_to_next (threads))
193 fputs (format->results_start, stdout);
195 fputs (format->item_sep, stdout);
197 thread = notmuch_threads_get (threads);
199 if (output == OUTPUT_THREADS) {
200 format->item_id (ctx, "thread:",
201 notmuch_thread_get_thread_id (thread));
202 } else { /* output == OUTPUT_SUMMARY */
203 fputs (format->item_start, stdout);
205 if (sort == NOTMUCH_SORT_OLDEST_FIRST)
206 date = notmuch_thread_get_oldest_date (thread);
208 date = notmuch_thread_get_newest_date (thread);
210 format->thread_summary (ctx,
211 notmuch_thread_get_thread_id (thread),
213 notmuch_thread_get_matched_messages (thread),
214 notmuch_thread_get_total_messages (thread),
215 notmuch_thread_get_authors (thread),
216 notmuch_thread_get_subject (thread));
218 fputs (format->tag_start, stdout);
220 for (tags = notmuch_thread_get_tags (thread);
221 notmuch_tags_valid (tags);
222 notmuch_tags_move_to_next (tags))
225 fputs (format->tag_sep, stdout);
226 printf (format->tag, notmuch_tags_get (tags));
230 fputs (format->tag_end, stdout);
232 fputs (format->item_end, stdout);
237 notmuch_thread_destroy (thread);
241 fputs (format->results_end, stdout);
247 do_search_messages (const void *ctx,
248 const search_format_t *format,
249 notmuch_query_t *query,
252 notmuch_message_t *message;
253 notmuch_messages_t *messages;
254 int first_message = 1;
256 messages = notmuch_query_search_messages (query);
257 if (messages == NULL)
261 notmuch_messages_valid (messages);
262 notmuch_messages_move_to_next (messages))
264 message = notmuch_messages_get (messages);
267 fputs (format->results_start, stdout);
269 fputs (format->item_sep, stdout);
271 if (output == OUTPUT_FILES) {
272 format->item_id (ctx, "",
273 notmuch_message_get_filename (message));
274 } else { /* output == OUTPUT_MESSAGES */
275 format->item_id (ctx, "id:",
276 notmuch_message_get_message_id (message));
281 notmuch_message_destroy (message);
284 notmuch_messages_destroy (messages);
287 fputs (format->results_end, stdout);
293 do_search_tags (const void *ctx,
294 notmuch_database_t *notmuch,
295 const search_format_t *format,
296 notmuch_query_t *query)
298 notmuch_messages_t *messages = NULL;
299 notmuch_tags_t *tags;
303 /* Special-case query of "*" for better performance. */
304 if (strcmp (notmuch_query_get_query_string (query), "*") == 0) {
305 tags = notmuch_database_get_all_tags (notmuch);
307 messages = notmuch_query_search_messages (query);
308 if (messages == NULL)
311 tags = notmuch_messages_collect_tags (messages);
317 notmuch_tags_valid (tags);
318 notmuch_tags_move_to_next (tags))
320 tag = notmuch_tags_get (tags);
323 fputs (format->results_start, stdout);
325 fputs (format->item_sep, stdout);
327 format->item_id (ctx, "", tag);
332 notmuch_tags_destroy (tags);
335 notmuch_messages_destroy (messages);
338 fputs (format->results_end, stdout);
344 notmuch_search_command (void *ctx, int argc, char *argv[])
346 notmuch_config_t *config;
347 notmuch_database_t *notmuch;
348 notmuch_query_t *query;
351 notmuch_sort_t sort = NOTMUCH_SORT_NEWEST_FIRST;
352 const search_format_t *format = &format_text;
354 output_t output = OUTPUT_SUMMARY;
356 for (i = 0; i < argc && argv[i][0] == '-'; i++) {
357 if (strcmp (argv[i], "--") == 0) {
361 if (STRNCMP_LITERAL (argv[i], "--sort=") == 0) {
362 opt = argv[i] + sizeof ("--sort=") - 1;
363 if (strcmp (opt, "oldest-first") == 0) {
364 sort = NOTMUCH_SORT_OLDEST_FIRST;
365 } else if (strcmp (opt, "newest-first") == 0) {
366 sort = NOTMUCH_SORT_NEWEST_FIRST;
368 fprintf (stderr, "Invalid value for --sort: %s\n", opt);
371 } else if (STRNCMP_LITERAL (argv[i], "--format=") == 0) {
372 opt = argv[i] + sizeof ("--format=") - 1;
373 if (strcmp (opt, "text") == 0) {
374 format = &format_text;
375 } else if (strcmp (opt, "json") == 0) {
376 format = &format_json;
378 fprintf (stderr, "Invalid value for --format: %s\n", opt);
381 } else if (STRNCMP_LITERAL (argv[i], "--output=") == 0) {
382 opt = argv[i] + sizeof ("--output=") - 1;
383 if (strcmp (opt, "summary") == 0) {
384 output = OUTPUT_SUMMARY;
385 } else if (strcmp (opt, "threads") == 0) {
386 output = OUTPUT_THREADS;
387 } else if (strcmp (opt, "messages") == 0) {
388 output = OUTPUT_MESSAGES;
389 } else if (strcmp (opt, "files") == 0) {
390 output = OUTPUT_FILES;
391 } else if (strcmp (opt, "tags") == 0) {
392 output = OUTPUT_TAGS;
394 fprintf (stderr, "Invalid value for --output: %s\n", opt);
398 fprintf (stderr, "Unrecognized option: %s\n", argv[i]);
406 config = notmuch_config_open (ctx, NULL, NULL);
410 notmuch = notmuch_database_open (notmuch_config_get_database_path (config),
411 NOTMUCH_DATABASE_MODE_READ_ONLY);
415 query_str = query_string_from_args (ctx, argc, argv);
416 if (query_str == NULL) {
417 fprintf (stderr, "Out of memory.\n");
420 if (*query_str == '\0') {
421 fprintf (stderr, "Error: notmuch search requires at least one search term.\n");
425 query = notmuch_query_create (notmuch, query_str);
427 fprintf (stderr, "Out of memory\n");
431 notmuch_query_set_sort (query, sort);
437 ret = do_search_threads (ctx, format, query, sort, output);
439 case OUTPUT_MESSAGES:
441 ret = do_search_messages (ctx, format, query, output);
444 ret = do_search_tags (ctx, notmuch, format, query);
448 notmuch_query_destroy (query);
449 notmuch_database_close (notmuch);