#!/usr/bin/perl -w # PogoBOT IRC module # Author: Tim Retout # Date Created: 2005-09-25 # use strict; use Net::IRC; use IO::Socket::INET; my $Server = 'irc.uwcs.co.uk'; my $Port = '6667'; my @Channels = ('#hop'); my $Nick = 'PogoBOT'; my $Ircname = 'Choob Killer'; my $irc = new Net::IRC; my $conn = $irc->newconn( Nick => $Nick, Server => $Server, Port => $Port, Ircname => $Ircname, ) or die "Can't connect to IRC server.\n"; ####################### # Core Communications # ####################### # This section should be made object-oriented, similar to Net::IRC::Connection. # Most of it shouldn't be here. # Create a UNIX socket to listen for messages from the core. my $FH = new IO::Socket::INET( PeerAddr => 'localhost', PeerPort => 3009, Proto => 'tcp', ); # Subroutine to handle messages from the core. sub handle_coreconn { my $socket = shift; die "Connection lost.\n" unless (defined($socket)); my $msg = <$socket>; print "Message from core: $msg"; if ($msg =~ s/^([^ ]*) ([^ ]*) ([^ ]*) //) { my $dest1 = $2; my $NICK = $3; if ($1 eq "$Nick\@$Server") { $msg =~ s/^\$NICK/$NICK/; $msg =~ s/([^\\])\$NICK/$1$NICK/g; if ($msg =~ s/^\/me //) { $conn->ctcp('action', $dest1, $msg); } else { $conn->privmsg($dest1, $msg); } } } } # Add the filehandle into the select() loop. $irc->addfh( $FH, \&handle_coreconn, 'r', undef ); ########################### # Add connection handlers # ########################### # See Net::IRC::Event for details of events to respond to. # Handlers, in asciibetical order. $conn->add_handler('msg', \&on_msg); # Private message $conn->add_handler('public', \&on_public); # Public message $conn->add_handler('invite', \&on_invite); # Invite $conn->add_handler('join', \&on_join); # Global handlers, in asciibetical order. $conn->add_global_handler(376, \&on_connect); # Connected to server # Set the bot running. $irc->start; exit; sub sanitise_text { my ($text, $dest1, $dest2, @args) = @_; $dest1 =~ s/\!.*//; # Remove hostnames. $dest2 =~ s/\!.*//; $text =~ s/\t/\\t/g; # Switch all tabs for a literal \t sequence. $text .= "\t$Nick\@$Server " . join(' ', $dest1, $dest2, @args) . "\n"; } ####################### # Handler subroutines # ####################### # Read the source of Net::IRC::Connection for IRC-related actions. # Keep the handler subroutines in alphabetical order. # On connecting to the server, set mode +B and join the desired channels. sub on_connect { my $self = shift; $self->mode($Nick, '+B'); for (qw( JinglyBOT BuckoBOT Choobie| )) { $self->ignore('all', $_); print "Ignoring $_...\n"; } print $FH "REG $Nick\@$Server\r\n"; } sub on_invite { my ($self, $event) = @_; my $channel = @{$event->{'args'}}[0]; $self->join($channel); print "Joining $channel\n"; } sub on_join { my ($self, $event) = @_; (my $From = $event->from) =~ s/\!.*//; if ($From eq $Nick) { $conn->privmsg($event->to->[0], "Bouncy, bouncy! Choob fts."); } else { print $FH 'JOIN ', sanitise_text('', $event->to->[0], $event->from); } } # On receiving a private message. sub on_msg { my ($self, $event) = @_; print $FH 'MSG ', sanitise_text($event->args, $event->from, $event->from); } # On receiving a public message. sub on_public { my ($self, $event) = @_; print $FH 'MSG ', sanitise_text($event->args, $event->to->[0], $event->from); }