From 4f1f44cc656df0601f8cb6c5d4770d3efb55c43e Mon Sep 17 00:00:00 2001 From: David Phillips Date: Mon, 8 Jun 2020 21:11:17 +1200 Subject: Add Correction module This commit adds a plugin for replacing arbitrary regexes with corrections. This is similar to the Titillate module, except that only the first match is replaced, and the whole line with the correction made is returned. This can be used for common spelling mistakes, or for memey replacements. Since it is likely to get annoying, there is also a config option to reduce the bot to only respond with some level of random chance. --- Plugin/Correction.pm | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++ docker/Dockerfile | 1 + 2 files changed, 52 insertions(+) create mode 100644 Plugin/Correction.pm diff --git a/Plugin/Correction.pm b/Plugin/Correction.pm new file mode 100644 index 0000000..bc5d1fc --- /dev/null +++ b/Plugin/Correction.pm @@ -0,0 +1,51 @@ +package Plugin::Correction; + +use strict; +use warnings; +use Encode qw/decode/; +use Data::Munge qw/replace/; + +use IdaliusConfig qw/assert_scalar/; + +my $config; +my $root_config; + +sub configure { + my $self = shift; + shift; # cmdref + shift; # run_command + $config = shift; + $root_config = shift; + + IdaliusConfig::assert_scalar($config, $self, "chance"); + die "chance must be from 0 to 100" + unless $config->{chance} >= 0 && $config->{chance} <= 100; + + IdaliusConfig::assert_dict($config, $self, "corrections"); + + return $self; +} + +sub on_message { + my ($self, $logger, $who, $where, $raw_what, $what, $irc) = @_; + + if (ref($where) eq "ARRAY") { + $where = $where->[0]; + } + + $what = Encode::decode('utf8', $what); + + foreach my $mistake (keys %{$config->{corrections}}) { + if ($what =~ /$mistake/i) { + my $response = replace($what, qr/$mistake/i, "\x02$config->{corrections}->{$mistake}\x02", "g"); + return unless rand(100) < $config->{chance}; + return $response; + } + } + return; +} + +sub on_action { + on_message(@_); +} +1; diff --git a/docker/Dockerfile b/docker/Dockerfile index 500d622..4241cab 100644 --- a/docker/Dockerfile +++ b/docker/Dockerfile @@ -10,6 +10,7 @@ RUN apk add --no-cache perl perl-dev perl-app-cpanminus make gcc libgcc wget mus apk add --no-cache perl-config-tiny perl-html-parser perl-datetime perl-io-socket-ssl && \ cpanm install POE POE::Component::IRC && \ cpanm install -n POE::Component::SSLify && \ + cpanm install Data::Munge && \ apk del --no-cache perl-dev perl-app-cpanminus make gcc libgcc wget musl-dev openssl-dev zlib-dev && \ rm -rf /root/.cpanm /var/cache/apk/* -- cgit v1.1