Blob


1 #!/usr/bin/perl
2 use strict;
3 use warnings;
5 package MonopBot;
6 use base qw(Bot::BasicBot);
7 use Expect;
8 $Expect::Log_Stdout = 0;
9 $Expect::Multiline_Matching = 0;
10 my $command = 'monop';
11 my $timeout = 300;
12 my $exp = new Expect;
13 $exp->raw_pty(1);
14 $exp->spawn($command, ()) or die "Cannot spawn $command: $!\n";
16 my $output;
17 my @nicks;
19 # returns output from command
20 sub readcmd {
21 my @results = $exp->expect($timeout, -re => '^[\n\s[:print:]]+$');
22 my ($pos, $error, $match, $before, $after) = @results;
23 return $before.$match.$after;
24 }
26 sub got_names {
27 my $self = shift;
28 my $arguments = shift;
29 @nicks = keys(%{$arguments->{names}});
30 }
32 sub chanjoin {
33 my $self = shift;
34 my $arguments = shift;
35 my $nick = $arguments->{who};
36 if ($nick eq $self->pocoirc->nick_name()) { # bot itself joins
37 $output = readcmd();
38 return $output;
39 }
40 return;
41 }
43 sub said {
44 my $self = shift;
45 my $arguments = shift;
46 if (scalar(@nicks) && grep /^$arguments->{who}$/, @nicks) {
47 print $exp "$arguments->{body}\n";
48 $output = readcmd();
49 return $output;
50 }
51 return;
52 }
54 package main;
56 my $bot = MonopBot->new(
57 server => 'irc.example.com',
58 port => '6667',
59 channels => ['#perl105'],
60 nick => 'nickname',
61 name => 'username',
62 );
64 local $SIG{INT} = sub {
65 $exp->hard_close();
66 print "Quitting program...\n";
67 $bot->shutdown("Quitting...");
68 };
69 $bot->run();