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