]> git.notmuchmail.org Git - notmuch/blob - tag-util.c
7091d294beae1cf9c0150377d867604d0fd2d45e
[notmuch] / tag-util.c
1 #include <assert.h>
2 #include "string-util.h"
3 #include "tag-util.h"
4 #include "hex-escape.h"
5
6 #define TAG_OP_LIST_INITIAL_SIZE 10
7
8 struct _tag_operation_t {
9     const char *tag;
10     notmuch_bool_t remove;
11 };
12
13 struct _tag_op_list_t {
14     tag_operation_t *ops;
15     size_t count;
16     size_t size;
17 };
18
19 static tag_parse_status_t
20 line_error (tag_parse_status_t status,
21             const char *line,
22             const char *format, ...)
23 {
24     va_list va_args;
25
26     va_start (va_args, format);
27
28     fprintf (stderr, status < 0 ? "Error: " : "Warning: ");
29     vfprintf (stderr, format, va_args);
30     fprintf (stderr, " [%s]\n", line);
31     return status;
32 }
33
34 const char *
35 illegal_tag (const char *tag, notmuch_bool_t remove)
36 {
37     if (*tag == '\0' && ! remove)
38         return "empty tag forbidden";
39
40     /* This disallows adding tags starting with "-", in particular the
41      * non-removable tag "-" and enables notmuch tag to take long
42      * options more easily.
43      */
44
45     if (*tag == '-' && ! remove)
46         return "tag starting with '-' forbidden";
47
48     return NULL;
49 }
50
51 tag_parse_status_t
52 parse_tag_line (void *ctx, char *line,
53                 tag_op_flag_t flags,
54                 char **query_string,
55                 tag_op_list_t *tag_ops)
56 {
57     char *tok = line;
58     size_t tok_len = 0;
59     char *line_for_error;
60     tag_parse_status_t ret = TAG_PARSE_SUCCESS;
61
62     chomp_newline (line);
63
64     line_for_error = talloc_strdup (ctx, line);
65     if (line_for_error == NULL) {
66         fprintf (stderr, "Error: out of memory\n");
67         return TAG_PARSE_OUT_OF_MEMORY;
68     }
69
70     /* remove leading space */
71     while (*tok == ' ' || *tok == '\t')
72         tok++;
73
74     /* Skip empty and comment lines. */
75     if (*tok == '\0' || *tok == '#') {
76         ret = TAG_PARSE_SKIPPED;
77         goto DONE;
78     }
79
80     tag_op_list_reset (tag_ops);
81
82     /* Parse tags. */
83     while ((tok = strtok_len (tok + tok_len, " ", &tok_len)) != NULL) {
84         notmuch_bool_t remove;
85         char *tag;
86
87         /* Optional explicit end of tags marker. */
88         if (tok_len == 2 && strncmp (tok, "--", tok_len) == 0) {
89             tok = strtok_len (tok + tok_len, " ", &tok_len);
90             if (tok == NULL) {
91                 ret = line_error (TAG_PARSE_INVALID, line_for_error,
92                                   "no query string after --");
93                 goto DONE;
94             }
95             break;
96         }
97
98         /* Implicit end of tags. */
99         if (*tok != '-' && *tok != '+')
100             break;
101
102         /* If tag is terminated by NUL, there's no query string. */
103         if (*(tok + tok_len) == '\0') {
104             ret = line_error (TAG_PARSE_INVALID, line_for_error,
105                               "no query string");
106             goto DONE;
107         }
108
109         /* Terminate, and start next token after terminator. */
110         *(tok + tok_len++) = '\0';
111
112         remove = (*tok == '-');
113         tag = tok + 1;
114
115         /* Maybe refuse illegal tags. */
116         if (! (flags & TAG_FLAG_BE_GENEROUS)) {
117             const char *msg = illegal_tag (tag, remove);
118             if (msg) {
119                 ret = line_error (TAG_PARSE_INVALID, line_for_error, msg);
120                 goto DONE;
121             }
122         }
123
124         /* Decode tag. */
125         if (hex_decode_inplace (tag) != HEX_SUCCESS) {
126             ret = line_error (TAG_PARSE_INVALID, line_for_error,
127                               "hex decoding of tag %s failed", tag);
128             goto DONE;
129         }
130
131         if (tag_op_list_append (tag_ops, tag, remove)) {
132             ret = line_error (TAG_PARSE_OUT_OF_MEMORY, line_for_error,
133                               "aborting");
134             goto DONE;
135         }
136     }
137
138     if (tok == NULL) {
139         /* use a different error message for testing */
140         ret = line_error (TAG_PARSE_INVALID, line_for_error,
141                           "missing query string");
142         goto DONE;
143     }
144
145     /* tok now points to the query string */
146     *query_string = tok;
147
148   DONE:
149     talloc_free (line_for_error);
150     return ret;
151 }
152
153 tag_parse_status_t
154 parse_tag_command_line (void *ctx, int argc, char **argv,
155                         char **query_str, tag_op_list_t *tag_ops)
156 {
157     int i;
158
159     for (i = 0; i < argc; i++) {
160         if (strcmp (argv[i], "--") == 0) {
161             i++;
162             break;
163         }
164
165         if (argv[i][0] != '+' && argv[i][0] != '-')
166             break;
167
168         notmuch_bool_t is_remove = argv[i][0] == '-';
169         const char *msg;
170
171         msg = illegal_tag (argv[i] + 1, is_remove);
172         if (msg) {
173             fprintf (stderr, "Error: %s\n", msg);
174             return TAG_PARSE_INVALID;
175         }
176
177         tag_op_list_append (tag_ops, argv[i] + 1, is_remove);
178     }
179
180     *query_str = query_string_from_args (ctx, argc - i, &argv[i]);
181
182     if (*query_str == NULL) {
183         fprintf (stderr, "Out of memory.\n");
184         return TAG_PARSE_OUT_OF_MEMORY;
185     }
186
187     return TAG_PARSE_SUCCESS;
188 }
189
190
191 static inline void
192 message_error (notmuch_message_t *message,
193                notmuch_status_t status,
194                const char *format, ...)
195 {
196     va_list va_args;
197
198     va_start (va_args, format);
199
200     vfprintf (stderr, format, va_args);
201     fprintf (stderr, "Message-ID: %s\n", notmuch_message_get_message_id (message));
202     fprintf (stderr, "Status: %s\n", notmuch_status_to_string (status));
203 }
204
205 static int
206 makes_changes (notmuch_message_t *message,
207                tag_op_list_t *list,
208                tag_op_flag_t flags)
209 {
210     size_t i;
211
212     notmuch_tags_t *tags;
213     notmuch_bool_t changes = FALSE;
214
215     /* First, do we delete an existing tag? */
216     changes = FALSE;
217     for (tags = notmuch_message_get_tags (message);
218          ! changes && notmuch_tags_valid (tags);
219          notmuch_tags_move_to_next (tags)) {
220         const char *cur_tag = notmuch_tags_get (tags);
221         int last_op =  (flags & TAG_FLAG_REMOVE_ALL) ? -1 : 0;
222
223         /* scan backwards to get last operation */
224         i = list->count;
225         while (i > 0) {
226             i--;
227             if (strcmp (cur_tag, list->ops[i].tag) == 0) {
228                 last_op = list->ops[i].remove ? -1 : 1;
229                 break;
230             }
231         }
232
233         changes = (last_op == -1);
234     }
235     notmuch_tags_destroy (tags);
236
237     if (changes)
238         return TRUE;
239
240     /* Now check for adding new tags */
241     for (i = 0; i < list->count; i++) {
242         notmuch_bool_t exists = FALSE;
243
244         if (list->ops[i].remove)
245             continue;
246
247         for (tags = notmuch_message_get_tags (message);
248              notmuch_tags_valid (tags);
249              notmuch_tags_move_to_next (tags)) {
250             const char *cur_tag = notmuch_tags_get (tags);
251             if (strcmp (cur_tag, list->ops[i].tag) == 0) {
252                 exists = TRUE;
253                 break;
254             }
255         }
256         notmuch_tags_destroy (tags);
257
258         /* the following test is conservative,
259          * in the sense it ignores cases like +foo ... -foo
260          * but this is OK from a correctness point of view
261          */
262         if (! exists)
263             return TRUE;
264     }
265     return FALSE;
266
267 }
268
269 notmuch_status_t
270 tag_op_list_apply (notmuch_message_t *message,
271                    tag_op_list_t *list,
272                    tag_op_flag_t flags)
273 {
274     size_t i;
275     notmuch_status_t status = 0;
276     tag_operation_t *tag_ops = list->ops;
277
278     if (! (flags & TAG_FLAG_PRE_OPTIMIZED) && ! makes_changes (message, list, flags))
279         return NOTMUCH_STATUS_SUCCESS;
280
281     status = notmuch_message_freeze (message);
282     if (status) {
283         message_error (message, status, "freezing message");
284         return status;
285     }
286
287     if (flags & TAG_FLAG_REMOVE_ALL) {
288         status = notmuch_message_remove_all_tags (message);
289         if (status) {
290             message_error (message, status, "removing all tags");
291             return status;
292         }
293     }
294
295     for (i = 0; i < list->count; i++) {
296         if (tag_ops[i].remove) {
297             status = notmuch_message_remove_tag (message, tag_ops[i].tag);
298             if (status) {
299                 message_error (message, status, "removing tag %s", tag_ops[i].tag);
300                 return status;
301             }
302         } else {
303             status = notmuch_message_add_tag (message, tag_ops[i].tag);
304             if (status) {
305                 message_error (message, status, "adding tag %s", tag_ops[i].tag);
306                 return status;
307             }
308
309         }
310     }
311
312     status = notmuch_message_thaw (message);
313     if (status) {
314         message_error (message, status, "thawing message");
315         return status;
316     }
317
318
319     if (flags & TAG_FLAG_MAILDIR_SYNC) {
320         status = notmuch_message_tags_to_maildir_flags (message);
321         if (status) {
322             message_error (message, status, "synching tags to maildir");
323             return status;
324         }
325     }
326
327     return NOTMUCH_STATUS_SUCCESS;
328
329 }
330
331
332 /* Array of tagging operations (add or remove.  Size will be increased
333  * as necessary. */
334
335 tag_op_list_t *
336 tag_op_list_create (void *ctx)
337 {
338     tag_op_list_t *list;
339
340     list = talloc (ctx, tag_op_list_t);
341     if (list == NULL)
342         return NULL;
343
344     list->size = TAG_OP_LIST_INITIAL_SIZE;
345     list->count = 0;
346
347     list->ops = talloc_array (list, tag_operation_t, list->size);
348     if (list->ops == NULL)
349         return NULL;
350
351     return list;
352 }
353
354
355 int
356 tag_op_list_append (tag_op_list_t *list,
357                     const char *tag,
358                     notmuch_bool_t remove)
359 {
360     /* Make room if current array is full.  This should be a fairly
361      * rare case, considering the initial array size.
362      */
363
364     if (list->count == list->size) {
365         list->size *= 2;
366         list->ops = talloc_realloc (list, list->ops, tag_operation_t,
367                                     list->size);
368         if (list->ops == NULL) {
369             fprintf (stderr, "Out of memory.\n");
370             return 1;
371         }
372     }
373
374     /* add the new operation */
375
376     list->ops[list->count].tag = tag;
377     list->ops[list->count].remove = remove;
378     list->count++;
379     return 0;
380 }
381
382 /*
383  *   Is the i'th tag operation a remove?
384  */
385
386 notmuch_bool_t
387 tag_op_list_isremove (const tag_op_list_t *list, size_t i)
388 {
389     assert (i < list->count);
390     return list->ops[i].remove;
391 }
392
393 /*
394  * Reset a list to contain no operations
395  */
396
397 void
398 tag_op_list_reset (tag_op_list_t *list)
399 {
400     list->count = 0;
401 }
402
403 /*
404  * Return the number of operations in a list
405  */
406
407 size_t
408 tag_op_list_size (const tag_op_list_t *list)
409 {
410     return list->count;
411 }
412
413 /*
414  *   return the i'th tag in the list
415  */
416
417 const char *
418 tag_op_list_tag (const tag_op_list_t *list, size_t i)
419 {
420     assert (i < list->count);
421     return list->ops[i].tag;
422 }