Bot irc : générateurs de logs au format mirc

4/5 (8 avis)

Snippet vu 5 375 fois - Téléchargée 19 fois

Contenu du snippet

Bot irc en php. Il permet si on le souhaite de générer des logs pour les différents salons où il est loggé.

Encore en phase de test.

Source / Exemple :


#!/usr/bin/php5
<?php
class MyException extends Exception  { 
    public function __construct($erreur=null, $code=0)  { 
        parent::__construct($erreur, $code); 
    } 
     
    public function echoErreur($exit=false)  { 
      echo $this->getMessage(); 
      if($exit) {
        exit;
      }
    } 
} 

class IrcConnection {
  protected $cfg_IrcConnexion = array();
  protected $connection       = null;
  
  protected function __construct($server, $port) {
    $this->cfg_IrcConnexion = array(
      'server' => $server,
      'port'   => $port
    );
    $this->connection = null;
  }
  
  protected function doConnection() {
    try {
      $connection = @fsockopen( $this->cfg_IrcConnexion['server'], $this->cfg_IrcConnexion['port'], $errorId, $errorMessage );
      if( !$connection ) {
        throw new MyException('ERREUR : Connexion au serveur impossible. Code ['.$errorId.'] : '.$errorMessage."\r\n");
      }  else {
        $this->connection = $connection;
      }
    } catch (MyException $e) {
      $e->echoErreur(true);
    }
  }
}

class Bot extends IrcConnection {

  public $serveur;
  public $port;
  
  public $bold;
  public $reverse;
  public $color; 
  
  private $botConnection;
  private $stats;
  
  public $cfg_bot      = array(); 
  public $cfg_server   = array(); 
  public $cfg_channels = array();
  
  public $enable_stats;
  public $cfg_stats     = array();

  public function __construct() {
    $this->cfg_bot = array(
      'nick'               => 'MyBot',
      'host'               => 'MyHost',
      'mail'               => 'mybot@myhost.tld'
    );
    
    $this->cfg_server = array(
      'server'             => 'domain.tld',
      'port'               => 6667
    );
    
    $this->cfg_stats = array(
      'enable'              => false
    );
    
    $this->cfg_channels  = array();
    $this->botConnection = null;
    $this->bold = '';
    $this->reverse = '';
    $this->color = '';
  }
  
  public function setConfig($type, $name, $value) {
    if($type=='bot') {
      $this->cfg_bot[$name] = $value;
    }
    if($type=='server') {
      $this->cfg_server[$name] = $value;
    }
    if($type=='stats') {
      $this->cfg_stats[$name] = $value;
    }
  }
      
  public function addChannelsToJoin($channels) {
    $channels = explode(',', $channels);
    foreach($channels as $channel) {
      $channel = trim($channel);
      if(!empty($channel)) {
        $this->cfg_channels[] = $channel;
      }
    }
  }
  
  private function sendIrcMessage($message) {
    fwrite($this->botConnection, $message);
  }
  
  private function identifyBot() {
    $this->sendIrcMessage('USER '.$this->cfg_bot['nick'].' '.$this->cfg_bot['mail'].' '.$this->cfg_bot['host'].' :'.$this->cfg_bot['nick']."\r\n");
    $this->sendIrcMessage('NICK '.$this->cfg_bot['nick']."\r\n");
  }
  
  private function joinChannels() {
    foreach( $this->cfg_channels as $channel) {
      $this->sendIrcMessage('JOIN '.$channel."\r\n");
    }
  }
  
  private function getTime() {
    return strtolower(date('d/m/y H:i:s', time()));
  }
  
  private function pong($ping) {
    $this->sendIrcMessage('PONG '.$ping);
  }
  
  public function makeCfgStats($cfg) {
    $this->cfg_stats = $cfg;
  }
  
  private function analyseLine($line) {
    static $instance = false;
    if(preg_match('`^PING`', $line)) {
      $explode = explode(' ', $line);
      $this->pong($explode[1]);
    }
    
    else {
      preg_match("`^:([^!]+)!([^ ]+) *([^ ]+) *([^ ]+) *:`isU", $line, $infos);
      $texte = str_replace($infos[0], '', $line);
      
      
      if(isset($infos[4])) {
        $channel = trim($infos[4]);
        if($channel == 'JOIN') {
          $channel = trim(array_pop(explode(':', $line)));
        }
      
        if(ereg('!regles', $line) && $infos[1] != $this->cfg_bot['nick']) {
          $this->giveRegles($infos[1], $channel);
        }
        if(ereg('!help2', $line) && $infos[1] != $this->cfg_bot['nick']) {
          $this->giveHelp2($infos[1], $channel);
        }
        elseif(ereg('!help3', $line) && $infos[1] != $this->cfg_bot['nick']) {
          $this->giveHelp3($infos[1], $channel);
        }
        elseif(ereg('!help4', $line) && $infos[1] != $this->cfg_bot['nick']) {
          $this->giveHelp4($infos[1], $channel);
        }
        elseif(ereg('!help5', $line) && $infos[1] != $this->cfg_bot['nick']) {
          $this->giveHelp5($infos[1], $channel);
        }
        elseif(ereg('!help', $line) && $infos[1] != $this->cfg_bot['nick']) {
          $this->giveHelp($infos[1], $channel);
        }

        if(preg_match('`^!calin`', $texte)) {
          $to = str_replace('!calin', '', $texte);
          if(trim($to) != '') {
            $this->sendCalin($infos[1], trim($to), $channel);
          }
        }
      
        if(preg_match('`^!owned`', $texte)) {
          $to = str_replace('!owned', '', $texte);
          if(trim($to) != '') {
            $this->sendOwned($infos[1], trim($to), $channel);
          }
        }
      
        if(preg_match('`^!boulet`', $texte)) {
          $to = str_replace('!boulet', '', $texte);
          if(trim($to) != '') {
            $this->sendBoulet($infos[1], trim($to), $channel);
          }
        }
      
        if(preg_match('`^!taloche`', $texte)) {
          $to = str_replace('!taloche', '', $texte);
          if(trim($to) != '') {
            $this->sendTaloche($infos[1], trim($to), $channel);
          }
        }      
       
        if(preg_match('`^!hache`', $texte)) {
          $to = str_replace('!hache', '', $texte);
          if(trim($to) != '') {
            $this->sendHache($infos[1], trim($to), $channel);
          }
        }      

        if(preg_match('`^!apwal`', $texte)) {
          $to = str_replace('!apwal', '', $texte);
          if(trim($to) != '') {
            $this->sendApwal($infos[1], trim($to), $channel);
          }
        }    
      
        if(preg_match('`^!bisous`', $texte)) {
          $to = str_replace('!bisous', '', $texte);
          if(trim($to) != '') {
            $this->sendBisous($infos[1], trim($to), $channel);
          }
        }      
      
        if(preg_match('`^!ange`', $texte)) {
          $to = str_replace('!ange', '', $texte);
          if(trim($to) != '') {
            $this->sendAnge($infos[1], trim($to), $channel);
          }
        }    
      
        if(preg_match('`^!clope`', $texte)) {
          $to = str_replace('!clope', '', $texte);
          if(trim($to) != '') {
            $this->sendClope($infos[1], trim($to), $channel);
          }
        }    

        if(preg_match('`^!cafe`', $texte)) {
          $to = str_replace('!cafe', '', $texte);
          if(trim($to) != '') {
            $this->sendCafe($infos[1], trim($to), $channel);
          }
        }    

        if(preg_match('`^!boude`', $texte)) {
          $to = str_replace('!boude', '', $texte);
          if(trim($to) != '') {
            $this->sendBoude($infos[1], trim($to), $channel);
          }
        }    

        if(preg_match('`^!taggle`', $texte)) {
          $to = str_replace('!taggle', '', $texte);
          if(trim($to) != '') {
            $this->sendTaggle($infos[1], trim($to), $channel);
          }
        }    
    
        if(preg_match('`^!love`', $texte)) {
          $to = str_replace('!love', '', $texte);
          if(trim($to) != '') {
            $this->sendLove($infos[1], trim($to), $channel);
          }
        }        
      

        if(preg_match('`^!hate`', $texte)) {
          $to = str_replace('!hate', '', $texte);
          if(trim($to) != '') {
            $this->sendHate($infos[1], trim($to), $channel);
          }
        }        
      
        if(preg_match('`^!ame`', $texte)) {
          $to = str_replace('!ame', '', $texte);
          if(trim($to) != '') {
            $this->sendAme($infos[1], trim($to), $channel);
          }
        }    
      
        if(preg_match('`^!cux`', $texte)) {
          $to = str_replace('!cux', '', $texte);
          if(trim($to) != '') {
            $this->sendCux($infos[1], trim($to), $channel);
          }
        }    
      
        if(preg_match('`^!nutella`', $texte)) {
          $to = str_replace('!nutella', '', $texte);
          if(trim($to) != '') {
            $this->sendNutella($infos[1], trim($to), $channel);
          }
        }    

        if(preg_match('`^!tupues`', $texte)) {
          $to = str_replace('!tupues', '', $texte);
          if(trim($to) != '') {
            $this->sendTupues($infos[1], trim($to), $channel);
          }
        }    
      
        if(preg_match('`^!prout`', $texte)) {
          $to = str_replace('!prout', '', $texte);
            $this->sendProut($infos[1], trim($to), $channel);
        }    
      
        if(preg_match('`^!sarko`', $texte)) {
          $to = str_replace('!sarko', '', $texte);
          if(trim($to) != '') {
            $this->sendSarko($infos[1], trim($to), $channel);
          }
        }    

        if(preg_match('`^!donner`', $texte)) {
          $to = str_replace('!donner', '', $texte);
          if(trim($to) != '') {
            while(substr_count($to, '  ')) {
              $to = str_replace('  ', ' ', $to);
            }
            $ex = explode(' ', trim($to));
            if(count($ex) > 1) {
              $to = trim($ex[0]);
              unset($ex[0]);
              $quoi = implode(' ', $ex);
              $this->sendDonner($infos[1], $to, $quoi, $channel);
            }
          }
        }    
            
        if(ereg(' JOIN ', $line) && $infos[1] != $this->cfg_bot['nick']) {
          $this->giveJoinInfos($infos[1]);
        }
    
        
        if( $this->cfg_stats['enable'] ) {
          if(ereg('!stats', $line) && $infos[1] != $this->cfg_bot['nick']) {
            $this->giveStatsUrl($channel);
          }
          if(!$instance) {
            $instance = new BotStats($this->cfg_stats, $this->cfg_server['server'], $this->cfg_channels) ;
          }
          $instance->updateLogs($this->getTime().' '.$line, $channel);
        }    
      }
    }
  }
  
  public function connectBot() {
    parent::__construct($this->cfg_server['server'], $this->cfg_server['port']);
    $this->doConnection();
    $this->botConnection = $this->connection;
    $this->identifyBot();
    $this->joinChannels();

    while (!feof($this->botConnection)) {
      $line = fgets($this->botConnection, 1024);
      $this->analyseLine($line);
    }
  }
  
  
  private function giveJoinInfos($nick) {
    $this->sendIrcMessage('NOTICE '.$nick.' Bienvenue sur le chan de Deblan.fr !'."\r\n");
    $this->sendIrcMessage('NOTICE '.$nick.' -----------------------------------------------------------------------'."\r\n");
    $this->sendIrcMessage('NOTICE '.$nick.' - Simmstein est le seul intervenant pour les reclamations vis a vis de l\'equipe de moderation'."\r\n");
    $this->sendIrcMessage('NOTICE '.$nick.' - Les regles sont accessibles via !regles'."\r\n");
    $this->sendIrcMessage('NOTICE '.$nick.' - Retrouves les stats du chan en tappant !stats'."\r\n");
    $this->sendIrcMessage('NOTICE '.$nick.' - Tapes !help pour avoir la liste des commandes utilisees pour DebStats'."\r\n");
    $this->sendIrcMessage('NOTICE '.$nick.' Amuse-toi bien ;)'."\r\n");
    $this->sendIrcMessage('NOTICE '.$nick.' -----------------------------------------------------------------------'."\r\n");
  }
  
  private function giveRegles($nick) {
    $this->sendIrcMessage('NOTICE '.$nick.' Regles du chan irc :'."\r\n");
    $this->sendIrcMessage('NOTICE '.$nick.' -----------------------------------------------------------------------'."\r\n");
    $this->sendIrcMessage('NOTICE '.$nick.' - Pas d\'insultes qui blaissent. Vive le second degre'."\r\n");
    $this->sendIrcMessage('NOTICE '.$nick.' - Evite le flood'."\r\n");
    $this->sendIrcMessage('NOTICE '.$nick.' - En cas de probleme : simon@deblan.fr ou en prive'."\r\n");
    $this->sendIrcMessage('NOTICE '.$nick.' - Les operateurs font leur taf, ne remet pas en cause leurs sanctions'."\r\n");
    $this->sendIrcMessage('NOTICE '.$nick.' -----------------------------------------------------------------------'."\r\n");
  }
  
  private function giveHelp($nick) {
    $this->sendIrcMessage('NOTICE '.$nick.' Liste des commandes utilisees pour DebStats (partie 1) :'."\r\n");
    $this->sendIrcMessage('NOTICE '.$nick.' -----------------------------------------------------------------------'."\r\n");
    $this->sendIrcMessage('NOTICE '.$nick.' - LE RESTE DES COMMANDES : !help2, !help3, !help4, !help5'."\r\n");
    $this->sendIrcMessage('NOTICE '.$nick.' - Les regles du chan sont dispos via : !regles'."\r\n");
    $this->sendIrcMessage('NOTICE '.$nick.' - Retrouver les stats du chan : !stats'."\r\n");
    $this->sendIrcMessage('NOTICE '.$nick.' - Faire un gros calin : !calin <destinataire>'."\r\n");
    $this->sendIrcMessage('NOTICE '.$nick.' - Faire des bisous : !bisous <destinataire>'."\r\n");
    $this->sendIrcMessage('NOTICE '.$nick.' - Fait savoir que tu aimes une personnes... : !love <destinataire>'."\r\n");
    $this->sendIrcMessage('NOTICE '.$nick.' -----------------------------------------------------------------------'."\r\n");
  }
  
  private function giveHelp2($nick) {
    $this->sendIrcMessage('NOTICE '.$nick.' Liste des commandes utilisees pour DebStats (partie 2) :'."\r\n");
    $this->sendIrcMessage('NOTICE '.$nick.' -----------------------------------------------------------------------'."\r\n");
    $this->sendIrcMessage('NOTICE '.$nick.' - LE RESTE DES COMMANDES : !help, !help3, !help4, !help5'."\r\n");
    $this->sendIrcMessage('NOTICE '.$nick.' - Frapper quelqu\'un (a) : !taloche <destinataire>'."\r\n");
    $this->sendIrcMessage('NOTICE '.$nick.' - Oh quel ange :o : !ange <destinataire>'."\r\n");
    $this->sendIrcMessage('NOTICE '.$nick.' - Donner une clope : !clope <destinataire>'."\r\n");
    $this->sendIrcMessage('NOTICE '.$nick.' - Servir un cafe : !cafe <destinataire>'."\r\n");
    $this->sendIrcMessage('NOTICE '.$nick.' - Donner quelque chose : !donner <destinataire> <la chose>'."\r\n");
    $this->sendIrcMessage('NOTICE '.$nick.' -----------------------------------------------------------------------'."\r\n");
  }

  private function giveHelp3($nick) {
    $this->sendIrcMessage('NOTICE '.$nick.' Liste des commandes utilisees pour DebStats (partie 3) :'."\r\n");
    $this->sendIrcMessage('NOTICE '.$nick.' -----------------------------------------------------------------------'."\r\n");
    $this->sendIrcMessage('NOTICE '.$nick.' - LE RESTE DES COMMANDES : !help, !help2, !help4, !help5'."\r\n");
    $this->sendIrcMessage('NOTICE '.$nick.' - Une envie soudaine qu\'il/elle ferme saggle ? : !taggle <destinataire>'."\r\n");
    $this->sendIrcMessage('NOTICE '.$nick.' - Tu veux bouder quelqu\'un ? : !boude <destinataire>'."\r\n");
    $this->sendIrcMessage('NOTICE '.$nick.' - Dis lui de se mettre a pwal !! : !apwal <destinataire>'."\r\n");
    $this->sendIrcMessage('NOTICE '.$nick.' - Paix a son ame... : !ame <destinataire>'."\r\n");
    $this->sendIrcMessage('NOTICE '.$nick.' - Prepare toi a bucheronner quelqu\'un avec une hache... : !hache <destinataire>'."\r\n");
    $this->sendIrcMessage('NOTICE '.$nick.' -----------------------------------------------------------------------'."\r\n");
  }

  private function giveHelp4($nick) {
    $this->sendIrcMessage('NOTICE '.$nick.' Liste des commandes utilisees pour DebStats (partie 4) :'."\r\n");
    $this->sendIrcMessage('NOTICE '.$nick.' -----------------------------------------------------------------------'."\r\n");
    $this->sendIrcMessage('NOTICE '.$nick.' - LE RESTE DES COMMANDES : !help, !help2, !help3, !help5'."\r\n");
    $this->sendIrcMessage('NOTICE '.$nick.' - Tu hais quelqu\'un ? : !hate <destinataire>'."\r\n");
    $this->sendIrcMessage('NOTICE '.$nick.' - Tartiner une personne avec du nutella : !nutella <destinataire>'."\r\n");
    $this->sendIrcMessage('NOTICE '.$nick.' - Tu veux lui pincer les fesses ? : !cux <destinataire>'."\r\n");
    $this->sendIrcMessage('NOTICE '.$nick.' - Elle/il pue ? Dis lui  : !tupues <destinataire>'."\r\n");
    $this->sendIrcMessage('NOTICE '.$nick.' - Arf, tu viens de peter... : !proute'."\r\n");
    $this->sendIrcMessage('NOTICE '.$nick.' -----------------------------------------------------------------------'."\r\n");
  }

  private function giveHelp5($nick) {
    $this->sendIrcMessage('NOTICE '.$nick.' Liste des commandes utilisees pour DebStats (partie 5) :'."\r\n");
    $this->sendIrcMessage('NOTICE '.$nick.' -----------------------------------------------------------------------'."\r\n");
    $this->sendIrcMessage('NOTICE '.$nick.' - LE RESTE DES COMMANDES : !help, !help2, !help3, !help4'."\r\n");
    $this->sendIrcMessage('NOTICE '.$nick.' - On a besoin d\'un karcher... : !sarko <destinataire>'."\r\n");
    $this->sendIrcMessage('NOTICE '.$nick.' - Mais quel boulet... : !boulet <destinataire>'."\r\n");
    $this->sendIrcMessage('NOTICE '.$nick.' - Il l\'a owned :D : !owned <destinataire>'."\r\n");
    $this->sendIrcMessage('NOTICE '.$nick.' -----------------------------------------------------------------------'."\r\n");
  }

  private function giveStatsUrl($channel) {
    $file = $this->cfg_stats['pisg_html_page_dir'].$this->cfg_server['server'].'.'.str_replace('#', '', $channel).'.html';
    if( file_exists($file) ) {
      $ftime = filemtime($file);
      $this->sendIrcMessage('PRIVMSG '.$channel.' '.$this->color.'10 Url : http://irc.deblan.fr/stats.php?chan='.$this->cfg_server['server'].'.'.str_replace('#', '', $channel).' - Derniere mise a jour il y a '.$this->bold.date('i', 
time()-$ftime)." minute(s) et ".date('s', time()-$ftime)." seconde(s)\r\n");
    }
  }

  private function giveInfos() {
    $this->sendIrcMessage('PRIVMSG '.$channel.' Je suis le bot qui s\'occupe de generer des logs pour le chan. Merci de ne pas me faire chier !'."\r\n");
  }
  
  private function sendCalin($from, $to, $channel) {
    if(strtolower($from) == strtolower($to)) $this->sendSkyzo($from);
    else
    $this->sendIrcMessage('PRIVMSG '.$channel.' '.$this->color.'13'.$from.$this->color.'10 fait un groooOOOooos calin a '.$this->color.'13'.$to.$this->color.'4 <3<3<3<3<3<3<3'."\r\n");
  }
  
  private function sendTaloche($from, $to, $channel) {
    if(strtolower($from) == strtolower($to)) $this->sendSkyzo($from);
    else
    $this->sendIrcMessage('PRIVMSG '.$channel.' '.$this->color.'13'.$from.$this->color.'10 balance une grosse taloche dans la gueule a '.$this->color.'13'.$to.$this->color.'4 !!!'."\r\n");

  }  
  
  private function sendHache($from, $to, $channel) {
    if(strtolower($from) == strtolower($to)) $this->sendSkyzo($from);
    else
    $this->sendIrcMessage('PRIVMSG '.$channel.' '.$this->color.'13'.$from.$this->color.'10 aiguise sa hache en regardant '.$this->color.'13'.$to.$this->color.'10 tel un bucheron devant un vieux chene...'."\r\n");
  }  
  
  private function sendBisous($from, $to, $channel) {
    if(strtolower($from) == strtolower($to)) $this->sendSkyzo($from);
    else
    $this->sendIrcMessage('PRIVMSG '.$channel.' '.$this->color.'13'.$from.$this->color.'10 fait tout plein de gros bisous a '.$this->color.'13'.$to.$this->color.'4 <3<3<3'."\r\n");
  } 
 
  private function sendBoude($from, $to, $channel) {
    if(strtolower($from) == strtolower($to)) $this->sendSkyzo($from);
    else
    $this->sendIrcMessage('PRIVMSG '.$channel.' '.$this->color.'13'.$from.$this->color.'10 detourne la tete et boude '.$this->color.'13'.$to.$this->color.'10 =('."\r\n");
  } 
 
  private function sendLove($from, $to, $channel) {
    if(strtolower($from) == strtolower($to)) $this->sendSkyzo($from);
    else
    $this->sendIrcMessage('PRIVMSG '.$channel.' '.$this->color.'13'.$to.$this->color.'10 ? Y\'a '.$this->color.'13'.$from.$this->color.'10 qui t\'aime...'.$this->color.'4 <3<3<3'."\r\n");
  } 
 
  private function sendHate($from, $to, $channel) {
    if(strtolower($from) == strtolower($to)) $this->sendSkyzo($from);
    else
    $this->sendIrcMessage('PRIVMSG '.$channel.' '.$this->color.'13'.$to.$this->color.'10 ? '.$this->color.'13'.$from.$this->color.'10 te fait savoir qu\'il a envie de te pendre avec tes tripes'.$this->color.'4 :@'."\r\n");
  } 
  
  private function sendBoulet($from, $to, $channel) {
    if(strtolower($from) == strtolower($to)) $this->sendSkyzo($from);
    else
    $this->sendIrcMessage('PRIVMSG '.$channel.' '.$this->color.'10Bordel...'.$this->color.'13'.$to.$this->color.'10...C\'est vraiment qu\'un boulet ><'."\r\n");
  }
  
  private function sendAme($from, $to, $channel) {
    if(strtolower($from) == strtolower($to)) $this->sendSkyzo($from);
    else
    $this->sendIrcMessage('PRIVMSG '.$channel.' '.$this->color.'10Paix a ton ame '.$this->color.'13'.$to.$this->color.'10...'.$this->bold.$this->color.'1 +'."\r\n");
  } 
  
  private function sendTupues($from, $to, $channel) {
    if(strtolower($from) == strtolower($to)) $this->sendSkyzo($from);
    else
    $this->sendIrcMessage('PRIVMSG '.$channel.' '.$this->color.'13'.$to.$this->color.'10 TUPUES !!!'."\r\n");
  } 
  
  private function sendProut($from, $to, $channel) {
    if(strtolower($from) == strtolower($to)) $this->sendSkyzo($from);
    else
    $this->sendIrcMessage('PRIVMSG '.$channel.' '.$this->color.'13'.$from.$this->color.'10 est ignoble, il vient de peter...'.$this->bold.$this->color.'3 :s'."\r\n");
  } 
  
  private function sendNutella($from, $to, $channel) {
    if(strtolower($from) == strtolower($to)) $this->sendSkyzo($from);
    else
    $this->sendIrcMessage('PRIVMSG '.$channel.' '.$this->color.'13'.$from.$this->color.'10 tartineuh '.$this->color.'13'.$to.$this->color.'10 avec amoureuh ^^'."\r\n");
  } 
  
  private function sendCux($from, $to, $channel) {
    if(strtolower($from) == strtolower($to)) $this->sendSkyzo($from);
    else
    $this->sendIrcMessage('PRIVMSG '.$channel.' '.$this->color.'13'.$from.$this->color.'10 pince les fesses de '.$this->color.'13'.$to.$this->color.'10 Owi !!!'."\r\n");
  }
  
  private function sendClope($from, $to, $channel) {
    if(strtolower($from) == strtolower($to)) $this->sendSkyzo($from);
    else
    $this->sendIrcMessage('PRIVMSG '.$channel.' '.$this->color.'13'.$from.$this->color.'10 offre une clope a '.$this->color.'13'.$to."\r\n");
  }   
  
  private function sendSarko($from, $to, $channel) {
    if(strtolower($from) == strtolower($to)) $this->sendSkyzo($from);
    else
    $this->sendIrcMessage('PRIVMSG '.$channel.' '.$this->color.'13'.$to.$this->color.'10, va chercher le karcher...y\'a des racailles ici'."\r\n");
  } 
  
  private function sendCafe($from, $to, $channel) {
    if(strtolower($from) == strtolower($to)) $this->sendSkyzo($from);
    else
    $this->sendIrcMessage('PRIVMSG '.$channel.' '.$this->color.'13'.$from.$this->color.'10 sert un bon cafe a '.$this->color.'13'.$to."\r\n");
  }  
  
 
  private function sendAnge($from, $to, $channel) {
    if(strtolower($from) == strtolower($to)) $this->sendSkyzo($from);
    else
    $this->sendIrcMessage('PRIVMSG '.$channel.' '.$this->color.'13'.$to.$this->color.'10 est un vrai ange ^^ '.$this->bold.$this->color.'8O'.$this->color.'7:)'."\r\n");
  } 
  
  private function sendTaggle($from, $to, $channel) {
    if(strtolower($from) == strtolower($to)) $this->sendSkyzo($from);
    else
    $this->sendIrcMessage('PRIVMSG '.$channel.' '.$this->color.'13'.$from.$this->color.'10 propose a '.$this->color.'13'.$to.$this->color.'10 de '.$this->color.'4fermer sa grande gueule '.$this->color.'10XD'."\r\n");
  }  
  
  private function sendApwal($from, $to, $channel) {
    if(strtolower($from) == strtolower($to)) $this->sendSkyzo($from);
    else
    $this->sendIrcMessage('PRIVMSG '.$channel.' '.$this->color.'13'.$from.$this->color.'10 voudrait bien que '.$this->color.'13'.$to.$this->color.'10 se '.$this->color.'4foutte a APWAL !!! '."\r\n");
  }    
  
  private function sendDonner($from, $to, $quoi, $channel) {
    if(strtolower($from) == strtolower($to)) $this->sendSkyzo($from);
    else
    $this->sendIrcMessage('PRIVMSG '.$channel.' '.$this->color.'13'.$from.$this->color.'10 donne '.$this->color.'4'.$quoi.$this->color.'10 a '.$this->color.'13'.$to."\r\n");
  }
  
  private function sendSkyzo($nick) {
    $this->sendIrcMessage('PRIVMSG '.$channel.' '.$this->color.'10Oh putain !! '.$this->color.'13'.$nick.$this->color.'10 est sckyzo !!'."\r\n");
  }
  
  private function sendOwned($from, $to, $channel) {
    $this->sendIrcMessage('PRIVMSG '.$channel.' '.$this->color.'10Mouahahahahahaha comment il t\'a owned '.$this->color.'13'.$to.$this->color.'10 /o/'."\r\n");
  } 
}

class BotStats extends Bot {
  protected $cfg;
  protected $server;
  private $logs_files_names;
  private $flog_files;
  private $channels = array();
  
  public function __construct($cfg_stats, $server, $channels) {
    $this->flog_files    = array(); 
    $this->logs_files_names = array();  
    $this->channels = $channels;
    $this->server = $server;
    
    try {
      if( $this->canUseBotStats() ) {
         $this->makeConfiguration($cfg_stats);
         $this->make_logs_files_names($channels);
         $this->openLogsFiles();
         
      } else {
        throw new MyException('Impossible d\'utiliser les stats ! Fonction(s) php manquante(s) ou inactive(s)');
      }
    } catch (Exception $e) {
      $e->echoErreur(true);
    }
  }
  
  
  private function makeConfiguration($cfg_stats) {
    $this->cfg = array(
      'log_file_dir'       => '', 
      'max_log_line'       => 1000,
      'pisg_html_page_dir' => '',
      'stats'              => false
    );
    
    foreach($cfg_stats as $key => $_cfg) {
      if(isset( $this->cfg[$key] )) {
        $this->cfg[$key] = $_cfg;
       }
    }
  }
  
  private function canUseBotStats() {
    $possible = true;
    if(!function_exists('fsockopen')) $possible = false;
    if(!function_exists('fwrite'))    $possible = false;
    if(!function_exists('feof'))      $possible = false;
    if(!function_exists('fgets'))     $possible = false;
    return $possible;
  }
  
  private function get_log_file_name($channel) {
    if(isset( $this->cfg[$channel] ) ) {
      return $this->cfg[$channel];
    } else {
      return false;
    }
  }  
  
  private function getNumberLogLine($channel) {
    if(file_exists($this->get_log_file_name($channel))) {
      if(is_readable($this->get_log_file_name($channel))) {
        return count(file($this->get_log_file_name($channel)));
      } else {
        return 0;
      }
    } else {
      return 0;
    }
  }
  
  private function make_logs_files_names() {
    foreach($this->channels as $channel) {
      $this->logs_files_names[$channel] = $this->cfg['log_file_dir'].$this->server.'.'.str_replace(array('"', '#'), '', $channel).'.log';
    }    
  }
  
  private function openLogsFiles() {
    $ok = true;
    foreach( $this->logs_files_names as $file) {
      if( file_exists($file)) {
        if(!is_readable($file)) {
          $erreur.= 'ERREUR : Le fichier de log "'.$file.'" n\'est pas lisible !'."\r\n";
          $ok = false;
        }
      }
    }

    try {
      if(!$ok) {
        throw new MyException($erreur);
      }
    } catch (Exception $e) {
       $e->echoErreur(true);
    }
    try { 
      foreach( $this->logs_files_names as $key => $file) {
        $this->flog_files[$key] = $this->openFile($file);
      }
    } catch (Exception $e) {
      $e->echoErreur(true);
    }
  }  
  
  private function openFile($file) {
    try {
      if(!$open = @fopen($file, 'a+')) {
        throw new MyException('ERREUR : impossible d\'ouvrir : '.$file."\r\n");
      }
      return $open;
    } catch (Exception $e) {
      $e->echoErreur(true);
    }
  }
  
  public function updateLogs($log_line, $channel) {
    try { 
      if(!$write = @fwrite($this->flog_files[$channel], $log_line)) {
        throw new MyException('ERREUR : Impossible d\'ecrire dans le fichier de log ('.$this->get_log_file_name($channel).')'."\r\n");
      }
    } catch (MyException $e) {
      $e->echoErreur(false);
    }
  }  
}

function makeGet() {
  global $argv;
  unset($argv[0]);
  $get = array();
  $c = count($argv);
  if($c > 0) {
    if($c%2 == 0) {
      foreach($argv as $key=>$vl) {
        if($key%2==1) {
          $get[$vl] = $argv[$key+1];
        }
      }
    }
  }
  return $get;
}

$get = makeGet();

$bot = new Bot();

$bot->setConfig('server', 'server', 'deblan.fr');
$bot->setConfig('server', 'port', 6667);
$bot->setConfig('stats', 'enable', true);

if(isset($get['-serveur']))  $bot->setConfig('server', 'server', $get['-serveur']);
if(isset($get['-port']))     $bot->setConfig('server', 'port', $get['-port']);
if(isset($get['-channels'])) $bot->addChannelsToJoin($get['-channels']);

if(isset($get['-nick']))  $bot->setConfig('bot', 'nick', $get['-nick']);
if(isset($get['-host']))  $bot->setConfig('bot', 'host', $get['-host']);
if(isset($get['-mail']))  $bot->setConfig('bot', 'mail', $get['-mail']);

if(isset($get['-log_file_dir'])) $bot->setConfig('stats', 'log_file_dir', $get['-log_file_dir']);
if(isset($get['-max_log_line'])) $bot->setConfig('stats', 'max_log_line', $get['-max_log_line']);
if(isset($get['-pisg_html_page_dir'])) $bot->setConfig('stats', 'pisg_html_page_dir', $get['-pisg_html_page_dir']);

if(isset($get['-enable_stats'])) {
  if($get['-enable_stats'] == 'on') {
    $bot->setConfig('stats', 'enable', true);
  }
  if($get['-enable_stats'] == 'off') {
    $bot->setConfig('stats', 'enable', false);
  }
}

$bot->connectBot();

Conclusion :


Évidemment, il est largement améliorable ^^

Critiques constructives demandées (positives ou pas hein :p) ^^

A voir également

Ajouter un commentaire Commentaires
coucou747 Messages postés 12303 Date d'inscription mardi 10 février 2004 Statut Membre Dernière intervention 30 juillet 2012 44
23 oct. 2008 à 16:02
j'aime bien le concept.

idealement, t'aurais du faire une interface genre IrcReader avec des methodes genre :
OnPrivMsg
OnNotice
etc...
simonviei Messages postés 17 Date d'inscription jeudi 13 avril 2006 Statut Membre Dernière intervention 1 décembre 2008
22 oct. 2008 à 08:49
Après avoir ajouté diverses focntion à mon bot, mais qui se trouvaient dans la class Irc_log elle même, j'ai décidé de refaire tout le code :
Une mini gestion des erreurs, une class de connexion, une class pour le bot, une class pour les stats.

J'ai encore des bugs, mais pour l'instant, rien de méchant, je me met dessus cette après midi.
malalam Messages postés 10839 Date d'inscription lundi 24 février 2003 Statut Membre Dernière intervention 2 mars 2010 25
2 juil. 2008 à 22:32
Hello,

je ne vais pas redire ce qui a été dit; donc, j'ajouterais juste que je trouve ça pas mal comme code : c'est propre, clair, pas mal codé...pour un débutant en PHP5, c'est très bien :-)
Morphinof Messages postés 255 Date d'inscription vendredi 20 avril 2007 Statut Membre Dernière intervention 9 août 2013 4
2 juil. 2008 à 12:16
En fait c'est pas très compliqué, comme tu as pu le voir le bloc try{ ... }catch(Exception $e){ ... } est fait pour "attraper" les exceptions qui seraient "jetées" par ton application.
Tu dois identifier les erreurs qui peuvent survenir, pennons l'exemple de ta fonction connexion, une exception évidente serait : on n'as pas réussi à ouvrir notre socket parce que par exemple le serveur n'existe pas ou encore le port est bloqué, il te faudra donc tester le $errno de fsockopen et selon sa valeur tu va jeter une exception correspondant au type d'erreur et tu peu même incorporer dans le message de ton exception $errstr, exemple :

throw new UnknowServerException('Exception : '.$errstr);
throw new PortException('Exception : '.$errstr);
(Ce ne sont que deux rapides exemples)

Bien évidement tu vas devoir créer les classes correspondant aux exceptions en les faisant hériter de la classe Exception et ensuite dès que tu va faire appel à ta fonction connexion c'est la que tu met ton bloc try{}catch(){} :)

Pour une meilleure explication : http://classes.scriptsphp.org/article.PHP-5-et-les-exceptions
codefalse Messages postés 1123 Date d'inscription mardi 8 janvier 2002 Statut Modérateur Dernière intervention 21 avril 2009 1
2 juil. 2008 à 12:06
De là à dire que tu es "débile", quand même ! :p J'en serai presque à supprimer ton post pour non respect de ... toi même ;)

Si tu passe tes scripts de php4 à 5, c'est forcément sur que tu oublie des choses, tu peux pas penser à tout ! Tes erreurs sont justifiées et ton acte est louable (enfin un qui comprends le fait de ne plus utiliser php4 !).

Tu verra, __get et __set, c'est que du bonheur !

Mes remarques ne sont pas très lourdes, ton code est corrigeable en une heure (sauf la dernière partie).

Bonne recherches :) n'hésite pas si tu bloque

Vous n'êtes pas encore membre ?

inscrivez-vous, c'est gratuit et ça prend moins d'une minute !

Les membres obtiennent plus de réponses que les utilisateurs anonymes.

Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.

Le fait d'être membre vous permet d'avoir des options supplémentaires.