]> git.notmuchmail.org Git - notmuch/blob - notmuch-restore.c
NEWS for emacs part visibility change
[notmuch] / notmuch-restore.c
1 /* notmuch - Not much of an email program, (just index and search)
2  *
3  * Copyright © 2009 Carl Worth
4  *
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.
9  *
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.
14  *
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/ .
17  *
18  * Author: Carl Worth <cworth@cworth.org>
19  */
20
21 #include "notmuch-client.h"
22 #include "dump-restore-private.h"
23 #include "tag-util.h"
24 #include "string-util.h"
25
26 static regex_t regex;
27
28 /* Non-zero return indicates an error in retrieving the message,
29  * or in applying the tags.
30  */
31 static int
32 tag_message (unused (void *ctx),
33              notmuch_database_t *notmuch,
34              const char *message_id,
35              tag_op_list_t *tag_ops,
36              tag_op_flag_t flags)
37 {
38     notmuch_status_t status;
39     notmuch_message_t *message = NULL;
40     int ret = 0;
41
42     status = notmuch_database_find_message (notmuch, message_id, &message);
43     if (status || message == NULL) {
44         fprintf (stderr, "Warning: cannot apply tags to %smessage: %s\n",
45                  message ? "" : "missing ", message_id);
46         if (status)
47             fprintf (stderr, "%s\n", notmuch_status_to_string (status));
48         return 1;
49     }
50
51     /* In order to detect missing messages, this check/optimization is
52      * intentionally done *after* first finding the message. */
53     if ((flags & TAG_FLAG_REMOVE_ALL) || tag_op_list_size (tag_ops))
54         ret = tag_op_list_apply (message, tag_ops, flags);
55
56     notmuch_message_destroy (message);
57
58     return ret;
59 }
60
61 /* Sup 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 ')'. */
65
66 static int
67 parse_sup_line (void *ctx, char *line,
68                 char **query_str, tag_op_list_t *tag_ops)
69 {
70
71     regmatch_t match[3];
72     char *file_tags;
73     int rerr;
74
75     tag_op_list_reset (tag_ops);
76
77     chomp_newline (line);
78
79     /* Silently ignore blank lines */
80     if (line[0] == '\0') {
81         return 1;
82     }
83
84     rerr = xregexec (&regex, line, 3, match, 0);
85     if (rerr == REG_NOMATCH) {
86         fprintf (stderr, "Warning: Ignoring invalid sup format line: %s\n",
87                  line);
88         return 1;
89     }
90
91     *query_str = talloc_strndup (ctx, line + match[1].rm_so,
92                                  match[1].rm_eo - match[1].rm_so);
93     file_tags = talloc_strndup (ctx, line + match[2].rm_so,
94                                 match[2].rm_eo - match[2].rm_so);
95
96     char *tok = file_tags;
97     size_t tok_len = 0;
98
99     tag_op_list_reset (tag_ops);
100
101     while ((tok = strtok_len (tok + tok_len, " ", &tok_len)) != NULL) {
102
103         if (*(tok + tok_len) != '\0') {
104             *(tok + tok_len) = '\0';
105             tok_len++;
106         }
107
108         if (tag_op_list_append (tag_ops, tok, FALSE))
109             return -1;
110     }
111
112     return 0;
113
114 }
115
116 int
117 notmuch_restore_command (unused (void *ctx), int argc, char *argv[])
118 {
119     notmuch_config_t *config;
120     notmuch_database_t *notmuch;
121     notmuch_bool_t accumulate = FALSE;
122     tag_op_flag_t flags = 0;
123     tag_op_list_t *tag_ops;
124
125     char *input_file_name = NULL;
126     FILE *input = stdin;
127     char *line = NULL;
128     void *line_ctx = NULL;
129     size_t line_size;
130     ssize_t line_len;
131
132     int ret = 0;
133     int opt_index;
134     int input_format = DUMP_FORMAT_AUTO;
135
136     config = notmuch_config_open (ctx, NULL, NULL);
137     if (config == NULL)
138         return 1;
139
140     if (notmuch_database_open (notmuch_config_get_database_path (config),
141                                NOTMUCH_DATABASE_MODE_READ_WRITE, &notmuch))
142         return 1;
143
144     if (notmuch_config_get_maildir_synchronize_flags (config))
145         flags |= TAG_FLAG_MAILDIR_SYNC;
146
147     notmuch_opt_desc_t options[] = {
148         { NOTMUCH_OPT_KEYWORD, &input_format, "format", 'f',
149           (notmuch_keyword_t []){ { "auto", DUMP_FORMAT_AUTO },
150                                   { "batch-tag", DUMP_FORMAT_BATCH_TAG },
151                                   { "sup", DUMP_FORMAT_SUP },
152                                   { 0, 0 } } },
153         { NOTMUCH_OPT_STRING, &input_file_name, "input", 'i', 0 },
154         { NOTMUCH_OPT_BOOLEAN,  &accumulate, "accumulate", 'a', 0 },
155         { 0, 0, 0, 0, 0 }
156     };
157
158     opt_index = parse_arguments (argc, argv, options, 1);
159
160     if (opt_index < 0) {
161         /* diagnostics already printed */
162         return 1;
163     }
164
165     if (! accumulate)
166         flags |= TAG_FLAG_REMOVE_ALL;
167
168     if (input_file_name) {
169         input = fopen (input_file_name, "r");
170         if (input == NULL) {
171             fprintf (stderr, "Error opening %s for reading: %s\n",
172                      input_file_name, strerror (errno));
173             return 1;
174         }
175     }
176
177     if (opt_index < argc) {
178         fprintf (stderr,
179                  "Unused positional parameter: %s\n",
180                  argv[opt_index]);
181         return 1;
182     }
183     char *p;
184
185     line_len = getline (&line, &line_size, input);
186     if (line_len == 0)
187         return 0;
188
189     tag_ops = tag_op_list_create (ctx);
190     if (tag_ops == NULL) {
191         fprintf (stderr, "Out of memory.\n");
192         return 1;
193     }
194
195     for (p = line; (input_format == DUMP_FORMAT_AUTO) && *p; p++) {
196         if (*p == '(')
197             input_format = DUMP_FORMAT_SUP;
198     }
199
200     if (input_format == DUMP_FORMAT_AUTO)
201         input_format = DUMP_FORMAT_BATCH_TAG;
202
203     if (input_format == DUMP_FORMAT_SUP)
204         if ( xregcomp (&regex,
205                        "^([^ ]+) \\(([^)]*)\\)$",
206                        REG_EXTENDED) )
207             INTERNAL_ERROR ("compile time constant regex failed.");
208
209     do {
210         char *query_string;
211
212         if (line_ctx != NULL)
213             talloc_free (line_ctx);
214
215         line_ctx = talloc_new (ctx);
216         if (input_format == DUMP_FORMAT_SUP) {
217             ret = parse_sup_line (line_ctx, line, &query_string, tag_ops);
218         } else {
219             ret = parse_tag_line (line_ctx, line, TAG_FLAG_BE_GENEROUS,
220                                   &query_string, tag_ops);
221
222             if (ret == 0) {
223                 if (strncmp ("id:", query_string, 3) != 0) {
224                     fprintf (stderr, "Warning: unsupported query: %s\n", query_string);
225                     continue;
226                 }
227                 /* delete id: from front of string; tag_message
228                  * expects a raw message-id.
229                  *
230                  * XXX: Note that query string id:foo and bar will be
231                  * interpreted as a message id "foo and bar". This
232                  * should eventually be fixed to give a better error
233                  * message.
234                  */
235                 query_string = query_string + 3;
236             }
237         }
238
239         if (ret > 0)
240             continue;
241
242         if (ret < 0)
243             break;
244
245         ret = tag_message (line_ctx, notmuch, query_string,
246                            tag_ops, flags);
247         if (ret)
248             break;
249
250     }  while ((line_len = getline (&line, &line_size, input)) != -1);
251
252     if (line_ctx != NULL)
253         talloc_free (line_ctx);
254
255     if (input_format == DUMP_FORMAT_SUP)
256         regfree (&regex);
257
258     if (line)
259         free (line);
260
261     notmuch_database_destroy (notmuch);
262
263     if (input != stdin)
264         fclose (input);
265
266     return ret;
267 }