2018-08-06 07:41:08 +02:00
|
|
|
# File: AntiSpam.pm
|
|
|
|
#
|
|
|
|
# Purpose: Checks if a message is spam
|
|
|
|
|
2021-07-11 00:00:22 +02:00
|
|
|
# SPDX-FileCopyrightText: 2021 Pragmatic Software <pragma78@gmail.com>
|
|
|
|
# SPDX-License-Identifier: MIT
|
2018-08-06 07:41:08 +02:00
|
|
|
|
2021-07-21 07:44:51 +02:00
|
|
|
package PBot::Core::AntiSpam;
|
|
|
|
use parent 'PBot::Core::Class';
|
2018-08-06 07:41:08 +02:00
|
|
|
|
2021-06-19 06:23:34 +02:00
|
|
|
use PBot::Imports;
|
2018-08-06 07:41:08 +02:00
|
|
|
|
|
|
|
sub initialize {
|
2020-02-15 23:38:32 +01:00
|
|
|
my ($self, %conf) = @_;
|
|
|
|
|
2021-07-24 01:41:32 +02:00
|
|
|
my $filename = $self->{pbot}->{registry}->get_value('general', 'data_dir') . '/spam_keywords';
|
2020-02-15 23:38:32 +01:00
|
|
|
|
2021-07-24 04:22:25 +02:00
|
|
|
$self->{keywords} = PBot::Core::Storage::DualIndexHashObject->new(
|
|
|
|
pbot => $self->{pbot},
|
|
|
|
name => 'SpamKeywords',
|
2021-07-24 01:41:32 +02:00
|
|
|
filename => $filename,
|
|
|
|
);
|
2020-02-15 23:38:32 +01:00
|
|
|
|
2021-07-24 01:41:32 +02:00
|
|
|
$self->{keywords}->load;
|
2018-08-06 07:41:08 +02:00
|
|
|
|
2021-07-24 01:41:32 +02:00
|
|
|
$self->{pbot}->{registry}->add_default('text', 'antispam', 'enforce', $conf{enforce_antispam} // 1);
|
2018-08-06 07:41:08 +02:00
|
|
|
}
|
|
|
|
|
2020-05-04 22:21:35 +02:00
|
|
|
sub is_spam {
|
|
|
|
my ($self, $namespace, $text, $all_namespaces) = @_;
|
|
|
|
my $lc_namespace = lc $namespace;
|
|
|
|
|
|
|
|
return 0 if not $self->{pbot}->{registry}->get_value('antispam', 'enforce');
|
|
|
|
return 0 if $self->{pbot}->{registry}->get_value($namespace, 'dont_enforce_antispam');
|
|
|
|
|
|
|
|
my $ret = eval {
|
|
|
|
foreach my $space ($self->{keywords}->get_keys) {
|
|
|
|
if ($all_namespaces or $lc_namespace eq $space) {
|
2021-07-24 01:41:32 +02:00
|
|
|
foreach my $keyword ($self->{keywords}->get_keys($space)) {
|
|
|
|
return 1 if $text =~ m/$keyword/i;
|
|
|
|
}
|
2020-05-04 22:21:35 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
};
|
|
|
|
|
|
|
|
if ($@) {
|
|
|
|
$self->{pbot}->{logger}->log("Error in is_spam: $@");
|
|
|
|
return 0;
|
|
|
|
}
|
2021-07-24 01:41:32 +02:00
|
|
|
|
2020-05-04 22:21:35 +02:00
|
|
|
$self->{pbot}->{logger}->log("AntiSpam: spam detected!\n") if $ret;
|
|
|
|
return $ret;
|
|
|
|
}
|
|
|
|
|
2018-08-06 07:41:08 +02:00
|
|
|
1;
|