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