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 #include "string-util.h"
25 static volatile sig_atomic_t interrupted;
28 handle_sigint (unused (int sig))
30 static char msg[] = "Stopping... \n";
32 /* This write is "opportunistic", so it's okay to ignore the
33 * result. It is not required for correctness, and if it does
34 * fail or produce a short write, we want to get out of the signal
35 * handler as quickly as possible, not retry it. */
36 IGNORE_RESULT (write (2, msg, sizeof (msg) - 1));
42 _optimize_tag_query (void *ctx, const char *orig_query_string,
43 const tag_op_list_t *list)
45 /* This is subtler than it looks. Xapian ignores the '-' operator
46 * at the beginning both queries and parenthesized groups and,
47 * furthermore, the presence of a '-' operator at the beginning of
48 * a group can inhibit parsing of the previous operator. Hence,
49 * the user-provided query MUST appear first, but it is safe to
50 * parenthesize and the exclusion part of the query must not use
51 * the '-' operator (though the NOT operator is fine). */
54 size_t escaped_len = 0;
56 const char *join = "";
59 /* Don't optimize if there are no tag changes. */
60 if (tag_op_list_size (list) == 0)
61 return talloc_strdup (ctx, orig_query_string);
63 /* Build the new query string */
64 if (strcmp (orig_query_string, "*") == 0)
65 query_string = talloc_strdup (ctx, "(");
67 query_string = talloc_asprintf (ctx, "( %s ) and (", orig_query_string);
69 for (i = 0; i < tag_op_list_size (list) && query_string; i++) {
70 /* XXX in case of OOM, query_string will be deallocated when
71 * ctx is, which might be at shutdown */
72 if (make_boolean_term (ctx,
73 "tag", tag_op_list_tag (list, i),
74 &escaped, &escaped_len))
77 query_string = talloc_asprintf_append_buffer (
78 query_string, "%s%s%s", join,
79 tag_op_list_isremove (list, i) ? "" : "not ",
85 query_string = talloc_strdup_append_buffer (query_string, ")");
87 talloc_free (escaped);
91 /* Tag messages matching 'query_string' according to 'tag_ops'
94 tag_query (void *ctx, notmuch_database_t *notmuch, const char *query_string,
95 tag_op_list_t *tag_ops, tag_op_flag_t flags)
97 notmuch_query_t *query;
98 notmuch_messages_t *messages;
99 notmuch_message_t *message;
100 int ret = NOTMUCH_STATUS_SUCCESS;
102 if (! (flags & TAG_FLAG_REMOVE_ALL)) {
103 /* Optimize the query so it excludes messages that already
104 * have the specified set of tags. */
105 query_string = _optimize_tag_query (ctx, query_string, tag_ops);
106 if (query_string == NULL) {
107 fprintf (stderr, "Out of memory.\n");
110 flags |= TAG_FLAG_PRE_OPTIMIZED;
113 query = notmuch_query_create (notmuch, query_string);
115 fprintf (stderr, "Out of memory.\n");
119 /* tagging is not interested in any special sort order */
120 notmuch_query_set_sort (query, NOTMUCH_SORT_UNSORTED);
122 for (messages = notmuch_query_search_messages (query);
123 notmuch_messages_valid (messages) && ! interrupted;
124 notmuch_messages_move_to_next (messages)) {
125 message = notmuch_messages_get (messages);
126 ret = tag_op_list_apply (message, tag_ops, flags);
127 notmuch_message_destroy (message);
128 if (ret != NOTMUCH_STATUS_SUCCESS)
132 notmuch_query_destroy (query);
134 return ret || interrupted;
138 tag_file (void *ctx, notmuch_database_t *notmuch, tag_op_flag_t flags,
142 char *query_string = NULL;
143 size_t line_size = 0;
147 tag_op_list_t *tag_ops;
149 tag_ops = tag_op_list_create (ctx);
150 if (tag_ops == NULL) {
151 fprintf (stderr, "Out of memory.\n");
155 while ((line_len = getline (&line, &line_size, input)) != -1 &&
158 ret = parse_tag_line (ctx, line, TAG_FLAG_NONE,
159 &query_string, tag_ops);
162 if (ret != TAG_PARSE_SKIPPED)
163 /* remember there has been problematic lines */
172 ret = tag_query (ctx, notmuch, query_string, tag_ops, flags);
184 notmuch_tag_command (notmuch_config_t *config, int argc, char *argv[])
186 tag_op_list_t *tag_ops = NULL;
187 char *query_string = NULL;
188 notmuch_database_t *notmuch;
189 struct sigaction action;
190 tag_op_flag_t tag_flags = TAG_FLAG_NONE;
191 notmuch_bool_t batch = FALSE;
192 notmuch_bool_t remove_all = FALSE;
194 char *input_file_name = NULL;
198 /* Set up our handler for SIGINT */
199 memset (&action, 0, sizeof (struct sigaction));
200 action.sa_handler = handle_sigint;
201 sigemptyset (&action.sa_mask);
202 action.sa_flags = SA_RESTART;
203 sigaction (SIGINT, &action, NULL);
205 notmuch_opt_desc_t options[] = {
206 { NOTMUCH_OPT_BOOLEAN, &batch, "batch", 0, 0 },
207 { NOTMUCH_OPT_STRING, &input_file_name, "input", 'i', 0 },
208 { NOTMUCH_OPT_BOOLEAN, &remove_all, "remove-all", 0, 0 },
209 { NOTMUCH_OPT_INHERIT, (void *) ¬much_shared_options, NULL, 0, 0 },
213 opt_index = parse_arguments (argc, argv, options, 1);
217 notmuch_process_shared_options (argv[0]);
219 if (input_file_name) {
221 input = fopen (input_file_name, "r");
223 fprintf (stderr, "Error opening %s for reading: %s\n",
224 input_file_name, strerror (errno));
230 if (opt_index != argc) {
231 fprintf (stderr, "Can't specify both cmdline and stdin!\n");
235 fprintf (stderr, "Can't specify both --remove-all and --batch\n");
239 tag_ops = tag_op_list_create (config);
240 if (tag_ops == NULL) {
241 fprintf (stderr, "Out of memory.\n");
245 if (parse_tag_command_line (config, argc - opt_index, argv + opt_index,
246 &query_string, tag_ops))
249 if (tag_op_list_size (tag_ops) == 0 && ! remove_all) {
250 fprintf (stderr, "Error: 'notmuch tag' requires at least one tag to add or remove.\n");
254 if (*query_string == '\0') {
255 fprintf (stderr, "Error: notmuch tag requires at least one search term.\n");
260 if (notmuch_database_open (notmuch_config_get_database_path (config),
261 NOTMUCH_DATABASE_MODE_READ_WRITE, ¬much))
264 notmuch_exit_if_unmatched_db_uuid (notmuch);
266 if (notmuch_config_get_maildir_synchronize_flags (config))
267 tag_flags |= TAG_FLAG_MAILDIR_SYNC;
270 tag_flags |= TAG_FLAG_REMOVE_ALL;
273 ret = tag_file (config, notmuch, tag_flags, input);
275 ret = tag_query (config, notmuch, query_string, tag_ops, tag_flags);
277 notmuch_database_destroy (notmuch);
282 return ret || interrupted ? EXIT_FAILURE : EXIT_SUCCESS;