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