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

57 lines
1.5 KiB
Perl
Raw Normal View History

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) = @_;
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',
filename => $filename,
);
2020-02-15 23:38:32 +01:00
$self->{keywords}->load;
2018-08-06 07:41:08 +02:00
$self->{pbot}->{registry}->add_default('text', 'antispam', 'enforce', $conf{enforce_antispam} // 1);
2018-08-06 07:41:08 +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) {
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;
}
2018-08-06 07:41:08 +02:00
1;