X-Git-Url: https://git.notmuchmail.org/git?a=blobdiff_plain;f=notmuch-config.c;h=fa09628729a1a8a81823a0db20ddd8541752991d;hb=65d278afb1174a4005a613cca8c306289b195876;hp=248149c85de0ee0401816537ce84b1adad5a88f0;hpb=6bd01e1b340f6a209dde64471bc9d7137511dada;p=notmuch diff --git a/notmuch-config.c b/notmuch-config.c index 248149c8..fa096287 100644 --- a/notmuch-config.c +++ b/notmuch-config.c @@ -37,6 +37,12 @@ static const char database_config_comment[] = " Notmuch will store its database within a sub-directory of the path\n" " configured here named \".notmuch\".\n"; +static const char messages_config_comment[] = + " Messages configuration\n" + "\n" + " The only value supported here is 'new_tags' which lists the tags that\n" + " should be applied to new messages.\n"; + static const char user_config_comment[] = " User configuration\n" "\n" @@ -62,6 +68,8 @@ struct _notmuch_config { char *user_primary_email; char **user_other_email; size_t user_other_email_length; + const char **new_tags; + size_t new_tags_length; }; static int @@ -80,8 +88,17 @@ get_name_from_passwd_file (void *ctx) char *pw_buf = talloc_zero_size (ctx, pw_buf_size); struct passwd passwd, *ignored; char *name; + int e; - if (getpwuid_r (getuid (), &passwd, pw_buf, pw_buf_size, &ignored) == 0) { + if (pw_buf_size == -1) pw_buf_size = 64; + + while ((e = getpwuid_r (getuid (), &passwd, pw_buf, + pw_buf_size, &ignored)) == ERANGE) { + pw_buf_size = pw_buf_size * 2; + pw_buf = talloc_zero_size(ctx, pw_buf_size); + } + + if (e == 0) { char *comma = strchr (passwd.pw_gecos, ','); if (comma) name = talloc_strndup (ctx, passwd.pw_gecos, @@ -104,8 +121,16 @@ get_username_from_passwd_file (void *ctx) char *pw_buf = talloc_zero_size (ctx, pw_buf_size); struct passwd passwd, *ignored; char *name; + int e; + + if (pw_buf_size == -1) pw_buf_size = 64; + while ((e = getpwuid_r (getuid (), &passwd, pw_buf, + pw_buf_size, &ignored)) == ERANGE) { + pw_buf_size = pw_buf_size * 2; + pw_buf = talloc_zero_size(ctx, pw_buf_size); + } - if (getpwuid_r (getuid (), &passwd, pw_buf, pw_buf_size, &ignored) == 0) + if (e == 0) name = talloc_strdup (ctx, passwd.pw_name); else name = talloc_strdup (ctx, ""); @@ -115,9 +140,10 @@ get_username_from_passwd_file (void *ctx) return name; } -/* Open the named notmuch configuration file. A filename of NULL will - * be interpreted as the default configuration file - * ($HOME/.notmuch-config). +/* Open the named notmuch configuration file. If the filename is NULL, + * the value of the environment variable $NOTMUCH_CONFIG will be used. + * If $NOTMUCH_CONFIG is unset, the default configuration file + * ($HOME/.notmuch-config) will be used. * * If any error occurs, (out of memory, or a permission-denied error, * etc.), this function will print a message to stderr and return @@ -151,6 +177,8 @@ notmuch_config_open (void *ctx, { GError *error = NULL; int is_new = 0; + size_t tmp; + char *notmuch_config_env = NULL; if (is_new_ret) *is_new_ret = 0; @@ -163,11 +191,14 @@ notmuch_config_open (void *ctx, talloc_set_destructor (config, notmuch_config_destructor); - if (filename) + if (filename) { config->filename = talloc_strdup (config, filename); - else + } else if ((notmuch_config_env = getenv ("NOTMUCH_CONFIG"))) { + config->filename = talloc_strdup (config, notmuch_config_env); + } else { config->filename = talloc_asprintf (config, "%s/.notmuch-config", getenv ("HOME")); + } config->key_file = g_key_file_new (); @@ -176,6 +207,8 @@ notmuch_config_open (void *ctx, config->user_primary_email = NULL; config->user_other_email = NULL; config->user_other_email_length = 0; + config->new_tags = NULL; + config->new_tags_length = 0; if (! g_key_file_load_from_file (config->key_file, config->filename, @@ -183,16 +216,22 @@ notmuch_config_open (void *ctx, &error)) { /* We are capable of dealing with a non-existent configuration - * file, so be silent about that. */ - if (!(error->domain == G_FILE_ERROR && + * file, so be silent about that (unless the user had set a + * non-default configuration file with the NOTMUCH_CONFIG + * variable) + */ + if (notmuch_config_env || + !(error->domain == G_FILE_ERROR && error->code == G_FILE_ERROR_NOENT)) { fprintf (stderr, "Error reading configuration file %s: %s\n", config->filename, error->message); talloc_free (config); + g_error_free (error); return NULL; } + g_error_free (error); is_new = 1; } @@ -239,6 +278,11 @@ notmuch_config_open (void *ctx, } } + if (notmuch_config_get_new_tags (config, &tmp) == NULL) { + const char *tags[] = { "unread", "inbox" }; + notmuch_config_set_new_tags (config, tags, 2); + } + /* When we create a new configuration file here, we add some * comments to help the user understand what can be done. */ if (is_new) { @@ -246,6 +290,8 @@ notmuch_config_open (void *ctx, toplevel_config_comment, NULL); g_key_file_set_comment (config->key_file, "database", NULL, database_config_comment, NULL); + g_key_file_set_comment (config->key_file, "messages", NULL, + messages_config_comment, NULL); g_key_file_set_comment (config->key_file, "user", NULL, user_config_comment, NULL); } @@ -291,9 +337,12 @@ notmuch_config_save (notmuch_config_t *config) if (! g_file_set_contents (config->filename, data, length, &error)) { fprintf (stderr, "Error saving configuration to %s: %s\n", config->filename, error->message); + g_error_free (error); + g_free (data); return 1; } + g_free (data); return 0; } @@ -424,3 +473,48 @@ notmuch_config_set_user_other_email (notmuch_config_t *config, talloc_free (config->user_other_email); config->user_other_email = NULL; } + +const char ** +notmuch_config_get_new_tags (notmuch_config_t *config, + size_t *length) +{ + char **tags; + size_t tags_length; + unsigned int i; + + if (config->new_tags == NULL) { + tags = g_key_file_get_string_list (config->key_file, + "messages", "new_tags", + &tags_length, NULL); + if (tags) { + config->new_tags = talloc_size (config, + sizeof (char *) * + (tags_length + 1)); + for (i = 0; i < tags_length; i++) + config->new_tags[i] = talloc_strdup (config->new_tags, + tags[i]); + config->new_tags[i] = NULL; + + g_strfreev (tags); + + config->new_tags_length = tags_length; + } + } + + *length = config->new_tags_length; + return config->new_tags; +} + +void +notmuch_config_set_new_tags (notmuch_config_t *config, + const char *new_tags[], + size_t length) +{ + g_key_file_set_string_list (config->key_file, + "messages", "new_tags", + new_tags, length); + + talloc_free (config->new_tags); + config->new_tags = NULL; +} +