]> git.cworth.org Git - notmuch/blob - notmuch-setup.c
CLI/config: remove calls to notmuch_config_open from top level
[notmuch] / notmuch-setup.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 https://www.gnu.org/licenses/ .
17  *
18  * Author: Carl Worth <cworth@cworth.org>
19  */
20
21 #include "notmuch-client.h"
22
23 static const char *
24 make_path_absolute (void *ctx, const char *path)
25 {
26     char *cwd;
27
28     if (*path == '/')
29         return path;
30
31     cwd = getcwd (NULL, 0);
32     if (cwd == NULL) {
33         fprintf (stderr, "Out of memory.\n");
34         return NULL;
35     }
36
37     path = talloc_asprintf (ctx, "%s/%s", cwd, path);
38     if (path == NULL)
39         fprintf (stderr, "Out of memory.\n");
40
41     free (cwd);
42
43     return path;
44 }
45
46 static void
47 welcome_message_pre_setup (void)
48 {
49     printf (
50         "Welcome to notmuch!\n\n"
51
52         "The goal of notmuch is to help you manage and search your collection of\n"
53         "email, and to efficiently keep up with the flow of email as it comes in.\n\n"
54
55         "Notmuch needs to know a few things about you such as your name and email\n"
56         "address, as well as the directory that contains your email. This is where\n"
57         "you already have mail stored and where messages will be delivered in the\n"
58         "future. This directory can contain any number of sub-directories. Regular\n"
59         "files in these directories should be individual email messages. If there\n"
60         "are other, non-email files (such as indexes maintained by other email\n"
61         "programs) then notmuch will do its best to detect those and ignore them.\n\n"
62
63         "If you already have your email being delivered to directories in either\n"
64         "maildir or mh format, then that's perfect. Mail storage that uses mbox\n"
65         "format, (where one mbox file contains many messages), will not work with\n"
66         "notmuch. If that's how your mail is currently stored, we recommend you\n"
67         "first convert it to maildir format with a utility such as mb2md. You can\n"
68         "continue configuring notmuch now, but be sure to complete the conversion\n"
69         "before you run \"notmuch new\" for the first time.\n\n");
70 }
71
72 static void
73 welcome_message_post_setup (void)
74 {
75     printf ("\n"
76             "Notmuch is now configured, and the configuration settings are saved in\n"
77             "a file in your home directory named .notmuch-config . If you'd like to\n"
78             "change the configuration in the future, you can either edit that file\n"
79             "directly or run \"notmuch setup\".  To choose an alternate configuration\n"
80             "location, set ${NOTMUCH_CONFIG}.\n\n"
81
82             "The next step is to run \"notmuch new\" which will create a database\n"
83             "that indexes all of your mail. Depending on the amount of mail you have\n"
84             "the initial indexing process can take a long time, so expect that.\n"
85             "Also, the resulting database will require roughly the same amount of\n"
86             "storage space as your current collection of email. So please ensure you\n"
87             "have sufficient storage space available now.\n\n");
88 }
89
90 static void
91 print_tag_list (notmuch_config_values_t *tags)
92 {
93     bool first = false;
94
95     for (;
96          notmuch_config_values_valid (tags);
97          notmuch_config_values_move_to_next (tags)) {
98         if (! first)
99             printf (" ");
100         first = false;
101         printf ("%s", notmuch_config_values_get (tags));
102     }
103 }
104
105 static GPtrArray *
106 parse_tag_list (void *ctx, char *response)
107 {
108     GPtrArray *tags = g_ptr_array_new ();
109     char *tag = response;
110     char *space;
111
112     while (tag && *tag) {
113         space = strchr (tag, ' ');
114         if (space)
115             g_ptr_array_add (tags, talloc_strndup (ctx, tag, space - tag));
116         else
117             g_ptr_array_add (tags, talloc_strdup (ctx, tag));
118         tag = space;
119         while (tag && *tag == ' ')
120             tag++;
121     }
122
123     return tags;
124 }
125
126 int
127 notmuch_setup_command (unused(notmuch_config_t *config),
128                        notmuch_database_t *notmuch,
129                        int argc, char *argv[])
130 {
131     char *response = NULL;
132     size_t response_size = 0;
133     GPtrArray *other_emails;
134     notmuch_config_values_t *new_tags, *search_exclude_tags, *emails;
135     notmuch_config_t *config;
136
137 #define prompt(format, ...)                                     \
138     do {                                                        \
139         printf (format, ##__VA_ARGS__);                         \
140         fflush (stdout);                                        \
141         if (getline (&response, &response_size, stdin) < 0) {   \
142             printf ("Exiting.\n");                              \
143             exit (EXIT_FAILURE);                                \
144         }                                                       \
145         chomp_newline (response);                               \
146     } while (0)
147
148     if (notmuch_minimal_options ("setup", argc, argv) < 0)
149         return EXIT_FAILURE;
150
151     if (notmuch_requested_db_uuid)
152         fprintf (stderr, "Warning: ignoring --uuid=%s\n",
153                  notmuch_requested_db_uuid);
154
155     config = notmuch_config_open (notmuch,
156                                   notmuch_config_path (notmuch), true);
157     if (! config)
158         return EXIT_FAILURE;
159
160     if (notmuch_config_is_new (config))
161         welcome_message_pre_setup ();
162
163     prompt ("Your full name [%s]: ", notmuch_config_get (notmuch, NOTMUCH_CONFIG_USER_NAME));
164     if (strlen (response))
165         notmuch_config_set_user_name (config, response);
166
167     prompt ("Your primary email address [%s]: ",
168             notmuch_config_get (notmuch, NOTMUCH_CONFIG_PRIMARY_EMAIL));
169     if (strlen (response))
170         notmuch_config_set_user_primary_email (config, response);
171
172     other_emails = g_ptr_array_new ();
173
174     for (emails = notmuch_config_get_values (notmuch, NOTMUCH_CONFIG_OTHER_EMAIL);
175          notmuch_config_values_valid (emails);
176          notmuch_config_values_move_to_next (emails)) {
177         const char *email = notmuch_config_values_get (emails);
178
179         prompt ("Additional email address [%s]: ", email);
180         if (strlen (response))
181             g_ptr_array_add (other_emails, talloc_strdup (config, response));
182         else
183             g_ptr_array_add (other_emails, talloc_strdup (config, email));
184     }
185
186     do {
187         prompt ("Additional email address [Press 'Enter' if none]: ");
188         if (strlen (response))
189             g_ptr_array_add (other_emails, talloc_strdup (config, response));
190     } while (strlen (response));
191     if (other_emails->len)
192         notmuch_config_set_user_other_email (config,
193                                              (const char **)
194                                              other_emails->pdata,
195                                              other_emails->len);
196     g_ptr_array_free (other_emails, true);
197
198     prompt ("Top-level directory of your email archive [%s]: ",
199             notmuch_config_get (notmuch, NOTMUCH_CONFIG_DATABASE_PATH));
200     if (strlen (response)) {
201         const char *absolute_path;
202
203         absolute_path = make_path_absolute (config, response);
204         notmuch_config_set_database_path (config, absolute_path);
205     }
206
207     new_tags = notmuch_config_get_values (notmuch, NOTMUCH_CONFIG_NEW_TAGS);
208
209     printf ("Tags to apply to all new messages (separated by spaces) [");
210     print_tag_list (new_tags);
211     prompt ("]: ");
212
213     if (strlen (response)) {
214         GPtrArray *tags = parse_tag_list (config, response);
215
216         notmuch_config_set_new_tags (config, (const char **) tags->pdata,
217                                      tags->len);
218
219         g_ptr_array_free (tags, true);
220     }
221
222     search_exclude_tags = notmuch_config_get_values (notmuch, NOTMUCH_CONFIG_EXCLUDE_TAGS);
223
224     printf ("Tags to exclude when searching messages (separated by spaces) [");
225     print_tag_list (search_exclude_tags);
226     prompt ("]: ");
227
228     if (strlen (response)) {
229         GPtrArray *tags = parse_tag_list (config, response);
230
231         notmuch_config_set_search_exclude_tags (config,
232                                                 (const char **) tags->pdata,
233                                                 tags->len);
234
235         g_ptr_array_free (tags, true);
236     }
237
238     if (notmuch_config_save (config))
239         return EXIT_FAILURE;
240
241     if (config)
242         notmuch_config_close (config);
243
244     if (notmuch_config_is_new (config))
245         welcome_message_post_setup ();
246
247     return EXIT_SUCCESS;
248 }