]> git.notmuchmail.org Git - notmuch/blob - notmuch-count.c
lib: consider all instances of Delivered-To header
[notmuch] / notmuch-count.c
1 /* notmuch - Not much of an email program, (just index and search)
2  *
3  * Copyright © 2009 Carl Worth
4  * Copyright © 2009 Keith Packard
5  *
6  * This program is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program.  If not, see https://www.gnu.org/licenses/ .
18  *
19  * Author: Keith Packard <keithp@keithp.com>
20  */
21
22 #include "notmuch-client.h"
23
24 enum {
25     OUTPUT_THREADS,
26     OUTPUT_MESSAGES,
27     OUTPUT_FILES,
28 };
29
30 /* Return the number of files matching the query, or -1 for an error */
31 static int
32 count_files (notmuch_query_t *query)
33 {
34     notmuch_messages_t *messages;
35     notmuch_message_t *message;
36     notmuch_filenames_t *filenames;
37     notmuch_status_t status;
38     int count = 0;
39
40     status = notmuch_query_search_messages (query, &messages);
41     if (print_status_query ("notmuch count", query, status))
42         return -1;
43
44     for (;
45          notmuch_messages_valid (messages);
46          notmuch_messages_move_to_next (messages)) {
47         message = notmuch_messages_get (messages);
48         filenames = notmuch_message_get_filenames (message);
49
50         for (;
51              notmuch_filenames_valid (filenames);
52              notmuch_filenames_move_to_next (filenames))
53             count++;
54
55         notmuch_filenames_destroy (filenames);
56         notmuch_message_destroy (message);
57     }
58
59     notmuch_messages_destroy (messages);
60
61     return count;
62 }
63
64 /* return 0 on success, -1 on failure */
65 static int
66 print_count (notmuch_database_t *notmuch, const char *query_str,
67              notmuch_config_values_t *exclude_tags, int output, int print_lastmod)
68 {
69     notmuch_query_t *query;
70     int count;
71     unsigned int ucount;
72     unsigned long revision;
73     const char *uuid;
74     int ret = 0;
75     notmuch_status_t status;
76
77     query = notmuch_query_create (notmuch, query_str);
78     if (query == NULL) {
79         fprintf (stderr, "Out of memory\n");
80         return -1;
81     }
82
83     for (notmuch_config_values_start (exclude_tags);
84          notmuch_config_values_valid (exclude_tags);
85          notmuch_config_values_move_to_next (exclude_tags)) {
86
87         status = notmuch_query_add_tag_exclude (query,
88                                                 notmuch_config_values_get (exclude_tags));
89         if (status && status != NOTMUCH_STATUS_IGNORED) {
90             print_status_query ("notmuch count", query, status);
91             ret = -1;
92             goto DONE;
93         }
94     }
95
96     switch (output) {
97     case OUTPUT_MESSAGES:
98         status = notmuch_query_count_messages (query, &ucount);
99         if (print_status_query ("notmuch count", query, status))
100             return -1;
101         printf ("%u", ucount);
102         break;
103     case OUTPUT_THREADS:
104         status = notmuch_query_count_threads (query, &ucount);
105         if (print_status_query ("notmuch count", query, status))
106             return -1;
107         printf ("%u", ucount);
108         break;
109     case OUTPUT_FILES:
110         count = count_files (query);
111         if (count >= 0) {
112             printf ("%d", count);
113         } else {
114             ret = -1;
115             goto DONE;
116         }
117         break;
118     }
119
120     if (print_lastmod) {
121         revision = notmuch_database_get_revision (notmuch, &uuid);
122         printf ("\t%s\t%lu\n", uuid, revision);
123     } else {
124         fputs ("\n", stdout);
125     }
126
127   DONE:
128     notmuch_query_destroy (query);
129
130     return ret;
131 }
132
133 static int
134 count_file (notmuch_database_t *notmuch, FILE *input, notmuch_config_values_t *exclude_tags,
135             int output, int print_lastmod)
136 {
137     char *line = NULL;
138     ssize_t line_len;
139     size_t line_size;
140     int ret = 0;
141
142     while (! ret && (line_len = getline (&line, &line_size, input)) != -1) {
143         chomp_newline (line);
144         ret = print_count (notmuch, line, exclude_tags, output, print_lastmod);
145     }
146
147     if (line)
148         free (line);
149
150     return ret;
151 }
152
153 int
154 notmuch_count_command (notmuch_database_t *notmuch, int argc, char *argv[])
155 {
156     char *query_str;
157     int opt_index;
158     int output = OUTPUT_MESSAGES;
159     bool exclude = true;
160     notmuch_config_values_t *exclude_tags = NULL;
161     bool batch = false;
162     bool print_lastmod = false;
163     FILE *input = stdin;
164     const char *input_file_name = NULL;
165     int ret;
166
167     notmuch_opt_desc_t options[] = {
168         { .opt_keyword = &output, .name = "output", .keywords =
169               (notmuch_keyword_t []){ { "threads", OUTPUT_THREADS },
170                                       { "messages", OUTPUT_MESSAGES },
171                                       { "files", OUTPUT_FILES },
172                                       { 0, 0 } } },
173         { .opt_bool = &exclude, .name = "exclude" },
174         { .opt_bool = &print_lastmod, .name = "lastmod" },
175         { .opt_bool = &batch, .name = "batch" },
176         { .opt_string = &input_file_name, .name = "input" },
177         { .opt_inherit = notmuch_shared_options },
178         { }
179     };
180
181     opt_index = parse_arguments (argc, argv, options, 1);
182     if (opt_index < 0)
183         return EXIT_FAILURE;
184
185     notmuch_process_shared_options (argv[0]);
186
187     if (input_file_name) {
188         batch = true;
189         input = fopen (input_file_name, "r");
190         if (input == NULL) {
191             fprintf (stderr, "Error opening %s for reading: %s\n",
192                      input_file_name, strerror (errno));
193             return EXIT_FAILURE;
194         }
195     }
196
197     if (batch && opt_index != argc) {
198         fprintf (stderr, "--batch and query string are not compatible\n");
199         if (input)
200             fclose (input);
201         return EXIT_FAILURE;
202     }
203
204     notmuch_exit_if_unmatched_db_uuid (notmuch);
205
206     query_str = query_string_from_args (notmuch, argc - opt_index, argv + opt_index);
207     if (query_str == NULL) {
208         fprintf (stderr, "Out of memory.\n");
209         return EXIT_FAILURE;
210     }
211
212     if (exclude) {
213         exclude_tags = notmuch_config_get_values (notmuch, NOTMUCH_CONFIG_EXCLUDE_TAGS);
214     }
215
216     if (batch)
217         ret = count_file (notmuch, input, exclude_tags, output, print_lastmod);
218     else
219         ret = print_count (notmuch, query_str, exclude_tags, output, print_lastmod);
220
221     notmuch_database_destroy (notmuch);
222
223     if (input != stdin)
224         fclose (input);
225
226     return ret ? EXIT_FAILURE : EXIT_SUCCESS;
227 }