For those who don't know, the IRC (Internet Relay Chat) is a great place. It is sort of a classic version of a chat room. If you didn't know what IRC was, then you must not know about the Valhalla IRC!!! There should be an article about that soon in more detail if there isn't already.

Anyways, now that you know about the IRC, it is a good idea to check out the #valhalla channel. There you will find a user online named 'lore'. Lore is ynori7's amazing IRC bot who can do many many awesome things. I am going to teach you the basics of making one of those, and you will hopefully be able to add more and more features to yours.

Now to the actual tutorial. In this tutorial, I assume you know the basics of PHP, as in variables, if else, and while loops, as well as for loops. The whole thing. Essentially, I just need you to get the ups and downs of it. I also assume that you have a tool such as Notepad++ or MacVim to use as an editor. The coloring really helps me.

This whole program works inside a class, so you should have the code start with this:

<?php
class Bot {
}
$bot = new Bot();
?>

Now that you have that, there are like two things you need to set as well as some server things you need for a connection in an array.

<?php
set_time_limit(0);
ini_set('display_errors', 'on');
class Bot {
...
...
?>

The ... in the above code represents code that was already entered. The rest is just to give you a little perspective on where things like that go.

Now we need the array that will have all of the server settings including the nick and the server hostname and all that other good stuff. Make an array, and edit it accordingly, and we will cut it and paste it in the right spot.

$configure = array(
'server' => 'ssl://irc.psych0tik.net',
    'port' => 6697,
    'nick' => 'bot',
    'name' => 'bot',
    'channel' => '#botlab',
    'pass' => null
);

The reason that there is ssl:// before the hostname of the server you want to connect to is because this server uses ssl which is also known as secure socket layer. Also note that these are the server settings if you wanted your bot to connect to the botlab irc, which is where you should to test it out.

Now place that code snippet here, in the rest of your code:

 <?php
set_time_limit(0);
ini_set('display_errors', 'on');
$configure = array(
    'server' => 'ssl://irc.psych0tik.net',
    'port' => 6697,
    'nick' => 'minion1',
    'name' => "minion1",
    'channel' => '#valahalla',
    //'channel' => '#minionlab',
    'pass' => null
);
class Bot{
...
...
...
?>

Also, you would want to change the ending from this:

$bot = new Bot();

to this:

$bot = new Bot($configure);

Make sure that in the parenthesis after Bot that it is the name of the variable that had the array with the server settings in it.

Now, we will be setting up some channel and server joining functions, and I will show you how to make some simple functions for your bot to interact with the users of the channel, or whatever channel you tell it to join. Within the bot you need to set up some functions that we will go into detail on later:

function __construct($configure){
    $this->configure = $configure;
    $this->socket = fsockopen($configure['server'], $configure['port']);
    $this->login($configure);
    sleep(5);
    $this->send_data('JOIN', $this->configure['channel']);
    while (True) {
        $this->main();
    }
}
function login($configure){
    $this->send_data('USER', $configure['nick'] . ' desiredhost.com ' . $configure['nick'] . ' :' . $configure['name']);
    $this->send_data('NICK', $configure['nick']);
}
function main(){
    $data = fgets($this->socket, 128);
    $this->ex = explode(' ', $data);
    if (!$data) {
        echo "Error";
        echo "var dump $data:";
        echo "Var Dump of $data: ";
        var_dump($data);
        exit();
    } 
        
    //Comment the below out to avoid duplicates
    elseif (count($this->ex) < 3) {
        echo "Error";
        var_dump($data);
    } else {
        /* This is where all of the user interactive functions go */
    }  
}  
function send_data($cmd, $msg = null){
    if($msg == null){
        fputs($this->socket, $cmd . "rn");
        echo "<b>$cmd<gb>";
    }else{                 
        fputs($this->socket, $cmd.' '.$msg."rn");
        echo "<b>$cmd $msg<gb>";
    }
}
function join_channel($channel){
    $this->send_data('JOIN', $channel);
}

Feel free to insert that inside the Bot class in that order, it's inside a class so it doesn't really matter. Now you may be wondering what it all means. In what follows, I will go pretty much line by line explaining what goes on.