]> git.notmuchmail.org Git - notmuch/blob - notmuch-search.c
notmuch setup: Add some comments when creating a .notmuch-config file.
[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     void *local = talloc_new (ctx);
27     notmuch_database_t *notmuch = NULL;
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     notmuch_status_t ret = NOTMUCH_STATUS_SUCCESS;
36
37     notmuch = notmuch_database_open (NULL);
38     if (notmuch == NULL) {
39         ret = 1;
40         goto DONE;
41     }
42
43     query_str = query_string_from_args (local, argc, argv);
44
45     query = notmuch_query_create (notmuch, query_str);
46     if (query == NULL) {
47         fprintf (stderr, "Out of memory\n");
48         ret = 1;
49         goto DONE;
50     }
51
52     for (threads = notmuch_query_search_threads (query);
53          notmuch_threads_has_more (threads);
54          notmuch_threads_advance (threads))
55     {
56         int first = 1;
57
58         thread = notmuch_threads_get (threads);
59
60         date = notmuch_thread_get_oldest_date (thread);
61         relative_date = notmuch_time_relative_date (local, date);
62
63         printf ("thread:%s %12s %s",
64                 notmuch_thread_get_thread_id (thread),
65                 relative_date,
66                 notmuch_thread_get_subject (thread));
67
68         printf (" (");
69         for (tags = notmuch_thread_get_tags (thread);
70              notmuch_tags_has_more (tags);
71              notmuch_tags_advance (tags))
72         {
73             if (! first)
74                 printf (" ");
75             printf ("%s", notmuch_tags_get (tags));
76             first = 0;
77         }
78         printf (")\n");
79
80         notmuch_thread_destroy (thread);
81     }
82
83     notmuch_query_destroy (query);
84
85   DONE:
86     if (notmuch)
87         notmuch_database_close (notmuch);
88     talloc_free (local);
89
90     return ret;
91 }