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