]> git.notmuchmail.org Git - notmuch/blob - notmuch-setup.c
smtp-dummy: Prefer return rather than exit() in main.
[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 http://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 int
91 notmuch_setup_command (unused (void *ctx),
92                        unused (int argc), unused (char *argv[]))
93 {
94     char *response = NULL;
95     size_t response_size = 0;
96     notmuch_config_t *config;
97     const char **old_other_emails;
98     size_t old_other_emails_len;
99     GPtrArray *other_emails;
100     unsigned int i;
101     int is_new;
102     const char **new_tags;
103     size_t new_tags_len;
104
105 #define prompt(format, ...)                                     \
106     do {                                                        \
107         printf (format, ##__VA_ARGS__);                         \
108         fflush (stdout);                                        \
109         if (getline (&response, &response_size, stdin) < 0) {   \
110             printf ("Exiting.\n");                              \
111             exit (1);                                           \
112         }                                                       \
113         chomp_newline (response);                               \
114     } while (0)
115
116     config = notmuch_config_open (ctx, NULL, &is_new);
117
118     if (is_new)
119         welcome_message_pre_setup ();
120
121     prompt ("Your full name [%s]: ", notmuch_config_get_user_name (config));
122     if (strlen (response))
123         notmuch_config_set_user_name (config, response);
124
125     prompt ("Your primary email address [%s]: ",
126             notmuch_config_get_user_primary_email (config));
127     if (strlen (response))
128         notmuch_config_set_user_primary_email (config, response);
129
130     other_emails = g_ptr_array_new ();
131
132     old_other_emails = notmuch_config_get_user_other_email (config,
133                                              &old_other_emails_len);
134     for (i = 0; i < old_other_emails_len; i++) {
135         prompt ("Additional email address [%s]: ", old_other_emails[i]);
136         if (strlen (response))
137             g_ptr_array_add (other_emails, talloc_strdup (ctx, response));
138         else
139             g_ptr_array_add (other_emails, talloc_strdup (ctx,
140                                                          old_other_emails[i]));
141     }
142
143     do {
144         prompt ("Additional email address [Press 'Enter' if none]: ");
145         if (strlen (response))
146             g_ptr_array_add (other_emails, talloc_strdup (ctx, response));
147     } while (strlen (response));
148     if (other_emails->len)
149         notmuch_config_set_user_other_email (config,
150                                              (const char **)
151                                              other_emails->pdata,
152                                              other_emails->len);
153     g_ptr_array_free (other_emails, TRUE);
154
155     prompt ("Top-level directory of your email archive [%s]: ",
156             notmuch_config_get_database_path (config));
157     if (strlen (response)) {
158         const char *absolute_path;
159
160         absolute_path = make_path_absolute (ctx, response);
161         notmuch_config_set_database_path (config, absolute_path);
162     }
163
164     new_tags = notmuch_config_get_new_tags (config, &new_tags_len);
165
166     printf ("Tags to apply to all new messages (separated by spaces) [");
167
168     for (i = 0; i < new_tags_len; i++) {
169         if (i != 0)
170             printf (" ");
171         printf ("%s", new_tags[i]);
172     }
173
174     prompt ("]: ");
175
176     if (strlen (response)) {
177         GPtrArray *tags = g_ptr_array_new ();
178         char *tag = response;
179         char *space;
180
181         while (tag && *tag) {
182             space = strchr (tag, ' ');
183             if (space)
184                 g_ptr_array_add (tags, talloc_strndup (ctx, tag, space - tag));
185             else
186                 g_ptr_array_add (tags, talloc_strdup (ctx, tag));
187             tag = space;
188             while (tag && *tag == ' ')
189                 tag++;
190         }
191
192         notmuch_config_set_new_tags (config, (const char **) tags->pdata,
193                                      tags->len);
194
195         g_ptr_array_free (tags, TRUE);
196     }
197
198     if (! notmuch_config_save (config)) {
199         if (is_new)
200           welcome_message_post_setup ();
201         return 0;
202     } else {
203         return 1;
204     }
205 }