]> git.notmuchmail.org Git - notmuch/blob - notmuch-config.c
aaa0372cd9bd3b4434dfe26e13d29154db260342
[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. A filename of NULL will
136  * be interpreted as the default configuration file
137  * ($HOME/.notmuch-config).
138  *
139  * If any error occurs, (out of memory, or a permission-denied error,
140  * etc.), this function will print a message to stderr and return
141  * NULL.
142  *
143  * Note: It is *not* an error if the specified configuration file does
144  * not exist. In this case, a default configuration will be created
145  * and returned. Subsequently calling notmuch_config_save will cause
146  * the configuration to be written to the filename specified at the
147  * time of notmuch_config_open.
148  *
149  * The default configuration settings are determined as follows:
150  *
151  *      database_path:          $HOME/mail
152  *
153  *      user_name:              From /etc/passwd
154  *
155  *      user_primary_mail:      $EMAIL variable if set, otherwise
156  *                              constructed from the username and
157  *                              hostname of the current machine.
158  *
159  *      user_other_email:       Not set.
160  *
161  * The default configuration also contains comments to guide the user
162  * in editing the file directly.
163  */
164 notmuch_config_t *
165 notmuch_config_open (void *ctx,
166                      const char *filename,
167                      notmuch_bool_t *is_new_ret)
168 {
169     GError *error = NULL;
170     int is_new = 0;
171
172     if (is_new_ret)
173         *is_new_ret = 0;
174
175     notmuch_config_t *config = talloc (ctx, notmuch_config_t);
176     if (config == NULL) {
177         fprintf (stderr, "Out of memory.\n");
178         return NULL;
179     }
180     
181     talloc_set_destructor (config, notmuch_config_destructor);
182
183     if (filename)
184         config->filename = talloc_strdup (config, filename);
185     else
186         config->filename = talloc_asprintf (config, "%s/.notmuch-config",
187                                             getenv ("HOME"));
188
189     config->key_file = g_key_file_new ();
190
191     config->database_path = NULL;
192     config->user_name = NULL;
193     config->user_primary_email = NULL;
194     config->user_other_email = NULL;
195     config->user_other_email_length = 0;
196
197     if (! g_key_file_load_from_file (config->key_file,
198                                      config->filename,
199                                      G_KEY_FILE_KEEP_COMMENTS,
200                                      &error))
201     {
202         /* We are capable of dealing with a non-existent configuration
203          * file, so be silent about that. */
204         if (!(error->domain == G_FILE_ERROR &&
205               error->code == G_FILE_ERROR_NOENT))
206         {
207             fprintf (stderr, "Error reading configuration file %s: %s\n",
208                      config->filename, error->message);
209             talloc_free (config);
210             g_error_free (error);
211             return NULL;
212         }
213
214         g_error_free (error);
215         is_new = 1;
216     }
217
218     if (notmuch_config_get_database_path (config) == NULL) {
219         char *path = talloc_asprintf (config, "%s/mail",
220                                       getenv ("HOME"));
221         notmuch_config_set_database_path (config, path);
222         talloc_free (path);
223     }
224
225     if (notmuch_config_get_user_name (config) == NULL) {
226         char *name = get_name_from_passwd_file (config);
227         notmuch_config_set_user_name (config, name);
228         talloc_free (name);
229     }
230
231     if (notmuch_config_get_user_primary_email (config) == NULL) {
232         char *email = getenv ("EMAIL");
233         if (email) {
234             notmuch_config_set_user_primary_email (config, email);
235         } else {
236             char hostname[256];
237             struct hostent *hostent;
238             const char *domainname;
239
240             char *username = get_username_from_passwd_file (config);
241
242             gethostname (hostname, 256);
243             hostname[255] = '\0';
244
245             hostent = gethostbyname (hostname);
246             if (hostent && (domainname = strchr (hostent->h_name, '.')))
247                 domainname += 1;
248             else
249                 domainname = "(none)";
250
251             email = talloc_asprintf (config, "%s@%s.%s",
252                                      username, hostname, domainname);
253
254             notmuch_config_set_user_primary_email (config, email);
255
256             talloc_free (username);
257             talloc_free (email);
258         }
259     }
260
261     /* When we create a new configuration file here, we  add some
262      * comments to help the user understand what can be done. */
263     if (is_new) {
264         g_key_file_set_comment (config->key_file, NULL, NULL,
265                                 toplevel_config_comment, NULL);
266         g_key_file_set_comment (config->key_file, "database", NULL,
267                                 database_config_comment, NULL);
268         g_key_file_set_comment (config->key_file, "user", NULL,
269                                 user_config_comment, NULL);
270     }
271
272     if (is_new_ret)
273         *is_new_ret = is_new;
274
275     return config;
276 }
277
278 /* Close the given notmuch_config_t object, freeing all resources.
279  * 
280  * Note: Any changes made to the configuration are *not* saved by this
281  * function. To save changes, call notmuch_config_save before
282  * notmuch_config_close.
283 */
284 void
285 notmuch_config_close (notmuch_config_t *config)
286 {
287     talloc_free (config);
288 }
289
290 /* Save any changes made to the notmuch configuration.
291  *
292  * Any comments originally in the file will be preserved.
293  *
294  * Returns 0 if successful, and 1 in case of any error, (after
295  * printing a description of the error to stderr).
296  */
297 int
298 notmuch_config_save (notmuch_config_t *config)
299 {
300     size_t length;
301     char *data;
302     GError *error = NULL;
303
304     data = g_key_file_to_data (config->key_file, &length, NULL);
305     if (data == NULL) {
306         fprintf (stderr, "Out of memory.\n");
307         return 1;
308     }
309
310     if (! g_file_set_contents (config->filename, data, length, &error)) {
311         fprintf (stderr, "Error saving configuration to %s: %s\n",
312                  config->filename, error->message);
313         g_error_free (error);
314         return 1;
315     }
316
317     return 0;
318 }
319
320 const char *
321 notmuch_config_get_database_path (notmuch_config_t *config)
322 {
323     char *path;
324
325     if (config->database_path == NULL) {
326         path = g_key_file_get_string (config->key_file,
327                                       "database", "path", NULL);
328         if (path) {
329             config->database_path = talloc_strdup (config, path);
330             free (path);
331         }
332     }
333
334     return config->database_path;
335 }
336
337 void
338 notmuch_config_set_database_path (notmuch_config_t *config,
339                                   const char *database_path)
340 {
341     g_key_file_set_string (config->key_file,
342                            "database", "path", database_path);
343
344     talloc_free (config->database_path);
345     config->database_path = NULL;
346 }
347
348 const char *
349 notmuch_config_get_user_name (notmuch_config_t *config)
350 {
351     char *name;
352
353     if (config->user_name == NULL) {
354         name = g_key_file_get_string (config->key_file,
355                                       "user", "name", NULL);
356         if (name) {
357             config->user_name = talloc_strdup (config, name);
358             free (name);
359         }
360     }
361
362     return config->user_name;
363 }
364
365 void
366 notmuch_config_set_user_name (notmuch_config_t *config,
367                               const char *user_name)
368 {
369     g_key_file_set_string (config->key_file,
370                            "user", "name", user_name);
371
372     talloc_free (config->user_name);
373     config->user_name = NULL;
374 }
375
376 const char *
377 notmuch_config_get_user_primary_email (notmuch_config_t *config)
378 {
379     char *email;
380
381     if (config->user_primary_email == NULL) {
382         email = g_key_file_get_string (config->key_file,
383                                        "user", "primary_email", NULL);
384         if (email) {
385             config->user_primary_email = talloc_strdup (config, email);
386             free (email);
387         }
388     }
389
390     return config->user_primary_email;
391 }
392
393 void
394 notmuch_config_set_user_primary_email (notmuch_config_t *config,
395                                        const char *primary_email)
396 {
397     g_key_file_set_string (config->key_file,
398                            "user", "primary_email", primary_email);
399
400     talloc_free (config->user_primary_email);
401     config->user_primary_email = NULL;
402 }
403
404 char **
405 notmuch_config_get_user_other_email (notmuch_config_t *config,
406                                      size_t *length)
407 {
408     char **emails;
409     size_t emails_length;
410     unsigned int i;
411
412     if (config->user_other_email == NULL) {
413         emails = g_key_file_get_string_list (config->key_file,
414                                              "user", "other_email",
415                                              &emails_length, NULL);
416         if (emails) {
417             config->user_other_email = talloc_size (config,
418                                                     sizeof (char *) *
419                                                     (emails_length + 1));
420             for (i = 0; i < emails_length; i++)
421                 config->user_other_email[i] = talloc_strdup (config->user_other_email,
422                                                              emails[i]);
423             config->user_other_email[i] = NULL;
424
425             g_strfreev (emails);
426
427             config->user_other_email_length = emails_length;
428         }
429     }
430
431     *length = config->user_other_email_length;
432     return config->user_other_email;
433 }
434
435 void
436 notmuch_config_set_user_other_email (notmuch_config_t *config,
437                                      const char *other_email[],
438                                      size_t length)
439 {
440     g_key_file_set_string_list (config->key_file,
441                                 "user", "other_email",
442                                 other_email, length);
443
444     talloc_free (config->user_other_email);
445     config->user_other_email = NULL;
446 }