summaryrefslogtreecommitdiffstats
path: root/lib/Crawl/Bot/Plugin/Monster.pm
blob: 9918b8df911fb7a20cc84fd67bb30a952ae2ed0a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package Crawl::Bot::Plugin::Monster;
use Moose;

extends 'Crawl::Bot::Plugin';

use autodie;

warn "Loading Plugin::Monster\n";

sub said {
    my $self = shift;
    my ($args) = @_;
    my @keys = (who => $args->{who}, channel => $args->{channel}, "body");

    if ($args->{body} =~ /^%(\?|0\.\d+|)\? *([^?].*)/) {
        my $branch = ($1 eq "?") ? "trunk" : ($1 || "stable");
        my $resp = $self->get_monster_info($2, $branch);
        if ($resp) {
            $self->say(@keys, $resp);
        } else {
            $self->say(@keys, "Error calling monster-$branch: $!")
        }
    }
}

sub get_monster_info {
    my $self = shift;
    my $monster = shift;
    my $branch = shift;

    return "Error: Bad branch $branch\n" unless $branch =~ /^(trunk|stable|0.\d+)$/;

    CORE::open(F, "-|") || do {
        # Collect stderr too
        close STDERR; open STDERR, '>&STDOUT';
        exec "bin/monster-$branch", $monster or return "Could not execute monster-$branch: $!";
    };
    binmode F, ":utf8";
    local $/ = undef;
    my $resp = <F>;
    CORE::close(F);

    return $resp;
}

__PACKAGE__->meta->make_immutable;
no Moose;

1;