Added lots more modules.
[xavierbot.git] / xavierbot.pl.in
index 243dcbc..caaf0f5 100755 (executable)
@@ -2,14 +2,11 @@
 # xavierbot : an OCaml interpreter IRC bot.
 # By Richard W.M. Jones <rich@annexia.org>.
 # This code is in the Public Domain.
-# $Id: xavierbot.pl.in,v 1.2 2007/06/28 20:49:10 rjones Exp $
+# $Id: xavierbot.pl.in,v 1.4 2007/06/29 07:40:15 rjones Exp $
 
 use strict;
-
 use POE qw(Component::IRC Wheel::Run);
 
-$ENV{PATH} = "/usr/bin:/bin";
-
 #----------------------------------------------------------------------
 # Start of configuration.
 
@@ -23,9 +20,11 @@ my $channel = "#ocaml";
 # End of configuration.
 #----------------------------------------------------------------------
 
+$ENV{PATH} = "/usr/bin:/bin";
+
 POE::Session->create (
   package_states => [
-    main => [ qw(_default _start irc_001 irc_public got_stdout) ],
+    main => [ qw(_default _start irc_001 irc_public got_stdout got_sigchld) ],
   ],
 );
 
@@ -44,12 +43,9 @@ sub _start
         port => $port,
         ) or die "POE::Component::IRC->spawn failed: $!";
 
-    my $ocaml = POE::Wheel::Run->new
-       (
-        Program => "@WRAPPER@",
-        StdoutEvent => "got_stdout",
-        StderrEvent => "got_stdout",
-        ) or die "POE::Wheel::Run->new @WRAPPER@ failed: $!";
+    my $ocaml = start_toplevel ();
+
+    $kernel->sig(CHLD => qw(got_sigchld));
 
     $heap->{irc} = $irc;
     $heap->{ocaml} = $ocaml;
@@ -81,9 +77,22 @@ sub irc_public
 
     print "got: $what\n";
     if (my ($stmt) = $what =~ /^\s*([^#].*;;)\s*$/) {
-       print "stmt = $stmt\n";
        $heap->{ocaml}->put ("$stmt\n");
     }
+    # XXX How to interpolate $nick into the patterns?
+    elsif ($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");
+    }
+    elsif ($what =~ /^\s*xavierbot\b.*\brestart\b/) {
+       print STDOUT "got instruction to restart ...\n";
+       restart_toplevel ($heap->{ocaml});
+    }
     undef;
 }
 
@@ -105,7 +114,7 @@ sub _default
 
 #----------------------------------------------------------------------
 
-# Bot wrote something.
+# Toplevel wrote something.
 
 sub got_stdout
 {
@@ -113,3 +122,38 @@ sub got_stdout
     print "Child said: $input\n";
     $kernel->post ($heap->{irc} => privmsg => $channel => "$input");
 }
+
+# Got a SIGCHLD, so start the bot up again.
+
+sub got_sigchld
+{
+    my ($kernel, $heap) = @_[KERNEL,HEAP];
+    my $ocaml = start_toplevel ();
+    $heap->{ocaml} = $ocaml;
+}
+
+# Start up the toplevel (assumes it's not running).
+
+sub start_toplevel
+{
+    return POE::Wheel::Run->new
+       (
+        Program => "@WRAPPER@",
+        StdoutEvent => "got_stdout",
+        StderrEvent => "got_stdout",
+        ) or die "POE::Wheel::Run->new @WRAPPER@ failed: $!";
+}
+
+# Restart the toplevel - kill the old one, and a new one
+# will be spawned after we get the SIGCHLD signal.
+#
+# XXX Can't send signal to setuid child, so instead just close
+# stdin.
+
+sub restart_toplevel
+{
+    my $ocaml = shift;
+
+    $ocaml->kill (9);
+    $ocaml->shutdown_stdin;
+}