Author Topic: PHP IRC bot  (Read 372 times)

King_Hual

  • Trial Moderator
  • Newbie
  • ***
  • Posts: 36
  • Reputation +3/-2
    • View Profile
PHP IRC bot
« on: February 16, 2011, 06:17:07 pm »
Code: [Select]
<?php//So the bot doesnt stop.set_time_limit(0);ini_set('display_errors', 'on');//Sample connection data.$config = array('server' => 'irc.focogaming.com','port'   => 6667,'channel' => '#king_hual','name'   => 'Hual`PHP','nick'   => 'Hual`PHP','pass'   => '',);/*//Set your connection data.$config = array('server' => 'example.com','port'   => 6667,'channel' => '#channel','name'   => 'real name','nick'   => 'user','pass'   => 'pass',);*/class IRCBot {//This is going to hold our TCP/IP connectionvar $socket;//This is going to hold all of the messages both server and clientvar $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'].' Hual`PHP '.$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].' :', 'I like pie');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', 'I like pie');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);?>

The reason why I reposted a PHP irc bot script is because this is another IRC bot script. There are problems with the commands in the other bot script - you can only add 3 commands. But this one works fine, even though it's more complicated.

Share on Bluesky Share on Facebook


Jane

  • Management
  • Full
  • *******
  • Posts: 130
  • Reputation +1/-0
  • mIRC Scripters
    • View Profile
Re: PHP IRC bot
« Reply #1 on: February 16, 2011, 08:59:02 pm »
Nice and simple, Keep Learning xD