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;
28 notmuch_bool_t synchronize_flags;
36 config = notmuch_config_open (ctx, NULL, NULL);
40 notmuch = notmuch_database_open (notmuch_config_get_database_path (config),
41 NOTMUCH_DATABASE_MODE_READ_WRITE);
45 synchronize_flags = notmuch_config_get_maildir_synchronize_flags (config);
47 argc--; argv++; /* skip subcommand argument */
50 input = fopen (argv[0], "r");
52 fprintf (stderr, "Error opening %s for reading: %s\n",
53 argv[0], strerror (errno));
57 printf ("No filename given. Reading dump from stdin.\n");
61 /* Dump output is one line per message. We match a sequence of
62 * non-space characters for the message-id, then one or more
63 * spaces, then a list of space-separated tags as a sequence of
64 * characters within literal '(' and ')'. */
66 "^([^ ]+) \\(([^)]*)\\)$",
69 while ((line_len = getline (&line, &line_size, input)) != -1) {
71 char *message_id, *file_tags, *tag, *next;
72 notmuch_message_t *message = NULL;
73 notmuch_status_t status;
74 notmuch_tags_t *db_tags;
79 rerr = xregexec (®ex, line, 3, match, 0);
80 if (rerr == REG_NOMATCH)
82 fprintf (stderr, "Warning: Ignoring invalid input line: %s\n",
87 message_id = xstrndup (line + match[1].rm_so,
88 match[1].rm_eo - match[1].rm_so);
89 file_tags = xstrndup (line + match[2].rm_so,
90 match[2].rm_eo - match[2].rm_so);
92 status = notmuch_database_find_message (notmuch, message_id, &message);
93 if (status || message == NULL) {
94 fprintf (stderr, "Warning: Cannot apply tags to %smessage: %s\n",
95 message ? "" : "missing ", message_id);
97 fprintf (stderr, "%s\n",
98 notmuch_status_to_string(status));
103 for (db_tags = notmuch_message_get_tags (message);
104 notmuch_tags_valid (db_tags);
105 notmuch_tags_move_to_next (db_tags))
107 const char *tag = notmuch_tags_get (db_tags);
110 db_tags_str = talloc_asprintf_append (db_tags_str, " %s", tag);
112 db_tags_str = talloc_strdup (message, tag);
115 if (((file_tags == NULL || *file_tags == '\0') &&
116 (db_tags_str == NULL || *db_tags_str == '\0')) ||
117 (file_tags && db_tags_str && strcmp (file_tags, db_tags_str) == 0))
122 notmuch_message_freeze (message);
123 notmuch_message_remove_all_tags (message);
127 tag = strsep (&next, " ");
130 status = notmuch_message_add_tag (message, tag);
133 "Error applying tag %s to message %s:\n",
135 fprintf (stderr, "%s\n",
136 notmuch_status_to_string (status));
140 notmuch_message_thaw (message);
142 if (synchronize_flags)
143 notmuch_message_tags_to_maildir_flags (message);
147 notmuch_message_destroy (message);
158 notmuch_database_close (notmuch);