]> git.notmuchmail.org Git - notmuch/blob - tag-util.c
fix line breaks in one comment paragraph in generated .notmuch-config file
[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     tag_parse_status_t ret = TAG_PARSE_SUCCESS;
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 TAG_PARSE_OUT_OF_MEMORY;
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     *query_string = tok;
128
129   DONE:
130     talloc_free (line_for_error);
131     return ret;
132 }
133
134 static inline void
135 message_error (notmuch_message_t *message,
136                notmuch_status_t status,
137                const char *format, ...)
138 {
139     va_list va_args;
140
141     va_start (va_args, format);
142
143     vfprintf (stderr, format, va_args);
144     fprintf (stderr, "Message-ID: %s\n", notmuch_message_get_message_id (message));
145     fprintf (stderr, "Status: %s\n", notmuch_status_to_string (status));
146 }
147
148 static int
149 makes_changes (notmuch_message_t *message,
150                tag_op_list_t *list,
151                tag_op_flag_t flags)
152 {
153
154     size_t i;
155
156     notmuch_tags_t *tags;
157     notmuch_bool_t changes = FALSE;
158
159     /* First, do we delete an existing tag? */
160     changes = FALSE;
161     for (tags = notmuch_message_get_tags (message);
162          ! changes && notmuch_tags_valid (tags);
163          notmuch_tags_move_to_next (tags)) {
164         const char *cur_tag = notmuch_tags_get (tags);
165         int last_op =  (flags & TAG_FLAG_REMOVE_ALL) ? -1 : 0;
166
167         /* scan backwards to get last operation */
168         i = list->count;
169         while (i > 0) {
170             i--;
171             if (strcmp (cur_tag, list->ops[i].tag) == 0) {
172                 last_op = list->ops[i].remove ? -1 : 1;
173                 break;
174             }
175         }
176
177         changes = (last_op == -1);
178     }
179     notmuch_tags_destroy (tags);
180
181     if (changes)
182         return TRUE;
183
184     /* Now check for adding new tags */
185     for (i = 0; i < list->count; i++) {
186         notmuch_bool_t exists = FALSE;
187
188         if (list->ops[i].remove)
189             continue;
190
191         for (tags = notmuch_message_get_tags (message);
192              notmuch_tags_valid (tags);
193              notmuch_tags_move_to_next (tags)) {
194             const char *cur_tag = notmuch_tags_get (tags);
195             if (strcmp (cur_tag, list->ops[i].tag) == 0) {
196                 exists = TRUE;
197                 break;
198             }
199         }
200         notmuch_tags_destroy (tags);
201
202         /* the following test is conservative,
203          * in the sense it ignores cases like +foo ... -foo
204          * but this is OK from a correctness point of view
205          */
206         if (! exists)
207             return TRUE;
208     }
209     return FALSE;
210
211 }
212
213 notmuch_status_t
214 tag_op_list_apply (notmuch_message_t *message,
215                    tag_op_list_t *list,
216                    tag_op_flag_t flags)
217 {
218     size_t i;
219     notmuch_status_t status = 0;
220     tag_operation_t *tag_ops = list->ops;
221
222     if (! (flags & TAG_FLAG_PRE_OPTIMIZED) && ! makes_changes (message, list, flags))
223         return NOTMUCH_STATUS_SUCCESS;
224
225     status = notmuch_message_freeze (message);
226     if (status) {
227         message_error (message, status, "freezing message");
228         return status;
229     }
230
231     if (flags & TAG_FLAG_REMOVE_ALL) {
232         status = notmuch_message_remove_all_tags (message);
233         if (status) {
234             message_error (message, status, "removing all tags");
235             return status;
236         }
237     }
238
239     for (i = 0; i < list->count; i++) {
240         if (tag_ops[i].remove) {
241             status = notmuch_message_remove_tag (message, tag_ops[i].tag);
242             if (status) {
243                 message_error (message, status, "removing tag %s", tag_ops[i].tag);
244                 return status;
245             }
246         } else {
247             status = notmuch_message_add_tag (message, tag_ops[i].tag);
248             if (status) {
249                 message_error (message, status, "adding tag %s", tag_ops[i].tag);
250                 return status;
251             }
252
253         }
254     }
255
256     status = notmuch_message_thaw (message);
257     if (status) {
258         message_error (message, status, "thawing message");
259         return status;
260     }
261
262
263     if (flags & TAG_FLAG_MAILDIR_SYNC) {
264         status = notmuch_message_tags_to_maildir_flags (message);
265         if (status) {
266             message_error (message, status, "synching tags to maildir");
267             return status;
268         }
269     }
270
271     return NOTMUCH_STATUS_SUCCESS;
272
273 }
274
275
276 /* Array of tagging operations (add or remove.  Size will be increased
277  * as necessary. */
278
279 tag_op_list_t *
280 tag_op_list_create (void *ctx)
281 {
282     tag_op_list_t *list;
283
284     list = talloc (ctx, tag_op_list_t);
285     if (list == NULL)
286         return NULL;
287
288     list->size = TAG_OP_LIST_INITIAL_SIZE;
289     list->count = 0;
290
291     list->ops = talloc_array (list, tag_operation_t, list->size);
292     if (list->ops == NULL)
293         return NULL;
294
295     return list;
296 }
297
298
299 int
300 tag_op_list_append (tag_op_list_t *list,
301                     const char *tag,
302                     notmuch_bool_t remove)
303 {
304     /* Make room if current array is full.  This should be a fairly
305      * rare case, considering the initial array size.
306      */
307
308     if (list->count == list->size) {
309         list->size *= 2;
310         list->ops = talloc_realloc (list, list->ops, tag_operation_t,
311                                     list->size);
312         if (list->ops == NULL) {
313             fprintf (stderr, "Out of memory.\n");
314             return 1;
315         }
316     }
317
318     /* add the new operation */
319
320     list->ops[list->count].tag = tag;
321     list->ops[list->count].remove = remove;
322     list->count++;
323     return 0;
324 }
325
326 /*
327  *   Is the i'th tag operation a remove?
328  */
329
330 notmuch_bool_t
331 tag_op_list_isremove (const tag_op_list_t *list, size_t i)
332 {
333     assert (i < list->count);
334     return list->ops[i].remove;
335 }
336
337 /*
338  * Reset a list to contain no operations
339  */
340
341 void
342 tag_op_list_reset (tag_op_list_t *list)
343 {
344     list->count = 0;
345 }
346
347 /*
348  * Return the number of operations in a list
349  */
350
351 size_t
352 tag_op_list_size (const tag_op_list_t *list)
353 {
354     return list->count;
355 }
356
357 /*
358  *   return the i'th tag in the list
359  */
360
361 const char *
362 tag_op_list_tag (const tag_op_list_t *list, size_t i)
363 {
364     assert (i < list->count);
365     return list->ops[i].tag;
366 }