]> git.notmuchmail.org Git - notmuch/blob - notmuch-search.c
emacs: send notmuch-query stderr to /dev/null
[notmuch] / notmuch-search.c
1 /* notmuch - Not much of an email program, (just index and search)
2  *
3  * Copyright © 2009 Carl Worth
4  *
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.
9  *
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.
14  *
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/ .
17  *
18  * Author: Carl Worth <cworth@cworth.org>
19  */
20
21 #include "notmuch-client.h"
22
23 typedef enum {
24     OUTPUT_SUMMARY,
25     OUTPUT_THREADS,
26     OUTPUT_MESSAGES,
27     OUTPUT_FILES,
28     OUTPUT_TAGS
29 } output_t;
30
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,
36                      const char *item_id);
37     void (*thread_summary) (const void *ctx,
38                             const char *thread_id,
39                             const time_t date,
40                             const int matched,
41                             const int total,
42                             const char *authors,
43                             const char *subject);
44     const char *tag_start;
45     const char *tag;
46     const char *tag_sep;
47     const char *tag_end;
48     const char *item_sep;
49     const char *item_end;
50     const char *results_end;
51 } search_format_t;
52
53 static void
54 format_item_id_text (const void *ctx,
55                      const char *item_type,
56                      const char *item_id);
57
58 static void
59 format_thread_text (const void *ctx,
60                     const char *thread_id,
61                     const time_t date,
62                     const int matched,
63                     const int total,
64                     const char *authors,
65                     const char *subject);
66 static const search_format_t format_text = {
67     "",
68         "",
69             format_item_id_text,
70             format_thread_text,
71             " (",
72                 "%s", " ",
73             ")", "\n",
74         "",
75     "\n",
76 };
77
78 static void
79 format_item_id_json (const void *ctx,
80                      const char *item_type,
81                      const char *item_id);
82
83 static void
84 format_thread_json (const void *ctx,
85                     const char *thread_id,
86                     const time_t date,
87                     const int matched,
88                     const int total,
89                     const char *authors,
90                     const char *subject);
91 static const search_format_t format_json = {
92     "[",
93         "{",
94             format_item_id_json,
95             format_thread_json,
96             "\"tags\": [",
97                 "\"%s\"", ", ",
98             "]", ",\n",
99         "}",
100     "]\n",
101 };
102
103 static void
104 format_item_id_text (unused (const void *ctx),
105                      const char *item_type,
106                      const char *item_id)
107 {
108     printf ("%s%s", item_type, item_id);
109 }
110
111 static void
112 format_thread_text (const void *ctx,
113                     const char *thread_id,
114                     const time_t date,
115                     const int matched,
116                     const int total,
117                     const char *authors,
118                     const char *subject)
119 {
120     printf ("thread:%s %12s [%d/%d] %s; %s",
121             thread_id,
122             notmuch_time_relative_date (ctx, date),
123             matched,
124             total,
125             authors,
126             subject);
127 }
128
129 static void
130 format_item_id_json (const void *ctx,
131                      unused (const char *item_type),
132                      const char *item_id)
133 {
134     void *ctx_quote = talloc_new (ctx);
135
136     printf ("%s", json_quote_str (ctx_quote, item_id));
137
138     talloc_free (ctx_quote);
139     
140 }
141
142 static void
143 format_thread_json (const void *ctx,
144                     const char *thread_id,
145                     const time_t date,
146                     const int matched,
147                     const int total,
148                     const char *authors,
149                     const char *subject)
150 {
151     void *ctx_quote = talloc_new (ctx);
152
153     printf ("\"thread\": %s,\n"
154             "\"timestamp\": %ld,\n"
155             "\"matched\": %d,\n"
156             "\"total\": %d,\n"
157             "\"authors\": %s,\n"
158             "\"subject\": %s,\n",
159             json_quote_str (ctx_quote, thread_id),
160             date,
161             matched,
162             total,
163             json_quote_str (ctx_quote, authors),
164             json_quote_str (ctx_quote, subject));
165
166     talloc_free (ctx_quote);
167 }
168
169 static int
170 do_search_threads (const search_format_t *format,
171                    notmuch_query_t *query,
172                    notmuch_sort_t sort,
173                    output_t output)
174 {
175     notmuch_thread_t *thread;
176     notmuch_threads_t *threads;
177     notmuch_tags_t *tags;
178     time_t date;
179     int first_thread = 1;
180
181     threads = notmuch_query_search_threads (query);
182     if (threads == NULL)
183         return 1;
184
185     for (;
186          notmuch_threads_valid (threads);
187          notmuch_threads_move_to_next (threads))
188     {
189         int first_tag = 1;
190
191         if (first_thread)
192             fputs (format->results_start, stdout);
193         else
194             fputs (format->item_sep, stdout);
195
196         thread = notmuch_threads_get (threads);
197
198         if (output == OUTPUT_THREADS) {
199             format->item_id (thread, "thread:",
200                              notmuch_thread_get_thread_id (thread));
201         } else { /* output == OUTPUT_SUMMARY */
202             fputs (format->item_start, stdout);
203
204             if (sort == NOTMUCH_SORT_OLDEST_FIRST)
205                 date = notmuch_thread_get_oldest_date (thread);
206             else
207                 date = notmuch_thread_get_newest_date (thread);
208
209             format->thread_summary (thread,
210                                     notmuch_thread_get_thread_id (thread),
211                                     date,
212                                     notmuch_thread_get_matched_messages (thread),
213                                     notmuch_thread_get_total_messages (thread),
214                                     notmuch_thread_get_authors (thread),
215                                     notmuch_thread_get_subject (thread));
216
217             fputs (format->tag_start, stdout);
218
219             for (tags = notmuch_thread_get_tags (thread);
220                  notmuch_tags_valid (tags);
221                  notmuch_tags_move_to_next (tags))
222             {
223                 if (! first_tag)
224                     fputs (format->tag_sep, stdout);
225                 printf (format->tag, notmuch_tags_get (tags));
226                 first_tag = 0;
227             }
228
229             fputs (format->tag_end, stdout);
230
231             fputs (format->item_end, stdout);
232         }
233
234         first_thread = 0;
235
236         notmuch_thread_destroy (thread);
237     }
238
239     if (! first_thread)
240         fputs (format->results_end, stdout);
241
242     return 0;
243 }
244
245 static int
246 do_search_messages (const search_format_t *format,
247                     notmuch_query_t *query,
248                     output_t output)
249 {
250     notmuch_message_t *message;
251     notmuch_messages_t *messages;
252     int first_message = 1;
253
254     messages = notmuch_query_search_messages (query);
255     if (messages == NULL)
256         return 1;
257
258     for (;
259          notmuch_messages_valid (messages);
260          notmuch_messages_move_to_next (messages))
261     {
262         message = notmuch_messages_get (messages);
263
264         if (first_message)
265             fputs (format->results_start, stdout);
266         else
267             fputs (format->item_sep, stdout);
268
269         if (output == OUTPUT_FILES) {
270             format->item_id (message, "",
271                              notmuch_message_get_filename (message));
272         } else { /* output == OUTPUT_MESSAGES */
273             format->item_id (message, "id:",
274                              notmuch_message_get_message_id (message));
275         }
276
277         first_message = 0;
278
279         notmuch_message_destroy (message);
280     }
281
282     notmuch_messages_destroy (messages);
283
284     if (! first_message)
285         fputs (format->results_end, stdout);
286
287     return 0;
288 }
289
290 static int
291 do_search_tags (notmuch_database_t *notmuch,
292                 const search_format_t *format,
293                 notmuch_query_t *query)
294 {
295     notmuch_messages_t *messages = NULL;
296     notmuch_tags_t *tags;
297     const char *tag;
298     int first_tag = 1;
299
300     /* Special-case query of "*" for better performance. */
301     if (strcmp (notmuch_query_get_query_string (query), "*") == 0) {
302         tags = notmuch_database_get_all_tags (notmuch);
303     } else {
304         messages = notmuch_query_search_messages (query);
305         if (messages == NULL)
306             return 1;
307
308         tags = notmuch_messages_collect_tags (messages);
309     }
310     if (tags == NULL)
311         return 1;
312
313     for (;
314          notmuch_tags_valid (tags);
315          notmuch_tags_move_to_next (tags))
316     {
317         tag = notmuch_tags_get (tags);
318
319         if (first_tag)
320             fputs (format->results_start, stdout);
321         else
322             fputs (format->item_sep, stdout);
323
324         format->item_id (tags, "", tag);
325
326         first_tag = 0;
327     }
328
329     notmuch_tags_destroy (tags);
330
331     if (messages)
332         notmuch_messages_destroy (messages);
333
334     if (! first_tag)
335         fputs (format->results_end, stdout);
336
337     return 0;
338 }
339
340 int
341 notmuch_search_command (void *ctx, int argc, char *argv[])
342 {
343     notmuch_config_t *config;
344     notmuch_database_t *notmuch;
345     notmuch_query_t *query;
346     char *query_str;
347     char *opt;
348     notmuch_sort_t sort = NOTMUCH_SORT_NEWEST_FIRST;
349     const search_format_t *format = &format_text;
350     int i, ret;
351     output_t output = OUTPUT_SUMMARY;
352
353     for (i = 0; i < argc && argv[i][0] == '-'; i++) {
354         if (strcmp (argv[i], "--") == 0) {
355             i++;
356             break;
357         }
358         if (STRNCMP_LITERAL (argv[i], "--sort=") == 0) {
359             opt = argv[i] + sizeof ("--sort=") - 1;
360             if (strcmp (opt, "oldest-first") == 0) {
361                 sort = NOTMUCH_SORT_OLDEST_FIRST;
362             } else if (strcmp (opt, "newest-first") == 0) {
363                 sort = NOTMUCH_SORT_NEWEST_FIRST;
364             } else {
365                 fprintf (stderr, "Invalid value for --sort: %s\n", opt);
366                 return 1;
367             }
368         } else if (STRNCMP_LITERAL (argv[i], "--format=") == 0) {
369             opt = argv[i] + sizeof ("--format=") - 1;
370             if (strcmp (opt, "text") == 0) {
371                 format = &format_text;
372             } else if (strcmp (opt, "json") == 0) {
373                 format = &format_json;
374             } else {
375                 fprintf (stderr, "Invalid value for --format: %s\n", opt);
376                 return 1;
377             }
378         } else if (STRNCMP_LITERAL (argv[i], "--output=") == 0) {
379             opt = argv[i] + sizeof ("--output=") - 1;
380             if (strcmp (opt, "summary") == 0) {
381                 output = OUTPUT_SUMMARY;
382             } else if (strcmp (opt, "threads") == 0) {
383                 output = OUTPUT_THREADS;
384             } else if (strcmp (opt, "messages") == 0) {
385                 output = OUTPUT_MESSAGES;
386             } else if (strcmp (opt, "files") == 0) {
387                 output = OUTPUT_FILES;
388             } else if (strcmp (opt, "tags") == 0) {
389                 output = OUTPUT_TAGS;
390             } else {
391                 fprintf (stderr, "Invalid value for --output: %s\n", opt);
392                 return 1;
393             }
394         } else {
395             fprintf (stderr, "Unrecognized option: %s\n", argv[i]);
396             return 1;
397         }
398     }
399
400     argc -= i;
401     argv += i;
402
403     config = notmuch_config_open (ctx, NULL, NULL);
404     if (config == NULL)
405         return 1;
406
407     notmuch = notmuch_database_open (notmuch_config_get_database_path (config),
408                                      NOTMUCH_DATABASE_MODE_READ_ONLY);
409     if (notmuch == NULL)
410         return 1;
411
412     query_str = query_string_from_args (notmuch, argc, argv);
413     if (query_str == NULL) {
414         fprintf (stderr, "Out of memory.\n");
415         return 1;
416     }
417     if (*query_str == '\0') {
418         fprintf (stderr, "Error: notmuch search requires at least one search term.\n");
419         return 1;
420     }
421
422     query = notmuch_query_create (notmuch, query_str);
423     if (query == NULL) {
424         fprintf (stderr, "Out of memory\n");
425         return 1;
426     }
427
428     notmuch_query_set_sort (query, sort);
429
430     switch (output) {
431     default:
432     case OUTPUT_SUMMARY:
433     case OUTPUT_THREADS:
434         ret = do_search_threads (format, query, sort, output);
435         break;
436     case OUTPUT_MESSAGES:
437     case OUTPUT_FILES:
438         ret = do_search_messages (format, query, output);
439         break;
440     case OUTPUT_TAGS:
441         ret = do_search_tags (notmuch, format, query);
442         break;
443     }
444
445     notmuch_query_destroy (query);
446     notmuch_database_close (notmuch);
447
448     return ret;
449 }