]> git.notmuchmail.org Git - notmuch/blob - notmuch-restore.c
cli: add --format=text0 to notmuch search
[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 static int
29 tag_message (unused (void *ctx),
30              notmuch_database_t *notmuch,
31              const char *message_id,
32              tag_op_list_t *tag_ops,
33              tag_op_flag_t flags)
34 {
35     notmuch_status_t status;
36     notmuch_message_t *message = NULL;
37     int ret = 0;
38
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);
43         if (status)
44             fprintf (stderr, "%s\n", notmuch_status_to_string (status));
45         return 1;
46     }
47
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);
52
53     notmuch_message_destroy (message);
54
55     return ret;
56 }
57
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 ')'. */
62
63 static int
64 parse_sup_line (void *ctx, char *line,
65                 char **query_str, tag_op_list_t *tag_ops)
66 {
67
68     regmatch_t match[3];
69     char *file_tags;
70     int rerr;
71
72     tag_op_list_reset (tag_ops);
73
74     chomp_newline (line);
75
76     /* Silently ignore blank lines */
77     if (line[0] == '\0') {
78         return 1;
79     }
80
81     rerr = xregexec (&regex, line, 3, match, 0);
82     if (rerr == REG_NOMATCH) {
83         fprintf (stderr, "Warning: Ignoring invalid sup format line: %s\n",
84                  line);
85         return 1;
86     }
87
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);
92
93     char *tok = file_tags;
94     size_t tok_len = 0;
95
96     tag_op_list_reset (tag_ops);
97
98     while ((tok = strtok_len (tok + tok_len, " ", &tok_len)) != NULL) {
99
100         if (*(tok + tok_len) != '\0') {
101             *(tok + tok_len) = '\0';
102             tok_len++;
103         }
104
105         if (tag_op_list_append (ctx, tag_ops, tok, FALSE))
106             return -1;
107     }
108
109     return 0;
110
111 }
112
113 int
114 notmuch_restore_command (unused (void *ctx), int argc, char *argv[])
115 {
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;
121
122     char *input_file_name = NULL;
123     FILE *input = stdin;
124     char *line = NULL;
125     size_t line_size;
126     ssize_t line_len;
127
128     int ret = 0;
129     int opt_index;
130     int input_format = DUMP_FORMAT_AUTO;
131
132     config = notmuch_config_open (ctx, NULL, NULL);
133     if (config == NULL)
134         return 1;
135
136     if (notmuch_database_open (notmuch_config_get_database_path (config),
137                                NOTMUCH_DATABASE_MODE_READ_WRITE, &notmuch))
138         return 1;
139
140     if (notmuch_config_get_maildir_synchronize_flags (config))
141         flags |= TAG_FLAG_MAILDIR_SYNC;
142
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 },
148                                   { 0, 0 } } },
149         { NOTMUCH_OPT_STRING, &input_file_name, "input", 'i', 0 },
150         { NOTMUCH_OPT_BOOLEAN,  &accumulate, "accumulate", 'a', 0 },
151         { 0, 0, 0, 0, 0 }
152     };
153
154     opt_index = parse_arguments (argc, argv, options, 1);
155
156     if (opt_index < 0) {
157         /* diagnostics already printed */
158         return 1;
159     }
160
161     if (! accumulate)
162         flags |= TAG_FLAG_REMOVE_ALL;
163
164     if (input_file_name) {
165         input = fopen (input_file_name, "r");
166         if (input == NULL) {
167             fprintf (stderr, "Error opening %s for reading: %s\n",
168                      input_file_name, strerror (errno));
169             return 1;
170         }
171     }
172
173     if (opt_index < argc) {
174         fprintf (stderr,
175                  "Unused positional parameter: %s\n",
176                  argv[opt_index]);
177         return 1;
178     }
179     char *p;
180
181     line_len = getline (&line, &line_size, input);
182     if (line_len == 0)
183         return 0;
184
185     tag_ops = tag_op_list_create (ctx);
186     if (tag_ops == NULL) {
187         fprintf (stderr, "Out of memory.\n");
188         return 1;
189     }
190
191     for (p = line; (input_format == DUMP_FORMAT_AUTO) && *p; p++) {
192         if (*p == '(')
193             input_format = DUMP_FORMAT_SUP;
194     }
195
196     if (input_format == DUMP_FORMAT_AUTO)
197         input_format = DUMP_FORMAT_BATCH_TAG;
198
199     if (input_format == DUMP_FORMAT_SUP)
200         if ( xregcomp (&regex,
201                        "^([^ ]+) \\(([^)]*)\\)$",
202                        REG_EXTENDED) )
203             INTERNAL_ERROR ("compile time constant regex failed.");
204
205     do {
206         char *query_string;
207
208         if (input_format == DUMP_FORMAT_SUP) {
209             ret = parse_sup_line (ctx, line, &query_string, tag_ops);
210         } else {
211             ret = parse_tag_line (ctx, line, TAG_FLAG_BE_GENEROUS,
212                                   &query_string, tag_ops);
213
214             if (ret == 0) {
215                 if (strncmp ("id:", query_string, 3) != 0) {
216                     fprintf (stderr, "Warning: unsupported query: %s\n", query_string);
217                     continue;
218                 }
219                 /* delete id: from front of string; tag_message
220                  * expects a raw message-id.
221                  *
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
225                  * message.
226                  */
227                 query_string = query_string + 3;
228             }
229         }
230
231         if (ret > 0)
232             continue;
233
234         if (ret < 0 || tag_message (ctx, notmuch, query_string,
235                                     tag_ops, flags))
236             break;
237
238     }  while ((line_len = getline (&line, &line_size, input)) != -1);
239
240     if (input_format == DUMP_FORMAT_SUP)
241         regfree (&regex);
242
243     if (line)
244         free (line);
245
246     notmuch_database_destroy (notmuch);
247
248     if (input != stdin)
249         fclose (input);
250
251     return ret;
252 }