Thread Rating:
  • 1 Vote(s) - 5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Perl] IRC Bot
#1
A Bot I Havent seen around here ^^

Code:
#!/usr/bin/perl
print "What server to join?\n";
chomp($Server = <STDIN>);
print "Port?\n";
chomp($Port = <STDIN>);

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

my $BotNick = "MyBot"; # Nick.

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_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_ctcp_action => sub {
        my ($kernel, $who, $where, $UserMsg) = @_[KERNEL, ARG0, ARG1, ARG2];
        my $UserNick    = (split /!/, $who)[0];
        my $UserHost    = (split /!/, $who)[1];
        my $Where = $where->[0];
        print GetTime"    $Where    ->    \* $UserNick    $UserMsg\n";
        Bot("$UserNick", "$UserMsg", "$UserHost", "$Where", "ChnlAct");
    },
    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);
        print GetTime()."    * Invited to $Where from $UserNick.";
    },
    irc_join => 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.
        print GetTime()."        * $UserNick ($UserHost) has joined $Where.\n"; # Shows up on terminal.
    },
    irc_part => sub {
        my ($kernel, $who, $Where, $UserMsg) = @_[KERNEL, ARG0, ARG1, ARG2];
        my $UserNick    = (split /!/, $who)[0]; # Gets the nick...
        my $UserHost    = (split /!/, $who)[1]; # And the Host of the user.
        print "$TimeStamp        * $UserNick ($UserHost) has left $Where ($UserMsg).\n"; # Shows up on terminal.
    },
    irc_quit => sub {
        my ($kernel, $who, $UserMsg) = @_[KERNEL, ARG0, ARG1];
        my $UserNick    = (split /!/, $who)[0]; # Gets the nick...
        my $UserHost    = (split /!/, $who)[1]; # And the Host of the user.
        print GetTime()."        * $UserNick ($UserHost) has quit ($UserMsg).\n"; # Shows up on terminal.
    },
    irc_disconnected => sub { exit 1; }, #This exits when it gets disconnected.
},
);
sub bot_start {
    $irc->yield(register => "all");
    $irc->yield(
    connect => {
        Nick     => $BotNick, # Nick name of the bot...
        Username => '.', # Username.
        Ircname  => 'a Perl bot. Skeleton by Caaz', # Ircname, or "Real Name"
        Server   => $Server, #Server, and port down there
        Port     => $Port,
    }
    );
}
sub on_connect {
    $irc->yield(join => "#Powerplant"); #Joins a channel.
    $irc->yield(mode => "$BotNick +B"); # Sets Bot mode.
    $irc->yield(privmsg => "Nickserv" => "id PURPLEUNICORNS"); # This Identifies you, for servises, and before you try, this is not my password.
}
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.
    print GetTime()." $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.
}
# 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];
    print GetTime()." $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) {
            if($UserNick =~ /^Caaz$/) {
                $irc->yield(quit => "- Quit - \) \( Powerplant Ftw "); # Quits from IRC.
            }
        }
        elsif($UserMsg =~ /^~Do (.*?) (.*?) (.*)$/i) {
            @Temp = ($1,$2,$3);
            if($UserNick =~ /^Caaz$/) {
                $irc->yield($Temp[0] => $Temp[1] => $Temp[2]);
            }
        }
        elsif($UserMsg =~ /^~Help$|^-Help$/i) {
            $irc->yield($Command => $Where => "This skeleton was made by Caaz.");
        }
    }
}

sub GetTime {
    my($Sec, $Min, $Hour, $Day, $Mon, $Year, $WDay, $YDay) = localtime;
    my $APM = "AM";
    if($Hour > 12) { $Hour -= 12; $APM = "PM"; }
    return "[ $Hour:$Min:$Sec $APM ]";
}

$poe_kernel->run();

exit 0;
Reply


Messages In This Thread
[Perl] IRC Bot - by wchar_t - 08-05-2010, 11:43 PM
RE: [Perl] IRC Bot - by Sιℓνєя - 08-16-2010, 03:15 PM
RE: [Perl] IRC Bot - by Bursihido - 12-11-2010, 04:13 PM
RE: [Perl] IRC Bot - by Saint Michael - 12-14-2010, 07:48 PM
RE: [Perl] IRC Bot - by Prior - 12-14-2010, 07:55 PM
RE: [Perl] IRC Bot - by Break - 01-05-2011, 12:28 PM
RE: [Perl] IRC Bot - by InfamousKnight - 02-08-2011, 01:11 PM
RE: [Perl] IRC Bot - by matt - 06-04-2011, 05:42 PM
RE: [Perl] IRC Bot - by BacklTrack - 06-11-2011, 03:13 PM
RE: [Perl] IRC Bot - by Impurity - 02-12-2011, 05:44 PM
RE: [Perl] IRC Bot - by Caaz - 02-14-2011, 06:46 PM
RE: [Perl] IRC Bot - by cOld fus1on - 02-14-2011, 07:21 PM
RE: [Perl] IRC Bot - by Nympho - 05-09-2011, 06:53 AM
RE: [Perl] IRC Bot - by BacklTrack - 05-31-2011, 10:46 PM
RE: [Perl] IRC Bot - by Caaz - 06-02-2011, 12:25 PM
RE: [Perl] IRC Bot - by epic - 06-04-2011, 05:54 PM
RE: [Perl] IRC Bot - by USN - 06-08-2011, 01:08 AM
RE: [Perl] IRC Bot - by Ted Bundy - 06-11-2011, 05:34 PM
RE: [Perl] IRC Bot - by g0dspeak - 06-11-2011, 09:47 PM
RE: [Perl] IRC Bot - by Link - 06-19-2011, 10:16 AM
RE: [Perl] IRC Bot - by Injection - 07-01-2011, 03:37 PM
RE: [Perl] IRC Bot - by B3cnoc - 07-07-2011, 04:51 PM
RE: [Perl] IRC Bot - by Hex - 11-07-2011, 08:51 PM
RE: [Perl] IRC Bot - by HF~Legend - 11-17-2011, 02:30 PM
RE: [Perl] IRC Bot - by New Jersey - 11-27-2011, 08:27 AM
RE: [Perl] IRC Bot - by HF~Legend - 08-20-2012, 01:34 PM
RE: [Perl] IRC Bot - by Trump - 08-20-2012, 01:40 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Compile Error in Perl liveproject101 0 1,160 04-12-2013, 03:08 AM
Last Post: liveproject101
  Perl Ebook ven0m 2 1,631 05-04-2012, 08:14 AM
Last Post: ven0m
  Help with a program written in perl Rodman42866 1 1,535 03-09-2012, 03:16 AM
Last Post: AceInfinity
  HTML perl script AceInfinity 5 2,415 12-03-2011, 11:35 PM
Last Post: Closed Account
  Perl ARGV Tutorial AceInfinity 0 2,274 08-22-2011, 02:30 PM
Last Post: AceInfinity

Forum Jump:


Users browsing this thread: 1 Guest(s)