]> git.notmuchmail.org Git - notmuch/blob - notmuch-restore.c
emacs: use a single history for all searches
[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
23 int
24 notmuch_restore_command (unused (void *ctx), int argc, char *argv[])
25 {
26     notmuch_config_t *config;
27     notmuch_database_t *notmuch;
28     notmuch_bool_t synchronize_flags;
29     notmuch_bool_t accumulate = FALSE;
30     char *input_file_name = NULL;
31     FILE *input = stdin;
32     char *line = NULL;
33     size_t line_size;
34     ssize_t line_len;
35     regex_t regex;
36     int rerr;
37     int opt_index;
38
39     config = notmuch_config_open (ctx, NULL, NULL);
40     if (config == NULL)
41         return 1;
42
43     notmuch = notmuch_database_open (notmuch_config_get_database_path (config),
44                                      NOTMUCH_DATABASE_MODE_READ_WRITE);
45     if (notmuch == NULL)
46         return 1;
47
48     synchronize_flags = notmuch_config_get_maildir_synchronize_flags (config);
49
50     notmuch_opt_desc_t options[] = {
51         { NOTMUCH_OPT_POSITION, &input_file_name, 0, 0, 0 },
52         { NOTMUCH_OPT_BOOLEAN,  &accumulate, "accumulate", 'a', 0 },
53         { 0, 0, 0, 0, 0 }
54     };
55
56     opt_index = parse_arguments (argc, argv, options, 1);
57
58     if (opt_index < 0) {
59         /* diagnostics already printed */
60         return 1;
61     }
62
63     if (input_file_name) {
64         input = fopen (input_file_name, "r");
65         if (input == NULL) {
66             fprintf (stderr, "Error opening %s for reading: %s\n",
67                      input_file_name, strerror (errno));
68             return 1;
69         }
70         optind++;
71     }
72
73     if (opt_index < argc) {
74         fprintf (stderr,
75          "Cannot read dump from more than one file: %s\n",
76                  argv[optind]);
77         return 1;
78     }
79
80     /* Dump output is one line per message. We match a sequence of
81      * non-space characters for the message-id, then one or more
82      * spaces, then a list of space-separated tags as a sequence of
83      * characters within literal '(' and ')'. */
84     if ( xregcomp (&regex,
85                    "^([^ ]+) \\(([^)]*)\\)$",
86                    REG_EXTENDED) )
87         INTERNAL_ERROR("compile time constant regex failed.");
88
89     while ((line_len = getline (&line, &line_size, input)) != -1) {
90         regmatch_t match[3];
91         char *message_id, *file_tags, *tag, *next;
92         notmuch_message_t *message = NULL;
93         notmuch_status_t status;
94         notmuch_tags_t *db_tags;
95         char *db_tags_str;
96
97         chomp_newline (line);
98
99         rerr = xregexec (&regex, line, 3, match, 0);
100         if (rerr == REG_NOMATCH)
101         {
102             fprintf (stderr, "Warning: Ignoring invalid input line: %s\n",
103                      line);
104             continue;
105         }
106
107         message_id = xstrndup (line + match[1].rm_so,
108                                match[1].rm_eo - match[1].rm_so);
109         file_tags = xstrndup (line + match[2].rm_so,
110                               match[2].rm_eo - match[2].rm_so);
111
112         status = notmuch_database_find_message (notmuch, message_id, &message);
113         if (status || message == NULL) {
114             fprintf (stderr, "Warning: Cannot apply tags to %smessage: %s\n",
115                      message ? "" : "missing ", message_id);
116             if (status)
117                 fprintf (stderr, "%s\n",
118                          notmuch_status_to_string(status));
119             goto NEXT_LINE;
120         }
121
122         /* In order to detect missing messages, this check/optimization is
123          * intentionally done *after* first finding the message.  */
124         if (accumulate && (file_tags == NULL || *file_tags == '\0'))
125         {
126             goto NEXT_LINE;
127         }
128
129         db_tags_str = NULL;
130         for (db_tags = notmuch_message_get_tags (message);
131              notmuch_tags_valid (db_tags);
132              notmuch_tags_move_to_next (db_tags))
133         {
134             const char *tag = notmuch_tags_get (db_tags);
135
136             if (db_tags_str)
137                 db_tags_str = talloc_asprintf_append (db_tags_str, " %s", tag);
138             else
139                 db_tags_str = talloc_strdup (message, tag);
140         }
141
142         if (((file_tags == NULL || *file_tags == '\0') &&
143              (db_tags_str == NULL || *db_tags_str == '\0')) ||
144             (file_tags && db_tags_str && strcmp (file_tags, db_tags_str) == 0))
145         {
146             goto NEXT_LINE;
147         }
148
149         notmuch_message_freeze (message);
150
151         if (!accumulate)
152             notmuch_message_remove_all_tags (message);
153
154         next = file_tags;
155         while (next) {
156             tag = strsep (&next, " ");
157             if (*tag == '\0')
158                 continue;
159             status = notmuch_message_add_tag (message, tag);
160             if (status) {
161                 fprintf (stderr,
162                          "Error applying tag %s to message %s:\n",
163                          tag, message_id);
164                 fprintf (stderr, "%s\n",
165                          notmuch_status_to_string (status));
166             }
167         }
168
169         notmuch_message_thaw (message);
170
171         if (synchronize_flags)
172             notmuch_message_tags_to_maildir_flags (message);
173
174       NEXT_LINE:
175         if (message)
176             notmuch_message_destroy (message);
177         message = NULL;
178         free (message_id);
179         free (file_tags);
180     }
181
182     regfree (&regex);
183
184     if (line)
185         free (line);
186
187     notmuch_database_close (notmuch);
188     if (input != stdin)
189         fclose (input);
190
191     return 0;
192 }