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.8 2007/07/05 12:43:14 rjones Exp $
8 use POE qw(Component::IRC Wheel::Run);
11 #----------------------------------------------------------------------
12 # Start of configuration.
14 my $nick = "xavierbot";
15 my $ircname = "Xavierbot"; # Printable name.
16 my $server = "chat.freenode.net";
18 my $channel = "#ocaml";
20 # End of configuration.
21 #----------------------------------------------------------------------
23 # Command line args can override configuration.
24 GetOptions ("nick=s" => \$nick,
25 "ircname=s" => \$ircname,
26 "server=s" => \$server,
28 "channel=s" => \$channel)
29 or die "$0: GetOptions: $!";
31 # Simple flood protection. This counts number of lines received from
32 # the toplevel, and is reset when we send a line. If this exceeds
33 # some value, then we just eat lines.
34 # XXX This ought to count characters, not lines.
37 # Are we awake or sleeping?
40 #----------------------------------------------------------------------
42 $ENV{PATH} = "/usr/bin:/bin";
44 POE::Session->create (
46 main => [ qw(_default _start irc_001 irc_public got_stdout got_sigchld) ],
55 my ($kernel, $heap) = @_[KERNEL,HEAP];
57 my $irc = POE::Component::IRC->spawn
63 ) or die "POE::Component::IRC->spawn failed: $!";
65 my $ocaml = start_toplevel ();
67 $kernel->sig(CHLD => qw(got_sigchld));
70 $heap->{ocaml} = $ocaml;
72 my $irc_session = $heap->{irc}->session_id ();
73 $kernel->post ($irc_session => register => "all");
74 $kernel->post ($irc_session => connect => { });
81 my ($kernel, $sender) = @_[KERNEL,SENDER];
83 my $poco_object = $sender->get_heap ();
84 print "Connected to ", $poco_object->server_name (), "\n";
86 $kernel->post ($sender => join => $channel);
92 my ($kernel, $sender, $who, $where, $what, $heap) =
93 @_[KERNEL,SENDER,ARG0,ARG1,ARG2,HEAP];
94 my $nick = (split /!/, $who)[0];
95 my $channel = $where->[0];
99 "expr ;; evaluate expr in toplevel and print result",
101 "restart restart the toplevel",
103 "wake wake me up from sleep",
106 print "got: $what\n";
107 # XXX How to interpolate $nick into the patterns?
108 if ($what =~ /^\s*xavierbot\b.*\bhelp\b/) {
109 my $nick = (split /!/, $who)[0];
110 $kernel->post ($sender => privmsg => $channel =>
111 "hello $nick, I am xavierbot @VERSION@, an OCaml toplevel");
112 $kernel->post ($sender => privmsg => $channel => $_)
116 if (my ($stmt) = $what =~ m/^\s*([^\#].*;;)\s*$/) {
117 $heap->{ocaml}->put ("$stmt\n");
120 elsif ($what =~ /^\s*xavierbot\b.*\b(sleep|shut|quiet)\b/) {
122 $kernel->post ($sender => privmsg => $channel =>
123 "xavierbot goes to sleep (do 'xavierbot wake' to wake)");
126 if ($what =~ /^\s*xavierbot\b.*\bwake\b/) {
128 $kernel->post ($sender => privmsg => $channel =>
129 "xavierbot wakes up");
131 elsif ($what =~ /^\s*xavierbot\b.*\brestart\b/) {
133 print STDOUT "got instruction to restart ...\n";
134 restart_toplevel ($heap->{ocaml});
142 my ($event, $args) = @_[ARG0 .. $#_];
143 my @output = ("$event: ");
145 foreach my $arg (@$args) {
146 if (ref ($arg) eq "ARRAY") {
147 push @output, "[" . join (" ,", @$arg) . "]";
149 push @output, "'$arg'";
152 print STDOUT join " ", @output, "\n";
156 #----------------------------------------------------------------------
158 # Toplevel wrote something.
162 my ($kernel,$heap, $input, $wheel_id) = @_[KERNEL,HEAP,ARG0,ARG1];
163 print "Child said: $input\n";
164 if ($flood_lim < 10) {
165 $kernel->post ($heap->{irc} => privmsg => $channel => "$input");
170 # Got a SIGCHLD, so start the bot up again.
174 my ($kernel, $heap) = @_[KERNEL,HEAP];
175 my $ocaml = start_toplevel ();
176 $heap->{ocaml} = $ocaml;
179 # Start up the toplevel (assumes it's not running).
183 return POE::Wheel::Run->new
185 Program => "@WRAPPER@",
186 StdoutEvent => "got_stdout",
187 StderrEvent => "got_stdout",
188 ) or die "POE::Wheel::Run->new @WRAPPER@ failed: $!";
191 # Restart the toplevel - kill the old one, and a new one
192 # will be spawned after we get the SIGCHLD signal.
194 # XXX Can't send signal to setuid child, so instead just close
202 $ocaml->shutdown_stdin;