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"
24 do_search_threads (const void *ctx,
25 notmuch_query_t *query,
28 notmuch_thread_t *thread;
29 notmuch_threads_t *threads;
32 const char *relative_date;
34 for (threads = notmuch_query_search_threads (query);
35 notmuch_threads_has_more (threads);
36 notmuch_threads_advance (threads))
40 thread = notmuch_threads_get (threads);
42 if (sort == NOTMUCH_SORT_OLDEST_FIRST)
43 date = notmuch_thread_get_oldest_date (thread);
45 date = notmuch_thread_get_newest_date (thread);
47 relative_date = notmuch_time_relative_date (ctx, date);
49 printf ("thread:%s %12s [%d/%d] %s; %s",
50 notmuch_thread_get_thread_id (thread),
52 notmuch_thread_get_matched_messages (thread),
53 notmuch_thread_get_total_messages (thread),
54 notmuch_thread_get_authors (thread),
55 notmuch_thread_get_subject (thread));
58 for (tags = notmuch_thread_get_tags (thread);
59 notmuch_tags_has_more (tags);
60 notmuch_tags_advance (tags))
64 printf ("%s", notmuch_tags_get (tags));
69 notmuch_thread_destroy (thread);
74 notmuch_search_command (void *ctx, int argc, char *argv[])
76 notmuch_config_t *config;
77 notmuch_database_t *notmuch;
78 notmuch_query_t *query;
81 notmuch_sort_t sort = NOTMUCH_SORT_NEWEST_FIRST;
84 for (i = 0; i < argc && argv[i][0] == '-'; i++) {
85 if (strcmp (argv[i], "--") == 0) {
89 if (STRNCMP_LITERAL (argv[i], "--sort=") == 0) {
90 opt = argv[i] + sizeof ("--sort=") - 1;
91 if (strcmp (opt, "oldest-first") == 0) {
92 sort = NOTMUCH_SORT_OLDEST_FIRST;
93 } else if (strcmp (opt, "newest-first") == 0) {
94 sort = NOTMUCH_SORT_NEWEST_FIRST;
96 fprintf (stderr, "Invalid value for --sort: %s\n", opt);
100 fprintf (stderr, "Unrecognized option: %s\n", argv[i]);
108 config = notmuch_config_open (ctx, NULL, NULL);
112 notmuch = notmuch_database_open (notmuch_config_get_database_path (config),
113 NOTMUCH_DATABASE_MODE_READ_ONLY);
117 query_str = query_string_from_args (ctx, argc, argv);
118 if (query_str == NULL) {
119 fprintf (stderr, "Out of memory.\n");
122 if (*query_str == '\0') {
123 fprintf (stderr, "Error: notmuch search requires at least one search term.\n");
127 query = notmuch_query_create (notmuch, query_str);
129 fprintf (stderr, "Out of memory\n");
133 notmuch_query_set_sort (query, sort);
135 do_search_threads (ctx, query, sort);
137 notmuch_query_destroy (query);
138 notmuch_database_close (notmuch);