]> git.notmuchmail.org Git - notmuch/blob - notmuch-search.c
test: Test atomicity of notmuch new.
[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_FILES,
28     OUTPUT_TAGS
29 } output_t;
30
31 typedef struct search_format {
32     const char *results_start;
33     const char *item_start;
34     void (*item_id) (const void *ctx,
35                      const char *item_type,
36                      const char *item_id);
37     void (*thread_summary) (const void *ctx,
38                             const char *thread_id,
39                             const time_t date,
40                             const int matched,
41                             const int total,
42                             const char *authors,
43                             const char *subject);
44     const char *tag_start;
45     const char *tag;
46     const char *tag_sep;
47     const char *tag_end;
48     const char *item_sep;
49     const char *item_end;
50     const char *results_end;
51     const char *results_null;
52 } search_format_t;
53
54 static void
55 format_item_id_text (const void *ctx,
56                      const char *item_type,
57                      const char *item_id);
58
59 static void
60 format_thread_text (const void *ctx,
61                     const char *thread_id,
62                     const time_t date,
63                     const int matched,
64                     const int total,
65                     const char *authors,
66                     const char *subject);
67 static const search_format_t format_text = {
68     "",
69         "",
70             format_item_id_text,
71             format_thread_text,
72             " (",
73                 "%s", " ",
74             ")", "\n",
75         "",
76     "\n",
77     "",
78 };
79
80 static void
81 format_item_id_json (const void *ctx,
82                      const char *item_type,
83                      const char *item_id);
84
85 static void
86 format_thread_json (const void *ctx,
87                     const char *thread_id,
88                     const time_t date,
89                     const int matched,
90                     const int total,
91                     const char *authors,
92                     const char *subject);
93 static const search_format_t format_json = {
94     "[",
95         "{",
96             format_item_id_json,
97             format_thread_json,
98             "\"tags\": [",
99                 "\"%s\"", ", ",
100             "]", ",\n",
101         "}",
102     "]\n",
103     "]\n",
104 };
105
106 static void
107 format_item_id_text (unused (const void *ctx),
108                      const char *item_type,
109                      const char *item_id)
110 {
111     printf ("%s%s", item_type, item_id);
112 }
113
114 static char *
115 sanitize_string (const void *ctx, const char *str)
116 {
117     char *out, *loop;
118
119     if (NULL == str)
120         return NULL;
121
122     loop = out = talloc_strdup (ctx, str);
123
124     for (; *loop; loop++) {
125         if ((unsigned char)(*loop) < 32)
126             *loop = '?';
127     }
128     return out;
129 }
130
131 static void
132 format_thread_text (const void *ctx,
133                     const char *thread_id,
134                     const time_t date,
135                     const int matched,
136                     const int total,
137                     const char *authors,
138                     const char *subject)
139 {
140     void *ctx_quote = talloc_new (ctx);
141
142     printf ("thread:%s %12s [%d/%d] %s; %s",
143             thread_id,
144             notmuch_time_relative_date (ctx, date),
145             matched,
146             total,
147             sanitize_string (ctx_quote, authors),
148             sanitize_string (ctx_quote, subject));
149
150     talloc_free (ctx_quote);
151 }
152
153 static void
154 format_item_id_json (const void *ctx,
155                      unused (const char *item_type),
156                      const char *item_id)
157 {
158     void *ctx_quote = talloc_new (ctx);
159
160     printf ("%s", json_quote_str (ctx_quote, item_id));
161
162     talloc_free (ctx_quote);
163     
164 }
165
166 static void
167 format_thread_json (const void *ctx,
168                     const char *thread_id,
169                     const time_t date,
170                     const int matched,
171                     const int total,
172                     const char *authors,
173                     const char *subject)
174 {
175     void *ctx_quote = talloc_new (ctx);
176
177     printf ("\"thread\": %s,\n"
178             "\"timestamp\": %ld,\n"
179             "\"matched\": %d,\n"
180             "\"total\": %d,\n"
181             "\"authors\": %s,\n"
182             "\"subject\": %s,\n",
183             json_quote_str (ctx_quote, thread_id),
184             date,
185             matched,
186             total,
187             json_quote_str (ctx_quote, authors),
188             json_quote_str (ctx_quote, subject));
189
190     talloc_free (ctx_quote);
191 }
192
193 static int
194 do_search_threads (const search_format_t *format,
195                    notmuch_query_t *query,
196                    notmuch_sort_t sort,
197                    output_t output)
198 {
199     notmuch_thread_t *thread;
200     notmuch_threads_t *threads;
201     notmuch_tags_t *tags;
202     time_t date;
203     int first_thread = 1;
204
205     threads = notmuch_query_search_threads (query);
206     if (threads == NULL)
207         return 1;
208
209     fputs (format->results_start, stdout);
210
211     for (;
212          notmuch_threads_valid (threads);
213          notmuch_threads_move_to_next (threads))
214     {
215         int first_tag = 1;
216
217         if (! first_thread)
218             fputs (format->item_sep, stdout);
219
220         thread = notmuch_threads_get (threads);
221
222         if (output == OUTPUT_THREADS) {
223             format->item_id (thread, "thread:",
224                              notmuch_thread_get_thread_id (thread));
225         } else { /* output == OUTPUT_SUMMARY */
226             fputs (format->item_start, stdout);
227
228             if (sort == NOTMUCH_SORT_OLDEST_FIRST)
229                 date = notmuch_thread_get_oldest_date (thread);
230             else
231                 date = notmuch_thread_get_newest_date (thread);
232
233             format->thread_summary (thread,
234                                     notmuch_thread_get_thread_id (thread),
235                                     date,
236                                     notmuch_thread_get_matched_messages (thread),
237                                     notmuch_thread_get_total_messages (thread),
238                                     notmuch_thread_get_authors (thread),
239                                     notmuch_thread_get_subject (thread));
240
241             fputs (format->tag_start, stdout);
242
243             for (tags = notmuch_thread_get_tags (thread);
244                  notmuch_tags_valid (tags);
245                  notmuch_tags_move_to_next (tags))
246             {
247                 if (! first_tag)
248                     fputs (format->tag_sep, stdout);
249                 printf (format->tag, notmuch_tags_get (tags));
250                 first_tag = 0;
251             }
252
253             fputs (format->tag_end, stdout);
254
255             fputs (format->item_end, stdout);
256         }
257
258         first_thread = 0;
259
260         notmuch_thread_destroy (thread);
261     }
262
263     if (first_thread)
264         fputs (format->results_null, stdout);
265     else
266         fputs (format->results_end, stdout);
267
268     return 0;
269 }
270
271 static int
272 do_search_messages (const search_format_t *format,
273                     notmuch_query_t *query,
274                     output_t output)
275 {
276     notmuch_message_t *message;
277     notmuch_messages_t *messages;
278     notmuch_filenames_t *filenames;
279     int first_message = 1;
280
281     messages = notmuch_query_search_messages (query);
282     if (messages == NULL)
283         return 1;
284
285     fputs (format->results_start, stdout);
286
287     for (;
288          notmuch_messages_valid (messages);
289          notmuch_messages_move_to_next (messages))
290     {
291         message = notmuch_messages_get (messages);
292
293         if (output == OUTPUT_FILES) {
294             filenames = notmuch_message_get_filenames (message);
295
296             for (;
297                  notmuch_filenames_valid (filenames);
298                  notmuch_filenames_move_to_next (filenames))
299             {
300                 if (! first_message)
301                     fputs (format->item_sep, stdout);
302
303                 format->item_id (message, "",
304                                  notmuch_filenames_get (filenames));
305
306                 first_message = 0;
307             }
308             
309             notmuch_filenames_destroy( filenames );
310
311         } else { /* output == OUTPUT_MESSAGES */
312             if (! first_message)
313                 fputs (format->item_sep, stdout);
314
315             format->item_id (message, "id:",
316                              notmuch_message_get_message_id (message));
317             first_message = 0;
318         }
319
320         notmuch_message_destroy (message);
321     }
322
323     notmuch_messages_destroy (messages);
324
325     if (first_message)
326         fputs (format->results_null, stdout);
327     else
328         fputs (format->results_end, stdout);
329
330     return 0;
331 }
332
333 static int
334 do_search_tags (notmuch_database_t *notmuch,
335                 const search_format_t *format,
336                 notmuch_query_t *query)
337 {
338     notmuch_messages_t *messages = NULL;
339     notmuch_tags_t *tags;
340     const char *tag;
341     int first_tag = 1;
342
343     /* Special-case query of "*" for better performance. */
344     if (strcmp (notmuch_query_get_query_string (query), "*") == 0) {
345         tags = notmuch_database_get_all_tags (notmuch);
346     } else {
347         messages = notmuch_query_search_messages (query);
348         if (messages == NULL)
349             return 1;
350
351         tags = notmuch_messages_collect_tags (messages);
352     }
353     if (tags == NULL)
354         return 1;
355
356     fputs (format->results_start, stdout);
357
358     for (;
359          notmuch_tags_valid (tags);
360          notmuch_tags_move_to_next (tags))
361     {
362         tag = notmuch_tags_get (tags);
363
364         if (! first_tag)
365             fputs (format->item_sep, stdout);
366
367         format->item_id (tags, "", tag);
368
369         first_tag = 0;
370     }
371
372     notmuch_tags_destroy (tags);
373
374     if (messages)
375         notmuch_messages_destroy (messages);
376
377     if (first_tag)
378         fputs (format->results_null, stdout);
379     else
380         fputs (format->results_end, stdout);
381
382     return 0;
383 }
384
385 int
386 notmuch_search_command (void *ctx, int argc, char *argv[])
387 {
388     notmuch_config_t *config;
389     notmuch_database_t *notmuch;
390     notmuch_query_t *query;
391     char *query_str;
392     char *opt;
393     notmuch_sort_t sort = NOTMUCH_SORT_NEWEST_FIRST;
394     const search_format_t *format = &format_text;
395     int i, ret;
396     output_t output = OUTPUT_SUMMARY;
397
398     for (i = 0; i < argc && argv[i][0] == '-'; i++) {
399         if (strcmp (argv[i], "--") == 0) {
400             i++;
401             break;
402         }
403         if (STRNCMP_LITERAL (argv[i], "--sort=") == 0) {
404             opt = argv[i] + sizeof ("--sort=") - 1;
405             if (strcmp (opt, "oldest-first") == 0) {
406                 sort = NOTMUCH_SORT_OLDEST_FIRST;
407             } else if (strcmp (opt, "newest-first") == 0) {
408                 sort = NOTMUCH_SORT_NEWEST_FIRST;
409             } else {
410                 fprintf (stderr, "Invalid value for --sort: %s\n", opt);
411                 return 1;
412             }
413         } else if (STRNCMP_LITERAL (argv[i], "--format=") == 0) {
414             opt = argv[i] + sizeof ("--format=") - 1;
415             if (strcmp (opt, "text") == 0) {
416                 format = &format_text;
417             } else if (strcmp (opt, "json") == 0) {
418                 format = &format_json;
419             } else {
420                 fprintf (stderr, "Invalid value for --format: %s\n", opt);
421                 return 1;
422             }
423         } else if (STRNCMP_LITERAL (argv[i], "--output=") == 0) {
424             opt = argv[i] + sizeof ("--output=") - 1;
425             if (strcmp (opt, "summary") == 0) {
426                 output = OUTPUT_SUMMARY;
427             } else if (strcmp (opt, "threads") == 0) {
428                 output = OUTPUT_THREADS;
429             } else if (strcmp (opt, "messages") == 0) {
430                 output = OUTPUT_MESSAGES;
431             } else if (strcmp (opt, "files") == 0) {
432                 output = OUTPUT_FILES;
433             } else if (strcmp (opt, "tags") == 0) {
434                 output = OUTPUT_TAGS;
435             } else {
436                 fprintf (stderr, "Invalid value for --output: %s\n", opt);
437                 return 1;
438             }
439         } else {
440             fprintf (stderr, "Unrecognized option: %s\n", argv[i]);
441             return 1;
442         }
443     }
444
445     argc -= i;
446     argv += i;
447
448     config = notmuch_config_open (ctx, NULL, NULL);
449     if (config == NULL)
450         return 1;
451
452     notmuch = notmuch_database_open (notmuch_config_get_database_path (config),
453                                      NOTMUCH_DATABASE_MODE_READ_ONLY);
454     if (notmuch == NULL)
455         return 1;
456
457     query_str = query_string_from_args (notmuch, argc, argv);
458     if (query_str == NULL) {
459         fprintf (stderr, "Out of memory.\n");
460         return 1;
461     }
462     if (*query_str == '\0') {
463         fprintf (stderr, "Error: notmuch search requires at least one search term.\n");
464         return 1;
465     }
466
467     query = notmuch_query_create (notmuch, query_str);
468     if (query == NULL) {
469         fprintf (stderr, "Out of memory\n");
470         return 1;
471     }
472
473     notmuch_query_set_sort (query, sort);
474
475     switch (output) {
476     default:
477     case OUTPUT_SUMMARY:
478     case OUTPUT_THREADS:
479         ret = do_search_threads (format, query, sort, output);
480         break;
481     case OUTPUT_MESSAGES:
482     case OUTPUT_FILES:
483         ret = do_search_messages (format, query, output);
484         break;
485     case OUTPUT_TAGS:
486         ret = do_search_tags (notmuch, format, query);
487         break;
488     }
489
490     notmuch_query_destroy (query);
491     notmuch_database_close (notmuch);
492
493     return ret;
494 }