]> git.notmuchmail.org Git - notmuch/blob - notmuch-search.c
Add some const correctness to talloc 'ctx' parameter.
[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     int i, first = 0, max_threads = -1;
36     char *opt, *end;
37     notmuch_sort_t sort = NOTMUCH_SORT_DATE;
38
39     for (i = 0; i < argc && argv[i][0] == '-'; i++) {
40         if (strcmp (argv[i], "--") == 0) {
41             i++;
42             break;
43         }
44         if (STRNCMP_LITERAL (argv[i], "--first=") == 0) {
45             opt = argv[i] + sizeof ("--first=") - 1;
46             first = strtoul (opt, &end, 10);
47             if (*opt == '\0' || *end != '\0') {
48                 fprintf (stderr, "Invalid value for --first: %s\n", opt);
49                 return 1;
50             }
51         } else if (STRNCMP_LITERAL (argv[i], "--max-threads=") == 0) {
52             opt = argv[i] + sizeof ("--max-threads=") - 1;
53             max_threads = strtoul (opt, &end, 10);
54             if (*opt == '\0' || *end != '\0') {
55                 fprintf (stderr, "Invalid value for --max-threads: %s\n", opt);
56                 return 1;
57             }
58         } else if (strcmp (argv[i], "--reverse") == 0) {
59             sort = NOTMUCH_SORT_DATE_REVERSE;
60         } else {
61             fprintf (stderr, "Unrecognized option: %s\n", argv[i]);
62             return 1;
63         }
64     }
65
66     argc -= i;
67     argv += i;
68
69     config = notmuch_config_open (ctx, NULL, NULL);
70     if (config == NULL)
71         return 1;
72
73     notmuch = notmuch_database_open (notmuch_config_get_database_path (config));
74     if (notmuch == NULL)
75         return 1;
76
77     query_str = query_string_from_args (ctx, argc, argv);
78     if (query_str == NULL) {
79         fprintf (stderr, "Out of moemory.\n");
80         return 1;
81     }
82
83     query = notmuch_query_create (notmuch, query_str);
84     if (query == NULL) {
85         fprintf (stderr, "Out of memory\n");
86         return 1;
87     }
88
89     notmuch_query_set_sort (query, sort);
90
91     for (threads = notmuch_query_search_threads (query, first, max_threads);
92          notmuch_threads_has_more (threads);
93          notmuch_threads_advance (threads))
94     {
95         int first = 1;
96
97         thread = notmuch_threads_get (threads);
98
99         if (sort == NOTMUCH_SORT_DATE)
100             date = notmuch_thread_get_oldest_date (thread);
101         else
102             date = notmuch_thread_get_newest_date (thread);
103
104         relative_date = notmuch_time_relative_date (ctx, date);
105
106         printf ("thread:%s %12s [%d/%d] %s; %s",
107                 notmuch_thread_get_thread_id (thread),
108                 relative_date,
109                 notmuch_thread_get_matched_messages (thread),
110                 notmuch_thread_get_total_messages (thread),
111                 notmuch_thread_get_authors (thread),
112                 notmuch_thread_get_subject (thread));
113
114         printf (" (");
115         for (tags = notmuch_thread_get_tags (thread);
116              notmuch_tags_has_more (tags);
117              notmuch_tags_advance (tags))
118         {
119             if (! first)
120                 printf (" ");
121             printf ("%s", notmuch_tags_get (tags));
122             first = 0;
123         }
124         printf (")\n");
125
126         notmuch_thread_destroy (thread);
127     }
128
129     notmuch_query_destroy (query);
130     notmuch_database_close (notmuch);
131
132     return 0;
133 }