<?php
$diff 
time() - $lastcmd;

#reply to our bot's commands
if(preg_match("/^:(?!{$nickname}!)(.+)!(.+)@.+ (PRIVMSG|NOTICE) (#.+|[!#].+) :\.(goog|goof|ascii\d?|php|comic|calc|md5|say|sb)(\s.+)?/i",$buffer,$act) && $diff >= 2){

    
$lastcmd time();
    
$to trim($act[4]);
    if(
$to == $nickname) {
        
$act[1] = $to;        
        
$to $nickname;
    }

    
#search ohnorobot
    #needs spell check?
    
if($act[5] == 'comic'){
        
$search urlencode(trim($act[6]));
        if(
strlen($search) > 1){
            
$ch=curl_init();
            
curl_setopt($ch,CURLOPT_URL,"http://www.ohnorobot.com/index.pl?s={$search}&Search=Search&e=0&n=0&b=0&m=0&d=0&t=0");
            
curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,2);
            
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
            
curl_setopt($ch,CURLOPT_USERAGENT,"Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0)");
            
$output curl_exec($ch);
            
curl_close($ch);
            if(!empty(
$output) && strlen($output) < 100000){
                echo 
"cURL went okay: ".strlen($output)." bytes\n";
                if(
preg_match('/\<a class="searchlink" href="(.+)">\s+(.+)\<\/a\>/',$output,$slink)){
                
sendcmd("{$act[3]} {$to} :\"".html_entity_decode(strip_tags($slink[2]),ENT_QUOTES)."\" --> {$slink[1]}\n\r");
                }
                elseif(
preg_match('/<p align="center">No results found\./',$output,$slink)){
                    
sendcmd("{$act[3]} {$to} :no results\n\r");
                }
            }
            else{
sendcmd("{$act[3]} {$to} :error?\n\r");}
        }
        else{
            
sendcmd("{$act[3]} {$to} :.comic <search> (uses ohnorobot.com)\n\r");
        }
    }
    

    
#do a google search
    #needs spell check and no htmlentities
    
elseif($act[5] == 'goog'){
        
$search urlencode(trim($act[6]));
        if(
strlen($search) > 1){
            
$ch=curl_init();
            
curl_setopt($ch,CURLOPT_URL,"http://www.google.com/ie?q={$search}&num=1&start=0&hl=en&ie=UTF-8&filter=true&c2coff=1&safe=off");
            
curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,2);
            
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
            
curl_setopt($ch,CURLOPT_USERAGENT,"Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0)");
            
$output curl_exec($ch);
            
curl_close($ch);
            if(!empty(
$output) && strlen($output) < 100000) {
                echo 
"cURL went okay: ".strlen($output)." bytes\n";
                
str_replace(array("<b>","</b>"),"",$output);
                if(
preg_match('/<nobr>1\. <a title="(.+)" href="?(.+)"?>(.+)<\/a><br><\/nobr>/',$output,$goog)){
                    
sendcmd("{$act[3]} {$to} :".html_entity_decode(strip_tags($goog[1]." ".str_replace(">"," ",$goog[2])),ENT_QUOTES)."\n\r");
                }
                elseif(@
stripos($output,"- did not match any documents.  <br><br>Suggestions:")){
                    
sendcmd("{$act[3]} {$to} :No Result for {$act[6]}\n\r");
                }
            }
            else{
sendcmd("{$act[3]} {$to} :error?\n\r");}
        }
        else{
            
sendcmd("{$act[3]} {$to} :.goog <search> (it has issues, i know)\n\r");
        }
    }
    
    
#google fight! (winning term has the most results in a google search)
    
elseif($act[5] == 'goof'){
        
$search trim($act[6]);
        if(
strlen($search) > 5){
            
$gfnum = array();
            
$terms explode(" && ",$search,2);
            if(
$terms[0] != $terms[1]){
                foreach(
$terms as $term){
                    
$ch=curl_init();
                    
curl_setopt($ch,CURLOPT_URL,"http://www.google.com/search?q=".urlencode(trim($term)));
                    
curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,2);
                    
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
                    
curl_setopt($ch,CURLOPT_USERAGENT,"Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0)");
                    
$output curl_exec($ch);
                    
curl_close($ch);
                    if(!empty(
$output) && preg_match('/<font size="?-1"?>Results <b>\d+<\/b> - <b>\d+<\/b> of about <b>(\d+)<\/b> for <b>.+<\/b>/i',str_replace(",","",$output),$goof)){
                        
$gfnum[] = intval(str_replace(",","",$goof[1]));
                    }
                    else echo 
"curl failed\n".var_dump($goof);
                }
                if(
count($gfnum) == 2){
                    if(
$gfnum[0] > $gfnum[1]){
                        
sendcmd("{$act[3]} {$to} :\"{$terms[0]}\" wins! ({$gfnum[0]} vs {$gfnum[1]})\n\r");
                    }
                    else 
sendcmd("{$act[3]} {$to} :\"{$terms[1]}\" wins! ({$gfnum[1]} vs {$gfnum[0]})\n\r");
                }
            }
            elseif(
$terms[0] == $terms[1]){
                
sendcmd("PRIVMSG {$to} :use two different terms please :P\n\r");
            }
            else{
                
sendcmd("PRIVMSG {$to} :\001ACTION has a headache\001\n\r");
            }
        }
        else{
            
sendcmd("PRIVMSG {$to} :.goof <term1> && <term2> (google fight!)\n\r");
        }
    }

    
#returns ascii art for a string, needs better spacing for "normal" fonts
    
elseif(substr($act[5],0,5) == 'ascii' && $diff 3){
        
$text trim($act[6]);
        if(
strlen($text) > 0){
            
$font_id intval(substr($act[5],5,1));
            if(
strlen($text) > && strlen($text) <= 10){
                include_once(
"ascii/ascii.class.php");
                
$asciiart = new phpFiglet();
                if(
$asciiart->loadFont(asciifonts($font_id))){
                    
$lines explode("\n",$asciiart->get($text));
                    foreach(
$lines as $line){
                        
$line preg_replace("/^\s+$/","",$line);
                        if(
strlen($line) > 2sendcmd("{$act[3]} {$to} :{$line}\n\r");
                    }
                }
                else {
                    
sendcmd("ascii error\n\r");
                }
            }
        }
        else{
            
sendcmd("{$act[3]} {$to} :.ascii# <text> (# _can_ be 1-6 for different fonts)\n\r");
        }
    }

    
#retuns a md5 hash
    
elseif($act[5] == 'md5'){
        if(
strlen($act[6]) > 0){
            
$hash md5(trim($act[6]));
            
sendcmd("{$act[3]} {$to} :{$act[1]}, {$hash}\n\r");
        }
        else{
            
sendcmd("{$act[3]} {$to} :.md5 <string>\n\r");
        }
    }
    
    
#speak to the world!
    
elseif($act[5] == 'say' && substr($to,0,1) == '#'){
        
sendcmd("{$act[3]} {$to} :{$act[6]}\n\r");
    }

    
#use google to calculate shit!
    
elseif($act[5] == 'calc'){
        
$search urlencode(trim($act[6]));
        if(
strlen($search) > 1){
            
$ch=curl_init();
            
curl_setopt($ch,CURLOPT_URL,"http://www.google.com/search?q={$search}");
            
curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,2);
            
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
            
curl_setopt($ch,CURLOPT_USERAGENT,"Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0)");
            
curl_setopt($ch,CURLOPT_REFERER,"http://www.google.com/");
            
$output curl_exec($ch);
            
curl_close($ch);
            if(!empty(
$output) && strlen($output) < 100000) {
                echo 
"cURL went okay: ".strlen($output)." bytes\n";
                if(@
stripos($output,'/images/calc_img.gif')){
                    
$result substr($output,@stripos($output,"<font size=+1><b>")+17,(@stripos($output,"</b></h2></td>"))-(@stripos($output,"<font size=+1><b>")+17));
                    
$answer html_entity_decode(strip_tags(trim(substr($result,@strpos($result,"=")+1))),ENT_QUOTES);
                    if(
strlen($answer) < 200sendcmd("{$act[3]} {$to} :{$act[1]}, {$answer}\n\r");
                }
                else 
sendcmd("{$act[3]} {$to} :\001ACTION has a headache\001\n\r");
            }
            else{
sendcmd("{$act[3]} {$to} :error?\n\r");}
        }
        else{
            
sendcmd("{$act[3]} {$to} :.calc <shit> (if you don't know how to use a calculator, you have issues)\n\r");
        }
    }

    
#search for a PHP function
    #needs a lot of work :/
    
elseif($act[5] == 'php'){
        
$search trim(str_replace(array("(",")"),"",$act[6]));
        if(
strlen($search) > 1){
            
$ch=curl_init();
            
curl_setopt($ch,CURLOPT_URL,"http://us3.php.net/{$search}");
            
curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,2);
            
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
            
curl_setopt($ch,CURLOPT_USERAGENT,"Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0)");
            
$output curl_exec($ch);
            
curl_close($ch);
            if(!empty(
$output) && strlen($output) < 100000){
                echo 
"cURL went okay: ".strlen($output)." bytes\n";
                if(
preg_match("/<p class=\"verinfo\">(.+)<\/p><p class=\"refpurpose dc-title\">(.+)<\/p>/",$output,$php)){
                    
$outs $php[1]." ".$php[2];
                    
$outs .= ": ".str_replace("\n","",strip_tags($php[3]));
                    
$pos = @stripos($output,"<div class=\"methodsynopsis dc-description\">");
                    if(
$pos){
                        
$outs .= " ".trim(substr($output,$pos+43,@stripos($output,"</div>",$pos)-($pos+43)));
                    }
                    
$pos2 = @stripos($output,'<p class="para rdfs-comment">',$pos+43);
                    if(
$pos2){
                        
$outs .= '. "'.trim(substr($output,$pos2+29,@stripos($output,"</p>",$pos2)-($pos2+29))).'"';
                    }
                    
$outs str_replace(array("   ","  ")," ",html_entity_decode(strip_tags(str_replace(array("\n","\r"),"",$outs),ENT_QUOTES)));
                    
sendcmd("{$act[3]} {$to} :{$outs}\n\r");                }
            }
            else{
sendcmd("{$act[3]} {$to} :\"{$search}\" not found\n\r");}
        }
        else{
            
sendcmd("{$act[3]} {$to} :.php <function> (*should* give some info on a given php function, if it exists)\n\r");
        }
    }

    
#SB command help
    
else sendcmd("{$act[3]} {$cmds[1]} :.goog, .calc, .comic, .ascii, .php, .md5, .say\n\r");
    
}

#reply to ppl grabbing info on me
elseif(preg_match("/^:(.+)!.+@.+ PRIVMSG {$nickname} :\001(VERSION|TIME|PING)\001/i",$buffer,$vtp)){
    if(
$vtp[2] == 'TIME'$stuff date("n/j/y g:i a");
    elseif(
$vtp[2] == 'PING'$stuff time();
    else 
$stuff $version;
    
sendcmd("NOTICE {$vtp[1]} :\001{$vtp[2]} {$stuff}\001\n\r");
}


#ping-pong!
elseif(preg_match("/^PING :(.+)/",$buffer,$ping)){
    
sendcmd("PONG :{$ping[1]}\n\r");
}

#after changing nicks, reset the variable
elseif(preg_match("/^:{$nickname}!{$username}@.+ NICK :(.+)/",$buffer,$nnick)){
    
$nickname trim($nnick[1]);
}

#let NickServ know i own my nicknames!
elseif(preg_match("/^NickServ!.+@.+ NOTICE {$nickname} :This nickname is registered and protected/",$buffer)){
    
sendcmd("PRIVMSG NickServ :\001IDENTIFY {$password}\001\n\r");
}

#join channel(s) after we get the MOTD (response #376)
elseif(preg_match("/:.+ 376 {$nickname}/",$buffer)){
    foreach(
$channels as $channel){
        
sendcmd("JOIN {$channel}\n\r");
    }
}


#hanlde all the PMs from my master
elseif(preg_match("/^:flystar06!flystar06@.+ PRIVMSG {$nickname} :.(join|part|rejoin|say|nick|quit|restart)(\s#?.+)?/",$buffer,$cmds)){
    
$cmds[1] = trim($cmds[1]);
    
$cmds[2] = trim($cmds[2]);
    
$says substr($cmds[2],@strpos($cmds[2]," ")+1);
    if(
$cmds[1] == 'rejoin'){
        
sendcmd("PART {$cmds[2]}\n\r");
        
sleep(1);
        
sendcmd("JOIN {$cmds[2]}\n\r");
    }
    elseif(
$cmds[1] == 'say'){
        
sendcmd("PRIVMSG {$cmds[2]} :{$says}\n\r");
    }
    elseif(
$cmds[1] == 'nick'){
        
sendcmd("NICK {$cmds[2]}\n\r");
    }
    elseif(
$cmds[1] == 'quit'){
        
sendcmd("QUIT :death to the bot\n\r");
        die(
"i has been quitted!\n");
    }
    elseif(
$cmds[1] == 'restart'){
        
$restarting TRUE;
        
sendcmd("QUIT :rawr\n\r");
    }
    else{
        
sendcmd(strtoupper($cmds[1])." {$cmds[2]}\n\r");
    }    
}

#rejoin channel after being kicked
# :rki!rootcontes@rootcontest.com KICK #Bots SB :STOP
elseif(preg_match("/^:.+!.+@.+ KICK (#.+) {$nickname}/",$buffer,$kick)){
    
sleep(1);
    
sendcmd("JOIN {$kick[1]}\n\r");
}

?>