2009-11-10 21:03:05 +01:00
|
|
|
/* notmuch - Not much of an email program, (just index and search)
|
|
|
|
*
|
|
|
|
* Copyright © 2009 Carl Worth
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
2016-06-02 18:26:14 +02:00
|
|
|
* along with this program. If not, see https://www.gnu.org/licenses/ .
|
2009-11-10 21:03:05 +01:00
|
|
|
*
|
|
|
|
* Author: Carl Worth <cworth@cworth.org>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "notmuch-client.h"
|
2012-12-25 20:42:39 +01:00
|
|
|
#include "tag-util.h"
|
2013-01-06 21:22:38 +01:00
|
|
|
#include "string-util.h"
|
2009-11-10 21:03:05 +01:00
|
|
|
|
2009-11-13 18:05:13 +01:00
|
|
|
static volatile sig_atomic_t interrupted;
|
|
|
|
|
|
|
|
static void
|
|
|
|
handle_sigint (unused (int sig))
|
|
|
|
{
|
2021-05-13 01:40:23 +02:00
|
|
|
static const char msg[] = "Stopping... \n";
|
2012-01-19 23:29:19 +01:00
|
|
|
|
|
|
|
/* This write is "opportunistic", so it's okay to ignore the
|
|
|
|
* result. It is not required for correctness, and if it does
|
|
|
|
* fail or produce a short write, we want to get out of the signal
|
|
|
|
* handler as quickly as possible, not retry it. */
|
2012-11-15 02:33:20 +01:00
|
|
|
IGNORE_RESULT (write (2, msg, sizeof (msg) - 1));
|
2009-11-13 18:05:13 +01:00
|
|
|
interrupted = 1;
|
|
|
|
}
|
|
|
|
|
2012-03-26 23:04:09 +02:00
|
|
|
|
2011-11-09 14:44:35 +01:00
|
|
|
static char *
|
2012-03-26 23:04:09 +02:00
|
|
|
_optimize_tag_query (void *ctx, const char *orig_query_string,
|
2012-12-25 20:42:39 +01:00
|
|
|
const tag_op_list_t *list)
|
2011-11-09 14:44:35 +01:00
|
|
|
{
|
|
|
|
/* This is subtler than it looks. Xapian ignores the '-' operator
|
|
|
|
* at the beginning both queries and parenthesized groups and,
|
|
|
|
* furthermore, the presence of a '-' operator at the beginning of
|
|
|
|
* a group can inhibit parsing of the previous operator. Hence,
|
|
|
|
* the user-provided query MUST appear first, but it is safe to
|
|
|
|
* parenthesize and the exclusion part of the query must not use
|
|
|
|
* the '-' operator (though the NOT operator is fine). */
|
|
|
|
|
2013-01-06 21:22:38 +01:00
|
|
|
char *escaped = NULL;
|
|
|
|
size_t escaped_len = 0;
|
|
|
|
char *query_string;
|
2011-11-09 14:44:35 +01:00
|
|
|
const char *join = "";
|
2013-01-06 21:22:38 +01:00
|
|
|
size_t i;
|
2011-11-09 14:44:35 +01:00
|
|
|
|
2012-03-26 23:04:09 +02:00
|
|
|
/* Don't optimize if there are no tag changes. */
|
2012-12-25 20:42:39 +01:00
|
|
|
if (tag_op_list_size (list) == 0)
|
2012-03-26 23:04:09 +02:00
|
|
|
return talloc_strdup (ctx, orig_query_string);
|
|
|
|
|
2011-11-09 14:44:35 +01:00
|
|
|
/* Build the new query string */
|
|
|
|
if (strcmp (orig_query_string, "*") == 0)
|
|
|
|
query_string = talloc_strdup (ctx, "(");
|
|
|
|
else
|
|
|
|
query_string = talloc_asprintf (ctx, "( %s ) and (", orig_query_string);
|
|
|
|
|
2012-12-25 20:42:39 +01:00
|
|
|
for (i = 0; i < tag_op_list_size (list) && query_string; i++) {
|
2013-01-06 21:22:38 +01:00
|
|
|
/* XXX in case of OOM, query_string will be deallocated when
|
|
|
|
* ctx is, which might be at shutdown */
|
|
|
|
if (make_boolean_term (ctx,
|
2012-12-25 20:42:39 +01:00
|
|
|
"tag", tag_op_list_tag (list, i),
|
2013-01-06 21:22:38 +01:00
|
|
|
&escaped, &escaped_len))
|
|
|
|
return NULL;
|
|
|
|
|
2011-11-09 14:44:35 +01:00
|
|
|
query_string = talloc_asprintf_append_buffer (
|
2013-01-06 21:22:38 +01:00
|
|
|
query_string, "%s%s%s", join,
|
2012-12-25 20:42:39 +01:00
|
|
|
tag_op_list_isremove (list, i) ? "" : "not ",
|
2013-01-06 21:22:38 +01:00
|
|
|
escaped);
|
2011-11-09 14:44:35 +01:00
|
|
|
join = " or ";
|
|
|
|
}
|
|
|
|
|
|
|
|
if (query_string)
|
|
|
|
query_string = talloc_strdup_append_buffer (query_string, ")");
|
|
|
|
|
|
|
|
talloc_free (escaped);
|
|
|
|
return query_string;
|
|
|
|
}
|
|
|
|
|
2012-12-25 20:42:39 +01:00
|
|
|
/* Tag messages matching 'query_string' according to 'tag_ops'
|
|
|
|
*/
|
2012-03-26 23:04:10 +02:00
|
|
|
static int
|
|
|
|
tag_query (void *ctx, notmuch_database_t *notmuch, const char *query_string,
|
2012-12-25 20:42:39 +01:00
|
|
|
tag_op_list_t *tag_ops, tag_op_flag_t flags)
|
2012-03-26 23:04:10 +02:00
|
|
|
{
|
|
|
|
notmuch_query_t *query;
|
|
|
|
notmuch_messages_t *messages;
|
|
|
|
notmuch_message_t *message;
|
2015-09-06 15:15:47 +02:00
|
|
|
notmuch_status_t status;
|
|
|
|
|
2013-01-19 14:14:46 +01:00
|
|
|
int ret = NOTMUCH_STATUS_SUCCESS;
|
2012-03-26 23:04:10 +02:00
|
|
|
|
2013-03-09 15:56:49 +01:00
|
|
|
if (! (flags & TAG_FLAG_REMOVE_ALL)) {
|
|
|
|
/* Optimize the query so it excludes messages that already
|
|
|
|
* have the specified set of tags. */
|
|
|
|
query_string = _optimize_tag_query (ctx, query_string, tag_ops);
|
|
|
|
if (query_string == NULL) {
|
|
|
|
fprintf (stderr, "Out of memory.\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
flags |= TAG_FLAG_PRE_OPTIMIZED;
|
2012-03-26 23:04:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
query = notmuch_query_create (notmuch, query_string);
|
|
|
|
if (query == NULL) {
|
|
|
|
fprintf (stderr, "Out of memory.\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* tagging is not interested in any special sort order */
|
|
|
|
notmuch_query_set_sort (query, NOTMUCH_SORT_UNSORTED);
|
|
|
|
|
2017-02-26 22:21:32 +01:00
|
|
|
status = notmuch_query_search_messages (query, &messages);
|
2015-09-06 15:15:47 +02:00
|
|
|
if (print_status_query ("notmuch tag", query, status))
|
|
|
|
return status;
|
|
|
|
|
|
|
|
for (;
|
2012-11-15 02:33:20 +01:00
|
|
|
notmuch_messages_valid (messages) && ! interrupted;
|
|
|
|
notmuch_messages_move_to_next (messages)) {
|
2012-03-26 23:04:10 +02:00
|
|
|
message = notmuch_messages_get (messages);
|
2013-03-09 15:56:49 +01:00
|
|
|
ret = tag_op_list_apply (message, tag_ops, flags);
|
2012-03-26 23:04:10 +02:00
|
|
|
notmuch_message_destroy (message);
|
2013-01-08 22:41:54 +01:00
|
|
|
if (ret != NOTMUCH_STATUS_SUCCESS)
|
|
|
|
break;
|
2012-03-26 23:04:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
notmuch_query_destroy (query);
|
|
|
|
|
2013-01-08 22:41:54 +01:00
|
|
|
return ret || interrupted;
|
2012-03-26 23:04:10 +02:00
|
|
|
}
|
|
|
|
|
2012-12-25 20:42:40 +01:00
|
|
|
static int
|
|
|
|
tag_file (void *ctx, notmuch_database_t *notmuch, tag_op_flag_t flags,
|
|
|
|
FILE *input)
|
|
|
|
{
|
|
|
|
char *line = NULL;
|
|
|
|
char *query_string = NULL;
|
|
|
|
size_t line_size = 0;
|
|
|
|
ssize_t line_len;
|
|
|
|
int ret = 0;
|
2013-01-12 08:40:14 +01:00
|
|
|
int warn = 0;
|
2012-12-25 20:42:40 +01:00
|
|
|
tag_op_list_t *tag_ops;
|
|
|
|
|
|
|
|
tag_ops = tag_op_list_create (ctx);
|
|
|
|
if (tag_ops == NULL) {
|
|
|
|
fprintf (stderr, "Out of memory.\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
while ((line_len = getline (&line, &line_size, input)) != -1 &&
|
|
|
|
! interrupted) {
|
|
|
|
|
|
|
|
ret = parse_tag_line (ctx, line, TAG_FLAG_NONE,
|
|
|
|
&query_string, tag_ops);
|
|
|
|
|
2013-01-12 08:40:14 +01:00
|
|
|
if (ret > 0) {
|
|
|
|
if (ret != TAG_PARSE_SKIPPED)
|
|
|
|
/* remember there has been problematic lines */
|
|
|
|
warn = 1;
|
|
|
|
ret = 0;
|
2012-12-25 20:42:40 +01:00
|
|
|
continue;
|
2013-01-12 08:40:14 +01:00
|
|
|
}
|
2012-12-25 20:42:40 +01:00
|
|
|
|
|
|
|
if (ret < 0)
|
|
|
|
break;
|
|
|
|
|
|
|
|
ret = tag_query (ctx, notmuch, query_string, tag_ops, flags);
|
|
|
|
if (ret)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (line)
|
|
|
|
free (line);
|
|
|
|
|
2013-01-12 08:40:14 +01:00
|
|
|
return ret || warn;
|
2012-12-25 20:42:40 +01:00
|
|
|
}
|
|
|
|
|
2009-11-10 21:03:05 +01:00
|
|
|
int
|
2021-02-27 15:07:45 +01:00
|
|
|
notmuch_tag_command (notmuch_database_t *notmuch, int argc, char *argv[])
|
2009-11-10 21:03:05 +01:00
|
|
|
{
|
2012-12-25 20:42:39 +01:00
|
|
|
tag_op_list_t *tag_ops = NULL;
|
|
|
|
char *query_string = NULL;
|
2009-11-13 18:05:13 +01:00
|
|
|
struct sigaction action;
|
2012-12-25 20:42:39 +01:00
|
|
|
tag_op_flag_t tag_flags = TAG_FLAG_NONE;
|
2017-10-07 10:44:04 +02:00
|
|
|
bool batch = false;
|
|
|
|
bool remove_all = false;
|
2012-12-25 20:42:40 +01:00
|
|
|
FILE *input = stdin;
|
cli: use designated initializers for opt desc
Several changes at once, just to not have to change the same lines
several times over:
- Use designated initializers to initialize opt desc arrays.
- Only initialize the needed fields.
- Remove arg_id (short options) as unused.
- Replace opt_type and output_var with several type safe output
variables, where the output variable being non-NULL determines the
type. Introduce checks to ensure only one is set. The downside is
some waste of const space per argument; this could be saved by
retaining opt_type and using a union, but that's still pretty
verbose.
- Fix some variables due to the type safety. Mostly a good thing, but
leads to some enums being changed to ints. This is pedantically
correct, but somewhat annoying. We could also cast, but that defeats
the purpose a bit.
- Terminate the opt desc arrays using {}.
The output variable type safety and the ability to add new fields for
just some output types or arguments are the big wins. For example, if
we wanted to add a variable to set when the argument is present, we
could do so for just the arguments that need it.
Beauty is in the eye of the beholder, but I think this looks nice when
defining the arguments, and reduces some of the verbosity we have
there.
2017-10-01 22:53:11 +02:00
|
|
|
const char *input_file_name = NULL;
|
2012-12-25 20:42:40 +01:00
|
|
|
int opt_index;
|
2014-01-10 22:28:53 +01:00
|
|
|
int ret;
|
2020-12-08 00:48:48 +01:00
|
|
|
notmuch_bool_t synchronize_flags;
|
2009-11-10 21:03:05 +01:00
|
|
|
|
2015-05-27 19:53:52 +02:00
|
|
|
/* Set up our handler for SIGINT */
|
2009-11-13 18:05:13 +01:00
|
|
|
memset (&action, 0, sizeof (struct sigaction));
|
|
|
|
action.sa_handler = handle_sigint;
|
|
|
|
sigemptyset (&action.sa_mask);
|
|
|
|
action.sa_flags = SA_RESTART;
|
|
|
|
sigaction (SIGINT, &action, NULL);
|
|
|
|
|
2012-12-25 20:42:40 +01:00
|
|
|
notmuch_opt_desc_t options[] = {
|
cli: use designated initializers for opt desc
Several changes at once, just to not have to change the same lines
several times over:
- Use designated initializers to initialize opt desc arrays.
- Only initialize the needed fields.
- Remove arg_id (short options) as unused.
- Replace opt_type and output_var with several type safe output
variables, where the output variable being non-NULL determines the
type. Introduce checks to ensure only one is set. The downside is
some waste of const space per argument; this could be saved by
retaining opt_type and using a union, but that's still pretty
verbose.
- Fix some variables due to the type safety. Mostly a good thing, but
leads to some enums being changed to ints. This is pedantically
correct, but somewhat annoying. We could also cast, but that defeats
the purpose a bit.
- Terminate the opt desc arrays using {}.
The output variable type safety and the ability to add new fields for
just some output types or arguments are the big wins. For example, if
we wanted to add a variable to set when the argument is present, we
could do so for just the arguments that need it.
Beauty is in the eye of the beholder, but I think this looks nice when
defining the arguments, and reduces some of the verbosity we have
there.
2017-10-01 22:53:11 +02:00
|
|
|
{ .opt_bool = &batch, .name = "batch" },
|
|
|
|
{ .opt_string = &input_file_name, .name = "input" },
|
|
|
|
{ .opt_bool = &remove_all, .name = "remove-all" },
|
|
|
|
{ .opt_inherit = notmuch_shared_options },
|
|
|
|
{ }
|
2012-12-25 20:42:40 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
opt_index = parse_arguments (argc, argv, options, 1);
|
|
|
|
if (opt_index < 0)
|
2014-01-10 22:28:53 +01:00
|
|
|
return EXIT_FAILURE;
|
2012-12-25 20:42:40 +01:00
|
|
|
|
2021-08-24 17:17:10 +02:00
|
|
|
notmuch_process_shared_options (notmuch, argv[0]);
|
2015-04-05 15:13:03 +02:00
|
|
|
|
2012-12-25 20:42:40 +01:00
|
|
|
if (input_file_name) {
|
2017-10-07 10:44:04 +02:00
|
|
|
batch = true;
|
2012-12-25 20:42:40 +01:00
|
|
|
input = fopen (input_file_name, "r");
|
|
|
|
if (input == NULL) {
|
|
|
|
fprintf (stderr, "Error opening %s for reading: %s\n",
|
|
|
|
input_file_name, strerror (errno));
|
2014-01-10 22:28:53 +01:00
|
|
|
return EXIT_FAILURE;
|
2012-12-25 20:42:40 +01:00
|
|
|
}
|
2009-11-10 21:03:05 +01:00
|
|
|
}
|
|
|
|
|
2012-12-25 20:42:40 +01:00
|
|
|
if (batch) {
|
|
|
|
if (opt_index != argc) {
|
|
|
|
fprintf (stderr, "Can't specify both cmdline and stdin!\n");
|
2017-08-26 16:41:38 +02:00
|
|
|
if (input)
|
|
|
|
fclose (input);
|
2014-01-10 22:28:53 +01:00
|
|
|
return EXIT_FAILURE;
|
2012-12-25 20:42:40 +01:00
|
|
|
}
|
|
|
|
} else {
|
2020-12-08 00:48:48 +01:00
|
|
|
tag_ops = tag_op_list_create (notmuch);
|
2012-12-25 20:42:40 +01:00
|
|
|
if (tag_ops == NULL) {
|
|
|
|
fprintf (stderr, "Out of memory.\n");
|
2014-01-10 22:28:53 +01:00
|
|
|
return EXIT_FAILURE;
|
2012-12-25 20:42:40 +01:00
|
|
|
}
|
|
|
|
|
2020-12-08 00:48:48 +01:00
|
|
|
if (parse_tag_command_line (notmuch, argc - opt_index, argv + opt_index,
|
2012-12-25 20:42:40 +01:00
|
|
|
&query_string, tag_ops))
|
2014-01-10 22:28:53 +01:00
|
|
|
return EXIT_FAILURE;
|
2013-03-09 15:56:48 +01:00
|
|
|
|
2013-03-09 15:56:49 +01:00
|
|
|
if (tag_op_list_size (tag_ops) == 0 && ! remove_all) {
|
2013-03-09 15:56:48 +01:00
|
|
|
fprintf (stderr, "Error: 'notmuch tag' requires at least one tag to add or remove.\n");
|
2014-01-10 22:28:53 +01:00
|
|
|
return EXIT_FAILURE;
|
2013-03-09 15:56:48 +01:00
|
|
|
}
|
2013-06-23 06:23:54 +02:00
|
|
|
|
|
|
|
if (*query_string == '\0') {
|
|
|
|
fprintf (stderr, "Error: notmuch tag requires at least one search term.\n");
|
2014-01-10 22:28:53 +01:00
|
|
|
return EXIT_FAILURE;
|
2013-06-23 06:23:54 +02:00
|
|
|
}
|
2012-12-25 20:42:40 +01:00
|
|
|
}
|
2009-11-10 21:03:05 +01:00
|
|
|
|
2020-12-08 00:48:48 +01:00
|
|
|
if (print_status_database (
|
|
|
|
"notmuch restore",
|
|
|
|
notmuch,
|
|
|
|
notmuch_config_get_bool (notmuch, NOTMUCH_CONFIG_SYNC_MAILDIR_FLAGS,
|
|
|
|
&synchronize_flags)))
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
|
|
|
|
if (synchronize_flags)
|
2012-12-25 20:42:39 +01:00
|
|
|
tag_flags |= TAG_FLAG_MAILDIR_SYNC;
|
2009-11-10 21:03:05 +01:00
|
|
|
|
2013-03-09 15:56:49 +01:00
|
|
|
if (remove_all)
|
|
|
|
tag_flags |= TAG_FLAG_REMOVE_ALL;
|
|
|
|
|
2012-12-25 20:42:40 +01:00
|
|
|
if (batch)
|
2020-12-08 00:48:48 +01:00
|
|
|
ret = tag_file (notmuch, notmuch, tag_flags, input);
|
2012-12-25 20:42:40 +01:00
|
|
|
else
|
2020-12-08 00:48:48 +01:00
|
|
|
ret = tag_query (notmuch, notmuch, query_string, tag_ops, tag_flags);
|
2010-04-16 08:38:48 +02:00
|
|
|
|
2012-04-22 14:07:53 +02:00
|
|
|
notmuch_database_destroy (notmuch);
|
2009-11-10 21:03:05 +01:00
|
|
|
|
2012-12-25 20:42:40 +01:00
|
|
|
if (input != stdin)
|
|
|
|
fclose (input);
|
|
|
|
|
2014-01-10 22:28:53 +01:00
|
|
|
return ret || interrupted ? EXIT_FAILURE : EXIT_SUCCESS;
|
2009-11-10 21:03:05 +01:00
|
|
|
}
|