2018-08-06 07:41:08 +02:00
|
|
|
# File: AntiSpam.pm
|
|
|
|
# Author: pragma_
|
|
|
|
#
|
|
|
|
# Purpose: Checks if a message is spam
|
|
|
|
|
|
|
|
# This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
# License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
|
|
|
|
|
|
package PBot::AntiSpam;
|
|
|
|
|
|
|
|
use warnings;
|
|
|
|
use strict;
|
|
|
|
|
2019-07-11 03:40:53 +02:00
|
|
|
use feature 'unicode_strings';
|
|
|
|
|
2018-08-06 07:41:08 +02:00
|
|
|
use feature 'switch';
|
|
|
|
no if $] >= 5.018, warnings => "experimental::smartmatch";
|
|
|
|
|
|
|
|
use PBot::DualIndexHashObject;
|
|
|
|
|
|
|
|
use Carp ();
|
|
|
|
use Time::HiRes qw(gettimeofday);
|
|
|
|
use POSIX qw/strftime/;
|
|
|
|
|
|
|
|
sub new {
|
|
|
|
Carp::croak("Options to " . __FILE__ . " should be key/value pairs, not hash reference") if ref($_[1]) eq 'HASH';
|
|
|
|
my ($class, %conf) = @_;
|
|
|
|
my $self = bless {}, $class;
|
|
|
|
$self->initialize(%conf);
|
|
|
|
return $self;
|
|
|
|
}
|
|
|
|
|
|
|
|
sub initialize {
|
|
|
|
my ($self, %conf) = @_;
|
2019-06-26 18:34:19 +02:00
|
|
|
|
2018-08-06 07:41:08 +02:00
|
|
|
$self->{pbot} = delete $conf{pbot} // Carp::croak("Missing pbot reference to " . __FILE__);
|
|
|
|
|
|
|
|
my $filename = delete $conf{spamkeywords_file} // $self->{pbot}->{registry}->get_value('general', 'data_dir') . '/spam_keywords';
|
2019-06-28 09:22:57 +02:00
|
|
|
$self->{keywords} = PBot::DualIndexHashObject->new(name => 'SpamKeywords', filename => $filename, pbot => $self->{pbot});
|
2018-08-06 07:41:08 +02:00
|
|
|
$self->{keywords}->load;
|
|
|
|
|
|
|
|
$self->{pbot}->{registry}->add_default('text', 'antispam', 'enforce', $conf{enforce_antispam} // 1);
|
|
|
|
$self->{pbot}->{commands}->register(sub { return $self->antispam_cmd(@_) }, "antispam", 10);
|
|
|
|
}
|
|
|
|
|
|
|
|
sub is_spam {
|
2018-08-06 20:01:23 +02:00
|
|
|
my ($self, $namespace, $text, $all_namespaces) = @_;
|
2020-01-15 03:10:53 +01:00
|
|
|
my $lc_namespace = lc $namespace;
|
2018-08-06 07:41:08 +02:00
|
|
|
|
|
|
|
return 0 if not $self->{pbot}->{registry}->get_value('antispam', 'enforce');
|
2018-08-06 20:01:23 +02:00
|
|
|
return 0 if $self->{pbot}->{registry}->get_value($namespace, 'dont_enforce_antispam');
|
2018-08-06 07:41:08 +02:00
|
|
|
|
|
|
|
my $ret = eval {
|
2020-01-15 03:10:53 +01:00
|
|
|
foreach my $space (keys %{ $self->{keywords}->{hash} }) {
|
|
|
|
if ($all_namespaces or $lc_namespace eq $space) {
|
|
|
|
foreach my $keyword (keys %{ $self->{keywords}->{hash}->{$space} }) {
|
|
|
|
next if $keyword eq '_name';
|
2018-08-06 20:24:05 +02:00
|
|
|
return 1 if $text =~ m/$keyword/i;
|
|
|
|
}
|
2018-08-06 07:41:08 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
};
|
|
|
|
|
|
|
|
if ($@) {
|
|
|
|
$self->{pbot}->{logger}->log("Error in is_spam: $@");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
$self->{pbot}->{logger}->log("AntiSpam: spam detected!\n") if $ret;
|
|
|
|
return $ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
sub antispam_cmd {
|
2018-08-07 05:24:15 +02:00
|
|
|
my ($self, $from, $nick, $user, $host, $arguments, $stuff) = @_;
|
2018-08-06 07:41:08 +02:00
|
|
|
|
2018-08-09 02:38:57 +02:00
|
|
|
my $arglist = $stuff->{arglist};
|
2018-08-07 05:24:15 +02:00
|
|
|
|
2018-08-09 02:38:57 +02:00
|
|
|
my $command = $self->{pbot}->{interpreter}->shift_arg($arglist);
|
2018-08-06 07:41:08 +02:00
|
|
|
|
|
|
|
return "Usage: antispam <command>, where commands are: list/show, add, remove, set, unset" if not defined $command;
|
|
|
|
|
|
|
|
given ($command) {
|
|
|
|
when ($_ eq "list" or $_ eq "show") {
|
|
|
|
my $text = "Spam keywords:\n";
|
|
|
|
my $entries = 0;
|
2020-01-15 03:10:53 +01:00
|
|
|
foreach my $namespace (keys %{ $self->{keywords}->{hash} }) {
|
|
|
|
$text .= " $self->{keywords}->{hash}->{$namespace}->{_name}:\n";
|
|
|
|
foreach my $keyword (keys %{ $self->{keywords}->{hash}->{$namespace} }) {
|
|
|
|
next if $keyword eq '_name';
|
|
|
|
$text .= " $self->{keywords}->{hash}->{$namespace}->{$keyword}->{_name},\n";
|
2018-08-06 07:41:08 +02:00
|
|
|
$entries++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$text .= "none" if $entries == 0;
|
|
|
|
return $text;
|
|
|
|
}
|
|
|
|
when ("set") {
|
2018-08-09 02:38:57 +02:00
|
|
|
my ($namespace, $keyword, $flag, $value) = $self->{pbot}->{interpreter}->split_args($arglist, 4);
|
2020-01-29 22:35:56 +01:00
|
|
|
return "Usage: antispam set <namespace> <regex> [flag [value]]" if not defined $namespace or not defined $keyword;
|
2018-08-06 07:41:08 +02:00
|
|
|
|
2020-01-15 03:10:53 +01:00
|
|
|
if (not exists $self->{keywords}->{hash}->{lc $namespace}) {
|
2018-08-07 05:24:15 +02:00
|
|
|
return "There is no such namespace `$namespace`.";
|
2018-08-06 07:41:08 +02:00
|
|
|
}
|
|
|
|
|
2020-01-15 03:10:53 +01:00
|
|
|
if (not exists $self->{keywords}->{hash}->{lc $namespace}->{lc $keyword}) {
|
|
|
|
return "There is no such regex `$keyword` for namespace `$self->{keywords}->{hash}->{$namespace}->{_name}`.";
|
2018-08-06 07:41:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (not defined $flag) {
|
|
|
|
my $text = "Flags:\n";
|
|
|
|
my $comma = '';
|
2020-01-15 03:10:53 +01:00
|
|
|
foreach $flag (keys %{ $self->{keywords}->{hash}->{lc $namespace}->{lc $keyword} }) {
|
2018-08-06 07:41:08 +02:00
|
|
|
if ($flag eq 'created_on') {
|
2020-01-15 03:10:53 +01:00
|
|
|
my $timestamp = strftime "%a %b %e %H:%M:%S %Z %Y", localtime $self->{keywords}->{hash}->{lc $namespace}->{lc $keyword}->{$flag};
|
2018-08-06 07:41:08 +02:00
|
|
|
$text .= $comma . "created_on: $timestamp";
|
|
|
|
} else {
|
2020-01-15 03:10:53 +01:00
|
|
|
$value = $self->{keywords}->{hash}->{lc $namespace}->{lc $keyword}->{$flag};
|
2018-08-06 07:41:08 +02:00
|
|
|
$text .= $comma . "$flag: $value";
|
|
|
|
}
|
|
|
|
$comma = ",\n ";
|
|
|
|
}
|
|
|
|
return $text;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (not defined $value) {
|
2020-01-15 03:10:53 +01:00
|
|
|
$value = $self->{keywords}->{hash}->{lc $namespace}->{lc $keyword}->{$flag};
|
2018-08-06 07:41:08 +02:00
|
|
|
if (not defined $value) {
|
|
|
|
return "/say $flag is not set.";
|
|
|
|
} else {
|
|
|
|
return "/say $flag is set to $value";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-15 03:10:53 +01:00
|
|
|
$self->{keywords}->{hash}->{lc $namespace}->{lc $keyword}->{$flag} = $value;
|
2018-08-06 07:41:08 +02:00
|
|
|
$self->{keywords}->save;
|
|
|
|
return "Flag set.";
|
|
|
|
}
|
|
|
|
when ("unset") {
|
2018-08-09 02:38:57 +02:00
|
|
|
my ($namespace, $keyword, $flag) = $self->{pbot}->{interpreter}->split_args($arglist, 3);
|
2018-08-07 05:24:15 +02:00
|
|
|
return "Usage: antispam unset <namespace> <regex> <flag>" if not defined $namespace or not defined $keyword or not defined $flag;
|
2018-08-06 07:41:08 +02:00
|
|
|
|
2020-01-15 03:10:53 +01:00
|
|
|
if (not exists $self->{keywords}->{hash}->{lc $namespace}) {
|
2018-08-07 05:24:15 +02:00
|
|
|
return "There is no such namespace `$namespace`.";
|
2018-08-06 07:41:08 +02:00
|
|
|
}
|
|
|
|
|
2020-01-15 03:10:53 +01:00
|
|
|
if (not exists $self->{keywords}->{hash}->{lc $namespace}->{lc $keyword}) {
|
2018-08-07 05:24:15 +02:00
|
|
|
return "There is no such keyword `$keyword` for namespace `$namespace`.";
|
2018-08-06 07:41:08 +02:00
|
|
|
}
|
|
|
|
|
2020-01-15 03:10:53 +01:00
|
|
|
if (not exists $self->{keywords}->{hash}->{lc $namespace}->{lc $keyword}->{$flag}) {
|
2018-08-07 05:24:15 +02:00
|
|
|
return "There is no such flag `$flag` for regex `$keyword` for namespace `$namespace`.";
|
2018-08-06 07:41:08 +02:00
|
|
|
}
|
|
|
|
|
2020-01-15 03:10:53 +01:00
|
|
|
delete $self->{keywords}->{hash}->{lc $namespace}->{lc $keyword}->{$flag};
|
2018-08-06 07:41:08 +02:00
|
|
|
$self->{keywords}->save;
|
|
|
|
return "Flag unset.";
|
|
|
|
}
|
|
|
|
when ("add") {
|
2018-08-09 02:38:57 +02:00
|
|
|
my ($namespace, $keyword) = $self->{pbot}->{interpreter}->split_args($arglist, 2);
|
2018-08-07 05:24:15 +02:00
|
|
|
return "Usage: antispam add <namespace> <regex>" if not defined $namespace or not defined $keyword;
|
2020-01-15 03:10:53 +01:00
|
|
|
my $data = {
|
|
|
|
owner => "$nick!$user\@$host",
|
|
|
|
created_on => scalar gettimeofday
|
|
|
|
};
|
|
|
|
$self->{keywords}->add($namespace, $keyword, $data);
|
2018-08-07 05:24:15 +02:00
|
|
|
return "/say Added `$keyword`.";
|
2018-08-06 07:41:08 +02:00
|
|
|
}
|
|
|
|
when ("remove") {
|
2018-08-09 02:38:57 +02:00
|
|
|
my ($namespace, $keyword) = $self->{pbot}->{interpreter}->split_args($arglist, 2);
|
2018-08-07 05:24:15 +02:00
|
|
|
return "Usage: antispam remove <namespace> <regex>" if not defined $namespace or not defined $keyword;
|
2020-01-15 03:10:53 +01:00
|
|
|
return $self->{keywords}->remove($namespace, $keyword);
|
2018-08-06 07:41:08 +02:00
|
|
|
}
|
|
|
|
default {
|
|
|
|
return "Unknown command '$command'; commands are: list/show, add, remove";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
1;
|