]> git.notmuchmail.org Git - notmuch/blob - notmuch-search.c
cli: crypto: abstract gpg context creation for clarity
[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 {
182     notmuch_message_t *message;
183     notmuch_messages_t *messages;
184     notmuch_filenames_t *filenames;
185     int i;
186
187     if (offset < 0) {
188         offset += notmuch_query_count_messages (query);
189         if (offset < 0)
190             offset = 0;
191     }
192
193     messages = notmuch_query_search_messages (query);
194     if (messages == NULL)
195         return 1;
196
197     format->begin_list (format);
198
199     for (i = 0;
200          notmuch_messages_valid (messages) && (limit < 0 || i < offset + limit);
201          notmuch_messages_move_to_next (messages), i++)
202     {
203         if (i < offset)
204             continue;
205
206         message = notmuch_messages_get (messages);
207
208         if (output == OUTPUT_FILES) {
209             filenames = notmuch_message_get_filenames (message);
210
211             for (;
212                  notmuch_filenames_valid (filenames);
213                  notmuch_filenames_move_to_next (filenames))
214             {
215                 format->string (format, notmuch_filenames_get (filenames));
216                 format->separator (format);
217             }
218             
219             notmuch_filenames_destroy( filenames );
220
221         } else { /* output == OUTPUT_MESSAGES */
222             format->set_prefix (format, "id");
223             format->string (format,
224                             notmuch_message_get_message_id (message));
225             format->separator (format);
226         }
227
228         notmuch_message_destroy (message);
229     }
230
231     notmuch_messages_destroy (messages);
232
233     format->end (format);
234
235     return 0;
236 }
237
238 static int
239 do_search_tags (notmuch_database_t *notmuch,
240                 sprinter_t *format,
241                 notmuch_query_t *query)
242 {
243     notmuch_messages_t *messages = NULL;
244     notmuch_tags_t *tags;
245     const char *tag;
246
247     /* should the following only special case if no excluded terms
248      * specified? */
249
250     /* Special-case query of "*" for better performance. */
251     if (strcmp (notmuch_query_get_query_string (query), "*") == 0) {
252         tags = notmuch_database_get_all_tags (notmuch);
253     } else {
254         messages = notmuch_query_search_messages (query);
255         if (messages == NULL)
256             return 1;
257
258         tags = notmuch_messages_collect_tags (messages);
259     }
260     if (tags == NULL)
261         return 1;
262
263     format->begin_list (format);
264
265     for (;
266          notmuch_tags_valid (tags);
267          notmuch_tags_move_to_next (tags))
268     {
269         tag = notmuch_tags_get (tags);
270
271         format->string (format, tag);
272         format->separator (format);
273
274     }
275
276     notmuch_tags_destroy (tags);
277
278     if (messages)
279         notmuch_messages_destroy (messages);
280
281     format->end (format);
282
283     return 0;
284 }
285
286 enum {
287     EXCLUDE_TRUE,
288     EXCLUDE_FALSE,
289     EXCLUDE_FLAG,
290 };
291
292 int
293 notmuch_search_command (notmuch_config_t *config, int argc, char *argv[])
294 {
295     notmuch_database_t *notmuch;
296     notmuch_query_t *query;
297     char *query_str;
298     notmuch_sort_t sort = NOTMUCH_SORT_NEWEST_FIRST;
299     sprinter_t *format = NULL;
300     int opt_index, ret;
301     output_t output = OUTPUT_SUMMARY;
302     int offset = 0;
303     int limit = -1; /* unlimited */
304     int exclude = EXCLUDE_TRUE;
305     unsigned int i;
306
307     enum {
308         NOTMUCH_FORMAT_JSON,
309         NOTMUCH_FORMAT_TEXT,
310         NOTMUCH_FORMAT_TEXT0,
311         NOTMUCH_FORMAT_SEXP
312     } format_sel = NOTMUCH_FORMAT_TEXT;
313
314     notmuch_opt_desc_t options[] = {
315         { NOTMUCH_OPT_KEYWORD, &sort, "sort", 's',
316           (notmuch_keyword_t []){ { "oldest-first", NOTMUCH_SORT_OLDEST_FIRST },
317                                   { "newest-first", NOTMUCH_SORT_NEWEST_FIRST },
318                                   { 0, 0 } } },
319         { NOTMUCH_OPT_KEYWORD, &format_sel, "format", 'f',
320           (notmuch_keyword_t []){ { "json", NOTMUCH_FORMAT_JSON },
321                                   { "sexp", NOTMUCH_FORMAT_SEXP },
322                                   { "text", NOTMUCH_FORMAT_TEXT },
323                                   { "text0", NOTMUCH_FORMAT_TEXT0 },
324                                   { 0, 0 } } },
325         { NOTMUCH_OPT_INT, &notmuch_format_version, "format-version", 0, 0 },
326         { NOTMUCH_OPT_KEYWORD, &output, "output", 'o',
327           (notmuch_keyword_t []){ { "summary", OUTPUT_SUMMARY },
328                                   { "threads", OUTPUT_THREADS },
329                                   { "messages", OUTPUT_MESSAGES },
330                                   { "files", OUTPUT_FILES },
331                                   { "tags", OUTPUT_TAGS },
332                                   { 0, 0 } } },
333         { NOTMUCH_OPT_KEYWORD, &exclude, "exclude", 'x',
334           (notmuch_keyword_t []){ { "true", EXCLUDE_TRUE },
335                                   { "false", EXCLUDE_FALSE },
336                                   { "flag", EXCLUDE_FLAG },
337                                   { 0, 0 } } },
338         { NOTMUCH_OPT_INT, &offset, "offset", 'O', 0 },
339         { NOTMUCH_OPT_INT, &limit, "limit", 'L', 0  },
340         { 0, 0, 0, 0, 0 }
341     };
342
343     opt_index = parse_arguments (argc, argv, options, 1);
344
345     if (opt_index < 0) {
346         return 1;
347     }
348
349     switch (format_sel) {
350     case NOTMUCH_FORMAT_TEXT:
351         format = sprinter_text_create (config, stdout);
352         break;
353     case NOTMUCH_FORMAT_TEXT0:
354         if (output == OUTPUT_SUMMARY) {
355             fprintf (stderr, "Error: --format=text0 is not compatible with --output=summary.\n");
356             return 1;
357         }
358         format = sprinter_text0_create (config, stdout);
359         break;
360     case NOTMUCH_FORMAT_JSON:
361         format = sprinter_json_create (config, stdout);
362         break;
363     case NOTMUCH_FORMAT_SEXP:
364         format = sprinter_sexp_create (config, stdout);
365         break;
366     default:
367         /* this should never happen */
368         INTERNAL_ERROR("no output format selected");
369     }
370
371     notmuch_exit_if_unsupported_format ();
372
373     if (notmuch_database_open (notmuch_config_get_database_path (config),
374                                NOTMUCH_DATABASE_MODE_READ_ONLY, &notmuch))
375         return 1;
376
377     query_str = query_string_from_args (notmuch, argc-opt_index, argv+opt_index);
378     if (query_str == NULL) {
379         fprintf (stderr, "Out of memory.\n");
380         return 1;
381     }
382     if (*query_str == '\0') {
383         fprintf (stderr, "Error: notmuch search requires at least one search term.\n");
384         return 1;
385     }
386
387     query = notmuch_query_create (notmuch, query_str);
388     if (query == NULL) {
389         fprintf (stderr, "Out of memory\n");
390         return 1;
391     }
392
393     notmuch_query_set_sort (query, sort);
394
395     if (exclude == EXCLUDE_FLAG && output != OUTPUT_SUMMARY) {
396         /* If we are not doing summary output there is nowhere to
397          * print the excluded flag so fall back on including the
398          * excluded messages. */
399         fprintf (stderr, "Warning: this output format cannot flag excluded messages.\n");
400         exclude = EXCLUDE_FALSE;
401     }
402
403     if (exclude == EXCLUDE_TRUE || exclude == EXCLUDE_FLAG) {
404         const char **search_exclude_tags;
405         size_t search_exclude_tags_length;
406
407         search_exclude_tags = notmuch_config_get_search_exclude_tags
408             (config, &search_exclude_tags_length);
409         for (i = 0; i < search_exclude_tags_length; i++)
410             notmuch_query_add_tag_exclude (query, search_exclude_tags[i]);
411         if (exclude == EXCLUDE_FLAG)
412             notmuch_query_set_omit_excluded (query, FALSE);
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);
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 }