diff --git a/src/app/chatPreprocessor.js b/src/app/chatPreprocessor.js index 23bf559..be7cf26 100644 --- a/src/app/chatPreprocessor.js +++ b/src/app/chatPreprocessor.js @@ -112,10 +112,10 @@ class chatPreprocessor{ //if it isn't just an exclimation point, and we have a real command if(commandObj.argumentArray != null){ //If the command processor knows what to do with whatever the fuck the user sent us - if(this.commandProcessor[commandObj.argumentArray[0].toLowerCase()] != null){ + if(this.commandProcessor != null && this.commandProcessor[commandObj.argumentArray[0].toLowerCase()] != null){ //Process the command and use the return value to set the sendflag (true if command valid) commandObj.sendFlag = await this.commandProcessor[commandObj.argumentArray[0].toLowerCase()](commandObj, this); - }else{ + }else if(this.tokebot != null){ //Process as toke command if we didnt get a match from the standard server-side command processor commandObj.sendFlag = await this.tokebot.tokeProcessor(commandObj); } diff --git a/src/app/pm/message.js b/src/app/pm/message.js index f2c216c..1ccb88d 100644 --- a/src/app/pm/message.js +++ b/src/app/pm/message.js @@ -23,9 +23,10 @@ class message{ * @param {String} sender - Name of user who sent the message * @param {Array} recipients - Array of usernames who are supposed to receive the message * @param {String} msg - Contents of the message, with links replaced with numbered file-seperator markers + * @param {String} type - Message Type Identifier, used for client-side processing. * @param {Array} links - Array of URLs/Links included in the message. */ - constructor(sender, recipients, msg, links){ + constructor(sender, recipients, msg, type, links){ /** * Name of user who sent the message @@ -42,6 +43,11 @@ class message{ */ this.msg = msg; + /** + * Message Type Identifier, used for client-side processing. + */ + this.type = type; + /** * Array of URLs/Links included in the message. */ diff --git a/src/app/pm/pmHandler.js b/src/app/pm/pmHandler.js index 0014ec2..0e80206 100644 --- a/src/app/pm/pmHandler.js +++ b/src/app/pm/pmHandler.js @@ -18,6 +18,7 @@ along with this program. If not, see .*/ const validator = require('validator');//No express here, so regular validator it is! //local includes +const chatPreprocessor = require('../chatPreprocessor'); const loggerUtils = require("../../utils/loggerUtils"); const socketUtils = require("../../utils/socketUtils"); const message = require("./message"); @@ -41,6 +42,8 @@ class pmHandler{ */ this.namespace = io.of('/pm'); + this.chatPreprocessor = new chatPreprocessor(null, null); + //Handle connections from private messaging namespace this.namespace.on("connection", this.handleConnection.bind(this) ); } @@ -80,39 +83,25 @@ class pmHandler{ async handlePM(data, socket){ try{ - //Create empty list of recipients - let recipients = []; - - //For each requested recipient - for(let user of data.recipients){ - //If the given user is online and didn't send the message - if(this.checkPresence(user) && user != socket.user.user){ - //Add the recipient to the list - recipients.push(user); - } - } + //Check recipients + const recipients = this.checkRecipients(data.recipients, socket); //If we don't have any valid recipients if(recipients.length <= 0){ //Drop that shit - return; + return false; } - //Sanatize Message - const msg = this.sanatizeMessage(data.msg); - - //If we have an invalid message - if(msg == null){ - //Drop that shit - return; - } + //preprocess message + const preprocessedMessage = await this.chatPreprocessor.preprocess(socket, data); //Create message object and relay it off to the recipients this.relayPMObj(new message( socket.user.user, recipients, - msg, - [] + preprocessedMessage.message, + preprocessedMessage.chatType, + preprocessedMessage.links )); //If something fucked up @@ -144,26 +133,21 @@ class pmHandler{ return this.namespace.adapter.rooms.get(user) != null; } - /** - * Sanatizes and Validates a single message, Temporary until we get commandPreprocessor split up. - * @param {String} msg - message to validate/sanatize - * @returns {String} sanatized/validates message, returns null on validation failure - */ - sanatizeMessage(msg){ - //Normally I'd kill empty messages here - //But instead we're allowing them for sesh startups + checkRecipients(input, socket){ + //Create empty recipients array + let recipients = []; - //Trim and Sanatize for XSS - msg = validator.trim(validator.escape(msg)); - - //Return whether or not the shit was too long - if(validator.isLength(msg, {min: 0, max: 255})){ - //If it's valid return the message - return msg; + //For each requested recipient + for(let user of input){ + //If the given user is online and didn't send the message + if(this.checkPresence(user) && user != socket.user.user){ + //Add the recipient to the list + recipients.push(user); + } } - //if not return nothing - return null; + //return recipients + return recipients; } }