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