1 /* notmuch - Not much of an email program, (just index and search)
3 * Copyright © 2009 Carl Worth
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see http://www.gnu.org/licenses/ .
18 * Author: Carl Worth <cworth@cworth.org>
21 #include "notmuch-client.h"
24 notmuch_tag_command (void *ctx, unused (int argc), unused (char *argv[]))
27 int *add_tags, *remove_tags;
28 int add_tags_count = 0;
29 int remove_tags_count = 0;
31 notmuch_database_t *notmuch = NULL;
32 notmuch_query_t *query;
33 notmuch_messages_t *messages;
34 notmuch_message_t *message;
38 local = talloc_new (ctx);
44 add_tags = talloc_size (local, argc * sizeof (int));
45 if (add_tags == NULL) {
50 remove_tags = talloc_size (local, argc * sizeof (int));
51 if (remove_tags == NULL) {
56 for (i = 0; i < argc; i++) {
57 if (strcmp (argv[i], "--") == 0) {
61 if (argv[i][0] == '+') {
62 add_tags[add_tags_count++] = i;
63 } else if (argv[i][0] == '-') {
64 remove_tags[remove_tags_count++] = i;
70 if (add_tags_count == 0 && remove_tags_count == 0) {
71 fprintf (stderr, "Error: 'notmuch tag' requires at least one tag to add or remove.\n");
77 fprintf (stderr, "Error: 'notmuch tag' requires at least one search term.\n");
82 notmuch = notmuch_database_open (NULL);
83 if (notmuch == NULL) {
88 query_string = query_string_from_args (local, argc - i, &argv[i]);
90 query = notmuch_query_create (notmuch, query_string);
92 fprintf (stderr, "Out of memory.\n");
97 for (messages = notmuch_query_search_messages (query);
98 notmuch_messages_has_more (messages);
99 notmuch_messages_advance (messages))
101 message = notmuch_messages_get (messages);
103 notmuch_message_freeze (message);
105 for (i = 0; i < remove_tags_count; i++)
106 notmuch_message_remove_tag (message,
107 argv[remove_tags[i]] + 1);
109 for (i = 0; i < add_tags_count; i++)
110 notmuch_message_add_tag (message, argv[add_tags[i]] + 1);
112 notmuch_message_thaw (message);
114 notmuch_message_destroy (message);
117 notmuch_query_destroy (query);
121 notmuch_database_close (notmuch);