3
0
mirror of https://github.com/pragma-/pbot.git synced 2024-10-03 01:48:38 +02:00

Add support for GECOS

This commit is contained in:
Pragmatic Software 2016-02-10 03:42:42 -08:00
parent 15da1428be
commit ddeef3cc62
5 changed files with 144 additions and 24 deletions

View File

@ -79,6 +79,7 @@ sub initialize {
$self->{pbot}->{commands}->register(sub { return $self->whitelist(@_) }, "whitelist", 10);
$self->{pbot}->{event_dispatcher}->register_handler('irc.whoisaccount', sub { $self->on_whoisaccount(@_) });
$self->{pbot}->{event_dispatcher}->register_handler('irc.whoisuser', sub { $self->on_whoisuser(@_) });
$self->{pbot}->{event_dispatcher}->register_handler('irc.endofwhois', sub { $self->on_endofwhois(@_) });
}
@ -736,23 +737,6 @@ sub check_bans {
next;
}
$self->{pbot}->{logger}->log("anti-flood: [check-bans] checking blacklist for $alias in channel $channel\n") if $debug_checkban >= 5;
if ($self->{pbot}->{blacklist}->check_blacklist($alias, $channel)) {
if($self->whitelisted($channel, $mask, 'user')) {
$self->{pbot}->{logger}->log("anti-flood: [check-bans] $mask [$alias] blacklisted in $channel, but allowed through whitelist\n");
next;
}
my $baninfo = {};
$baninfo->{banmask} = $alias;
$baninfo->{channel} = $channel;
$baninfo->{owner} = 'blacklist';
$baninfo->{when} = 0;
$baninfo->{type} = 'blacklist';
push @$bans, $baninfo;
next;
}
my @nickservs;
if (exists $aliases{$alias}->{nickserv}) {
@ -762,6 +746,34 @@ sub check_bans {
}
foreach my $nickserv (@nickservs) {
my @gecoses;
if (exists $aliases{$alias}->{gecos}) {
@gecoses = split /,/, $aliases{$alias}->{gecos};
} else {
@gecoses = (undef);
}
foreach my $gecos (@gecoses) {
my $tgecos = defined $gecos ? $gecos : "[undefined]";
my $tnickserv = defined $nickserv ? $nickserv : "[undefined]";
$self->{pbot}->{logger}->log("anti-flood: [check-bans] checking blacklist for $alias in channel $channel using gecos '$tgecos' and nickserv '$tnickserv'\n") if $debug_checkban >= 5;
if ($self->{pbot}->{blacklist}->check_blacklist($alias, $channel, $nickserv, $gecos)) {
if($self->whitelisted($channel, $mask, 'user')) {
$self->{pbot}->{logger}->log("anti-flood: [check-bans] $mask [$alias] blacklisted in $channel, but allowed through whitelist\n");
next;
}
my $baninfo = {};
$baninfo->{banmask} = $alias;
$baninfo->{channel} = $channel;
$baninfo->{owner} = 'blacklist';
$baninfo->{when} = 0;
$baninfo->{type} = 'blacklist';
push @$bans, $baninfo;
next;
}
}
$self->{pbot}->{logger}->log("anti-flood: [check-bans] checking for bans in $channel on $alias using nickserv " . (defined $nickserv ? $nickserv : "[undefined]") . "\n") if $debug_checkban >= 2;
my $baninfos = $self->{pbot}->{bantracker}->get_baninfo($alias, $channel, $nickserv);
@ -919,6 +931,21 @@ sub on_endofwhois {
return 0;
}
sub on_whoisuser {
my ($self, $event_type, $event) = @_;
my $nick = $event->{event}->{args}[1];
my $gecos = lc $event->{event}->{args}[5];
my ($id) = $self->{pbot}->{messagehistory}->{database}->find_message_account_by_nick($nick);
if ($self->{pbot}->{registry}->get_value('antiflood', 'debug_checkban')) {
$self->{pbot}->{logger}->log("Got gecos for $nick ($id): '$gecos'\n");
}
$self->{pbot}->{messagehistory}->{database}->update_gecos($id, $gecos, scalar gettimeofday);
}
sub on_whoisaccount {
my ($self, $event_type, $event) = @_;
my $nick = $event->{event}->{args}[1];

View File

@ -128,19 +128,28 @@ sub save_blacklist {
sub check_blacklist {
my $self = shift;
my ($hostmask, $channel) = @_;
my ($hostmask, $channel, $nickserv, $gecos) = @_;
return 0 if not defined $channel;
foreach my $black_channel (keys %{ $self->{blacklist} }) {
foreach my $black_hostmask (keys %{ $self->{blacklist}->{$black_channel} }) {
my $flag = '';
$flag = $1 if $black_hostmask =~ s/^\$(.)://;
my $black_channel_escaped = quotemeta $black_channel;
my $black_hostmask_escaped = quotemeta $black_hostmask;
$black_channel_escaped =~ s/\\(\.|\*)/$1/g;
$black_hostmask_escaped =~ s/\\(\.|\*)/$1/g;
if(($channel =~ /$black_channel_escaped/i) && ($hostmask =~ /$black_hostmask_escaped/i)) {
if ($flag eq 'a' && defined $nickserv && $nickserv =~ /$black_hostmask_escaped/i) {
$self->{pbot}->{logger}->log("$hostmask nickserv $nickserv blacklisted in channel $channel (matches [\$a:$black_hostmask] host and [$black_channel] channel)\n");
return 1;
} elsif ($flag eq 'r' && defined $gecos && $gecos =~ /$black_hostmask_escaped/i) {
$self->{pbot}->{logger}->log("$hostmask GECOS $gecos blacklisted in channel $channel (matches [\$r:$black_hostmask] host and [$black_channel] channel)\n");
return 1;
} elsif ($flag eq '' && $channel =~ /$black_channel_escaped/i && $hostmask =~ /$black_hostmask_escaped/i) {
$self->{pbot}->{logger}->log("$hostmask blacklisted in channel $channel (matches [$black_hostmask] host and [$black_channel] channel)\n");
return 1;
}

View File

@ -130,7 +130,7 @@ sub aka_unlink {
sub list_also_known_as {
my ($self, $from, $nick, $user, $host, $arguments) = @_;
my $usage = "Usage: aka [-h] [-i] [-n] [-r] [-w] <nick>; -h show hostmasks; -i show ids; -n show nickserv accounts; -r show relationships; -w show weak links";
my $usage = "Usage: aka [-h] [-i] [-n] [-g] [-r] [-w] <nick>; -h show hostmasks; -i show ids; -n show nickserv accounts; -g show gecos, -r show relationships; -w show weak links";
if(not length $arguments) {
return $usage;
@ -142,11 +142,12 @@ sub list_also_known_as {
chomp $getopt_error;
};
my ($show_hostmasks, $show_nickserv, $show_id, $show_relationship, $show_weak, $dont_use_aliases_table);
my ($show_hostmasks, $show_gecos, $show_nickserv, $show_id, $show_relationship, $show_weak, $dont_use_aliases_table);
my ($ret, $args) = GetOptionsFromString($arguments,
'h' => \$show_hostmasks,
'n' => \$show_nickserv,
'r' => \$show_relationship,
'g' => \$show_gecos,
'w' => \$show_weak,
'nt' => \$dont_use_aliases_table,
'i' => \$show_id);
@ -175,6 +176,7 @@ sub list_also_known_as {
$result .= "$sep$aka";
}
$result .= " ($akas{$aka}->{nickserv})" if $show_nickserv and exists $akas{$aka}->{nickserv};
$result .= " {$akas{$aka}->{gecos}}" if $show_gecos and exists $akas{$aka}->{gecos};
if ($show_relationship) {
if ($akas{$aka}->{id} == $akas{$aka}->{alias}) {
@ -188,7 +190,7 @@ sub list_also_known_as {
$result .= " [WEAK]" if $akas{$aka}->{type} == $self->{database}->{alias_type}->{WEAK};
if ($show_hostmasks or $show_nickserv or $show_id or $show_relationship) {
if ($show_hostmasks or $show_nickserv or $show_gecos or $show_id or $show_relationship) {
$sep = ",\n";
} else {
$sep = ", ";

View File

@ -99,6 +99,14 @@ CREATE TABLE IF NOT EXISTS Nickserv (
nickserv TEXT,
timestamp NUMERIC
)
SQL
$self->{dbh}->do(<<SQL);
CREATE TABLE IF NOT EXISTS Gecos (
id INTEGER,
gecos TEXT,
timestamp NUMERIC
)
SQL
$self->{dbh}->do(<<SQL);
@ -155,6 +163,19 @@ sub end {
}
}
sub get_gecos {
my ($self, $id) = @_;
my $gecos = eval {
my $sth = $self->{dbh}->prepare('SELECT gecos FROM Gecos WHERE ID = ?');
$sth->bind_param(1, $id);
$sth->execute();
return $sth->fetchall_arrayref();
};
$self->{pbot}->{logger}->log($@) if $@;
return map {$_->[0]} @$gecos;
}
sub get_nickserv_accounts {
my ($self, $id) = @_;
@ -227,6 +248,37 @@ sub update_nickserv_account {
$self->{pbot}->{logger}->log($@) if $@;
}
sub create_gecos {
my ($self, $id, $gecos) = @_;
eval {
my $sth = $self->{dbh}->prepare('INSERT INTO Gecos SELECT ?, ?, 0 WHERE NOT EXISTS (SELECT 1 FROM Gecos WHERE id = ? AND gecos = ?)');
$sth->bind_param(1, $id);
$sth->bind_param(2, $gecos);
$sth->bind_param(3, $id);
$sth->bind_param(4, $gecos);
my $rv = $sth->execute();
$self->{new_entries}++ if $sth->rows;
};
$self->{pbot}->{logger}->log($@) if $@;
}
sub update_gecos {
my ($self, $id, $gecos, $timestamp) = @_;
$self->create_gecos($id, $gecos);
eval {
my $sth = $self->{dbh}->prepare('UPDATE Gecos SET timestamp = ? WHERE id = ? AND gecos = ?');
$sth->bind_param(1, $timestamp);
$sth->bind_param(2, $id);
$sth->bind_param(3, $gecos);
$sth->execute();
$self->{new_entries}++;
};
$self->{pbot}->{logger}->log($@) if $@;
}
sub add_message_account {
my ($self, $mask, $link_id) = @_;
my $id;
@ -427,6 +479,20 @@ sub get_nickserv_accounts_for_hostmask {
return map {$_->[0]} @$nickservs;
}
sub get_gecos_for_hostmask {
my ($self, $hostmask) = @_;
my $gecos = eval {
my $sth = $self->{dbh}->prepare('SELECT gecos FROM Hostmasks, Gecos WHERE gecos.id = hostmasks.id AND hostmasks.hostmask = ?');
$sth->bind_param(1, $hostmask);
$sth->execute();
return $sth->fetchall_arrayref();
};
$self->{pbot}->{logger}->log($@) if $@;
return map {$_->[0]} @$gecos;
}
sub get_hostmasks_for_channel {
my ($self, $channel) = @_;
@ -1100,6 +1166,7 @@ sub get_also_known_as {
my $hostmask_sth = $self->{dbh}->prepare('SELECT hostmask FROM Hostmasks WHERE id = ?');
my $nickserv_sth = $self->{dbh}->prepare('SELECT nickserv FROM Nickserv WHERE id = ?');
my $gecos_sth = $self->{dbh}->prepare('SELECT gecos FROM Gecos WHERE id = ?');
foreach my $id (keys %ids) {
$hostmask_sth->bind_param(1, $id);
@ -1126,6 +1193,22 @@ sub get_also_known_as {
}
}
}
$gecos_sth->bind_param(1, $id);
$gecos_sth->execute();
$rows = $gecos_sth->fetchall_arrayref({});
foreach my $row (@$rows) {
foreach my $aka (keys %akas) {
if ($akas{$aka}->{id} == $id) {
if (exists $akas{$aka}->{gecos}) {
$akas{$aka}->{gecos} .= ",$row->{gecos}";
} else {
$akas{$aka}->{gecos} = $row->{gecos};
}
}
}
}
}
return %akas;

View File

@ -185,8 +185,7 @@ sub connect {
$self->{conn}->add_handler([ 251,252,253,254,255,302 ], sub { $self->{irchandlers}->on_init(@_) });
# ignore these events
$self->{conn}->add_handler(['whoisuser',
'whoisserver',
$self->{conn}->add_handler(['whoisserver',
'whoiscountry',
'whoischannels',
'whoisidle',