]> git.notmuchmail.org Git - notmuch/blob - notmuch-tag.c
emacs: add notmuch-search-edit-search and notmuch-tree-edit-search
[notmuch] / notmuch-tag.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 https://www.gnu.org/licenses/ .
17  *
18  * Author: Carl Worth <cworth@cworth.org>
19  */
20
21 #include "notmuch-client.h"
22 #include "tag-util.h"
23 #include "string-util.h"
24
25 static volatile sig_atomic_t interrupted;
26
27 static void
28 handle_sigint (unused (int sig))
29 {
30     static const char msg[] = "Stopping...         \n";
31
32     /* This write is "opportunistic", so it's okay to ignore the
33      * result.  It is not required for correctness, and if it does
34      * fail or produce a short write, we want to get out of the signal
35      * handler as quickly as possible, not retry it. */
36     IGNORE_RESULT (write (2, msg, sizeof (msg) - 1));
37     interrupted = 1;
38 }
39
40
41 static char *
42 _optimize_tag_query_infix (void *ctx, const char *orig_query_string,
43                            const tag_op_list_t *list)
44 {
45     /* This is subtler than it looks.  Xapian ignores the '-' operator
46      * at the beginning both queries and parenthesized groups and,
47      * furthermore, the presence of a '-' operator at the beginning of
48      * a group can inhibit parsing of the previous operator.  Hence,
49      * the user-provided query MUST appear first, but it is safe to
50      * parenthesize and the exclusion part of the query must not use
51      * the '-' operator (though the NOT operator is fine). */
52
53     char *escaped = NULL;
54     size_t escaped_len = 0;
55     char *query_string;
56     const char *join = "";
57     size_t i;
58
59     /* Don't optimize if there are no tag changes. */
60     if (tag_op_list_size (list) == 0)
61         return talloc_strdup (ctx, orig_query_string);
62
63     /* Build the new query string */
64     if (strcmp (orig_query_string, "*") == 0)
65         query_string = talloc_strdup (ctx, "(");
66     else
67         query_string = talloc_asprintf (ctx, "( %s ) and (", orig_query_string);
68
69     for (i = 0; i < tag_op_list_size (list) && query_string; i++) {
70         /* XXX in case of OOM, query_string will be deallocated when
71          * ctx is, which might be at shutdown */
72         if (make_boolean_term (ctx,
73                                "tag", tag_op_list_tag (list, i),
74                                &escaped, &escaped_len))
75             return NULL;
76
77         query_string = talloc_asprintf_append_buffer (
78             query_string, "%s%s%s", join,
79             tag_op_list_isremove (list, i) ? "" : "not ",
80             escaped);
81         join = " or ";
82     }
83
84     if (query_string)
85         query_string = talloc_strdup_append_buffer (query_string, ")");
86
87     talloc_free (escaped);
88     return query_string;
89 }
90
91 static char *
92 _optimize_tag_query (void *ctx, const char *orig_query_string,
93                      notmuch_query_syntax_t stx,
94                      const tag_op_list_t *list)
95 {
96     char *query_string;
97
98     if (stx == NOTMUCH_QUERY_SYNTAX_XAPIAN)
99         return _optimize_tag_query_infix (ctx, orig_query_string, list);
100
101     /* Don't optimize if there are no tag changes. */
102     if (tag_op_list_size (list) == 0)
103         return talloc_strdup (ctx, orig_query_string);
104
105     query_string = talloc_asprintf (ctx, "(and %s", orig_query_string);
106     for (size_t i = 0; i < tag_op_list_size (list) && query_string; i++) {
107         query_string = talloc_asprintf_append_buffer (
108             query_string, tag_op_list_isremove (list, i) ? " (tag \"%s\")" : " (not (tag \"%s\"))",
109             tag_op_list_tag (list, i));
110     }
111
112     if (query_string)
113         query_string = talloc_strdup_append_buffer (query_string, ")");
114
115     return query_string;
116 }
117
118 /* Tag messages matching 'query_string' according to 'tag_ops'
119  */
120 static int
121 tag_query (void *ctx, notmuch_database_t *notmuch, const char *query_string,
122            tag_op_list_t *tag_ops, tag_op_flag_t flags)
123 {
124     notmuch_query_t *query;
125     notmuch_messages_t *messages;
126     notmuch_message_t *message;
127     notmuch_status_t status;
128
129     int ret = NOTMUCH_STATUS_SUCCESS;
130
131     if (! (flags & TAG_FLAG_REMOVE_ALL)) {
132         /* Optimize the query so it excludes messages that already
133          * have the specified set of tags. */
134         query_string = _optimize_tag_query (ctx, query_string,
135                                             shared_option_query_syntax (),
136                                             tag_ops);
137         if (query_string == NULL) {
138             fprintf (stderr, "Out of memory.\n");
139             return 1;
140         }
141         flags |= TAG_FLAG_PRE_OPTIMIZED;
142     }
143
144     status = notmuch_query_create_with_syntax (notmuch, query_string,
145                                                shared_option_query_syntax (),
146                                                &query);
147     if (print_status_database ("notmuch tag", notmuch, status))
148         return 1;
149
150     /* tagging is not interested in any special sort order */
151     notmuch_query_set_sort (query, NOTMUCH_SORT_UNSORTED);
152
153     status = notmuch_query_search_messages (query, &messages);
154     if (print_status_query ("notmuch tag", query, status))
155         return status;
156
157     for (;
158          notmuch_messages_valid (messages) && ! interrupted;
159          notmuch_messages_move_to_next (messages)) {
160         message = notmuch_messages_get (messages);
161         ret = tag_op_list_apply (message, tag_ops, flags);
162         notmuch_message_destroy (message);
163         if (ret != NOTMUCH_STATUS_SUCCESS)
164             break;
165     }
166
167     notmuch_query_destroy (query);
168
169     return ret || interrupted;
170 }
171
172 static int
173 tag_file (void *ctx, notmuch_database_t *notmuch, tag_op_flag_t flags,
174           FILE *input)
175 {
176     char *line = NULL;
177     char *query_string = NULL;
178     size_t line_size = 0;
179     ssize_t line_len;
180     int ret = 0;
181     int warn = 0;
182     tag_op_list_t *tag_ops;
183
184     tag_ops = tag_op_list_create (ctx);
185     if (tag_ops == NULL) {
186         fprintf (stderr, "Out of memory.\n");
187         return 1;
188     }
189
190     while ((line_len = getline (&line, &line_size, input)) != -1 &&
191            ! interrupted) {
192
193         ret = parse_tag_line (ctx, line, TAG_FLAG_NONE,
194                               &query_string, tag_ops);
195
196         if (ret > 0) {
197             if (ret != TAG_PARSE_SKIPPED)
198                 /* remember there has been problematic lines */
199                 warn = 1;
200             ret = 0;
201             continue;
202         }
203
204         if (ret < 0)
205             break;
206
207         ret = tag_query (ctx, notmuch, query_string, tag_ops, flags);
208         if (ret)
209             break;
210     }
211
212     if (line)
213         free (line);
214
215     return ret || warn;
216 }
217
218 int
219 notmuch_tag_command (notmuch_database_t *notmuch, int argc, char *argv[])
220 {
221     tag_op_list_t *tag_ops = NULL;
222     char *query_string = NULL;
223     struct sigaction action;
224     tag_op_flag_t tag_flags = TAG_FLAG_NONE;
225     bool batch = false;
226     bool remove_all = false;
227     FILE *input = stdin;
228     const char *input_file_name = NULL;
229     int opt_index;
230     int ret;
231     notmuch_bool_t synchronize_flags;
232
233     /* Set up our handler for SIGINT */
234     memset (&action, 0, sizeof (struct sigaction));
235     action.sa_handler = handle_sigint;
236     sigemptyset (&action.sa_mask);
237     action.sa_flags = SA_RESTART;
238     sigaction (SIGINT, &action, NULL);
239
240     notmuch_opt_desc_t options[] = {
241         { .opt_bool = &batch, .name = "batch" },
242         { .opt_string = &input_file_name, .name = "input" },
243         { .opt_bool = &remove_all, .name = "remove-all" },
244         { .opt_inherit = notmuch_shared_options },
245         { }
246     };
247
248     opt_index = parse_arguments (argc, argv, options, 1);
249     if (opt_index < 0)
250         return EXIT_FAILURE;
251
252     notmuch_process_shared_options (notmuch, argv[0]);
253
254     if (input_file_name) {
255         batch = true;
256         input = fopen (input_file_name, "r");
257         if (input == NULL) {
258             fprintf (stderr, "Error opening %s for reading: %s\n",
259                      input_file_name, strerror (errno));
260             return EXIT_FAILURE;
261         }
262     }
263
264     if (batch) {
265         if (opt_index != argc) {
266             fprintf (stderr, "Can't specify both cmdline and stdin!\n");
267             if (input)
268                 fclose (input);
269             return EXIT_FAILURE;
270         }
271     } else {
272         tag_ops = tag_op_list_create (notmuch);
273         if (tag_ops == NULL) {
274             fprintf (stderr, "Out of memory.\n");
275             return EXIT_FAILURE;
276         }
277
278         if (parse_tag_command_line (notmuch, argc - opt_index, argv + opt_index,
279                                     &query_string, tag_ops))
280             return EXIT_FAILURE;
281
282         if (tag_op_list_size (tag_ops) == 0 && ! remove_all) {
283             fprintf (stderr, "Error: 'notmuch tag' requires at least one tag to add or remove.\n");
284             return EXIT_FAILURE;
285         }
286
287         if (*query_string == '\0') {
288             fprintf (stderr, "Error: notmuch tag requires at least one search term.\n");
289             return EXIT_FAILURE;
290         }
291     }
292
293     if (print_status_database (
294             "notmuch restore",
295             notmuch,
296             notmuch_config_get_bool (notmuch, NOTMUCH_CONFIG_SYNC_MAILDIR_FLAGS,
297                                      &synchronize_flags)))
298         return EXIT_FAILURE;
299
300     if (synchronize_flags)
301         tag_flags |= TAG_FLAG_MAILDIR_SYNC;
302
303     if (remove_all)
304         tag_flags |= TAG_FLAG_REMOVE_ALL;
305
306     if (batch)
307         ret = tag_file (notmuch, notmuch, tag_flags, input);
308     else
309         ret = tag_query (notmuch, notmuch, query_string, tag_ops, tag_flags);
310
311     notmuch_database_destroy (notmuch);
312
313     if (input != stdin)
314         fclose (input);
315
316     return ret || interrupted ? EXIT_FAILURE : EXIT_SUCCESS;
317 }