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"
23 static volatile sig_atomic_t interrupted;
26 handle_sigint (unused (int sig))
28 static char msg[] = "Stopping... \n";
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));
39 _escape_tag (char *buf, const char *tag)
43 /* Boolean terms surrounded by double quotes can contain any
44 * character. Double quotes are quoted by doubling them. */
58 notmuch_bool_t remove;
62 _optimize_tag_query (void *ctx, const char *orig_query_string,
63 const tag_operation_t *tag_ops)
65 /* This is subtler than it looks. Xapian ignores the '-' operator
66 * at the beginning both queries and parenthesized groups and,
67 * furthermore, the presence of a '-' operator at the beginning of
68 * a group can inhibit parsing of the previous operator. Hence,
69 * the user-provided query MUST appear first, but it is safe to
70 * parenthesize and the exclusion part of the query must not use
71 * the '-' operator (though the NOT operator is fine). */
73 char *escaped, *query_string;
74 const char *join = "";
76 unsigned int max_tag_len = 0;
78 /* Don't optimize if there are no tag changes. */
79 if (tag_ops[0].tag == NULL)
80 return talloc_strdup (ctx, orig_query_string);
82 /* Allocate a buffer for escaping tags. This is large enough to
83 * hold a fully escaped tag with every character doubled plus
84 * enclosing quotes and a NUL. */
85 for (i = 0; tag_ops[i].tag; i++)
86 if (strlen (tag_ops[i].tag) > max_tag_len)
87 max_tag_len = strlen (tag_ops[i].tag);
88 escaped = talloc_array(ctx, char, max_tag_len * 2 + 3);
92 /* Build the new query string */
93 if (strcmp (orig_query_string, "*") == 0)
94 query_string = talloc_strdup (ctx, "(");
96 query_string = talloc_asprintf (ctx, "( %s ) and (", orig_query_string);
98 for (i = 0; tag_ops[i].tag && query_string; i++) {
99 query_string = talloc_asprintf_append_buffer (
100 query_string, "%s%stag:%s", join,
101 tag_ops[i].remove ? "" : "not ",
102 _escape_tag (escaped, tag_ops[i].tag));
107 query_string = talloc_strdup_append_buffer (query_string, ")");
109 talloc_free (escaped);
114 notmuch_tag_command (void *ctx, int argc, char *argv[])
116 tag_operation_t *tag_ops;
117 int tag_ops_count = 0;
119 notmuch_config_t *config;
120 notmuch_database_t *notmuch;
121 notmuch_query_t *query;
122 notmuch_messages_t *messages;
123 notmuch_message_t *message;
124 struct sigaction action;
125 notmuch_bool_t synchronize_flags;
128 /* Setup our handler for SIGINT */
129 memset (&action, 0, sizeof (struct sigaction));
130 action.sa_handler = handle_sigint;
131 sigemptyset (&action.sa_mask);
132 action.sa_flags = SA_RESTART;
133 sigaction (SIGINT, &action, NULL);
135 argc--; argv++; /* skip subcommand argument */
137 /* Array of tagging operations (add or remove), terminated with an
139 tag_ops = talloc_array (ctx, tag_operation_t, argc + 1);
140 if (tag_ops == NULL) {
141 fprintf (stderr, "Out of memory.\n");
145 for (i = 0; i < argc; i++) {
146 if (strcmp (argv[i], "--") == 0) {
150 if (argv[i][0] == '+' || argv[i][0] == '-') {
151 tag_ops[tag_ops_count].tag = argv[i] + 1;
152 tag_ops[tag_ops_count].remove = (argv[i][0] == '-');
159 tag_ops[tag_ops_count].tag = NULL;
161 if (tag_ops_count == 0) {
162 fprintf (stderr, "Error: 'notmuch tag' requires at least one tag to add or remove.\n");
166 query_string = query_string_from_args (ctx, argc - i, &argv[i]);
168 if (*query_string == '\0') {
169 fprintf (stderr, "Error: notmuch tag requires at least one search term.\n");
173 /* Optimize the query so it excludes messages that already have
174 * the specified set of tags. */
175 query_string = _optimize_tag_query (ctx, query_string, tag_ops);
176 if (query_string == NULL) {
177 fprintf (stderr, "Out of memory.\n");
181 config = notmuch_config_open (ctx, NULL, NULL);
185 notmuch = notmuch_database_open (notmuch_config_get_database_path (config),
186 NOTMUCH_DATABASE_MODE_READ_WRITE);
190 synchronize_flags = notmuch_config_get_maildir_synchronize_flags (config);
192 query = notmuch_query_create (notmuch, query_string);
194 fprintf (stderr, "Out of memory.\n");
198 /* tagging is not interested in any special sort order */
199 notmuch_query_set_sort (query, NOTMUCH_SORT_UNSORTED);
201 for (messages = notmuch_query_search_messages (query);
202 notmuch_messages_valid (messages) && !interrupted;
203 notmuch_messages_move_to_next (messages))
205 message = notmuch_messages_get (messages);
207 notmuch_message_freeze (message);
209 for (i = 0; tag_ops[i].tag; i++) {
210 if (tag_ops[i].remove)
211 notmuch_message_remove_tag (message, tag_ops[i].tag);
213 notmuch_message_add_tag (message, tag_ops[i].tag);
216 notmuch_message_thaw (message);
218 if (synchronize_flags)
219 notmuch_message_tags_to_maildir_flags (message);
221 notmuch_message_destroy (message);
224 notmuch_query_destroy (query);
225 notmuch_database_close (notmuch);