aboutsummaryrefslogtreecommitdiff
path: root/IdaliusConfig.pm
diff options
context:
space:
mode:
authorDavid Phillips <david@sighup.nz>2018-09-21 14:10:58 +1200
committerDavid Phillips <david@sighup.nz>2018-09-21 14:13:28 +1200
commitcfa90dc368a1ac3441185c94d287829d3985ef6d (patch)
tree7ded14e51cad7f85e5a60ec7fa8abe41d58319cc /IdaliusConfig.pm
parent62555509a4ced02f05f44e658abf86d7f665d63f (diff)
downloadidalius-cfa90dc368a1ac3441185c94d287829d3985ef6d.tar.xz
Validate configuration parameter presence and type
Diffstat (limited to 'IdaliusConfig.pm')
-rw-r--r--IdaliusConfig.pm87
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;