]> git.notmuchmail.org Git - notmuch/blob - notmuch-search.c
notmuch search: Add a new --output=(summary|threads|messages|tags) option.
[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_TAGS
28 } output_t;
29
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,
35                      const char *item_id);
36     void (*thread_summary) (const void *ctx,
37                             const char *thread_id,
38                             const time_t date,
39                             const int matched,
40                             const int total,
41                             const char *authors,
42                             const char *subject);
43     const char *tag_start;
44     const char *tag;
45     const char *tag_sep;
46     const char *tag_end;
47     const char *item_sep;
48     const char *item_end;
49     const char *results_end;
50 } search_format_t;
51
52 static void
53 format_item_id_text (const void *ctx,
54                      const char *item_type,
55                      const char *item_id);
56
57 static void
58 format_thread_text (const void *ctx,
59                     const char *thread_id,
60                     const time_t date,
61                     const int matched,
62                     const int total,
63                     const char *authors,
64                     const char *subject);
65 static const search_format_t format_text = {
66     "",
67         "",
68             format_item_id_text,
69             format_thread_text,
70             " (",
71                 "%s", " ",
72             ")", "\n",
73         "",
74     "\n",
75 };
76
77 static void
78 format_item_id_json (const void *ctx,
79                      const char *item_type,
80                      const char *item_id);
81
82 static void
83 format_thread_json (const void *ctx,
84                     const char *thread_id,
85                     const time_t date,
86                     const int matched,
87                     const int total,
88                     const char *authors,
89                     const char *subject);
90 static const search_format_t format_json = {
91     "[",
92         "{",
93             format_item_id_json,
94             format_thread_json,
95             "\"tags\": [",
96                 "\"%s\"", ", ",
97             "]", ",\n",
98         "}",
99     "]\n",
100 };
101
102 static void
103 format_item_id_text (unused (const void *ctx),
104                      const char *item_type,
105                      const char *item_id)
106 {
107     printf ("%s%s", item_type, item_id);
108 }
109
110 static void
111 format_thread_text (const void *ctx,
112                     const char *thread_id,
113                     const time_t date,
114                     const int matched,
115                     const int total,
116                     const char *authors,
117                     const char *subject)
118 {
119     printf ("thread:%s %12s [%d/%d] %s; %s",
120             thread_id,
121             notmuch_time_relative_date (ctx, date),
122             matched,
123             total,
124             authors,
125             subject);
126 }
127
128 static void
129 format_item_id_json (const void *ctx,
130                      unused (const char *item_type),
131                      const char *item_id)
132 {
133     void *ctx_quote = talloc_new (ctx);
134
135     printf ("%s", json_quote_str (ctx_quote, item_id));
136
137     talloc_free (ctx_quote);
138     
139 }
140
141 static void
142 format_thread_json (const void *ctx,
143                     const char *thread_id,
144                     const time_t date,
145                     const int matched,
146                     const int total,
147                     const char *authors,
148                     const char *subject)
149 {
150     void *ctx_quote = talloc_new (ctx);
151
152     printf ("\"thread\": %s,\n"
153             "\"timestamp\": %ld,\n"
154             "\"matched\": %d,\n"
155             "\"total\": %d,\n"
156             "\"authors\": %s,\n"
157             "\"subject\": %s,\n",
158             json_quote_str (ctx_quote, thread_id),
159             date,
160             matched,
161             total,
162             json_quote_str (ctx_quote, authors),
163             json_quote_str (ctx_quote, subject));
164
165     talloc_free (ctx_quote);
166 }
167
168 static int
169 do_search_threads (const void *ctx,
170                    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->item_sep, stdout);
193
194         thread = notmuch_threads_get (threads);
195
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);
201
202             if (sort == NOTMUCH_SORT_OLDEST_FIRST)
203                 date = notmuch_thread_get_oldest_date (thread);
204             else
205                 date = notmuch_thread_get_newest_date (thread);
206
207             format->thread_summary (ctx,
208                                     notmuch_thread_get_thread_id (thread),
209                                     date,
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));
214
215             fputs (format->tag_start, stdout);
216
217             for (tags = notmuch_thread_get_tags (thread);
218                  notmuch_tags_valid (tags);
219                  notmuch_tags_move_to_next (tags))
220             {
221                 if (! first_tag)
222                     fputs (format->tag_sep, stdout);
223                 printf (format->tag, notmuch_tags_get (tags));
224                 first_tag = 0;
225             }
226
227             fputs (format->tag_end, stdout);
228
229             fputs (format->item_end, stdout);
230         }
231
232         first_thread = 0;
233
234         notmuch_thread_destroy (thread);
235     }
236
237     return 0;
238 }
239
240 static int
241 do_search_messages (const void *ctx,
242                     const search_format_t *format,
243                     notmuch_query_t *query)
244 {
245     notmuch_message_t *message;
246     notmuch_messages_t *messages;
247     int first_message = 1;
248
249     messages = notmuch_query_search_messages (query);
250     if (messages == NULL)
251         return 1;
252
253     for (;
254          notmuch_messages_valid (messages);
255          notmuch_messages_move_to_next (messages))
256     {
257         message = notmuch_messages_get (messages);
258
259         if (! first_message)
260             fputs (format->item_sep, stdout);
261
262         format->item_id (ctx, "id:", notmuch_message_get_message_id (message));
263
264         first_message = 0;
265
266         notmuch_message_destroy (message);
267     }
268
269     notmuch_messages_destroy (messages);
270
271     return 0;
272 }
273
274 static int
275 do_search_tags (const void *ctx,
276                 notmuch_database_t *notmuch,
277                 const search_format_t *format,
278                 notmuch_query_t *query)
279 {
280     notmuch_messages_t *messages = NULL;
281     notmuch_tags_t *tags;
282     const char *tag;
283     int first_tag = 1;
284
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);
288     } else {
289         messages = notmuch_query_search_messages (query);
290         if (messages == NULL)
291             return 1;
292
293         tags = notmuch_messages_collect_tags (messages);
294     }
295     if (tags == NULL)
296         return 1;
297
298     for (;
299          notmuch_tags_valid (tags);
300          notmuch_tags_move_to_next (tags))
301     {
302         tag = notmuch_tags_get (tags);
303
304         if (! first_tag)
305             fputs (format->item_sep, stdout);
306
307         format->item_id (ctx, "", tag);
308
309         first_tag = 0;
310     }
311
312     notmuch_tags_destroy (tags);
313
314     if (messages)
315         notmuch_messages_destroy (messages);
316
317     return 0;
318 }
319
320 int
321 notmuch_search_command (void *ctx, int argc, char *argv[])
322 {
323     notmuch_config_t *config;
324     notmuch_database_t *notmuch;
325     notmuch_query_t *query;
326     char *query_str;
327     char *opt;
328     notmuch_sort_t sort = NOTMUCH_SORT_NEWEST_FIRST;
329     const search_format_t *format = &format_text;
330     int i, ret;
331     output_t output = OUTPUT_SUMMARY;
332
333     for (i = 0; i < argc && argv[i][0] == '-'; i++) {
334         if (strcmp (argv[i], "--") == 0) {
335             i++;
336             break;
337         }
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;
344             } else {
345                 fprintf (stderr, "Invalid value for --sort: %s\n", opt);
346                 return 1;
347             }
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;
354             } else {
355                 fprintf (stderr, "Invalid value for --format: %s\n", opt);
356                 return 1;
357             }
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;
368             } else {
369                 fprintf (stderr, "Invalid value for --output: %s\n", opt);
370                 return 1;
371             }
372         } else {
373             fprintf (stderr, "Unrecognized option: %s\n", argv[i]);
374             return 1;
375         }
376     }
377
378     argc -= i;
379     argv += i;
380
381     config = notmuch_config_open (ctx, NULL, NULL);
382     if (config == NULL)
383         return 1;
384
385     notmuch = notmuch_database_open (notmuch_config_get_database_path (config),
386                                      NOTMUCH_DATABASE_MODE_READ_ONLY);
387     if (notmuch == NULL)
388         return 1;
389
390     query_str = query_string_from_args (ctx, argc, argv);
391     if (query_str == NULL) {
392         fprintf (stderr, "Out of memory.\n");
393         return 1;
394     }
395     if (*query_str == '\0') {
396         fprintf (stderr, "Error: notmuch search requires at least one search term.\n");
397         return 1;
398     }
399
400     query = notmuch_query_create (notmuch, query_str);
401     if (query == NULL) {
402         fprintf (stderr, "Out of memory\n");
403         return 1;
404     }
405
406     notmuch_query_set_sort (query, sort);
407
408     fputs (format->results_start, stdout);
409
410     switch (output) {
411     default:
412     case OUTPUT_SUMMARY:
413     case OUTPUT_THREADS:
414         ret = do_search_threads (ctx, format, query, sort, output);
415         break;
416     case OUTPUT_MESSAGES:
417         ret = do_search_messages (ctx, format, query);
418         break;
419     case OUTPUT_TAGS:
420         ret = do_search_tags (ctx, notmuch, format, query);
421         break;
422     }
423
424     fputs (format->results_end, stdout);
425
426     notmuch_query_destroy (query);
427     notmuch_database_close (notmuch);
428
429     return ret;
430 }