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>
23 #include "notmuch-client.h"
26 notmuch_restore_command (unused (void *ctx), int argc, char *argv[])
28 notmuch_config_t *config;
29 notmuch_database_t *notmuch;
30 notmuch_bool_t synchronize_flags;
31 notmuch_bool_t accumulate = FALSE;
39 config = notmuch_config_open (ctx, NULL, NULL);
43 notmuch = notmuch_database_open (notmuch_config_get_database_path (config),
44 NOTMUCH_DATABASE_MODE_READ_WRITE);
48 synchronize_flags = notmuch_config_get_maildir_synchronize_flags (config);
50 struct option options[] = {
51 { "accumulate", no_argument, 0, 'a' },
57 opt = getopt_long (argc, argv, "", options, NULL);
71 input = fopen (argv[optind], "r");
73 fprintf (stderr, "Error opening %s for reading: %s\n",
74 argv[optind], strerror (errno));
82 "Cannot read dump from more than one file: %s\n",
87 /* Dump output is one line per message. We match a sequence of
88 * non-space characters for the message-id, then one or more
89 * spaces, then a list of space-separated tags as a sequence of
90 * characters within literal '(' and ')'. */
92 "^([^ ]+) \\(([^)]*)\\)$",
95 while ((line_len = getline (&line, &line_size, input)) != -1) {
97 char *message_id, *file_tags, *tag, *next;
98 notmuch_message_t *message = NULL;
99 notmuch_status_t status;
100 notmuch_tags_t *db_tags;
103 chomp_newline (line);
105 rerr = xregexec (®ex, line, 3, match, 0);
106 if (rerr == REG_NOMATCH)
108 fprintf (stderr, "Warning: Ignoring invalid input line: %s\n",
113 message_id = xstrndup (line + match[1].rm_so,
114 match[1].rm_eo - match[1].rm_so);
115 file_tags = xstrndup (line + match[2].rm_so,
116 match[2].rm_eo - match[2].rm_so);
118 status = notmuch_database_find_message (notmuch, message_id, &message);
119 if (status || message == NULL) {
120 fprintf (stderr, "Warning: Cannot apply tags to %smessage: %s\n",
121 message ? "" : "missing ", message_id);
123 fprintf (stderr, "%s\n",
124 notmuch_status_to_string(status));
128 /* In order to detect missing messages, this check/optimization is
129 * intentionally done *after* first finding the message. */
130 if (accumulate && (file_tags == NULL || *file_tags == '\0'))
136 for (db_tags = notmuch_message_get_tags (message);
137 notmuch_tags_valid (db_tags);
138 notmuch_tags_move_to_next (db_tags))
140 const char *tag = notmuch_tags_get (db_tags);
143 db_tags_str = talloc_asprintf_append (db_tags_str, " %s", tag);
145 db_tags_str = talloc_strdup (message, tag);
148 if (((file_tags == NULL || *file_tags == '\0') &&
149 (db_tags_str == NULL || *db_tags_str == '\0')) ||
150 (file_tags && db_tags_str && strcmp (file_tags, db_tags_str) == 0))
155 notmuch_message_freeze (message);
158 notmuch_message_remove_all_tags (message);
162 tag = strsep (&next, " ");
165 status = notmuch_message_add_tag (message, tag);
168 "Error applying tag %s to message %s:\n",
170 fprintf (stderr, "%s\n",
171 notmuch_status_to_string (status));
175 notmuch_message_thaw (message);
177 if (synchronize_flags)
178 notmuch_message_tags_to_maildir_flags (message);
182 notmuch_message_destroy (message);
193 notmuch_database_close (notmuch);