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 "hex-escape.h"
24 #include "string-util.h"
25 #include "zlib-extra.h"
29 /* Non-zero return indicates an error in retrieving the message,
30 * or in applying the tags. Missing messages are reported, but not
34 tag_message (unused (void *ctx),
35 notmuch_database_t *notmuch,
36 const char *message_id,
37 tag_op_list_t *tag_ops,
40 notmuch_status_t status;
41 notmuch_message_t *message = NULL;
44 status = notmuch_database_find_message (notmuch, message_id, &message);
46 fprintf (stderr, "Error applying tags to message %s: %s\n",
47 message_id, notmuch_status_to_string (status));
50 if (message == NULL) {
51 fprintf (stderr, "Warning: cannot apply tags to missing message: %s\n",
53 /* We consider this a non-fatal error. */
57 /* In order to detect missing messages, this check/optimization is
58 * intentionally done *after* first finding the message. */
59 if ((flags & TAG_FLAG_REMOVE_ALL) || tag_op_list_size (tag_ops))
60 ret = tag_op_list_apply (message, tag_ops, flags);
62 notmuch_message_destroy (message);
67 /* Sup dump output is one line per message. We match a sequence of
68 * non-space characters for the message-id, then one or more
69 * spaces, then a list of space-separated tags as a sequence of
70 * characters within literal '(' and ')'. */
73 parse_sup_line (void *ctx, char *line,
74 char **query_str, tag_op_list_t *tag_ops)
81 tag_op_list_reset (tag_ops);
85 /* Silently ignore blank lines */
86 if (line[0] == '\0') {
90 rerr = xregexec (®ex, line, 3, match, 0);
91 if (rerr == REG_NOMATCH) {
92 fprintf (stderr, "Warning: Ignoring invalid sup format line: %s\n",
97 *query_str = talloc_strndup_debug (ctx, line + match[1].rm_so,
98 match[1].rm_eo - match[1].rm_so);
100 file_tags = talloc_strndup_debug (ctx, line + match[2].rm_so,
101 match[2].rm_eo - match[2].rm_so);
103 char *tok = file_tags;
106 tag_op_list_reset (tag_ops);
108 while ((tok = strtok_len (tok + tok_len, " ", &tok_len)) != NULL) {
110 if (*(tok + tok_len) != '\0') {
111 *(tok + tok_len) = '\0';
115 if (tag_op_list_append (tag_ops, tok, FALSE))
124 notmuch_restore_command (notmuch_config_t *config, int argc, char *argv[])
126 notmuch_database_t *notmuch;
127 notmuch_bool_t accumulate = FALSE;
128 tag_op_flag_t flags = 0;
129 tag_op_list_t *tag_ops;
131 char *input_file_name = NULL;
132 const char *name_for_error = NULL;
135 void *line_ctx = NULL;
140 int input_format = DUMP_FORMAT_AUTO;
142 if (notmuch_database_open (notmuch_config_get_database_path (config),
143 NOTMUCH_DATABASE_MODE_READ_WRITE, ¬much))
146 if (notmuch_config_get_maildir_synchronize_flags (config))
147 flags |= TAG_FLAG_MAILDIR_SYNC;
149 notmuch_opt_desc_t options[] = {
150 { NOTMUCH_OPT_KEYWORD, &input_format, "format", 'f',
151 (notmuch_keyword_t []){ { "auto", DUMP_FORMAT_AUTO },
152 { "batch-tag", DUMP_FORMAT_BATCH_TAG },
153 { "sup", DUMP_FORMAT_SUP },
155 { NOTMUCH_OPT_STRING, &input_file_name, "input", 'i', 0 },
156 { NOTMUCH_OPT_BOOLEAN, &accumulate, "accumulate", 'a', 0 },
160 opt_index = parse_arguments (argc, argv, options, 1);
166 name_for_error = input_file_name ? input_file_name : "stdin";
169 flags |= TAG_FLAG_REMOVE_ALL;
173 input = gzopen (input_file_name, "r");
175 int infd = dup (STDIN_FILENO);
177 fprintf (stderr, "Error duping stdin: %s\n",
182 input = gzdopen (infd, "r");
188 fprintf (stderr, "Error opening %s for (gzip) reading: %s\n",
189 name_for_error, strerror (errno));
194 if (opt_index < argc) {
195 fprintf (stderr, "Unused positional parameter: %s\n", argv[opt_index]);
200 tag_ops = tag_op_list_create (config);
201 if (tag_ops == NULL) {
202 fprintf (stderr, "Out of memory.\n");
208 util_status_t status;
210 status = gz_getline (line_ctx, &line, &line_len, input);
212 /* empty input file not considered an error */
213 if (status == UTIL_EOF) {
219 fprintf (stderr, "Error reading (gzipped) input: %s\n",
220 gz_error_string(status, input));
224 } while ((line_len == 0) ||
226 /* the cast is safe because we checked about for line_len < 0 */
227 (strspn (line, " \t\n") == (unsigned)line_len));
230 for (p = line; (input_format == DUMP_FORMAT_AUTO) && *p; p++) {
232 input_format = DUMP_FORMAT_SUP;
235 if (input_format == DUMP_FORMAT_AUTO)
236 input_format = DUMP_FORMAT_BATCH_TAG;
238 if (input_format == DUMP_FORMAT_SUP)
239 if ( xregcomp (®ex,
240 "^([^ ]+) \\(([^)]*)\\)$",
242 INTERNAL_ERROR ("compile time constant regex failed.");
245 char *query_string, *prefix, *term;
247 if (line_ctx != NULL)
248 talloc_free (line_ctx);
250 line_ctx = talloc_new (config);
251 if (input_format == DUMP_FORMAT_SUP) {
252 ret = parse_sup_line (line_ctx, line, &query_string, tag_ops);
254 ret = parse_tag_line (line_ctx, line, TAG_FLAG_BE_GENEROUS,
255 &query_string, tag_ops);
258 ret = parse_boolean_term (line_ctx, query_string,
260 if (ret && errno == EINVAL) {
261 fprintf (stderr, "Warning: cannot parse query: %s (skipping)\n", query_string);
264 /* This is more fatal (e.g., out of memory) */
265 fprintf (stderr, "Error parsing query: %s\n",
269 } else if (strcmp ("id", prefix) != 0) {
270 fprintf (stderr, "Warning: not an id query: %s (skipping)\n", query_string);
283 ret = tag_message (line_ctx, notmuch, query_string,
288 } while (! (ret = gz_getline (line_ctx, &line, &line_len, input)));
291 /* EOF is normal loop termination condition, UTIL_SUCCESS is
293 if (ret == UTIL_EOF) {
296 fprintf (stderr, "Error reading (gzipped) input: %s\n",
297 gz_error_string (ret, input));
301 /* currently this should not be after DONE: since we don't
302 * know if the xregcomp was reached
305 if (input_format == DUMP_FORMAT_SUP)
309 if (line_ctx != NULL)
310 talloc_free (line_ctx);
313 notmuch_database_destroy (notmuch);
315 if (input && gzclose_r (input)) {
316 fprintf (stderr, "Error closing %s: %s\n",
317 name_for_error, gzerror (input, NULL));
321 return ret ? EXIT_FAILURE : EXIT_SUCCESS;