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"
22 #include "dump-restore-private.h"
24 #include "string-util.h"
29 tag_message (unused (void *ctx),
30 notmuch_database_t *notmuch,
31 const char *message_id,
32 tag_op_list_t *tag_ops,
35 notmuch_status_t status;
36 notmuch_message_t *message = NULL;
39 status = notmuch_database_find_message (notmuch, message_id, &message);
40 if (status || message == NULL) {
41 fprintf (stderr, "Warning: Cannot apply tags to %smessage: %s\n",
42 message ? "" : "missing ", message_id);
44 fprintf (stderr, "%s\n", notmuch_status_to_string (status));
48 /* In order to detect missing messages, this check/optimization is
49 * intentionally done *after* first finding the message. */
50 if ((flags & TAG_FLAG_REMOVE_ALL) || tag_op_list_size (tag_ops))
51 tag_op_list_apply (message, tag_ops, flags);
53 notmuch_message_destroy (message);
58 /* Sup dump output is one line per message. We match a sequence of
59 * non-space characters for the message-id, then one or more
60 * spaces, then a list of space-separated tags as a sequence of
61 * characters within literal '(' and ')'. */
64 parse_sup_line (void *ctx, char *line,
65 char **query_str, tag_op_list_t *tag_ops)
72 tag_op_list_reset (tag_ops);
76 /* Silently ignore blank lines */
77 if (line[0] == '\0') {
81 rerr = xregexec (®ex, line, 3, match, 0);
82 if (rerr == REG_NOMATCH) {
83 fprintf (stderr, "Warning: Ignoring invalid sup format line: %s\n",
88 *query_str = talloc_strndup (ctx, line + match[1].rm_so,
89 match[1].rm_eo - match[1].rm_so);
90 file_tags = talloc_strndup (ctx, line + match[2].rm_so,
91 match[2].rm_eo - match[2].rm_so);
93 char *tok = file_tags;
96 tag_op_list_reset (tag_ops);
98 while ((tok = strtok_len (tok + tok_len, " ", &tok_len)) != NULL) {
100 if (*(tok + tok_len) != '\0') {
101 *(tok + tok_len) = '\0';
105 if (tag_op_list_append (ctx, tag_ops, tok, FALSE))
114 notmuch_restore_command (unused (void *ctx), int argc, char *argv[])
116 notmuch_config_t *config;
117 notmuch_database_t *notmuch;
118 notmuch_bool_t accumulate = FALSE;
119 tag_op_flag_t flags = 0;
120 tag_op_list_t *tag_ops;
122 char *input_file_name = NULL;
130 int input_format = DUMP_FORMAT_AUTO;
132 config = notmuch_config_open (ctx, NULL, NULL);
136 if (notmuch_database_open (notmuch_config_get_database_path (config),
137 NOTMUCH_DATABASE_MODE_READ_WRITE, ¬much))
140 if (notmuch_config_get_maildir_synchronize_flags (config))
141 flags |= TAG_FLAG_MAILDIR_SYNC;
143 notmuch_opt_desc_t options[] = {
144 { NOTMUCH_OPT_KEYWORD, &input_format, "format", 'f',
145 (notmuch_keyword_t []){ { "auto", DUMP_FORMAT_AUTO },
146 { "batch-tag", DUMP_FORMAT_BATCH_TAG },
147 { "sup", DUMP_FORMAT_SUP },
149 { NOTMUCH_OPT_STRING, &input_file_name, "input", 'i', 0 },
150 { NOTMUCH_OPT_BOOLEAN, &accumulate, "accumulate", 'a', 0 },
154 opt_index = parse_arguments (argc, argv, options, 1);
157 /* diagnostics already printed */
162 flags |= TAG_FLAG_REMOVE_ALL;
164 if (input_file_name) {
165 input = fopen (input_file_name, "r");
167 fprintf (stderr, "Error opening %s for reading: %s\n",
168 input_file_name, strerror (errno));
173 if (opt_index < argc) {
175 "Unused positional parameter: %s\n",
181 line_len = getline (&line, &line_size, input);
185 tag_ops = tag_op_list_create (ctx);
186 if (tag_ops == NULL) {
187 fprintf (stderr, "Out of memory.\n");
191 for (p = line; (input_format == DUMP_FORMAT_AUTO) && *p; p++) {
193 input_format = DUMP_FORMAT_SUP;
196 if (input_format == DUMP_FORMAT_AUTO)
197 input_format = DUMP_FORMAT_BATCH_TAG;
199 if (input_format == DUMP_FORMAT_SUP)
200 if ( xregcomp (®ex,
201 "^([^ ]+) \\(([^)]*)\\)$",
203 INTERNAL_ERROR ("compile time constant regex failed.");
208 if (input_format == DUMP_FORMAT_SUP) {
209 ret = parse_sup_line (ctx, line, &query_string, tag_ops);
211 ret = parse_tag_line (ctx, line, TAG_FLAG_BE_GENEROUS,
212 &query_string, tag_ops);
215 if (strncmp ("id:", query_string, 3) != 0) {
216 fprintf (stderr, "Unsupported query: %s\n", query_string);
219 /* delete id: from front of string; tag_message
220 * expects a raw message-id.
222 * XXX: Note that query string id:foo and bar will be
223 * interpreted as a message id "foo and bar". This
224 * should eventually be fixed to give a better error
227 query_string = query_string + 3;
234 if (ret < 0 || tag_message (ctx, notmuch, query_string,
238 } while ((line_len = getline (&line, &line_size, input)) != -1);
240 if (input_format == DUMP_FORMAT_SUP)
246 notmuch_database_destroy (notmuch);