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 format_thread_text (const void *ctx,
116 const char *thread_id,
123 printf ("thread:%s %12s [%d/%d] %s; %s",
125 notmuch_time_relative_date (ctx, date),
133 format_item_id_json (const void *ctx,
134 unused (const char *item_type),
137 void *ctx_quote = talloc_new (ctx);
139 printf ("%s", json_quote_str (ctx_quote, item_id));
141 talloc_free (ctx_quote);
146 format_thread_json (const void *ctx,
147 const char *thread_id,
154 void *ctx_quote = talloc_new (ctx);
156 printf ("\"thread\": %s,\n"
157 "\"timestamp\": %ld,\n"
161 "\"subject\": %s,\n",
162 json_quote_str (ctx_quote, thread_id),
166 json_quote_str (ctx_quote, authors),
167 json_quote_str (ctx_quote, subject));
169 talloc_free (ctx_quote);
173 do_search_threads (const search_format_t *format,
174 notmuch_query_t *query,
178 notmuch_thread_t *thread;
179 notmuch_threads_t *threads;
180 notmuch_tags_t *tags;
182 int first_thread = 1;
184 threads = notmuch_query_search_threads (query);
188 fputs (format->results_start, stdout);
191 notmuch_threads_valid (threads);
192 notmuch_threads_move_to_next (threads))
197 fputs (format->item_sep, stdout);
199 thread = notmuch_threads_get (threads);
201 if (output == OUTPUT_THREADS) {
202 format->item_id (thread, "thread:",
203 notmuch_thread_get_thread_id (thread));
204 } else { /* output == OUTPUT_SUMMARY */
205 fputs (format->item_start, stdout);
207 if (sort == NOTMUCH_SORT_OLDEST_FIRST)
208 date = notmuch_thread_get_oldest_date (thread);
210 date = notmuch_thread_get_newest_date (thread);
212 format->thread_summary (thread,
213 notmuch_thread_get_thread_id (thread),
215 notmuch_thread_get_matched_messages (thread),
216 notmuch_thread_get_total_messages (thread),
217 notmuch_thread_get_authors (thread),
218 notmuch_thread_get_subject (thread));
220 fputs (format->tag_start, stdout);
222 for (tags = notmuch_thread_get_tags (thread);
223 notmuch_tags_valid (tags);
224 notmuch_tags_move_to_next (tags))
227 fputs (format->tag_sep, stdout);
228 printf (format->tag, notmuch_tags_get (tags));
232 fputs (format->tag_end, stdout);
234 fputs (format->item_end, stdout);
239 notmuch_thread_destroy (thread);
243 fputs (format->results_null, stdout);
245 fputs (format->results_end, stdout);
251 do_search_messages (const search_format_t *format,
252 notmuch_query_t *query,
255 notmuch_message_t *message;
256 notmuch_messages_t *messages;
257 int first_message = 1;
259 messages = notmuch_query_search_messages (query);
260 if (messages == NULL)
263 fputs (format->results_start, stdout);
266 notmuch_messages_valid (messages);
267 notmuch_messages_move_to_next (messages))
269 message = notmuch_messages_get (messages);
272 fputs (format->item_sep, stdout);
274 if (output == OUTPUT_FILES) {
275 format->item_id (message, "",
276 notmuch_message_get_filename (message));
277 } else { /* output == OUTPUT_MESSAGES */
278 format->item_id (message, "id:",
279 notmuch_message_get_message_id (message));
284 notmuch_message_destroy (message);
287 notmuch_messages_destroy (messages);
290 fputs (format->results_null, stdout);
292 fputs (format->results_end, stdout);
298 do_search_tags (notmuch_database_t *notmuch,
299 const search_format_t *format,
300 notmuch_query_t *query)
302 notmuch_messages_t *messages = NULL;
303 notmuch_tags_t *tags;
307 /* Special-case query of "*" for better performance. */
308 if (strcmp (notmuch_query_get_query_string (query), "*") == 0) {
309 tags = notmuch_database_get_all_tags (notmuch);
311 messages = notmuch_query_search_messages (query);
312 if (messages == NULL)
315 tags = notmuch_messages_collect_tags (messages);
320 fputs (format->results_start, stdout);
323 notmuch_tags_valid (tags);
324 notmuch_tags_move_to_next (tags))
326 tag = notmuch_tags_get (tags);
329 fputs (format->item_sep, stdout);
331 format->item_id (tags, "", tag);
336 notmuch_tags_destroy (tags);
339 notmuch_messages_destroy (messages);
342 fputs (format->results_null, stdout);
344 fputs (format->results_end, stdout);
350 notmuch_search_command (void *ctx, int argc, char *argv[])
352 notmuch_config_t *config;
353 notmuch_database_t *notmuch;
354 notmuch_query_t *query;
357 notmuch_sort_t sort = NOTMUCH_SORT_NEWEST_FIRST;
358 const search_format_t *format = &format_text;
360 output_t output = OUTPUT_SUMMARY;
362 for (i = 0; i < argc && argv[i][0] == '-'; i++) {
363 if (strcmp (argv[i], "--") == 0) {
367 if (STRNCMP_LITERAL (argv[i], "--sort=") == 0) {
368 opt = argv[i] + sizeof ("--sort=") - 1;
369 if (strcmp (opt, "oldest-first") == 0) {
370 sort = NOTMUCH_SORT_OLDEST_FIRST;
371 } else if (strcmp (opt, "newest-first") == 0) {
372 sort = NOTMUCH_SORT_NEWEST_FIRST;
374 fprintf (stderr, "Invalid value for --sort: %s\n", opt);
377 } else if (STRNCMP_LITERAL (argv[i], "--format=") == 0) {
378 opt = argv[i] + sizeof ("--format=") - 1;
379 if (strcmp (opt, "text") == 0) {
380 format = &format_text;
381 } else if (strcmp (opt, "json") == 0) {
382 format = &format_json;
384 fprintf (stderr, "Invalid value for --format: %s\n", opt);
387 } else if (STRNCMP_LITERAL (argv[i], "--output=") == 0) {
388 opt = argv[i] + sizeof ("--output=") - 1;
389 if (strcmp (opt, "summary") == 0) {
390 output = OUTPUT_SUMMARY;
391 } else if (strcmp (opt, "threads") == 0) {
392 output = OUTPUT_THREADS;
393 } else if (strcmp (opt, "messages") == 0) {
394 output = OUTPUT_MESSAGES;
395 } else if (strcmp (opt, "files") == 0) {
396 output = OUTPUT_FILES;
397 } else if (strcmp (opt, "tags") == 0) {
398 output = OUTPUT_TAGS;
400 fprintf (stderr, "Invalid value for --output: %s\n", opt);
404 fprintf (stderr, "Unrecognized option: %s\n", argv[i]);
412 config = notmuch_config_open (ctx, NULL, NULL);
416 notmuch = notmuch_database_open (notmuch_config_get_database_path (config),
417 NOTMUCH_DATABASE_MODE_READ_ONLY);
421 query_str = query_string_from_args (notmuch, argc, argv);
422 if (query_str == NULL) {
423 fprintf (stderr, "Out of memory.\n");
426 if (*query_str == '\0') {
427 fprintf (stderr, "Error: notmuch search requires at least one search term.\n");
431 query = notmuch_query_create (notmuch, query_str);
433 fprintf (stderr, "Out of memory\n");
437 notmuch_query_set_sort (query, sort);
443 ret = do_search_threads (format, query, sort, output);
445 case OUTPUT_MESSAGES:
447 ret = do_search_messages (format, query, output);
450 ret = do_search_tags (notmuch, format, query);
454 notmuch_query_destroy (query);
455 notmuch_database_close (notmuch);