irc bot reporting on bnetd activities
authorShiar <shiar@shiar.org>
Mon, 31 Dec 2007 16:11:46 +0000 (16:11 +0000)
committerShiar <shiar@shiar.org>
Mon, 31 Dec 2007 16:11:46 +0000 (16:11 +0000)
Connects to #schtarr at flutnet and prints out basic Starcraft activity
on the server (players entering/leaving, games starting/ending).

schtarrbot [new file with mode: 0755]

diff --git a/schtarrbot b/schtarrbot
new file mode 100755 (executable)
index 0000000..b83adcf
--- /dev/null
@@ -0,0 +1,110 @@
+#!/usr/bin/env perl
+
+use strict;
+use warnings;
+
+use Net::IRC;
+
+my %botinfo = (
+       Nick => "schtarrbot",
+       Ircname => "schtarrbot",
+       Server => "shiar.net",
+       Port => 6667,
+);
+my @chaninfo = ("#schtarr");
+my %loginfo = (
+       filename => "/var/log/pvpgn/bnetd.log",
+       debug => 1,
+);
+
+my $irc = new Net::IRC;
+my $conn = $irc->newconn(%botinfo) or die "couldn't connect to server";
+my $joined = 0;
+
+$conn->add_global_handler('join', sub {
+       $joined = 1;
+});
+
+$conn->add_global_handler('376', sub {
+       # connect
+       $_[0]->join(@chaninfo) or die "couldn't connect to channel";
+});
+
+$conn->add_global_handler('public', sub {
+       my ($self, $event) = @_;
+       $event->args =~ m/^!(\w+)/ or next;
+       handle_cmd($1);
+});
+
+open my $log, "<", $loginfo{filename} or die $!;
+seek $log, 0, 2;  # eof
+
+sub handle_cmd {
+}
+
+sub handle_log {
+       my ($func, $msg) = @_;
+
+       if ($func eq "_client_loginreq2" and $line =~ m{
+               "([^"]+)" \Q logged in (correct password)\E $
+       }x) {
+               my $nick = $1;
+               print "* login $nick\n";
+               $conn->privmsg($chaninfo[0], "$nick entered bnet");
+       }
+
+       elsif ($func eq "conn_destroy" and $line =~ m{
+               "([^"]+)" \Q logged out\E $
+       }x) {
+               my $nick = $1;
+               print "* logout $nick\n";
+               $conn->privmsg($chaninfo[0], "$nick left bnet");
+       }
+
+       elsif ($func eq "game_create" and $line =~ m{
+               \Qgame "\E ([^"]*) \Q" (pass "\E([^"]*)\Q") type \E(\d*)\(([^)]*)\)\Q startver \E(\d+)\Q created\E $
+       }x) {
+               my ($name, $pass, $typeid, $type, $version) = ($1, $2, $3, $4, $5);
+               print "* game created\n";
+               $conn->privmsg($chaninfo[0], "$type game '$name' created");
+       }
+
+       elsif ($func eq "_client_startgame4" and $line =~ m{
+               \Qgot startgame4 status for game "\E([^"]*)\Q" is 0x\E
+       }) {
+               my $game = $1;
+               print "* game started\n";
+               $conn->privmsg($chaninfo[0], "game '$game' started");
+       }
+
+       if ($func eq "game_report" and $line =~ m{
+               \Qgame report saved as "\E([^"]*)" $
+       }) {
+               my $report = $1;
+               print "* game reported\n";
+       }
+
+       elsif (not $loginfo{debug} and $func eq "game_destroy" and $line =~ m{
+               \Qgame deleted\E $
+       }x) {
+               print "* game deleted\n";
+               $conn->privmsg($chaninfo[0], "game closed");
+       }
+}
+
+while (1) {
+       sleep 1.5;
+       $irc->do_one_loop();
+       $joined or next;
+
+       seek $log, 0, 1; # clear eof condition
+       while (defined (my $line = readline $log)) {
+               my ($date, $level, $func, $msg) = $line =~ m{
+                       ^ (\S+\s\S+\s\S+) \s \[([^\]\s]+)\s*\] \s ([^\s:]+): \s (.*) $
+               }x or warn("Invalid line: $line"), next;
+               $msg =~ s/(?:\[\d+\])+\s//g;
+#              print ". $func ($msg)\n";
+               handle_log($func, $msg);
+       }
+}
+