-
Notifications
You must be signed in to change notification settings - Fork 1
/
fight-club.pl
executable file
·123 lines (95 loc) · 2.31 KB
/
fight-club.pl
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#! /usr/bin/perl
use strict;
use warnings;
use IO::Handle;
use Getopt::Long;
END {
kill TERM => -$$;
}
# The plot:
# 1) wait for requests on IRC
# 2) on request, fork and play request, or queue request.
# 3) cancels blow the whole playlist.
my $CRAWL_HOME = $ENV{CRAWL_HOME} or die "CRAWL_HOME must be set!\n";
my $ARENA_REQ_FILE = 'fight-club.req';
my $ARENA_IRC_PASSFILE = 'fight-club-irc.pwd';
my $IRCSERVER = 'irc.freenode.net';
my $IRCNICK = 'varmin';
my $IRCNAME = 'Varmin the sexy verminbot';
my $IRCPORT = 6667;
my @CHANNELS = ('##crawl', '##crawl-dev');
our $IRC;
our $LOGCRAWL;
our $LOGCRAWLDEV;
local $SIG{CHLD} = sub { };
my %opt;
GetOptions(\%opt, 'local', 'req=s');
open my $REQH, '>', $ARENA_REQ_FILE or die "Could not open $ARENA_REQ_FILE: $!";
# Start the slave that plays requests.
run_slave();
do_irc();
sub run_slave {
my $pid = fork;
die "Unable to fork!" unless defined $pid;
# Parent does IRC, child does the pty.
return if $pid;
my @args = ('./fight-club-slave.pl', '--req', $ARENA_REQ_FILE);
push @args, '--local' if $opt{local};
exec(@args);
exit 0;
}
sub do_irc {
$IRC = Varmin->new(nick => $IRCNICK,
server => $IRCSERVER,
port => $IRCPORT,
ircname => $IRCNAME,
channels => [ @CHANNELS ])
or die "Unable to connect to $IRCSERVER: $!";
$IRC->run();
}
sub chomped_line {
my $fname = shift;
if (-r $fname) {
open my $inf, '<', $fname or return undef;
chomp(my $contents = <$inf>);
return $contents;
}
undef
}
sub clean_response {
my $text = shift;
length($text) > 400? substr($text, 0, 400) : $text
}
sub process_msg {
my ($m) = @_;
my $nick = $$m{who};
my $channel = $$m{channel};
my $body = $$m{body};
if ($body =~ /^!fight (.*)/i) {
print "Fight request: $1 by $nick\n";
run_arena($nick, $1);
}
}
sub run_arena {
my ($nick, $what) = @_;
print $REQH "$nick: $what\n";
$REQH->flush();
}
package Varmin;
use base 'Bot::BasicBot';
sub connected {
my $self = shift;
my $password = main::chomped_line($ARENA_IRC_PASSFILE);
if ($password) {
$self->say(channel => 'msg',
who => 'nickserv',
body => "identify $password");
}
return undef;
}
sub said {
my ($self, $m) = @_;
main::process_msg($m);
return undef;
}
1