08-05-2010, 11:43 PM
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;