From 876d9fb5c6d2a04e7b7576f01bdfee6ab96b72f3 Mon Sep 17 00:00:00 2001 From: Pragmatic Software Date: Wed, 1 Aug 2012 09:57:43 +0000 Subject: [PATCH] Improved grab command to accept regex for history --- PBot/AntiFlood.pm | 1 + PBot/PBot.pm | 2 +- PBot/Quotegrabs.pm | 53 ++++++++++++++++++++++++++++++++++++---------- PBot/VERSION.pm | 4 ++-- 4 files changed, 46 insertions(+), 14 deletions(-) diff --git a/PBot/AntiFlood.pm b/PBot/AntiFlood.pm index bac20724..5e7c01ec 100644 --- a/PBot/AntiFlood.pm +++ b/PBot/AntiFlood.pm @@ -162,6 +162,7 @@ sub add_message { #$self->{pbot}->logger->log("appending new message\n"); push(@{ $self->message_history->{$account}->{channels}->{$channel}{messages} }, { timestamp => $now, msg => $text, mode => $mode }); + $self->message_history->{$account}->{channels}->{$channel}{last_spoken} = $now; my $length = $#{ $self->message_history->{$account}->{channels}->{$channel}{messages} } + 1; diff --git a/PBot/PBot.pm b/PBot/PBot.pm index 903427fa..31f3a327 100644 --- a/PBot/PBot.pm +++ b/PBot/PBot.pm @@ -81,7 +81,7 @@ sub initialize { $self->{max_msg_len} = delete $conf{max_msg_len} // 430; $self->{MAX_FLOOD_MESSAGES} = delete $conf{MAX_FLOOD_MESSAGES} // 4; - $self->{MAX_NICK_MESSAGES} = delete $conf{MAX_NICK_MESSAGES} // 12; + $self->{MAX_NICK_MESSAGES} = delete $conf{MAX_NICK_MESSAGES} // 32; $self->{trigger} = delete $conf{trigger} // '!'; diff --git a/PBot/Quotegrabs.pm b/PBot/Quotegrabs.pm index 300307e7..858ba2fc 100644 --- a/PBot/Quotegrabs.pm +++ b/PBot/Quotegrabs.pm @@ -161,7 +161,7 @@ sub grab_quotegrab { } if(not defined $arguments) { - return "Usage: !grab [history] [channel] -- where [history] is an optional argument that is an integer number of recent messages; e.g., to grab the 3rd most recent message for nick, use !grab nick 3"; + return "Usage: !grab [history] [channel] -- where [history] is an optional argument that is either an integer number of recent messages or a regex (without whitespace) of the text within the message; e.g., to grab the 3rd most recent message for nick, use !grab nick 3; and [channel] is an optional channel, so you can use it from /msg (you will need to also specify [history] in this case)"; } $arguments = lc $arguments; @@ -173,15 +173,19 @@ sub grab_quotegrab { } $channel = $from if not defined $channel; - if($grab_history < 1 || $grab_history > $self->{pbot}->{MAX_NICK_MESSAGES}) { + if($grab_history =~ /^\d+$/ and ($grab_history < 1 || $grab_history > $self->{pbot}->{MAX_NICK_MESSAGES})) { return "/msg $nick Please choose a history between 1 and $self->{pbot}->{MAX_NICK_MESSAGES}"; } my $found_mask = undef; + my $last_spoken = 0; foreach my $mask (keys %{ $self->{pbot}->antiflood->message_history }) { if($mask =~ m/^\Q$grab_nick\E!/i) { - $found_mask = $mask; - last; + if(defined $self->{pbot}->antiflood->message_history->{$mask}->{channels}->{$channel}{last_spoken} + and $self->{pbot}->antiflood->message_history->{$mask}->{channels}->{$channel}{last_spoken} > $last_spoken) { + $last_spoken = $self->{pbot}->antiflood->message_history->{$mask}->{channels}->{$channel}{last_spoken}; + $found_mask = $mask; + } } } @@ -195,13 +199,40 @@ sub grab_quotegrab { my @messages = @{ $self->{pbot}->antiflood->message_history->{$found_mask}->{channels}->{$channel}{messages} }; - $grab_history--; - - if($grab_history > $#messages) { - return "$grab_nick has only " . ($#messages + 1) . " messages in the history."; - } + if($grab_history =~ /^\d+$/) { + # integral history + $grab_history--; - $grab_history = $#messages - $grab_history; + if($grab_history > $#messages) { + return "$grab_nick has only " . ($#messages + 1) . " messages in the history."; + } + + $grab_history = $#messages - $grab_history; + } else { + # regex history + my $ret = eval { + my $i = 0; + my $found = 0; + while($i <= $#messages) { + if($messages[$i]->{msg} =~ m/$grab_history/i) { + $grab_history = $i; + $found = 1; + last; + } + $i++; + } + + if($found == 0) { + return "/msg $nick No message containing regex '$grab_history' found for $grab_nick"; + } else { + return undef; + } + }; + return "/msg $nick Bad grab regex: $@" if $@; + if(defined $ret) { + return $ret; + } + } $self->{pbot}->logger->log("$nick ($from) grabbed <$grab_nick/$channel> $messages[$grab_history]->{msg}\n"); @@ -218,7 +249,7 @@ sub grab_quotegrab { $self->save_quotegrabs(); my $msg = $messages[$grab_history]->{msg}; - $msg =~ s/(.{21}).*/$1.../; + #$msg =~ s/(.{21}).*/$1.../; return "Quote grabbed: " . ($#{ $self->{quotegrabs} } + 1) . ": <$grab_nick> $msg"; } diff --git a/PBot/VERSION.pm b/PBot/VERSION.pm index 14e0ebef..eaa5e947 100644 --- a/PBot/VERSION.pm +++ b/PBot/VERSION.pm @@ -13,8 +13,8 @@ use warnings; # These are set automatically by the build/commit script use constant { BUILD_NAME => "PBot", - BUILD_REVISION => 370, - BUILD_DATE => "2012-07-22", + BUILD_REVISION => 371, + BUILD_DATE => "2012-08-01", }; 1;