Added sleep/wake up.
authorrjones <rjones>
Fri, 29 Jun 2007 21:43:21 +0000 (21:43 +0000)
committerrjones <rjones>
Fri, 29 Jun 2007 21:43:21 +0000 (21:43 +0000)
Makefile
Makefile.config
init
ocamlbotwrapper.c
ocamlbotwrapper.c.in
xavierbot.pl
xavierbot.pl.in

index 26991b6..a56955e 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -1,4 +1,4 @@
-# $Id: Makefile,v 1.3 2007/06/28 23:18:28 rjones Exp $
+# $Id: Makefile,v 1.4 2007/06/29 21:43:21 rjones Exp $
 
 include Makefile.config
 
 
 include Makefile.config
 
@@ -26,6 +26,7 @@ init: init.in Makefile.config
 xavierbot.pl: xavierbot.pl.in Makefile.config
        sed \
          -e 's|@WRAPPER@|$(WRAPPER)|' \
 xavierbot.pl: xavierbot.pl.in Makefile.config
        sed \
          -e 's|@WRAPPER@|$(WRAPPER)|' \
+         -e 's|@VERSION@|$(VERSION)|' \
          < $< > $@
        chmod 0755 $@
 
          < $< > $@
        chmod 0755 $@
 
index 63671fe..a66fbee 100644 (file)
@@ -1,8 +1,8 @@
 # -*- Makefile -*-
 # -*- Makefile -*-
-# $Id: Makefile.config,v 1.7 2007/06/29 19:39:13 rjones Exp $
+# $Id: Makefile.config,v 1.8 2007/06/29 21:43:21 rjones Exp $
 
 PACKAGE := xavierbot
 
 PACKAGE := xavierbot
-VERSION := 0.5
+VERSION := 0.6
 
 # Prefix for installation.
 # - Binaries are installed in $(PREFIX)/sbin
 
 # Prefix for installation.
 # - Binaries are installed in $(PREFIX)/sbin
diff --git a/init b/init
index 52dc690..178497f 100644 (file)
--- a/init
+++ b/init
@@ -1,5 +1,5 @@
 (* Initialise the toplevel environment. -*- tuareg -*-
 (* Initialise the toplevel environment. -*- tuareg -*-
- * $Id: init,v 1.6 2007/06/29 19:39:13 rjones Exp $
+ * $Id: init,v 1.7 2007/06/29 21:43:21 rjones Exp $
  * - Removes the Pervasives module and any dangerous functions.
  * - Loads just the modules we want to give access to, and just
  *   the functions within those modules that we want to give.
  * - Removes the Pervasives module and any dangerous functions.
  * - Loads just the modules we want to give access to, and just
  *   the functions within those modules that we want to give.
index 460bbc2..8557ed1 100644 (file)
@@ -1,5 +1,5 @@
 /* -*- C -*-
 /* -*- C -*-
- * $Id: ocamlbotwrapper.c,v 1.5 2007/06/29 19:39:13 rjones Exp $
+ * $Id: ocamlbotwrapper.c,v 1.6 2007/06/29 21:43:21 rjones Exp $
  * SUID wrapper around ocaml program.
  */
 
  * SUID wrapper around ocaml program.
  */
 
@@ -51,7 +51,7 @@ main ()
   setrlimit (RLIMIT_CORE, &lim);
 #endif
 #ifdef RLIMIT_CPU
   setrlimit (RLIMIT_CORE, &lim);
 #endif
 #ifdef RLIMIT_CPU
-  lim.rlim_cur = lim.rlim_max = 60; /* seconds */
+  lim.rlim_cur = lim.rlim_max = 10; /* seconds */
   setrlimit (RLIMIT_CPU, &lim);
 #endif
 #ifdef RLIMIT_MEMLOCK
   setrlimit (RLIMIT_CPU, &lim);
 #endif
 #ifdef RLIMIT_MEMLOCK
index 2909182..4835b3a 100644 (file)
@@ -1,5 +1,5 @@
 /* -*- C -*-
 /* -*- C -*-
- * $Id: ocamlbotwrapper.c.in,v 1.3 2007/06/28 23:18:28 rjones Exp $
+ * $Id: ocamlbotwrapper.c.in,v 1.4 2007/06/29 21:43:21 rjones Exp $
  * SUID wrapper around ocaml program.
  */
 
  * SUID wrapper around ocaml program.
  */
 
@@ -51,7 +51,7 @@ main ()
   setrlimit (RLIMIT_CORE, &lim);
 #endif
 #ifdef RLIMIT_CPU
   setrlimit (RLIMIT_CORE, &lim);
 #endif
 #ifdef RLIMIT_CPU
-  lim.rlim_cur = lim.rlim_max = 60; /* seconds */
+  lim.rlim_cur = lim.rlim_max = 10; /* seconds */
   setrlimit (RLIMIT_CPU, &lim);
 #endif
 #ifdef RLIMIT_MEMLOCK
   setrlimit (RLIMIT_CPU, &lim);
 #endif
 #ifdef RLIMIT_MEMLOCK
index 4650f0d..a621a4e 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.
 # 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.8 2007/06/29 19:39:13 rjones Exp $
+# $Id: xavierbot.pl,v 1.9 2007/06/29 21:43:21 rjones Exp $
 
 use strict;
 use POE qw(Component::IRC Wheel::Run);
 
 use strict;
 use POE qw(Component::IRC Wheel::Run);
@@ -12,21 +12,28 @@ use POE qw(Component::IRC Wheel::Run);
 
 my $nick = "xavierbot";
 my $ircname = "Xavierbot";             # Printable name.
 
 my $nick = "xavierbot";
 my $ircname = "Xavierbot";             # Printable name.
-my $server = "chat.freenode.net";
-#my $server = "devserv.devel.redhat.com";
+#my $server = "chat.freenode.net";
+my $server = "devserv.devel.redhat.com";
 my $port = 6667;
 my $channel = "#ocaml";
 
 # End of configuration.
 #----------------------------------------------------------------------
 my $port = 6667;
 my $channel = "#ocaml";
 
 # End of configuration.
 #----------------------------------------------------------------------
-
-$ENV{PATH} = "/usr/bin:/bin";
+# Current state.
 
 # 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.
 
 # 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;
 
 my $flood_lim = 0;
 
+# Are we awake or sleeping?
+my $sleeping = 0;
+
+#----------------------------------------------------------------------
+
+$ENV{PATH} = "/usr/bin:/bin";
+
 POE::Session->create (
   package_states => [
     main => [ qw(_default _start irc_001 irc_public got_stdout got_sigchld) ],
 POE::Session->create (
   package_states => [
     main => [ qw(_default _start irc_001 irc_public got_stdout got_sigchld) ],
@@ -80,26 +87,45 @@ sub irc_public
     my $nick = (split /!/, $who)[0];
     my $channel = $where->[0];
 
     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";
     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?
     # 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 =>
        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");
-       $kernel->post ($sender => privmsg => $channel =>
-                      "$nick: restart      restart the toplevel");
+           "hello $nick, I am xavierbot 0.6, an OCaml toplevel");
+       $kernel->post ($sender => privmsg => $channel => $_)
+           foreach (@usage);
     }
     }
-    elsif ($what =~ /^\s*xavierbot\b.*\brestart\b/) {
-       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");
+       }
+       elsif ($what =~ /^\s*xavierbot\b.*\brestart\b/) {
+           $sleeping = 0;
+           print STDOUT "got instruction to restart ...\n";
+           restart_toplevel ($heap->{ocaml});
+       }
     }
     undef;
 }
     }
     undef;
 }
@@ -128,7 +154,7 @@ sub got_stdout
 {
     my ($kernel,$heap, $input, $wheel_id) = @_[KERNEL,HEAP,ARG0,ARG1];
     print "Child said: $input\n";
 {
     my ($kernel,$heap, $input, $wheel_id) = @_[KERNEL,HEAP,ARG0,ARG1];
     print "Child said: $input\n";
-    if ($flood_lim < 16) {
+    if ($flood_lim < 10) {
        $kernel->post ($heap->{irc} => privmsg => $channel => "$input");
     }
     $flood_lim++;
        $kernel->post ($heap->{irc} => privmsg => $channel => "$input");
     }
     $flood_lim++;
index fa59d84..76258c6 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.
 # 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.5 2007/06/29 19:39:13 rjones Exp $
+# $Id: xavierbot.pl.in,v 1.6 2007/06/29 21:43:21 rjones Exp $
 
 use strict;
 use POE qw(Component::IRC Wheel::Run);
 
 use strict;
 use POE qw(Component::IRC Wheel::Run);
@@ -12,21 +12,28 @@ use POE qw(Component::IRC Wheel::Run);
 
 my $nick = "xavierbot";
 my $ircname = "Xavierbot";             # Printable name.
 
 my $nick = "xavierbot";
 my $ircname = "Xavierbot";             # Printable name.
-my $server = "chat.freenode.net";
-#my $server = "devserv.devel.redhat.com";
+#my $server = "chat.freenode.net";
+my $server = "devserv.devel.redhat.com";
 my $port = 6667;
 my $channel = "#ocaml";
 
 # End of configuration.
 #----------------------------------------------------------------------
 my $port = 6667;
 my $channel = "#ocaml";
 
 # End of configuration.
 #----------------------------------------------------------------------
-
-$ENV{PATH} = "/usr/bin:/bin";
+# Current state.
 
 # 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.
 
 # 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;
 
 my $flood_lim = 0;
 
+# Are we awake or sleeping?
+my $sleeping = 0;
+
+#----------------------------------------------------------------------
+
+$ENV{PATH} = "/usr/bin:/bin";
+
 POE::Session->create (
   package_states => [
     main => [ qw(_default _start irc_001 irc_public got_stdout got_sigchld) ],
 POE::Session->create (
   package_states => [
     main => [ qw(_default _start irc_001 irc_public got_stdout got_sigchld) ],
@@ -80,26 +87,45 @@ sub irc_public
     my $nick = (split /!/, $who)[0];
     my $channel = $where->[0];
 
     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";
     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?
     # 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 =>
        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");
-       $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/) {
-       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");
+       }
+       elsif ($what =~ /^\s*xavierbot\b.*\brestart\b/) {
+           $sleeping = 0;
+           print STDOUT "got instruction to restart ...\n";
+           restart_toplevel ($heap->{ocaml});
+       }
     }
     undef;
 }
     }
     undef;
 }
@@ -128,7 +154,7 @@ sub got_stdout
 {
     my ($kernel,$heap, $input, $wheel_id) = @_[KERNEL,HEAP,ARG0,ARG1];
     print "Child said: $input\n";
 {
     my ($kernel,$heap, $input, $wheel_id) = @_[KERNEL,HEAP,ARG0,ARG1];
     print "Child said: $input\n";
-    if ($flood_lim < 16) {
+    if ($flood_lim < 10) {
        $kernel->post ($heap->{irc} => privmsg => $channel => "$input");
     }
     $flood_lim++;
        $kernel->post ($heap->{irc} => privmsg => $channel => "$input");
     }
     $flood_lim++;