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 notmuch_search_command (void *ctx, int argc, char *argv[])
26 notmuch_config_t *config;
27 notmuch_database_t *notmuch;
28 notmuch_query_t *query;
29 notmuch_threads_t *threads;
30 notmuch_thread_t *thread;
33 const char *relative_date;
35 int i, first = 0, max_threads = -1;
38 for (i = 0; i < argc && argv[i][0] == '-'; i++) {
39 if (strcmp (argv[i], "--") == 0) {
43 if (STRNCMP_LITERAL (argv[i], "--first=") == 0) {
44 opt = argv[i] + sizeof ("--first=") - 1;
45 first = strtoul (opt, &end, 10);
46 if (*opt == '\0' || *end != '\0') {
47 fprintf (stderr, "Invalid value for --first: %s\n", opt);
50 } else if (STRNCMP_LITERAL (argv[i], "--max-threads=") == 0) {
51 opt = argv[i] + sizeof ("--max-threads=") - 1;
52 max_threads = strtoul (opt, &end, 10);
53 if (*opt == '\0' || *end != '\0') {
54 fprintf (stderr, "Invalid value for --max-threads: %s\n", opt);
63 config = notmuch_config_open (ctx, NULL, NULL);
67 notmuch = notmuch_database_open (notmuch_config_get_database_path (config));
71 query_str = query_string_from_args (ctx, argc, argv);
72 if (query_str == NULL) {
73 fprintf (stderr, "Out of moemory.\n");
77 query = notmuch_query_create (notmuch, query_str);
79 fprintf (stderr, "Out of memory\n");
83 for (threads = notmuch_query_search_threads (query, first, max_threads);
84 notmuch_threads_has_more (threads);
85 notmuch_threads_advance (threads))
89 thread = notmuch_threads_get (threads);
91 date = notmuch_thread_get_oldest_date (thread);
92 relative_date = notmuch_time_relative_date (ctx, date);
94 printf ("thread:%s %12s %s; %s",
95 notmuch_thread_get_thread_id (thread),
97 notmuch_thread_get_authors (thread),
98 notmuch_thread_get_subject (thread));
101 for (tags = notmuch_thread_get_tags (thread);
102 notmuch_tags_has_more (tags);
103 notmuch_tags_advance (tags))
107 printf ("%s", notmuch_tags_get (tags));
112 notmuch_thread_destroy (thread);
115 notmuch_query_destroy (query);
116 notmuch_database_close (notmuch);