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.5 2007/06/29 19:39:13 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 #----------------------------------------------------------------------
23 $ENV{PATH} = "/usr/bin:/bin";
25 # Simple flood protection. This counts number of lines received from
26 # the toplevel, and is reset when we send a line. If this exceeds
27 # some value, then we just eat lines.
30 POE::Session->create (
32 main => [ qw(_default _start irc_001 irc_public got_stdout got_sigchld) ],
41 my ($kernel, $heap) = @_[KERNEL,HEAP];
43 my $irc = POE::Component::IRC->spawn
49 ) or die "POE::Component::IRC->spawn failed: $!";
51 my $ocaml = start_toplevel ();
53 $kernel->sig(CHLD => qw(got_sigchld));
56 $heap->{ocaml} = $ocaml;
58 my $irc_session = $heap->{irc}->session_id ();
59 $kernel->post ($irc_session => register => "all");
60 $kernel->post ($irc_session => connect => { });
67 my ($kernel, $sender) = @_[KERNEL,SENDER];
69 my $poco_object = $sender->get_heap ();
70 print "Connected to ", $poco_object->server_name (), "\n";
72 $kernel->post ($sender => join => $channel);
78 my ($kernel, $sender, $who, $where, $what, $heap) =
79 @_[KERNEL,SENDER,ARG0,ARG1,ARG2,HEAP];
80 my $nick = (split /!/, $who)[0];
81 my $channel = $where->[0];
84 if (my ($stmt) = $what =~ /^\s*([^#].*;;)\s*$/) {
85 $heap->{ocaml}->put ("$stmt\n");
88 # XXX How to interpolate $nick into the patterns?
89 elsif ($what =~ /^\s*xavierbot\b.*\bhelp\b/) {
90 my $nick = (split /!/, $who)[0];
91 $kernel->post ($sender => privmsg => $channel =>
92 "hello $nick, I am xavierbot, an OCaml toplevel");
93 $kernel->post ($sender => privmsg => $channel =>
94 "$nick: expr ;; evaluate expr in OCaml toplevel");
95 $kernel->post ($sender => privmsg => $channel =>
96 "$nick: help help message");
97 $kernel->post ($sender => privmsg => $channel =>
98 "$nick: restart restart the toplevel");
100 elsif ($what =~ /^\s*xavierbot\b.*\brestart\b/) {
101 print STDOUT "got instruction to restart ...\n";
102 restart_toplevel ($heap->{ocaml});
109 my ($event, $args) = @_[ARG0 .. $#_];
110 my @output = ("$event: ");
112 foreach my $arg (@$args) {
113 if (ref ($arg) eq "ARRAY") {
114 push @output, "[" . join (" ,", @$arg) . "]";
116 push @output, "'$arg'";
119 print STDOUT join " ", @output, "\n";
123 #----------------------------------------------------------------------
125 # Toplevel wrote something.
129 my ($kernel,$heap, $input, $wheel_id) = @_[KERNEL,HEAP,ARG0,ARG1];
130 print "Child said: $input\n";
131 if ($flood_lim < 16) {
132 $kernel->post ($heap->{irc} => privmsg => $channel => "$input");
137 # Got a SIGCHLD, so start the bot up again.
141 my ($kernel, $heap) = @_[KERNEL,HEAP];
142 my $ocaml = start_toplevel ();
143 $heap->{ocaml} = $ocaml;
146 # Start up the toplevel (assumes it's not running).
150 return POE::Wheel::Run->new
152 Program => "@WRAPPER@",
153 StdoutEvent => "got_stdout",
154 StderrEvent => "got_stdout",
155 ) or die "POE::Wheel::Run->new @WRAPPER@ failed: $!";
158 # Restart the toplevel - kill the old one, and a new one
159 # will be spawned after we get the SIGCHLD signal.
161 # XXX Can't send signal to setuid child, so instead just close
169 $ocaml->shutdown_stdin;