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,v 1.4 2007/06/28 20:49:10 rjones Exp $
9 use POE qw(Component::IRC Wheel::Run);
11 $ENV{PATH} = "/usr/bin:/bin";
13 #----------------------------------------------------------------------
14 # Start of configuration.
16 my $nick = "xavierbot";
17 my $ircname = "Xavierbot"; # Printable name.
18 my $server = "chat.freenode.net";
19 #my $server = "devserv.devel.redhat.com";
21 my $channel = "#ocaml";
23 # End of configuration.
24 #----------------------------------------------------------------------
26 POE::Session->create (
28 main => [ qw(_default _start irc_001 irc_public got_stdout) ],
37 my ($kernel, $heap) = @_[KERNEL,HEAP];
39 my $irc = POE::Component::IRC->spawn
45 ) or die "POE::Component::IRC->spawn failed: $!";
47 my $ocaml = POE::Wheel::Run->new
49 Program => "./ocamlbotwrapper",
50 StdoutEvent => "got_stdout",
51 StderrEvent => "got_stdout",
52 ) or die "POE::Wheel::Run->new ./ocamlbotwrapper failed: $!";
55 $heap->{ocaml} = $ocaml;
57 my $irc_session = $heap->{irc}->session_id ();
58 $kernel->post ($irc_session => register => "all");
59 $kernel->post ($irc_session => connect => { });
66 my ($kernel, $sender) = @_[KERNEL,SENDER];
68 my $poco_object = $sender->get_heap ();
69 print "Connected to ", $poco_object->server_name (), "\n";
71 $kernel->post ($sender => join => $channel);
77 my ($kernel, $sender, $who, $where, $what, $heap) =
78 @_[KERNEL,SENDER,ARG0,ARG1,ARG2,HEAP];
79 my $nick = (split /!/, $who)[0];
80 my $channel = $where->[0];
83 if (my ($stmt) = $what =~ /^\s*([^#].*;;)\s*$/) {
84 print "stmt = $stmt\n";
85 $heap->{ocaml}->put ("$stmt\n");
92 my ($event, $args) = @_[ARG0 .. $#_];
93 my @output = ("$event: ");
95 foreach my $arg (@$args) {
96 if (ref ($arg) eq "ARRAY") {
97 push @output, "[" . join (" ,", @$arg) . "]";
99 push @output, "'$arg'";
102 print STDOUT join " ", @output, "\n";
106 #----------------------------------------------------------------------
108 # Bot wrote something.
112 my ($kernel,$heap, $input, $wheel_id) = @_[KERNEL,HEAP,ARG0,ARG1];
113 print "Child said: $input\n";
114 $kernel->post ($heap->{irc} => privmsg => $channel => "$input");