Ver Mensaje Individual
  #1 (permalink)  
Antiguo 02/03/2011, 13:49
kozenko
 
Fecha de Ingreso: octubre-2010
Mensajes: 83
Antigüedad: 16 años
Puntos: 1
Problema con un bot de irc

Estoy haciendo un bot de irc
tiene muchas funciones
pero tengo un problema
estoy tratando de hacer que interprete los comandos escritos en una base de datos,
hay tres columnas id, cmd (comando), rta (respuesta)
le saque muchos comandos porque no me entraba en el post

aca esta el bot
Código PHP:
<?php
 
 
set_time_limit
(0);
ini_set('display_errors', 'on');
 
/* --- Varibles and Config Info --- */
 
$config = array(
        
'server' => 'irc.toribash.com',
        
'port'   => 6667,
        
'name'   => 'Kozbot',
        
'nick'   => 'Kozzy',
       
       
        
'channel' => '#koz',
                
'channel2' => '#latin',              
        
'logging' => false,
            
'warning' => false,
);
 
 
/* --- IRCBot Class --- */
 
class IRCBot {
 

        var 
$socket;
 

        var 
$ex = array();    

 
 
        function 
__construct($config)
        {
                
$this->socket = fsockopen($config['server'], $config['port']);
                
$this->login($config);
                
$this->main($config);
        }
 

 
        function 
login($config)
        {
                
$this->send_data('USER', $config['nick'].' KozEnko '.$config['nick'].' :'.$config['name']);
                
$this->send_data('NICK', $config['nick']);
                                
$this->send_data('NICKSERV', ' identify ******');
                                
$this->join_channel($config['channel']);
                                                                
$this->join_channel($config['channel2']);
                                if(
$config['logging']) {
                        
$date = date("n-j-y");
                        
$time = date('h:i:s A');
                        
$logfile = fopen("$date-log.html","a");
                        
fwrite($logfile,"<br/>**************** Logging Started at $time ****************<br/>");
                        
fclose($logfile);
                               

                         if(
$config['warning']) {
                                        
$this->send_data('PRIVMSG '.$config['channel'].' :', "Chat Logging has been [Enabled]");
                                }
                 }
        }
 

 
function 
main($config)
        {            
                
$data = fgets($this->socket, 256);
               
                echo 
nl2br($data);
                               
                
flush();
 
                
$this->ex = explode(' ', $data);
                                
$mensaje_base = strstr($data, ':');
                                
$mensaje_basee = substr($mensaje_base, 1);
                                
$mensajee = strstr($mensaje_basee, ':');
                                
$mensaje = substr($mensajee, 1);
                                               
                if(
$this->ex[0] == 'PING')
                {
                        
$this->send_data('PONG', $this->ex[0]); //Plays ping-pong with the server to stay connected.
                                                
}
               
                if(
$config['logging'])
                {
                        
$logtxt = $this->filter_log($this->ex[1], $this->ex[2], $this->ex[0], $this->get_msg($this->ex)); //Gets human readable text from irc data
                        
if($logtxt != null) { //Writes to log if it is a message
                                
$date = date("n-j-y");
                                
$logfile = fopen("$date-log.html","a");
                                
fwrite($logfile,"$logtxt<br />");
                                
fclose($logfile);      
                        }
                }
        
$command = str_replace(array(chr(10), chr(13)), '', $this->ex[3])." ".$this->ex[0];
        switch(
$command) {
                       case 
':!pegar '.$this->ex[0]:
                                
$msg = "";
                for(
$i=4; $i <= (count($this->ex)); $i++) { $msg .= $this->ex[$i]." "; }
                
$this->send_data('PRIVMSG', $this->ex[2]." :\x01ACTION le pega en la cara a ".$msg." tan fuerte que le rompe la nariz");
                break;
            case 
':!matar '.$this->ex[0]:
                                
$msg = "";
                for(
$i=4; $i <= (count($this->ex)); $i++) { $msg .= $this->ex[$i]." "; }
                
$this->send_data('PRIVMSG', $this->ex[2]." :\x01ACTION le pega un tiro a ".$msg);
                break;
            case 
':!odiar'.$this->ex[0]:
                                
$msg = "";
                for(
$i=4; $i <= (count($this->ex)); $i++) { $msg .= $this->ex[$i]." "; }
                
$this->send_data('PRIVMSG', $this->ex[2]." :\x01ACTION odia a ".$msg);
                break;
            case 
':!amar '.$this->ex[0]:
                            
$msg = "";
                for(
$i=4; $i <= (count($this->ex)); $i++) { $msg .= $this->ex[$i]." "; }
                
$this->send_data('PRIVMSG', $this->ex[2]." :\x01ACTION ama a ".$msg);
                break;
            case 
':!fail '.$this->ex[0]:
                                
$msg = "";
                for(
$i=4; $i <= (count($this->ex)); $i++) { $msg .= $this->ex[$i]." "; }
                
$this->send_data('PRIVMSG', $this->ex[2]." :\x01ACTION eres un fail ".$msg);
                break;
            case 
':!fag '.$this->ex[0]:
                                
$msg = "";
                for(
$i=4; $i <= (count($this->ex)); $i++) { $msg .= $this->ex[$i]." "; }
                
$this->send_data('PRIVMSG', $this->ex[2]." :\x01ACTION eres un puto fag ".$msg."!");
                break;
            case 
':!morder '.$this->ex[0]:
                                
$msg = "";
                for(
$i=4; $i <= (count($this->ex)); $i++) { $msg .= $this->ex[$i]." "; }
                
$this->send_data('PRIVMSG', $this->ex[2]." :\x01ACTION le muerde el culo a ".$msg."!");
                break;
            case 
':!epic '.$this->ex[0]:
                                
$msg = "";
                for(
$i=4; $i <= (count($this->ex)); $i++) { $msg .= $this->ex[$i]." "; }
                
$this->send_data('PRIVMSG', $this->ex[2]." eres epic ".$msg."!");
                break;
            case 
':!lol '.$this->ex[0]:
                                
$msg = "";
                for(
$i=4; $i <= (count($this->ex)); $i++) { $msg .= $this->ex[$i]." "; }
                
$this->send_data('PRIVMSG', $this->ex[2]." se le rie en la cara a ".$msg."!");
                break;
            case 
':!escupir '.$this->ex[0]:
                                
$msg = "";
                for(
$i=4; $i <= (count($this->ex)); $i++) { $msg .= $this->ex[$i]." "; }
                
$this->send_data('PRIVMSG', $this->ex[2]." :\x01ACTION le encaja un terrible garzo en la cara a ".$mensaje."!");
                break;
            case 
':!chupar '.$this->ex[0]:
                                
$msg = "";
                for(
$i=4; $i <= (count($this->ex)); $i++) { $msg .= $this->ex[$i]." "; }
                
$this->send_data('PRIVMSG', $this->ex[2]." Hacelo vos!");
                break;
//enseñar
            
case ':Kozzy= '.$this->ex[0]:
                            
$dataa = "";
                for(
$i=4; $i <= (count($this->ex)); $i++) { $dataa .= $this->ex[$i]." "; }
                
$cmd = strstr($dataa, '|', true);
                                
$answer_base = strstr($dataa, '|');
                                
$answer = substr($answer_base, 1);
                               
                                
$link = mysql_connect('localhost', 'root');
if (!
$link) {
    die(
'Not connected : ' . mysql_error());
}
 
$db_selected = mysql_select_db('kozzy', $link);
            
$sql4 = "INSERT INTO `Kozzy`(`cmd`,`rta`) VALUES ('".$cmd."','".$answer."')";
            
$res4 = mysql_query($sql4) or die(mysql_error());
                                
$this->send_data('PRIVMSG', $this->ex[2]." done");
                                break;
 
 
            
}
        
$this->main($config);
    }
 
    
/* --- IRCBot Class's Functions --- */
 
function txt2bin($str) {
      
$text_array = explode("\r\n", chunk_split($str, 1));
      for (
$n = 0; $n < count($text_array) - 1; $n++) {
        
$newstring .= substr("0000".base_convert(ord($text_array[$n]), 10, 2), -8);
      }
      return 
$newstring;
    }
    function 
filter_log($type, $chan, $nick, $msg) {
        
$nick = ltrim($nick, ":");
        
$nick = substr($nick, 0, strpos($nick, "!"));
        
$msg = ltrim($msg, ":");
 
        if(
$type == "PRIVMSG") {
            return 
date("[H:i]")." &lt;".$nick."&gt; ".$msg;
        }
        return 
null     ;
    }
 
    function 
get_msg($n) {
        
$message = "";
        for(
$i=$n; $i <= (count($this->ex)); $i++) { $message .= $this->ex[$i]." "; }
        return 
$message;
    }
 
    function 
send_data($cmd, $msg = null) {
        if(
$msg != null) {
            
fputs($this->socket, $cmd.' '.$msg."\r\n");
            echo 
'<strong>'.$cmd.' '.$msg.'</strong><br />';
        } else {
            
fputs($this->socket, $cmd."\r\n");
            echo 
'<strong>'.$cmd.'</strong><br />';
        }
    }
 
        function 
join_channel($channel) {
        
$this->send_data('JOIN', $channel);
    }
 
function 
evall ($msg){
        
$this->send_data('PRIVMSG', $this->ex[2].' :'.eval($msg));}
 
 function 
ban_user($chan = "#DAT", $user = "Koz", $ban = true) {
        if(
$ban) { $this->send_data('MODE', $chan.' +b '.$user);
        } else { 
$this->send_data('MODE', $chan.' -b '.$user); }
    }
 
 function 
kick_user($chan = "#DAT", $user = "Koz") {
        
$this->send_data('KICK ' . $chan, $user);
}
 
 
function 
build($offset) {
          
$out = "";
        for(
$i=($offset); $i<=(count($this->ex)-1); $i++) {
            
$out = $out.$this->ex[$i]." ";
        }
        return 
$out;
    }
 
}
$bot = new IRCBot($config);
?>