POE Component IRC Bot

By Caaz on May 05, 2010

This is if you want to make a simple Perl IRC bot. You need to get POE::Component things if you want to use it. I also loaded the thing up with comments, and a couple commands just to let you know what goes on. Hopefully I didn't screw anything up, my first post.

#!/usr/bin/perl
use POE;
use POE::Component::IRC;
my $V = "1.0";
my $MyChannel = "#MyChannel";
my ($irc) = POE::Component::IRC->spawn();
POE::Session->create(
inline_states => {
    _start     => \&bot_start,
    irc_001    => \&on_connect, # Once you're connected, it goes to the sub, on_connect. check the CPan to see more of these kind of thingies
    irc_public => \&ChannelMsg, # Channel messages...
    irc_ctcp_action => \&ChannelAction,
    irc_notice => \&Notice,
    irc_msg => \&Notice, # Private messages go to Notice too, because it's basically the same thing, and it's easy this way.
    irc_invite => sub {
        my ($kernel, $who, $Where) = @_[KERNEL, ARG0, ARG1];
        my $UserNick    = (split /!/, $who)[0]; # Gets the nick...
        my $UserHost    = (split /!/, $who)[1]; # And the Host of the user.
        $irc->yield(join => $Where);
    },
    irc_disconnected => sub { exit 1; }, #This exits when it gets disconnected.
},
);
sub bot_start {
    $irc->yield(register => "all");
    $irc->yield(
    connect => {
        Nick     => 'MyBot', # Nick name of the bot...
        Username => '.', # Username.
        Ircname  => 'a Perl bot V '.$V, # Ircname, or "Real Name"
        Server   => 'irc.purplesurge.com', #Server, and port down there
        Port     => '6667',
    }
    );
}
sub on_connect {
    $irc->yield(join => $MyChannel);
}
sub ChannelMsg {
    my ($kernel, $who, $where, $UserMsg) = @_[KERNEL, ARG0, ARG1, ARG2]; # This sets a bunch of variables, some will be modified by the time this sub is over
    my $UserNick    = (split /!/, $who)[0]; # Gets the nick...
    my $UserHost    = (split /!/, $who)[1]; # And the Host of the user.
    my $Where = $where->[0]; # This would be the channel.
    # ->
    my($Sec, $Min, $Hour, $Day, $Mon, $Year, $WDay, $YDay) = localtime; 
    my $APM = "AM";
    if($Hour > 12) { $Hour -= 12; $APM = "PM"; }
    my $TimeStamp = "[ $Hour:$Min:$Sec $APM ]";
    # <- All timestamp stuff, you can mess with it, but I just leave it alone...
    print "$TimeStamp   $Where  ->  <$UserNick> $UserMsg\n"; # Shows up on terminal.
    Bot("$UserNick", "$UserMsg", "$UserHost", "$Where", "ChnlMsg"); # This sends variables to the Bot sub, where all the magic happens.
}
sub ChannelAction {
    my ($kernel, $who, $where, $UserMsg) = @_[KERNEL, ARG0, ARG1, ARG2]; # This sets a bunch of variables, some will be modified by the time this sub is over
    my $UserNick    = (split /!/, $who)[0]; # Gets the nick...
    my $UserHost    = (split /!/, $who)[1]; # And the Host of the user.
    my $Where = $where->[0]; # This would be the channel.
    # ->
    my($Sec, $Min, $Hour, $Day, $Mon, $Year, $WDay, $YDay) = localtime; 
    my $APM = "AM";
    if($Hour > 12) { $Hour -= 12; $APM = "PM"; }
    my $TimeStamp = "[ $Hour:$Min:$Sec $APM ]";
    # <- All timestamp stuff, you can mess with it, but I just leave it alone...
    print "$TimeStamp   $Where  ->  \* $UserNick    $UserMsg\n"; # Shows up on terminal.
    Bot("$UserNick", "$UserMsg", "$UserHost", "$Where", "ChnlAct"); # This sends variables to the Bot sub, where all the magic happens.
}
# The Notice sub is almost exactly the same as Channel.
sub Notice {
    my ($kernel, $who, $where, $UserMsg) = @_[KERNEL, ARG0, ARG1, ARG2];
    my $UserNick    = (split /!/, $who)[0];
    my $UserHost    = (split /!/, $who)[1];
    my $Where = $where->[0];
    my $ts      = scalar localtime;
    print "$Where <$UserNick> $UserMsg\n";
    my($Sec, $Min, $Hour, $Day, $Mon, $Year, $WDay, $YDay) = localtime;
    my $APM = "AM";
    if($Hour > 12) { $Hour -= 12; $APM = "PM"; }
    my $TimeStamp = "[ $Hour:$Min:$Sec $APM ]";
    #
    print "$TimeStamp   $Where  ->  -$UserNick- $UserMsg\n";
    Bot("$UserNick", "$UserMsg", "$UserHost", "$Where", "Notice");
}

sub Bot {
    my ($UserNick, $UserMsg, $UserHost, $Where, $Event) = ($_[0], $_[1], $_[2], $_[3], $_[4]); # This sets variables, that were passed down from previous subs.
    $Command = "privmsg";
    if($Event =~ /Notice/i) { $Where = $UserNick; $Command = "notice"; }
    if($Event =~ /^ChnlMsg$|^Notice$/i) { # This handles if the event is Channel message, or Notice.
        if($UserMsg =~ /^!End$/i) { 
            $irc->yield(quit => "Quit"); 
        }
        elsif($UserMsg =~ /^!Do (.*?) (.*?) (.*)$/i) {
            @Temp = ($1,$2,$3);
            $irc->yield($Temp[0] => $Temp[1] => $Temp[2]); #Anyone can do this, be careful with it.
        }
        elsif($UserMsg =~ /^!Help$/i) {
            $irc->yield($Command => $Where => "This is where a command would go, and print to the IRC Channel.");
        }
    }
}

$poe_kernel->run();
exit 0;

Comments

Sign in to comment.
JoKabua   -  May 17, 2012

Can some one tell me how do I use this? give me step by step and commands for using Perl..

 Respond  
JoKabua   -  May 17, 2012

Can some one give us a step by step...

 Respond  
Caaz   -  Aug 29, 2010

Sorry about being very late, but you can use "cpan POE::Component::IRC" to install it. (For *nix systems, from terminal.) For windows, you have to install Perl, and go through a lot more annoying things.

 Respond  
awkwardsaw   -  Jun 24, 2010

sorry for being a noob, but how do i get POE::components, theres no real sauce on how to use the cpan

also, where do i put it using the svn?

 Respond  
Caaz   -  May 09, 2010

I would assume the script. seeing as this is built to go to the subroutine "Bot". It's exactly like the XChat Pichu I had before. Although I'm familiar so yay.

 Respond  
bugboy1028   -  May 09, 2010

What looks Familiar? The user Caaz or the script :P

 Respond  
  -  May 05, 2010

.

 Respond  
Are you sure you want to unfollow this person?
Are you sure you want to delete this?
Click "Unsubscribe" to stop receiving notices pertaining to this post.
Click "Subscribe" to resume notices pertaining to this post.