]> git.notmuchmail.org Git - notmuch/blob - notmuch-tag.c
notmuch reply: Process headers a bit more accurately.
[notmuch] / notmuch-tag.c
1 /* notmuch - Not much of an email program, (just index and search)
2  *
3  * Copyright © 2009 Carl Worth
4  *
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.
9  *
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.
14  *
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/ .
17  *
18  * Author: Carl Worth <cworth@cworth.org>
19  */
20
21 #include "notmuch-client.h"
22
23 int
24 notmuch_tag_command (void *ctx, unused (int argc), unused (char *argv[]))
25 {
26     void *local;
27     int *add_tags, *remove_tags;
28     int add_tags_count = 0;
29     int remove_tags_count = 0;
30     char *query_string;
31     notmuch_database_t *notmuch = NULL;
32     notmuch_query_t *query;
33     notmuch_messages_t *messages;
34     notmuch_message_t *message;
35     int ret = 0;
36     int i;
37
38     local = talloc_new (ctx);
39     if (local == NULL) {
40         ret = 1;
41         goto DONE;
42     }
43
44     add_tags = talloc_size (local, argc * sizeof (int));
45     if (add_tags == NULL) {
46         ret = 1;
47         goto DONE;
48     }
49
50     remove_tags = talloc_size (local, argc * sizeof (int));
51     if (remove_tags == NULL) {
52         ret = 1;
53         goto DONE;
54     }
55
56     for (i = 0; i < argc; i++) {
57         if (strcmp (argv[i], "--") == 0) {
58             i++;
59             break;
60         }
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;
65         } else {
66             break;
67         }
68     }
69
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");
72         ret = 1;
73         goto DONE;
74     }
75
76     if (i == argc) {
77         fprintf (stderr, "Error: 'notmuch tag' requires at least one search term.\n");
78         ret = 1;
79         goto DONE;
80     }
81
82     notmuch = notmuch_database_open (NULL);
83     if (notmuch == NULL) {
84         ret = 1;
85         goto DONE;
86     }
87
88     query_string = query_string_from_args (local, argc - i, &argv[i]);
89
90     query = notmuch_query_create (notmuch, query_string);
91     if (query == NULL) {
92         fprintf (stderr, "Out of memory.\n");
93         ret = 1;
94         goto DONE;
95     }
96
97     for (messages = notmuch_query_search_messages (query);
98          notmuch_messages_has_more (messages);
99          notmuch_messages_advance (messages))
100     {
101         message = notmuch_messages_get (messages);
102
103         notmuch_message_freeze (message);
104
105         for (i = 0; i < remove_tags_count; i++)
106             notmuch_message_remove_tag (message,
107                                         argv[remove_tags[i]] + 1);
108
109         for (i = 0; i < add_tags_count; i++)
110             notmuch_message_add_tag (message, argv[add_tags[i]] + 1);
111
112         notmuch_message_thaw (message);
113
114         notmuch_message_destroy (message);
115     }
116
117     notmuch_query_destroy (query);
118
119   DONE:
120     if (notmuch)
121         notmuch_database_close (notmuch);
122
123     talloc_free (local);
124
125     return ret;
126 }