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