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