3
0
mirror of https://github.com/pragma-/pbot.git synced 2025-01-11 04:22:35 +01:00

Add count option to recall command

This commit is contained in:
Pragmatic Software 2015-06-15 17:58:25 -07:00
parent b4bffe9233
commit 2ae493f1c2
2 changed files with 50 additions and 16 deletions

View File

@ -202,7 +202,7 @@ sub recall_message {
return "";
}
my $usage = 'Usage: recall [nick [history [channel]]] [-c,channel <channel>] [-t,text,h,history <history>] [-b,before <context before>] [-a,after <context after>] [-x,context <nick>] [+ ...]';
my $usage = 'Usage: recall [nick [history [channel]]] [-c,channel <channel>] [-t,text,h,history <history>] [-b,before <context before>] [-a,after <context after>] [-x,context <nick>] [-n,count <count>] [+ ...]';
if(not defined $arguments or not length $arguments) {
return $usage;
@ -221,14 +221,15 @@ sub recall_message {
my $recall_text;
foreach my $recall (@recalls) {
my ($recall_nick, $recall_history, $recall_channel, $recall_before, $recall_after, $recall_context);
my ($recall_nick, $recall_history, $recall_channel, $recall_before, $recall_after, $recall_context, $recall_count);
my ($ret, $args) = GetOptionsFromString($recall,
'channel|c=s' => \$recall_channel,
'text|t|history|h=s' => \$recall_history,
'before|b=s' => \$recall_before,
'after|a=s' => \$recall_after,
'context|x=s' => \$recall_context);
'channel|c:s' => \$recall_channel,
'text|t|history|h:s' => \$recall_history,
'before|b:i' => \$recall_before,
'after|a:i' => \$recall_after,
'count|n:i' => \$recall_count,
'context|x:s' => \$recall_context);
return "$getopt_error -- $usage" if defined $getopt_error;
@ -238,8 +239,17 @@ sub recall_message {
$recall_nick = shift @$args;
$recall_history = shift @$args if not defined $recall_history;
$recall_channel = shift @$args if not defined $recall_channel;
$recall_before = 0 if not defined $recall_before;
$recall_after = 0 if not defined $recall_after;
$recall_count = 1 if $recall_count <= 0;
return "You may only select a count of up to 50 messages." if $recall_count > 50;
if ($recall_before + $recall_after > 200) {
return "You may only select up to 200 lines of surrounding context.";
}
if ($recall_count > 1 and ($recall_before > 0 or $recall_after > 0)) {
return "The `count` and `context before/after` options cannot be used together.";
}
# swap nick and channel if recall nick looks like channel and channel wasn't specified
if(not $channel_arg and $recall_nick =~ m/^#/) {
@ -310,10 +320,6 @@ sub recall_message {
}
}
if ($recall_before + $recall_after > 200) {
return "You may only select 200 lines of surrounding context.";
}
my $context_account;
if (defined $recall_context) {
@ -324,7 +330,7 @@ sub recall_message {
}
}
my $messages = $self->{database}->get_message_context($message, $recall_before, $recall_after, $context_account);
my $messages = $self->{database}->get_message_context($message, $recall_before, $recall_after, $recall_count, $recall_history, $context_account);
foreach my $msg (@$messages) {
$self->{pbot}->{logger}->log("$nick ($from) recalled <$msg->{nick}/$msg->{channel}> $msg->{msg}\n");

View File

@ -496,9 +496,36 @@ SQL
}
sub get_message_context {
my ($self, $message, $before, $after, $context_id) = @_;
my ($self, $message, $before, $after, $count, $text, $context_id) = @_;
my ($messages_before, $messages_after);
my ($messages_before, $messages_after, $messages_count);
if (defined $count and $count > 1) {
$text =~ s/\.?\*\??/%/g;
$text =~ s/\./_/g;
print "got count [$count] and text [$text]\n";
$messages_count = eval {
my $sth;
if (defined $context_id) {
$sth = $self->{dbh}->prepare('SELECT id, msg, mode, timestamp, channel FROM Messages WHERE id = ? AND channel = ? AND msg LIKE ? AND timestamp < ? AND mode = 0 ORDER BY timestamp DESC LIMIT ?');
$sth->bind_param(1, $context_id);
$sth->bind_param(2, $message->{channel});
$sth->bind_param(3, "%$text%");
$sth->bind_param(4, $message->{timestamp});
$sth->bind_param(5, $count);
} else {
$sth = $self->{dbh}->prepare('SELECT id, msg, mode, timestamp, channel FROM Messages WHERE channel = ? AND msg LIKE ? AND timestamp < ? AND mode = 0 ORDER BY timestamp DESC LIMIT ?');
$sth->bind_param(1, $message->{channel});
$sth->bind_param(2, "%$text%");
$sth->bind_param(3, $message->{timestamp});
$sth->bind_param(4, $count);
}
$sth->execute();
return [reverse @{$sth->fetchall_arrayref({})}];
};
$self->{pbot}->{logger}->log($@) if $@;
}
if (defined $before and $before > 0) {
$messages_before = eval {
@ -544,6 +571,7 @@ sub get_message_context {
my @messages;
push(@messages, @$messages_before) if defined $messages_before;
push(@messages, @$messages_count) if defined $messages_count;
push(@messages, $message);
push(@messages, @$messages_after) if defined $messages_after;