]> git.notmuchmail.org Git - notmuch/blob - notmuch-setup.c
debian: Add package for debugging symbols (Closes: #717339)
[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 static void
91 print_tag_list (const char **tags, size_t tags_len)
92 {
93     unsigned int i;
94     for (i = 0; i < tags_len; i++) {
95         if (i != 0)
96             printf (" ");
97         printf ("%s", tags[i]);
98     }
99 }
100
101 static GPtrArray *
102 parse_tag_list (void *ctx, char *response)
103 {
104     GPtrArray *tags = g_ptr_array_new ();
105     char *tag = response;
106     char *space;
107
108     while (tag && *tag) {
109         space = strchr (tag, ' ');
110         if (space)
111             g_ptr_array_add (tags, talloc_strndup (ctx, tag, space - tag));
112         else
113             g_ptr_array_add (tags, talloc_strdup (ctx, tag));
114         tag = space;
115         while (tag && *tag == ' ')
116             tag++;
117     }
118
119     return tags;
120 }
121
122 int
123 notmuch_setup_command (notmuch_config_t *config,
124                        unused (int argc), unused (char *argv[]))
125 {
126     char *response = NULL;
127     size_t response_size = 0;
128     const char **old_other_emails;
129     size_t old_other_emails_len;
130     GPtrArray *other_emails;
131     unsigned int i;
132     const char **new_tags;
133     size_t new_tags_len;
134     const char **search_exclude_tags;
135     size_t search_exclude_tags_len;
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 (1);                                           \
144         }                                                       \
145         chomp_newline (response);                               \
146     } while (0)
147
148     if (notmuch_config_is_new (config))
149         welcome_message_pre_setup ();
150
151     prompt ("Your full name [%s]: ", notmuch_config_get_user_name (config));
152     if (strlen (response))
153         notmuch_config_set_user_name (config, response);
154
155     prompt ("Your primary email address [%s]: ",
156             notmuch_config_get_user_primary_email (config));
157     if (strlen (response))
158         notmuch_config_set_user_primary_email (config, response);
159
160     other_emails = g_ptr_array_new ();
161
162     old_other_emails = notmuch_config_get_user_other_email (config,
163                                              &old_other_emails_len);
164     for (i = 0; i < old_other_emails_len; i++) {
165         prompt ("Additional email address [%s]: ", old_other_emails[i]);
166         if (strlen (response))
167             g_ptr_array_add (other_emails, talloc_strdup (config, response));
168         else
169             g_ptr_array_add (other_emails, talloc_strdup (config,
170                                                          old_other_emails[i]));
171     }
172
173     do {
174         prompt ("Additional email address [Press 'Enter' if none]: ");
175         if (strlen (response))
176             g_ptr_array_add (other_emails, talloc_strdup (config, response));
177     } while (strlen (response));
178     if (other_emails->len)
179         notmuch_config_set_user_other_email (config,
180                                              (const char **)
181                                              other_emails->pdata,
182                                              other_emails->len);
183     g_ptr_array_free (other_emails, TRUE);
184
185     prompt ("Top-level directory of your email archive [%s]: ",
186             notmuch_config_get_database_path (config));
187     if (strlen (response)) {
188         const char *absolute_path;
189
190         absolute_path = make_path_absolute (config, response);
191         notmuch_config_set_database_path (config, absolute_path);
192     }
193
194     new_tags = notmuch_config_get_new_tags (config, &new_tags_len);
195
196     printf ("Tags to apply to all new messages (separated by spaces) [");
197     print_tag_list (new_tags, new_tags_len);
198     prompt ("]: ");
199
200     if (strlen (response)) {
201         GPtrArray *tags = parse_tag_list (config, response);
202
203         notmuch_config_set_new_tags (config, (const char **) tags->pdata,
204                                      tags->len);
205
206         g_ptr_array_free (tags, TRUE);
207     }
208
209
210     search_exclude_tags = notmuch_config_get_search_exclude_tags (config, &search_exclude_tags_len);
211
212     printf ("Tags to exclude when searching messages (separated by spaces) [");
213     print_tag_list (search_exclude_tags, search_exclude_tags_len);
214     prompt ("]: ");
215
216     if (strlen (response)) {
217         GPtrArray *tags = parse_tag_list (config, response);
218
219         notmuch_config_set_search_exclude_tags (config,
220                                                 (const char **) tags->pdata,
221                                                 tags->len);
222
223         g_ptr_array_free (tags, TRUE);
224     }
225
226
227     if (! notmuch_config_save (config)) {
228         if (notmuch_config_is_new (config))
229           welcome_message_post_setup ();
230         return 0;
231     } else {
232         return 1;
233     }
234 }