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