// *************************************************************** // Function: CommandProcessing.cpp // *************************************************************** #include "Main.h" #include "ConsoleScripts.h" #include "ScriptManager.h" void CommandProcessing(char* pCommandString, bool adminConsole, void* pLir, Player* pPlayer); //************************************ // Name: ConsoleWrite // // This function writes a line of text to the console. // // Parameters: // string - Text string to output // adminConsole - Is this being called from the admin console? // networkId - networkId to send to // color - color of text to output // // Return: // //************************************ void ConsoleWrite(char* string, bool adminConsole, short networkId, int color) { if (networkId<=0) { return; } if (adminConsole) { if (strlen(string)>=246) { CompiledPacket pck; pck.AddByte(GS_ADMIN_CONSOLE_RESPONSE); pck.AddByte(2+strlen(string)); pck.AddShort(networkId); pck.Add("Command buffer too long!"); NetMessage s; s.networkId=gpInternalComm->GetClientModeId(); s.pData=pck.GetString(&s.dataSize); gpInternalComm->SendData(&s); pck.Clear(); } else { CompiledPacket pck; pck.AddByte(GS_ADMIN_CONSOLE_RESPONSE); pck.AddByte(2+strlen(string)); pck.AddShort(networkId); pck.Add(string); NetMessage s; s.networkId=gpInternalComm->GetClientModeId(); s.pData=pck.GetString(&s.dataSize); gpInternalComm->SendData(&s); pck.Clear(); } } else { //Convert the string to Unicode unsigned char buffer[500]; strcpy((char*)buffer, string); Util::ConvertToUnicode(buffer); Instance::WriteGeneralChat(buffer, strlen(string), color, 0, networkId); } } //************************************ // Name: gm_ConsoleWrite // // GM binding for console write. // // Parameters: // a_thread - Document // // Return: // - GM_OK //************************************ int GM_CDECL gm_ConsoleWrite(gmThread *a_thread) { // Check the number of parameters passed is correct GM_CHECK_NUM_PARAMS(4); GM_CHECK_INT_PARAM(adminConsole, 1); GM_CHECK_INT_PARAM(NetID, 2); GM_CHECK_INT_PARAM(color, 3); GM_CHECK_STRING_PARAM(string, 0); bool aConsole=false; if (adminConsole>=1){aConsole=true;} ConsoleWrite((char*)string, aConsole, NetID, color); return GM_OK; } //************************************ // Name: gm_ExecuteCommand // // GM binding for executing a command // // Parameters: // a_thread - Document // // Return: // //************************************ int GM_CDECL gm_ExecuteCommand(gmThread *a_thread) { GM_CHECK_NUM_PARAMS(3); GM_CHECK_STRING_PARAM(cmdLn, 0); GM_CHECK_INT_PARAM(adminConsole, 2); GM_CHECK_INT_PARAM(netID, 1); bool aConsole=false; if (adminConsole>=1){aConsole=true;} void* pLir=NULL; Player* pPlayer=NULL; if (adminConsole) { pLir=&netID; } else { PLAYER_DATA* li=AccessLocalData(netID); if (li!=NULL) { pLir=li; pPlayer=(Player*)li->pPlayer; } } CommandProcessing((char*)cmdLn, aConsole, pLir, pPlayer); return GM_OK; } //************************************ // Name: CommandProcessing // // Processes a command. // // Parameters: // commandString - ASCII and the / should have been removed. // adminConsole - Is being executed from admin console? // lir - Data // p - Player class pointer // // Return: // //************************************ void CommandProcessing(char *commandString, bool adminConsole, void *lir, Player *p) { char args[11][125]; memset(args, 0, 11*125); char* argp[11]; for (unsigned int i=0; i<11; i++){argp[i]=args[i];} if (strstr(commandString, " ")!=NULL) { strcpy(argp[10], strstr(commandString, " ")+1); } //Break the commandString into tokens char count=0; char* pToken=strtok(commandString, " "); while(pToken!=NULL) { strcpy(args[count], pToken); pToken=strtok(NULL, " "); count++; if (count==10){break;} } //Check the command against the list of registered commands list::iterator itr; for (itr=gConsoleCommands.begin(); itr!=gConsoleCommands.end(); itr++) { if (strcmp((*itr)->pFunctionName+8, args[0])==0) { //Check that the command can be executed in this context if (((*itr)->allowedModes==0 && !adminConsole) || ((*itr)->allowedModes==1 && adminConsole) || ((*itr)->allowedModes>=2)) { //Check that the user has sufficient privileges to execute this command bool can_execute=false; if (!(*itr)->restricted || adminConsole) { can_execute=true; } else { //Check with the permissions system if this account has permission to execute this command if (gpPermissionGroups->HasPermission(p->GetGroupId(), args[0])) { can_execute=true; } else { short net_id=0; if (adminConsole){net_id=*((short*)lir);} else{net_id=((PLAYER_DATA*)lir)->networkId;} ConsoleWrite("Command Not Available", adminConsole, net_id, COMMAND_COLOR); } } //Execute the command script if (can_execute) { //Execute a hard coded command if ((*itr)->hardcodedProcessing!=NULL) { short net_id=0; if (adminConsole){net_id=*((short*)lir);} else{net_id=((PLAYER_DATA*)lir)->networkId;} (*itr)->hardcodedProcessing(argp, count, adminConsole, net_id); } //Execute a command script else { gmTableObject* pTable=gpScriptMan->GetVM()->AllocTableObject(); for (unsigned int i=0; i<10; i++) { if (args[i][0]!=0) { gmVariable gv; gv.SetString(gpScriptMan->GetVM(), args[i]); pTable->Set(gpScriptMan->GetVM(), i, gv); } } short net_id=0; if (adminConsole){net_id=*((short*)lir);} else{net_id=((PLAYER_DATA*)lir)->networkId;} CallScriptFunctionDelay((*itr)->pFunctionName) { call.AddParamTable(pTable); call.AddParamInt(net_id); call.AddParamInt(adminConsole); call.End(); OutputScriptErrors(); } else { if (adminConsole){net_id=*((short*)lir);} else{net_id=((PLAYER_DATA*)lir)->networkId;} ConsoleWrite("Error calling script function.", adminConsole, net_id, COMMAND_COLOR); } } } } else { short net_id=0; if (adminConsole){net_id=*((short*)lir);} else{net_id=((PLAYER_DATA*)lir)->networkId;} ConsoleWrite("Command cannot be executed in this context.", adminConsole, net_id, COMMAND_COLOR); } if (adminConsole) { short net_id=0; if (adminConsole){net_id=*((short*)lir);} else{net_id=((PLAYER_DATA*)lir)->networkId;} ConsoleWrite("TERMINATOR", adminConsole, net_id, COMMAND_COLOR); } return; } } //We couldn't find the command, write an error message to console short net_id=0; if (adminConsole){net_id=*((short*)lir);} else{net_id=((PLAYER_DATA*)lir)->networkId;} ConsoleWrite("Unknown Command", adminConsole, net_id, COMMAND_COLOR); if (adminConsole) { ConsoleWrite("TERMINATOR", adminConsole, net_id, COMMAND_COLOR); } }