]> git.notmuchmail.org Git - notmuch/blob - notmuch-tag.c
Avoid abbreviation, preferring notmuch_config_get_maildir_synchronize_flags
[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     ssize_t ignored;
29
30     static char msg[] = "Stopping...         \n";
31     ignored = write(2, msg, sizeof(msg)-1);
32     interrupted = 1;
33 }
34
35 int
36 notmuch_tag_command (void *ctx, unused (int argc), unused (char *argv[]))
37 {
38     int *add_tags, *remove_tags;
39     int add_tags_count = 0;
40     int remove_tags_count = 0;
41     char *query_string;
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;
48     int i;
49
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);
56
57     add_tags = talloc_size (ctx, argc * sizeof (int));
58     if (add_tags == NULL) {
59         fprintf (stderr, "Out of memory.\n");
60         return 1;
61     }
62
63     remove_tags = talloc_size (ctx, argc * sizeof (int));
64     if (remove_tags == NULL) {
65         fprintf (stderr, "Out of memory.\n");
66         return 1;
67     }
68
69     for (i = 0; i < argc; i++) {
70         if (strcmp (argv[i], "--") == 0) {
71             i++;
72             break;
73         }
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;
78         } else {
79             break;
80         }
81     }
82
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");
85         return 1;
86     }
87
88     query_string = query_string_from_args (ctx, argc - i, &argv[i]);
89
90     if (*query_string == '\0') {
91         fprintf (stderr, "Error: notmuch tag requires at least one search term.\n");
92         return 1;
93     }
94
95     config = notmuch_config_open (ctx, NULL, NULL);
96     if (config == NULL)
97         return 1;
98
99     notmuch = notmuch_database_open (notmuch_config_get_database_path (config),
100                                      NOTMUCH_DATABASE_MODE_READ_WRITE);
101     if (notmuch == NULL)
102         return 1;
103     notmuch_database_set_maildir_sync (notmuch,
104                                        notmuch_config_get_maildir_synchronize_flags (config));
105
106     query = notmuch_query_create (notmuch, query_string);
107     if (query == NULL) {
108         fprintf (stderr, "Out of memory.\n");
109         return 1;
110     }
111
112     /* tagging is not interested in any special sort order */
113     notmuch_query_set_sort (query, NOTMUCH_SORT_UNSORTED);
114
115     for (messages = notmuch_query_search_messages (query);
116          notmuch_messages_valid (messages) && !interrupted;
117          notmuch_messages_move_to_next (messages))
118     {
119         message = notmuch_messages_get (messages);
120
121         notmuch_message_freeze (message);
122
123         for (i = 0; i < remove_tags_count; i++)
124             notmuch_message_remove_tag (message,
125                                         argv[remove_tags[i]] + 1);
126
127         for (i = 0; i < add_tags_count; i++)
128             notmuch_message_add_tag (message, argv[add_tags[i]] + 1);
129
130         notmuch_message_thaw (message);
131
132         notmuch_message_destroy (message);
133     }
134
135     notmuch_query_destroy (query);
136     notmuch_database_close (notmuch);
137
138     return interrupted;
139 }