From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mp1 ([2001:41d0:2:bcc0::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by ms11 with LMTPS id wPJhBNE8MWDAYAAA0tVLHw (envelope-from ) for ; Sat, 20 Feb 2021 16:46:09 +0000 Received: from aspmx1.migadu.com ([2001:41d0:2:bcc0::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by mp1 with LMTPS id aKwYANE8MWBDYAAAbx9fmQ (envelope-from ) for ; Sat, 20 Feb 2021 16:46:09 +0000 Received: from mail.notmuchmail.org (nmbug.tethera.net [144.217.243.247]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by aspmx1.migadu.com (Postfix) with ESMTPS id 6C146133F5 for ; Sat, 20 Feb 2021 17:46:08 +0100 (CET) Received: from nmbug.tethera.net (localhost [127.0.0.1]) by mail.notmuchmail.org (Postfix) with ESMTP id D608C26C16; Sat, 20 Feb 2021 11:45:26 -0500 (EST) Received: from fethera.tethera.net (fethera.tethera.net [198.245.60.197]) by mail.notmuchmail.org (Postfix) with ESMTP id 1D99026AD5 for ; Sat, 20 Feb 2021 11:45:04 -0500 (EST) Received: by fethera.tethera.net (Postfix, from userid 1001) id 15F6D60666; Sat, 20 Feb 2021 11:45:04 -0500 (EST) Received: (nullmailer pid 3956190 invoked by uid 1000); Sat, 20 Feb 2021 16:44:54 -0000 From: David Bremner To: notmuch@notmuchmail.org Cc: David Bremner Subject: [PATCH 14/18] CLI/config: migrate notmuch_config_open to new config Date: Sat, 20 Feb 2021 12:44:44 -0400 Message-Id: <20210220164448.3956011-15-david@tethera.net> X-Mailer: git-send-email 2.30.0 In-Reply-To: <20210220164448.3956011-1-david@tethera.net> References: <20210220164448.3956011-1-david@tethera.net> MIME-Version: 1.0 Message-ID-Hash: 5BIMQTCUBZLFQWPLMVASRSZWR4BXCCVT X-Message-ID-Hash: 5BIMQTCUBZLFQWPLMVASRSZWR4BXCCVT X-MailFrom: bremner@tethera.net X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; emergency; loop; banned-address; member-moderation; header-match-notmuch.notmuchmail.org-0; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; suspicious-header X-Mailman-Version: 3.2.1 Precedence: list List-Id: "Use and development of the notmuch mail system." List-Help: List-Post: List-Subscribe: List-Unsubscribe: Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit X-Migadu-Flow: FLOW_IN X-Migadu-Spam-Score: 0.48 Authentication-Results: aspmx1.migadu.com; dkim=none; dmarc=none; spf=pass (aspmx1.migadu.com: domain of notmuch-bounces@notmuchmail.org designates 144.217.243.247 as permitted sender) smtp.mailfrom=notmuch-bounces@notmuchmail.org X-Migadu-Queue-Id: 6C146133F5 X-Spam-Score: 0.48 X-Migadu-Scanner: scn0.migadu.com X-TUID: z7nziaxYjopr 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