3
0
mirror of https://github.com/pragma-/pbot.git synced 2025-02-12 11:31:03 +01:00
pbot/lib/PBot/Core/AntiSpam.pm

54 lines
1.5 KiB
Perl
Raw Normal View History

2018-08-05 22:41:08 -07:00
# File: AntiSpam.pm
#
# Purpose: Checks if a message is spam
2023-02-20 21:31:52 -08:00
# SPDX-FileCopyrightText: 2018-2023 Pragmatic Software <pragma78@gmail.com>
2021-07-10 15:00:22 -07:00
# SPDX-License-Identifier: MIT
2018-08-05 22:41:08 -07:00
2021-07-20 22:44:51 -07:00
package PBot::Core::AntiSpam;
use parent 'PBot::Core::Class';
2018-08-05 22:41:08 -07:00
2021-06-18 21:23:34 -07:00
use PBot::Imports;
2018-08-05 22:41:08 -07:00
sub initialize($self, %conf) {
my $filename = $self->{pbot}->{registry}->get_value('general', 'data_dir') . '/spam_keywords';
2020-02-15 14:38:32 -08:00
2021-07-23 19:22:25 -07:00
$self->{keywords} = PBot::Core::Storage::DualIndexHashObject->new(
pbot => $self->{pbot},
name => 'SpamKeywords',
filename => $filename,
);
2020-02-15 14:38:32 -08:00
$self->{keywords}->load;
2018-08-05 22:41:08 -07:00
$self->{pbot}->{registry}->add_default('text', 'antispam', 'enforce', $conf{enforce_antispam} // 1);
2018-08-05 22:41:08 -07:00
}
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;
}
2018-08-05 22:41:08 -07:00
1;