3
0
mirror of https://github.com/pragma-/pbot.git synced 2024-12-23 19:22:40 +01:00
pbot/lib/PBot/Plugin/TypoSub.pm

116 lines
4.7 KiB
Perl
Raw Normal View History

2021-06-19 06:23:34 +02:00
# File: TypoSub.pm
#
# Purpose: Replaces "typos" with "corrections".
2020-01-31 10:56:20 +01:00
#
# Examples:
#
# <alice> i like dogs
# <bob> s/dogs/cats/
# <PBot> bob thinks alice meant to say: i like cats
#
# <alice> i like candy
# <alice> s/like/love/
# <PBot> alice meant to say: i love candy
2023-02-21 06:31:52 +01:00
# SPDX-FileCopyrightText: 2020-2023 Pragmatic Software <pragma78@gmail.com>
2021-07-11 00:00:22 +02:00
# SPDX-License-Identifier: MIT
2021-06-19 06:23:34 +02:00
2021-07-14 04:45:56 +02:00
package PBot::Plugin::TypoSub;
use parent 'PBot::Plugin::Base';
2021-06-19 06:23:34 +02:00
use PBot::Imports;
2020-01-31 10:56:20 +01:00
sub initialize($self, %conf) {
2020-02-15 23:38:32 +01:00
$self->{pbot}->{event_dispatcher}->register_handler('irc.public', sub { $self->on_public(@_) });
$self->{pbot}->{event_dispatcher}->register_handler('irc.caction', sub { $self->on_public(@_) });
2020-01-31 10:56:20 +01:00
}
sub unload($self) {
2020-02-15 23:38:32 +01:00
$self->{pbot}->{event_dispatcher}->remove_handler('irc.public');
$self->{pbot}->{event_dispatcher}->remove_handler('irc.caction');
2020-01-31 10:56:20 +01:00
}
sub on_public($self, $event_type, $event) {
2023-01-31 14:44:34 +01:00
my ($nick, $user, $host, $msg) = ($event->nick, $event->user, $event->host, $event->args);
my $channel = lc $event->{to}[0];
2020-02-15 23:38:32 +01:00
($nick, $user, $host) = $self->{pbot}->{irchandlers}->normalize_hostmask($nick, $user, $host);
2020-04-09 20:42:17 +02:00
return 0 if $self->{pbot}->{ignorelist}->is_ignored($channel, "$nick!$user\@$host");
2023-01-31 14:44:34 +01:00
2020-02-15 23:38:32 +01:00
my $nosubs = $self->{pbot}->{registry}->get_value($channel, 'notyposub');
return 0 if defined $nosubs and $nosubs;
2020-02-15 23:38:32 +01:00
return 0 if $self->{pbot}->{users}->get_loggedin_user_metadata($channel, "$nick!$user\@$host", 'notyposub');
2020-02-15 23:38:32 +01:00
return 0 if $channel !~ m/^#/;
return 0 if $event->{interpreted};
if ($msg =~ m/^\s*s([[:punct:]])/) {
my $separator = $1;
my $sep = quotemeta $separator;
if ($msg =~ m/^\s*s${sep}(.*?)(?<!\\)${sep}(.*?)(?<!\\)${sep}([g]*).*$/ or $msg =~ m/^\s*s${sep}(.*?)(?<!\\)${sep}(.*)$/) {
my ($regex, $replacement, $modifiers) = ($1, $2, $3);
eval {
use re::engine::RE2 -strict => 1;
2020-02-15 23:38:32 +01:00
my $rx = qr/$regex/;
my $messages = $self->{pbot}->{messagehistory}->{database}->get_recent_messages_from_channel($channel, 50, $self->{pbot}->{messagehistory}->{MSG_CHAT}, 'DESC');
my $botnick = $self->{pbot}->{conn}->nick;
2020-02-15 23:38:32 +01:00
my $bot_trigger = $self->{pbot}->{registry}->get_value($channel, 'trigger') // $self->{pbot}->{registry}->get_value('general', 'trigger');
my $ignore_commands = $self->{pbot}->{registry}->get_value($channel, 'typosub_ignore_commands') // $self->{pbot}->{registry}->get_value('typosub', 'ignore_commands')
// 1;
foreach my $message (@$messages) {
next if $ignore_commands and $message->{msg} =~ m/^(?:$bot_trigger|$botnick.?)/;
next if $message->{msg} =~ m/^\s*s[[:punct:]](.*?)[[:punct:]](.*?)[[:punct:]]?g?\s*$/;
if ($message->{msg} =~ /$rx/) {
my $hostmask = $self->{pbot}->{messagehistory}->{database}->find_message_account_by_id($message->{id});
my ($target) = $hostmask =~ m/([^!]+)/;
my $result;
if ($nick eq $target) { $result = "$nick meant to say: "; }
else { $result = "$nick thinks $target meant to say: "; }
my $text = $message->{msg};
if ($modifiers and $modifiers =~ m/g/) {
$text =~ s{$rx}
{
my @stuff = ($1, $2, $3, $4, $5, $6, $7, $8, $9);
my $t = $replacement;
my $i = 0;
2020-02-22 16:26:23 +01:00
defined $_ || last, ++$i, $t =~ s|[\$\\]$i|$_|g for @stuff;
$t
}gxe;
$text = substr($text, 0, 350);
2020-02-15 23:38:32 +01:00
} else {
$text =~ s{$rx}
{
my $i = 0;
my @stuff = ($1, $2, $3, $4, $5, $6, $7, $8, $9);
my $t = $replacement;
2020-02-22 16:26:23 +01:00
defined $_ || last, ++$i, $t =~ s|[\$\\]$i|$_|g for @stuff;
$t
}xe;
2020-02-15 23:38:32 +01:00
}
$event->{conn}->privmsg($channel, "$result$text");
return 0;
}
}
};
if ($@) {
my $error = "Error in `s${separator}${regex}${separator}${replacement}${separator}${modifiers}`: $@";
$error =~ s/ at .*$//;
$event->{conn}->privmsg($nick, $error);
return 0;
2020-01-31 10:56:20 +01:00
}
}
}
2020-02-15 23:38:32 +01:00
return 0;
2020-01-31 10:56:20 +01:00
}
1;