]> git.somenet.org - irc/bugbot.git/blob - BotModules/Filter.bm
some old base
[irc/bugbot.git] / BotModules / Filter.bm
1 ################################
2 # Filter Module                #
3 ################################
4
5 # The canonical filters should be installed on your path somewhere.
6 # You can get the source from these from your local distributor.
7
8 package BotModules::Filter;
9 use vars qw(@ISA);
10 use IPC::Open2;
11 @ISA = qw(BotModules);
12 1;
13
14 my @Filters = (
15     'b1ff',
16     'chef',
17     'cockney',
18     'eleet',
19     'jethro',
20     'jibberish',
21     'jive',
22     'kraut',
23     'nyc',
24     'rasterman',
25     'upside-down',
26 );
27
28 sub Help {
29     my $self = shift;
30     my ($event) = @_;
31     my $reply = {
32         '' => 'This module is an interface to the text filter applications.',
33     };
34     foreach (@Filters) {
35         $reply->{$_} = "Pass the text through the $_ filter. Syntax: $_ <text>";
36     }
37     if ($self->isAdmin($event)) {
38         $reply->{'filtersay'} = "Pass text through a filter and send it to a channel. Syntax: filtersay <filter> <channel> <text>";
39     }
40     return $reply;
41 }
42
43 sub Told {
44     my $self = shift;
45     my ($event, $message) = @_;
46     foreach (@Filters) {
47         if ($message =~ /^\s*\Q$_\E\s+(.+?)\s*$/si) {
48             $self->spawnChild($event, sub { return $self->Filter(@_); }, [$_, $1], 'filter', []);
49             return 0; # we've dealt with it, no need to do anything else.
50         } elsif (($message =~ /^\s*filtersay\s+\Q$_\E\s+(\S+)\s+(.+?)\s*$/si) and ($self->isAdmin($event))) {
51             $self->spawnChild($event, sub { return $self->Filter(@_); }, [$_, $2], 'filter', [$1]);
52             return 0; # we've dealt with it, no need to do anything else.
53         }
54     }
55     return $self->SUPER::Told(@_);
56 }
57
58 sub Filter {
59     my $self = shift;
60     my($filter, $text) = @_;
61     my $reader;
62     my $writer;
63     local $/ = undef;
64     my $pid = open2($reader, $writer, $filter);
65     print $writer $text;
66     close($writer);
67     my $reply = <$reader>;
68     close($reader);
69     waitpid($pid, 0);
70     return $reply;
71 }
72
73 # ChildCompleted - Called when a child process has quit
74 sub ChildCompleted {
75     my $self = shift;
76     my ($event, $type, $output, @data) = @_;
77     if ($type eq 'filter') {
78         local $event->{'target'} = $data[0] if defined($data[0]);
79         $self->say($event, $output);
80     } else {
81         return $self->SUPER::ChildCompleted(@_);
82     }
83 }