X-Git-Url: http://git.annexia.org/?a=blobdiff_plain;f=xavierbot.pl.in;h=f1a4cc8fdf39ffbbf53f2e7821ce9039c99d315a;hb=3cd6e84fb16187e3d85775397d35f5c432064e63;hp=caaf0f5715b6459041dd93a8d26bfaa7e01343b8;hpb=49cb8586a1d3d6b697a92dfcb8977dc0ecfc113f;p=xavierbot.git diff --git a/xavierbot.pl.in b/xavierbot.pl.in index caaf0f5..f1a4cc8 100755 --- a/xavierbot.pl.in +++ b/xavierbot.pl.in @@ -1,11 +1,12 @@ -#!/usr/bin/perl -wT +#!/usr/bin/perl -w # xavierbot : an OCaml interpreter IRC bot. # By Richard W.M. Jones . # This code is in the Public Domain. -# $Id: xavierbot.pl.in,v 1.4 2007/06/29 07:40:15 rjones Exp $ +# $Id: xavierbot.pl.in,v 1.9 2008/01/23 15:44:46 rjones Exp $ use strict; use POE qw(Component::IRC Wheel::Run); +use Getopt::Long; #---------------------------------------------------------------------- # Start of configuration. @@ -13,13 +14,31 @@ use POE qw(Component::IRC Wheel::Run); my $nick = "xavierbot"; my $ircname = "Xavierbot"; # Printable name. my $server = "chat.freenode.net"; -#my $server = "devserv.devel.redhat.com"; my $port = 6667; my $channel = "#ocaml"; # End of configuration. #---------------------------------------------------------------------- +# Command line args can override configuration. +GetOptions ("nick=s" => \$nick, + "ircname=s" => \$ircname, + "server=s" => \$server, + "port=i" => \$port, + "channel=s" => \$channel) + or die "$0: GetOptions: $!"; + +# Simple flood protection. This counts number of lines received from +# the toplevel, and is reset when we send a line. If this exceeds +# some value, then we just eat lines. +# XXX This ought to count characters, not lines. +my $flood_lim = 0; + +# Are we awake or sleeping? +my $sleeping = 0; + +#---------------------------------------------------------------------- + $ENV{PATH} = "/usr/bin:/bin"; POE::Session->create ( @@ -75,24 +94,46 @@ sub irc_public my $nick = (split /!/, $who)[0]; my $channel = $where->[0]; + my @usage = + ( + "expr ;; evaluate expr in toplevel and print result", + "help help message", + "restart restart the toplevel", + "sleep go to sleep", + "wake wake me up from sleep", + ); + print "got: $what\n"; - if (my ($stmt) = $what =~ /^\s*([^#].*;;)\s*$/) { - $heap->{ocaml}->put ("$stmt\n"); - } # XXX How to interpolate $nick into the patterns? - elsif ($what =~ /^\s*xavierbot\b.*\bhelp\b/) { + if ($what =~ /^\s*xavierbot\b.*\bhelp\b/) { my $nick = (split /!/, $who)[0]; $kernel->post ($sender => privmsg => $channel => - "$nick: expr ;; evaluate expr in OCaml toplevel"); - $kernel->post ($sender => privmsg => $channel => - "$nick: help help message"); - $kernel->post ($sender => privmsg => $channel => - "$nick: restart restart the toplevel"); + "hello $nick, I am xavierbot @VERSION@, an OCaml toplevel"); + $kernel->post ($sender => privmsg => $channel => $_) + foreach (@usage); } elsif ($what =~ /^\s*xavierbot\b.*\brestart\b/) { + $sleeping = 0; print STDOUT "got instruction to restart ...\n"; restart_toplevel ($heap->{ocaml}); } + elsif (!$sleeping) { + if (my ($stmt) = $what =~ m/^\s*([^\#].*;;)\s*$/) { + $heap->{ocaml}->put ("$stmt\n"); + $flood_lim = 0; + } + elsif ($what =~ /^\s*xavierbot\b.*\b(sleep|shut|quiet)\b/) { + $sleeping = 1; + $kernel->post ($sender => privmsg => $channel => + "xavierbot goes to sleep (do 'xavierbot wake' to wake)"); + } + } else { # sleeping + if ($what =~ /^\s*xavierbot\b.*\bwake\b/) { + $sleeping = 0; + $kernel->post ($sender => privmsg => $channel => + "xavierbot wakes up"); + } + } undef; } @@ -120,7 +161,10 @@ sub got_stdout { my ($kernel,$heap, $input, $wheel_id) = @_[KERNEL,HEAP,ARG0,ARG1]; print "Child said: $input\n"; - $kernel->post ($heap->{irc} => privmsg => $channel => "$input"); + if ($flood_lim < 10) { + $kernel->post ($heap->{irc} => privmsg => $channel => "$input"); + } + $flood_lim++; } # Got a SIGCHLD, so start the bot up again.