Version 0.5.
[xavierbot.git] / xavierbot.pl
index bfdd9bf..4650f0d 100755 (executable)
@@ -2,7 +2,7 @@
 # xavierbot : an OCaml interpreter IRC bot.
 # By Richard W.M. Jones <rich@annexia.org>.
 # This code is in the Public Domain.
-# $Id: xavierbot.pl,v 1.7 2007/06/29 07:55:47 rjones Exp $
+# $Id: xavierbot.pl,v 1.8 2007/06/29 19:39:13 rjones Exp $
 
 use strict;
 use POE qw(Component::IRC Wheel::Run);
@@ -22,6 +22,11 @@ my $channel = "#ocaml";
 
 $ENV{PATH} = "/usr/bin:/bin";
 
+# 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.
+my $flood_lim = 0;
+
 POE::Session->create (
   package_states => [
     main => [ qw(_default _start irc_001 irc_public got_stdout got_sigchld) ],
@@ -78,11 +83,14 @@ sub irc_public
     print "got: $what\n";
     if (my ($stmt) = $what =~ /^\s*([^#].*;;)\s*$/) {
        $heap->{ocaml}->put ("$stmt\n");
+        $flood_lim = 0;
     }
     # 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 =>
+                      "hello $nick, I am xavierbot, an OCaml toplevel");
+       $kernel->post ($sender => privmsg => $channel =>
                       "$nick: expr ;;      evaluate expr in OCaml toplevel");
        $kernel->post ($sender => privmsg => $channel =>
                       "$nick: help         help message");
@@ -120,7 +128,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 < 16) {
+       $kernel->post ($heap->{irc} => privmsg => $channel => "$input");
+    }
+    $flood_lim++;
 }
 
 # Got a SIGCHLD, so start the bot up again.