]> git.notmuchmail.org Git - notmuch/blob - notmuch-restore.c
notmuch: Break notmuch.c up into several smaller files.
[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     FILE *input = NULL;
27     notmuch_database_t *notmuch = NULL;
28     char *line = NULL;
29     size_t line_size;
30     ssize_t line_len;
31     regex_t regex;
32     int rerr;
33     int ret = 0;
34
35     if (argc) {
36         input = fopen (argv[0], "r");
37         if (input == NULL) {
38             fprintf (stderr, "Error opening %s for reading: %s\n",
39                      argv[0], strerror (errno));
40             ret = 1;
41             goto DONE;
42         }
43     } else {
44         printf ("No filename given. Reading dump from stdin.\n");
45         input = stdin;
46     }
47
48     notmuch = notmuch_database_open (NULL);
49     if (notmuch == NULL) {
50         ret = 1;
51         goto DONE;
52     }
53
54     /* Dump output is one line per message. We match a sequence of
55      * non-space characters for the message-id, then one or more
56      * spaces, then a list of space-separated tags as a sequence of
57      * characters within literal '(' and ')'. */
58     xregcomp (&regex,
59               "^([^ ]+) \\(([^)]*)\\)$",
60               REG_EXTENDED);
61
62     while ((line_len = getline (&line, &line_size, input)) != -1) {
63         regmatch_t match[3];
64         char *message_id, *tags, *tag, *next;
65         notmuch_message_t *message;
66         notmuch_status_t status;
67
68         chomp_newline (line);
69
70         rerr = xregexec (&regex, line, 3, match, 0);
71         if (rerr == REG_NOMATCH)
72         {
73             fprintf (stderr, "Warning: Ignoring invalid input line: %s\n",
74                      line);
75             continue;
76         }
77
78         message_id = xstrndup (line + match[1].rm_so,
79                                match[1].rm_eo - match[1].rm_so);
80         tags = xstrndup (line + match[2].rm_so,
81                          match[2].rm_eo - match[2].rm_so);
82
83         if (strlen (tags)) {
84
85             message = notmuch_database_find_message (notmuch, message_id);
86             if (message == NULL) {
87                 fprintf (stderr, "Warning: Cannot apply tags to missing message: %s\n",
88                          message_id);
89                 goto NEXT_LINE;
90             }
91
92             notmuch_message_freeze (message);
93
94             notmuch_message_remove_all_tags (message);
95
96             next = tags;
97             while (next) {
98                 tag = strsep (&next, " ");
99                 if (*tag == '\0')
100                     continue;
101                 status = notmuch_message_add_tag (message, tag);
102                 if (status) {
103                     fprintf (stderr,
104                              "Error applying tag %s to message %s:\n",
105                              tag, message_id);
106                     fprintf (stderr, "%s\n",
107                              notmuch_status_to_string (status));
108                 }
109             }
110
111             notmuch_message_thaw (message);
112             notmuch_message_destroy (message);
113         }
114       NEXT_LINE:
115         free (message_id);
116         free (tags);
117     }
118
119     regfree (&regex);
120
121   DONE:
122     if (line)
123         free (line);
124     if (notmuch)
125         notmuch_database_close (notmuch);
126     if (input && input != stdin)
127         fclose (input);
128
129     return ret;
130 }