]> git.notmuchmail.org Git - notmuch/blob - notmuch-search.c
Merge remote branch 'drax/master'
[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 /* If the user asks for a *lot* of threads, lets give some results as
24  * quickly as possible and let the user read those while we compute
25  * the remainder. */
26 #define NOTMUCH_SHOW_INITIAL_BURST 100
27
28 static void
29 do_search_threads (const void *ctx,
30                    notmuch_query_t *query,
31                    notmuch_sort_t sort,
32                    int first, int max_threads)
33 {
34     notmuch_thread_t *thread;
35     notmuch_threads_t *threads;
36     notmuch_tags_t *tags;
37     time_t date;
38     const char *relative_date;
39
40     for (threads = notmuch_query_search_threads (query, first, max_threads);
41          notmuch_threads_has_more (threads);
42          notmuch_threads_advance (threads))
43     {
44         int first_tag = 1;
45
46         thread = notmuch_threads_get (threads);
47
48         if (sort == NOTMUCH_SORT_OLDEST_FIRST)
49             date = notmuch_thread_get_oldest_date (thread);
50         else
51             date = notmuch_thread_get_newest_date (thread);
52
53         relative_date = notmuch_time_relative_date (ctx, date);
54
55         printf ("thread:%s %12s [%d/%d] %s; %s",
56                 notmuch_thread_get_thread_id (thread),
57                 relative_date,
58                 notmuch_thread_get_matched_messages (thread),
59                 notmuch_thread_get_total_messages (thread),
60                 notmuch_thread_get_authors (thread),
61                 notmuch_thread_get_subject (thread));
62
63         printf (" (");
64         for (tags = notmuch_thread_get_tags (thread);
65              notmuch_tags_has_more (tags);
66              notmuch_tags_advance (tags))
67         {
68             if (! first_tag)
69                 printf (" ");
70             printf ("%s", notmuch_tags_get (tags));
71             first_tag = 0;
72         }
73         printf (")\n");
74
75         notmuch_thread_destroy (thread);
76     }
77 }
78
79 int
80 notmuch_search_command (void *ctx, int argc, char *argv[])
81 {
82     notmuch_config_t *config;
83     notmuch_database_t *notmuch;
84     notmuch_query_t *query;
85     char *query_str;
86     int i, first = 0, max_threads = -1;
87     char *opt, *end;
88     notmuch_sort_t sort = NOTMUCH_SORT_NEWEST_FIRST;
89
90     for (i = 0; i < argc && argv[i][0] == '-'; i++) {
91         if (strcmp (argv[i], "--") == 0) {
92             i++;
93             break;
94         }
95         if (STRNCMP_LITERAL (argv[i], "--first=") == 0) {
96             opt = argv[i] + sizeof ("--first=") - 1;
97             first = strtoul (opt, &end, 10);
98             if (*opt == '\0' || *end != '\0') {
99                 fprintf (stderr, "Invalid value for --first: %s\n", opt);
100                 return 1;
101             }
102         } else if (STRNCMP_LITERAL (argv[i], "--max-threads=") == 0) {
103             opt = argv[i] + sizeof ("--max-threads=") - 1;
104             max_threads = strtoul (opt, &end, 10);
105             if (*opt == '\0' || *end != '\0') {
106                 fprintf (stderr, "Invalid value for --max-threads: %s\n", opt);
107                 return 1;
108             }
109         } else if (STRNCMP_LITERAL (argv[i], "--sort=") == 0) {
110             opt = argv[i] + sizeof ("--sort=") - 1;
111             if (strcmp (opt, "oldest-first") == 0) {
112                 sort = NOTMUCH_SORT_OLDEST_FIRST;
113             } else if (strcmp (opt, "newest-first") == 0) {
114                 sort = NOTMUCH_SORT_NEWEST_FIRST;
115             } else {
116                 fprintf (stderr, "Invalid value for --sort: %s\n", opt);
117                 return 1;
118             }
119         } else {
120             fprintf (stderr, "Unrecognized option: %s\n", argv[i]);
121             return 1;
122         }
123     }
124
125     argc -= i;
126     argv += i;
127
128     config = notmuch_config_open (ctx, NULL, NULL);
129     if (config == NULL)
130         return 1;
131
132     notmuch = notmuch_database_open (notmuch_config_get_database_path (config),
133                                      NOTMUCH_DATABASE_MODE_READ_ONLY);
134     if (notmuch == NULL)
135         return 1;
136
137     query_str = query_string_from_args (ctx, argc, argv);
138     if (query_str == NULL) {
139         fprintf (stderr, "Out of memory.\n");
140         return 1;
141     }
142     if (*query_str == '\0') {
143         fprintf (stderr, "Error: notmuch search requires at least one search term.\n");
144         return 1;
145     }
146
147     query = notmuch_query_create (notmuch, query_str);
148     if (query == NULL) {
149         fprintf (stderr, "Out of memory\n");
150         return 1;
151     }
152
153     notmuch_query_set_sort (query, sort);
154
155     if (max_threads < 0 || max_threads > NOTMUCH_SHOW_INITIAL_BURST)
156     {
157         do_search_threads (ctx, query, sort,
158                            first, NOTMUCH_SHOW_INITIAL_BURST);
159
160         first += NOTMUCH_SHOW_INITIAL_BURST;
161         if (max_threads > 0)
162             max_threads -= NOTMUCH_SHOW_INITIAL_BURST;
163     }
164
165     do_search_threads (ctx, query, sort, first, max_threads);
166
167     notmuch_query_destroy (query);
168     notmuch_database_close (notmuch);
169
170     return 0;
171 }