Added basic serverside command logic (no commands yet)

This commit is contained in:
rainbow napkin 2024-12-08 08:55:49 -05:00
parent 358d01d730
commit 4c1f0f10a7
3 changed files with 58 additions and 3 deletions

View file

@ -18,23 +18,29 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.*/
const validator = require('validator');//No express here, so regular validator it is! const validator = require('validator');//No express here, so regular validator it is!
//local imports //local imports
const commandPreprocessor = require('./commandPreprocessor');
const loggerUtils = require('../../utils/loggerUtils'); const loggerUtils = require('../../utils/loggerUtils');
const {userModel} = require('../../schemas/userSchema'); const {userModel} = require('../../schemas/userSchema');
module.exports = class{ module.exports = class{
constructor(server){ constructor(server){
this.server = server; this.server = server;
this.commandPreprocessor = new commandPreprocessor(server)
} }
defineListeners(socket){ defineListeners(socket){
socket.on("chatMessage", (data) => {this.relayChat(socket, data)}); socket.on("chatMessage", (data) => {this.handleChat(socket, data)});
socket.on("setFlair", (data) => {this.setFlair(socket, data)}); socket.on("setFlair", (data) => {this.setFlair(socket, data)});
socket.on("setHighLevel", (data) => {this.setHighLevel(socket, data)}); socket.on("setHighLevel", (data) => {this.setHighLevel(socket, data)});
} }
handleChat(socket, data){
this.commandPreprocessor.preprocess(socket, data);
}
relayChat(socket, data){ relayChat(socket, data){
//Trim and Sanatize for XSS //Trim and Sanatize for XSS
const msg = validator.trim(validator.escape(data.msg)); const msg = validator.trim(validator.escape(data));
const user = this.server.getSocketInfo(socket); const user = this.server.getSocketInfo(socket);
//nuke the message if its empty or huge //nuke the message if its empty or huge

View file

@ -0,0 +1,48 @@
module.exports = class commandPreprocessor{
constructor(server){
this.server = server;
this.commandProcessor = new commandProcessor(server);
}
preprocess(socket, data){
//Set command, socket and sendFlag
this.command = data.msg;
this.socket = socket;
this.sendFlag = true;
this.splitBody();
this.processServerCommand();
this.sendChat();
}
splitBody(){
//Split string by words
this.commandArray = this.command.split(/\b/g);//Split by word-borders
this.argumentArray = this.command.match(/\b\w+\b/g);//Match by words surrounded by borders
}
processServerCommand(){
if(this.commandArray[0] == '!'){
if(this.argumentArray != null && this.commandProcessor[this.argumentArray[0]] != null){
this.sendFlag = false;
this.commandProcessor[this.argumentArray[0]](this.socket, this.argumentArray);
}
}
}
sendChat(){
if(this.sendFlag){
this.server.chatHandler.relayChat(this.socket, this.commandArray.join(''));
}
}
}
class commandProcessor{
constructor(server){
this.server = server;
}
whisper(socket, argumentArray){
console.log(argumentArray);
}
}

View file

@ -26,10 +26,11 @@ class commandPreprocessor{
//If this is a local command //If this is a local command
if(this.commandArray[0] == '/'){ if(this.commandArray[0] == '/'){
//If the command exists //If the command exists
if(this.commandProcessor[this.argumentArray[0]] != null){ if(this.argumentArray != null && this.commandProcessor[this.argumentArray[0]] != null){
//Don't send it to the server //Don't send it to the server
this.sendFlag = false; this.sendFlag = false;
//Call the command with the argument array
this.commandProcessor[this.argumentArray[0]](this.argumentArray); this.commandProcessor[this.argumentArray[0]](this.argumentArray);
} }
} }