]> git.notmuchmail.org Git - notmuch/blob - notmuch-tag.c
dump/restore: Use Xapian queries for batch-tag format
[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 http://www.gnu.org/licenses/ .
17  *
18  * Author: Carl Worth <cworth@cworth.org>
19  */
20
21 #include "notmuch-client.h"
22 #include "string-util.h"
23
24 static volatile sig_atomic_t interrupted;
25
26 static void
27 handle_sigint (unused (int sig))
28 {
29     static char msg[] = "Stopping...         \n";
30
31     /* This write is "opportunistic", so it's okay to ignore the
32      * result.  It is not required for correctness, and if it does
33      * fail or produce a short write, we want to get out of the signal
34      * handler as quickly as possible, not retry it. */
35     IGNORE_RESULT (write (2, msg, sizeof (msg) - 1));
36     interrupted = 1;
37 }
38
39 typedef struct {
40     const char *tag;
41     notmuch_bool_t remove;
42 } tag_operation_t;
43
44 static char *
45 _optimize_tag_query (void *ctx, const char *orig_query_string,
46                      const tag_operation_t *tag_ops)
47 {
48     /* This is subtler than it looks.  Xapian ignores the '-' operator
49      * at the beginning both queries and parenthesized groups and,
50      * furthermore, the presence of a '-' operator at the beginning of
51      * a group can inhibit parsing of the previous operator.  Hence,
52      * the user-provided query MUST appear first, but it is safe to
53      * parenthesize and the exclusion part of the query must not use
54      * the '-' operator (though the NOT operator is fine). */
55
56     char *escaped = NULL;
57     size_t escaped_len = 0;
58     char *query_string;
59     const char *join = "";
60     size_t i;
61
62     /* Don't optimize if there are no tag changes. */
63     if (tag_ops[0].tag == NULL)
64         return talloc_strdup (ctx, orig_query_string);
65
66     /* Build the new query string */
67     if (strcmp (orig_query_string, "*") == 0)
68         query_string = talloc_strdup (ctx, "(");
69     else
70         query_string = talloc_asprintf (ctx, "( %s ) and (", orig_query_string);
71
72     for (i = 0; tag_ops[i].tag && query_string; i++) {
73         /* XXX in case of OOM, query_string will be deallocated when
74          * ctx is, which might be at shutdown */
75         if (make_boolean_term (ctx,
76                                "tag", tag_ops[i].tag,
77                                &escaped, &escaped_len))
78             return NULL;
79
80         query_string = talloc_asprintf_append_buffer (
81             query_string, "%s%s%s", join,
82             tag_ops[i].remove ? "" : "not ",
83             escaped);
84         join = " or ";
85     }
86
87     if (query_string)
88         query_string = talloc_strdup_append_buffer (query_string, ")");
89
90     talloc_free (escaped);
91     return query_string;
92 }
93
94 /* Tag messages matching 'query_string' according to 'tag_ops', which
95  * must be an array of tagging operations terminated with an empty
96  * element. */
97 static int
98 tag_query (void *ctx, notmuch_database_t *notmuch, const char *query_string,
99            tag_operation_t *tag_ops, notmuch_bool_t synchronize_flags)
100 {
101     notmuch_query_t *query;
102     notmuch_messages_t *messages;
103     notmuch_message_t *message;
104     int i;
105
106     /* Optimize the query so it excludes messages that already have
107      * the specified set of tags. */
108     query_string = _optimize_tag_query (ctx, query_string, tag_ops);
109     if (query_string == NULL) {
110         fprintf (stderr, "Out of memory.\n");
111         return 1;
112     }
113
114     query = notmuch_query_create (notmuch, query_string);
115     if (query == NULL) {
116         fprintf (stderr, "Out of memory.\n");
117         return 1;
118     }
119
120     /* tagging is not interested in any special sort order */
121     notmuch_query_set_sort (query, NOTMUCH_SORT_UNSORTED);
122
123     for (messages = notmuch_query_search_messages (query);
124          notmuch_messages_valid (messages) && ! interrupted;
125          notmuch_messages_move_to_next (messages)) {
126         message = notmuch_messages_get (messages);
127
128         notmuch_message_freeze (message);
129
130         for (i = 0; tag_ops[i].tag; i++) {
131             if (tag_ops[i].remove)
132                 notmuch_message_remove_tag (message, tag_ops[i].tag);
133             else
134                 notmuch_message_add_tag (message, tag_ops[i].tag);
135         }
136
137         notmuch_message_thaw (message);
138
139         if (synchronize_flags)
140             notmuch_message_tags_to_maildir_flags (message);
141
142         notmuch_message_destroy (message);
143     }
144
145     notmuch_query_destroy (query);
146
147     return interrupted;
148 }
149
150 int
151 notmuch_tag_command (void *ctx, int argc, char *argv[])
152 {
153     tag_operation_t *tag_ops;
154     int tag_ops_count = 0;
155     char *query_string;
156     notmuch_config_t *config;
157     notmuch_database_t *notmuch;
158     struct sigaction action;
159     notmuch_bool_t synchronize_flags;
160     int i;
161     int ret;
162
163     /* Setup our handler for SIGINT */
164     memset (&action, 0, sizeof (struct sigaction));
165     action.sa_handler = handle_sigint;
166     sigemptyset (&action.sa_mask);
167     action.sa_flags = SA_RESTART;
168     sigaction (SIGINT, &action, NULL);
169
170     argc--; argv++; /* skip subcommand argument */
171
172     /* Array of tagging operations (add or remove), terminated with an
173      * empty element. */
174     tag_ops = talloc_array (ctx, tag_operation_t, argc + 1);
175     if (tag_ops == NULL) {
176         fprintf (stderr, "Out of memory.\n");
177         return 1;
178     }
179
180     for (i = 0; i < argc; i++) {
181         if (strcmp (argv[i], "--") == 0) {
182             i++;
183             break;
184         }
185         if (argv[i][0] == '+' || argv[i][0] == '-') {
186             if (argv[i][0] == '+' && argv[i][1] == '\0') {
187                 fprintf (stderr, "Error: tag names cannot be empty.\n");
188                 return 1;
189             }
190             if (argv[i][0] == '+' && argv[i][1] == '-') {
191                 /* This disallows adding the non-removable tag "-" and
192                  * enables notmuch tag to take long options in the
193                  * future. */
194                 fprintf (stderr, "Error: tag names must not start with '-'.\n");
195                 return 1;
196             }
197             tag_ops[tag_ops_count].tag = argv[i] + 1;
198             tag_ops[tag_ops_count].remove = (argv[i][0] == '-');
199             tag_ops_count++;
200         } else {
201             break;
202         }
203     }
204
205     tag_ops[tag_ops_count].tag = NULL;
206
207     if (tag_ops_count == 0) {
208         fprintf (stderr, "Error: 'notmuch tag' requires at least one tag to add or remove.\n");
209         return 1;
210     }
211
212     query_string = query_string_from_args (ctx, argc - i, &argv[i]);
213
214     if (*query_string == '\0') {
215         fprintf (stderr, "Error: notmuch tag requires at least one search term.\n");
216         return 1;
217     }
218
219     config = notmuch_config_open (ctx, NULL, NULL);
220     if (config == NULL)
221         return 1;
222
223     if (notmuch_database_open (notmuch_config_get_database_path (config),
224                                NOTMUCH_DATABASE_MODE_READ_WRITE, &notmuch))
225         return 1;
226
227     synchronize_flags = notmuch_config_get_maildir_synchronize_flags (config);
228
229     ret = tag_query (ctx, notmuch, query_string, tag_ops, synchronize_flags);
230
231     notmuch_database_destroy (notmuch);
232
233     return ret;
234 }