1 /* notmuch - Not much of an email program, (just index and search)
3 * Copyright © 2009 Carl Worth
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.
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.
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/ .
18 * Author: Carl Worth <cworth@cworth.org>
21 #include "notmuch-client.h"
24 make_path_absolute (void *ctx, const char *path)
31 cwd = getcwd (NULL, 0);
33 fprintf (stderr, "Out of memory.\n");
37 path = talloc_asprintf (ctx, "%s/%s", cwd, path);
39 fprintf (stderr, "Out of memory.\n");
47 welcome_message_pre_setup (void)
50 "Welcome to notmuch!\n\n"
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"
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"
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");
73 welcome_message_post_setup (void)
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"
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");
91 print_tag_list (notmuch_config_values_t *tags)
96 notmuch_config_values_valid (tags);
97 notmuch_config_values_move_to_next (tags)) {
101 printf ("%s", notmuch_config_values_get (tags));
106 parse_tag_list (void *ctx, char *response)
108 GPtrArray *tags = g_ptr_array_new ();
109 char *tag = response;
112 while (tag && *tag) {
113 space = strchr (tag, ' ');
115 g_ptr_array_add (tags, talloc_strndup (ctx, tag, space - tag));
117 g_ptr_array_add (tags, talloc_strdup (ctx, tag));
119 while (tag && *tag == ' ')
127 notmuch_setup_command (notmuch_database_t *notmuch,
128 int argc, char *argv[])
130 char *response = NULL;
131 size_t response_size = 0;
132 GPtrArray *other_emails;
133 notmuch_config_values_t *new_tags, *search_exclude_tags, *emails;
134 notmuch_conffile_t *config;
136 #define prompt(format, ...) \
138 printf (format, ##__VA_ARGS__); \
140 if (getline (&response, &response_size, stdin) < 0) { \
141 printf ("Exiting.\n"); \
142 exit (EXIT_FAILURE); \
144 chomp_newline (response); \
147 if (notmuch_minimal_options ("setup", argc, argv) < 0)
150 if (notmuch_requested_db_uuid)
151 fprintf (stderr, "Warning: ignoring --uuid=%s\n",
152 notmuch_requested_db_uuid);
154 config = notmuch_conffile_open (notmuch,
155 notmuch_config_path (notmuch), true);
159 if (notmuch_conffile_is_new (config))
160 welcome_message_pre_setup ();
162 prompt ("Your full name [%s]: ", notmuch_config_get (notmuch, NOTMUCH_CONFIG_USER_NAME));
163 if (strlen (response))
164 notmuch_conffile_set_user_name (config, response);
166 prompt ("Your primary email address [%s]: ",
167 notmuch_config_get (notmuch, NOTMUCH_CONFIG_PRIMARY_EMAIL));
168 if (strlen (response))
169 notmuch_conffile_set_user_primary_email (config, response);
171 other_emails = g_ptr_array_new ();
173 for (emails = notmuch_config_get_values (notmuch, NOTMUCH_CONFIG_OTHER_EMAIL);
174 notmuch_config_values_valid (emails);
175 notmuch_config_values_move_to_next (emails)) {
176 const char *email = notmuch_config_values_get (emails);
178 prompt ("Additional email address [%s]: ", email);
179 if (strlen (response))
180 g_ptr_array_add (other_emails, talloc_strdup (config, response));
182 g_ptr_array_add (other_emails, talloc_strdup (config, email));
186 prompt ("Additional email address [Press 'Enter' if none]: ");
187 if (strlen (response))
188 g_ptr_array_add (other_emails, talloc_strdup (config, response));
189 } while (strlen (response));
190 if (other_emails->len)
191 notmuch_conffile_set_user_other_email (config,
195 g_ptr_array_free (other_emails, true);
197 prompt ("Top-level directory of your email archive [%s]: ",
198 notmuch_config_get (notmuch, NOTMUCH_CONFIG_DATABASE_PATH));
199 if (strlen (response)) {
200 const char *absolute_path;
202 absolute_path = make_path_absolute (config, response);
203 notmuch_conffile_set_database_path (config, absolute_path);
206 new_tags = notmuch_config_get_values (notmuch, NOTMUCH_CONFIG_NEW_TAGS);
208 printf ("Tags to apply to all new messages (separated by spaces) [");
209 print_tag_list (new_tags);
212 if (strlen (response)) {
213 GPtrArray *tags = parse_tag_list (config, response);
215 notmuch_conffile_set_new_tags (config, (const char **) tags->pdata,
218 g_ptr_array_free (tags, true);
221 search_exclude_tags = notmuch_config_get_values (notmuch, NOTMUCH_CONFIG_EXCLUDE_TAGS);
223 printf ("Tags to exclude when searching messages (separated by spaces) [");
224 print_tag_list (search_exclude_tags);
227 if (strlen (response)) {
228 GPtrArray *tags = parse_tag_list (config, response);
230 notmuch_conffile_set_search_exclude_tags (config,
231 (const char **) tags->pdata,
234 g_ptr_array_free (tags, true);
237 if (notmuch_conffile_save (config))
241 notmuch_conffile_close (config);
243 if (notmuch_conffile_is_new (config))
244 welcome_message_post_setup ();