]> git.notmuchmail.org Git - notmuch/blob - notmuch-tag.c
notmuch new: Don't ignore files with mtime of 0.
[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 int
24 notmuch_tag_command (void *ctx, unused (int argc), unused (char *argv[]))
25 {
26     int *add_tags, *remove_tags;
27     int add_tags_count = 0;
28     int remove_tags_count = 0;
29     char *query_string;
30     notmuch_config_t *config;
31     notmuch_database_t *notmuch;
32     notmuch_query_t *query;
33     notmuch_messages_t *messages;
34     notmuch_message_t *message;
35     int i;
36
37     add_tags = talloc_size (ctx, argc * sizeof (int));
38     if (add_tags == NULL) {
39         fprintf (stderr, "Out of memory.\n");
40         return 1;
41     }
42
43     remove_tags = talloc_size (ctx, argc * sizeof (int));
44     if (remove_tags == NULL) {
45         fprintf (stderr, "Out of memory.\n");
46         return 1;
47     }
48
49     for (i = 0; i < argc; i++) {
50         if (strcmp (argv[i], "--") == 0) {
51             i++;
52             break;
53         }
54         if (argv[i][0] == '+') {
55             add_tags[add_tags_count++] = i;
56         } else if (argv[i][0] == '-') {
57             remove_tags[remove_tags_count++] = i;
58         } else {
59             break;
60         }
61     }
62
63     if (add_tags_count == 0 && remove_tags_count == 0) {
64         fprintf (stderr, "Error: 'notmuch tag' requires at least one tag to add or remove.\n");
65         return 1;
66     }
67
68     if (i == argc) {
69         fprintf (stderr, "Error: 'notmuch tag' requires at least one search term.\n");
70         return 1;
71     }
72
73     config = notmuch_config_open (ctx, NULL, NULL);
74     if (config == NULL)
75         return 1;
76
77     notmuch = notmuch_database_open (notmuch_config_get_database_path (config));
78     if (notmuch == NULL)
79         return 1;
80
81     query_string = query_string_from_args (ctx, argc - i, &argv[i]);
82
83     query = notmuch_query_create (notmuch, query_string);
84     if (query == NULL) {
85         fprintf (stderr, "Out of memory.\n");
86         return 1;
87     }
88
89     for (messages = notmuch_query_search_messages (query);
90          notmuch_messages_has_more (messages);
91          notmuch_messages_advance (messages))
92     {
93         message = notmuch_messages_get (messages);
94
95         notmuch_message_freeze (message);
96
97         for (i = 0; i < remove_tags_count; i++)
98             notmuch_message_remove_tag (message,
99                                         argv[remove_tags[i]] + 1);
100
101         for (i = 0; i < add_tags_count; i++)
102             notmuch_message_add_tag (message, argv[add_tags[i]] + 1);
103
104         notmuch_message_thaw (message);
105
106         notmuch_message_destroy (message);
107     }
108
109     notmuch_query_destroy (query);
110     notmuch_database_close (notmuch);
111
112     return 0;
113 }