#!/usr/local/bin/perl -w

require 5.002;

use Socket;
 
#
$myversion ="Oper's OP bot";
# configuration parameters for the service
#

## Server Connection Info
$server="irc.network.net"; # Use host.name or IP#
$port=6668;                 # Port to use

# information about the service
$myname = "Road";         # Nick to use on IRC
$cchannel = "#twilight_zone";      # Channel to join (if any)
$myrealname = "Oper's OP bot";
$myhost = "0";


### CTCP REPLY Stuff - Change to show your info ###
$myclientinfo = "PING VERSION CLIENTINFO OP";
#

START:

# set up connection
#
$iaddr = inet_aton($server) || die "no host: $server";
$paddr = sockaddr_in($port, $iaddr);
$proto = getprotobyname('tcp');
socket(SOCK, PF_INET, SOCK_STREAM, $proto) || die "socket: $!";
connect(SOCK, $paddr) || die "connect: $!";

select SOCK; $| = 1;
select STDOUT;

#
# register
#

# Parameters: <username> <hostname> <servername> <realname>
#print SOCK "PASS xxx\n";
print SOCK "USER $myname $myhost $myhost :$myrealname\nNICK $myname\n";
#

while (<SOCK>)
{
        chomp;
        if ( /^ERROR/ )
        {
                print "$_\n";
        }
        elsif (  /^PING (.*)/  )
        {
                print SOCK "PONG $1\n";
        }
	elsif ( /:(\S*) 001 \S* :(.*)$/ )
	{
		#join a channel
		print SOCK "JOIN $cchannel  \n";
	}
        elsif ( /:(\S*) PRIVMSG \S* :(.*)$/ )
        {
                ($nick , $query) = ($1, $2);               # split the user-addy from the message
		($nnick, $nhost) = split('!', $nick);
#######################   CTCP COMMANDS   ###################
                if ( $query =~ /^\001VERSION\001/i )
                {
                        print SOCK "NOTICE $nnick :\001VERSION $myversion\001\n";
                }
                elsif ( $query =~ /^\001CLIENTINFO\001/i )
                {
                        print SOCK "NOTICE $nnick :\001CLIENTINFO $myclientinfo\001\n";
                }
                elsif ( $query =~ /^\001PING (\S*)\001/i )
                {
                        print SOCK "NOTICE $nnick :\001PING $1\001\n";
                }
		elsif ( $query =~ /^\001OP\001/i )
		{
			print SOCK "WHO $nnick\n";
			$wline=<SOCK>;
			if ($wline =~ /\S* 352 \S* \S* \S* \S* \S* \S* (\S*) .*/i)
			{
				$flags=$1;
				if ($flags =~ /\*/g)
				{
					print SOCK "MODE $cchannel +o $nnick\n";
				}
			}
		}
        }
        else
        {
#
        }
}

close (SOCK) || die "close: $!";

goto START;
exit;
