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 http://www.gnu.org/licenses/ .
18 * Author: Carl Worth <cworth@cworth.org>
21 #include "notmuch-client.h"
24 notmuch_restore_command (unused (void *ctx), int argc, char *argv[])
26 notmuch_config_t *config;
27 notmuch_database_t *notmuch;
35 config = notmuch_config_open (ctx, NULL, NULL);
39 notmuch = notmuch_database_open (notmuch_config_get_database_path (config),
40 NOTMUCH_DATABASE_MODE_READ_WRITE);
45 input = fopen (argv[0], "r");
47 fprintf (stderr, "Error opening %s for reading: %s\n",
48 argv[0], strerror (errno));
52 printf ("No filename given. Reading dump from stdin.\n");
56 /* Dump output is one line per message. We match a sequence of
57 * non-space characters for the message-id, then one or more
58 * spaces, then a list of space-separated tags as a sequence of
59 * characters within literal '(' and ')'. */
61 "^([^ ]+) \\(([^)]*)\\)$",
64 while ((line_len = getline (&line, &line_size, input)) != -1) {
66 char *message_id, *tags, *tag, *next;
67 notmuch_message_t *message;
68 notmuch_status_t status;
72 rerr = xregexec (®ex, line, 3, match, 0);
73 if (rerr == REG_NOMATCH)
75 fprintf (stderr, "Warning: Ignoring invalid input line: %s\n",
80 message_id = xstrndup (line + match[1].rm_so,
81 match[1].rm_eo - match[1].rm_so);
82 tags = xstrndup (line + match[2].rm_so,
83 match[2].rm_eo - match[2].rm_so);
85 message = notmuch_database_find_message (notmuch, message_id);
86 if (message == NULL) {
87 fprintf (stderr, "Warning: Cannot apply tags to missing message: %s\n",
92 notmuch_message_freeze (message);
94 notmuch_message_remove_all_tags (message);
98 tag = strsep (&next, " ");
101 status = notmuch_message_add_tag (message, tag);
104 "Error applying tag %s to message %s:\n",
106 fprintf (stderr, "%s\n",
107 notmuch_status_to_string (status));
111 notmuch_message_thaw (message);
112 notmuch_message_destroy (message);
123 notmuch_database_close (notmuch);