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.3 2007/06/28 23:18:28 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 POE::Session->create (
27 main => [ qw(_default _start irc_001 irc_public got_stdout) ],
36 my ($kernel, $heap) = @_[KERNEL,HEAP];
38 my $irc = POE::Component::IRC->spawn
44 ) or die "POE::Component::IRC->spawn failed: $!";
46 my $ocaml = POE::Wheel::Run->new
48 Program => "@WRAPPER@",
49 StdoutEvent => "got_stdout",
50 StderrEvent => "got_stdout",
51 ) or die "POE::Wheel::Run->new @WRAPPER@ failed: $!";
54 $heap->{ocaml} = $ocaml;
56 my $irc_session = $heap->{irc}->session_id ();
57 $kernel->post ($irc_session => register => "all");
58 $kernel->post ($irc_session => connect => { });
65 my ($kernel, $sender) = @_[KERNEL,SENDER];
67 my $poco_object = $sender->get_heap ();
68 print "Connected to ", $poco_object->server_name (), "\n";
70 $kernel->post ($sender => join => $channel);
76 my ($kernel, $sender, $who, $where, $what, $heap) =
77 @_[KERNEL,SENDER,ARG0,ARG1,ARG2,HEAP];
78 my $nick = (split /!/, $who)[0];
79 my $channel = $where->[0];
82 if (my ($stmt) = $what =~ /^\s*([^#].*;;)\s*$/) {
83 $heap->{ocaml}->put ("$stmt\n");
85 elsif ($what =~ /$nick.*restart/) {
86 print STDOUT "got instruction to restart ...\n"
93 my ($event, $args) = @_[ARG0 .. $#_];
94 my @output = ("$event: ");
96 foreach my $arg (@$args) {
97 if (ref ($arg) eq "ARRAY") {
98 push @output, "[" . join (" ,", @$arg) . "]";
100 push @output, "'$arg'";
103 print STDOUT join " ", @output, "\n";
107 #----------------------------------------------------------------------
109 # Bot wrote something.
113 my ($kernel,$heap, $input, $wheel_id) = @_[KERNEL,HEAP,ARG0,ARG1];
114 print "Child said: $input\n";
115 $kernel->post ($heap->{irc} => privmsg => $channel => "$input");