]> git.notmuchmail.org Git - notmuch/blob - tag-util.c
debian: package ruby bindings
[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     tag_op_list_reset (tag_ops);
169
170     for (i = 0; i < argc; i++) {
171         if (strcmp (argv[i], "--") == 0) {
172             i++;
173             break;
174         }
175
176         if (argv[i][0] != '+' && argv[i][0] != '-')
177             break;
178
179         notmuch_bool_t is_remove = argv[i][0] == '-';
180         const char *msg;
181
182         msg = illegal_tag (argv[i] + 1, is_remove);
183         if (msg) {
184             fprintf (stderr, "Error: %s", msg);
185             return TAG_PARSE_INVALID;
186         }
187
188         tag_op_list_append (tag_ops, argv[i] + 1, is_remove);
189     }
190
191     *query_str = query_string_from_args (ctx, argc - i, &argv[i]);
192
193     if (*query_str == NULL || **query_str == '\0') {
194         fprintf (stderr, "Error: notmuch tag requires at least one search term.\n");
195         return TAG_PARSE_INVALID;
196     }
197
198     return TAG_PARSE_SUCCESS;
199 }
200
201
202 static inline void
203 message_error (notmuch_message_t *message,
204                notmuch_status_t status,
205                const char *format, ...)
206 {
207     va_list va_args;
208
209     va_start (va_args, format);
210
211     vfprintf (stderr, format, va_args);
212     fprintf (stderr, "Message-ID: %s\n", notmuch_message_get_message_id (message));
213     fprintf (stderr, "Status: %s\n", notmuch_status_to_string (status));
214 }
215
216 static int
217 makes_changes (notmuch_message_t *message,
218                tag_op_list_t *list,
219                tag_op_flag_t flags)
220 {
221
222     size_t i;
223
224     notmuch_tags_t *tags;
225     notmuch_bool_t changes = FALSE;
226
227     /* First, do we delete an existing tag? */
228     changes = FALSE;
229     for (tags = notmuch_message_get_tags (message);
230          ! changes && notmuch_tags_valid (tags);
231          notmuch_tags_move_to_next (tags)) {
232         const char *cur_tag = notmuch_tags_get (tags);
233         int last_op =  (flags & TAG_FLAG_REMOVE_ALL) ? -1 : 0;
234
235         /* scan backwards to get last operation */
236         i = list->count;
237         while (i > 0) {
238             i--;
239             if (strcmp (cur_tag, list->ops[i].tag) == 0) {
240                 last_op = list->ops[i].remove ? -1 : 1;
241                 break;
242             }
243         }
244
245         changes = (last_op == -1);
246     }
247     notmuch_tags_destroy (tags);
248
249     if (changes)
250         return TRUE;
251
252     /* Now check for adding new tags */
253     for (i = 0; i < list->count; i++) {
254         notmuch_bool_t exists = FALSE;
255
256         if (list->ops[i].remove)
257             continue;
258
259         for (tags = notmuch_message_get_tags (message);
260              notmuch_tags_valid (tags);
261              notmuch_tags_move_to_next (tags)) {
262             const char *cur_tag = notmuch_tags_get (tags);
263             if (strcmp (cur_tag, list->ops[i].tag) == 0) {
264                 exists = TRUE;
265                 break;
266             }
267         }
268         notmuch_tags_destroy (tags);
269
270         /* the following test is conservative,
271          * in the sense it ignores cases like +foo ... -foo
272          * but this is OK from a correctness point of view
273          */
274         if (! exists)
275             return TRUE;
276     }
277     return FALSE;
278
279 }
280
281 notmuch_status_t
282 tag_op_list_apply (notmuch_message_t *message,
283                    tag_op_list_t *list,
284                    tag_op_flag_t flags)
285 {
286     size_t i;
287     notmuch_status_t status = 0;
288     tag_operation_t *tag_ops = list->ops;
289
290     if (! (flags & TAG_FLAG_PRE_OPTIMIZED) && ! makes_changes (message, list, flags))
291         return NOTMUCH_STATUS_SUCCESS;
292
293     status = notmuch_message_freeze (message);
294     if (status) {
295         message_error (message, status, "freezing message");
296         return status;
297     }
298
299     if (flags & TAG_FLAG_REMOVE_ALL) {
300         status = notmuch_message_remove_all_tags (message);
301         if (status) {
302             message_error (message, status, "removing all tags");
303             return status;
304         }
305     }
306
307     for (i = 0; i < list->count; i++) {
308         if (tag_ops[i].remove) {
309             status = notmuch_message_remove_tag (message, tag_ops[i].tag);
310             if (status) {
311                 message_error (message, status, "removing tag %s", tag_ops[i].tag);
312                 return status;
313             }
314         } else {
315             status = notmuch_message_add_tag (message, tag_ops[i].tag);
316             if (status) {
317                 message_error (message, status, "adding tag %s", tag_ops[i].tag);
318                 return status;
319             }
320
321         }
322     }
323
324     status = notmuch_message_thaw (message);
325     if (status) {
326         message_error (message, status, "thawing message");
327         return status;
328     }
329
330
331     if (flags & TAG_FLAG_MAILDIR_SYNC) {
332         status = notmuch_message_tags_to_maildir_flags (message);
333         if (status) {
334             message_error (message, status, "synching tags to maildir");
335             return status;
336         }
337     }
338
339     return NOTMUCH_STATUS_SUCCESS;
340
341 }
342
343
344 /* Array of tagging operations (add or remove.  Size will be increased
345  * as necessary. */
346
347 tag_op_list_t *
348 tag_op_list_create (void *ctx)
349 {
350     tag_op_list_t *list;
351
352     list = talloc (ctx, tag_op_list_t);
353     if (list == NULL)
354         return NULL;
355
356     list->size = TAG_OP_LIST_INITIAL_SIZE;
357     list->count = 0;
358
359     list->ops = talloc_array (list, tag_operation_t, list->size);
360     if (list->ops == NULL)
361         return NULL;
362
363     return list;
364 }
365
366
367 int
368 tag_op_list_append (tag_op_list_t *list,
369                     const char *tag,
370                     notmuch_bool_t remove)
371 {
372     /* Make room if current array is full.  This should be a fairly
373      * rare case, considering the initial array size.
374      */
375
376     if (list->count == list->size) {
377         list->size *= 2;
378         list->ops = talloc_realloc (list, list->ops, tag_operation_t,
379                                     list->size);
380         if (list->ops == NULL) {
381             fprintf (stderr, "Out of memory.\n");
382             return 1;
383         }
384     }
385
386     /* add the new operation */
387
388     list->ops[list->count].tag = tag;
389     list->ops[list->count].remove = remove;
390     list->count++;
391     return 0;
392 }
393
394 /*
395  *   Is the i'th tag operation a remove?
396  */
397
398 notmuch_bool_t
399 tag_op_list_isremove (const tag_op_list_t *list, size_t i)
400 {
401     assert (i < list->count);
402     return list->ops[i].remove;
403 }
404
405 /*
406  * Reset a list to contain no operations
407  */
408
409 void
410 tag_op_list_reset (tag_op_list_t *list)
411 {
412     list->count = 0;
413 }
414
415 /*
416  * Return the number of operations in a list
417  */
418
419 size_t
420 tag_op_list_size (const tag_op_list_t *list)
421 {
422     return list->count;
423 }
424
425 /*
426  *   return the i'th tag in the list
427  */
428
429 const char *
430 tag_op_list_tag (const tag_op_list_t *list, size_t i)
431 {
432     assert (i < list->count);
433     return list->ops[i].tag;
434 }