Description

Simple PHP IRC Bot, is a simple PHP IRC Bot class designed to connect to an IRC server, run a few simple commands.  This script is designed and intended to be built upon, perhaps you could build a PHP IRC Client, so please follow some of the notes below for guidance. This bot is based off a PHP IRC Bot tutorial done on our sister site. In the coming weeks more specialized PHP IRC Bots will be made from this script. It will be expanded and modified for many uses including channel logging and channel moderation, so don’t forget to subscribe.

Warning

This script uses PHP sockets, and most shared hosting and some servers have it disabled by default. This script runs continuously so it is not recommended to run on shared hosting accounts. We recommend you run it locally using the instructions below.

Installation

  1. Download or copy paste the script from this page
  2. (Optional) If downloaded rename the file to bot.php ( Sorry, the blog will only allow me to upload it as bot.txt)
  3. Edit the $config array in the script to the server settings, should be self explanatory
  4. Download and install XAMPP
  5. Place bot.php in the htdocs folder of your XAMPP installation
  6. Don’t forget to start the Apache server by pressing the start button in the XAMPP Control Panel
  7. Go to http://localhost/bot.php, and the bot should connect and join the channel you specified in about 30 seconds

Code

Download bot.txt (Right click, Save As…)

or


<?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:
* http://creativecommons.org/licenses/by/3.0/.  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    http://creativecommons.org/licenses/by/3.0/
* @link       http://wildphp.com (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' => 'chat.freenode.net',
'port'   => 6667,
'channel' => '#nystic_chat',
'name'   => 'wildphp-bot',
'nick'   => 'wildphp-bot',
'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 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);
?>

Features and Functions

  • !join #channel – Joins the specified IRC channel
  • !part #channel – Leaves the specified IRC channel
  • !say #channel Message – Says message in the specified IRC channel
  • !restart – Quits and restarts the script. (Helpful while testing the script)
  • !shutdown – Quits and stops the script.

Usage and output:

<random-user> !say #nystic_chat hello there
<wildphp-bot> hello there

Code Lines of Note

  • Lines 24 – 48: This is where you set your connection data
  • Line 101: Prints all the output you see on the screen
  • Line 115-140: Contains all commands
  • Line 180: Stats the bot

Conclusion

Thanks for trying out our PHP IRC bot. If their are any bugs in the code please let us know right away.


Comments are closed.