3
0
mirror of https://github.com/pragma-/pbot.git synced 2024-12-27 13:12:42 +01:00

Improved grab command to accept regex for history

This commit is contained in:
Pragmatic Software 2012-08-01 09:57:43 +00:00
parent 52b23418e2
commit 876d9fb5c6
4 changed files with 46 additions and 14 deletions

View File

@ -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;

View File

@ -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} // '!';

View File

@ -161,7 +161,7 @@ sub grab_quotegrab {
}
if(not defined $arguments) {
return "Usage: !grab <nick> [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 <nick> [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";
}

View File

@ -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;