aboutsummaryrefslogtreecommitdiff
path: root/config_file.pm
diff options
context:
space:
mode:
authorDavid Phillips <david@sighup.nz>2017-03-27 15:22:56 +1300
committerDavid Phillips <david@sighup.nz>2017-03-27 15:22:56 +1300
commit753cd376d775fcb27a95bd780a554808c64cd05a (patch)
tree66e54726dab89581447aa1ea5fd9a5c6e7bc02c5 /config_file.pm
parentb0f83d8e448c6ab65c6aff6050e63b2c8c7c2b9d (diff)
downloadidalius-753cd376d775fcb27a95bd780a554808c64cd05a.tar.xz
Rename old saxrobot/saxbot sb_config to more appropriate name
Diffstat (limited to 'config_file.pm')
-rw-r--r--config_file.pm68
1 files changed, 68 insertions, 0 deletions
diff --git a/config_file.pm b/config_file.pm
new file mode 100644
index 0000000..fe23b89
--- /dev/null
+++ b/config_file.pm
@@ -0,0 +1,68 @@
+#!/usr/bin/env perl
+
+package config_file;
+
+use strict;
+use warnings;
+use Config::Tiny;
+
+sub parse_config
+{
+ my @scalar_configs = (
+ 'nick',
+ 'username',
+ 'ircname',
+ 'server',
+ 'port',
+ 'usessl',
+ 'password',
+ 'must_id',
+ 'quit_msg',
+ 'user',
+ 'group');
+ my @list_configs = (
+ 'channels',
+ 'ignore',
+ 'admins');
+ my @optional_configs = (
+ 'password');
+ 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";
+ }
+ $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;
+ }
+
+ $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";
+
+
+ $built_config{triggers} = \%triggers;
+
+ return %built_config;
+}
+1;