]> git.notmuchmail.org Git - notmuch/blob - notmuch-search.c
thread: Simplify code for assigning the subject.
[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 struct search_format {
24     const char *results_start;
25     const char *thread_start;
26     void (*thread) (const void *ctx,
27                     const char *thread_id,
28                     const time_t date,
29                     const int matched,
30                     const int total,
31                     const char *authors,
32                     const char *subject);
33     const char *tag_start;
34     const char *tag;
35     const char *tag_sep;
36     const char *tag_end;
37     const char *thread_sep;
38     const char *thread_end;
39     const char *results_end;
40 } search_format_t;
41
42 static void
43 format_thread_text (const void *ctx,
44                     const char *thread_id,
45                     const time_t date,
46                     const int matched,
47                     const int total,
48                     const char *authors,
49                     const char *subject);
50 static const search_format_t format_text = {
51     "",
52         "",
53             format_thread_text,
54             " (",
55                 "%s", " ",
56             ")", "",
57         "\n",
58     "",
59 };
60
61 static void
62 format_thread_json (const void *ctx,
63                     const char *thread_id,
64                     const time_t date,
65                     const int matched,
66                     const int total,
67                     const char *authors,
68                     const char *subject);
69 static const search_format_t format_json = {
70     "[",
71         "{",
72             format_thread_json,
73             "\"tags\": [",
74                 "\"%s\"", ", ",
75             "]", ",\n",
76         "}",
77     "]\n",
78 };
79
80 static void
81 format_thread_text (const void *ctx,
82                     const char *thread_id,
83                     const time_t date,
84                     const int matched,
85                     const int total,
86                     const char *authors,
87                     const char *subject)
88 {
89     printf ("thread:%s %12s [%d/%d] %s; %s",
90             thread_id,
91             notmuch_time_relative_date (ctx, date),
92             matched,
93             total,
94             authors,
95             subject);
96 }
97
98 static void
99 format_thread_json (const void *ctx,
100                     const char *thread_id,
101                     const time_t date,
102                     const int matched,
103                     const int total,
104                     const char *authors,
105                     const char *subject)
106 {
107     struct tm *tm;
108     char timestamp[40];
109     void *ctx_quote = talloc_new (ctx);
110
111     tm = gmtime (&date);
112     if (tm == NULL)
113         INTERNAL_ERROR ("gmtime failed on thread %s.", thread_id);
114
115     if (strftime (timestamp, sizeof (timestamp), "%s", tm) == 0)
116         INTERNAL_ERROR ("strftime failed on thread %s.", thread_id);
117
118     printf ("\"thread\": %s,\n"
119             "\"timestamp\": %s,\n"
120             "\"matched\": %d,\n"
121             "\"total\": %d,\n"
122             "\"authors\": %s,\n"
123             "\"subject\": %s,\n",
124             json_quote_str (ctx_quote, thread_id),
125             timestamp,
126             matched,
127             total,
128             json_quote_str (ctx_quote, authors),
129             json_quote_str (ctx_quote, subject));
130
131     talloc_free (ctx_quote);
132 }
133
134 static void
135 do_search_threads (const void *ctx,
136                    const search_format_t *format,
137                    notmuch_query_t *query,
138                    notmuch_sort_t sort)
139 {
140     notmuch_thread_t *thread;
141     notmuch_threads_t *threads;
142     notmuch_tags_t *tags;
143     time_t date;
144     int first_thread = 1;
145
146     fputs (format->results_start, stdout);
147
148     for (threads = notmuch_query_search_threads (query);
149          notmuch_threads_valid (threads);
150          notmuch_threads_move_to_next (threads))
151     {
152         int first_tag = 1;
153
154         if (! first_thread)
155             fputs (format->thread_sep, stdout);
156
157         thread = notmuch_threads_get (threads);
158
159         if (sort == NOTMUCH_SORT_OLDEST_FIRST)
160             date = notmuch_thread_get_oldest_date (thread);
161         else
162             date = notmuch_thread_get_newest_date (thread);
163
164         fputs (format->thread_start, stdout);
165
166         format->thread (ctx,
167                         notmuch_thread_get_thread_id (thread),
168                         date,
169                         notmuch_thread_get_matched_messages (thread),
170                         notmuch_thread_get_total_messages (thread),
171                         notmuch_thread_get_authors (thread),
172                         notmuch_thread_get_subject (thread));
173
174         fputs (format->tag_start, stdout);
175
176         for (tags = notmuch_thread_get_tags (thread);
177              notmuch_tags_valid (tags);
178              notmuch_tags_move_to_next (tags))
179         {
180             if (! first_tag)
181                 fputs (format->tag_sep, stdout);
182             printf (format->tag, notmuch_tags_get (tags));
183             first_tag = 0;
184         }
185
186         fputs (format->tag_end, stdout);
187         fputs (format->thread_end, stdout);
188
189         first_thread = 0;
190
191         notmuch_thread_destroy (thread);
192     }
193
194     fputs (format->results_end, stdout);
195 }
196
197 int
198 notmuch_search_command (void *ctx, int argc, char *argv[])
199 {
200     notmuch_config_t *config;
201     notmuch_database_t *notmuch;
202     notmuch_query_t *query;
203     char *query_str;
204     char *opt;
205     notmuch_sort_t sort = NOTMUCH_SORT_NEWEST_FIRST;
206     const search_format_t *format = &format_text;
207     int i;
208
209     for (i = 0; i < argc && argv[i][0] == '-'; i++) {
210         if (strcmp (argv[i], "--") == 0) {
211             i++;
212             break;
213         }
214         if (STRNCMP_LITERAL (argv[i], "--sort=") == 0) {
215             opt = argv[i] + sizeof ("--sort=") - 1;
216             if (strcmp (opt, "oldest-first") == 0) {
217                 sort = NOTMUCH_SORT_OLDEST_FIRST;
218             } else if (strcmp (opt, "newest-first") == 0) {
219                 sort = NOTMUCH_SORT_NEWEST_FIRST;
220             } else {
221                 fprintf (stderr, "Invalid value for --sort: %s\n", opt);
222                 return 1;
223             }
224         } else if (STRNCMP_LITERAL (argv[i], "--format=") == 0) {
225             opt = argv[i] + sizeof ("--format=") - 1;
226             if (strcmp (opt, "text") == 0) {
227                 format = &format_text;
228             } else if (strcmp (opt, "json") == 0) {
229                 format = &format_json;
230             } else {
231                 fprintf (stderr, "Invalid value for --format: %s\n", opt);
232                 return 1;
233             }
234         } else {
235             fprintf (stderr, "Unrecognized option: %s\n", argv[i]);
236             return 1;
237         }
238     }
239
240     argc -= i;
241     argv += i;
242
243     config = notmuch_config_open (ctx, NULL, NULL);
244     if (config == NULL)
245         return 1;
246
247     notmuch = notmuch_database_open (notmuch_config_get_database_path (config),
248                                      NOTMUCH_DATABASE_MODE_READ_ONLY);
249     if (notmuch == NULL)
250         return 1;
251
252     query_str = query_string_from_args (ctx, argc, argv);
253     if (query_str == NULL) {
254         fprintf (stderr, "Out of memory.\n");
255         return 1;
256     }
257     if (*query_str == '\0') {
258         fprintf (stderr, "Error: notmuch search requires at least one search term.\n");
259         return 1;
260     }
261
262     query = notmuch_query_create (notmuch, query_str);
263     if (query == NULL) {
264         fprintf (stderr, "Out of memory\n");
265         return 1;
266     }
267
268     notmuch_query_set_sort (query, sort);
269
270     do_search_threads (ctx, format, query, sort);
271
272     notmuch_query_destroy (query);
273     notmuch_database_close (notmuch);
274
275     return 0;
276 }