]> git.notmuchmail.org Git - notmuch/blob - notmuch-restore.c
packaging: fedora: package python bindings
[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 (notmuch_config_t *config, int argc, char *argv[])
124 {
125     notmuch_database_t *notmuch;
126     notmuch_bool_t accumulate = FALSE;
127     tag_op_flag_t flags = 0;
128     tag_op_list_t *tag_ops;
129
130     char *input_file_name = NULL;
131     FILE *input = stdin;
132     char *line = NULL;
133     void *line_ctx = NULL;
134     size_t line_size;
135     ssize_t line_len;
136
137     int ret = 0;
138     int opt_index;
139     int input_format = DUMP_FORMAT_AUTO;
140
141     if (notmuch_database_open (notmuch_config_get_database_path (config),
142                                NOTMUCH_DATABASE_MODE_READ_WRITE, &notmuch))
143         return 1;
144
145     if (notmuch_config_get_maildir_synchronize_flags (config))
146         flags |= TAG_FLAG_MAILDIR_SYNC;
147
148     notmuch_opt_desc_t options[] = {
149         { NOTMUCH_OPT_KEYWORD, &input_format, "format", 'f',
150           (notmuch_keyword_t []){ { "auto", DUMP_FORMAT_AUTO },
151                                   { "batch-tag", DUMP_FORMAT_BATCH_TAG },
152                                   { "sup", DUMP_FORMAT_SUP },
153                                   { 0, 0 } } },
154         { NOTMUCH_OPT_STRING, &input_file_name, "input", 'i', 0 },
155         { NOTMUCH_OPT_BOOLEAN,  &accumulate, "accumulate", 'a', 0 },
156         { 0, 0, 0, 0, 0 }
157     };
158
159     opt_index = parse_arguments (argc, argv, options, 1);
160
161     if (opt_index < 0) {
162         /* diagnostics already printed */
163         return 1;
164     }
165
166     if (! accumulate)
167         flags |= TAG_FLAG_REMOVE_ALL;
168
169     if (input_file_name) {
170         input = fopen (input_file_name, "r");
171         if (input == NULL) {
172             fprintf (stderr, "Error opening %s for reading: %s\n",
173                      input_file_name, strerror (errno));
174             return 1;
175         }
176     }
177
178     if (opt_index < argc) {
179         fprintf (stderr,
180                  "Unused positional parameter: %s\n",
181                  argv[opt_index]);
182         return 1;
183     }
184
185     tag_ops = tag_op_list_create (config);
186     if (tag_ops == NULL) {
187         fprintf (stderr, "Out of memory.\n");
188         return 1;
189     }
190
191     do {
192         line_len = getline (&line, &line_size, input);
193
194         /* empty input file not considered an error */
195         if (line_len < 0)
196             return 0;
197
198     } while ((line_len == 0) ||
199              (line[0] == '#') ||
200              /* the cast is safe because we checked about for line_len < 0 */
201              (strspn (line, " \t\n") == (unsigned)line_len));
202
203     char *p;
204     for (p = line; (input_format == DUMP_FORMAT_AUTO) && *p; p++) {
205         if (*p == '(')
206             input_format = DUMP_FORMAT_SUP;
207     }
208
209     if (input_format == DUMP_FORMAT_AUTO)
210         input_format = DUMP_FORMAT_BATCH_TAG;
211
212     if (input_format == DUMP_FORMAT_SUP)
213         if ( xregcomp (&regex,
214                        "^([^ ]+) \\(([^)]*)\\)$",
215                        REG_EXTENDED) )
216             INTERNAL_ERROR ("compile time constant regex failed.");
217
218     do {
219         char *query_string, *prefix, *term;
220
221         if (line_ctx != NULL)
222             talloc_free (line_ctx);
223
224         line_ctx = talloc_new (config);
225         if (input_format == DUMP_FORMAT_SUP) {
226             ret = parse_sup_line (line_ctx, line, &query_string, tag_ops);
227         } else {
228             ret = parse_tag_line (line_ctx, line, TAG_FLAG_BE_GENEROUS,
229                                   &query_string, tag_ops);
230
231             if (ret == 0) {
232                 ret = parse_boolean_term (line_ctx, query_string,
233                                           &prefix, &term);
234                 if (ret && errno == EINVAL) {
235                     fprintf (stderr, "Warning: cannot parse query: %s (skipping)\n", query_string);
236                     continue;
237                 } else if (ret) {
238                     /* This is more fatal (e.g., out of memory) */
239                     fprintf (stderr, "Error parsing query: %s\n",
240                              strerror (errno));
241                     ret = 1;
242                     break;
243                 } else if (strcmp ("id", prefix) != 0) {
244                     fprintf (stderr, "Warning: not an id query: %s (skipping)\n", query_string);
245                     continue;
246                 }
247                 query_string = term;
248             }
249         }
250
251         if (ret > 0)
252             continue;
253
254         if (ret < 0)
255             break;
256
257         ret = tag_message (line_ctx, notmuch, query_string,
258                            tag_ops, flags);
259         if (ret)
260             break;
261
262     }  while ((line_len = getline (&line, &line_size, input)) != -1);
263
264     if (line_ctx != NULL)
265         talloc_free (line_ctx);
266
267     if (input_format == DUMP_FORMAT_SUP)
268         regfree (&regex);
269
270     if (line)
271         free (line);
272
273     notmuch_database_destroy (notmuch);
274
275     if (input != stdin)
276         fclose (input);
277
278     return ret;
279 }