1 /* notmuch - Not much of an email program, (just index and search)
3 * Copyright © 2009 Carl Worth
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.
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.
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/ .
18 * Author: Carl Worth <cworth@cworth.org>
21 #include "notmuch-client.h"
23 #include "string-util.h"
26 OUTPUT_SUMMARY = 1 << 0,
27 OUTPUT_THREADS = 1 << 1,
28 OUTPUT_MESSAGES = 1 << 2,
29 OUTPUT_FILES = 1 << 3,
31 OUTPUT_SENDER = 1 << 5,
32 OUTPUT_RECIPIENTS = 1 << 6,
35 #define OUTPUT_ADDRESS_FLAGS (OUTPUT_SENDER | OUTPUT_RECIPIENTS)
39 notmuch_query_t *query;
52 /* Return two stable query strings that identify exactly the matched
53 * and unmatched messages currently in thread. If there are no
54 * matched or unmatched messages, the returned buffers will be
57 get_thread_query (notmuch_thread_t *thread,
58 char **matched_out, char **unmatched_out)
60 notmuch_messages_t *messages;
62 size_t escaped_len = 0;
64 *matched_out = *unmatched_out = NULL;
66 for (messages = notmuch_thread_get_messages (thread);
67 notmuch_messages_valid (messages);
68 notmuch_messages_move_to_next (messages))
70 notmuch_message_t *message = notmuch_messages_get (messages);
71 const char *mid = notmuch_message_get_message_id (message);
72 /* Determine which query buffer to extend */
73 char **buf = notmuch_message_get_flag (
74 message, NOTMUCH_MESSAGE_FLAG_MATCH) ? matched_out : unmatched_out;
75 /* Add this message's id: query. Since "id" is an exclusive
76 * prefix, it is implicitly 'or'd together, so we only need to
77 * join queries with a space. */
78 if (make_boolean_term (thread, "id", mid, &escaped, &escaped_len) < 0)
81 *buf = talloc_asprintf_append_buffer (*buf, " %s", escaped);
83 *buf = talloc_strdup (thread, escaped);
87 talloc_free (escaped);
92 do_search_threads (search_options_t *opt)
94 notmuch_thread_t *thread;
95 notmuch_threads_t *threads;
97 sprinter_t *format = opt->format;
101 if (opt->offset < 0) {
102 opt->offset += notmuch_query_count_threads (opt->query);
107 threads = notmuch_query_search_threads (opt->query);
111 format->begin_list (format);
114 notmuch_threads_valid (threads) && (opt->limit < 0 || i < opt->offset + opt->limit);
115 notmuch_threads_move_to_next (threads), i++)
117 thread = notmuch_threads_get (threads);
119 if (i < opt->offset) {
120 notmuch_thread_destroy (thread);
124 if (opt->output == OUTPUT_THREADS) {
125 format->set_prefix (format, "thread");
126 format->string (format,
127 notmuch_thread_get_thread_id (thread));
128 format->separator (format);
129 } else { /* output == OUTPUT_SUMMARY */
130 void *ctx_quote = talloc_new (thread);
131 const char *authors = notmuch_thread_get_authors (thread);
132 const char *subject = notmuch_thread_get_subject (thread);
133 const char *thread_id = notmuch_thread_get_thread_id (thread);
134 int matched = notmuch_thread_get_matched_messages (thread);
135 int total = notmuch_thread_get_total_messages (thread);
136 const char *relative_date = NULL;
137 notmuch_bool_t first_tag = TRUE;
139 format->begin_map (format);
141 if (opt->sort == NOTMUCH_SORT_OLDEST_FIRST)
142 date = notmuch_thread_get_oldest_date (thread);
144 date = notmuch_thread_get_newest_date (thread);
146 relative_date = notmuch_time_relative_date (ctx_quote, date);
148 if (format->is_text_printer) {
149 /* Special case for the text formatter */
150 printf ("thread:%s %12s [%d/%d] %s; %s (",
155 sanitize_string (ctx_quote, authors),
156 sanitize_string (ctx_quote, subject));
157 } else { /* Structured Output */
158 format->map_key (format, "thread");
159 format->string (format, thread_id);
160 format->map_key (format, "timestamp");
161 format->integer (format, date);
162 format->map_key (format, "date_relative");
163 format->string (format, relative_date);
164 format->map_key (format, "matched");
165 format->integer (format, matched);
166 format->map_key (format, "total");
167 format->integer (format, total);
168 format->map_key (format, "authors");
169 format->string (format, authors);
170 format->map_key (format, "subject");
171 format->string (format, subject);
172 if (notmuch_format_version >= 2) {
173 char *matched_query, *unmatched_query;
174 if (get_thread_query (thread, &matched_query,
175 &unmatched_query) < 0) {
176 fprintf (stderr, "Out of memory\n");
179 format->map_key (format, "query");
180 format->begin_list (format);
182 format->string (format, matched_query);
184 format->null (format);
186 format->string (format, unmatched_query);
188 format->null (format);
189 format->end (format);
193 talloc_free (ctx_quote);
195 format->map_key (format, "tags");
196 format->begin_list (format);
198 for (tags = notmuch_thread_get_tags (thread);
199 notmuch_tags_valid (tags);
200 notmuch_tags_move_to_next (tags))
202 const char *tag = notmuch_tags_get (tags);
204 if (format->is_text_printer) {
205 /* Special case for the text formatter */
211 } else { /* Structured Output */
212 format->string (format, tag);
216 if (format->is_text_printer)
219 format->end (format);
220 format->end (format);
221 format->separator (format);
224 notmuch_thread_destroy (thread);
227 format->end (format);
233 print_mailbox (const search_options_t *opt, const mailbox_t *mailbox)
235 const char *name = mailbox->name;
236 const char *addr = mailbox->addr;
238 if (opt->format->is_text_printer) {
242 mailbox_str = talloc_asprintf (opt->format, "%s <%s>", name, addr);
244 mailbox_str = talloc_strdup (opt->format, addr);
247 fprintf (stderr, "Error: out of memory\n");
250 opt->format->string (opt->format, mailbox_str);
251 opt->format->separator (opt->format);
253 talloc_free (mailbox_str);
255 opt->format->begin_map (opt->format);
256 opt->format->map_key (opt->format, "name");
257 opt->format->string (opt->format, name);
258 opt->format->map_key (opt->format, "address");
259 opt->format->string (opt->format, addr);
260 opt->format->end (opt->format);
261 opt->format->separator (opt->format);
266 process_address_list (const search_options_t *opt, InternetAddressList *list)
268 InternetAddress *address;
271 for (i = 0; i < internet_address_list_length (list); i++) {
272 address = internet_address_list_get_address (list, i);
273 if (INTERNET_ADDRESS_IS_GROUP (address)) {
274 InternetAddressGroup *group;
275 InternetAddressList *group_list;
277 group = INTERNET_ADDRESS_GROUP (address);
278 group_list = internet_address_group_get_members (group);
279 if (group_list == NULL)
282 process_address_list (opt, group_list);
284 InternetAddressMailbox *mailbox = INTERNET_ADDRESS_MAILBOX (address);
286 .name = internet_address_get_name (address),
287 .addr = internet_address_mailbox_get_addr (mailbox),
290 print_mailbox (opt, &mbx);
296 process_address_header (const search_options_t *opt, const char *value)
298 InternetAddressList *list;
303 list = internet_address_list_parse_string (value);
307 process_address_list (opt, list);
311 do_search_messages (search_options_t *opt)
313 notmuch_message_t *message;
314 notmuch_messages_t *messages;
315 notmuch_filenames_t *filenames;
316 sprinter_t *format = opt->format;
319 if (opt->offset < 0) {
320 opt->offset += notmuch_query_count_messages (opt->query);
325 messages = notmuch_query_search_messages (opt->query);
326 if (messages == NULL)
329 format->begin_list (format);
332 notmuch_messages_valid (messages) && (opt->limit < 0 || i < opt->offset + opt->limit);
333 notmuch_messages_move_to_next (messages), i++)
338 message = notmuch_messages_get (messages);
340 if (opt->output == OUTPUT_FILES) {
342 filenames = notmuch_message_get_filenames (message);
345 notmuch_filenames_valid (filenames);
346 notmuch_filenames_move_to_next (filenames), j++)
348 if (opt->dupe < 0 || opt->dupe == j) {
349 format->string (format, notmuch_filenames_get (filenames));
350 format->separator (format);
354 notmuch_filenames_destroy( filenames );
356 } else if (opt->output == OUTPUT_MESSAGES) {
357 format->set_prefix (format, "id");
358 format->string (format,
359 notmuch_message_get_message_id (message));
360 format->separator (format);
362 if (opt->output & OUTPUT_SENDER) {
365 addrs = notmuch_message_get_header (message, "from");
366 process_address_header (opt, addrs);
369 if (opt->output & OUTPUT_RECIPIENTS) {
370 const char *hdrs[] = { "to", "cc", "bcc" };
374 for (j = 0; j < ARRAY_SIZE (hdrs); j++) {
375 addrs = notmuch_message_get_header (message, hdrs[j]);
376 process_address_header (opt, addrs);
381 notmuch_message_destroy (message);
384 notmuch_messages_destroy (messages);
386 format->end (format);
392 do_search_tags (notmuch_database_t *notmuch,
393 const search_options_t *opt)
395 notmuch_messages_t *messages = NULL;
396 notmuch_tags_t *tags;
398 sprinter_t *format = opt->format;
399 notmuch_query_t *query = opt->query;
401 /* should the following only special case if no excluded terms
404 /* Special-case query of "*" for better performance. */
405 if (strcmp (notmuch_query_get_query_string (query), "*") == 0) {
406 tags = notmuch_database_get_all_tags (notmuch);
408 messages = notmuch_query_search_messages (query);
409 if (messages == NULL)
412 tags = notmuch_messages_collect_tags (messages);
417 format->begin_list (format);
420 notmuch_tags_valid (tags);
421 notmuch_tags_move_to_next (tags))
423 tag = notmuch_tags_get (tags);
425 format->string (format, tag);
426 format->separator (format);
430 notmuch_tags_destroy (tags);
433 notmuch_messages_destroy (messages);
435 format->end (format);
441 notmuch_search_command (notmuch_config_t *config, int argc, char *argv[])
443 notmuch_database_t *notmuch;
444 search_options_t opt = {
445 .sort = NOTMUCH_SORT_NEWEST_FIRST,
448 .limit = -1, /* unlimited */
453 notmuch_exclude_t exclude = NOTMUCH_EXCLUDE_TRUE;
459 NOTMUCH_FORMAT_TEXT0,
461 } format_sel = NOTMUCH_FORMAT_TEXT;
463 notmuch_opt_desc_t options[] = {
464 { NOTMUCH_OPT_KEYWORD, &opt.sort, "sort", 's',
465 (notmuch_keyword_t []){ { "oldest-first", NOTMUCH_SORT_OLDEST_FIRST },
466 { "newest-first", NOTMUCH_SORT_NEWEST_FIRST },
468 { NOTMUCH_OPT_KEYWORD, &format_sel, "format", 'f',
469 (notmuch_keyword_t []){ { "json", NOTMUCH_FORMAT_JSON },
470 { "sexp", NOTMUCH_FORMAT_SEXP },
471 { "text", NOTMUCH_FORMAT_TEXT },
472 { "text0", NOTMUCH_FORMAT_TEXT0 },
474 { NOTMUCH_OPT_INT, ¬much_format_version, "format-version", 0, 0 },
475 { NOTMUCH_OPT_KEYWORD_FLAGS, &opt.output, "output", 'o',
476 (notmuch_keyword_t []){ { "summary", OUTPUT_SUMMARY },
477 { "threads", OUTPUT_THREADS },
478 { "messages", OUTPUT_MESSAGES },
479 { "sender", OUTPUT_SENDER },
480 { "recipients", OUTPUT_RECIPIENTS },
481 { "files", OUTPUT_FILES },
482 { "tags", OUTPUT_TAGS },
484 { NOTMUCH_OPT_KEYWORD, &exclude, "exclude", 'x',
485 (notmuch_keyword_t []){ { "true", NOTMUCH_EXCLUDE_TRUE },
486 { "false", NOTMUCH_EXCLUDE_FALSE },
487 { "flag", NOTMUCH_EXCLUDE_FLAG },
488 { "all", NOTMUCH_EXCLUDE_ALL },
490 { NOTMUCH_OPT_INT, &opt.offset, "offset", 'O', 0 },
491 { NOTMUCH_OPT_INT, &opt.limit, "limit", 'L', 0 },
492 { NOTMUCH_OPT_INT, &opt.dupe, "duplicate", 'D', 0 },
496 opt_index = parse_arguments (argc, argv, options, 1);
501 opt.output = OUTPUT_SUMMARY;
503 switch (format_sel) {
504 case NOTMUCH_FORMAT_TEXT:
505 opt.format = sprinter_text_create (config, stdout);
507 case NOTMUCH_FORMAT_TEXT0:
508 if (opt.output == OUTPUT_SUMMARY) {
509 fprintf (stderr, "Error: --format=text0 is not compatible with --output=summary.\n");
512 opt.format = sprinter_text0_create (config, stdout);
514 case NOTMUCH_FORMAT_JSON:
515 opt.format = sprinter_json_create (config, stdout);
517 case NOTMUCH_FORMAT_SEXP:
518 opt.format = sprinter_sexp_create (config, stdout);
521 /* this should never happen */
522 INTERNAL_ERROR("no output format selected");
525 notmuch_exit_if_unsupported_format ();
527 if (notmuch_database_open (notmuch_config_get_database_path (config),
528 NOTMUCH_DATABASE_MODE_READ_ONLY, ¬much))
531 query_str = query_string_from_args (notmuch, argc-opt_index, argv+opt_index);
532 if (query_str == NULL) {
533 fprintf (stderr, "Out of memory.\n");
536 if (*query_str == '\0') {
537 fprintf (stderr, "Error: notmuch search requires at least one search term.\n");
541 opt.query = notmuch_query_create (notmuch, query_str);
542 if (opt.query == NULL) {
543 fprintf (stderr, "Out of memory\n");
547 notmuch_query_set_sort (opt.query, opt.sort);
549 if (exclude == NOTMUCH_EXCLUDE_FLAG && opt.output != OUTPUT_SUMMARY) {
550 /* If we are not doing summary output there is nowhere to
551 * print the excluded flag so fall back on including the
552 * excluded messages. */
553 fprintf (stderr, "Warning: this output format cannot flag excluded messages.\n");
554 exclude = NOTMUCH_EXCLUDE_FALSE;
557 if (exclude != NOTMUCH_EXCLUDE_FALSE) {
558 const char **search_exclude_tags;
559 size_t search_exclude_tags_length;
561 search_exclude_tags = notmuch_config_get_search_exclude_tags
562 (config, &search_exclude_tags_length);
563 for (i = 0; i < search_exclude_tags_length; i++)
564 notmuch_query_add_tag_exclude (opt.query, search_exclude_tags[i]);
565 notmuch_query_set_omit_excluded (opt.query, exclude);
568 if (opt.output == OUTPUT_SUMMARY ||
569 opt.output == OUTPUT_THREADS)
570 ret = do_search_threads (&opt);
571 else if (opt.output == OUTPUT_MESSAGES ||
572 opt.output == OUTPUT_FILES ||
573 (opt.output & OUTPUT_ADDRESS_FLAGS && !(opt.output & ~OUTPUT_ADDRESS_FLAGS)))
574 ret = do_search_messages (&opt);
575 else if (opt.output == OUTPUT_TAGS)
576 ret = do_search_tags (notmuch, &opt);
578 fprintf (stderr, "Error: the combination of outputs is not supported.\n");
582 notmuch_query_destroy (opt.query);
583 notmuch_database_destroy (notmuch);
585 talloc_free (opt.format);
587 return ret ? EXIT_FAILURE : EXIT_SUCCESS;