]> git.notmuchmail.org Git - notmuch/blob - notmuch-config.c
notmuch: Abort if specified configuration file is not found.
[notmuch] / notmuch-config.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 #include <pwd.h>
24 #include <netdb.h>
25
26 static const char toplevel_config_comment[] =
27     " .notmuch-config - Configuration file for the notmuch mail system\n"
28     "\n"
29     " For more information about notmuch, see http://notmuchmail.org";
30
31 static const char database_config_comment[] =
32     " Database configuration\n"
33     "\n"
34     " The only value supported here is 'path' which should be the top-level\n"
35     " directory where your mail currently exists and to where mail will be\n"
36     " delivered in the future. Files should be individual email messages.\n"
37     " Notmuch will store its database within a sub-directory of the path\n"
38     " configured here named \".notmuch\".\n";
39
40 static const char user_config_comment[] =
41     " User configuration\n"
42     "\n"
43     " Here is where you can let notmuch know how you would like to be\n"
44     " addressed. Valid settings are\n"
45     "\n"
46     "\tname             Your full name.\n"
47     "\tprimary_email    Your primary email address.\n"
48     "\tother_email      A list (separated by ';') of other email addresses\n"
49     "\t         at which you receive email.\n"
50     "\n"
51     " Notmuch will use the various email addresses configured here when\n"
52     " formatting replies. It will avoid including your own addresses in the\n"
53     " recipient list of replies, and will set the From address based on the\n"
54     " address to which the original email was addressed.\n";
55
56 struct _notmuch_config {
57     char *filename;
58     GKeyFile *key_file;
59
60     char *database_path;
61     char *user_name;
62     char *user_primary_email;
63     char **user_other_email;
64     size_t user_other_email_length;
65 };
66
67 static int
68 notmuch_config_destructor (notmuch_config_t *config)
69 {
70     if (config->key_file)
71         g_key_file_free (config->key_file);
72
73     return 0;
74 }
75
76 static char *
77 get_name_from_passwd_file (void *ctx)
78 {
79     long pw_buf_size = sysconf(_SC_GETPW_R_SIZE_MAX);
80     char *pw_buf = talloc_zero_size (ctx, pw_buf_size);
81     struct passwd passwd, *ignored;
82     char *name;
83     int e;
84
85     if (pw_buf_size == -1) pw_buf_size = 64;
86
87     while ((e = getpwuid_r (getuid (), &passwd, pw_buf,
88                             pw_buf_size, &ignored)) == ERANGE) {
89         pw_buf_size = pw_buf_size * 2;
90         pw_buf = talloc_zero_size(ctx, pw_buf_size);
91     }
92
93     if (e == 0) {
94         char *comma = strchr (passwd.pw_gecos, ',');
95         if (comma)
96             name = talloc_strndup (ctx, passwd.pw_gecos,
97                                    comma - passwd.pw_gecos);
98         else
99             name = talloc_strdup (ctx, passwd.pw_gecos);
100     } else {
101         name = talloc_strdup (ctx, "");
102     }
103
104     talloc_free (pw_buf);
105
106     return name;
107 }
108
109 static char *
110 get_username_from_passwd_file (void *ctx)
111 {
112     long pw_buf_size = sysconf(_SC_GETPW_R_SIZE_MAX);
113     char *pw_buf = talloc_zero_size (ctx, pw_buf_size);
114     struct passwd passwd, *ignored;
115     char *name;
116     int e;
117
118     if (pw_buf_size == -1) pw_buf_size = 64;
119     while ((e = getpwuid_r (getuid (), &passwd, pw_buf,
120                             pw_buf_size, &ignored)) == ERANGE) {
121         pw_buf_size = pw_buf_size * 2;
122         pw_buf = talloc_zero_size(ctx, pw_buf_size);
123     }
124
125     if (e == 0)
126         name = talloc_strdup (ctx, passwd.pw_name);
127     else
128         name = talloc_strdup (ctx, "");
129
130     talloc_free (pw_buf);
131
132     return name;
133 }
134
135 /* Open the named notmuch configuration file. If the filename is NULL,
136  * the value of the environment variable $NOTMUCH_CONFIG will be used.
137  * If $NOTMUCH_CONFIG is unset, the default configuration file
138  * ($HOME/.notmuch-config) will be used.
139  *
140  * If any error occurs, (out of memory, or a permission-denied error,
141  * etc.), this function will print a message to stderr and return
142  * NULL.
143  *
144  * Note: It is *not* an error if the specified configuration file does
145  * not exist. In this case, a default configuration will be created
146  * and returned. Subsequently calling notmuch_config_save will cause
147  * the configuration to be written to the filename specified at the
148  * time of notmuch_config_open.
149  *
150  * The default configuration settings are determined as follows:
151  *
152  *      database_path:          $HOME/mail
153  *
154  *      user_name:              From /etc/passwd
155  *
156  *      user_primary_mail:      $EMAIL variable if set, otherwise
157  *                              constructed from the username and
158  *                              hostname of the current machine.
159  *
160  *      user_other_email:       Not set.
161  *
162  * The default configuration also contains comments to guide the user
163  * in editing the file directly.
164  */
165 notmuch_config_t *
166 notmuch_config_open (void *ctx,
167                      const char *filename,
168                      notmuch_bool_t *is_new_ret)
169 {
170     GError *error = NULL;
171     int is_new = 0;
172     char *notmuch_config_env = NULL;
173
174     if (is_new_ret)
175         *is_new_ret = 0;
176
177     notmuch_config_t *config = talloc (ctx, notmuch_config_t);
178     if (config == NULL) {
179         fprintf (stderr, "Out of memory.\n");
180         return NULL;
181     }
182     
183     talloc_set_destructor (config, notmuch_config_destructor);
184
185     if (filename) {
186         config->filename = talloc_strdup (config, filename);
187     } else if ((notmuch_config_env = getenv ("NOTMUCH_CONFIG"))) {
188         config->filename = talloc_strdup (config, notmuch_config_env);
189     } else {
190         config->filename = talloc_asprintf (config, "%s/.notmuch-config",
191                                             getenv ("HOME"));
192     }
193
194     config->key_file = g_key_file_new ();
195
196     config->database_path = NULL;
197     config->user_name = NULL;
198     config->user_primary_email = NULL;
199     config->user_other_email = NULL;
200     config->user_other_email_length = 0;
201
202     if (! g_key_file_load_from_file (config->key_file,
203                                      config->filename,
204                                      G_KEY_FILE_KEEP_COMMENTS,
205                                      &error))
206     {
207         /* We are capable of dealing with a non-existent configuration
208          * file, so be silent about that (unless the user had set a
209          * non-default configuration file with the NOTMUCH_CONFIG
210          * variable)
211          */
212         if (notmuch_config_env ||
213             !(error->domain == G_FILE_ERROR &&
214               error->code == G_FILE_ERROR_NOENT))
215         {
216             fprintf (stderr, "Error reading configuration file %s: %s\n",
217                      config->filename, error->message);
218             talloc_free (config);
219             g_error_free (error);
220             return NULL;
221         }
222
223         g_error_free (error);
224         is_new = 1;
225     }
226
227     if (notmuch_config_get_database_path (config) == NULL) {
228         char *path = talloc_asprintf (config, "%s/mail",
229                                       getenv ("HOME"));
230         notmuch_config_set_database_path (config, path);
231         talloc_free (path);
232     }
233
234     if (notmuch_config_get_user_name (config) == NULL) {
235         char *name = get_name_from_passwd_file (config);
236         notmuch_config_set_user_name (config, name);
237         talloc_free (name);
238     }
239
240     if (notmuch_config_get_user_primary_email (config) == NULL) {
241         char *email = getenv ("EMAIL");
242         if (email) {
243             notmuch_config_set_user_primary_email (config, email);
244         } else {
245             char hostname[256];
246             struct hostent *hostent;
247             const char *domainname;
248
249             char *username = get_username_from_passwd_file (config);
250
251             gethostname (hostname, 256);
252             hostname[255] = '\0';
253
254             hostent = gethostbyname (hostname);
255             if (hostent && (domainname = strchr (hostent->h_name, '.')))
256                 domainname += 1;
257             else
258                 domainname = "(none)";
259
260             email = talloc_asprintf (config, "%s@%s.%s",
261                                      username, hostname, domainname);
262
263             notmuch_config_set_user_primary_email (config, email);
264
265             talloc_free (username);
266             talloc_free (email);
267         }
268     }
269
270     /* When we create a new configuration file here, we  add some
271      * comments to help the user understand what can be done. */
272     if (is_new) {
273         g_key_file_set_comment (config->key_file, NULL, NULL,
274                                 toplevel_config_comment, NULL);
275         g_key_file_set_comment (config->key_file, "database", NULL,
276                                 database_config_comment, NULL);
277         g_key_file_set_comment (config->key_file, "user", NULL,
278                                 user_config_comment, NULL);
279     }
280
281     if (is_new_ret)
282         *is_new_ret = is_new;
283
284     return config;
285 }
286
287 /* Close the given notmuch_config_t object, freeing all resources.
288  * 
289  * Note: Any changes made to the configuration are *not* saved by this
290  * function. To save changes, call notmuch_config_save before
291  * notmuch_config_close.
292 */
293 void
294 notmuch_config_close (notmuch_config_t *config)
295 {
296     talloc_free (config);
297 }
298
299 /* Save any changes made to the notmuch configuration.
300  *
301  * Any comments originally in the file will be preserved.
302  *
303  * Returns 0 if successful, and 1 in case of any error, (after
304  * printing a description of the error to stderr).
305  */
306 int
307 notmuch_config_save (notmuch_config_t *config)
308 {
309     size_t length;
310     char *data;
311     GError *error = NULL;
312
313     data = g_key_file_to_data (config->key_file, &length, NULL);
314     if (data == NULL) {
315         fprintf (stderr, "Out of memory.\n");
316         return 1;
317     }
318
319     if (! g_file_set_contents (config->filename, data, length, &error)) {
320         fprintf (stderr, "Error saving configuration to %s: %s\n",
321                  config->filename, error->message);
322         g_error_free (error);
323         g_free (data);
324         return 1;
325     }
326
327     g_free (data);
328     return 0;
329 }
330
331 const char *
332 notmuch_config_get_database_path (notmuch_config_t *config)
333 {
334     char *path;
335
336     if (config->database_path == NULL) {
337         path = g_key_file_get_string (config->key_file,
338                                       "database", "path", NULL);
339         if (path) {
340             config->database_path = talloc_strdup (config, path);
341             free (path);
342         }
343     }
344
345     return config->database_path;
346 }
347
348 void
349 notmuch_config_set_database_path (notmuch_config_t *config,
350                                   const char *database_path)
351 {
352     g_key_file_set_string (config->key_file,
353                            "database", "path", database_path);
354
355     talloc_free (config->database_path);
356     config->database_path = NULL;
357 }
358
359 const char *
360 notmuch_config_get_user_name (notmuch_config_t *config)
361 {
362     char *name;
363
364     if (config->user_name == NULL) {
365         name = g_key_file_get_string (config->key_file,
366                                       "user", "name", NULL);
367         if (name) {
368             config->user_name = talloc_strdup (config, name);
369             free (name);
370         }
371     }
372
373     return config->user_name;
374 }
375
376 void
377 notmuch_config_set_user_name (notmuch_config_t *config,
378                               const char *user_name)
379 {
380     g_key_file_set_string (config->key_file,
381                            "user", "name", user_name);
382
383     talloc_free (config->user_name);
384     config->user_name = NULL;
385 }
386
387 const char *
388 notmuch_config_get_user_primary_email (notmuch_config_t *config)
389 {
390     char *email;
391
392     if (config->user_primary_email == NULL) {
393         email = g_key_file_get_string (config->key_file,
394                                        "user", "primary_email", NULL);
395         if (email) {
396             config->user_primary_email = talloc_strdup (config, email);
397             free (email);
398         }
399     }
400
401     return config->user_primary_email;
402 }
403
404 void
405 notmuch_config_set_user_primary_email (notmuch_config_t *config,
406                                        const char *primary_email)
407 {
408     g_key_file_set_string (config->key_file,
409                            "user", "primary_email", primary_email);
410
411     talloc_free (config->user_primary_email);
412     config->user_primary_email = NULL;
413 }
414
415 char **
416 notmuch_config_get_user_other_email (notmuch_config_t *config,
417                                      size_t *length)
418 {
419     char **emails;
420     size_t emails_length;
421     unsigned int i;
422
423     if (config->user_other_email == NULL) {
424         emails = g_key_file_get_string_list (config->key_file,
425                                              "user", "other_email",
426                                              &emails_length, NULL);
427         if (emails) {
428             config->user_other_email = talloc_size (config,
429                                                     sizeof (char *) *
430                                                     (emails_length + 1));
431             for (i = 0; i < emails_length; i++)
432                 config->user_other_email[i] = talloc_strdup (config->user_other_email,
433                                                              emails[i]);
434             config->user_other_email[i] = NULL;
435
436             g_strfreev (emails);
437
438             config->user_other_email_length = emails_length;
439         }
440     }
441
442     *length = config->user_other_email_length;
443     return config->user_other_email;
444 }
445
446 void
447 notmuch_config_set_user_other_email (notmuch_config_t *config,
448                                      const char *other_email[],
449                                      size_t length)
450 {
451     g_key_file_set_string_list (config->key_file,
452                                 "user", "other_email",
453                                 other_email, length);
454
455     talloc_free (config->user_other_email);
456     config->user_other_email = NULL;
457 }