diff --git a/src/app/channel/chatHandler.js b/src/app/channel/chatHandler.js index 2682c92..2d99a7e 100644 --- a/src/app/channel/chatHandler.js +++ b/src/app/channel/chatHandler.js @@ -18,23 +18,29 @@ along with this program. If not, see .*/ const validator = require('validator');//No express here, so regular validator it is! //local imports +const commandPreprocessor = require('./commandPreprocessor'); const loggerUtils = require('../../utils/loggerUtils'); const {userModel} = require('../../schemas/userSchema'); module.exports = class{ constructor(server){ this.server = server; + this.commandPreprocessor = new commandPreprocessor(server) } 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("setHighLevel", (data) => {this.setHighLevel(socket, data)}); } + handleChat(socket, data){ + this.commandPreprocessor.preprocess(socket, data); + } + relayChat(socket, data){ //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); //nuke the message if its empty or huge diff --git a/src/app/channel/commandPreprocessor.js b/src/app/channel/commandPreprocessor.js new file mode 100644 index 0000000..04bf731 --- /dev/null +++ b/src/app/channel/commandPreprocessor.js @@ -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); + } +} \ No newline at end of file diff --git a/www/js/channel/commandPreprocessor.js b/www/js/channel/commandPreprocessor.js index 209f881..6549da9 100644 --- a/www/js/channel/commandPreprocessor.js +++ b/www/js/channel/commandPreprocessor.js @@ -26,10 +26,11 @@ class commandPreprocessor{ //If this is a local command if(this.commandArray[0] == '/'){ //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 this.sendFlag = false; + //Call the command with the argument array this.commandProcessor[this.argumentArray[0]](this.argumentArray); } }