3
0
mirror of https://github.com/pragma-/pbot.git synced 2024-11-09 13:39:34 +01:00
pbot/lib/PBot/Core/AntiSpam.pm
2023-04-13 21:04:12 -07:00

54 lines
1.5 KiB
Perl

# File: AntiSpam.pm
#
# Purpose: Checks if a message is spam
# SPDX-FileCopyrightText: 2018-2023 Pragmatic Software <pragma78@gmail.com>
# SPDX-License-Identifier: MIT
package PBot::Core::AntiSpam;
use parent 'PBot::Core::Class';
use PBot::Imports;
sub initialize($self, %conf) {
my $filename = $self->{pbot}->{registry}->get_value('general', 'data_dir') . '/spam_keywords';
$self->{keywords} = PBot::Core::Storage::DualIndexHashObject->new(
pbot => $self->{pbot},
name => 'SpamKeywords',
filename => $filename,
);
$self->{keywords}->load;
$self->{pbot}->{registry}->add_default('text', 'antispam', 'enforce', $conf{enforce_antispam} // 1);
}
sub is_spam($self, $namespace, $text, $all_namespaces = 0) {
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) {
foreach my $keyword ($self->{keywords}->get_keys($space)) {
return 1 if $text =~ m/$keyword/i;
}
}
}
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;
}
1;