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.10 2010/04/04 19:38:40 rjones Exp $
8 use POE qw(Component::IRC Wheel::Run);
10 use POE::Component::IRC::Plugin::NickServID;
12 #----------------------------------------------------------------------
13 # Start of configuration.
15 my $nick = "xavierbot";
16 my $ircname = "Xavierbot"; # Printable name.
17 my $server = "chat.freenode.net";
19 my $channel = "#ocaml";
21 my $nickservpw = "123456"; # or undef
23 # End of configuration.
24 #----------------------------------------------------------------------
26 # Command line args can override configuration.
27 GetOptions ("nick=s" => \$nick,
28 "ircname=s" => \$ircname,
29 "server=s" => \$server,
31 "channel=s" => \$channel)
32 or die "$0: GetOptions: $!";
34 # Simple flood protection. This counts number of lines received from
35 # the toplevel, and is reset when we send a line. If this exceeds
36 # some value, then we just eat lines.
37 # XXX This ought to count characters, not lines.
40 # Are we awake or sleeping?
43 #----------------------------------------------------------------------
45 $ENV{PATH} = "/usr/bin:/bin";
47 POE::Session->create (
49 main => [ qw(_default _start irc_001 irc_public got_stdout got_sigchld) ],
58 my ($kernel, $heap) = @_[KERNEL,HEAP];
60 my $irc = POE::Component::IRC->spawn
66 ) or die "POE::Component::IRC->spawn failed: $!";
68 $irc->plugin_add('NickServID',
69 POE::Component::IRC::Plugin::NickServID->new(
70 Password => $nickservpw,
73 my $ocaml = start_toplevel ();
75 $kernel->sig(CHLD => qw(got_sigchld));
78 $heap->{ocaml} = $ocaml;
80 my $irc_session = $heap->{irc}->session_id ();
81 $kernel->post ($irc_session => register => "all");
82 $kernel->post ($irc_session => connect => { });
89 my ($kernel, $sender) = @_[KERNEL,SENDER];
91 my $poco_object = $sender->get_heap ();
92 print "Connected to ", $poco_object->server_name (), "\n";
94 $kernel->post ($sender => join => $channel);
100 my ($kernel, $sender, $who, $where, $what, $heap) =
101 @_[KERNEL,SENDER,ARG0,ARG1,ARG2,HEAP];
102 my $nick = (split /!/, $who)[0];
103 my $channel = $where->[0];
107 "expr ;; evaluate expr in toplevel and print result",
109 "restart restart the toplevel",
111 "wake wake me up from sleep",
114 print "got: $what\n";
115 # XXX How to interpolate $nick into the patterns?
116 if ($what =~ /^\s*xavierbot\b.*\bhelp\b/) {
117 my $nick = (split /!/, $who)[0];
118 $kernel->post ($sender => privmsg => $channel =>
119 "hello $nick, I am xavierbot @VERSION@, an OCaml toplevel");
120 $kernel->post ($sender => privmsg => $channel => $_)
123 elsif ($what =~ /^\s*xavierbot\b.*\brestart\b/) {
125 print STDOUT "got instruction to restart ...\n";
126 restart_toplevel ($heap->{ocaml});
129 if (my ($stmt) = $what =~ m/^\s*([^\#].*;;)\s*$/) {
130 $heap->{ocaml}->put ("$stmt\n");
133 elsif ($what =~ /^\s*xavierbot\b.*\b(sleep|shut|quiet)\b/) {
135 $kernel->post ($sender => privmsg => $channel =>
136 "xavierbot goes to sleep (do 'xavierbot wake' to wake)");
139 if ($what =~ /^\s*xavierbot\b.*\bwake\b/) {
141 $kernel->post ($sender => privmsg => $channel =>
142 "xavierbot wakes up");
150 my ($event, $args) = @_[ARG0 .. $#_];
151 my @output = ("$event: ");
153 foreach my $arg (@$args) {
154 if (ref ($arg) eq "ARRAY") {
155 push @output, "[" . join (" ,", @$arg) . "]";
157 push @output, "'$arg'";
160 print STDOUT join " ", @output, "\n";
164 #----------------------------------------------------------------------
166 # Toplevel wrote something.
170 my ($kernel,$heap, $input, $wheel_id) = @_[KERNEL,HEAP,ARG0,ARG1];
171 print "Child said: $input\n";
172 if ($flood_lim < 10) {
173 $kernel->post ($heap->{irc} => privmsg => $channel => "$input");
178 # Got a SIGCHLD, so start the bot up again.
182 my ($kernel, $heap) = @_[KERNEL,HEAP];
183 my $ocaml = start_toplevel ();
184 $heap->{ocaml} = $ocaml;
187 # Start up the toplevel (assumes it's not running).
191 return POE::Wheel::Run->new
193 Program => "@WRAPPER@",
194 StdoutEvent => "got_stdout",
195 StderrEvent => "got_stdout",
196 ) or die "POE::Wheel::Run->new @WRAPPER@ failed: $!";
199 # Restart the toplevel - kill the old one, and a new one
200 # will be spawned after we get the SIGCHLD signal.
202 # XXX Can't send signal to setuid child, so instead just close
210 $ocaml->shutdown_stdin;