]> git.notmuchmail.org Git - notmuch/blob - notmuch-restore.c
man: tag changes are applied in the order they are specified in notmuch tag
[notmuch] / notmuch-restore.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 #include "dump-restore-private.h"
23 #include "tag-util.h"
24 #include "string-util.h"
25
26 static regex_t regex;
27
28 /* Non-zero return indicates an error in retrieving the message,
29  * or in applying the tags.  Missing messages are reported, but not
30  * considered errors.
31  */
32 static int
33 tag_message (unused (void *ctx),
34              notmuch_database_t *notmuch,
35              const char *message_id,
36              tag_op_list_t *tag_ops,
37              tag_op_flag_t flags)
38 {
39     notmuch_status_t status;
40     notmuch_message_t *message = NULL;
41     int ret = 0;
42
43     status = notmuch_database_find_message (notmuch, message_id, &message);
44     if (status) {
45         fprintf (stderr, "Error applying tags to message %s: %s\n",
46                  message_id, notmuch_status_to_string (status));
47         return 1;
48     }
49     if (message == NULL) {
50         fprintf (stderr, "Warning: cannot apply tags to missing message: %s\n",
51                  message_id);
52         /* We consider this a non-fatal error. */
53         return 0;
54     }
55
56     /* In order to detect missing messages, this check/optimization is
57      * intentionally done *after* first finding the message. */
58     if ((flags & TAG_FLAG_REMOVE_ALL) || tag_op_list_size (tag_ops))
59         ret = tag_op_list_apply (message, tag_ops, flags);
60
61     notmuch_message_destroy (message);
62
63     return ret;
64 }
65
66 /* Sup dump output is one line per message. We match a sequence of
67  * non-space characters for the message-id, then one or more
68  * spaces, then a list of space-separated tags as a sequence of
69  * characters within literal '(' and ')'. */
70
71 static int
72 parse_sup_line (void *ctx, char *line,
73                 char **query_str, tag_op_list_t *tag_ops)
74 {
75
76     regmatch_t match[3];
77     char *file_tags;
78     int rerr;
79
80     tag_op_list_reset (tag_ops);
81
82     chomp_newline (line);
83
84     /* Silently ignore blank lines */
85     if (line[0] == '\0') {
86         return 1;
87     }
88
89     rerr = xregexec (&regex, line, 3, match, 0);
90     if (rerr == REG_NOMATCH) {
91         fprintf (stderr, "Warning: Ignoring invalid sup format line: %s\n",
92                  line);
93         return 1;
94     }
95
96     *query_str = talloc_strndup_debug (ctx, line + match[1].rm_so,
97                                        match[1].rm_eo - match[1].rm_so);
98
99     file_tags = talloc_strndup_debug (ctx, line + match[2].rm_so,
100                                       match[2].rm_eo - match[2].rm_so);
101
102     char *tok = file_tags;
103     size_t tok_len = 0;
104
105     tag_op_list_reset (tag_ops);
106
107     while ((tok = strtok_len (tok + tok_len, " ", &tok_len)) != NULL) {
108
109         if (*(tok + tok_len) != '\0') {
110             *(tok + tok_len) = '\0';
111             tok_len++;
112         }
113
114         if (tag_op_list_append (tag_ops, tok, FALSE))
115             return -1;
116     }
117
118     return 0;
119
120 }
121
122 int
123 notmuch_restore_command (unused (void *ctx), int argc, char *argv[])
124 {
125     notmuch_config_t *config;
126     notmuch_database_t *notmuch;
127     notmuch_bool_t accumulate = FALSE;
128     tag_op_flag_t flags = 0;
129     tag_op_list_t *tag_ops;
130
131     char *input_file_name = NULL;
132     FILE *input = stdin;
133     char *line = NULL;
134     void *line_ctx = NULL;
135     size_t line_size;
136     ssize_t line_len;
137
138     int ret = 0;
139     int opt_index;
140     int input_format = DUMP_FORMAT_AUTO;
141
142     config = notmuch_config_open (ctx, NULL, NULL);
143     if (config == NULL)
144         return 1;
145
146     if (notmuch_database_open (notmuch_config_get_database_path (config),
147                                NOTMUCH_DATABASE_MODE_READ_WRITE, &notmuch))
148         return 1;
149
150     if (notmuch_config_get_maildir_synchronize_flags (config))
151         flags |= TAG_FLAG_MAILDIR_SYNC;
152
153     notmuch_opt_desc_t options[] = {
154         { NOTMUCH_OPT_KEYWORD, &input_format, "format", 'f',
155           (notmuch_keyword_t []){ { "auto", DUMP_FORMAT_AUTO },
156                                   { "batch-tag", DUMP_FORMAT_BATCH_TAG },
157                                   { "sup", DUMP_FORMAT_SUP },
158                                   { 0, 0 } } },
159         { NOTMUCH_OPT_STRING, &input_file_name, "input", 'i', 0 },
160         { NOTMUCH_OPT_BOOLEAN,  &accumulate, "accumulate", 'a', 0 },
161         { 0, 0, 0, 0, 0 }
162     };
163
164     opt_index = parse_arguments (argc, argv, options, 1);
165
166     if (opt_index < 0) {
167         /* diagnostics already printed */
168         return 1;
169     }
170
171     if (! accumulate)
172         flags |= TAG_FLAG_REMOVE_ALL;
173
174     if (input_file_name) {
175         input = fopen (input_file_name, "r");
176         if (input == NULL) {
177             fprintf (stderr, "Error opening %s for reading: %s\n",
178                      input_file_name, strerror (errno));
179             return 1;
180         }
181     }
182
183     if (opt_index < argc) {
184         fprintf (stderr,
185                  "Unused positional parameter: %s\n",
186                  argv[opt_index]);
187         return 1;
188     }
189
190     tag_ops = tag_op_list_create (ctx);
191     if (tag_ops == NULL) {
192         fprintf (stderr, "Out of memory.\n");
193         return 1;
194     }
195
196     do {
197         line_len = getline (&line, &line_size, input);
198
199         /* empty input file not considered an error */
200         if (line_len < 0)
201             return 0;
202
203     } while ((line_len == 0) ||
204              (line[0] == '#') ||
205              /* the cast is safe because we checked about for line_len < 0 */
206              (strspn (line, " \t\n") == (unsigned)line_len));
207
208     char *p;
209     for (p = line; (input_format == DUMP_FORMAT_AUTO) && *p; p++) {
210         if (*p == '(')
211             input_format = DUMP_FORMAT_SUP;
212     }
213
214     if (input_format == DUMP_FORMAT_AUTO)
215         input_format = DUMP_FORMAT_BATCH_TAG;
216
217     if (input_format == DUMP_FORMAT_SUP)
218         if ( xregcomp (&regex,
219                        "^([^ ]+) \\(([^)]*)\\)$",
220                        REG_EXTENDED) )
221             INTERNAL_ERROR ("compile time constant regex failed.");
222
223     do {
224         char *query_string, *prefix, *term;
225
226         if (line_ctx != NULL)
227             talloc_free (line_ctx);
228
229         line_ctx = talloc_new (ctx);
230         if (input_format == DUMP_FORMAT_SUP) {
231             ret = parse_sup_line (line_ctx, line, &query_string, tag_ops);
232         } else {
233             ret = parse_tag_line (line_ctx, line, TAG_FLAG_BE_GENEROUS,
234                                   &query_string, tag_ops);
235
236             if (ret == 0) {
237                 ret = parse_boolean_term (line_ctx, query_string,
238                                           &prefix, &term);
239                 if (ret && errno == EINVAL) {
240                     fprintf (stderr, "Warning: cannot parse query: %s (skipping)\n", query_string);
241                     continue;
242                 } else if (ret) {
243                     /* This is more fatal (e.g., out of memory) */
244                     fprintf (stderr, "Error parsing query: %s\n",
245                              strerror (errno));
246                     ret = 1;
247                     break;
248                 } else if (strcmp ("id", prefix) != 0) {
249                     fprintf (stderr, "Warning: not an id query: %s (skipping)\n", query_string);
250                     continue;
251                 }
252                 query_string = term;
253             }
254         }
255
256         if (ret > 0)
257             continue;
258
259         if (ret < 0)
260             break;
261
262         ret = tag_message (line_ctx, notmuch, query_string,
263                            tag_ops, flags);
264         if (ret)
265             break;
266
267     }  while ((line_len = getline (&line, &line_size, input)) != -1);
268
269     if (line_ctx != NULL)
270         talloc_free (line_ctx);
271
272     if (input_format == DUMP_FORMAT_SUP)
273         regfree (&regex);
274
275     if (line)
276         free (line);
277
278     notmuch_database_destroy (notmuch);
279
280     if (input != stdin)
281         fclose (input);
282
283     return ret;
284 }