pppmbot

Steve Smythe ssmythe at docent.com
Fri Sep 26 14:18:40 CDT 2003


I wrote a quick and simple POE::IRC bot that logs messages
up on the irc.community.tummy.com.  Anyone want to extend
it to automatically email the logs on a daily basis?  :-)

POE and POE::Component::IRC are available through PPM
for ActiveState users...

Enjoy!

Steve





#!c:/perl/bin/perl -w
############################################################################
##
# This is a simple IRC bot that just logs public messages.
############################################################################
##

use warnings;
use strict;

use POE;
use POE::Component::IRC;


############################################################################
##
# Init
############################################################################
##
sub Init {
	# Create the component that will represent an IRC network.
	POE::Component::IRC->new("magnet");

	# Create the bot session.  The new() call specifies the events the
bot
	# knows about and the functions that will handle those events.
	POE::Session->new
	  ( _start => \&bot_start,
		irc_001    => \&on_connect,
		irc_public => \&on_public,
	  );
}


############################################################################
##
# CHANNEL
############################################################################
##
sub CHANNEL () {
	"#test"
}


############################################################################
##
# bot_start
#
# The bot session has started.  Register this bot with the "magnet"
# IRC component.  Select a nickname.  Connect to a server.
############################################################################
##
sub bot_start {
    my $kernel  = $_[KERNEL];
    my $heap    = $_[HEAP];
    my $session = $_[SESSION];

    $kernel->post( magnet => register => "all" );

#    my $nick = 'usepoe' . $$ % 1000;
    my $nick = "pppmbot";
    $kernel->post( magnet => connect =>
          { Nick => $nick,
            Username => 'pppmbot',
            Ircname  => 'pppmbot',
            Server   => 'irc.community.tummy.com',
            Port     => '6667',
          }
    );
}

############################################################################
##
# on_connect
#
# The bot has successfully connected to a server.  Join a channel.
############################################################################
##
sub on_connect {
    $_[KERNEL]->post( magnet => join => CHANNEL );
}


############################################################################
##
# on_public
#
# The bot has received a public message.  Parse it for commands, and
# respond to interesting things.
############################################################################
##
sub on_public {
    my ( $kernel, $who, $where, $msg ) = @_[ KERNEL, ARG0, ARG1, ARG2 ];
    my $nick = ( split /!/, $who )[0];
    my $channel = $where->[0];

    my $ts = scalar localtime;
    print " [$ts] <$nick:$channel> $msg\n";
    
    my $file="c:/tmp/pppmbot.log";
    open(FILE, ">>$file") || die "can't open file \"$file\" for write: $!";
    print FILE " [$ts] <$nick:$channel> $msg\n";
    close(FILE) || die "can't close file \"$file\" for write: $!";

    if ( my ($response) = $msg =~ /^pppmbot (.+)/i ) {
        $response = "Hello from pppmbot";

        # Send a response back to the server.
        $kernel->post( magnet => privmsg => CHANNEL, $response );

		# Log response
	    $ts = scalar localtime;
	    $nick="pppmbot";
		open(FILE, ">>$file") || die "can't open file \"$file\" for
write: $!";
		print FILE " [$ts] <$nick:$channel> $response\n";
		close(FILE) || die "can't close file \"$file\" for write:
$!";

    }
}


############################################################################
##
# Process
############################################################################
##
sub Process {
	# Run the bot until it is done.
	$poe_kernel->run();
	exit 0;
}


############################################################################
##
# MAIN MAIN MAIN MAIN MAIN MAIN MAIN MAIN MAIN MAIN MAIN MAIN MAIN MAIN MAIN
#
############################################################################
##
Init();
Process();




More information about the Pikes-peak-pm mailing list