Vous n'êtes pas identifié(e).

Top posteurs

Statistiques

Partenaires

  • eXolia Hosting
  • Eggdrop.fr

#1 08/11/2011 23:35:18

TiSmA
Administrateur
Lieu : Saint Martin (87)
Inscription : 18/08/2004
Messages : 43
Site Web

[PHP] Construire un bot IRC

Bonsoir,

Voici un petit snippet qui a pour but de vous montrez le système de connexion d'un bot IRC en language PHP :

Nom du fichier : bot.php
Modification code : adresse-du-serveur / port-du-serveur

Code: PHP
<?php
 
/**
 * Simple PHP IRC Bot
 *
 * PHP Version 5
 *
 * LICENSE: This source file is subject to Creative Commons Attribution
 * 3.0 License that is available through the world-wide-web at the following URI:
 * [url]http://creativecommons.org/licenses/by/3.0/[/url].  Basically you are free to adapt 
 * and use this script commercially/non-commercially. My only requirement is that
 * you keep this header as an attribution to my work. Enjoy! 
 *
 * @category   Chat Room Scipt
 * @package    Simple PHP IRC Bot 
 * @author     Super3boy <admin@wildphp.com>
 * @copyright  2010, The Nystic Network
 * @license    [url]http://creativecommons.org/licenses/by/3.0/[/url]
 * @link       [url]http://wildphp.com[/url] (Visit for updated versions and more free scripts!)
 * @version    1.0.0 (Last updated 03-20-2010)
 *
 */
 
//So the bot doesnt stop.
set_time_limit(0);
ini_set('display_errors', 'on');
 
//Sample connection data.
$config = array( 
        'server' => 'adresse-du-serveur', 
        'port'   => port-du-serveur, 
        'channel' => '#IRCbot',
        'name'   => 'IRCbot', 
        'nick'   => 'IRCbot', 
        'pass'   => '', 
);
 
class IRCBot {
 
        //This is going to hold our TCP/IP connection
        var $socket;
 
        //This is going to hold all of the messages both server and client
        var $ex = array();
 
        /*
 
         Construct item, opens the server connection, logs the bot in
         @param array
 
        */
 
        function __construct($config)
 
        {
                $this->socket = fsockopen($config['server'], $config['port']);
                $this->login($config);
                $this->main($config);
        }
 
 
 
        /*
 
         Logs the bot in on the server
         @param array
 
        */
 
        function login($config)
        {
                $this->send_data('USER', $config['nick'].' wildphp.com '.$config['nick'].' :'.$config['name']);
                $this->send_data('NICK', $config['nick']);
		$this->join_channel($config['channel']);
        }
 
 
 
        /*
 
         This is the workhorse function, grabs the data from the server and displays on the browser
 
        */
 
        function main($config)
        {             
                $data = fgets($this->socket, 256);
 
                echo nl2br($data);
 
                flush();
 
                $this->ex = explode(' ', $data);
 
 
                if($this->ex[0] == 'PING')
                {
                        $this->send_data('PONG', $this->ex[1]); //Plays ping-pong with the server to stay connected.
                }
 
                $command = str_replace(array(chr(10), chr(13)), '', $this->ex[3]);
 
                switch($command) //List of commands the bot responds to from a user.
                {                      
                        case ':!join':
                                $this->join_channel($this->ex[4]);
                                break;                     
                        case ':!part':
                                $this->send_data('PART '.$this->ex[4].' :', 'Wildphp.com Free IRC Bot Script');
                                break;   
 
                        case ':!say':
                                $message = "";
                                for($i=5; $i <= (count($this->ex)); $i++)
                                {
                                        $message .= $this->ex[$i]." ";
                                }
 
                                $this->send_data('PRIVMSG '.$this->ex[4].' :', $message);
                                break;                        		
 
                        case ':!restart':
                                echo "<meta http-equiv=\"refresh\" content=\"5\">";
                                exit;
                        case ':!shutdown':
                        		$this->send_data('QUIT', 'Wildphp.com Free IRC Bot Script');
                                exit;
                }
 
                $this->main($config);
        }
 
 
 
        function send_data($cmd, $msg = null) //displays stuff to the broswer and sends data to the server.
        {
                if($msg == null)
                {
                        fputs($this->socket, $cmd."\r\n");
                        echo '<strong>'.$cmd.'</strong><br />';
                } else {
 
                        fputs($this->socket, $cmd.' '.$msg."\r\n");
                        echo '<strong>'.$cmd.' '.$msg.'</strong><br />';
                }
 
        }
 
 
 
        function join_channel($channel) //Joins a channel, used in the join function.
        {
 
                if(is_array($channel))
                {
                        foreach($channel as $chan)
                        {
                                $this->send_data('JOIN', $chan);
                        }
 
                } else {
                        $this->send_data('JOIN', $channel);
                }
        }     
}
 
//Start the bot
$bot = new IRCBot($config);
?>

Plus d'informations sur le site de l'auteur : http://www.wildphp.com/scripts/simple-php-irc-bot/


Inutile d'avoir un Tigre dans son ordi, s'il y'a un âne derrière l'écran...  big_smile

Hors ligne

#2 09/11/2011 08:09:09

DoxYs
Nouveau IRCzien
Inscription : 18/06/2011
Messages : 16

Re : [PHP] Construire un bot IRC

Bonjour,

Je pense que l'utilisation des function() peuvent encore être amélioré wink.
Je pense aussi qu'il serait plus envisageable de faire le même robot mais pas en PHP5 (étant donné que la POO "pompe" plus de ressource que les autres version wink).

Cordialement,


Webmaster

Hors ligne

Pied de page des forums

502372 visites ( 232 aujourd'hui ) 3 visiteurs en ligne
Copyright © 2004 - 2013 IRCz