2 # xavierbot : an OCaml interpreter IRC bot.
3 # By Richard W.M. Jones <rich@annexia.org>.
4 # This code is in the Public Domain.
5 # $Id: xavierbot.pl.in,v 1.7 2007/06/29 21:44:02 rjones Exp $
8 use POE qw(Component::IRC Wheel::Run);
10 #----------------------------------------------------------------------
11 # Start of configuration.
13 my $nick = "xavierbot";
14 my $ircname = "Xavierbot"; # Printable name.
15 my $server = "chat.freenode.net";
16 #my $server = "devserv.devel.redhat.com";
18 my $channel = "#ocaml";
20 # End of configuration.
21 #----------------------------------------------------------------------
24 # Simple flood protection. This counts number of lines received from
25 # the toplevel, and is reset when we send a line. If this exceeds
26 # some value, then we just eat lines.
27 # XXX This ought to count characters, not lines.
30 # Are we awake or sleeping?
33 #----------------------------------------------------------------------
35 $ENV{PATH} = "/usr/bin:/bin";
37 POE::Session->create (
39 main => [ qw(_default _start irc_001 irc_public got_stdout got_sigchld) ],
48 my ($kernel, $heap) = @_[KERNEL,HEAP];
50 my $irc = POE::Component::IRC->spawn
56 ) or die "POE::Component::IRC->spawn failed: $!";
58 my $ocaml = start_toplevel ();
60 $kernel->sig(CHLD => qw(got_sigchld));
63 $heap->{ocaml} = $ocaml;
65 my $irc_session = $heap->{irc}->session_id ();
66 $kernel->post ($irc_session => register => "all");
67 $kernel->post ($irc_session => connect => { });
74 my ($kernel, $sender) = @_[KERNEL,SENDER];
76 my $poco_object = $sender->get_heap ();
77 print "Connected to ", $poco_object->server_name (), "\n";
79 $kernel->post ($sender => join => $channel);
85 my ($kernel, $sender, $who, $where, $what, $heap) =
86 @_[KERNEL,SENDER,ARG0,ARG1,ARG2,HEAP];
87 my $nick = (split /!/, $who)[0];
88 my $channel = $where->[0];
92 "expr ;; evaluate expr in toplevel and print result",
94 "restart restart the toplevel",
96 "wake wake me up from sleep",
100 # XXX How to interpolate $nick into the patterns?
101 if ($what =~ /^\s*xavierbot\b.*\bhelp\b/) {
102 my $nick = (split /!/, $who)[0];
103 $kernel->post ($sender => privmsg => $channel =>
104 "hello $nick, I am xavierbot @VERSION@, an OCaml toplevel");
105 $kernel->post ($sender => privmsg => $channel => $_)
109 if (my ($stmt) = $what =~ m/^\s*([^\#].*;;)\s*$/) {
110 $heap->{ocaml}->put ("$stmt\n");
113 elsif ($what =~ /^\s*xavierbot\b.*\b(sleep|shut|quiet)\b/) {
115 $kernel->post ($sender => privmsg => $channel =>
116 "xavierbot goes to sleep (do 'xavierbot wake' to wake)");
119 if ($what =~ /^\s*xavierbot\b.*\bwake\b/) {
121 $kernel->post ($sender => privmsg => $channel =>
122 "xavierbot wakes up");
124 elsif ($what =~ /^\s*xavierbot\b.*\brestart\b/) {
126 print STDOUT "got instruction to restart ...\n";
127 restart_toplevel ($heap->{ocaml});
135 my ($event, $args) = @_[ARG0 .. $#_];
136 my @output = ("$event: ");
138 foreach my $arg (@$args) {
139 if (ref ($arg) eq "ARRAY") {
140 push @output, "[" . join (" ,", @$arg) . "]";
142 push @output, "'$arg'";
145 print STDOUT join " ", @output, "\n";
149 #----------------------------------------------------------------------
151 # Toplevel wrote something.
155 my ($kernel,$heap, $input, $wheel_id) = @_[KERNEL,HEAP,ARG0,ARG1];
156 print "Child said: $input\n";
157 if ($flood_lim < 10) {
158 $kernel->post ($heap->{irc} => privmsg => $channel => "$input");
163 # Got a SIGCHLD, so start the bot up again.
167 my ($kernel, $heap) = @_[KERNEL,HEAP];
168 my $ocaml = start_toplevel ();
169 $heap->{ocaml} = $ocaml;
172 # Start up the toplevel (assumes it's not running).
176 return POE::Wheel::Run->new
178 Program => "@WRAPPER@",
179 StdoutEvent => "got_stdout",
180 StderrEvent => "got_stdout",
181 ) or die "POE::Wheel::Run->new @WRAPPER@ failed: $!";
184 # Restart the toplevel - kill the old one, and a new one
185 # will be spawned after we get the SIGCHLD signal.
187 # XXX Can't send signal to setuid child, so instead just close
195 $ocaml->shutdown_stdin;