]> git.notmuchmail.org Git - notmuch/blob - notmuch-search.c
notmuch search: Fix to actually return something.
[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 int
24 notmuch_search_command (void *ctx, int argc, char *argv[])
25 {
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;
31     notmuch_tags_t *tags;
32     char *query_str;
33     const char *relative_date;
34     time_t date;
35
36     config = notmuch_config_open (ctx, NULL, NULL);
37     if (config == NULL)
38         return 1;
39
40     notmuch = notmuch_database_open (notmuch_config_get_database_path (config));
41     if (notmuch == NULL)
42         return 1;
43
44     query_str = query_string_from_args (ctx, argc, argv);
45     if (query_str == NULL) {
46         fprintf (stderr, "Out of moemory.\n");
47         return 1;
48     }
49
50     query = notmuch_query_create (notmuch, query_str);
51     if (query == NULL) {
52         fprintf (stderr, "Out of memory\n");
53         return 1;
54     }
55
56     for (threads = notmuch_query_search_threads (query, 0, -1);
57          notmuch_threads_has_more (threads);
58          notmuch_threads_advance (threads))
59     {
60         int first = 1;
61
62         thread = notmuch_threads_get (threads);
63
64         date = notmuch_thread_get_oldest_date (thread);
65         relative_date = notmuch_time_relative_date (ctx, date);
66
67         printf ("thread:%s %12s %s; %s",
68                 notmuch_thread_get_thread_id (thread),
69                 relative_date,
70                 notmuch_thread_get_authors (thread),
71                 notmuch_thread_get_subject (thread));
72
73         printf (" (");
74         for (tags = notmuch_thread_get_tags (thread);
75              notmuch_tags_has_more (tags);
76              notmuch_tags_advance (tags))
77         {
78             if (! first)
79                 printf (" ");
80             printf ("%s", notmuch_tags_get (tags));
81             first = 0;
82         }
83         printf (")\n");
84
85         notmuch_thread_destroy (thread);
86     }
87
88     notmuch_query_destroy (query);
89     notmuch_database_close (notmuch);
90
91     return 0;
92 }