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