#!perl -w
# Author: weby <kumcho@vulcho.com>
# Version: no version avaiable dude
use strict;
use IO::Socket;
# CONFIGURATION VARIABLES:
my $nick=shift666 || "TheBigPerlBook";
my $server="127.0.0.1";
my $port=6667;
my @channels=qw(otep);
# END OF CONFIGURATION VARIABLES!


print "Establishing connection to IRC SERVER: $server...";
my $sock = IO::Socket::INET->new(PeerAddr => $server,
				 PeerPort => $port,
				 Proto    => 'tcp')
    or die "[error] $!\n";
print("[ Ok ]\n");

print("Sending USER information...");
$sock->print("USER $nick a a a\n");
$sock->print("NICK $nick\n");
print("[ Ok ]\nTrying to join in [ ",scalar @channels," ] channels:\n");

for my $channel (@channels) {
    print "#$channel\n";
    $sock->print("JOIN #$channel\n");
}

while (<$sock>) {
    chomp;
    print "$_\n";
    # response to server if send to us PING request:
    keep_alive($sock,$1) if /^PING\s*:(.*?)$/i;
    &parse_text($sock,$_);
}
close $sock;
print "exit ...\n";
# ###################################################
# SUBROUTINES 
# ###################################################
sub parse_text {
    my($sock,$str)=@_;
    chomp($str);
    if ($str=~/^:(.*?)\!(.*?)\s+(.*?)\s+(.*?)\s+:(.*?)$/i) {
	my =$1;
	my $uhost=$2;
	my $mtype=$3;
	my $rcpto=$4;
	my $mssge=$5;
	if($mtype && $mtype =~ /PRIVMSG/i){
	    # CHANNEL MESSAGE
	    if ($rcpto =~ /^\#(.*?)$/) {
		&channel_response($sock, $1,$mssge,$unick);
	    }
	    else {
		&private_response($sock,$unick,$mssge,$nick);
	    }
	}
    }
    return;
}
sub join_ {
    my($sock,$channels_ref)=@_;
    $sock->print("JOIN ");
    return;
}
sub private_response {
    my($sock,$unick,$str, $nick)=@_;
    chomp($str);
    print "PRIVATE $unick: $str\n\n";
    if($unick eq 'weby') {
	if ($str =~ /quit/) {
	    $sock->print("PRIVMSG $unick :yes master, i'm going out...\n");
	    $sock->print("QUIT :oh noooooooo at ",scalar localtime,"\n");
	    $sock->close();
	    exit;
	}
	
	if($str=~/^cmd\s+(.*?)$/i) {
	    $sock->print("$1\n");
	}
    }
    elsif ($str=~m/$nick/) {
	my @answer = `/usr/games/fortune`;
	for (@answer) {
	    $sock->print("PRIVMSG $unick :$_");
	}
	my $ans=join('',@answer);
	&dolog($nick, $unick, $str, $ans);
    }
    return;
}
sub channel_response {
    my($sock,$channel,$str,$unick)=@_;
    chomp($str);
    print "CHANNEL #$channel: $str\n\n";
    if ($str=~/$nick/) {
	my @answer = `/usr/games/fortune`;
	for (@answer) {
	    $sock->print("PRIVMSG #$channel :$_");
	}
	
	my $ans=join('',@answer);
	&dolog($nick, $unick, $str, $ans);
    }
    return;
}
sub keep_alive {
    my($sock, $str) = @_;
    chomp($str);
    $sock->print("PONG $str\n");
    print "* PONG\n\n";
    return;
}
sub dolog {
    my($nick,$unick,$msg,$ans)=@_;
    die "Dorectory bot_logs does not exists!\n" unless -d "bot_logs";
    open  LOG,">> bot_logs/$unick.log" or die "$!\n";
    print LOG "$unick: $msg\n";
    print LOG "$nick: $ans\n\n";
    close LOG;
}
# That's all folks friends!
__END__
Published in: CGI
Download

Related snippets