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