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 typedef struct search_format {
24 const char *results_start;
25 const char *thread_start;
26 void (*thread) (const void *ctx,
27 const char *thread_id,
33 const char *tag_start;
37 const char *thread_sep;
38 const char *thread_end;
39 const char *results_end;
43 format_thread_text (const void *ctx,
44 const char *thread_id,
50 static const search_format_t format_text = {
62 format_thread_json (const void *ctx,
63 const char *thread_id,
69 static const search_format_t format_json = {
81 format_thread_text (const void *ctx,
82 const char *thread_id,
89 printf ("thread:%s %12s [%d/%d] %s; %s",
91 notmuch_time_relative_date (ctx, date),
99 format_thread_json (const void *ctx,
100 const char *thread_id,
109 void *ctx_quote = talloc_new (ctx);
111 printf ("\"thread\": %s,\n"
112 "\"timestamp\": %ld,\n"
116 "\"subject\": %s,\n",
117 json_quote_str (ctx_quote, thread_id),
121 json_quote_str (ctx_quote, authors),
122 json_quote_str (ctx_quote, subject));
124 talloc_free (ctx_quote);
128 do_search_threads (const void *ctx,
129 const search_format_t *format,
130 notmuch_query_t *query,
133 notmuch_thread_t *thread;
134 notmuch_threads_t *threads;
135 notmuch_tags_t *tags;
137 int first_thread = 1;
139 fputs (format->results_start, stdout);
141 for (threads = notmuch_query_search_threads (query);
142 notmuch_threads_valid (threads);
143 notmuch_threads_move_to_next (threads))
148 fputs (format->thread_sep, stdout);
150 thread = notmuch_threads_get (threads);
152 if (sort == NOTMUCH_SORT_OLDEST_FIRST)
153 date = notmuch_thread_get_oldest_date (thread);
155 date = notmuch_thread_get_newest_date (thread);
157 fputs (format->thread_start, stdout);
160 notmuch_thread_get_thread_id (thread),
162 notmuch_thread_get_matched_messages (thread),
163 notmuch_thread_get_total_messages (thread),
164 notmuch_thread_get_authors (thread),
165 notmuch_thread_get_subject (thread));
167 fputs (format->tag_start, stdout);
169 for (tags = notmuch_thread_get_tags (thread);
170 notmuch_tags_valid (tags);
171 notmuch_tags_move_to_next (tags))
174 fputs (format->tag_sep, stdout);
175 printf (format->tag, notmuch_tags_get (tags));
179 fputs (format->tag_end, stdout);
180 fputs (format->thread_end, stdout);
184 notmuch_thread_destroy (thread);
187 fputs (format->results_end, stdout);
191 notmuch_search_command (void *ctx, int argc, char *argv[])
193 notmuch_config_t *config;
194 notmuch_database_t *notmuch;
195 notmuch_query_t *query;
198 notmuch_sort_t sort = NOTMUCH_SORT_NEWEST_FIRST;
199 const search_format_t *format = &format_text;
202 for (i = 0; i < argc && argv[i][0] == '-'; i++) {
203 if (strcmp (argv[i], "--") == 0) {
207 if (STRNCMP_LITERAL (argv[i], "--sort=") == 0) {
208 opt = argv[i] + sizeof ("--sort=") - 1;
209 if (strcmp (opt, "oldest-first") == 0) {
210 sort = NOTMUCH_SORT_OLDEST_FIRST;
211 } else if (strcmp (opt, "newest-first") == 0) {
212 sort = NOTMUCH_SORT_NEWEST_FIRST;
214 fprintf (stderr, "Invalid value for --sort: %s\n", opt);
217 } else if (STRNCMP_LITERAL (argv[i], "--format=") == 0) {
218 opt = argv[i] + sizeof ("--format=") - 1;
219 if (strcmp (opt, "text") == 0) {
220 format = &format_text;
221 } else if (strcmp (opt, "json") == 0) {
222 format = &format_json;
224 fprintf (stderr, "Invalid value for --format: %s\n", opt);
228 fprintf (stderr, "Unrecognized option: %s\n", argv[i]);
236 config = notmuch_config_open (ctx, NULL, NULL);
240 notmuch = notmuch_database_open (notmuch_config_get_database_path (config),
241 NOTMUCH_DATABASE_MODE_READ_ONLY);
245 query_str = query_string_from_args (ctx, argc, argv);
246 if (query_str == NULL) {
247 fprintf (stderr, "Out of memory.\n");
250 if (*query_str == '\0') {
251 fprintf (stderr, "Error: notmuch search requires at least one search term.\n");
255 query = notmuch_query_create (notmuch, query_str);
257 fprintf (stderr, "Out of memory\n");
261 notmuch_query_set_sort (query, sort);
263 do_search_threads (ctx, format, query, sort);
265 notmuch_query_destroy (query);
266 notmuch_database_close (notmuch);