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