1 /* notmuch - Not much of an email program, (just index and search)
3 * Copyright © 2009 Carl Worth
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.
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.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see https://www.gnu.org/licenses/ .
18 * Author: Carl Worth <cworth@cworth.org>
21 #include "notmuch-client.h"
22 #include "hex-escape.h"
24 #include "string-util.h"
25 #include "zlib-extra.h"
28 process_config_line (notmuch_database_t *notmuch, const char *line)
30 const char *key_p, *val_p;
32 size_t key_len, val_len;
33 const char *delim = " \t\n";
34 int ret = EXIT_FAILURE;
36 void *local = talloc_new (NULL);
38 key_p = strtok_len_c (line, delim, &key_len);
39 val_p = strtok_len_c (key_p + key_len, delim, &val_len);
41 key = talloc_strndup (local, key_p, key_len);
42 val = talloc_strndup (local, val_p, val_len);
43 if (hex_decode_inplace (key) != HEX_SUCCESS ||
44 hex_decode_inplace (val) != HEX_SUCCESS ) {
45 fprintf (stderr, "hex decoding failure on line %s\n", line);
49 if (print_status_database ("notmuch restore", notmuch,
50 notmuch_database_set_config (notmuch, key, val)))
61 process_properties_line (notmuch_database_t *notmuch, const char *line)
63 const char *id_p, *tok;
64 size_t id_len = 0, tok_len = 0;
67 notmuch_message_t *message = NULL;
68 const char *delim = " \t\n";
69 int ret = EXIT_FAILURE;
71 void *local = talloc_new (NULL);
73 id_p = strtok_len_c (line, delim, &id_len);
74 id = talloc_strndup (local, id_p, id_len);
75 if (hex_decode_inplace (id) != HEX_SUCCESS) {
76 fprintf (stderr, "hex decoding failure on line %s\n", line);
80 if (print_status_database ("notmuch restore", notmuch,
81 notmuch_database_find_message (notmuch, id, &message)))
84 if (print_status_database ("notmuch restore", notmuch,
85 notmuch_message_remove_all_properties (message, NULL)))
90 while ((tok = strtok_len_c (tok + tok_len, delim, &tok_len)) != NULL) {
92 size_t off = strcspn (tok, "=");
94 fprintf (stderr, "unparsable token %s\n", tok);
98 key = talloc_strndup (local, tok, off);
99 value = talloc_strndup (local, tok + off + 1, tok_len - off - 1);
101 if (hex_decode_inplace (key) != HEX_SUCCESS) {
102 fprintf (stderr, "hex decoding failure on key %s\n", key);
106 if (hex_decode_inplace (value) != HEX_SUCCESS) {
107 fprintf (stderr, "hex decoding failure on value %s\n", value);
111 if (print_status_database ("notmuch restore", notmuch,
112 notmuch_message_add_property (message, key, value)))
125 static regex_t regex;
127 /* Non-zero return indicates an error in retrieving the message,
128 * or in applying the tags. Missing messages are reported, but not
132 tag_message (unused (void *ctx),
133 notmuch_database_t *notmuch,
134 const char *message_id,
135 tag_op_list_t *tag_ops,
138 notmuch_status_t status;
139 notmuch_message_t *message = NULL;
142 status = notmuch_database_find_message (notmuch, message_id, &message);
144 fprintf (stderr, "Error applying tags to message %s: %s\n",
145 message_id, notmuch_status_to_string (status));
148 if (message == NULL) {
149 fprintf (stderr, "Warning: cannot apply tags to missing message: %s\n",
151 /* We consider this a non-fatal error. */
155 /* In order to detect missing messages, this check/optimization is
156 * intentionally done *after* first finding the message. */
157 if ((flags & TAG_FLAG_REMOVE_ALL) || tag_op_list_size (tag_ops))
158 ret = tag_op_list_apply (message, tag_ops, flags);
160 notmuch_message_destroy (message);
165 /* Sup dump output is one line per message. We match a sequence of
166 * non-space characters for the message-id, then one or more
167 * spaces, then a list of space-separated tags as a sequence of
168 * characters within literal '(' and ')'. */
171 parse_sup_line (void *ctx, char *line,
172 char **query_str, tag_op_list_t *tag_ops)
179 tag_op_list_reset (tag_ops);
181 chomp_newline (line);
183 /* Silently ignore blank lines */
184 if (line[0] == '\0') {
188 rerr = xregexec (®ex, line, 3, match, 0);
189 if (rerr == REG_NOMATCH) {
190 fprintf (stderr, "Warning: Ignoring invalid sup format line: %s\n",
195 *query_str = talloc_strndup_debug (ctx, line + match[1].rm_so,
196 match[1].rm_eo - match[1].rm_so);
198 file_tags = talloc_strndup_debug (ctx, line + match[2].rm_so,
199 match[2].rm_eo - match[2].rm_so);
201 char *tok = file_tags;
204 tag_op_list_reset (tag_ops);
206 while ((tok = strtok_len (tok + tok_len, " ", &tok_len)) != NULL) {
208 if (*(tok + tok_len) != '\0') {
209 *(tok + tok_len) = '\0';
213 if (tag_op_list_append (tag_ops, tok, false))
222 notmuch_restore_command (notmuch_database_t *notmuch, int argc, char *argv[])
224 bool accumulate = false;
225 tag_op_flag_t flags = 0;
226 tag_op_list_t *tag_ops;
228 const char *input_file_name = NULL;
229 const char *name_for_error = NULL;
232 void *line_ctx = NULL;
238 int input_format = DUMP_FORMAT_AUTO;
240 notmuch_bool_t synchronize_flags;
242 if (print_status_database (
245 notmuch_config_get_bool (notmuch, NOTMUCH_CONFIG_SYNC_MAILDIR_FLAGS,
246 &synchronize_flags)))
249 if (synchronize_flags)
250 flags |= TAG_FLAG_MAILDIR_SYNC;
252 notmuch_opt_desc_t options[] = {
253 { .opt_keyword = &input_format, .name = "format", .keywords =
254 (notmuch_keyword_t []){ { "auto", DUMP_FORMAT_AUTO },
255 { "batch-tag", DUMP_FORMAT_BATCH_TAG },
256 { "sup", DUMP_FORMAT_SUP },
258 { .opt_flags = &include, .name = "include", .keywords =
259 (notmuch_keyword_t []){ { "config", DUMP_INCLUDE_CONFIG },
260 { "properties", DUMP_INCLUDE_PROPERTIES },
261 { "tags", DUMP_INCLUDE_TAGS } } },
263 { .opt_string = &input_file_name, .name = "input" },
264 { .opt_bool = &accumulate, .name = "accumulate" },
265 { .opt_inherit = notmuch_shared_options },
269 opt_index = parse_arguments (argc, argv, options, 1);
275 notmuch_process_shared_options (argv[0]);
276 notmuch_exit_if_unmatched_db_uuid (notmuch);
279 include = DUMP_INCLUDE_CONFIG | DUMP_INCLUDE_PROPERTIES | DUMP_INCLUDE_TAGS;
282 name_for_error = input_file_name ? input_file_name : "stdin";
285 flags |= TAG_FLAG_REMOVE_ALL;
289 input = gzopen (input_file_name, "r");
291 int infd = dup (STDIN_FILENO);
293 fprintf (stderr, "Error duping stdin: %s\n",
298 input = gzdopen (infd, "r");
304 fprintf (stderr, "Error opening %s for (gzip) reading: %s\n",
305 name_for_error, strerror (errno));
310 if (opt_index < argc) {
311 fprintf (stderr, "Unused positional parameter: %s\n", argv[opt_index]);
316 tag_ops = tag_op_list_create (notmuch);
317 if (tag_ops == NULL) {
318 fprintf (stderr, "Out of memory.\n");
324 util_status_t status;
326 status = gz_getline (line_ctx, &line, &line_len, input);
328 /* empty input file not considered an error */
329 if (status == UTIL_EOF) {
335 fprintf (stderr, "Error reading (gzipped) input: %s\n",
336 gz_error_string (status, input));
341 if ((include & DUMP_INCLUDE_CONFIG) && line_len >= 2 && line[0] == '#' && line[1] == '@') {
342 ret = process_config_line (notmuch, line + 2);
346 if ((include & DUMP_INCLUDE_PROPERTIES) && line_len >= 2 && line[0] == '#' && line[1] ==
348 ret = process_properties_line (notmuch, line + 2);
353 } while ((line_len == 0) ||
355 /* the cast is safe because we checked about for line_len < 0 */
356 (strspn (line, " \t\n") == (unsigned) line_len));
358 if (! ((include & DUMP_INCLUDE_TAGS) || (include & DUMP_INCLUDE_PROPERTIES))) {
365 for (p = line; (input_format == DUMP_FORMAT_AUTO) && *p; p++) {
367 input_format = DUMP_FORMAT_SUP;
370 if (input_format == DUMP_FORMAT_AUTO)
371 input_format = DUMP_FORMAT_BATCH_TAG;
373 if (input_format == DUMP_FORMAT_SUP)
374 if ( xregcomp (®ex,
375 "^([^ ]+) \\(([^)]*)\\)$",
377 INTERNAL_ERROR ("compile time constant regex failed.");
380 char *query_string, *prefix, *term;
382 if (line_ctx != NULL)
383 talloc_free (line_ctx);
385 line_ctx = talloc_new (notmuch);
387 if ((include & DUMP_INCLUDE_PROPERTIES) && line_len >= 2 && line[0] == '#' && line[1] ==
389 ret = process_properties_line (notmuch, line + 2);
394 if (input_format == DUMP_FORMAT_SUP) {
395 ret = parse_sup_line (line_ctx, line, &query_string, tag_ops);
397 ret = parse_tag_line (line_ctx, line, TAG_FLAG_BE_GENEROUS,
398 &query_string, tag_ops);
401 ret = parse_boolean_term (line_ctx, query_string,
403 if (ret && errno == EINVAL) {
404 fprintf (stderr, "Warning: cannot parse query: %s (skipping)\n", query_string);
407 /* This is more fatal (e.g., out of memory) */
408 fprintf (stderr, "Error parsing query: %s\n",
412 } else if (strcmp ("id", prefix) != 0) {
413 fprintf (stderr, "Warning: not an id query: %s (skipping)\n", query_string);
426 ret = tag_message (line_ctx, notmuch, query_string,
431 } while (! (ret = gz_getline (line_ctx, &line, &line_len, input)));
434 /* EOF is normal loop termination condition, UTIL_SUCCESS is
436 if (ret == UTIL_EOF) {
439 fprintf (stderr, "Error reading (gzipped) input: %s\n",
440 gz_error_string (ret, input));
444 /* currently this should not be after DONE: since we don't
445 * know if the xregcomp was reached
448 if (input_format == DUMP_FORMAT_SUP)
452 if (line_ctx != NULL)
453 talloc_free (line_ctx);
456 notmuch_database_destroy (notmuch);
459 errnum = gzclose_r (input);
461 fprintf (stderr, "Error closing %s: %d\n",
462 name_for_error, errnum);
467 return ret ? EXIT_FAILURE : EXIT_SUCCESS;