3
0
mirror of https://github.com/pragma-/pbot.git synced 2024-11-02 10:09:32 +01:00

Add -x/-context <nick> flag to limit recall -before/-after context to that nick

This commit is contained in:
Pragmatic Software 2015-01-23 11:44:38 -08:00
parent 90597350c5
commit 5ab76e6e6c
2 changed files with 46 additions and 13 deletions

View File

@ -123,7 +123,7 @@ sub recall_message {
return ""; return "";
} }
my $usage = 'Usage: recall [nick [history [channel]]] [-c,channel <channel>] [-t,text,h,history <history>] [-b,before <context before>] [-a,after <context after>] [+ ...]'; 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>] [+ ...]';
if(not defined $arguments or not length $arguments) { if(not defined $arguments or not length $arguments) {
return $usage; return $usage;
@ -142,13 +142,14 @@ sub recall_message {
my $recall_text; my $recall_text;
foreach my $recall (@recalls) { foreach my $recall (@recalls) {
my ($recall_nick, $recall_history, $recall_channel, $recall_before, $recall_after); my ($recall_nick, $recall_history, $recall_channel, $recall_before, $recall_after, $recall_context);
my ($ret, $args) = GetOptionsFromString($recall, my ($ret, $args) = GetOptionsFromString($recall,
'channel|c=s' => \$recall_channel, 'channel|c=s' => \$recall_channel,
'text|t|history|h=s' => \$recall_history, 'text|t|history|h=s' => \$recall_history,
'before|b=s' => \$recall_before, 'before|b=s' => \$recall_before,
'after|a=s' => \$recall_after); 'after|a=s' => \$recall_after,
'context|x=s' => \$recall_context);
return "$getopt_error -- $usage" if defined $getopt_error; return "$getopt_error -- $usage" if defined $getopt_error;
@ -184,6 +185,10 @@ sub recall_message {
# set channel to current channel if not specified # set channel to current channel if not specified
$recall_channel = $from if not defined $recall_channel; $recall_channel = $from if not defined $recall_channel;
if (not defined $recall_nick and defined $recall_context) {
$recall_nick = $recall_context;
}
my ($account, $found_nick); my ($account, $found_nick);
if(defined $recall_nick) { if(defined $recall_nick) {
@ -228,7 +233,17 @@ sub recall_message {
return "You may only select 200 lines of surrounding context."; return "You may only select 200 lines of surrounding context.";
} }
my $messages = $self->{database}->get_message_context($message, $recall_before, $recall_after); my $context_account;
if (defined $recall_context) {
($context_account) = $self->{database}->find_message_account_by_nick($recall_context);
if(not defined $context_account) {
return "I don't know anybody named $recall_context.";
}
}
my $messages = $self->{database}->get_message_context($message, $recall_before, $recall_after, $context_account);
foreach my $msg (@$messages) { foreach my $msg (@$messages) {
$self->{pbot}->{logger}->log("$nick ($from) recalled <$msg->{nick}/$msg->{channel}> $msg->{msg}\n"); $self->{pbot}->{logger}->log("$nick ($from) recalled <$msg->{nick}/$msg->{channel}> $msg->{msg}\n");

View File

@ -442,16 +442,25 @@ SQL
} }
sub get_message_context { sub get_message_context {
my ($self, $message, $before, $after) = @_; my ($self, $message, $before, $after, $context_id) = @_;
my ($messages_before, $messages_after); my ($messages_before, $messages_after);
if (defined $before and $before > 0) { if (defined $before and $before > 0) {
$messages_before = eval { $messages_before = eval {
my $sth = $self->{dbh}->prepare('SELECT id, msg, mode, timestamp, channel FROM Messages WHERE channel = ? AND timestamp < ? AND mode != 1 ORDER BY timestamp DESC LIMIT ?'); my $sth;
if (defined $context_id) {
$sth = $self->{dbh}->prepare('SELECT id, msg, mode, timestamp, channel FROM Messages WHERE id = ? AND channel = ? AND timestamp < ? AND mode != 1 ORDER BY timestamp DESC LIMIT ?');
$sth->bind_param(1, $context_id);
$sth->bind_param(2, $message->{channel});
$sth->bind_param(3, $message->{timestamp});
$sth->bind_param(4, $before);
} else {
$sth = $self->{dbh}->prepare('SELECT id, msg, mode, timestamp, channel FROM Messages WHERE channel = ? AND timestamp < ? AND mode != 1 ORDER BY timestamp DESC LIMIT ?');
$sth->bind_param(1, $message->{channel}); $sth->bind_param(1, $message->{channel});
$sth->bind_param(2, $message->{timestamp}); $sth->bind_param(2, $message->{timestamp});
$sth->bind_param(3, $before); $sth->bind_param(3, $before);
}
$sth->execute(); $sth->execute();
return [reverse @{$sth->fetchall_arrayref({})}]; return [reverse @{$sth->fetchall_arrayref({})}];
}; };
@ -460,10 +469,19 @@ sub get_message_context {
if (defined $after and $after > 0) { if (defined $after and $after > 0) {
$messages_after = eval { $messages_after = eval {
my $sth = $self->{dbh}->prepare('SELECT id, msg, mode, timestamp, channel FROM Messages WHERE channel = ? AND timestamp > ? AND mode != 1 LIMIT ?'); my $sth;
if (defined $context_id) {
$sth = $self->{dbh}->prepare('SELECT id, msg, mode, timestamp, channel FROM Messages WHERE id = ? AND channel = ? AND timestamp > ? AND mode != 1 LIMIT ?');
$sth->bind_param(1, $context_id);
$sth->bind_param(2, $message->{channel});
$sth->bind_param(3, $message->{timestamp});
$sth->bind_param(4, $after);
} else {
$sth = $self->{dbh}->prepare('SELECT id, msg, mode, timestamp, channel FROM Messages WHERE channel = ? AND timestamp > ? AND mode != 1 LIMIT ?');
$sth->bind_param(1, $message->{channel}); $sth->bind_param(1, $message->{channel});
$sth->bind_param(2, $message->{timestamp}); $sth->bind_param(2, $message->{timestamp});
$sth->bind_param(3, $after); $sth->bind_param(3, $after);
}
$sth->execute(); $sth->execute();
return $sth->fetchall_arrayref({}); return $sth->fetchall_arrayref({});
}; };