Skip to content

Custom irc style commands

Frug edited this page Oct 11, 2012 · 1 revision

Table of Contents

Adding a custom IRC style command

AJAX Chat provides interfaces to add your own custom IRC style commands.
Commands are parsed on server-side (using PHP) and on client-side (using JavaScript).
This separates layout and language specific strings from the server-side logic.

In this tutorial we will add a custom command to display your own IP in the chatlist.

Server-side

Edit lib/class/CustomAJAXChat.php and override the method parseCustomCommands by adding

    // Return true if a custom command has been successfully parsed, else false
    // $text contains the whole message, $textParts the message split up as words array
    function parseCustomCommands($text, $textParts) {
        switch($textParts[0]) {
            // Display userIP:
            case '/myip':
                $this->insertChatBotMessage(
                    $this->getPrivateMessageID(),
                    '/myip '.$this->getSessionIP()
                );
                return true;
            default:
                return false;
        }
    }
to the class CustomAJAXChat before
}
?>

Client-side

Edit js/custom.js and override the method replaceCustomCommands by adding

// Return replaced text for custom commands
// text contains the whole message, textParts the message split up as words array
ajaxChat.replaceCustomCommands = function(text, textParts) {
   switch(textParts[0]) {
      case '/myip':
         return '<span class="chatBotMessage">' + this.lang['myip'].replace(/%s/, textParts[1]) + '</span>';
      default:
         return text;
   }
}
to the end of the file.

Language variables

Edit js/lang/en.js and add

   myip: 'Your IP is %s.',
after
var ajaxChatLang = {


Notes:

  • The server-side method parseCustomCommands is applied on any message with a slash ("/") as first character, after the built-in commands have been parsed. If a valid command is found, this method is supposed to insert the modified message into the database using the method insertCustomMessage - have a look at the method insertParsedMessage in lib/class/AJAXChat.php for arguments and usage.
  • The client-side method replaceCustomCommands is applied on any message with a slash ("/") as first character, after the built-in commands have been replaced. If a valid command is found, this method is supposed to replace the command parts and return the modified message - have a look at the method replaceCommands in js/chat.js for examples.
  • this.lang is a reference to the client-side language object containing language specific strings. In this example we only added the english language strings. Normally, you would have to add new language strings to every language available for the chat.