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