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))
30 static char msg[] = "Stopping... \n";
31 ignored = write(2, msg, sizeof(msg)-1);
36 notmuch_tag_command (void *ctx, unused (int argc), unused (char *argv[]))
38 int *add_tags, *remove_tags;
39 int add_tags_count = 0;
40 int remove_tags_count = 0;
42 notmuch_config_t *config;
43 notmuch_database_t *notmuch;
44 notmuch_query_t *query;
45 notmuch_messages_t *messages;
46 notmuch_message_t *message;
47 struct sigaction action;
50 /* Setup our handler for SIGINT */
51 memset (&action, 0, sizeof (struct sigaction));
52 action.sa_handler = handle_sigint;
53 sigemptyset (&action.sa_mask);
54 action.sa_flags = SA_RESTART;
55 sigaction (SIGINT, &action, NULL);
57 add_tags = talloc_size (ctx, argc * sizeof (int));
58 if (add_tags == NULL) {
59 fprintf (stderr, "Out of memory.\n");
63 remove_tags = talloc_size (ctx, argc * sizeof (int));
64 if (remove_tags == NULL) {
65 fprintf (stderr, "Out of memory.\n");
69 for (i = 0; i < argc; i++) {
70 if (strcmp (argv[i], "--") == 0) {
74 if (argv[i][0] == '+') {
75 add_tags[add_tags_count++] = i;
76 } else if (argv[i][0] == '-') {
77 remove_tags[remove_tags_count++] = i;
83 if (add_tags_count == 0 && remove_tags_count == 0) {
84 fprintf (stderr, "Error: 'notmuch tag' requires at least one tag to add or remove.\n");
88 query_string = query_string_from_args (ctx, argc - i, &argv[i]);
90 if (*query_string == '\0') {
91 fprintf (stderr, "Error: notmuch tag requires at least one search term.\n");
95 config = notmuch_config_open (ctx, NULL, NULL);
99 notmuch = notmuch_database_open (notmuch_config_get_database_path (config),
100 NOTMUCH_DATABASE_MODE_READ_WRITE);
104 query = notmuch_query_create (notmuch, query_string);
106 fprintf (stderr, "Out of memory.\n");
110 for (messages = notmuch_query_search_messages (query);
111 notmuch_messages_has_more (messages) && !interrupted;
112 notmuch_messages_advance (messages))
114 message = notmuch_messages_get (messages);
116 notmuch_message_freeze (message);
118 for (i = 0; i < remove_tags_count; i++)
119 notmuch_message_remove_tag (message,
120 argv[remove_tags[i]] + 1);
122 for (i = 0; i < add_tags_count; i++)
123 notmuch_message_add_tag (message, argv[add_tags[i]] + 1);
125 notmuch_message_thaw (message);
127 notmuch_message_destroy (message);
130 notmuch_query_destroy (query);
131 notmuch_database_close (notmuch);