1 /* notmuch - Not much of an email program, (just index and search)
3 * Copyright © 2009 Carl Worth
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.
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.
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/ .
18 * Author: Carl Worth <cworth@cworth.org>
21 #include "notmuch-client.h"
26 static const char toplevel_config_comment[] =
27 " .notmuch-config - Configuration file for the notmuch mail system\n"
29 " For more information about notmuch, see http://notmuchmail.org";
31 static const char database_config_comment[] =
32 " Database configuration\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";
40 static const char new_config_comment[] =
41 " Configuration for \"notmuch new\"\n"
43 " The following options are supported here:\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";
48 static const char user_config_comment[] =
49 " User configuration\n"
51 " Here is where you can let notmuch know how you would like to be\n"
52 " addressed. Valid settings are\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"
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";
64 static const char maildir_config_comment[] =
65 " Maildir compatibility configuration\n"
67 " Here you can configure whether notmuch will synchronize its tags with\n"
70 "\tsynchronize_flags Valid values are true and false.\n";
72 struct _notmuch_config {
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_synchronize_flags;
86 #define MAILDIR_SYNC_UNDEF ((notmuch_bool_t)-1)
89 notmuch_config_destructor (notmuch_config_t *config)
92 g_key_file_free (config->key_file);
98 get_name_from_passwd_file (void *ctx)
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;
106 if (pw_buf_size == -1) pw_buf_size = 64;
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);
115 char *comma = strchr (passwd.pw_gecos, ',');
117 name = talloc_strndup (ctx, passwd.pw_gecos,
118 comma - passwd.pw_gecos);
120 name = talloc_strdup (ctx, passwd.pw_gecos);
122 name = talloc_strdup (ctx, "");
125 talloc_free (pw_buf);
131 get_username_from_passwd_file (void *ctx)
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;
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);
147 name = talloc_strdup (ctx, passwd.pw_name);
149 name = talloc_strdup (ctx, "");
151 talloc_free (pw_buf);
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.
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
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'
170 * If is_new_ret is NULL, then a "file not found" message will be
171 * printed to stderr and NULL will be returned.
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.
177 * These default configuration settings are determined as
180 * database_path: $HOME/mail
182 * user_name: From /etc/passwd
184 * user_primary_mail: $EMAIL variable if set, otherwise
185 * constructed from the username and
186 * hostname of the current machine.
188 * user_other_email: Not set.
190 * The default configuration also contains comments to guide the
191 * user in editing the file directly.
194 notmuch_config_open (void *ctx,
195 const char *filename,
196 notmuch_bool_t *is_new_ret)
198 GError *error = NULL;
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;
210 notmuch_config_t *config = talloc (ctx, notmuch_config_t);
211 if (config == NULL) {
212 fprintf (stderr, "Out of memory.\n");
216 talloc_set_destructor (config, notmuch_config_destructor);
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);
223 config->filename = talloc_asprintf (config, "%s/.notmuch-config",
227 config->key_file = g_key_file_new ();
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_synchronize_flags = MAILDIR_SYNC_UNDEF;
238 if (! g_key_file_load_from_file (config->key_file,
240 G_KEY_FILE_KEEP_COMMENTS,
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
249 error->domain == G_FILE_ERROR &&
250 error->code == G_FILE_ERROR_NOENT)
252 g_error_free (error);
257 fprintf (stderr, "Error reading configuration file %s: %s\n",
258 config->filename, error->message);
259 talloc_free (config);
260 g_error_free (error);
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.
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
275 file_had_database_group = g_key_file_has_group (config->key_file,
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");
282 if (notmuch_config_get_database_path (config) == NULL) {
283 char *path = talloc_asprintf (config, "%s/mail",
285 notmuch_config_set_database_path (config, path);
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);
295 if (notmuch_config_get_user_primary_email (config) == NULL) {
296 char *email = getenv ("EMAIL");
298 notmuch_config_set_user_primary_email (config, email);
301 struct hostent *hostent;
302 const char *domainname;
304 char *username = get_username_from_passwd_file (config);
306 gethostname (hostname, 256);
307 hostname[255] = '\0';
309 hostent = gethostbyname (hostname);
310 if (hostent && (domainname = strchr (hostent->h_name, '.')))
313 domainname = "(none)";
315 email = talloc_asprintf (config, "%s@%s.%s",
316 username, hostname, domainname);
318 notmuch_config_set_user_primary_email (config, email);
320 talloc_free (username);
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);
330 if (notmuch_config_get_maildir_synchronize_flags (config) == MAILDIR_SYNC_UNDEF) {
331 notmuch_config_set_maildir_synchronize_flags (config, FALSE);
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. */
339 g_key_file_set_comment (config->key_file, NULL, NULL,
340 toplevel_config_comment, NULL);
343 if (! file_had_database_group)
345 g_key_file_set_comment (config->key_file, "database", NULL,
346 database_config_comment, NULL);
349 if (! file_had_new_group)
351 g_key_file_set_comment (config->key_file, "new", NULL,
352 new_config_comment, NULL);
355 if (! file_had_user_group)
357 g_key_file_set_comment (config->key_file, "user", NULL,
358 user_config_comment, NULL);
361 if (! file_had_maildir_group)
363 g_key_file_set_comment (config->key_file, "maildir", NULL,
364 maildir_config_comment, NULL);
368 *is_new_ret = is_new;
373 /* Close the given notmuch_config_t object, freeing all resources.
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.
380 notmuch_config_close (notmuch_config_t *config)
382 talloc_free (config);
385 /* Save any changes made to the notmuch configuration.
387 * Any comments originally in the file will be preserved.
389 * Returns 0 if successful, and 1 in case of any error, (after
390 * printing a description of the error to stderr).
393 notmuch_config_save (notmuch_config_t *config)
397 GError *error = NULL;
399 data = g_key_file_to_data (config->key_file, &length, NULL);
401 fprintf (stderr, "Out of memory.\n");
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);
418 notmuch_config_get_database_path (notmuch_config_t *config)
422 if (config->database_path == NULL) {
423 path = g_key_file_get_string (config->key_file,
424 "database", "path", NULL);
426 config->database_path = talloc_strdup (config, path);
431 return config->database_path;
435 notmuch_config_set_database_path (notmuch_config_t *config,
436 const char *database_path)
438 g_key_file_set_string (config->key_file,
439 "database", "path", database_path);
441 talloc_free (config->database_path);
442 config->database_path = NULL;
446 notmuch_config_get_user_name (notmuch_config_t *config)
450 if (config->user_name == NULL) {
451 name = g_key_file_get_string (config->key_file,
452 "user", "name", NULL);
454 config->user_name = talloc_strdup (config, name);
459 return config->user_name;
463 notmuch_config_set_user_name (notmuch_config_t *config,
464 const char *user_name)
466 g_key_file_set_string (config->key_file,
467 "user", "name", user_name);
469 talloc_free (config->user_name);
470 config->user_name = NULL;
474 notmuch_config_get_user_primary_email (notmuch_config_t *config)
478 if (config->user_primary_email == NULL) {
479 email = g_key_file_get_string (config->key_file,
480 "user", "primary_email", NULL);
482 config->user_primary_email = talloc_strdup (config, email);
487 return config->user_primary_email;
491 notmuch_config_set_user_primary_email (notmuch_config_t *config,
492 const char *primary_email)
494 g_key_file_set_string (config->key_file,
495 "user", "primary_email", primary_email);
497 talloc_free (config->user_primary_email);
498 config->user_primary_email = NULL;
502 notmuch_config_get_user_other_email (notmuch_config_t *config,
506 size_t emails_length;
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);
514 config->user_other_email = talloc_size (config,
516 (emails_length + 1));
517 for (i = 0; i < emails_length; i++)
518 config->user_other_email[i] = talloc_strdup (config->user_other_email,
520 config->user_other_email[i] = NULL;
524 config->user_other_email_length = emails_length;
528 *length = config->user_other_email_length;
529 return config->user_other_email;
533 notmuch_config_set_user_other_email (notmuch_config_t *config,
534 const char *other_email[],
537 g_key_file_set_string_list (config->key_file,
538 "user", "other_email",
539 other_email, length);
541 talloc_free (config->user_other_email);
542 config->user_other_email = NULL;
546 notmuch_config_get_new_tags (notmuch_config_t *config,
553 if (config->new_tags == NULL) {
554 tags = g_key_file_get_string_list (config->key_file,
558 config->new_tags = talloc_size (config,
561 for (i = 0; i < tags_length; i++)
562 config->new_tags[i] = talloc_strdup (config->new_tags,
564 config->new_tags[i] = NULL;
568 config->new_tags_length = tags_length;
572 *length = config->new_tags_length;
573 return config->new_tags;
577 notmuch_config_set_new_tags (notmuch_config_t *config,
578 const char *new_tags[],
581 g_key_file_set_string_list (config->key_file,
585 talloc_free (config->new_tags);
586 config->new_tags = NULL;
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.
593 * Note: This function modifies the original 'item' string.
596 _item_split (char *item, char **group, char **key)
602 period = index (item, '.');
603 if (period == NULL || *(period+1) == '\0') {
605 "Invalid configuration name: %s\n"
606 "(Should be of the form <section>.<item>)\n", item);
617 notmuch_config_command_get (void *ctx, char *item)
619 notmuch_config_t *config;
621 config = notmuch_config_open (ctx, NULL, NULL);
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;
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) {
642 tags = notmuch_config_get_new_tags (config, &length);
643 for (i = 0; i < length; i++)
644 printf ("%s\n", tags[i]);
650 if (_item_split (item, &group, &key))
653 value = g_key_file_get_string_list (config->key_file,
657 fprintf (stderr, "Unknown configuration item: %s.%s\n",
662 for (i = 0; i < length; i++)
663 printf ("%s\n", value[i]);
668 notmuch_config_close (config);
674 notmuch_config_command_set (void *ctx, char *item, int argc, char *argv[])
676 notmuch_config_t *config;
680 if (_item_split (item, &group, &key))
683 config = notmuch_config_open (ctx, NULL, NULL);
687 /* With only the name of an item, we clear it from the
688 * configuration file.
690 * With a single value, we set it as a string.
692 * With multiple values, we set them as a string list.
696 g_key_file_remove_key (config->key_file, group, key, NULL);
699 g_key_file_set_string (config->key_file, group, key, argv[0]);
702 g_key_file_set_string_list (config->key_file, group, key,
703 (const gchar **) argv, argc);
707 ret = notmuch_config_save (config);
708 notmuch_config_close (config);
714 notmuch_config_command (void *ctx, int argc, char *argv[])
717 fprintf (stderr, "Error: notmuch config requires at least two arguments.\n");
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);
726 fprintf (stderr, "Unrecognized argument for notmuch config: %s\n",
732 notmuch_config_get_maildir_synchronize_flags (notmuch_config_t *config)
735 if (config->maildir_synchronize_flags == MAILDIR_SYNC_UNDEF) {
736 config->maildir_synchronize_flags =
737 g_key_file_get_boolean (config->key_file,
738 "maildir", "synchronize_flags", &err);
740 config->maildir_synchronize_flags = MAILDIR_SYNC_UNDEF;
744 return config->maildir_synchronize_flags;
748 notmuch_config_set_maildir_synchronize_flags (notmuch_config_t *config,
749 notmuch_bool_t synchronize_flags)
751 g_key_file_set_boolean (config->key_file,
752 "maildir", "synchronize_flags", synchronize_flags);
753 config->maildir_synchronize_flags = synchronize_flags;