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