]> git.notmuchmail.org Git - notmuch/blob - notmuch-search.c
cli: search: Refactor passing of command line options
[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 #include "sprinter.h"
23 #include "string-util.h"
24
25 typedef enum {
26     OUTPUT_SUMMARY,
27     OUTPUT_THREADS,
28     OUTPUT_MESSAGES,
29     OUTPUT_FILES,
30     OUTPUT_TAGS
31 } output_t;
32
33 typedef struct {
34     sprinter_t *format;
35     notmuch_query_t *query;
36     notmuch_sort_t sort;
37     output_t output;
38     int offset;
39     int limit;
40     int dupe;
41 } search_options_t;
42
43 /* Return two stable query strings that identify exactly the matched
44  * and unmatched messages currently in thread.  If there are no
45  * matched or unmatched messages, the returned buffers will be
46  * NULL. */
47 static int
48 get_thread_query (notmuch_thread_t *thread,
49                   char **matched_out, char **unmatched_out)
50 {
51     notmuch_messages_t *messages;
52     char *escaped = NULL;
53     size_t escaped_len = 0;
54
55     *matched_out = *unmatched_out = NULL;
56
57     for (messages = notmuch_thread_get_messages (thread);
58          notmuch_messages_valid (messages);
59          notmuch_messages_move_to_next (messages))
60     {
61         notmuch_message_t *message = notmuch_messages_get (messages);
62         const char *mid = notmuch_message_get_message_id (message);
63         /* Determine which query buffer to extend */
64         char **buf = notmuch_message_get_flag (
65             message, NOTMUCH_MESSAGE_FLAG_MATCH) ? matched_out : unmatched_out;
66         /* Add this message's id: query.  Since "id" is an exclusive
67          * prefix, it is implicitly 'or'd together, so we only need to
68          * join queries with a space. */
69         if (make_boolean_term (thread, "id", mid, &escaped, &escaped_len) < 0)
70             return -1;
71         if (*buf)
72             *buf = talloc_asprintf_append_buffer (*buf, " %s", escaped);
73         else
74             *buf = talloc_strdup (thread, escaped);
75         if (!*buf)
76             return -1;
77     }
78     talloc_free (escaped);
79     return 0;
80 }
81
82 static int
83 do_search_threads (search_options_t *opt)
84 {
85     notmuch_thread_t *thread;
86     notmuch_threads_t *threads;
87     notmuch_tags_t *tags;
88     sprinter_t *format = opt->format;
89     time_t date;
90     int i;
91
92     if (opt->offset < 0) {
93         opt->offset += notmuch_query_count_threads (opt->query);
94         if (opt->offset < 0)
95             opt->offset = 0;
96     }
97
98     threads = notmuch_query_search_threads (opt->query);
99     if (threads == NULL)
100         return 1;
101
102     format->begin_list (format);
103
104     for (i = 0;
105          notmuch_threads_valid (threads) && (opt->limit < 0 || i < opt->offset + opt->limit);
106          notmuch_threads_move_to_next (threads), i++)
107     {
108         thread = notmuch_threads_get (threads);
109
110         if (i < opt->offset) {
111             notmuch_thread_destroy (thread);
112             continue;
113         }
114
115         if (opt->output == OUTPUT_THREADS) {
116             format->set_prefix (format, "thread");
117             format->string (format,
118                             notmuch_thread_get_thread_id (thread));
119             format->separator (format);
120         } else { /* output == OUTPUT_SUMMARY */
121             void *ctx_quote = talloc_new (thread);
122             const char *authors = notmuch_thread_get_authors (thread);
123             const char *subject = notmuch_thread_get_subject (thread);
124             const char *thread_id = notmuch_thread_get_thread_id (thread);
125             int matched = notmuch_thread_get_matched_messages (thread);
126             int total = notmuch_thread_get_total_messages (thread);
127             const char *relative_date = NULL;
128             notmuch_bool_t first_tag = TRUE;
129
130             format->begin_map (format);
131
132             if (opt->sort == NOTMUCH_SORT_OLDEST_FIRST)
133                 date = notmuch_thread_get_oldest_date (thread);
134             else
135                 date = notmuch_thread_get_newest_date (thread);
136
137             relative_date = notmuch_time_relative_date (ctx_quote, date);
138
139             if (format->is_text_printer) {
140                 /* Special case for the text formatter */
141                 printf ("thread:%s %12s [%d/%d] %s; %s (",
142                         thread_id,
143                         relative_date,
144                         matched,
145                         total,
146                         sanitize_string (ctx_quote, authors),
147                         sanitize_string (ctx_quote, subject));
148             } else { /* Structured Output */
149                 format->map_key (format, "thread");
150                 format->string (format, thread_id);
151                 format->map_key (format, "timestamp");
152                 format->integer (format, date);
153                 format->map_key (format, "date_relative");
154                 format->string (format, relative_date);
155                 format->map_key (format, "matched");
156                 format->integer (format, matched);
157                 format->map_key (format, "total");
158                 format->integer (format, total);
159                 format->map_key (format, "authors");
160                 format->string (format, authors);
161                 format->map_key (format, "subject");
162                 format->string (format, subject);
163                 if (notmuch_format_version >= 2) {
164                     char *matched_query, *unmatched_query;
165                     if (get_thread_query (thread, &matched_query,
166                                           &unmatched_query) < 0) {
167                         fprintf (stderr, "Out of memory\n");
168                         return 1;
169                     }
170                     format->map_key (format, "query");
171                     format->begin_list (format);
172                     if (matched_query)
173                         format->string (format, matched_query);
174                     else
175                         format->null (format);
176                     if (unmatched_query)
177                         format->string (format, unmatched_query);
178                     else
179                         format->null (format);
180                     format->end (format);
181                 }
182             }
183
184             talloc_free (ctx_quote);
185
186             format->map_key (format, "tags");
187             format->begin_list (format);
188
189             for (tags = notmuch_thread_get_tags (thread);
190                  notmuch_tags_valid (tags);
191                  notmuch_tags_move_to_next (tags))
192             {
193                 const char *tag = notmuch_tags_get (tags);
194
195                 if (format->is_text_printer) {
196                   /* Special case for the text formatter */
197                     if (first_tag)
198                         first_tag = FALSE;
199                     else
200                         fputc (' ', stdout);
201                     fputs (tag, stdout);
202                 } else { /* Structured Output */
203                     format->string (format, tag);
204                 }
205             }
206
207             if (format->is_text_printer)
208                 printf (")");
209
210             format->end (format);
211             format->end (format);
212             format->separator (format);
213         }
214
215         notmuch_thread_destroy (thread);
216     }
217
218     format->end (format);
219
220     return 0;
221 }
222
223 static int
224 do_search_messages (search_options_t *opt)
225 {
226     notmuch_message_t *message;
227     notmuch_messages_t *messages;
228     notmuch_filenames_t *filenames;
229     sprinter_t *format = opt->format;
230     int i;
231
232     if (opt->offset < 0) {
233         opt->offset += notmuch_query_count_messages (opt->query);
234         if (opt->offset < 0)
235             opt->offset = 0;
236     }
237
238     messages = notmuch_query_search_messages (opt->query);
239     if (messages == NULL)
240         return 1;
241
242     format->begin_list (format);
243
244     for (i = 0;
245          notmuch_messages_valid (messages) && (opt->limit < 0 || i < opt->offset + opt->limit);
246          notmuch_messages_move_to_next (messages), i++)
247     {
248         if (i < opt->offset)
249             continue;
250
251         message = notmuch_messages_get (messages);
252
253         if (opt->output == OUTPUT_FILES) {
254             int j;
255             filenames = notmuch_message_get_filenames (message);
256
257             for (j = 1;
258                  notmuch_filenames_valid (filenames);
259                  notmuch_filenames_move_to_next (filenames), j++)
260             {
261                 if (opt->dupe < 0 || opt->dupe == j) {
262                     format->string (format, notmuch_filenames_get (filenames));
263                     format->separator (format);
264                 }
265             }
266             
267             notmuch_filenames_destroy( filenames );
268
269         } else { /* output == OUTPUT_MESSAGES */
270             format->set_prefix (format, "id");
271             format->string (format,
272                             notmuch_message_get_message_id (message));
273             format->separator (format);
274         }
275
276         notmuch_message_destroy (message);
277     }
278
279     notmuch_messages_destroy (messages);
280
281     format->end (format);
282
283     return 0;
284 }
285
286 static int
287 do_search_tags (notmuch_database_t *notmuch,
288                 const search_options_t *opt)
289 {
290     notmuch_messages_t *messages = NULL;
291     notmuch_tags_t *tags;
292     const char *tag;
293     sprinter_t *format = opt->format;
294     notmuch_query_t *query = opt->query;
295
296     /* should the following only special case if no excluded terms
297      * specified? */
298
299     /* Special-case query of "*" for better performance. */
300     if (strcmp (notmuch_query_get_query_string (query), "*") == 0) {
301         tags = notmuch_database_get_all_tags (notmuch);
302     } else {
303         messages = notmuch_query_search_messages (query);
304         if (messages == NULL)
305             return 1;
306
307         tags = notmuch_messages_collect_tags (messages);
308     }
309     if (tags == NULL)
310         return 1;
311
312     format->begin_list (format);
313
314     for (;
315          notmuch_tags_valid (tags);
316          notmuch_tags_move_to_next (tags))
317     {
318         tag = notmuch_tags_get (tags);
319
320         format->string (format, tag);
321         format->separator (format);
322
323     }
324
325     notmuch_tags_destroy (tags);
326
327     if (messages)
328         notmuch_messages_destroy (messages);
329
330     format->end (format);
331
332     return 0;
333 }
334
335 int
336 notmuch_search_command (notmuch_config_t *config, int argc, char *argv[])
337 {
338     notmuch_database_t *notmuch;
339     search_options_t opt = {
340         .sort = NOTMUCH_SORT_NEWEST_FIRST,
341         .output = OUTPUT_SUMMARY,
342         .offset = 0,
343         .limit = -1, /* unlimited */
344         .dupe = -1,
345     };
346     char *query_str;
347     int opt_index, ret;
348     notmuch_exclude_t exclude = NOTMUCH_EXCLUDE_TRUE;
349     unsigned int i;
350
351     enum {
352         NOTMUCH_FORMAT_JSON,
353         NOTMUCH_FORMAT_TEXT,
354         NOTMUCH_FORMAT_TEXT0,
355         NOTMUCH_FORMAT_SEXP
356     } format_sel = NOTMUCH_FORMAT_TEXT;
357
358     notmuch_opt_desc_t options[] = {
359         { NOTMUCH_OPT_KEYWORD, &opt.sort, "sort", 's',
360           (notmuch_keyword_t []){ { "oldest-first", NOTMUCH_SORT_OLDEST_FIRST },
361                                   { "newest-first", NOTMUCH_SORT_NEWEST_FIRST },
362                                   { 0, 0 } } },
363         { NOTMUCH_OPT_KEYWORD, &format_sel, "format", 'f',
364           (notmuch_keyword_t []){ { "json", NOTMUCH_FORMAT_JSON },
365                                   { "sexp", NOTMUCH_FORMAT_SEXP },
366                                   { "text", NOTMUCH_FORMAT_TEXT },
367                                   { "text0", NOTMUCH_FORMAT_TEXT0 },
368                                   { 0, 0 } } },
369         { NOTMUCH_OPT_INT, &notmuch_format_version, "format-version", 0, 0 },
370         { NOTMUCH_OPT_KEYWORD, &opt.output, "output", 'o',
371           (notmuch_keyword_t []){ { "summary", OUTPUT_SUMMARY },
372                                   { "threads", OUTPUT_THREADS },
373                                   { "messages", OUTPUT_MESSAGES },
374                                   { "files", OUTPUT_FILES },
375                                   { "tags", OUTPUT_TAGS },
376                                   { 0, 0 } } },
377         { NOTMUCH_OPT_KEYWORD, &exclude, "exclude", 'x',
378           (notmuch_keyword_t []){ { "true", NOTMUCH_EXCLUDE_TRUE },
379                                   { "false", NOTMUCH_EXCLUDE_FALSE },
380                                   { "flag", NOTMUCH_EXCLUDE_FLAG },
381                                   { "all", NOTMUCH_EXCLUDE_ALL },
382                                   { 0, 0 } } },
383         { NOTMUCH_OPT_INT, &opt.offset, "offset", 'O', 0 },
384         { NOTMUCH_OPT_INT, &opt.limit, "limit", 'L', 0  },
385         { NOTMUCH_OPT_INT, &opt.dupe, "duplicate", 'D', 0  },
386         { 0, 0, 0, 0, 0 }
387     };
388
389     opt_index = parse_arguments (argc, argv, options, 1);
390     if (opt_index < 0)
391         return EXIT_FAILURE;
392
393     switch (format_sel) {
394     case NOTMUCH_FORMAT_TEXT:
395         opt.format = sprinter_text_create (config, stdout);
396         break;
397     case NOTMUCH_FORMAT_TEXT0:
398         if (opt.output == OUTPUT_SUMMARY) {
399             fprintf (stderr, "Error: --format=text0 is not compatible with --output=summary.\n");
400             return EXIT_FAILURE;
401         }
402         opt.format = sprinter_text0_create (config, stdout);
403         break;
404     case NOTMUCH_FORMAT_JSON:
405         opt.format = sprinter_json_create (config, stdout);
406         break;
407     case NOTMUCH_FORMAT_SEXP:
408         opt.format = sprinter_sexp_create (config, stdout);
409         break;
410     default:
411         /* this should never happen */
412         INTERNAL_ERROR("no output format selected");
413     }
414
415     notmuch_exit_if_unsupported_format ();
416
417     if (notmuch_database_open (notmuch_config_get_database_path (config),
418                                NOTMUCH_DATABASE_MODE_READ_ONLY, &notmuch))
419         return EXIT_FAILURE;
420
421     query_str = query_string_from_args (notmuch, argc-opt_index, argv+opt_index);
422     if (query_str == NULL) {
423         fprintf (stderr, "Out of memory.\n");
424         return EXIT_FAILURE;
425     }
426     if (*query_str == '\0') {
427         fprintf (stderr, "Error: notmuch search requires at least one search term.\n");
428         return EXIT_FAILURE;
429     }
430
431     opt.query = notmuch_query_create (notmuch, query_str);
432     if (opt.query == NULL) {
433         fprintf (stderr, "Out of memory\n");
434         return EXIT_FAILURE;
435     }
436
437     notmuch_query_set_sort (opt.query, opt.sort);
438
439     if (exclude == NOTMUCH_EXCLUDE_FLAG && opt.output != OUTPUT_SUMMARY) {
440         /* If we are not doing summary output there is nowhere to
441          * print the excluded flag so fall back on including the
442          * excluded messages. */
443         fprintf (stderr, "Warning: this output format cannot flag excluded messages.\n");
444         exclude = NOTMUCH_EXCLUDE_FALSE;
445     }
446
447     if (exclude != NOTMUCH_EXCLUDE_FALSE) {
448         const char **search_exclude_tags;
449         size_t search_exclude_tags_length;
450
451         search_exclude_tags = notmuch_config_get_search_exclude_tags
452             (config, &search_exclude_tags_length);
453         for (i = 0; i < search_exclude_tags_length; i++)
454             notmuch_query_add_tag_exclude (opt.query, search_exclude_tags[i]);
455         notmuch_query_set_omit_excluded (opt.query, exclude);
456     }
457
458     switch (opt.output) {
459     default:
460     case OUTPUT_SUMMARY:
461     case OUTPUT_THREADS:
462         ret = do_search_threads (&opt);
463         break;
464     case OUTPUT_MESSAGES:
465     case OUTPUT_FILES:
466         ret = do_search_messages (&opt);
467         break;
468     case OUTPUT_TAGS:
469         ret = do_search_tags (notmuch, &opt);
470         break;
471     }
472
473     notmuch_query_destroy (opt.query);
474     notmuch_database_destroy (notmuch);
475
476     talloc_free (opt.format);
477
478     return ret ? EXIT_FAILURE : EXIT_SUCCESS;
479 }