IRC bot that needs help

By wade2462 on Dec 28, 2009

I have some code that I wrote some code for an irc bot, part from a tutorial, and it will not sign into the server. Any help would greatly be appreciated.

<?php

//So the bot doesnt stop.

set_time_limit(0);

ini_set('display_errors', 'on');

    //Example connection stuff.

    $config = array( 
        'server' => 'irc.rizon.net', 
        'port' => 6667, 
        'nick' => 'Mlia', 
        'name' => 'Ihavenoname', 
        'pass' => 'meh', 
    );

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();

        $this->send_data('JOIN', '#tc');

    }

    /*

     Logs the bot in on the server

     @param array

    */

    function login($config)

    {

        $this->send_data('USER', $config['nick'].$config['nick'].' :'.$config['name']);

        $this->send_data('NICK', $config['nick']);

    }

    /*

     This is the workhorse function, grabs the data from the server and displays on the browser

    */

    function main()

    {

        $data = fgets($this->socket, 128);

        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 ':!mlia':

                $this->mlia($this->ex[4]);

                break;
        }

        $this->main();

    }

    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 Mlia($channel){
    $mlianumber = rand(0,436672);
    $mliapage = file_get_contents("http://mylifeisaverage.com/s/$mlianumber");
    $pattern = "<div class=\"sc\">(.|[\r\n])*?<\/div>";
preg_match("/$pattern/i",$mliapage,$match);
$match = $match[0];
$match = str_replace ("<div class=\"sc\">","",$match);
$match = str_replace ("</div>","",$match);
$match = str_replace ("\n","",$match);
$match = str_replace ("\r","",$match);
$match = trim($match);
$string = "4MLIA:1 $match";
send("PRIVMSG",$channel,$string);
}

    $bot = new IRCBot($config);

?>

Comments

Sign in to comment.
sunslayer   -  Dec 29, 2009

if you need help you should have used the forums
you need an extra } at the end of the class and the reason why it wont login is because of ur syntax in IRCBot::login u only gave 3 parameters, u need atleast 5 for the USER cmd

 Respond  
Are you sure you want to unfollow this person?
Are you sure you want to delete this?
Click "Unsubscribe" to stop receiving notices pertaining to this post.
Click "Subscribe" to resume notices pertaining to this post.