From bb1808b1ef92af9a14a073f2e14973ac132b7e7e Mon Sep 17 00:00:00 2001 From: David Phillips Date: Tue, 10 Apr 2018 15:06:10 +1200 Subject: Add command modules, map command --- config_file.pm | 1 + idalius.pl | 37 ++++++++++++++++++++++++++++++++++--- plugin/antiflood.pm | 3 ++- plugin/map.pm | 34 ++++++++++++++++++++++++++++++++++ plugin/timezone.pm | 36 ++++++++++++++++++------------------ plugin/tittilate.pm | 3 ++- plugin/url_title.pm | 3 ++- 7 files changed, 93 insertions(+), 24 deletions(-) create mode 100644 plugin/map.pm diff --git a/config_file.pm b/config_file.pm index 5ee51d8..304338f 100644 --- a/config_file.pm +++ b/config_file.pm @@ -22,6 +22,7 @@ sub parse_config 'group', 'url_on', 'url_len', + 'prefix', 'antiflood_on'); my @list_configs = ( 'channels', diff --git a/idalius.pl b/idalius.pl index 077e5ba..0e01e32 100755 --- a/idalius.pl +++ b/idalius.pl @@ -16,6 +16,8 @@ my %config = config_file::parse_config($config_file); my %laststrike = (); my $ping_delay = 300; +my %commands = (); + $| = 1; my $current_nick = $config{nick}; @@ -24,7 +26,7 @@ my $current_nick = $config{nick}; +$config{url_on}; +$config{url_len}; -my @plugin_list = plugins("dummy", \%config); +my @plugin_list = plugins("dummy", \®ister_command, \%config, \&run_command); # New PoCo-IRC object my $irc = POE::Component::IRC->spawn( @@ -67,6 +69,26 @@ drop_priv(); $poe_kernel->run(); + +# Register a command name to a certain sub +sub register_command { + my ($command, $action) = @_; + print ("registering $command to $action\n"); + $commands{$command} = $action; +} + +sub run_command { + my ($command_string, $who, $where) = @_; + my @arguments; + my ($command, $rest) = split /\s+/, $command_string, 2; + @arguments = split /\s+/, $rest if $rest; + if ($commands{$command}) { + return ($commands{$command})->(\&log_info, $who, $where, $rest, @arguments); + } else { + return "No such command \"$command\""; + } +} + sub custom_ping { my ($irc, $heap) = @_[KERNEL, HEAP]; $irc->yield(userhost => $current_nick); @@ -149,15 +171,24 @@ sub irc_public { my ($sender, $who, $where, $what) = @_[SENDER, ARG0 .. ARG2]; my $nick = ( split /!/, $who )[0]; my $channel = $where->[0]; + my $output; log_info("[$channel] $who: $what"); # reject ignored nicks first return if (grep {$_ eq $nick} @{$config{ignore}}); + my $stripped_what = strip_color(strip_formatting($what)); + if ($stripped_what =~ s/^$config{prefix}//) { + $output = run_command($stripped_what, $who, $where); + $irc->yield(privmsg => $where => $output) if $output; + } + for my $module (@plugin_list) { - my $stripped_what = strip_color(strip_formatting($what)); - my $output = $module->message(\&log_info, $irc->nick_name, $who, $where, $what, $stripped_what, $irc); + $output = ""; + if ($module->can("message")) { + $output = $module->message(\&log_info, $irc->nick_name, $who, $where, $what, $stripped_what, $irc); + } strike_add $nick if $output; $irc->yield(privmsg => $where => $output) if $output; } diff --git a/plugin/antiflood.pm b/plugin/antiflood.pm index 7eb5b7f..a44c07c 100644 --- a/plugin/antiflood.pm +++ b/plugin/antiflood.pm @@ -14,7 +14,8 @@ my %lastmsg = (); sub configure { my $self = $_[0]; - my $cref = $_[1]; + my $cmdref = $_[1]; + my $cref = $_[2]; %config = %$cref; return $self; } diff --git a/plugin/map.pm b/plugin/map.pm new file mode 100644 index 0000000..f8cb256 --- /dev/null +++ b/plugin/map.pm @@ -0,0 +1,34 @@ +#!/usr/bin/env perl + +package plugin::map; + +use strict; +use warnings; + +my %config; +my $run_command; + + +sub configure { + my $self = shift; + my $cmdref = shift; + my $cref = shift; + %config = %$cref; + $run_command = shift; + + $cmdref->("map", sub { $self->map(@_); } ); + + return $self; +} + +sub map { + my ($self, $logger, $who, $where, $rest, @arguments) = @_; + my ($command, $subjects) = ($rest =~ /^(.+?)\s+(.*)$/); + + return "[]" unless $subjects; + + my @array = map { $run_command->("$command $_", $who, $where) } (split /,/, $subjects); + + return "[" . (join ", ", @array). "]"; +} +1; diff --git a/plugin/timezone.pm b/plugin/timezone.pm index ecbbd73..8807d54 100644 --- a/plugin/timezone.pm +++ b/plugin/timezone.pm @@ -11,30 +11,30 @@ my %config; sub configure { my $self = $_[0]; - my $cref = $_[1]; + my $cmdref = $_[1]; + my $cref = $_[2]; %config = %$cref; + + $cmdref->("time", sub { $self->time(@_); } ); + return $self; } -sub message { - my ($self, $logger, $me, $who, $where, $raw_what, $what, $irc) = @_; - - my $requester = ( split /!/, $who )[0]; +sub time { + my ($self, $logger, $who, $where, $rest, @arguments) = @_; + my $requester = ( split /!/, $who)[0]; my @known_zones = (keys %{$config{timezone}}); - if ($what =~ /^%time\s/) { - if ($what =~ /^%time\s+(.+?)\s*$/) { - my $nick = $1; - if (grep {$_ eq $nick} @known_zones) { - my $d = DateTime->now(); - $d->set_time_zone($config{timezone}->{$nick}); - return "$requester: $nick\'s clock reads $d"; - } else { - return "$requester: I don't know what timezone $nick is in"; - } - } else { - return "$requester: Syntax: %time [nick]"; - } + + return "Syntax: time [nick]" unless @arguments == 1; + + my $nick = $arguments[0]; + if (grep {$_ eq $nick} @known_zones) { + my $d = DateTime->now(); + $d->set_time_zone($config{timezone}->{$nick}); + return "$requester: $nick\'s clock reads $d"; + } else { + return "$requester: I don't know what timezone $nick is in"; } } 1; diff --git a/plugin/tittilate.pm b/plugin/tittilate.pm index 7a1ecd5..4df6b07 100644 --- a/plugin/tittilate.pm +++ b/plugin/tittilate.pm @@ -9,7 +9,8 @@ my %config; sub configure { my $self = $_[0]; - my $cref = $_[1]; + my $cmdref = $_[1]; + my $cref = $_[2]; %config = %$cref; return $self; } diff --git a/plugin/url_title.pm b/plugin/url_title.pm index 9d96410..32995fd 100644 --- a/plugin/url_title.pm +++ b/plugin/url_title.pm @@ -12,7 +12,8 @@ my %config; sub configure { my $self = $_[0]; - my $cref = $_[1]; + my $cmdref = $_[1]; + my $cref = $_[2]; %config = %$cref; return $self; } -- cgit v1.1