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"
22 #include "string-util.h"
24 static volatile sig_atomic_t interrupted;
27 handle_sigint (unused (int sig))
29 static char msg[] = "Stopping... \n";
31 /* This write is "opportunistic", so it's okay to ignore the
32 * result. It is not required for correctness, and if it does
33 * fail or produce a short write, we want to get out of the signal
34 * handler as quickly as possible, not retry it. */
35 IGNORE_RESULT (write (2, msg, sizeof (msg) - 1));
41 notmuch_bool_t remove;
45 _optimize_tag_query (void *ctx, const char *orig_query_string,
46 const tag_operation_t *tag_ops)
48 /* This is subtler than it looks. Xapian ignores the '-' operator
49 * at the beginning both queries and parenthesized groups and,
50 * furthermore, the presence of a '-' operator at the beginning of
51 * a group can inhibit parsing of the previous operator. Hence,
52 * the user-provided query MUST appear first, but it is safe to
53 * parenthesize and the exclusion part of the query must not use
54 * the '-' operator (though the NOT operator is fine). */
57 size_t escaped_len = 0;
59 const char *join = "";
62 /* Don't optimize if there are no tag changes. */
63 if (tag_ops[0].tag == NULL)
64 return talloc_strdup (ctx, orig_query_string);
66 /* Build the new query string */
67 if (strcmp (orig_query_string, "*") == 0)
68 query_string = talloc_strdup (ctx, "(");
70 query_string = talloc_asprintf (ctx, "( %s ) and (", orig_query_string);
72 for (i = 0; tag_ops[i].tag && query_string; i++) {
73 /* XXX in case of OOM, query_string will be deallocated when
74 * ctx is, which might be at shutdown */
75 if (make_boolean_term (ctx,
76 "tag", tag_ops[i].tag,
77 &escaped, &escaped_len))
80 query_string = talloc_asprintf_append_buffer (
81 query_string, "%s%s%s", join,
82 tag_ops[i].remove ? "" : "not ",
88 query_string = talloc_strdup_append_buffer (query_string, ")");
90 talloc_free (escaped);
94 /* Tag messages matching 'query_string' according to 'tag_ops', which
95 * must be an array of tagging operations terminated with an empty
98 tag_query (void *ctx, notmuch_database_t *notmuch, const char *query_string,
99 tag_operation_t *tag_ops, notmuch_bool_t synchronize_flags)
101 notmuch_query_t *query;
102 notmuch_messages_t *messages;
103 notmuch_message_t *message;
106 /* Optimize the query so it excludes messages that already have
107 * the specified set of tags. */
108 query_string = _optimize_tag_query (ctx, query_string, tag_ops);
109 if (query_string == NULL) {
110 fprintf (stderr, "Out of memory.\n");
114 query = notmuch_query_create (notmuch, query_string);
116 fprintf (stderr, "Out of memory.\n");
120 /* tagging is not interested in any special sort order */
121 notmuch_query_set_sort (query, NOTMUCH_SORT_UNSORTED);
123 for (messages = notmuch_query_search_messages (query);
124 notmuch_messages_valid (messages) && ! interrupted;
125 notmuch_messages_move_to_next (messages)) {
126 message = notmuch_messages_get (messages);
128 notmuch_message_freeze (message);
130 for (i = 0; tag_ops[i].tag; i++) {
131 if (tag_ops[i].remove)
132 notmuch_message_remove_tag (message, tag_ops[i].tag);
134 notmuch_message_add_tag (message, tag_ops[i].tag);
137 notmuch_message_thaw (message);
139 if (synchronize_flags)
140 notmuch_message_tags_to_maildir_flags (message);
142 notmuch_message_destroy (message);
145 notmuch_query_destroy (query);
151 notmuch_tag_command (void *ctx, int argc, char *argv[])
153 tag_operation_t *tag_ops;
154 int tag_ops_count = 0;
156 notmuch_config_t *config;
157 notmuch_database_t *notmuch;
158 struct sigaction action;
159 notmuch_bool_t synchronize_flags;
163 /* Setup our handler for SIGINT */
164 memset (&action, 0, sizeof (struct sigaction));
165 action.sa_handler = handle_sigint;
166 sigemptyset (&action.sa_mask);
167 action.sa_flags = SA_RESTART;
168 sigaction (SIGINT, &action, NULL);
170 argc--; argv++; /* skip subcommand argument */
172 /* Array of tagging operations (add or remove), terminated with an
174 tag_ops = talloc_array (ctx, tag_operation_t, argc + 1);
175 if (tag_ops == NULL) {
176 fprintf (stderr, "Out of memory.\n");
180 for (i = 0; i < argc; i++) {
181 if (strcmp (argv[i], "--") == 0) {
185 if (argv[i][0] == '+' || argv[i][0] == '-') {
186 if (argv[i][0] == '+' && argv[i][1] == '\0') {
187 fprintf (stderr, "Error: tag names cannot be empty.\n");
190 if (argv[i][0] == '+' && argv[i][1] == '-') {
191 /* This disallows adding the non-removable tag "-" and
192 * enables notmuch tag to take long options in the
194 fprintf (stderr, "Error: tag names must not start with '-'.\n");
197 tag_ops[tag_ops_count].tag = argv[i] + 1;
198 tag_ops[tag_ops_count].remove = (argv[i][0] == '-');
205 tag_ops[tag_ops_count].tag = NULL;
207 if (tag_ops_count == 0) {
208 fprintf (stderr, "Error: 'notmuch tag' requires at least one tag to add or remove.\n");
212 query_string = query_string_from_args (ctx, argc - i, &argv[i]);
214 if (*query_string == '\0') {
215 fprintf (stderr, "Error: notmuch tag requires at least one search term.\n");
219 config = notmuch_config_open (ctx, NULL, NULL);
223 if (notmuch_database_open (notmuch_config_get_database_path (config),
224 NOTMUCH_DATABASE_MODE_READ_WRITE, ¬much))
227 synchronize_flags = notmuch_config_get_maildir_synchronize_flags (config);
229 ret = tag_query (ctx, notmuch, query_string, tag_ops, synchronize_flags);
231 notmuch_database_destroy (notmuch);