2014-11-15 02:18:33 +01:00
|
|
|
# File: NickList.pm
|
|
|
|
#
|
|
|
|
# Purpose: Maintains lists of nicks currently present in channels.
|
2019-06-26 18:34:19 +02:00
|
|
|
# Used to retrieve list of channels a nick is present in or to
|
2014-11-15 02:18:33 +01:00
|
|
|
# determine if a nick is present in a channel.
|
|
|
|
|
2021-07-11 00:00:22 +02:00
|
|
|
# SPDX-FileCopyrightText: 2021 Pragmatic Software <pragma78@gmail.com>
|
|
|
|
# SPDX-License-Identifier: MIT
|
2017-03-05 22:33:31 +01:00
|
|
|
|
2021-07-21 07:44:51 +02:00
|
|
|
package PBot::Core::NickList;
|
|
|
|
use parent 'PBot::Core::Class';
|
2014-11-15 02:18:33 +01:00
|
|
|
|
2021-06-19 06:23:34 +02:00
|
|
|
use PBot::Imports;
|
2019-07-11 03:40:53 +02:00
|
|
|
|
2016-09-25 09:03:37 +02:00
|
|
|
use Text::Levenshtein qw/fastdistance/;
|
|
|
|
use Time::HiRes qw/gettimeofday/;
|
2020-05-18 01:47:48 +02:00
|
|
|
|
2014-11-15 02:18:33 +01:00
|
|
|
sub initialize {
|
2020-02-15 23:38:32 +01:00
|
|
|
my ($self, %conf) = @_;
|
2021-06-05 22:20:03 +02:00
|
|
|
|
2021-08-02 00:53:58 +02:00
|
|
|
# nicklist hashtable
|
2020-02-15 23:38:32 +01:00
|
|
|
$self->{nicklist} = {};
|
2021-06-05 22:20:03 +02:00
|
|
|
|
|
|
|
# nicklist debug registry entry
|
2020-02-15 23:38:32 +01:00
|
|
|
$self->{pbot}->{registry}->add_default('text', 'nicklist', 'debug', '0');
|
2014-11-15 02:18:33 +01:00
|
|
|
}
|
|
|
|
|
2016-09-25 09:03:37 +02:00
|
|
|
sub update_timestamp {
|
2020-02-15 23:38:32 +01:00
|
|
|
my ($self, $channel, $nick) = @_;
|
2021-06-05 22:20:03 +02:00
|
|
|
|
2020-02-15 23:38:32 +01:00
|
|
|
my $orig_nick = $nick;
|
2021-06-05 22:20:03 +02:00
|
|
|
|
2020-02-15 23:38:32 +01:00
|
|
|
$channel = lc $channel;
|
|
|
|
$nick = lc $nick;
|
|
|
|
|
2021-08-02 00:53:58 +02:00
|
|
|
if (not exists $self->{nicklist}->{$channel} or not exists $self->{nicklist}->{$channel}->{$nick}) {
|
|
|
|
$self->add_nick($channel, $orig_nick);
|
2020-02-15 23:38:32 +01:00
|
|
|
}
|
2021-08-02 00:53:58 +02:00
|
|
|
|
|
|
|
$self->{nicklist}->{$channel}->{$nick}->{timestamp} = gettimeofday;
|
2016-09-25 09:03:37 +02:00
|
|
|
}
|
|
|
|
|
2014-11-15 02:18:33 +01:00
|
|
|
sub remove_channel {
|
2020-02-15 23:38:32 +01:00
|
|
|
my ($self, $channel) = @_;
|
|
|
|
delete $self->{nicklist}->{lc $channel};
|
2014-11-15 02:18:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
sub add_nick {
|
2020-02-15 23:38:32 +01:00
|
|
|
my ($self, $channel, $nick) = @_;
|
2021-06-05 22:20:03 +02:00
|
|
|
|
2020-02-15 23:38:32 +01:00
|
|
|
if (not exists $self->{nicklist}->{lc $channel}->{lc $nick}) {
|
2021-08-02 00:53:58 +02:00
|
|
|
if ($self->{pbot}->{registry}->get_value('nicklist', 'debug')) {
|
|
|
|
$self->{pbot}->{logger}->log("Adding nick '$nick' to channel '$channel'\n");
|
|
|
|
}
|
|
|
|
$self->{nicklist}->{lc $channel}->{lc $nick} = { nick => $nick, timestamp => 0, join => 0 };
|
2020-02-15 23:38:32 +01:00
|
|
|
}
|
2014-11-15 02:18:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
sub remove_nick {
|
2020-02-15 23:38:32 +01:00
|
|
|
my ($self, $channel, $nick) = @_;
|
2021-08-02 00:53:58 +02:00
|
|
|
|
|
|
|
if ($self->{pbot}->{registry}->get_value('nicklist', 'debug')) {
|
|
|
|
$self->{pbot}->{logger}->log("Removing nick '$nick' from channel '$channel'\n");
|
|
|
|
}
|
2020-02-15 23:38:32 +01:00
|
|
|
delete $self->{nicklist}->{lc $channel}->{lc $nick};
|
2014-11-15 02:18:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
sub get_channels {
|
2020-02-15 23:38:32 +01:00
|
|
|
my ($self, $nick) = @_;
|
2014-11-15 02:18:33 +01:00
|
|
|
|
2020-02-15 23:38:32 +01:00
|
|
|
$nick = lc $nick;
|
2014-11-15 02:18:33 +01:00
|
|
|
|
2021-06-05 22:20:03 +02:00
|
|
|
my @channels;
|
|
|
|
|
2020-02-15 23:38:32 +01:00
|
|
|
foreach my $channel (keys %{$self->{nicklist}}) {
|
2021-06-05 22:20:03 +02:00
|
|
|
if (exists $self->{nicklist}->{$channel}->{$nick}) {
|
|
|
|
push @channels, $channel;
|
|
|
|
}
|
2014-11-15 02:18:33 +01:00
|
|
|
}
|
2019-06-26 18:34:19 +02:00
|
|
|
|
2020-02-15 23:38:32 +01:00
|
|
|
return \@channels;
|
2014-11-15 02:18:33 +01:00
|
|
|
}
|
|
|
|
|
2020-02-06 19:47:19 +01:00
|
|
|
sub get_nicks {
|
2020-02-15 23:38:32 +01:00
|
|
|
my ($self, $channel) = @_;
|
2021-06-05 22:20:03 +02:00
|
|
|
|
2020-02-15 23:38:32 +01:00
|
|
|
$channel = lc $channel;
|
2021-06-05 22:20:03 +02:00
|
|
|
|
2020-02-15 23:38:32 +01:00
|
|
|
my @nicks;
|
2021-06-05 22:20:03 +02:00
|
|
|
|
2020-02-15 23:38:32 +01:00
|
|
|
return @nicks if not exists $self->{nicklist}->{$channel};
|
2021-06-05 22:20:03 +02:00
|
|
|
|
|
|
|
foreach my $nick (keys %{$self->{nicklist}->{$channel}}) {
|
|
|
|
push @nicks, $self->{nicklist}->{$channel}->{$nick}->{nick};
|
|
|
|
}
|
|
|
|
|
2020-02-15 23:38:32 +01:00
|
|
|
return @nicks;
|
2020-02-06 19:47:19 +01:00
|
|
|
}
|
|
|
|
|
2018-08-03 21:31:45 +02:00
|
|
|
sub set_meta {
|
2020-02-15 23:38:32 +01:00
|
|
|
my ($self, $channel, $nick, $key, $value) = @_;
|
|
|
|
|
|
|
|
$channel = lc $channel;
|
|
|
|
$nick = lc $nick;
|
|
|
|
|
|
|
|
if (not exists $self->{nicklist}->{$channel} or not exists $self->{nicklist}->{$channel}->{$nick}) {
|
|
|
|
if (exists $self->{nicklist}->{$channel} and $nick =~ m/[*?]/) {
|
|
|
|
my $regex = quotemeta $nick;
|
2021-06-05 22:20:03 +02:00
|
|
|
|
2020-02-15 23:38:32 +01:00
|
|
|
$regex =~ s/\\\*/.*?/g;
|
|
|
|
$regex =~ s/\\\?/./g;
|
|
|
|
|
|
|
|
my $found = 0;
|
2021-06-05 22:20:03 +02:00
|
|
|
|
2020-02-15 23:38:32 +01:00
|
|
|
foreach my $n (keys %{$self->{nicklist}->{$channel}}) {
|
|
|
|
if (exists $self->{nicklist}->{$channel}->{$n}->{hostmask} and $self->{nicklist}->{$channel}->{$n}->{hostmask} =~ m/$regex/i) {
|
|
|
|
$self->{nicklist}->{$channel}->{$n}->{$key} = $value;
|
|
|
|
$found++;
|
|
|
|
}
|
|
|
|
}
|
2021-06-05 22:20:03 +02:00
|
|
|
|
2020-02-15 23:38:32 +01:00
|
|
|
return $found;
|
|
|
|
} else {
|
|
|
|
$self->{pbot}->{logger}->log("Nicklist: Attempt to set invalid meta ($key => $value) for $nick in $channel.\n");
|
|
|
|
return 0;
|
2020-01-23 01:50:45 +01:00
|
|
|
}
|
|
|
|
}
|
2018-08-03 21:31:45 +02:00
|
|
|
|
2020-02-15 23:38:32 +01:00
|
|
|
$self->{nicklist}->{$channel}->{$nick}->{$key} = $value;
|
|
|
|
return 1;
|
2018-08-03 21:31:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
sub delete_meta {
|
2020-02-15 23:38:32 +01:00
|
|
|
my ($self, $channel, $nick, $key) = @_;
|
2018-08-03 21:31:45 +02:00
|
|
|
|
2020-02-15 23:38:32 +01:00
|
|
|
$channel = lc $channel;
|
|
|
|
$nick = lc $nick;
|
2018-08-03 21:31:45 +02:00
|
|
|
|
2020-02-15 23:38:32 +01:00
|
|
|
if (not exists $self->{nicklist}->{$channel} or not exists $self->{nicklist}->{$channel}->{$nick} or not exists $self->{nicklist}->{$channel}->{$nick}->{$key}) {
|
|
|
|
return undef;
|
|
|
|
}
|
2021-06-05 22:20:03 +02:00
|
|
|
|
2020-02-15 23:38:32 +01:00
|
|
|
return delete $self->{nicklist}->{$channel}->{$nick}->{$key};
|
2018-08-03 21:31:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
sub get_meta {
|
2020-02-15 23:38:32 +01:00
|
|
|
my ($self, $channel, $nick, $key) = @_;
|
2018-08-03 21:31:45 +02:00
|
|
|
|
2020-02-15 23:38:32 +01:00
|
|
|
$channel = lc $channel;
|
|
|
|
$nick = lc $nick;
|
2018-08-03 21:31:45 +02:00
|
|
|
|
2020-02-15 23:38:32 +01:00
|
|
|
if (not exists $self->{nicklist}->{$channel} or not exists $self->{nicklist}->{$channel}->{$nick} or not exists $self->{nicklist}->{$channel}->{$nick}->{$key}) {
|
|
|
|
return undef;
|
|
|
|
}
|
2018-08-03 21:31:45 +02:00
|
|
|
|
2020-02-15 23:38:32 +01:00
|
|
|
return $self->{nicklist}->{$channel}->{$nick}->{$key};
|
2018-08-03 21:31:45 +02:00
|
|
|
}
|
|
|
|
|
2017-08-12 10:28:23 +02:00
|
|
|
sub is_present_any_channel {
|
2020-02-15 23:38:32 +01:00
|
|
|
my ($self, $nick) = @_;
|
2017-08-12 10:28:23 +02:00
|
|
|
|
2020-02-15 23:38:32 +01:00
|
|
|
$nick = lc $nick;
|
2017-08-12 10:28:23 +02:00
|
|
|
|
2020-02-15 23:38:32 +01:00
|
|
|
foreach my $channel (keys %{$self->{nicklist}}) {
|
2021-06-05 22:20:03 +02:00
|
|
|
if (exists $self->{nicklist}->{$channel}->{$nick}) {
|
|
|
|
return $self->{nicklist}->{$channel}->{$nick}->{nick};
|
|
|
|
}
|
2017-08-12 10:28:23 +02:00
|
|
|
}
|
2021-06-05 22:20:03 +02:00
|
|
|
|
2020-02-15 23:38:32 +01:00
|
|
|
return 0;
|
2017-08-12 10:28:23 +02:00
|
|
|
}
|
|
|
|
|
2014-11-15 02:18:33 +01:00
|
|
|
sub is_present {
|
2020-02-15 23:38:32 +01:00
|
|
|
my ($self, $channel, $nick) = @_;
|
2014-11-15 02:18:33 +01:00
|
|
|
|
2020-02-15 23:38:32 +01:00
|
|
|
$channel = lc $channel;
|
|
|
|
$nick = lc $nick;
|
2015-06-08 13:43:00 +02:00
|
|
|
|
2021-06-05 22:20:03 +02:00
|
|
|
if (exists $self->{nicklist}->{$channel} and exists $self->{nicklist}->{$channel}->{$nick}) {
|
|
|
|
return $self->{nicklist}->{$channel}->{$nick}->{nick};
|
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
2014-11-15 02:18:33 +01:00
|
|
|
}
|
|
|
|
|
2016-09-25 09:03:37 +02:00
|
|
|
sub is_present_similar {
|
2021-06-11 23:58:16 +02:00
|
|
|
my ($self, $channel, $nick, $similarity) = @_;
|
2016-09-25 09:03:37 +02:00
|
|
|
|
2020-02-15 23:38:32 +01:00
|
|
|
$channel = lc $channel;
|
|
|
|
$nick = lc $nick;
|
2016-09-25 09:03:37 +02:00
|
|
|
|
2021-06-05 22:20:03 +02:00
|
|
|
return 0 if not exists $self->{nicklist}->{$channel};
|
|
|
|
|
2020-02-15 23:38:32 +01:00
|
|
|
return $self->{nicklist}->{$channel}->{$nick}->{nick} if $self->is_present($channel, $nick);
|
2016-09-25 21:59:03 +02:00
|
|
|
|
2021-06-05 22:20:03 +02:00
|
|
|
if ($nick =~ m/(?:^\$|\s)/) {
|
|
|
|
# not nick-like
|
|
|
|
# TODO: why do we have this check? added log message to find out when/if it happens
|
|
|
|
$self->{pbot}->{logger}->log("NickList::is_present_similiar [$channel] [$nick] is not nick-like?\n");
|
|
|
|
return 0;
|
|
|
|
}
|
2016-09-25 09:03:37 +02:00
|
|
|
|
2021-06-05 22:20:03 +02:00
|
|
|
my $percentage;
|
|
|
|
|
2021-06-11 23:58:16 +02:00
|
|
|
if (defined $similarity) {
|
|
|
|
$percentage = $similarity;
|
2021-06-05 22:20:03 +02:00
|
|
|
} else {
|
|
|
|
$percentage = $self->{pbot}->{registry}->get_value('interpreter', 'nick_similarity') // 0.20;
|
|
|
|
}
|
2017-05-21 08:44:45 +02:00
|
|
|
|
2020-02-15 23:38:32 +01:00
|
|
|
my $now = gettimeofday;
|
2021-06-05 22:20:03 +02:00
|
|
|
|
|
|
|
foreach my $person (sort { $self->{nicklist}->{$channel}->{$b}->{timestamp} <=> $self->{nicklist}->{$channel}->{$a}->{timestamp} } keys %{$self->{nicklist}->{$channel}}) {
|
|
|
|
if ($now - $self->{nicklist}->{$channel}->{$person}->{timestamp} > 3600) {
|
|
|
|
# if it has been 1 hour since this person has last spoken, the similar nick
|
|
|
|
# is probably not intended for them.
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-02-15 23:38:32 +01:00
|
|
|
my $distance = fastdistance($nick, $person);
|
|
|
|
my $length = length $nick > length $person ? length $nick : length $person;
|
2016-09-25 09:03:37 +02:00
|
|
|
|
2021-06-05 22:20:03 +02:00
|
|
|
if ($length != 0 && $distance / $length <= $percentage) {
|
|
|
|
return $self->{nicklist}->{$channel}->{$person}->{nick};
|
|
|
|
}
|
2016-09-25 09:03:37 +02:00
|
|
|
}
|
2021-06-05 22:20:03 +02:00
|
|
|
|
2020-02-15 23:38:32 +01:00
|
|
|
return 0;
|
2016-09-25 09:03:37 +02:00
|
|
|
}
|
|
|
|
|
2015-06-08 13:43:00 +02:00
|
|
|
sub random_nick {
|
2020-02-15 23:38:32 +01:00
|
|
|
my ($self, $channel) = @_;
|
2015-06-08 13:43:00 +02:00
|
|
|
|
2020-02-15 23:38:32 +01:00
|
|
|
$channel = lc $channel;
|
2015-06-08 13:43:00 +02:00
|
|
|
|
2020-02-15 23:38:32 +01:00
|
|
|
if (exists $self->{nicklist}->{$channel}) {
|
|
|
|
my $now = gettimeofday;
|
2021-06-05 22:20:03 +02:00
|
|
|
|
|
|
|
# build list of nicks that have spoken within the last 2 hours
|
2020-02-15 23:38:32 +01:00
|
|
|
my @nicks = grep { $now - $self->{nicklist}->{$channel}->{$_}->{timestamp} < 3600 * 2 } keys %{$self->{nicklist}->{$channel}};
|
2019-06-26 18:34:19 +02:00
|
|
|
|
2021-06-05 22:20:03 +02:00
|
|
|
# pick a random nick from tha list
|
2020-02-15 23:38:32 +01:00
|
|
|
my $nick = $nicks[rand @nicks];
|
2021-06-05 22:20:03 +02:00
|
|
|
|
|
|
|
# return its canonical name
|
2020-02-15 23:38:32 +01:00
|
|
|
return $self->{nicklist}->{$channel}->{$nick}->{nick};
|
|
|
|
} else {
|
|
|
|
return undef;
|
|
|
|
}
|
2015-06-08 13:43:00 +02:00
|
|
|
}
|
|
|
|
|
2014-11-15 02:18:33 +01:00
|
|
|
1;
|