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