Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 1 of 1
  1. #1
    Regular Coder
    Join Date
    Jan 2009
    Location
    Norway
    Posts
    118
    Thanks
    8
    Thanked 2 Times in 2 Posts

    IRCBot (Dinkbot)

    This is an IRC bot I coded a while ago, it was my very first attempt so please don't laugh too much

    index.php
    PHP Code:
    <?php 
        
    require("bot.class.php");
        
    $con = new irc(); 
        echo 
    $con->ircConnect(); 
        echo 
    $con->irc_loop();
    ?>
    bot.class.php
    PHP Code:
    <?php
        
    /****************************************************************************************
        *                                     DinkBot v.0.1                                        *
        *****************************************************************************************
        This is the very first release of DinkBot and it's a bot I created for our IRC channel.
        I never really planned on creating this, but I found a guide on how to connect to an
        IRC server on CodingForum.com and figured I'd give it a shot. So in all honesty, I 
        figure that I owe the guy some credit, even though I rewrote everything and used
        PHP's OO to help me, even though it was not entirely necessary. This application
        is developed for use with PHP CLI. It will work in a browser as well, but you'd have to 
        slightly modify the code, but I would not recommend this, it's easier to read with the
        command line interface and it won't crash like the browser if too much information is 
        received from the server
        
        The link to the example I used is located here:
        http://www.codingforums.com/showthread.php?t=91765
        
        This IRC class has the following strcuture:
        class irc
            __construct()
            ircConnect()
            irc_loop()
            sendCmd()
            
        The constructor takes care of certain PHP settings that are needed for this bot to work
        it also restricts the ammount of errors PHP will output due to I want a clean console,
        though, when I develop morefeatures I switch it over the E_ALL to in case something
        goes wrong, but currently it just shows critical errors which will halt the script.
        The script has no timeout session either because I disabled that so it will remain
        stuck in it's loop.
        
        The ircConnect method creates a socket and sends certain information to the server
        so it will allow you to connect. The NICK and USER commands are vital information
        you need so the IRC server will recognize you and allow you to connect. in the index.php
        this method will be gone through first, followed by irc_loop().

        the irc_loop method is what takes care of server output, parses it and makes the bot
        do commands based on that output. The script was started without any planning on how to
        do this, so I ended up splitting the server output each time the loop is gone through
        and then check for keywords in the array elements and do commands. For example in the
        very first switch case, will utilize a command if the property $this->server["CMD_BUFFER"][1]
        contains the words JOIN, PART and soon and do commands based on that. Other commands are done like
        this. If other commands are to be added you have to find out where the word you want the bot
        to react to is located in the property.
        */
        
        
    class irc
        
    {
            
    // server connect
            
    public $host "irc.freenode.net";
            public 
    $port 6665;
            public 
    $channel "#dinksmallwood";
            public 
    $bot_password "BOT_IRC_PASSWORD_HERE";
            public 
    $username "Dinkbot";
            public 
    $hostname "lolwut_@_port_80";
            public 
    $server = array();
            public 
    $mysql_username "glennglenn";
            public 
    $mysql_password "glennglenn";
            public 
    $mysql_host     "localhost";
            public 
    $mysql_database "irc_log";    
            public 
    $siteFile$profileTitle$fetchTitle$splitString$dato$compareDato$logName$mQuery;
            public 
    $currString$userString;
            public 
    $pongString$i$stringFix$greetName$cleanString = array();
            public 
    $badWords = array("****""faggot""****ing""pussy""*****""****""cocksucker""dickhead""****""whore");
            public 
    $crap false;
            public 
    $swearFilter false;
            public 
    $enableLogging true;
            
            public function 
    __construct()
            {
                
    error_reporting(E_ERROR);
                
    ignore_user_abort(true);
                
    set_time_limit(0);
                
                if (
    $this->enableLogging == true)
                    if (!
    mysql_connect($this->mysql_host$this->mysql_username$this->mysql_password) || !mysql_select_db($this->mysql_database))
                        die(
    mysql_error());
            }
            
            public function 
    ircConnect()
            {
                
    // Connect to IRC server
                
    $this->server['SOCKET'] = fsockopen($this->host$this->port$errno$errstr2);
                
    // Needed headers in order to connect, otherwise we recieve error.
                
    $this->server['HEADERS'] .= "NICK " $this->username "\n\r";
                
    $this->server['HEADERS'] .= "USER " $this->username ' ' $this->hostname ' ' $this->host ' :' $this->username "\n\r";
                
    // Sending headers.
                
    $this->sendCmd($this->server['HEADERS'], true);
            }
            
            public function 
    message_log($string$name)
            {
                
    $this->splitString explode(" "trim($string));
                
    $this->splitString array_slice($this->splitString3);
                
    $this->splitString implode(" "$this->splitString);
                
    $this->dato date('l jS \of F Y h:i:s A');
                
    $this->splitString substr($this->splitString1strlen($this->splitString));
                
    $this->mQuery mysql_query("SELECT * FROM log WHERE username = '".name."' AND message = '".$this->splitString."'");
                if (!
    mysql_num_rows($this->mQuery) > 5)
                {
                    
    $this->mQuery mysql_result($this->mQuery0);
                    
    $this->compareDato strftime("%l %j%S \%o%f %F %Y %h:%i:%s %A");
                    
    mysql_query("INSERT INTO log (id, username, message, date) VALUES ('".NULL."', '".$name."', '".addslashes($this->splitString)."', '".$this->dato."')");
                }
            }
            
            public function 
    irc_loop()
            {
                if(
    $this->server['SOCKET'])
                {
                    while(!
    feof($this->server['SOCKET']))
                    {    
                    
                        
    // Code to read everything from the server
                        // $this->server['RAW_BUFFER'] = fgets($this->server['SOCKET'], 1024);
                        // echo $this->server['RAW_BUFFER'] . "\n\r";
                        // End of code
                        
                        
    $this->server['CMD_BUFFER'] = explode(" "trim(fgets($this->server['SOCKET'], 1024)));
                        
    // $this->server['RAW_BUFFER'] .= "[RECEIVE]" . $this->server['CMD_BUFFER'][0] . " " . $this->server['CMD_BUFFER'][1] . " " . $this->server['CMD_BUFFER'][2] . " " . $this->server['CMD_BUFFER'][3] . " ";
                        // $this->server['RAW_BUFFER'] .= $this->server['CMD_BUFFER'][4] . " " . $this->server['CMD_BUFFER'][5] . " " . $this->server['CMD_BUFFER'][6] . " " . $this->server['CMD_BUFFER'][7] . "\n\r";
                        
    $this->server['RAW_BUFFER'] = implode(" "$this->server['CMD_BUFFER']);
                        if (!empty(
    $this->server['RAW_BUFFER']))
                            echo 
    $this->server['RAW_BUFFER'] . "\n\r";
                                            
                        if (
    $this->crap == false)
                        {
                            if (
    strpos($this->server['CMD_BUFFER'][0], "NickServ"))
                            {
                                
    $this->sendCmd("PRIVMSG" " nickserv :" "identify ".$this->bot_password."\n\r"true);
                                
    fputs ($this->server["SOCKET"], "JOIN " "".$this->channel."" "\n");
                                
    $this->crap true;
                            }
                        }
                        
                        switch (
    $this->server["CMD_BUFFER"][1])
                        {
                            case 
    "JOIN":
                                
    preg_match("(:([^!]+)!)"$this->server['CMD_BUFFER'][0], $this->greetName);
                                if (
    $this->greetName[1] == $this->username)
                                {
                                    
    $this->sendCmd("PRIVMSG" " ".$this->channel." :" "Hello guys :33\n\r"true);
                                } else {
                                    
    $this->sendCmd("PRIVMSG" " ".$this->channel." :" "Welcome ".$this->greetName[1]." to the Dink Smallwood IRC chat" "\n\r"true);
                                }
                                break;
                                
                            case 
    "PART":
                                
    preg_match("(:([^!]+)!)"$this->server['CMD_BUFFER'][0], $this->greetName);
                                
    $this->sendCmd("PRIVMSG" " ".$this->channel." :" "Goodbye ".$this->greetName[1].". See you soon :3" "\n\r"true);
                                break;
                                
                            case 
    "KICK":
                                
    $this->sendCmd("PRIVMSG" " ".$this->channel." :" "Lol, pwn3d." "\n\r"true);
                                break;
                                
                            case 
    "NICK":
                                
    preg_match("(:([^!]+)!)"$this->server['CMD_BUFFER'][0], $this->greetName);
                                
    $this->sendCmd("PRIVMSG" " ".$this->channel." :" "Keep changing yer names, huh.\n\r"true);
                                break;
                                
                            case 
    "QUIT":
                                
    preg_match("(:([^!]+)!)"$this->server['CMD_BUFFER'][0], $this->greetName);
                                
    $this->sendCmd("PRIVMSG" " ".$this->channel." :" "Goodbye ".$this->greetName[1].". See you soon :3" "\n\r"true);
                                break;
                                
                            case 
    "PRIVMSG":
                                
    preg_match("(:([^!]+)!)"$this->server['CMD_BUFFER'][0], $this->greetName);
                                
    // Sends raw buffer to the IRC class and will be dealt with there.
                                
    if ($this->enableLogging == true)
                                    
    $this->message_log($this->server['RAW_BUFFER'], $this->greetName[1]);
                                if (
    $this->server["CMD_BUFFER"][2] == "Dinkbot")
                                {
                                    if (
    strtolower($this->greetName[1]) == "glennglenn" || strtolower($this->greetName[1]) == "magicman")
                                    {
                                        
    $this->currString array_slice($this->server['CMD_BUFFER'], 3);
                                        
    $this->currString implode(" "$this->currString);
                                        
    $this->currString substr($this->currString1strlen($this->currString));
                                        
    // preg_match("(:([^!]+):)", $this->currString, $this->userString);
                                        
    $this->sendCmd("PRIVMSG" " ".$this->channel." :" "".$this->currString."" "\n\r"true);
                                    }
                                }
                                
                                
    // swear filter
                                
    if ($this->swearFilter == true)
                                {
                                    for (
    $this->0$this->count($this->badWords); $this->i++)
                                    {
                                        if (
    strpos(strtolower($this->server['RAW_BUFFER']), $this->badWords[$this->i]))
                                        {
                                            
    preg_match("(:([^!]+)!)"$this->server['CMD_BUFFER'][0], $this->greetName);
                                                if (
    strtolower($this->greetName[1]) == "glennglenn" || strtolower($this->greetName[1]) == "magicman")
                                                {
                                                    
    $this->sendCmd("PRIVMSG" " ".$this->channel." :" "I can't kick you, I love you :3\n\r"true);
                                                }
                                                else
                                                {
                                                    
    $this->sendCmd("KICK" " ".$this->channel." ".$this->greetName[1]." :" "No swearing. For a list of banned words, see @help bannedwords." "\n\r"true);
                                                }
                                        }
                                    }
                                }
                                break;
                        }
                        
                        switch (
    $this->server['CMD_BUFFER'][3])
                        {
                            case 
    ":@profile":
                                
    preg_match("(:([^!]+)!)"$this->server['CMD_BUFFER'][0], $this->greetName);
                                if (empty(
    $this->server['CMD_BUFFER'][4]))
                                    
    $this->server['CMD_BUFFER'][4] = $this->greetName[1];
                                
    $this->siteFile = @fopen("http://dinknetwork.com/user/".$this->server['CMD_BUFFER'][4]."/""r") or die ("Can't read input stream");
                                
    $this->profileTitle fread($this->siteFile16384);
                                
    preg_match('/<title>(.*?)<\/title>/is'$this->profileTitle$this->fetchTitle);
                                if (
    $this->fetchTitle[1] == "The Dink Network | Error")
                                {
                                    
    $this->sendCmd("PRIVMSG" " ".$this->channel." :" "I could not find the user specified. \n\r"true);
                                } else {
                                    if (empty(
    $this->server['CMD_BUFFER'][4]))
                                    {
                                        
    $this->sendCmd("PRIVMSG" " ".$this->channel." :" "http://www.dinknetwork.com/user/".$this->greetName[1]."\n\r"true);
                                    } else {
                                        
    $this->sendCmd("PRIVMSG" " ".$this->channel." :" "http://www.dinknetwork.com/user/".$this->server['CMD_BUFFER'][4]."\n\r"true);
                                    }
                                }
                                break;
                            
                            case 
    ":@help":
                                
    // "@help bannedwords" command
                                
    if ($this->server['CMD_BUFFER'][4] == "bannedwords")
                                {
                                    foreach(
    $this->badWords as $wordList)
                                    {
                                        
    $this->.= $wordList ", ";
                                    }
                                    
    $this->str_replace(count($this->badWords), ""$this->i);
                                    
    $this->sendCmd("PRIVMSG" " ".$this->channel." :" "Currently banned words: ".$this->i."\n\r"true);
                                }
                                break;
                                
                            case 
    ":@dinkc":
                                
    // help with the procedures.
                                
    if ($this->server['CMD_BUFFER'][4] == "procedure")
                                    if (
    $this->server['CMD_BUFFER'][5] == "main")
                                        
    $this->sendCmd("PRIVMSG" " ".$this->channel." :" "When the script is first loaded (if attached to a screen or sprite, when the screen or sprite is first displayed on the screen).\n\r"true);
                                    if (
    $this->server['CMD_BUFFER'][5] == "push")
                                        
    $this->sendCmd("PRIVMSG" " ".$this->channel." :" "When Dink pushes the sprite, and starts the pushing animation.\n\r"true);
                                    if (
    $this->server['CMD_BUFFER'][5] == "touch")
                                        
    $this->sendCmd("PRIVMSG" " ".$this->channel." :" "When Dink touches the sprite.\n\r"true);
                                    if (
    $this->server['CMD_BUFFER'][5] == "talk")
                                        
    $this->sendCmd("PRIVMSG" " ".$this->channel." :" "When Dink talks to a sprite.\n\r"true);
                                    if (
    $this->server['CMD_BUFFER'][5] == "attack")
                                        
    $this->sendCmd("PRIVMSG" " ".$this->channel." :" "When the sprite attacks (only for sprites that have a sp_brain of 10 or sp_base_attack and a sp_brain of 9, touch-damage doesn't count).\n\r"true);
                                    if (
    $this->server['CMD_BUFFER'][5] == "hit")
                                        
    $this->sendCmd("PRIVMSG" " ".$this->channel." :" "When the sprite is hit by another sprite.\n\r"true);
                                    if (
    $this->server['CMD_BUFFER'][5] == "die")
                                        
    $this->sendCmd("PRIVMSG" " ".$this->channel." :" "When the sprite dies in combat. Killing a sprite with sp_kill or sp_active will not trigger the die procedure. When Dink dies, the die procedure is run from dinfo.c.\n\r"true);
                                    if (
    $this->server['CMD_BUFFER'][5] == "damage")
                                        
    $this->sendCmd("PRIVMSG" " ".$this->channel." :" "When the missile hits another sprite.\n\r"true);
                                    if (
    $this->server['CMD_BUFFER'][5] == "pickup")
                                        
    $this->sendCmd("PRIVMSG" " ".$this->channel." :" "When the item is added to the player's inventory.\n\r"true);
                                    if (
    $this->server['CMD_BUFFER'][5] == "arm")
                                        
    $this->sendCmd("PRIVMSG" " ".$this->channel." :" "When the item is armed.\n\r"true);
                                    if (
    $this->server['CMD_BUFFER'][5] == "armmovie")
                                        
    $this->sendCmd("PRIVMSG" " ".$this->channel." :" "After the item is armed.\n\r"true);
                                    if (
    $this->server['CMD_BUFFER'][5] == "use")
                                        
    $this->sendCmd("PRIVMSG" " ".$this->channel." :" "When the item is used (with Ctrl for items or Shift for magic).\n\r"true);
                                    if (
    $this->server['CMD_BUFFER'][5] == "disarm")
                                        
    $this->sendCmd("PRIVMSG" " ".$this->channel." :" "When the item is disarmed.\n\r"true);
                                    if (
    $this->server['CMD_BUFFER'][5] == "holdingdrop")
                                        
    $this->sendCmd("PRIVMSG" " ".$this->channel." :" "When the item is removed from the player's inventory while it is armed.\n\r"true);
                                    if (
    $this->server['CMD_BUFFER'][5] == "drop")
                                        
    $this->sendCmd("PRIVMSG" " ".$this->channel." :" "When the item is removed from the player's inventory.\n\r"true);
                                    if (
    $this->server['CMD_BUFFER'][5] == "raise")
                                        
    $this->sendCmd("PRIVMSG" " ".$this->channel." :" "Called in lraise.c when Dink gains a level.\n\r"true);
                                    if (
    $this->server['CMD_BUFFER'][5] == "buttonon")
                                        
    $this->sendCmd("PRIVMSG" " ".$this->channel." :" "Called if the sprite has a sp_brain of 14 and Dink walks on top of its hardbox.\n\r"true);
                                    if (
    $this->server['CMD_BUFFER'][5] == "buttonoff")
                                        
    $this->sendCmd("PRIVMSG" " ".$this->channel." :" "Called if the sprite has a sp_brain of 14 and Dink walks off of its hardbox.\n\r"true);
                                
                                
    // Help with DinkC functions
                                
    if ($this->server['CMD_BUFFER'][4] == "function")
                                    if (
    $this->server['CMD_BUFFER'][5] == "activate_bow")
                                        
    $this->sendCmd("PRIVMSG" " ".$this->channel." :" "activate_bow allows bow-type weapons to charge up.  The animation is hard-coded to base 100, uses all 8 directions, and each direction uses 6 frames. As the function name implies, it is used almost exclusively for the bow. If the Ctrl key is already being pressed when activate_bow is issued (as must be the case in a weapon's use() procedure), the script is paused until the Ctrl key is released. Dink can change his direction, but cannot move.  During this time, a counter is quickly incremented based on how long the Ctrl key is held. In about one second, the counter reaches a ceiling value of 500. The script remains paused, but the counter is not incremented further. get_last_bow_power then returns the value of the counter when the Ctrl key is released and the script is allowed to continue.\n\r"true);
                                    
                                    if (
    $this->server['CMD_BUFFER'][5] == "add_exp")
                                        
    $this->sendCmd("PRIVMSG" " ".$this->channel." :" "add_exp adds amount experience and creates a text-sprite to float above active_sprite. v1.07 and earlier required that active_sprite was last hit by Dink. v1.08 will work with any active_sprite. For most situations sp_exp will work well enough and will automatically add and display the experience addition properly. However, if you change a monster's brain when it dies, Dink won't get the experience points.
    \n\r"
    true);    
                                
                                            
                                break;            
                        }
                        
                        switch (
    $this->server['CMD_BUFFER'][0])
                        {
                            case 
    "PING":
                                
    $this->pongString explode(" "$this->server['RAW_BUFFER']);
                                
    $this->sendCmd("PONG ".$this->pongString[1]."\n\r"true);
                                break;
                        }
                        
    flush(); 
                    }
                }
            }
            
            public function 
    sendCmd($command$showConsole false)
            {
                @
    fputs($this->server['SOCKET'], $commandstrlen($command));
                if (
    $showConsole == true)
                {
                    echo 
    "".$command."";
                }
            }
        }
    ?>
    Last edited by CyberPirate; 06-14-2010 at 11:21 PM.


 

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •