]> git.notmuchmail.org Git - notmuch/blob - notmuch-config.c
vim: add archive support from 'show'
[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     const 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  * FILE NOT FOUND: When the specified configuration file (whether from
155  * 'filename' or the $NOTMUCH_CONFIG environment variable) does not
156  * exist, the behavior of this function depends on the 'is_new_ret'
157  * variable.
158  *
159  *      If is_new_ret is NULL, then a "file not found" message will be
160  *      printed to stderr and NULL will be returned.
161
162  *      If is_new_ret is non-NULL then a default configuration will be
163  *      returned and *is_new_ret will be set to 1 on return so that
164  *      the caller can recognize this case.
165  *
166  *      These default configuration settings are determined as
167  *      follows:
168  *
169  *              database_path:          $HOME/mail
170  *
171  *              user_name:              From /etc/passwd
172  *
173  *              user_primary_mail:      $EMAIL variable if set, otherwise
174  *                                      constructed from the username and
175  *                                      hostname of the current machine.
176  *
177  *              user_other_email:       Not set.
178  *
179  *      The default configuration also contains comments to guide the
180  *      user in editing the file directly.
181  */
182 notmuch_config_t *
183 notmuch_config_open (void *ctx,
184                      const char *filename,
185                      notmuch_bool_t *is_new_ret)
186 {
187     GError *error = NULL;
188     int is_new = 0;
189     size_t tmp;
190     char *notmuch_config_env = NULL;
191     int file_had_database_group;
192     int file_had_new_group;
193     int file_had_user_group;
194
195     if (is_new_ret)
196         *is_new_ret = 0;
197
198     notmuch_config_t *config = talloc (ctx, notmuch_config_t);
199     if (config == NULL) {
200         fprintf (stderr, "Out of memory.\n");
201         return NULL;
202     }
203     
204     talloc_set_destructor (config, notmuch_config_destructor);
205
206     if (filename) {
207         config->filename = talloc_strdup (config, filename);
208     } else if ((notmuch_config_env = getenv ("NOTMUCH_CONFIG"))) {
209         config->filename = talloc_strdup (config, notmuch_config_env);
210     } else {
211         config->filename = talloc_asprintf (config, "%s/.notmuch-config",
212                                             getenv ("HOME"));
213     }
214
215     config->key_file = g_key_file_new ();
216
217     config->database_path = NULL;
218     config->user_name = NULL;
219     config->user_primary_email = NULL;
220     config->user_other_email = NULL;
221     config->user_other_email_length = 0;
222     config->new_tags = NULL;
223     config->new_tags_length = 0;
224
225     if (! g_key_file_load_from_file (config->key_file,
226                                      config->filename,
227                                      G_KEY_FILE_KEEP_COMMENTS,
228                                      &error))
229     {
230         /* If the caller passed a non-NULL value for is_new_ret, then
231          * the caller is prepared for a default configuration file in
232          * the case of FILE NOT FOUND. Otherwise, any read failure is
233          * an error.
234          */
235         if (is_new_ret &&
236             error->domain == G_FILE_ERROR &&
237             error->code == G_FILE_ERROR_NOENT)
238         {
239             g_error_free (error);
240             is_new = 1;
241         }
242         else
243         {
244             fprintf (stderr, "Error reading configuration file %s: %s\n",
245                      config->filename, error->message);
246             talloc_free (config);
247             g_error_free (error);
248             return NULL;
249         }
250     }
251
252     /* Whenever we know of configuration sections that don't appear in
253      * the configuration file, we add some comments to help the user
254      * understand what can be done.
255      *
256      * It would be convenient to just add those comments now, but
257      * apparently g_key_file will clear any comments when keys are
258      * added later that create the groups. So we have to check for the
259      * groups now, but add the comments only after setting all of our
260      * values.
261      */
262     file_had_database_group = g_key_file_has_group (config->key_file,
263                                                     "database");
264     file_had_new_group = g_key_file_has_group (config->key_file, "new");
265     file_had_user_group = g_key_file_has_group (config->key_file, "user");
266
267
268     if (notmuch_config_get_database_path (config) == NULL) {
269         char *path = talloc_asprintf (config, "%s/mail",
270                                       getenv ("HOME"));
271         notmuch_config_set_database_path (config, path);
272         talloc_free (path);
273     }
274
275     if (notmuch_config_get_user_name (config) == NULL) {
276         char *name = get_name_from_passwd_file (config);
277         notmuch_config_set_user_name (config, name);
278         talloc_free (name);
279     }
280
281     if (notmuch_config_get_user_primary_email (config) == NULL) {
282         char *email = getenv ("EMAIL");
283         if (email) {
284             notmuch_config_set_user_primary_email (config, email);
285         } else {
286             char hostname[256];
287             struct hostent *hostent;
288             const char *domainname;
289
290             char *username = get_username_from_passwd_file (config);
291
292             gethostname (hostname, 256);
293             hostname[255] = '\0';
294
295             hostent = gethostbyname (hostname);
296             if (hostent && (domainname = strchr (hostent->h_name, '.')))
297                 domainname += 1;
298             else
299                 domainname = "(none)";
300
301             email = talloc_asprintf (config, "%s@%s.%s",
302                                      username, hostname, domainname);
303
304             notmuch_config_set_user_primary_email (config, email);
305
306             talloc_free (username);
307             talloc_free (email);
308         }
309     }
310
311     if (notmuch_config_get_new_tags (config, &tmp) == NULL) {
312         const char *tags[] = { "unread", "inbox" };
313         notmuch_config_set_new_tags (config, tags, 2);
314     }
315
316     /* Whenever we know of configuration sections that don't appear in
317      * the configuration file, we add some comments to help the user
318      * understand what can be done. */
319     if (is_new)
320     {
321         g_key_file_set_comment (config->key_file, NULL, NULL,
322                                 toplevel_config_comment, NULL);
323     }
324
325     if (! file_had_database_group)
326     {
327         g_key_file_set_comment (config->key_file, "database", NULL,
328                                 database_config_comment, NULL);
329     }
330
331     if (! file_had_new_group)
332     {
333         g_key_file_set_comment (config->key_file, "new", NULL,
334                                 new_config_comment, NULL);
335     }
336
337     if (! file_had_user_group)
338     {
339         g_key_file_set_comment (config->key_file, "user", NULL,
340                                 user_config_comment, NULL);
341     }
342
343     if (is_new_ret)
344         *is_new_ret = is_new;
345
346     return config;
347 }
348
349 /* Close the given notmuch_config_t object, freeing all resources.
350  * 
351  * Note: Any changes made to the configuration are *not* saved by this
352  * function. To save changes, call notmuch_config_save before
353  * notmuch_config_close.
354 */
355 void
356 notmuch_config_close (notmuch_config_t *config)
357 {
358     talloc_free (config);
359 }
360
361 /* Save any changes made to the notmuch configuration.
362  *
363  * Any comments originally in the file will be preserved.
364  *
365  * Returns 0 if successful, and 1 in case of any error, (after
366  * printing a description of the error to stderr).
367  */
368 int
369 notmuch_config_save (notmuch_config_t *config)
370 {
371     size_t length;
372     char *data;
373     GError *error = NULL;
374
375     data = g_key_file_to_data (config->key_file, &length, NULL);
376     if (data == NULL) {
377         fprintf (stderr, "Out of memory.\n");
378         return 1;
379     }
380
381     if (! g_file_set_contents (config->filename, data, length, &error)) {
382         fprintf (stderr, "Error saving configuration to %s: %s\n",
383                  config->filename, error->message);
384         g_error_free (error);
385         g_free (data);
386         return 1;
387     }
388
389     g_free (data);
390     return 0;
391 }
392
393 const char *
394 notmuch_config_get_database_path (notmuch_config_t *config)
395 {
396     char *path;
397
398     if (config->database_path == NULL) {
399         path = g_key_file_get_string (config->key_file,
400                                       "database", "path", NULL);
401         if (path) {
402             config->database_path = talloc_strdup (config, path);
403             free (path);
404         }
405     }
406
407     return config->database_path;
408 }
409
410 void
411 notmuch_config_set_database_path (notmuch_config_t *config,
412                                   const char *database_path)
413 {
414     g_key_file_set_string (config->key_file,
415                            "database", "path", database_path);
416
417     talloc_free (config->database_path);
418     config->database_path = NULL;
419 }
420
421 const char *
422 notmuch_config_get_user_name (notmuch_config_t *config)
423 {
424     char *name;
425
426     if (config->user_name == NULL) {
427         name = g_key_file_get_string (config->key_file,
428                                       "user", "name", NULL);
429         if (name) {
430             config->user_name = talloc_strdup (config, name);
431             free (name);
432         }
433     }
434
435     return config->user_name;
436 }
437
438 void
439 notmuch_config_set_user_name (notmuch_config_t *config,
440                               const char *user_name)
441 {
442     g_key_file_set_string (config->key_file,
443                            "user", "name", user_name);
444
445     talloc_free (config->user_name);
446     config->user_name = NULL;
447 }
448
449 const char *
450 notmuch_config_get_user_primary_email (notmuch_config_t *config)
451 {
452     char *email;
453
454     if (config->user_primary_email == NULL) {
455         email = g_key_file_get_string (config->key_file,
456                                        "user", "primary_email", NULL);
457         if (email) {
458             config->user_primary_email = talloc_strdup (config, email);
459             free (email);
460         }
461     }
462
463     return config->user_primary_email;
464 }
465
466 void
467 notmuch_config_set_user_primary_email (notmuch_config_t *config,
468                                        const char *primary_email)
469 {
470     g_key_file_set_string (config->key_file,
471                            "user", "primary_email", primary_email);
472
473     talloc_free (config->user_primary_email);
474     config->user_primary_email = NULL;
475 }
476
477 const char **
478 notmuch_config_get_user_other_email (notmuch_config_t *config,
479                                      size_t *length)
480 {
481     char **emails;
482     size_t emails_length;
483     unsigned int i;
484
485     if (config->user_other_email == NULL) {
486         emails = g_key_file_get_string_list (config->key_file,
487                                              "user", "other_email",
488                                              &emails_length, NULL);
489         if (emails) {
490             config->user_other_email = talloc_size (config,
491                                                     sizeof (char *) *
492                                                     (emails_length + 1));
493             for (i = 0; i < emails_length; i++)
494                 config->user_other_email[i] = talloc_strdup (config->user_other_email,
495                                                              emails[i]);
496             config->user_other_email[i] = NULL;
497
498             g_strfreev (emails);
499
500             config->user_other_email_length = emails_length;
501         }
502     }
503
504     *length = config->user_other_email_length;
505     return config->user_other_email;
506 }
507
508 void
509 notmuch_config_set_user_other_email (notmuch_config_t *config,
510                                      const char *other_email[],
511                                      size_t length)
512 {
513     g_key_file_set_string_list (config->key_file,
514                                 "user", "other_email",
515                                 other_email, length);
516
517     talloc_free (config->user_other_email);
518     config->user_other_email = NULL;
519 }
520
521 const char **
522 notmuch_config_get_new_tags (notmuch_config_t *config,
523                              size_t *length)
524 {
525     char **tags;
526     size_t tags_length;
527     unsigned int i;
528
529     if (config->new_tags == NULL) {
530         tags = g_key_file_get_string_list (config->key_file,
531                                            "new", "tags",
532                                            &tags_length, NULL);
533         if (tags) {
534             config->new_tags = talloc_size (config,
535                                             sizeof (char *) *
536                                             (tags_length + 1));
537             for (i = 0; i < tags_length; i++)
538                 config->new_tags[i] = talloc_strdup (config->new_tags,
539                                                      tags[i]);
540             config->new_tags[i] = NULL;
541
542             g_strfreev (tags);
543
544             config->new_tags_length = tags_length;
545         }
546     }
547
548     *length = config->new_tags_length;
549     return config->new_tags;
550 }
551
552 void
553 notmuch_config_set_new_tags (notmuch_config_t *config,
554                              const char *new_tags[],
555                              size_t length)
556 {
557     g_key_file_set_string_list (config->key_file,
558                                 "new", "tags",
559                                 new_tags, length);
560
561     talloc_free (config->new_tags);
562     config->new_tags = NULL;
563 }
564
565 /* Given a configuration item of the form <group>.<key> return the
566  * component group and key. If any error occurs, print a message on
567  * stderr and return 1. Otherwise, return 0.
568  *
569  * Note: This function modifies the original 'item' string.
570  */
571 static int
572 _item_split (char *item, char **group, char **key)
573 {
574     char *period;
575
576     *group = item;
577
578     period = index (item, '.');
579     if (period == NULL || *(period+1) == '\0') {
580         fprintf (stderr,
581                  "Invalid configuration name: %s\n"
582                  "(Should be of the form <section>.<item>)\n", item);
583         return 1;
584     }
585
586     *period = '\0';
587     *key = period + 1;
588
589     return 0;
590 }
591
592 static int
593 notmuch_config_command_get (void *ctx, char *item)
594 {
595     notmuch_config_t *config;
596
597     config = notmuch_config_open (ctx, NULL, NULL);
598     if (config == NULL)
599         return 1;
600
601     if (strcmp(item, "database.path") == 0) {
602         printf ("%s\n", notmuch_config_get_database_path (config));
603     } else if (strcmp(item, "user.name") == 0) {
604         printf ("%s\n", notmuch_config_get_user_name (config));
605     } else if (strcmp(item, "user.primary_email") == 0) {
606         printf ("%s\n", notmuch_config_get_user_primary_email (config));
607     } else if (strcmp(item, "user.other_email") == 0) {
608         const char **other_email;
609         size_t i, length;
610         
611         other_email = notmuch_config_get_user_other_email (config, &length);
612         for (i = 0; i < length; i++)
613             printf ("%s\n", other_email[i]);
614     } else if (strcmp(item, "new.tags") == 0) {
615         const char **tags;
616         size_t i, length;
617
618         tags = notmuch_config_get_new_tags (config, &length);
619         for (i = 0; i < length; i++)
620             printf ("%s\n", tags[i]);
621     } else {
622         char **value;
623         size_t i, length;
624         char *group, *key;
625
626         if (_item_split (item, &group, &key))
627             return 1;
628
629         value = g_key_file_get_string_list (config->key_file,
630                                             group, key,
631                                             &length, NULL);
632         if (value == NULL) {
633             fprintf (stderr, "Unknown configuration item: %s.%s\n",
634                      group, key);
635             return 1;
636         }
637
638         for (i = 0; i < length; i++)
639             printf ("%s\n", value[i]);
640
641         free (value);
642     }
643
644     notmuch_config_close (config);
645
646     return 0;
647 }
648
649 static int
650 notmuch_config_command_set (void *ctx, char *item, int argc, char *argv[])
651 {
652     notmuch_config_t *config;
653     char *group, *key;
654     int ret;
655
656     if (_item_split (item, &group, &key))
657         return 1;
658
659     config = notmuch_config_open (ctx, NULL, NULL);
660     if (config == NULL)
661         return 1;
662
663     /* With only the name of an item, we clear it from the
664      * configuration file.
665      *
666      * With a single value, we set it as a string.
667      *
668      * With multiple values, we set them as a string list.
669      */
670     switch (argc) {
671     case 0:
672         g_key_file_remove_key (config->key_file, group, key, NULL);
673         break;
674     case 1:
675         g_key_file_set_string (config->key_file, group, key, argv[0]);
676         break;
677     default:
678         g_key_file_set_string_list (config->key_file, group, key,
679                                     (const gchar **) argv, argc);
680         break;
681     }
682
683     ret = notmuch_config_save (config);
684     notmuch_config_close (config);
685
686     return ret;
687 }
688
689 int
690 notmuch_config_command (void *ctx, int argc, char *argv[])
691 {
692     if (argc < 2) {
693         fprintf (stderr, "Error: notmuch config requires at least two arguments.\n");
694         return 1;
695     }
696
697     if (strcmp (argv[0], "get") == 0)
698         return notmuch_config_command_get (ctx, argv[1]);
699     else if (strcmp (argv[0], "set") == 0)
700         return notmuch_config_command_set (ctx, argv[1], argc - 2, argv + 2);
701
702     fprintf (stderr, "Unrecognized argument for notmuch config: %s\n",
703              argv[0]);
704     return 1;
705 }