From 2ae493f1c214b9b12f389b8a7671306eb49e2770 Mon Sep 17 00:00:00 2001 From: Pragmatic Software Date: Mon, 15 Jun 2015 17:58:25 -0700 Subject: [PATCH] Add `count` option to `recall` command --- PBot/MessageHistory.pm | 34 ++++++++++++++++++++-------------- PBot/MessageHistory_SQLite.pm | 32 ++++++++++++++++++++++++++++++-- 2 files changed, 50 insertions(+), 16 deletions(-) diff --git a/PBot/MessageHistory.pm b/PBot/MessageHistory.pm index fec59a15..f3e9eab7 100644 --- a/PBot/MessageHistory.pm +++ b/PBot/MessageHistory.pm @@ -202,7 +202,7 @@ sub recall_message { return ""; } - my $usage = 'Usage: recall [nick [history [channel]]] [-c,channel ] [-t,text,h,history ] [-b,before ] [-a,after ] [-x,context ] [+ ...]'; + my $usage = 'Usage: recall [nick [history [channel]]] [-c,channel ] [-t,text,h,history ] [-b,before ] [-a,after ] [-x,context ] [-n,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"); diff --git a/PBot/MessageHistory_SQLite.pm b/PBot/MessageHistory_SQLite.pm index 3fc2b81f..2f575d1e 100644 --- a/PBot/MessageHistory_SQLite.pm +++ b/PBot/MessageHistory_SQLite.pm @@ -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;