]> git.notmuchmail.org Git - notmuch/blob - notmuch-tag.c
vim: implement sending with ,s from compose buffer
[notmuch] / notmuch-tag.c
1 /* notmuch - Not much of an email program, (just index and search)
2  *
3  * Copyright © 2009 Carl Worth
4  *
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.
9  *
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.
14  *
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/ .
17  *
18  * Author: Carl Worth <cworth@cworth.org>
19  */
20
21 #include "notmuch-client.h"
22
23 static volatile sig_atomic_t interrupted;
24
25 static void
26 handle_sigint (unused (int sig))
27 {
28     static char msg[] = "Stopping...         \n";
29     write(2, msg, sizeof(msg)-1);
30     interrupted = 1;
31 }
32
33 int
34 notmuch_tag_command (void *ctx, unused (int argc), unused (char *argv[]))
35 {
36     int *add_tags, *remove_tags;
37     int add_tags_count = 0;
38     int remove_tags_count = 0;
39     char *query_string;
40     notmuch_config_t *config;
41     notmuch_database_t *notmuch;
42     notmuch_query_t *query;
43     notmuch_messages_t *messages;
44     notmuch_message_t *message;
45     struct sigaction action;
46     int i;
47
48     /* Setup our handler for SIGINT */
49     memset (&action, 0, sizeof (struct sigaction));
50     action.sa_handler = handle_sigint;
51     sigemptyset (&action.sa_mask);
52     action.sa_flags = SA_RESTART;
53     sigaction (SIGINT, &action, NULL);
54
55     add_tags = talloc_size (ctx, argc * sizeof (int));
56     if (add_tags == NULL) {
57         fprintf (stderr, "Out of memory.\n");
58         return 1;
59     }
60
61     remove_tags = talloc_size (ctx, argc * sizeof (int));
62     if (remove_tags == NULL) {
63         fprintf (stderr, "Out of memory.\n");
64         return 1;
65     }
66
67     for (i = 0; i < argc; i++) {
68         if (strcmp (argv[i], "--") == 0) {
69             i++;
70             break;
71         }
72         if (argv[i][0] == '+') {
73             add_tags[add_tags_count++] = i;
74         } else if (argv[i][0] == '-') {
75             remove_tags[remove_tags_count++] = i;
76         } else {
77             break;
78         }
79     }
80
81     if (add_tags_count == 0 && remove_tags_count == 0) {
82         fprintf (stderr, "Error: 'notmuch tag' requires at least one tag to add or remove.\n");
83         return 1;
84     }
85
86     query_string = query_string_from_args (ctx, argc - i, &argv[i]);
87
88     if (*query_string == '\0') {
89         fprintf (stderr, "Error: notmuch tag requires at least one search term.\n");
90         return 1;
91     }
92
93     config = notmuch_config_open (ctx, NULL, NULL);
94     if (config == NULL)
95         return 1;
96
97     notmuch = notmuch_database_open (notmuch_config_get_database_path (config),
98                                      NOTMUCH_DATABASE_MODE_READ_WRITE);
99     if (notmuch == NULL)
100         return 1;
101
102     query = notmuch_query_create (notmuch, query_string);
103     if (query == NULL) {
104         fprintf (stderr, "Out of memory.\n");
105         return 1;
106     }
107
108     for (messages = notmuch_query_search_messages (query);
109          notmuch_messages_has_more (messages) && !interrupted;
110          notmuch_messages_advance (messages))
111     {
112         message = notmuch_messages_get (messages);
113
114         notmuch_message_freeze (message);
115
116         for (i = 0; i < remove_tags_count; i++)
117             notmuch_message_remove_tag (message,
118                                         argv[remove_tags[i]] + 1);
119
120         for (i = 0; i < add_tags_count; i++)
121             notmuch_message_add_tag (message, argv[add_tags[i]] + 1);
122
123         notmuch_message_thaw (message);
124
125         notmuch_message_destroy (message);
126     }
127
128     notmuch_query_destroy (query);
129     notmuch_database_close (notmuch);
130
131     return interrupted;
132 }