From 313da0b587cc957f4d12245500410fc5a81e41ff Mon Sep 17 00:00:00 2001 From: Pragmatic Software Date: Tue, 23 Mar 2010 03:09:03 +0000 Subject: [PATCH] Progressing on converting things to objects --- PBot/AntiFlood.pm | 2 +- PBot/FactoidCommands.pm | 29 ++++++++++++++--------------- PBot/FactoidModuleLauncher.pm | 3 +++ PBot/IRCHandlers.pm | 2 +- PBot/IgnoreList.pm | 31 +++++++++++++++++++++++++++++++ PBot/IgnoreListCommands.pm | 3 +-- PBot/Interpreter.pm | 4 ++-- 7 files changed, 53 insertions(+), 21 deletions(-) diff --git a/PBot/AntiFlood.pm b/PBot/AntiFlood.pm index 3608112a..604b04a1 100644 --- a/PBot/AntiFlood.pm +++ b/PBot/AntiFlood.pm @@ -102,7 +102,7 @@ sub check_flood { } } else { # private message flood $self->{pbot}->logger->log("$nick msg flood offense ${ $self->message_history }{$nick}{$channel}{offenses} earned $length second ignore\n"); - $self->{pbot}->ignorelist->ignore_user("", "floodcontrol", "", "$nick" . '@' . "$host $channel $length"); + $self->{pbot}->{ignorelistcmds}->ignore_user("", "floodcontrol", "", "", "$nick!$user\@$host $channel $length"); } } } diff --git a/PBot/FactoidCommands.pm b/PBot/FactoidCommands.pm index 3afc5e6f..c5389708 100644 --- a/PBot/FactoidCommands.pm +++ b/PBot/FactoidCommands.pm @@ -69,23 +69,24 @@ sub list { return "/msg $nick Usage: list "; } -=cut move to PBot::AntiFlood somehow if($arguments =~/^messages\s+(.*?)\s+(.*)$/) { my $nick_search = $1; my $channel = $2; - if(not exists $flood_watch{$nick_search}) { + if(not exists ${ $self->{pbot}->antiflood->message_history }{$nick_search}) { return "/msg $nick No messages for $nick_search yet."; } - if(not exists $flood_watch{$nick_search}{$channel}) { + if(not exists ${ $self->{pbot}->antiflood->message_history }{$nick_search}{$channel}) { return "/msg $nick No messages for $nick_search in $channel yet."; } - my @messages = @{ $flood_watch{$nick_search}{$channel}{messages} }; + my @messages = @{ ${ $self->{pbot}->antiflood->message_history }{$nick_search}{$channel}{messages} }; + my $botnick = $self->{pbot}->botnick; for(my $i = 0; $i <= $#messages; $i++) { - $conn->privmsg($nick, "" . ($i + 1) . ": " . $messages[$i]->{msg} . "\n") unless $nick =~ /\Q$botnick\E/i; + $self->{pbot}->logger->log("" . ($i + 1) . ") " . localtime($messages[$i]->{timestamp}) . " <$nick_search> " . $messages[$i]->{msg} . "\n"); + $self->{pbot}->conn->privmsg($nick, "" . ($i + 1) . ") " . localtime($messages[$i]->{timestamp}) . " <$nick_search> " . $messages[$i]->{msg} . "\n") unless $nick =~ /\Q$botnick\E/i; } return ""; } @@ -101,29 +102,27 @@ sub list { } if($arguments =~ /^commands$/i) { - $text = "Internal commands: "; - foreach my $command (sort keys %internal_commands) { - $text .= "$command "; - $text .= "($internal_commands{$command}{level}) " - if $internal_commands{$command}{level} > 0; + $text = "Registered commands: "; + foreach my $command (sort { $a->{name} cmp $b->{name} } @{ $self->{pbot}->commands->{handlers} }) { + $text .= "$command->{name} "; + $text .= "($command->{level}) " if $command->{level} > 0; } return $text; } if($arguments =~ /^factoids$/i) { - return "For a list of factoids see http://blackshell.com/~msmud/candide/factoids.html"; + return "For a list of factoids see " . $self->{pbot}->factoids->export_site; } if($arguments =~ /^admins$/i) { $text = "Admins: "; - foreach my $admin (sort { $admins{$b}{level} <=> $admins{$a}{level} } keys %admins) { - $text .= "*" if exists $admins{$admin}{login}; - $text .= "$admin ($admins{$admin}{level}) "; + foreach my $admin (sort { ${ $self->{pbot}->admins->admins }{$b}{level} <=> ${ $self->{pbot}->admins->admins }{$a}{level} } keys %{ $self->{pbot}->admins->admins }) { + $text .= "*" if exists ${ $self->{pbot}->admins->admins }{$admin}{login}; + $text .= "$admin (" . ${ $self->{pbot}->admins->admins }{$admin}{level} . ") "; } return $text; } return "/msg $nick Usage: list "; -=cut } sub alias { diff --git a/PBot/FactoidModuleLauncher.pm b/PBot/FactoidModuleLauncher.pm index 2b00f157..2f85e258 100644 --- a/PBot/FactoidModuleLauncher.pm +++ b/PBot/FactoidModuleLauncher.pm @@ -82,6 +82,9 @@ sub execute_module { return "/me moans loudly."; # er, didn't execute the module? } # end child block + else { + $self->{child} = 0; + } return ""; # child returns bot command, not parent -- so return blank/no command } diff --git a/PBot/IRCHandlers.pm b/PBot/IRCHandlers.pm index c84dd9ff..9af19a60 100644 --- a/PBot/IRCHandlers.pm +++ b/PBot/IRCHandlers.pm @@ -50,7 +50,7 @@ sub on_disconnect { $conn->connect(); if(not $conn->connected) { sleep(5); - on_disconnect($self, $conn, $event) + on_disconnect($self, $conn, $event); } } diff --git a/PBot/IgnoreList.pm b/PBot/IgnoreList.pm index 0e459a37..235ed83d 100644 --- a/PBot/IgnoreList.pm +++ b/PBot/IgnoreList.pm @@ -35,6 +35,8 @@ sub initialize { $self->{pbot} = $pbot; $self->{ignore_list} = {}; + $self->{ignore_flood_counter} = 0; + $self->{last_timestamp} = gettimeofday; } sub add { @@ -54,10 +56,39 @@ sub remove { sub check_ignore { my $self = shift; my ($nick, $user, $host, $channel) = @_; + my $pbot = $self->{pbot}; $channel = lc $channel; my $hostmask = "$nick!$user\@$host"; + my $now = gettimeofday; + + if(defined $channel) { # do not execute following if text is coming from STDIN ($channel undef) + if($channel =~ /^#/) { + $self->{ignore_flood_counter}++; # TODO: make this per channel, e.g., ${ $self->{ignore_flood_counter} }{$channel}++ + $pbot->logger->log("flood_msg: $self->{ignore_flood_counter}\n"); + } + + if($self->{ignore_flood_counter} > 4) { + $pbot->logger->log("flood_msg exceeded! [$self->{ignore_flood_counter}]\n"); + $self->{pbot}->{ignorelistcmds}->ignore_user("", "floodcontrol", "", "", ".* $channel 300"); + $self->{ignore_flood_counter} = 0; + if($channel =~ /^#/) { + $pbot->conn->me($channel, "has been overwhelmed."); + $pbot->conn->me($channel, "lies down and falls asleep."); + return; + } + } + + if($now - $self->{last_timestamp} >= 15) { + $self->{last_timestamp} = $now; + if($self->{ignore_flood_counter} > 0) { + $pbot->logger->log("flood_msg reset: (was $self->{ignore_flood_counter})\n"); + $self->{ignore_flood_counter} = 0; + } + } + } + foreach my $ignored (keys %{ $self->{ignore_list} }) { foreach my $ignored_channel (keys %{ ${ $self->{ignore_list} }{$ignored} }) { $self->{pbot}->logger->log("check_ignore: comparing '$hostmask' against '$ignored' for channel '$channel'\n"); diff --git a/PBot/IgnoreListCommands.pm b/PBot/IgnoreListCommands.pm index b66b00b6..53db37ee 100644 --- a/PBot/IgnoreListCommands.pm +++ b/PBot/IgnoreListCommands.pm @@ -48,7 +48,6 @@ sub ignore_user { my ($target, $channel, $length) = split /\s+/, $arguments; - if(not defined $target) { return "/msg $nick Usage: ignore host [channel] [timeout]"; } @@ -82,7 +81,7 @@ sub ignore_user { sub unignore_user { my $self = shift; my ($from, $nick, $user, $host, $arguments) = @_; - my ($target, $channel) = split /\s+/, $arguments; + my ($target, $channel) = split /\s+/, $arguments if defined $arguments; if(not defined $target) { return "/msg $nick Usage: unignore host [channel]"; diff --git a/PBot/Interpreter.pm b/PBot/Interpreter.pm index 16cb78ca..646b99d2 100644 --- a/PBot/Interpreter.pm +++ b/PBot/Interpreter.pm @@ -119,8 +119,8 @@ sub process_line { # TODO: move this to FactoidModuleLauncher somehow, completely out of Interpreter! if($pbot->factoids->{factoidmodulelauncher}->{child} != 0) { # if this process is a child, it must die now - #$pbot->logger->log("Terminating module.\n"); - exit; + $pbot->logger->log("Terminating module.\n"); + exit 0; } } }