<commit_before>package RoboBot::Plugin::Filter;

use v5.20;

use namespace::autoclean;

use Moose;
use MooseX::SetOnce;

use FileHandle;
use IPC::Open2;

extends 'RoboBot::Plugin';

has '+name' => (
    default => 'Filter',
);

has '+description' => (
    default => 'Provides various text translation and munging filters. Mostly humorous.',
);

has '+commands' => (
    default => sub {
        my %h = map { $_ => { method      => 'filter_text',
                        description => 'Filters input argument text through the ' . $_ . ' program.',
                        usage       => '<text>' }
          } qw( b1ff chef cockney eleet fudd nethackify newspeak pirate scottish scramble uniencode );
        return \%h;
    },
);

has 'filter_paths' => (
    is      => 'rw',
    isa     => 'HashRef',
    default => sub { {} },
);

sub filter_text {
    my ($self, $message, $filter, @args) = @_;

    return unless exists $self->commands->{$filter};

    my $prog = $self->find_filter($message, $filter);
    return unless defined $prog;

    my $pid = open2(my $rfh, my $wfh, $prog) || return;
    print $wfh join("\n", @args);
    close($wfh);

    my $filtered = join("\n", <$rfh>);
    $filtered =~ s{[\n\r]+}{\n}ogs;

    return split(/\n/o, $filtered);
}

sub find_filter {
    my ($self, $message, $filter) = @_;

    return $self->filter_paths->{$filter} if exists $self->filter_paths->{$filter};

    return undef unless $filter =~ m{^[A-Za-z0-9]+$}o;

    my $path = `which $filter`;
    chomp($path);

    unless (-x $path) {
        $message->response->raise(sprintf('The filter %s appears to not be installed on this machine.', $filter));
        return undef;
    }

    $self->filter_paths->{$filter} = $path;
    return $path;
}

__PACKAGE__->meta->make_immutable;

1;<commit_msg>force base path of /usr/games on various filter executables, since the simplistic 'which' from before would produce bad results on a host that also had the config-mgmt-of-nightmares installed<commit_after>package RoboBot::Plugin::Filter;

use v5.20;

use namespace::autoclean;

use Moose;
use MooseX::SetOnce;

use FileHandle;
use IPC::Open2;

extends 'RoboBot::Plugin';

has '+name' => (
    default => 'Filter',
);

has '+description' => (
    default => 'Provides various text translation and munging filters. Mostly humorous.',
);

has '+commands' => (
    default => sub {
        my %h = map { $_ => { method      => 'filter_text',
                        description => 'Filters input argument text through the ' . $_ . ' program.',
                        usage       => '<text>' }
          } qw( b1ff chef cockney eleet fudd nethackify newspeak pirate scottish scramble uniencode );
        return \%h;
    },
);

has 'filter_paths' => (
    is      => 'rw',
    isa     => 'HashRef',
    default => sub { {} },
);

sub filter_text {
    my ($self, $message, $filter, @args) = @_;

    return unless exists $self->commands->{$filter};

    my $prog = $self->find_filter($message, $filter);
    return unless defined $prog;

    my $pid = open2(my $rfh, my $wfh, $prog) || return;
    print $wfh join("\n", @args);
    close($wfh);

    my $filtered = join("\n", <$rfh>);
    $filtered =~ s{[\n\r]+}{\n}ogs;

    return split(/\n/o, $filtered);
}

sub find_filter {
    my ($self, $message, $filter) = @_;

    return $self->filter_paths->{$filter} if exists $self->filter_paths->{$filter};

    return undef unless $filter =~ m{^[A-Za-z0-9]+$}o;

    unless (-x "/usr/games/$filter") {
        $message->response->raise(sprintf('The filter %s appears to not be installed on this machine.', $filter));
        return undef;
    }

    $self->filter_paths->{$filter} = "/usr/games/$filter";
    return "/usr/games/$filter";
}

__PACKAGE__->meta->make_immutable;

1;
{'fraction_non_alphanumeric': 0.17196608373078962, 'fraction_numerical': 0.0047694753577106515, 'mean_word_length': 2.7939698492462313, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 61, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '897774', 'n_tokens_mistral': 1301, 'n_tokens_neox': 1235, 'n_words': 337}