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