]> git.notmuchmail.org Git - notmuch/blob - notmuch-search.c
90ff4c2dffe99234171c0294f60e2c3eb1cbe9e0
[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             "\"date_relative\": \"%s\",\n"
180             "\"matched\": %d,\n"
181             "\"total\": %d,\n"
182             "\"authors\": %s,\n"
183             "\"subject\": %s,\n",
184             json_quote_str (ctx_quote, thread_id),
185             date,
186             notmuch_time_relative_date (ctx, date),
187             matched,
188             total,
189             json_quote_str (ctx_quote, authors),
190             json_quote_str (ctx_quote, subject));
191
192     talloc_free (ctx_quote);
193 }
194
195 static int
196 do_search_threads (const search_format_t *format,
197                    notmuch_query_t *query,
198                    notmuch_sort_t sort,
199                    output_t output,
200                    int offset,
201                    int limit)
202 {
203     notmuch_thread_t *thread;
204     notmuch_threads_t *threads;
205     notmuch_tags_t *tags;
206     time_t date;
207     int first_thread = 1;
208     int i;
209
210     if (offset < 0) {
211         offset += notmuch_query_count_threads (query);
212         if (offset < 0)
213             offset = 0;
214     }
215
216     threads = notmuch_query_search_threads (query);
217     if (threads == NULL)
218         return 1;
219
220     fputs (format->results_start, stdout);
221
222     for (i = 0;
223          notmuch_threads_valid (threads) && (limit < 0 || i < offset + limit);
224          notmuch_threads_move_to_next (threads), i++)
225     {
226         int first_tag = 1;
227
228         thread = notmuch_threads_get (threads);
229
230         if (i < offset) {
231             notmuch_thread_destroy (thread);
232             continue;
233         }
234
235         if (! first_thread)
236             fputs (format->item_sep, stdout);
237
238         if (output == OUTPUT_THREADS) {
239             format->item_id (thread, "thread:",
240                              notmuch_thread_get_thread_id (thread));
241         } else { /* output == OUTPUT_SUMMARY */
242             fputs (format->item_start, stdout);
243
244             if (sort == NOTMUCH_SORT_OLDEST_FIRST)
245                 date = notmuch_thread_get_oldest_date (thread);
246             else
247                 date = notmuch_thread_get_newest_date (thread);
248
249             format->thread_summary (thread,
250                                     notmuch_thread_get_thread_id (thread),
251                                     date,
252                                     notmuch_thread_get_matched_messages (thread),
253                                     notmuch_thread_get_total_messages (thread),
254                                     notmuch_thread_get_authors (thread),
255                                     notmuch_thread_get_subject (thread));
256
257             fputs (format->tag_start, stdout);
258
259             for (tags = notmuch_thread_get_tags (thread);
260                  notmuch_tags_valid (tags);
261                  notmuch_tags_move_to_next (tags))
262             {
263                 if (! first_tag)
264                     fputs (format->tag_sep, stdout);
265                 printf (format->tag, notmuch_tags_get (tags));
266                 first_tag = 0;
267             }
268
269             fputs (format->tag_end, stdout);
270
271             fputs (format->item_end, stdout);
272         }
273
274         first_thread = 0;
275
276         notmuch_thread_destroy (thread);
277     }
278
279     if (first_thread)
280         fputs (format->results_null, stdout);
281     else
282         fputs (format->results_end, stdout);
283
284     return 0;
285 }
286
287 static int
288 do_search_messages (const search_format_t *format,
289                     notmuch_query_t *query,
290                     output_t output,
291                     int offset,
292                     int limit)
293 {
294     notmuch_message_t *message;
295     notmuch_messages_t *messages;
296     notmuch_filenames_t *filenames;
297     int first_message = 1;
298     int i;
299
300     if (offset < 0) {
301         offset += notmuch_query_count_messages (query);
302         if (offset < 0)
303             offset = 0;
304     }
305
306     messages = notmuch_query_search_messages (query);
307     if (messages == NULL)
308         return 1;
309
310     fputs (format->results_start, stdout);
311
312     for (i = 0;
313          notmuch_messages_valid (messages) && (limit < 0 || i < offset + limit);
314          notmuch_messages_move_to_next (messages), i++)
315     {
316         if (i < offset)
317             continue;
318
319         message = notmuch_messages_get (messages);
320
321         if (output == OUTPUT_FILES) {
322             filenames = notmuch_message_get_filenames (message);
323
324             for (;
325                  notmuch_filenames_valid (filenames);
326                  notmuch_filenames_move_to_next (filenames))
327             {
328                 if (! first_message)
329                     fputs (format->item_sep, stdout);
330
331                 format->item_id (message, "",
332                                  notmuch_filenames_get (filenames));
333
334                 first_message = 0;
335             }
336             
337             notmuch_filenames_destroy( filenames );
338
339         } else { /* output == OUTPUT_MESSAGES */
340             if (! first_message)
341                 fputs (format->item_sep, stdout);
342
343             format->item_id (message, "id:",
344                              notmuch_message_get_message_id (message));
345             first_message = 0;
346         }
347
348         notmuch_message_destroy (message);
349     }
350
351     notmuch_messages_destroy (messages);
352
353     if (first_message)
354         fputs (format->results_null, stdout);
355     else
356         fputs (format->results_end, stdout);
357
358     return 0;
359 }
360
361 static int
362 do_search_tags (notmuch_database_t *notmuch,
363                 const search_format_t *format,
364                 notmuch_query_t *query)
365 {
366     notmuch_messages_t *messages = NULL;
367     notmuch_tags_t *tags;
368     const char *tag;
369     int first_tag = 1;
370
371     /* Special-case query of "*" for better performance. */
372     if (strcmp (notmuch_query_get_query_string (query), "*") == 0) {
373         tags = notmuch_database_get_all_tags (notmuch);
374     } else {
375         messages = notmuch_query_search_messages (query);
376         if (messages == NULL)
377             return 1;
378
379         tags = notmuch_messages_collect_tags (messages);
380     }
381     if (tags == NULL)
382         return 1;
383
384     fputs (format->results_start, stdout);
385
386     for (;
387          notmuch_tags_valid (tags);
388          notmuch_tags_move_to_next (tags))
389     {
390         tag = notmuch_tags_get (tags);
391
392         if (! first_tag)
393             fputs (format->item_sep, stdout);
394
395         format->item_id (tags, "", tag);
396
397         first_tag = 0;
398     }
399
400     notmuch_tags_destroy (tags);
401
402     if (messages)
403         notmuch_messages_destroy (messages);
404
405     if (first_tag)
406         fputs (format->results_null, stdout);
407     else
408         fputs (format->results_end, stdout);
409
410     return 0;
411 }
412
413 int
414 notmuch_search_command (void *ctx, int argc, char *argv[])
415 {
416     notmuch_config_t *config;
417     notmuch_database_t *notmuch;
418     notmuch_query_t *query;
419     char *query_str;
420     char *opt;
421     notmuch_sort_t sort = NOTMUCH_SORT_NEWEST_FIRST;
422     const search_format_t *format = &format_text;
423     int i, ret;
424     output_t output = OUTPUT_SUMMARY;
425     int offset = 0;
426     int limit = -1; /* unlimited */
427
428     argc--; argv++; /* skip subcommand argument */
429
430     for (i = 0; i < argc && argv[i][0] == '-'; i++) {
431         if (strcmp (argv[i], "--") == 0) {
432             i++;
433             break;
434         }
435         if (STRNCMP_LITERAL (argv[i], "--sort=") == 0) {
436             opt = argv[i] + sizeof ("--sort=") - 1;
437             if (strcmp (opt, "oldest-first") == 0) {
438                 sort = NOTMUCH_SORT_OLDEST_FIRST;
439             } else if (strcmp (opt, "newest-first") == 0) {
440                 sort = NOTMUCH_SORT_NEWEST_FIRST;
441             } else {
442                 fprintf (stderr, "Invalid value for --sort: %s\n", opt);
443                 return 1;
444             }
445         } else if (STRNCMP_LITERAL (argv[i], "--offset=") == 0) {
446             char *p;
447             opt = argv[i] + sizeof ("--offset=") - 1;
448             offset = strtol (opt, &p, 10);
449             if (*opt == '\0' || p == opt || *p != '\0') {
450                 fprintf (stderr, "Invalid value for --offset: %s\n", opt);
451                 return 1;
452             }
453         } else if (STRNCMP_LITERAL (argv[i], "--limit=") == 0) {
454             char *p;
455             opt = argv[i] + sizeof ("--limit=") - 1;
456             limit = strtoul (opt, &p, 10);
457             if (*opt == '\0' || p == opt || *p != '\0') {
458                 fprintf (stderr, "Invalid value for --limit: %s\n", opt);
459                 return 1;
460             }
461         } else if (STRNCMP_LITERAL (argv[i], "--format=") == 0) {
462             opt = argv[i] + sizeof ("--format=") - 1;
463             if (strcmp (opt, "text") == 0) {
464                 format = &format_text;
465             } else if (strcmp (opt, "json") == 0) {
466                 format = &format_json;
467             } else {
468                 fprintf (stderr, "Invalid value for --format: %s\n", opt);
469                 return 1;
470             }
471         } else if (STRNCMP_LITERAL (argv[i], "--output=") == 0) {
472             opt = argv[i] + sizeof ("--output=") - 1;
473             if (strcmp (opt, "summary") == 0) {
474                 output = OUTPUT_SUMMARY;
475             } else if (strcmp (opt, "threads") == 0) {
476                 output = OUTPUT_THREADS;
477             } else if (strcmp (opt, "messages") == 0) {
478                 output = OUTPUT_MESSAGES;
479             } else if (strcmp (opt, "files") == 0) {
480                 output = OUTPUT_FILES;
481             } else if (strcmp (opt, "tags") == 0) {
482                 output = OUTPUT_TAGS;
483             } else {
484                 fprintf (stderr, "Invalid value for --output: %s\n", opt);
485                 return 1;
486             }
487         } else {
488             fprintf (stderr, "Unrecognized option: %s\n", argv[i]);
489             return 1;
490         }
491     }
492
493     argc -= i;
494     argv += i;
495
496     config = notmuch_config_open (ctx, NULL, NULL);
497     if (config == NULL)
498         return 1;
499
500     notmuch = notmuch_database_open (notmuch_config_get_database_path (config),
501                                      NOTMUCH_DATABASE_MODE_READ_ONLY);
502     if (notmuch == NULL)
503         return 1;
504
505     query_str = query_string_from_args (notmuch, argc, argv);
506     if (query_str == NULL) {
507         fprintf (stderr, "Out of memory.\n");
508         return 1;
509     }
510     if (*query_str == '\0') {
511         fprintf (stderr, "Error: notmuch search requires at least one search term.\n");
512         return 1;
513     }
514
515     query = notmuch_query_create (notmuch, query_str);
516     if (query == NULL) {
517         fprintf (stderr, "Out of memory\n");
518         return 1;
519     }
520
521     notmuch_query_set_sort (query, sort);
522
523     switch (output) {
524     default:
525     case OUTPUT_SUMMARY:
526     case OUTPUT_THREADS:
527         ret = do_search_threads (format, query, sort, output, offset, limit);
528         break;
529     case OUTPUT_MESSAGES:
530     case OUTPUT_FILES:
531         ret = do_search_messages (format, query, output, offset, limit);
532         break;
533     case OUTPUT_TAGS:
534         ret = do_search_tags (notmuch, format, query);
535         break;
536     }
537
538     notmuch_query_destroy (query);
539     notmuch_database_close (notmuch);
540
541     return ret;
542 }