From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mp0 ([2001:41d0:2:4a6f::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by ms11 with LMTPS id cIJ5FnvaQGAobAAA0tVLHw (envelope-from ) for ; Thu, 04 Mar 2021 13:02:51 +0000 Received: from aspmx1.migadu.com ([2001:41d0:2:4a6f::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by mp0 with LMTPS id IGNMEnvaQGC1GgAA1q6Kng (envelope-from ) for ; Thu, 04 Mar 2021 13:02:51 +0000 Received: from mail.notmuchmail.org (nmbug.tethera.net [IPv6:2607:5300:201:3100::1657]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by aspmx1.migadu.com (Postfix) with ESMTPS id 9171615FA3 for ; Thu, 4 Mar 2021 14:02:50 +0100 (CET) Received: from nmbug.tethera.net (localhost [127.0.0.1]) by mail.notmuchmail.org (Postfix) with ESMTP id 4C6B127567; Thu, 4 Mar 2021 08:02:05 -0500 (EST) Received: from fethera.tethera.net (fethera.tethera.net [IPv6:2607:5300:60:c5::1]) by mail.notmuchmail.org (Postfix) with ESMTP id 29EAF1FADE for ; Thu, 4 Mar 2021 08:01:53 -0500 (EST) Received: by fethera.tethera.net (Postfix, from userid 1001) id 2155B5FA96; Thu, 4 Mar 2021 08:01:53 -0500 (EST) Received: (nullmailer pid 4047194 invoked by uid 1000); Thu, 04 Mar 2021 13:01:45 -0000 From: David Bremner To: notmuch@notmuchmail.org Cc: David Bremner Subject: [PATCH 15/27] CLI/config: migrate notmuch_config_open to new config Date: Thu, 4 Mar 2021 09:01:27 -0400 Message-Id: <20210304130139.4046183-16-david@tethera.net> X-Mailer: git-send-email 2.30.1 In-Reply-To: <20210304130139.4046183-1-david@tethera.net> References: <20210304130139.4046183-1-david@tethera.net> MIME-Version: 1.0 Message-ID-Hash: J3JJSQVIN44IHUNUPNTWU6YVVSI7MFGG X-Message-ID-Hash: J3JJSQVIN44IHUNUPNTWU6YVVSI7MFGG 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 ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=yhetil.org; s=key1; t=1614862970; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:list-id:list-help: list-unsubscribe:list-subscribe:list-post; bh=DBgrzfOVL0nBDHb+4GLxfLjggd/BHm+KypYEK7quuCw=; b=FYtxjo53/22zUc84cjL0TNi+pVtHRX2qGuCTeIhcuTaBnrOdgU4WX2Ro40sLeWzka1YI1+ V45wBxFmTRTXzYz1ByA0+vyCdFYSA0PbL+6LBnMP0eWBcJwTABNTcOCkVkVgvsl/jHkJxy VNDXeEGHXCRJ8X+38HnjvXGV6vfrIQpqSyYMYgHJd8nwM97qOgiKD5Cs747X/cWq2L6u8Z fCbnZreJ6l+3JTq7/c/ZBFJy0g6L6faiEamEAgCek8WRVrx/LXTF7dfmNOXyuSCstjuGsv 3z2n8EuBV13GNnNcFvDMHSPGZ4F4VqUF0n2sIWrJtNnMZp0flduapXfxdHr9Hg== ARC-Seal: i=1; s=key1; d=yhetil.org; t=1614862970; a=rsa-sha256; cv=none; b=duX6lQ9RGljI75iXA/8/LAtBUfoLkeGk3WR8nCAuTkf3Jl2/qW/F6///1Kb+aJueVBcgE+ IknfURlaF5pED9i1flkQI4bLfRkgXO0dhmvxfJMNtbzG3T3nCQzEbXMBBBuFbO3VAleCiT NjH01VN6OYhnSuVt+/h/qn/D+NE04tw2O3UwQbM2S6w1RDa6+J2vEFUz1SNOJ8qr7ImiDB szHnS2prrDyC3lVWtuntKXAr4kXikzgZ0FKx7xa5Ao/c8U/THIlXKj3moLxftSRFLj4YNE Fg6m5l2z/0/buWz3lWIscggyErNZZ3+OXhcYrVGwq9BSZudhDxGfpd7Mwu8lJw== ARC-Authentication-Results: i=1; aspmx1.migadu.com; dkim=none; dmarc=none; spf=pass (aspmx1.migadu.com: domain of notmuch-bounces@notmuchmail.org designates 2607:5300:201:3100::1657 as permitted sender) smtp.mailfrom=notmuch-bounces@notmuchmail.org X-Migadu-Spam-Score: -0.80 Authentication-Results: aspmx1.migadu.com; dkim=none; dmarc=none; spf=pass (aspmx1.migadu.com: domain of notmuch-bounces@notmuchmail.org designates 2607:5300:201:3100::1657 as permitted sender) smtp.mailfrom=notmuch-bounces@notmuchmail.org X-Migadu-Queue-Id: 9171615FA3 X-Spam-Score: -0.80 X-Migadu-Scanner: scn1.migadu.com X-TUID: tQWoRYTraOLN 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 | 371 +++++------------- notmuch.c | 2 +- test/T030-config.sh | 2 +- test/T590-libconfig.sh | 12 +- .../config-with-comments | 2 - 7 files changed, 106 insertions(+), 290 deletions(-) diff --git a/lib/config.cc b/lib/config.cc index cfb5bd13..855816c1 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 7c7f89d9..a46c639e 100644 --- a/notmuch-config.c +++ b/notmuch-config.c @@ -31,87 +31,88 @@ static const char toplevel_config_comment[] = "\n" " For more information about notmuch, see https://notmuchmail.org"; -static const char database_config_comment[] = - " Database configuration\n" - "\n" - " The only value supported here is 'path' which should be the top-level\n" - " directory where your mail currently exists and to where mail will be\n" - " delivered in the future. Files should be individual email messages.\n" - " Notmuch will store its database within a sub-directory of the path\n" - " configured here named \".notmuch\".\n"; - -static const char new_config_comment[] = - " Configuration for \"notmuch new\"\n" - "\n" - " The following options are supported here:\n" - "\n" - "\ttags A list (separated by ';') of the tags that will be\n" - "\t added to all messages incorporated by \"notmuch new\".\n" - "\n" - "\tignore A list (separated by ';') of file and directory names\n" - "\t that will not be searched for messages by \"notmuch new\".\n" - "\n" - "\t NOTE: *Every* file/directory that goes by one of those\n" - "\t names will be ignored, independent of its depth/location\n" - "\t in the mail store.\n"; - -static const char user_config_comment[] = - " User configuration\n" - "\n" - " Here is where you can let notmuch know how you would like to be\n" - " addressed. Valid settings are\n" - "\n" - "\tname Your full name.\n" - "\tprimary_email Your primary email address.\n" - "\tother_email A list (separated by ';') of other email addresses\n" - "\t at which you receive email.\n" - "\n" - " Notmuch will use the various email addresses configured here when\n" - " formatting replies. It will avoid including your own addresses in the\n" - " recipient list of replies, and will set the From address based on the\n" - " address to which the original email was addressed.\n"; - -static const char maildir_config_comment[] = - " Maildir compatibility configuration\n" - "\n" - " The following option is supported here:\n" - "\n" - "\tsynchronize_flags Valid values are true and false.\n" - "\n" - "\tIf true, then the following maildir flags (in message filenames)\n" - "\twill be synchronized with the corresponding notmuch tags:\n" - "\n" - "\t\tFlag Tag\n" - "\t\t---- -------\n" - "\t\tD draft\n" - "\t\tF flagged\n" - "\t\tP passed\n" - "\t\tR replied\n" - "\t\tS unread (added when 'S' flag is not present)\n" - "\n" - "\tThe \"notmuch new\" command will notice flag changes in filenames\n" - "\tand update tags, while the \"notmuch tag\" and \"notmuch restore\"\n" - "\tcommands will notice tag changes and update flags in filenames\n"; - -static const char search_config_comment[] = - " Search configuration\n" - "\n" - " The following option is supported here:\n" - "\n" - "\texclude_tags\n" - "\t\tA ;-separated list of tags that will be excluded from\n" - "\t\tsearch results by default. Using an excluded tag in a\n" - "\t\tquery will override that exclusion.\n"; - -static const char crypto_config_comment[] = - " Cryptography related configuration\n" - "\n" - " The following old option is now ignored:\n" - "\n" - "\tgpgpath\n" - "\t\tThis option was used by older builds of notmuch to choose\n" - "\t\tthe version of gpg to use.\n" - "\t\tSetting $PATH is a better approach.\n"; +struct config_group { + const char *group_name; + const char *comment; +} group_comment_table [] = { + { + "database", + " Database configuration\n" + "\n" + " The only value supported here is 'path' which should be the top-level\n" + " directory where your mail currently exists and to where mail will be\n" + " delivered in the future. Files should be individual email messages.\n" + " Notmuch will store its database within a sub-directory of the path\n" + " configured here named \".notmuch\".\n" + }, + { + "user", + " User configuration\n" + "\n" + " Here is where you can let notmuch know how you would like to be\n" + " addressed. Valid settings are\n" + "\n" + "\tname Your full name.\n" + "\tprimary_email Your primary email address.\n" + "\tother_email A list (separated by ';') of other email addresses\n" + "\t at which you receive email.\n" + "\n" + " Notmuch will use the various email addresses configured here when\n" + " formatting replies. It will avoid including your own addresses in the\n" + " recipient list of replies, and will set the From address based on the\n" + " address to which the original email was addressed.\n" + }, + { + "new", + " Configuration for \"notmuch new\"\n" + "\n" + " The following options are supported here:\n" + "\n" + "\ttags A list (separated by ';') of the tags that will be\n" + "\t added to all messages incorporated by \"notmuch new\".\n" + "\n" + "\tignore A list (separated by ';') of file and directory names\n" + "\t that will not be searched for messages by \"notmuch new\".\n" + "\n" + "\t NOTE: *Every* file/directory that goes by one of those\n" + "\t names will be ignored, independent of its depth/location\n" + "\t in the mail store.\n" + }, + { + "search", + " Search configuration\n" + "\n" + " The following option is supported here:\n" + "\n" + "\texclude_tags\n" + "\t\tA ;-separated list of tags that will be excluded from\n" + "\t\tsearch results by default. Using an excluded tag in a\n" + "\t\tquery will override that exclusion.\n" + }, + { + "maildir", + " Maildir compatibility configuration\n" + "\n" + " The following option is supported here:\n" + "\n" + "\tsynchronize_flags Valid values are true and false.\n" + "\n" + "\tIf true, then the following maildir flags (in message filenames)\n" + "\twill be synchronized with the corresponding notmuch tags:\n" + "\n" + "\t\tFlag Tag\n" + "\t\t---- -------\n" + "\t\tD draft\n" + "\t\tF flagged\n" + "\t\tP passed\n" + "\t\tR replied\n" + "\t\tS unread (added when 'S' flag is not present)\n" + "\n" + "\tThe \"notmuch new\" command will notice flag changes in filenames\n" + "\tand update tags, while the \"notmuch tag\" and \"notmuch restore\"\n" + "\tcommands will notice tag changes and update flags in filenames\n" + }, +}; struct _notmuch_config { char *filename; @@ -141,69 +142,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,21 +260,13 @@ 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; - int file_had_user_group; - int file_had_maildir_group; - 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,133 +298,20 @@ 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"); - file_had_new_group = g_key_file_has_group (config->key_file, "new"); - file_had_user_group = g_key_file_has_group (config->key_file, "user"); - file_had_maildir_group = g_key_file_has_group (config->key_file, "maildir"); - 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); - if (! file_had_database_group) - g_key_file_set_comment (config->key_file, "database", NULL, - database_config_comment, NULL); - - if (! file_had_new_group) - g_key_file_set_comment (config->key_file, "new", NULL, - new_config_comment, NULL); - - if (! file_had_user_group) - g_key_file_set_comment (config->key_file, "user", NULL, - user_config_comment, NULL); - - if (! file_had_maildir_group) - g_key_file_set_comment (config->key_file, "maildir", NULL, - maildir_config_comment, NULL); - - if (! file_had_search_group) - g_key_file_set_comment (config->key_file, "search", NULL, - search_config_comment, NULL); - - if (! file_had_crypto_group) - g_key_file_set_comment (config->key_file, "crypto", NULL, - crypto_config_comment, NULL); - + for (size_t i=0; i < ARRAY_SIZE (group_comment_table); i++) { + const char *name = group_comment_table[i].group_name; + if (! g_key_file_has_group (config->key_file, name)) { + /* Force group to exist before adding comment */ + g_key_file_set_value (config->key_file, name, "dummy_key", "dummy_val"); + g_key_file_remove_key (config->key_file, name, "dummy_key", NULL); + g_key_file_set_comment (config->key_file, name, NULL, + group_comment_table[i].comment, NULL); + } + } return config; } diff --git a/notmuch.c b/notmuch.c index 5e2cbdc8..6ff6473e 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 ef615152..65cb189b 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 292778d5..51dd29c8 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 diff --git a/test/setup.expected-output/config-with-comments b/test/setup.expected-output/config-with-comments index d9f796f2..56c628e5 100644 --- a/test/setup.expected-output/config-with-comments +++ b/test/setup.expected-output/config-with-comments @@ -49,7 +49,6 @@ other_email=another.suite@example.com; # [new] tags=foo;bar; -ignore= # Search configuration # @@ -85,4 +84,3 @@ exclude_tags=baz; # commands will notice tag changes and update flags in filenames # [maildir] -synchronize_flags=true -- 2.30.1