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