diff options
author | David Phillips <david@sighup.nz> | 2018-09-16 23:42:35 +1200 |
---|---|---|
committer | David Phillips <david@sighup.nz> | 2018-09-17 00:01:48 +1200 |
commit | 5331e8ef6cdb9163e3ec6ee85491b7b286f4cbdc (patch) | |
tree | 034e3ffcd6f0f450ba2d6ee51ba54ac3feced716 /IdaliusConfig.pm | |
parent | 0352e7d89441abc913d69ce6cff551872edc1fe9 (diff) | |
download | idalius-5331e8ef6cdb9163e3ec6ee85491b7b286f4cbdc.tar.xz |
Overhaul config parsing
* makes plugin config more private:
The config file now uses sections denoted with [Plugin::Foo] where plugin-
private config can be stored. Plugins are now passed the usual, as well
as a hashref for their own config section. They are also passed the config
section of the core, i.e. those config options not appearing in an explicit
section. Generally, these are used for bot-global options, so should be
accessible to all plugins, but plugin-specific config shall be hidden
* tries to improve parsing of hash-like strings and arrays
The previous mechanism of using regex to pull out possible tokens was only
ever meant to be temporary, and caused problems with escaping or
encapsulation inside strings. I have made steps on hash parsing to allow
tokens inside strings. Both array and hash parsing still to provide an
escape character to escape the item separator (,)
Diffstat (limited to 'IdaliusConfig.pm')
-rw-r--r-- | IdaliusConfig.pm | 70 |
1 files changed, 30 insertions, 40 deletions
diff --git a/IdaliusConfig.pm b/IdaliusConfig.pm index 0180569..5827209 100644 --- a/IdaliusConfig.pm +++ b/IdaliusConfig.pm @@ -4,8 +4,11 @@ use strict; use warnings; use Config::Tiny; -sub parse_config +use ListParser; + +sub check_config { + # FIXME to do: check that passed config is sane for core config vars my @scalar_configs = ( 'nick', 'username', @@ -30,54 +33,41 @@ sub parse_config 'plugins'); my @optional_configs = ( 'password'); + +} + +sub parse_config +{ my $file = $_[0]; my %built_config; my $config = Config::Tiny->read($file); - # FIXME catch undefined/missing config options - foreach my $option (@scalar_configs) { - my $value = $config->{_}->{$option}; - if (! defined $value && ! grep {$_ eq $option} @optional_configs) { - die "Option \"$option\" must be set in $file\n"; + foreach my $section (keys %{$config}) { + foreach my $opt (keys %{$config->{$section}}) { + # Detect list or hash config option + my $c = substr $config->{$section}->{$opt}, 0, 1; + if ($c eq "[") { + my ($error, @listified) = ListParser::parse_list($config->{$section}->{$opt}, 0); + die $error if $error; + $config->{$section}->{$opt} = \@listified; + } elsif ($c eq "{") { + my ($error, %hashified) = ListParser::parse_list($config->{$section}->{$opt}, 1); + die $error if $error; + $config->{$section}->{$opt} = \%hashified; + } } - $built_config{$option} = $config->{_}->{$option}; - } - - foreach my $option (@list_configs) { - my $vals = $config->{_}->{$option}; - $vals =~ s/^\s+|\s+$//g; - @built_config{$option} = [split /\s*,\s*/, $vals]; - } - - # special case: triggers hash - my %triggers; - foreach (split ',', $config->{_}->{triggers}) { - my ($match, $response) = split /=>/; - # strip outer quotes - $match =~ s/^[^']*'|'[^']*$//g; - $response =~ s/^[^']*'|'[^']*$//g; - $triggers{$match} = $response; - } - - # special case: timezones hash - my %timezone; - foreach (split ',', $config->{_}->{timezone}) { - my ($who, $tz) = split /=>/; - # strip outer quotes - $who =~ s/^[^']*'|'[^']*$//g; - $tz =~ s/^[^']*'|'[^']*$//g; - $timezone{$who} = $tz; } - $built_config{uid} = getpwnam($built_config{user}) - or die "Cannot get uid of $built_config{user}: $!\n"; - $built_config{gid} = getgrnam($built_config{group}) - or die "Cannot get gid of $built_config{group}: $!\n"; +# my ($error, @tmp) = ListParser::parse_list($config->{_}->{plugins}); +# $config->{_}->{plugins} = \@tmp; - $built_config{triggers} = \%triggers; - $built_config{timezone} = \%timezone; + # Special case + $config->{_}->{uid} = getpwnam($config->{_}->{user}) + or die "Cannot get uid of $config->{_}->{user}: $!\n"; + $config->{_}->{gid} = getgrnam($config->{_}->{group}) + or die "Cannot get gid of $config->{_}->{group}: $!\n"; - return %built_config; + return $config; } 1; |