From c2a2debf3eeed0efc90e8352a5da3a29881273a1 Mon Sep 17 00:00:00 2001 From: Pragmatic Software Date: Sun, 4 Dec 2016 22:00:50 -0800 Subject: [PATCH] Nick-floods are now once again detected --- PBot/AntiFlood.pm | 14 +++++----- PBot/MessageHistory_SQLite.pm | 49 ++++++++++++++++++++++++++++++----- 2 files changed, 49 insertions(+), 14 deletions(-) diff --git a/PBot/AntiFlood.pm b/PBot/AntiFlood.pm index d0218180..f9b72185 100644 --- a/PBot/AntiFlood.pm +++ b/PBot/AntiFlood.pm @@ -373,8 +373,7 @@ sub check_flood { my $channels; if($mode == $self->{pbot}->{messagehistory}->{MSG_NICKCHANGE}) { - $channels = $self->{pbot}->{nicklist}->get_channels($nick); - $self->{pbot}->{logger}->log("Nick changes for $ancestor: $self->{nickflood}->{$ancestor}->{changes}\n"); + $channels = $self->{pbot}->{nicklist}->get_channels($oldnick); } else { $self->update_join_watch($account, $channel, $text, $mode); push @$channels, $channel; @@ -476,11 +475,12 @@ sub check_flood { return; } - my $last = $self->{pbot}->{messagehistory}->{database}->recall_message_by_count($mode == $self->{pbot}->{messagehistory}->{MSG_NICKCHANGE} ? $ancestor : $account, $channel, 0); - - #$self->{pbot}->{logger}->log(" msg: [$msg->{timestamp}] $msg->{msg}\n"); - #$self->{pbot}->{logger}->log("last: [$last->{timestamp}] $last->{msg}\n"); - #$self->{pbot}->{logger}->log("Comparing message timestamps $last->{timestamp} - $msg->{timestamp} = " . ($last->{timestamp} - $msg->{timestamp}) . " against max_time $max_time\n"); + my $last; + if ($mode == $self->{pbot}->{messagehistory}->{MSG_NICKCHANGE}) { + $last = $self->{pbot}->{messagehistory}->{database}->recall_message_by_count($ancestor, $channel, 0, undef, $nick); + } else { + $last = $self->{pbot}->{messagehistory}->{database}->recall_message_by_count($account, $channel, 0); + } if ($last->{timestamp} - $msg->{timestamp} <= $max_time) { if($mode == $self->{pbot}->{messagehistory}->{MSG_JOIN}) { diff --git a/PBot/MessageHistory_SQLite.pm b/PBot/MessageHistory_SQLite.pm index 7ed8ae34..62e2e1e3 100644 --- a/PBot/MessageHistory_SQLite.pm +++ b/PBot/MessageHistory_SQLite.pm @@ -944,18 +944,53 @@ sub get_message_context { } sub recall_message_by_count { - my ($self, $id, $channel, $count, $ignore_command) = @_; + my ($self, $id, $channel, $count, $ignore_command, $use_aliases) = @_; my $messages; if(defined $id) { $messages = eval { - my $sth = $self->{dbh}->prepare('SELECT id, msg, mode, timestamp, channel FROM Messages WHERE id = ? AND channel = ? ORDER BY timestamp DESC LIMIT 10 OFFSET ?'); - $sth->bind_param(1, $id); - $sth->bind_param(2, $channel); - $sth->bind_param(3, $count); - $sth->execute(); - return $sth->fetchall_arrayref({}); + if (defined $use_aliases) { + my %akas = $self->get_also_known_as($use_aliases); + my %seen_id; + my $ids; + my $or = ''; + foreach my $aka (keys %akas) { + next if $akas{$aka}->{type} == $self->{alias_type}->{WEAK}; + next if $akas{$aka}->{nickchange} == 1; + next if exists $seen_id{$akas{$aka}->{id}}; + $seen_id{$akas{$aka}->{id}} = 1; + + $ids .= "${or}id = ?"; + $or = ' OR '; + } + + my $sql = "SELECT id, msg, mode, timestamp, channel FROM Messages WHERE ($ids) AND channel = ? ORDER BY timestamp DESC LIMIT 10 OFFSET ?"; + my $sth = $self->{dbh}->prepare($sql); + + my $param = 1; + %seen_id = (); + foreach my $aka (keys %akas) { + next if $akas{$aka}->{type} == $self->{alias_type}->{WEAK}; + next if $akas{$aka}->{nickchange} == 1; + next if exists $seen_id{$akas{$aka}->{id}}; + $seen_id{$akas{$aka}->{id}} = 1; + + $sth->bind_param($param++, $akas{$aka}->{id}); + } + + $sth->bind_param($param++, $channel); + $sth->bind_param($param++, $count); + $sth->execute(); + return $sth->fetchall_arrayref({}); + } else { + my $sth = $self->{dbh}->prepare('SELECT id, msg, mode, timestamp, channel FROM Messages WHERE id = ? AND channel = ? ORDER BY timestamp DESC LIMIT 10 OFFSET ?'); + $sth->bind_param(1, $id); + $sth->bind_param(2, $channel); + $sth->bind_param(3, $count); + $sth->execute(); + return $sth->fetchall_arrayref({}); + } }; } else { $messages = eval {