]> git.cworth.org Git - notmuch/blob - notmuch-tag.c
Silence buildbot warnings about unused results
[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 static volatile sig_atomic_t interrupted;
24
25 static void
26 handle_sigint (unused (int sig))
27 {
28     static char msg[] = "Stopping...         \n";
29
30     /* This write is "opportunistic", so it's okay to ignore the
31      * result.  It is not required for correctness, and if it does
32      * fail or produce a short write, we want to get out of the signal
33      * handler as quickly as possible, not retry it. */
34     IGNORE_RESULT (write (2, msg, sizeof(msg)-1));
35     interrupted = 1;
36 }
37
38 static char *
39 _escape_tag (char *buf, const char *tag)
40 {
41     const char *in = tag;
42     char *out = buf;
43     /* Boolean terms surrounded by double quotes can contain any
44      * character.  Double quotes are quoted by doubling them. */
45     *out++ = '"';
46     while (*in) {
47         if (*in == '"')
48             *out++ = '"';
49         *out++ = *in++;
50     }
51     *out++ = '"';
52     *out = 0;
53     return buf;
54 }
55
56 static char *
57 _optimize_tag_query (void *ctx, const char *orig_query_string, char *argv[],
58                      int *add_tags, int add_tags_count,
59                      int *remove_tags, int remove_tags_count)
60 {
61     /* This is subtler than it looks.  Xapian ignores the '-' operator
62      * at the beginning both queries and parenthesized groups and,
63      * furthermore, the presence of a '-' operator at the beginning of
64      * a group can inhibit parsing of the previous operator.  Hence,
65      * the user-provided query MUST appear first, but it is safe to
66      * parenthesize and the exclusion part of the query must not use
67      * the '-' operator (though the NOT operator is fine). */
68
69     char *escaped, *query_string;
70     const char *join = "";
71     int i;
72     unsigned int max_tag_len = 0;
73
74     /* Allocate a buffer for escaping tags.  This is large enough to
75      * hold a fully escaped tag with every character doubled plus
76      * enclosing quotes and a NUL. */
77     for (i = 0; i < add_tags_count; i++)
78         if (strlen (argv[add_tags[i]] + 1) > max_tag_len)
79             max_tag_len = strlen (argv[add_tags[i]] + 1);
80     for (i = 0; i < remove_tags_count; i++)
81         if (strlen (argv[remove_tags[i]] + 1) > max_tag_len)
82             max_tag_len = strlen (argv[remove_tags[i]] + 1);
83     escaped = talloc_array(ctx, char, max_tag_len * 2 + 3);
84     if (!escaped)
85         return NULL;
86
87     /* Build the new query string */
88     if (strcmp (orig_query_string, "*") == 0)
89         query_string = talloc_strdup (ctx, "(");
90     else
91         query_string = talloc_asprintf (ctx, "( %s ) and (", orig_query_string);
92
93     for (i = 0; i < add_tags_count && query_string; i++) {
94         query_string = talloc_asprintf_append_buffer (
95             query_string, "%snot tag:%s", join,
96             _escape_tag (escaped, argv[add_tags[i]] + 1));
97         join = " or ";
98     }
99     for (i = 0; i < remove_tags_count && query_string; i++) {
100         query_string = talloc_asprintf_append_buffer (
101             query_string, "%stag:%s", join,
102             _escape_tag (escaped, argv[remove_tags[i]] + 1));
103         join = " or ";
104     }
105
106     if (query_string)
107         query_string = talloc_strdup_append_buffer (query_string, ")");
108
109     talloc_free (escaped);
110     return query_string;
111 }
112
113 int
114 notmuch_tag_command (void *ctx, unused (int argc), unused (char *argv[]))
115 {
116     int *add_tags, *remove_tags;
117     int add_tags_count = 0;
118     int remove_tags_count = 0;
119     char *query_string;
120     notmuch_config_t *config;
121     notmuch_database_t *notmuch;
122     notmuch_query_t *query;
123     notmuch_messages_t *messages;
124     notmuch_message_t *message;
125     struct sigaction action;
126     notmuch_bool_t synchronize_flags;
127     int i;
128
129     /* Setup our handler for SIGINT */
130     memset (&action, 0, sizeof (struct sigaction));
131     action.sa_handler = handle_sigint;
132     sigemptyset (&action.sa_mask);
133     action.sa_flags = SA_RESTART;
134     sigaction (SIGINT, &action, NULL);
135
136     add_tags = talloc_size (ctx, argc * sizeof (int));
137     if (add_tags == NULL) {
138         fprintf (stderr, "Out of memory.\n");
139         return 1;
140     }
141
142     remove_tags = talloc_size (ctx, argc * sizeof (int));
143     if (remove_tags == NULL) {
144         fprintf (stderr, "Out of memory.\n");
145         return 1;
146     }
147
148     argc--; argv++; /* skip subcommand argument */
149
150     for (i = 0; i < argc; i++) {
151         if (strcmp (argv[i], "--") == 0) {
152             i++;
153             break;
154         }
155         if (argv[i][0] == '+') {
156             add_tags[add_tags_count++] = i;
157         } else if (argv[i][0] == '-') {
158             remove_tags[remove_tags_count++] = i;
159         } else {
160             break;
161         }
162     }
163
164     if (add_tags_count == 0 && remove_tags_count == 0) {
165         fprintf (stderr, "Error: 'notmuch tag' requires at least one tag to add or remove.\n");
166         return 1;
167     }
168
169     query_string = query_string_from_args (ctx, argc - i, &argv[i]);
170
171     if (*query_string == '\0') {
172         fprintf (stderr, "Error: notmuch tag requires at least one search term.\n");
173         return 1;
174     }
175
176     /* Optimize the query so it excludes messages that already have
177      * the specified set of tags. */
178     query_string = _optimize_tag_query (ctx, query_string, argv,
179                                         add_tags, add_tags_count,
180                                         remove_tags, remove_tags_count);
181     if (query_string == NULL) {
182         fprintf (stderr, "Out of memory.\n");
183         return 1;
184     }
185
186     config = notmuch_config_open (ctx, NULL, NULL);
187     if (config == NULL)
188         return 1;
189
190     notmuch = notmuch_database_open (notmuch_config_get_database_path (config),
191                                      NOTMUCH_DATABASE_MODE_READ_WRITE);
192     if (notmuch == NULL)
193         return 1;
194
195     synchronize_flags = notmuch_config_get_maildir_synchronize_flags (config);
196
197     query = notmuch_query_create (notmuch, query_string);
198     if (query == NULL) {
199         fprintf (stderr, "Out of memory.\n");
200         return 1;
201     }
202
203     /* tagging is not interested in any special sort order */
204     notmuch_query_set_sort (query, NOTMUCH_SORT_UNSORTED);
205
206     for (messages = notmuch_query_search_messages (query);
207          notmuch_messages_valid (messages) && !interrupted;
208          notmuch_messages_move_to_next (messages))
209     {
210         message = notmuch_messages_get (messages);
211
212         notmuch_message_freeze (message);
213
214         for (i = 0; i < remove_tags_count; i++)
215             notmuch_message_remove_tag (message,
216                                         argv[remove_tags[i]] + 1);
217
218         for (i = 0; i < add_tags_count; i++)
219             notmuch_message_add_tag (message, argv[add_tags[i]] + 1);
220
221         notmuch_message_thaw (message);
222
223         if (synchronize_flags)
224             notmuch_message_tags_to_maildir_flags (message);
225
226         notmuch_message_destroy (message);
227     }
228
229     notmuch_query_destroy (query);
230     notmuch_database_close (notmuch);
231
232     return interrupted;
233 }