From: David Bremner <david@tethera.net>
To: notmuch@notmuchmail.org
Cc: David Bremner <david@tethera.net>
Subject: [PATCH 14/18] CLI/config: migrate notmuch_config_open to new config
Date: Sat, 20 Feb 2021 12:44:44 -0400 [thread overview]
Message-ID: <20210220164448.3956011-15-david@tethera.net> (raw)
In-Reply-To: <20210220164448.3956011-1-david@tethera.net>
notmuch_config_open will be preserved in the medium term for use by
the commands that are manipulating the config file directly (config
and setup)
---
lib/config.cc | 5 +-
notmuch-client.h | 2 +-
notmuch-config.c | 157 +----------------------------------------
notmuch.c | 2 +-
test/T030-config.sh | 2 +-
test/T590-libconfig.sh | 12 ++--
6 files changed, 15 insertions(+), 165 deletions(-)
diff --git a/lib/config.cc b/lib/config.cc
index 03ba2731..44cbe711 100644
--- a/lib/config.cc
+++ b/lib/config.cc
@@ -575,7 +575,7 @@ _notmuch_config_default (notmuch_database_t *notmuch, notmuch_config_key_t key)
case NOTMUCH_CONFIG_EXCLUDE_TAGS:
return "";
case NOTMUCH_CONFIG_NEW_TAGS:
- return "inbox;unread";
+ return "unread;inbox";
case NOTMUCH_CONFIG_SYNC_MAILDIR_FLAGS:
return "true";
case NOTMUCH_CONFIG_USER_NAME:
@@ -592,9 +592,10 @@ _notmuch_config_default (notmuch_database_t *notmuch, notmuch_config_key_t key)
else
email = _get_email_from_passwd_file (notmuch);
return email;
+ case NOTMUCH_CONFIG_NEW_IGNORE:
+ return "";
case NOTMUCH_CONFIG_HOOK_DIR:
case NOTMUCH_CONFIG_BACKUP_DIR:
- case NOTMUCH_CONFIG_NEW_IGNORE:
case NOTMUCH_CONFIG_OTHER_EMAIL:
return NULL;
default:
diff --git a/notmuch-client.h b/notmuch-client.h
index db88daf8..dfdfc876 100644
--- a/notmuch-client.h
+++ b/notmuch-client.h
@@ -262,7 +262,7 @@ typedef enum {
} notmuch_command_mode_t;
notmuch_config_t *
-notmuch_config_open (void *ctx,
+notmuch_config_open (notmuch_database_t *notmuch,
const char *filename,
notmuch_command_mode_t config_mode);
diff --git a/notmuch-config.c b/notmuch-config.c
index f46e5c27..3d0959fb 100644
--- a/notmuch-config.c
+++ b/notmuch-config.c
@@ -141,69 +141,6 @@ notmuch_config_destructor (notmuch_config_t *config)
return 0;
}
-static char *
-get_name_from_passwd_file (void *ctx)
-{
- long pw_buf_size;
- char *pw_buf;
- struct passwd passwd, *ignored;
- char *name;
- int e;
-
- pw_buf_size = sysconf (_SC_GETPW_R_SIZE_MAX);
- if (pw_buf_size == -1) pw_buf_size = 64;
- pw_buf = talloc_size (ctx, pw_buf_size);
-
- 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,
- comma - passwd.pw_gecos);
- else
- name = talloc_strdup (ctx, passwd.pw_gecos);
- } else {
- name = talloc_strdup (ctx, "");
- }
-
- talloc_free (pw_buf);
-
- return name;
-}
-
-static char *
-get_username_from_passwd_file (void *ctx)
-{
- long pw_buf_size;
- char *pw_buf;
- struct passwd passwd, *ignored;
- char *name;
- int e;
-
- pw_buf_size = sysconf (_SC_GETPW_R_SIZE_MAX);
- if (pw_buf_size == -1) pw_buf_size = 64;
- pw_buf = talloc_zero_size (ctx, pw_buf_size);
-
- 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)
- name = talloc_strdup (ctx, passwd.pw_name);
- else
- name = talloc_strdup (ctx, "");
-
- talloc_free (pw_buf);
-
- return name;
-}
static bool
get_config_from_file (notmuch_config_t *config, bool create_new)
@@ -322,12 +259,10 @@ get_config_from_file (notmuch_config_t *config, bool create_new)
* user in editing the file directly.
*/
notmuch_config_t *
-notmuch_config_open (void *ctx,
+notmuch_config_open (notmuch_database_t *notmuch,
const char *filename,
notmuch_command_mode_t config_mode)
{
- GError *error = NULL;
- size_t tmp;
char *notmuch_config_env = NULL;
int file_had_database_group;
int file_had_new_group;
@@ -336,7 +271,7 @@ notmuch_config_open (void *ctx,
int file_had_search_group;
int file_had_crypto_group;
- notmuch_config_t *config = talloc_zero (ctx, notmuch_config_t);
+ notmuch_config_t *config = talloc_zero (notmuch, notmuch_config_t);
if (config == NULL) {
fprintf (stderr, "Out of memory.\n");
@@ -368,15 +303,10 @@ notmuch_config_open (void *ctx,
}
}
+
/* Whenever we know of configuration sections that don't appear in
* the configuration file, we add some comments to help the user
* understand what can be done.
- *
- * It would be convenient to just add those comments now, but
- * apparently g_key_file will clear any comments when keys are
- * added later that create the groups. So we have to check for the
- * groups now, but add the comments only after setting all of our
- * values.
*/
file_had_database_group = g_key_file_has_group (config->key_file,
"database");
@@ -386,87 +316,6 @@ notmuch_config_open (void *ctx,
file_had_search_group = g_key_file_has_group (config->key_file, "search");
file_had_crypto_group = g_key_file_has_group (config->key_file, "crypto");
- if (notmuch_config_get_database_path (config) == NULL) {
- char *path = getenv ("MAILDIR");
- if (path)
- path = talloc_strdup (config, path);
- else
- path = talloc_asprintf (config, "%s/mail",
- getenv ("HOME"));
- notmuch_config_set_database_path (config, path);
- talloc_free (path);
- }
-
- if (notmuch_config_get_user_name (config) == NULL) {
- char *name = getenv ("NAME");
- if (name)
- name = talloc_strdup (config, name);
- else
- name = get_name_from_passwd_file (config);
- notmuch_config_set_user_name (config, name);
- talloc_free (name);
- }
-
- if (notmuch_config_get_user_primary_email (config) == NULL) {
- char *email = getenv ("EMAIL");
- if (email) {
- notmuch_config_set_user_primary_email (config, email);
- } else {
- char hostname[256];
- struct hostent *hostent;
- const char *domainname;
-
- char *username = get_username_from_passwd_file (config);
-
- gethostname (hostname, 256);
- hostname[255] = '\0';
-
- hostent = gethostbyname (hostname);
- if (hostent && (domainname = strchr (hostent->h_name, '.')))
- domainname += 1;
- else
- domainname = "(none)";
-
- email = talloc_asprintf (config, "%s@%s.%s",
- username, hostname, domainname);
-
- notmuch_config_set_user_primary_email (config, email);
-
- talloc_free (username);
- talloc_free (email);
- }
- }
-
- if (notmuch_config_get_new_tags (config, &tmp) == NULL) {
- const char *tags[] = { "unread", "inbox" };
- notmuch_config_set_new_tags (config, tags, 2);
- }
-
- if (notmuch_config_get_new_ignore (config, &tmp) == NULL) {
- notmuch_config_set_new_ignore (config, NULL, 0);
- }
-
- if (notmuch_config_get_search_exclude_tags (config, &tmp) == NULL) {
- if (config->is_new) {
- const char *tags[] = { "deleted", "spam" };
- notmuch_config_set_search_exclude_tags (config, tags, 2);
- } else {
- notmuch_config_set_search_exclude_tags (config, NULL, 0);
- }
- }
-
- error = NULL;
- config->maildir_synchronize_flags =
- g_key_file_get_boolean (config->key_file,
- "maildir", "synchronize_flags", &error);
- if (error) {
- notmuch_config_set_maildir_synchronize_flags (config, true);
- g_error_free (error);
- }
-
- /* Whenever we know of configuration sections that don't appear in
- * the configuration file, we add some comments to help the user
- * understand what can be done. */
if (config->is_new)
g_key_file_set_comment (config->key_file, NULL, NULL,
toplevel_config_comment, NULL);
diff --git a/notmuch.c b/notmuch.c
index 7beeb177..093af0a5 100644
--- a/notmuch.c
+++ b/notmuch.c
@@ -577,7 +577,7 @@ main (int argc, char *argv[])
}
if (command->mode & NOTMUCH_COMMAND_CONFIG_OPEN) {
- config = notmuch_config_open (local, config_file_name, command->mode);
+ config = notmuch_config_open (notmuch, config_file_name, command->mode);
if (! config) {
ret = EXIT_FAILURE;
goto DONE;
diff --git a/test/T030-config.sh b/test/T030-config.sh
index d974850f..9a43c1e4 100755
--- a/test/T030-config.sh
+++ b/test/T030-config.sh
@@ -57,7 +57,7 @@ foo.list=this;is another;list value;
foo.string=this is another string value
maildir.synchronize_flags=true
new.ignore=
-new.tags=unread;inbox;
+new.tags=unread;inbox
search.exclude_tags=
user.name=Notmuch Test Suite
user.other_email=test_suite_other@notmuchmail.org;test_suite@otherdomain.org
diff --git a/test/T590-libconfig.sh b/test/T590-libconfig.sh
index cf294d04..75b47073 100755
--- a/test/T590-libconfig.sh
+++ b/test/T590-libconfig.sh
@@ -394,8 +394,8 @@ MAIL_DIR
MAIL_DIR/.notmuch/hooks
MAIL_DIR/.notmuch/backups
-inbox;unread
-NULL
+unread;inbox
+
true
USERNAME@FQDN
NULL
@@ -705,7 +705,7 @@ MAIL_DIR
MAIL_DIR/.notmuch/hooks
MAIL_DIR/.notmuch/backups
foo;bar;fub
-unread;inbox;
+unread;inbox
sekrit_junk
true
test_suite@notmuchmail.org
@@ -736,8 +736,8 @@ MAIL_DIR
MAIL_DIR/.notmuch/hooks
MAIL_DIR/.notmuch/backups
-inbox;unread
-NULL
+unread;inbox
+
true
USERNAME@FQDN
NULL
@@ -815,7 +815,7 @@ database.path MAIL_DIR
key with spaces value, with, spaces!
maildir.synchronize_flags true
new.ignore sekrit_junk
-new.tags unread;inbox;
+new.tags unread;inbox
search.exclude_tags foo;bar;fub
test.key1 testvalue1
test.key2 testvalue2
--
2.30.0
next prev parent reply other threads:[~2021-02-20 16:46 UTC|newest]
Thread overview: 19+ messages / expand[flat|nested] mbox.gz Atom feed top
2021-02-20 16:44 RFC convert remaining CLI to new configuration David Bremner
2021-02-20 16:44 ` [PATCH 01/18] lib: add missing status strings David Bremner
2021-02-20 16:44 ` [PATCH 02/18] test: convert random-corpus to use n_d_open_with_config David Bremner
2021-02-20 16:44 ` [PATCH 03/18] lib/open: pull _load_key_file out of _choose_database_path David Bremner
2021-02-20 16:44 ` [PATCH 04/18] WIP: add notmuch_database_load_config David Bremner
2021-02-20 16:44 ` [PATCH 05/18] WIP: add n_d_get_config_values David Bremner
2021-02-20 16:44 ` [PATCH 06/18] lib/config: add config_pairs iterators David Bremner
2021-02-20 16:44 ` [PATCH 07/18] lib/config: set defaults for user full name David Bremner
2021-02-20 16:44 ` [PATCH 08/18] lib/config: set default for primary user email David Bremner
2021-02-20 16:44 ` [PATCH 09/18] CLI/notmuch: replace use of notmuch_config_get_* David Bremner
2021-02-20 16:44 ` [PATCH 10/18] CLI/config: WIP: load merged config David Bremner
2021-02-20 16:44 ` [PATCH 11/18] WIP: CLI/config: use merged config for "config get" David Bremner
2021-02-20 16:44 ` [PATCH 12/18] WIP: switch notmuch-setup to new configuration framework David Bremner
2021-02-20 16:44 ` [PATCH 13/18] WIP: CLI/config: switch list to new config David Bremner
2021-02-20 16:44 ` David Bremner [this message]
2021-02-20 16:44 ` [PATCH 15/18] WIP: cli/config: convert to new config API David Bremner
2021-02-20 16:44 ` [PATCH 16/18] cli/config: drop obsolete notmuch_config_get_* David Bremner
2021-02-20 16:44 ` [PATCH 17/18] CLI/config: drop cached data from notmuch_config_t David Bremner
2021-02-20 16:44 ` [PATCH 18/18] WIP: CLI/config: make storing configuration in database optional David Bremner
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
List information: https://notmuchmail.org/
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20210220164448.3956011-15-david@tethera.net \
--to=david@tethera.net \
--cc=notmuch@notmuchmail.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
Code repositories for project(s) associated with this public inbox
https://yhetil.org/notmuch.git/
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).