<?php
/*********************************************************************
    Name        : YBOT - IRC bot made with PHP CLI
    Author    : Yeni Setiawan (yenisetiawan@gmail.com)
    License    : I don't care. Just use it at your own risk ^_^
    
    Description:
    ------------------------------------------------------------------
    I write this script to run in Windows platform, to run it in
    Linux platform, don't forget to add the she-bang line:
        #! /path/to/php
    You must activate socket extension to run this script, othe
    rwise it won't work.
    
    Run the script:
    ------------------------------------------------------------------
    Windows:
        C:\php> php.exe php_irc_bot.phps
    Linux:
        $ /path/to/php php.exe php_irc_bot.phps
        or
        $ ./php.exe php_irc_bot.phps (chmod first)
        
    Disclaimer:
    ------------------------------------------------------------------
    I've tested this script on my own machine and it run well 
    for 3 days. Theoritically, it can run forever as you wish.
    You use this script at your own risk!
    
*********************************************************************/

// set max execution time, it will loop'd forever
ini_set("max_execution_time",0);

// inisiasi
$server        "aremania.kr.allnetwork.org";
$port           6666;
$nickname    "dukun_sakti";
$info        "ada apa dengan cinta";
$channel    "#nol";
$master        "sandal";
$master_name    "sandal";
$logfile    "";
$botinfo    "is a bot-script created by $master using php.";

// USABLE FUNCTION
function clean($txt){
    
$txt str_replace("\r","",$txt);
    
$txt str_replace("\n","",$txt);
    
$txt str_replace(":","",$txt);
    return 
trim($txt);
}

// just displayed a # on screen, useless :P
echo "#";

// join to server
$sock fsockopen($server,$port);
if(!
$sock){
sleep(10);
}
fputs($sock,"user ".$info."\r\n");
fputs($sock,"nick $nickname\r\n");

// wait for the ping and reply with pong
$i=0;
while(
$i<1){
    echo 
"#";
    
$a fgets($sock,1024);
    if(
eregi("PING :",$a)){
        
$ping explode(":",$a);
        
$pong "PONG :".$ping[1];
        
fputs($sock,$pong."\r\n");
        echo 
"bot connected!";
        
$i=2;
    }
}

// join to first channel and say first greeting
fputs($sock"join ".$channel."\r\n");
fputs($sock"privmsg $channel :good morning..\r\n");

###################################### START HERE #######################################
// connected, go ahead
while($out fgets($sock,1024)){
    
    
// PREPARE TO REPLY PING WITH PONG
    
if(ereg("PING :",$out)){
        
$ping explode(":",$out);
        
$pong "PONG :".$ping[1];
        
fputs($sock,$pong."\r\n");
    }

    
// RE-JOIN IF KICKED
    
if(ereg(" KICK ",$out)){
        
$who=explode(" ",$out);
        echo 
"$who[3] alias $nickname di kick!\n";
        if(
$who[3]==$nickname){
            
fputs($sock,"join ".$who[2]."\r\n");
        }
        elseif(
$who[3]==$master){
            
fputs($sock,"privmsg ".$who[2]." :kenapa majikanku di-kick?\r\n");
        }
        else{
            
fputs($sock,"privmsg ".$who[2]." :rasain lu di-kick :p\r\n");
        }
    }

    
// GREETING WHEN MASTER JOIN
    
if(ereg(" JOIN ",$out)){
        
// get who join
        
$use  explode("!",$out);
        
$user clean(str_replace(":","",$use[0]));

        
// get realname
        
$real explode("@",$out);
        
$real explode("!",$real[0]);
        
$realname clean($real[1]);

        
// get the channel
        
$chan explode(" ",$out);
        
$chan clean($chan[2]);

        if(
$user==$master && $realname==$master_name){
            
fputs($sock,"privmsg $chan :bos $master datang, hormaaaaat graaaak!!!\r\n");
        }
    }

    
// RESPONSE ON OTHERS SPEAK
    
if(ereg(" PRIVMSG ",$out)){
        
        
// get nickname of the speaker
        
$use  explode("!",$out);
        
$user clean(str_replace(":","",$use[0]));
        
        
// get realname
        
$real explode("@",$out);
        
$real explode("!",$real[0]);
        
$realname clean($real[1]);

        
// get the channel
        
$chan explode(" ",$out);
        
$chan clean($chan[2]);
        if(
$chan==$nickname){ // <-- for privat message
            
$chan=$user;
            
fputs($sock,"privmsg $chan :".chr(1)."ACTION is just a bot :".chr(222)." ".chr(1)."\r\n");
        }
        
        
// get the spoken words
        
$word explode(":",$out);
        
$word clean($word[2]);

        
// RESPONSES MASTER HERE:
        
if($user==$master && $realname=$master_name){
            
            
// change nickname
            
if(ereg("`nick",$word)){
                
$newnick explode(" ",$word);
                
$nickname clean($newnick[1]);
                if(
$nickname!=""){
                    
fputs($sock,"nick ".$nickname."\r\n");
                }
            }
            
// go to a channel
            
elseif(ereg("`go",$word)){
                
$loc explode(" ",$word);
                
$location clean($loc[1]);
                if(
ereg("#",$location)){
                    
fputs($sock,"join $location\r\n");
                    
fputs($sock,"privmsg $location :assalamualaikum wr wb\r\n");
                }
                else{
                    
fputs($sock,"join #".$location."\r\n");
                    
fputs($sock,"privmsg #".$location." :assalamualaikum wr wb\r\n");
                }
            }
            
// say a word
            
elseif(ereg("`say",$word)){
                
$text str_replace("`say","",$word);
                
$text clean(trim($text));
                
fputs($sock,"privmsg $chan :$text\r\n");
            }
            
// do a thing
            
elseif(ereg("`do",$word)){
                
$text str_replace("`do","",$word);
                
$text clean(trim($text));
                
fputs($sock,"privmsg $chan :".chr(1)."ACTION $text".chr(1)."\r\n");
            }
            
// grant a chan-op mode
            
if(ereg("`sakti",$word)){
                
fputs($sock,"mode $chan +o $master\r\n");
            }
            
// leave the channel
            
elseif(ereg("`home",$word)){
                
$rea explode("`home",$word);
                
$reason=clean(trim($rea[1]));
                
fputs($sock,"part $chan :$reason\r\n");
            }
            
// kill me
            
elseif(ereg("`sleep",$word)){
                
$rea explode("`sleep",$word);
                
$reason=clean(trim($rea[1]));
                
fputs($sock,"quit :$reason\r\n");
                exit;
            }
        }
        
// RESPONSE TO PUBLIC HERE
        
else{
            
// reply a greet
            
if(eregi("salamualaikum",$word)){
                
fputs($sock,"privmsg $chan :wa alaikum salam $user\r\n");
            }
            
// give bot information
            
if(eregi("`bot",$word)||eregi("!bot",$word)){
                
fputs($sock,"privmsg $chan :".chr(1)."ACTION $botinfo".chr(1)."\r\n");
            }
            if(
eregi("asl pls",$word)||eregi("asl plz",$word)){
                
fputs($sock,"privmsg $chan :23 male Jogja (Indonesia), u?\r\n");
            }
        }
    }

    
// display output
    
echo $out;

}
?>