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