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"
30 typedef struct search_format {
31 const char *results_start;
32 const char *item_start;
33 void (*item_id) (const void *ctx,
34 const char *item_type,
36 void (*thread_summary) (const void *ctx,
37 const char *thread_id,
43 const char *tag_start;
49 const char *results_end;
53 format_item_id_text (const void *ctx,
54 const char *item_type,
58 format_thread_text (const void *ctx,
59 const char *thread_id,
65 static const search_format_t format_text = {
78 format_item_id_json (const void *ctx,
79 const char *item_type,
83 format_thread_json (const void *ctx,
84 const char *thread_id,
90 static const search_format_t format_json = {
103 format_item_id_text (unused (const void *ctx),
104 const char *item_type,
107 printf ("%s%s", item_type, item_id);
111 format_thread_text (const void *ctx,
112 const char *thread_id,
119 printf ("thread:%s %12s [%d/%d] %s; %s",
121 notmuch_time_relative_date (ctx, date),
129 format_item_id_json (const void *ctx,
130 unused (const char *item_type),
133 void *ctx_quote = talloc_new (ctx);
135 printf ("%s", json_quote_str (ctx_quote, item_id));
137 talloc_free (ctx_quote);
142 format_thread_json (const void *ctx,
143 const char *thread_id,
150 void *ctx_quote = talloc_new (ctx);
152 printf ("\"thread\": %s,\n"
153 "\"timestamp\": %ld,\n"
157 "\"subject\": %s,\n",
158 json_quote_str (ctx_quote, thread_id),
162 json_quote_str (ctx_quote, authors),
163 json_quote_str (ctx_quote, subject));
165 talloc_free (ctx_quote);
169 do_search_threads (const void *ctx,
170 const search_format_t *format,
171 notmuch_query_t *query,
175 notmuch_thread_t *thread;
176 notmuch_threads_t *threads;
177 notmuch_tags_t *tags;
179 int first_thread = 1;
181 threads = notmuch_query_search_threads (query);
186 notmuch_threads_valid (threads);
187 notmuch_threads_move_to_next (threads))
192 fputs (format->item_sep, stdout);
194 thread = notmuch_threads_get (threads);
196 if (output == OUTPUT_THREADS) {
197 format->item_id (ctx, "thread:",
198 notmuch_thread_get_thread_id (thread));
199 } else { /* output == OUTPUT_SUMMARY */
200 fputs (format->item_start, stdout);
202 if (sort == NOTMUCH_SORT_OLDEST_FIRST)
203 date = notmuch_thread_get_oldest_date (thread);
205 date = notmuch_thread_get_newest_date (thread);
207 format->thread_summary (ctx,
208 notmuch_thread_get_thread_id (thread),
210 notmuch_thread_get_matched_messages (thread),
211 notmuch_thread_get_total_messages (thread),
212 notmuch_thread_get_authors (thread),
213 notmuch_thread_get_subject (thread));
215 fputs (format->tag_start, stdout);
217 for (tags = notmuch_thread_get_tags (thread);
218 notmuch_tags_valid (tags);
219 notmuch_tags_move_to_next (tags))
222 fputs (format->tag_sep, stdout);
223 printf (format->tag, notmuch_tags_get (tags));
227 fputs (format->tag_end, stdout);
229 fputs (format->item_end, stdout);
234 notmuch_thread_destroy (thread);
241 do_search_messages (const void *ctx,
242 const search_format_t *format,
243 notmuch_query_t *query)
245 notmuch_message_t *message;
246 notmuch_messages_t *messages;
247 int first_message = 1;
249 messages = notmuch_query_search_messages (query);
250 if (messages == NULL)
254 notmuch_messages_valid (messages);
255 notmuch_messages_move_to_next (messages))
257 message = notmuch_messages_get (messages);
260 fputs (format->item_sep, stdout);
262 format->item_id (ctx, "id:", notmuch_message_get_message_id (message));
266 notmuch_message_destroy (message);
269 notmuch_messages_destroy (messages);
275 do_search_tags (const void *ctx,
276 notmuch_database_t *notmuch,
277 const search_format_t *format,
278 notmuch_query_t *query)
280 notmuch_messages_t *messages = NULL;
281 notmuch_tags_t *tags;
285 /* Special-case query of "*" for better performance. */
286 if (strcmp (notmuch_query_get_query_string (query), "*") == 0) {
287 tags = notmuch_database_get_all_tags (notmuch);
289 messages = notmuch_query_search_messages (query);
290 if (messages == NULL)
293 tags = notmuch_messages_collect_tags (messages);
299 notmuch_tags_valid (tags);
300 notmuch_tags_move_to_next (tags))
302 tag = notmuch_tags_get (tags);
305 fputs (format->item_sep, stdout);
307 format->item_id (ctx, "", tag);
312 notmuch_tags_destroy (tags);
315 notmuch_messages_destroy (messages);
321 notmuch_search_command (void *ctx, int argc, char *argv[])
323 notmuch_config_t *config;
324 notmuch_database_t *notmuch;
325 notmuch_query_t *query;
328 notmuch_sort_t sort = NOTMUCH_SORT_NEWEST_FIRST;
329 const search_format_t *format = &format_text;
331 output_t output = OUTPUT_SUMMARY;
333 for (i = 0; i < argc && argv[i][0] == '-'; i++) {
334 if (strcmp (argv[i], "--") == 0) {
338 if (STRNCMP_LITERAL (argv[i], "--sort=") == 0) {
339 opt = argv[i] + sizeof ("--sort=") - 1;
340 if (strcmp (opt, "oldest-first") == 0) {
341 sort = NOTMUCH_SORT_OLDEST_FIRST;
342 } else if (strcmp (opt, "newest-first") == 0) {
343 sort = NOTMUCH_SORT_NEWEST_FIRST;
345 fprintf (stderr, "Invalid value for --sort: %s\n", opt);
348 } else if (STRNCMP_LITERAL (argv[i], "--format=") == 0) {
349 opt = argv[i] + sizeof ("--format=") - 1;
350 if (strcmp (opt, "text") == 0) {
351 format = &format_text;
352 } else if (strcmp (opt, "json") == 0) {
353 format = &format_json;
355 fprintf (stderr, "Invalid value for --format: %s\n", opt);
358 } else if (STRNCMP_LITERAL (argv[i], "--output=") == 0) {
359 opt = argv[i] + sizeof ("--output=") - 1;
360 if (strcmp (opt, "summary") == 0) {
361 output = OUTPUT_SUMMARY;
362 } else if (strcmp (opt, "threads") == 0) {
363 output = OUTPUT_THREADS;
364 } else if (strcmp (opt, "messages") == 0) {
365 output = OUTPUT_MESSAGES;
366 } else if (strcmp (opt, "tags") == 0) {
367 output = OUTPUT_TAGS;
369 fprintf (stderr, "Invalid value for --output: %s\n", opt);
373 fprintf (stderr, "Unrecognized option: %s\n", argv[i]);
381 config = notmuch_config_open (ctx, NULL, NULL);
385 notmuch = notmuch_database_open (notmuch_config_get_database_path (config),
386 NOTMUCH_DATABASE_MODE_READ_ONLY);
390 query_str = query_string_from_args (ctx, argc, argv);
391 if (query_str == NULL) {
392 fprintf (stderr, "Out of memory.\n");
395 if (*query_str == '\0') {
396 fprintf (stderr, "Error: notmuch search requires at least one search term.\n");
400 query = notmuch_query_create (notmuch, query_str);
402 fprintf (stderr, "Out of memory\n");
406 notmuch_query_set_sort (query, sort);
408 fputs (format->results_start, stdout);
414 ret = do_search_threads (ctx, format, query, sort, output);
416 case OUTPUT_MESSAGES:
417 ret = do_search_messages (ctx, format, query);
420 ret = do_search_tags (ctx, notmuch, format, query);
424 fputs (format->results_end, stdout);
426 notmuch_query_destroy (query);
427 notmuch_database_close (notmuch);