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"
23 /* If the user asks for a *lot* of threads, lets give some results as
24 * quickly as possible and let the user read those while we compute
26 #define NOTMUCH_SHOW_INITIAL_BURST 100
29 do_search_threads (const void *ctx,
30 notmuch_query_t *query,
32 int first, int max_threads)
34 notmuch_thread_t *thread;
35 notmuch_threads_t *threads;
38 const char *relative_date;
40 for (threads = notmuch_query_search_threads (query, first, max_threads);
41 notmuch_threads_has_more (threads);
42 notmuch_threads_advance (threads))
46 thread = notmuch_threads_get (threads);
48 if (sort == NOTMUCH_SORT_OLDEST_FIRST)
49 date = notmuch_thread_get_oldest_date (thread);
51 date = notmuch_thread_get_newest_date (thread);
53 relative_date = notmuch_time_relative_date (ctx, date);
55 printf ("thread:%s %12s [%d/%d] %s; %s",
56 notmuch_thread_get_thread_id (thread),
58 notmuch_thread_get_matched_messages (thread),
59 notmuch_thread_get_total_messages (thread),
60 notmuch_thread_get_authors (thread),
61 notmuch_thread_get_subject (thread));
64 for (tags = notmuch_thread_get_tags (thread);
65 notmuch_tags_has_more (tags);
66 notmuch_tags_advance (tags))
70 printf ("%s", notmuch_tags_get (tags));
75 notmuch_thread_destroy (thread);
80 notmuch_search_command (void *ctx, int argc, char *argv[])
82 notmuch_config_t *config;
83 notmuch_database_t *notmuch;
84 notmuch_query_t *query;
86 int i, first = 0, max_threads = -1;
88 notmuch_sort_t sort = NOTMUCH_SORT_NEWEST_FIRST;
90 for (i = 0; i < argc && argv[i][0] == '-'; i++) {
91 if (strcmp (argv[i], "--") == 0) {
95 if (STRNCMP_LITERAL (argv[i], "--first=") == 0) {
96 opt = argv[i] + sizeof ("--first=") - 1;
97 first = strtoul (opt, &end, 10);
98 if (*opt == '\0' || *end != '\0') {
99 fprintf (stderr, "Invalid value for --first: %s\n", opt);
102 } else if (STRNCMP_LITERAL (argv[i], "--max-threads=") == 0) {
103 opt = argv[i] + sizeof ("--max-threads=") - 1;
104 max_threads = strtoul (opt, &end, 10);
105 if (*opt == '\0' || *end != '\0') {
106 fprintf (stderr, "Invalid value for --max-threads: %s\n", opt);
109 } else if (STRNCMP_LITERAL (argv[i], "--sort=") == 0) {
110 opt = argv[i] + sizeof ("--sort=") - 1;
111 if (strcmp (opt, "oldest-first") == 0) {
112 sort = NOTMUCH_SORT_OLDEST_FIRST;
113 } else if (strcmp (opt, "newest-first") == 0) {
114 sort = NOTMUCH_SORT_NEWEST_FIRST;
116 fprintf (stderr, "Invalid value for --sort: %s\n", opt);
120 fprintf (stderr, "Unrecognized option: %s\n", argv[i]);
128 config = notmuch_config_open (ctx, NULL, NULL);
132 notmuch = notmuch_database_open (notmuch_config_get_database_path (config));
136 query_str = query_string_from_args (ctx, argc, argv);
137 if (query_str == NULL) {
138 fprintf (stderr, "Out of memory.\n");
141 if (*query_str == '\0') {
142 fprintf (stderr, "Error: notmuch search requires at least one search term.\n");
146 query = notmuch_query_create (notmuch, query_str);
148 fprintf (stderr, "Out of memory\n");
152 notmuch_query_set_sort (query, sort);
154 if (max_threads < 0 || max_threads > NOTMUCH_SHOW_INITIAL_BURST)
156 do_search_threads (ctx, query, sort,
157 first, NOTMUCH_SHOW_INITIAL_BURST);
159 first += NOTMUCH_SHOW_INITIAL_BURST;
161 max_threads -= NOTMUCH_SHOW_INITIAL_BURST;
164 do_search_threads (ctx, query, sort, first, max_threads);
166 notmuch_query_destroy (query);
167 notmuch_database_close (notmuch);