diff options
Diffstat (limited to 'IdaliusConfig.pm')
-rw-r--r-- | IdaliusConfig.pm | 87 |
1 files changed, 62 insertions, 25 deletions
diff --git a/IdaliusConfig.pm b/IdaliusConfig.pm index 5827209..4a8b42f 100644 --- a/IdaliusConfig.pm +++ b/IdaliusConfig.pm @@ -6,33 +6,68 @@ use Config::Tiny; use ListParser; +sub config_describe { + my ($plugin, $parm) = @_; + + # Plugin "_" is the root config + return $parm unless $plugin ne "_"; + return "$plugin -> $parm"; +} + +sub assert_scalar { + my ($config, $plugin, $parm) = @_; + my $ref = $config->{$parm}; + my $name = config_describe($plugin, $parm); + + die "Error: Configuration \"$name\" must be scalar" unless + defined $ref + and ref($ref) eq ""; +} + +sub assert_list { + my ($config, $plugin, $parm) = @_; + my $ref = $config->{$parm}; + my $name = config_describe($plugin, $parm); + + die "Error: Configuration \"$name\" must be list" unless + defined $ref + and ref($ref) eq "ARRAY"; +} + +sub assert_dict { + my ($config, $plugin, $parm) = @_; + my $ref = $config->{$parm}; + my $name = config_describe($plugin, $parm); + + die "Error: Configuration \"$name\" must be dictionary" unless + defined $ref + and ref($ref) eq "HASH"; + +} + +# Check presence and/or sanity of config parameters for the bot's core +# I.e. it is up to each module to ensure its own config is there and sane, +# normally in sub configure. sub check_config { - # FIXME to do: check that passed config is sane for core config vars - my @scalar_configs = ( - 'nick', - 'username', - 'ircname', - 'server', - 'port', - 'usessl', - 'sslcert', - 'sslkey', - 'password', - 'must_id', - 'quit_msg', - 'user', - 'group', - 'url_len', - 'prefix_nick', - 'prefix'); - my @list_configs = ( - 'channels', - 'ignore', - 'admins', - 'plugins'); - my @optional_configs = ( - 'password'); + my ($config) = @_; + + # Lists of mandatory config variables + my @scalars = qw/nick username ircname server port usessl sslcert sslkey user group prefix_nick prefix/; + my @lists = qw/plugins channels ignore/; + + foreach my $name (@scalars) { + assert_scalar($config->{_}, "_", $name); + } + + foreach my $name (@lists) { + assert_list($config->{_}, "_", $name); + } + + # Special case: password is optional scalar + if (defined $config->{_}->{password}) { + assert_scalar($config->{_}, "_", "password"); + } } @@ -68,6 +103,8 @@ sub parse_config $config->{_}->{gid} = getgrnam($config->{_}->{group}) or die "Cannot get gid of $config->{_}->{group}: $!\n"; + check_config($config); + return $config; } 1; |