]> git.notmuchmail.org Git - notmuch/blob - notmuch-restore.c
b5c5c48dfdc4f3be5f023d946900e7c22c42a3cc
[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     FILE *input;
29     char *line = NULL;
30     size_t line_size;
31     ssize_t line_len;
32     regex_t regex;
33     int rerr;
34
35     config = notmuch_config_open (ctx, NULL, NULL);
36     if (config == NULL)
37         return 1;
38
39     notmuch = notmuch_database_open (notmuch_config_get_database_path (config),
40                                      NOTMUCH_DATABASE_MODE_READ_WRITE);
41     if (notmuch == NULL)
42         return 1;
43
44     notmuch_database_set_maildir_sync (notmuch,
45                                        notmuch_config_get_maildir_sync (config));
46     if (argc) {
47         input = fopen (argv[0], "r");
48         if (input == NULL) {
49             fprintf (stderr, "Error opening %s for reading: %s\n",
50                      argv[0], strerror (errno));
51             return 1;
52         }
53     } else {
54         printf ("No filename given. Reading dump from stdin.\n");
55         input = stdin;
56     }
57
58     /* 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     xregcomp (&regex,
63               "^([^ ]+) \\(([^)]*)\\)$",
64               REG_EXTENDED);
65
66     while ((line_len = getline (&line, &line_size, input)) != -1) {
67         regmatch_t match[3];
68         char *message_id, *file_tags, *tag, *next;
69         notmuch_message_t *message = NULL;
70         notmuch_status_t status;
71         notmuch_tags_t *db_tags;
72         char *db_tags_str;
73
74         chomp_newline (line);
75
76         rerr = xregexec (&regex, line, 3, match, 0);
77         if (rerr == REG_NOMATCH)
78         {
79             fprintf (stderr, "Warning: Ignoring invalid input line: %s\n",
80                      line);
81             continue;
82         }
83
84         message_id = xstrndup (line + match[1].rm_so,
85                                match[1].rm_eo - match[1].rm_so);
86         file_tags = xstrndup (line + match[2].rm_so,
87                               match[2].rm_eo - match[2].rm_so);
88
89         message = notmuch_database_find_message (notmuch, message_id);
90         if (message == NULL) {
91             fprintf (stderr, "Warning: Cannot apply tags to missing message: %s\n",
92                      message_id);
93             goto NEXT_LINE;
94         }
95
96         db_tags_str = NULL;
97         for (db_tags = notmuch_message_get_tags (message);
98              notmuch_tags_valid (db_tags);
99              notmuch_tags_move_to_next (db_tags))
100         {
101             const char *tag = notmuch_tags_get (db_tags);
102
103             if (db_tags_str)
104                 db_tags_str = talloc_asprintf_append (db_tags_str, " %s", tag);
105             else
106                 db_tags_str = talloc_strdup (message, tag);
107         }
108
109         if (((file_tags == NULL || *file_tags == '\0') &&
110              (db_tags_str == NULL || *db_tags_str == '\0')) ||
111             (file_tags && db_tags_str && strcmp (file_tags, db_tags_str) == 0))
112         {
113             goto NEXT_LINE;
114         }
115
116         notmuch_message_freeze (message);
117         notmuch_message_remove_all_tags (message);
118
119         next = file_tags;
120         while (next) {
121             tag = strsep (&next, " ");
122             if (*tag == '\0')
123                 continue;
124             status = notmuch_message_add_tag (message, tag);
125             if (status) {
126                 fprintf (stderr,
127                          "Error applying tag %s to message %s:\n",
128                          tag, message_id);
129                 fprintf (stderr, "%s\n",
130                          notmuch_status_to_string (status));
131             }
132         }
133
134         notmuch_message_thaw (message);
135
136       NEXT_LINE:
137         if (message)
138             notmuch_message_destroy (message);
139         message = NULL;
140         free (message_id);
141         free (file_tags);
142     }
143
144     regfree (&regex);
145
146     if (line)
147         free (line);
148
149     notmuch_database_close (notmuch);
150     if (input != stdin)
151         fclose (input);
152
153     return 0;
154 }