aboutsummaryrefslogtreecommitdiff
path: root/idalius.pl
diff options
context:
space:
mode:
authorDavid Phillips <david@sighup.nz>2018-09-16 23:42:35 +1200
committerDavid Phillips <david@sighup.nz>2018-09-17 00:01:48 +1200
commit5331e8ef6cdb9163e3ec6ee85491b7b286f4cbdc (patch)
tree034e3ffcd6f0f450ba2d6ee51ba54ac3feced716 /idalius.pl
parent0352e7d89441abc913d69ce6cff551872edc1fe9 (diff)
downloadidalius-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 'idalius.pl')
-rwxr-xr-xidalius.pl64
1 files changed, 33 insertions, 31 deletions
diff --git a/idalius.pl b/idalius.pl
index dc90f53..c99f798 100755
--- a/idalius.pl
+++ b/idalius.pl
@@ -11,7 +11,7 @@ use IdaliusConfig;
use IRC::Utils qw(strip_color strip_formatting);
my $config_file = "bot.conf";
-my %config = IdaliusConfig::parse_config($config_file);
+my $config = IdaliusConfig::parse_config($config_file);
my %laststrike = ();
my $ping_delay = 300;
my %commands = ();
@@ -23,42 +23,44 @@ sub log_info {
}
eval {
- for my $module (@{$config{plugins}}) {
+ for my $module (@{$config->{_}->{plugins}}) {
log_info "Loading $module";
(my $path = $module) =~ s,::,/,g;
require $path . ".pm";
- $module->configure(\&register_command, \%config, \&run_command);
+ $module->configure(
+ \&register_command,
+ \&run_command,
+ $config->{$module},
+ $config->{_});
}
1;
} or do {
log_info "Error: failed to load module: $@";
+ die;
};
$| = 1;
-my $current_nick = $config{nick};
-
-# Hack: coerce into numeric type
-+$config{url_len};
+my $current_nick = $config->{_}->{nick};
# New PoCo-IRC object
my $irc = POE::Component::IRC->spawn(
- UseSSL => $config{usessl},
- SSLCert => $config{sslcert},
- SSLKey => $config{sslkey},
- nick => $config{nick},
- ircname => $config{ircname},
- port => $config{port},
- server => $config{server},
- username => $config{username},
+ UseSSL => $config->{_}->{usessl},
+ SSLCert => $config->{_}->{sslcert},
+ SSLKey => $config->{_}->{sslkey},
+ nick => $config->{_}->{nick},
+ ircname => $config->{_}->{ircname},
+ port => $config->{_}->{port},
+ server => $config->{_}->{server},
+ username => $config->{_}->{username},
) or die "Failed to create new PoCo-IRC: $!";
# Plugins
-$config{password} and $irc->plugin_add(
+$config->{_}->{password} and $irc->plugin_add(
'NickServID',
POE::Component::IRC::Plugin::NickServID->new(
- Password => $config{password}
+ Password => $config->{_}->{password}
));
POE::Session->create(
@@ -88,7 +90,7 @@ $poe_kernel->run();
sub module_is_enabled {
my $module = $_[0];
- return grep {$_ eq $module} @{$config{plugins}};
+ return grep {$_ eq $module} @{$config->{_}->{plugins}};
}
# Register a command name to a certain sub
@@ -127,8 +129,8 @@ sub custom_ping {
}
sub drop_priv {
- setgid($config{gid}) or die "Failed to setgid: $!\n";
- setuid($config{uid}) or die "Failed to setuid: $!\n";
+ setgid($config->{_}->{gid}) or die "Failed to setgid: $!\n";
+ setuid($config->{_}->{uid}) or die "Failed to setuid: $!\n";
}
# Add a strike against a nick for module flood protection
@@ -147,14 +149,14 @@ sub strike_add {
if ($now - $first <= $strike_period) {
log_info "Ignoring $nick because of command flood";
$irc->yield(privmsg => $channel => "$nick: I'm ignoring you now, you've caused me to talk too much");
- push @{$config{ignore}}, $nick;
+ push @{$config->{_}->{ignore}}, $nick;
}
}
}
sub should_ignore {
my ($nick) = @_;
- return grep {$_ eq $nick} @{$config{ignore}};
+ return grep {$_ eq $nick} @{$config->{_}->{ignore}};
}
sub _start {
@@ -171,8 +173,8 @@ sub irc_001 {
log_info("Connected to server ", $heap->server_name());
- $current_nick = $config{nick};
- $heap->yield(join => $_) for @{$config{channels}};
+ $current_nick = $config->{_}->{nick};
+ $heap->yield(join => $_) for @{$config->{_}->{channels}};
$irc->delay(custom_ping => $ping_delay);
return;
}
@@ -205,8 +207,8 @@ sub handle_common {
my $stripped_what = strip_color(strip_formatting($what));
my $no_prefix_what = $stripped_what;
- if (!should_ignore($nick) && ($config{prefix_nick} && $no_prefix_what =~ s/^\Q$current_nick\E[:,]\s+//g ||
- $no_prefix_what =~ s/^$config{prefix}//)) {
+ if (!should_ignore($nick) && ($config->{_}->{prefix_nick} && $no_prefix_what =~ s/^\Q$current_nick\E[:,]\s+//g ||
+ $no_prefix_what =~ s/^$config->{_}->{prefix}//)) {
$output = run_command($no_prefix_what, $who, $where);
$irc->yield(privmsg => $where => $output) if $output;
strike_add($nick, $channel) if $output;
@@ -215,7 +217,7 @@ sub handle_common {
# handler names are defined as being prefixed with on_
$message_type = "on_$message_type";
my $ignore_suffix = "_yes_really_even_from_ignored_nicks";
- for my $module (@{$config{plugins}}) {
+ for my $module (@{$config->{_}->{plugins}}) {
if (module_is_enabled($module)) {
if (!should_ignore($nick) and $module->can($message_type)) {
# Leave message type unchanged
@@ -258,12 +260,12 @@ sub irc_public {
sub irc_msg {
my ($who, $to, $what, $ided) = @_[ARG0 .. ARG3];
my $nick = (split /!/, $who)[0];
- my $is_admin = grep {$_ eq $who} @{$config{admins}};
+ my $is_admin = grep {$_ eq $who} @{$config->{_}->{admins}};
# reject ignored nicks who aren't also admins (prevent lockout)
return if should_ignore($nick) and not $is_admin;
- if ($config{must_id} && $ided != 1) {
+ if ($config->{_}->{must_id} && $ided != 1) {
$irc->yield(privmsg => $nick => "You must identify with services");
return;
}
@@ -278,12 +280,12 @@ sub irc_msg {
sub irc_invite {
my ($who, $where) = @_[ARG0 .. ARG1];
- $irc->yield(join => $where) if (grep {$_ eq $where} @{$config{channels}});
+ $irc->yield(join => $where) if (grep {$_ eq $where} @{$config->{_}->{channels}});
}
sub irc_disconnected {
_default(@_); # Dump the message
- %config = IdaliusConfig::parse_config($config_file);
+ $config = IdaliusConfig::parse_config($config_file);
$irc->yield(connect => { });
}