Integrated server-side chatPreprocessor.js with pmHandler.js

This commit is contained in:
rainbow napkin 2025-10-02 04:56:47 -04:00
parent ad3cdd38a3
commit 6f89f36fb8
3 changed files with 32 additions and 42 deletions

View file

@ -112,10 +112,10 @@ class chatPreprocessor{
//if it isn't just an exclimation point, and we have a real command //if it isn't just an exclimation point, and we have a real command
if(commandObj.argumentArray != null){ if(commandObj.argumentArray != null){
//If the command processor knows what to do with whatever the fuck the user sent us //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) //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); 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 //Process as toke command if we didnt get a match from the standard server-side command processor
commandObj.sendFlag = await this.tokebot.tokeProcessor(commandObj); commandObj.sendFlag = await this.tokebot.tokeProcessor(commandObj);
} }

View file

@ -23,9 +23,10 @@ class message{
* @param {String} sender - Name of user who sent the 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 {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} 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. * @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 * Name of user who sent the message
@ -42,6 +43,11 @@ class message{
*/ */
this.msg = msg; this.msg = msg;
/**
* Message Type Identifier, used for client-side processing.
*/
this.type = type;
/** /**
* Array of URLs/Links included in the message. * Array of URLs/Links included in the message.
*/ */

View file

@ -18,6 +18,7 @@ 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 includes //local includes
const chatPreprocessor = require('../chatPreprocessor');
const loggerUtils = require("../../utils/loggerUtils"); const loggerUtils = require("../../utils/loggerUtils");
const socketUtils = require("../../utils/socketUtils"); const socketUtils = require("../../utils/socketUtils");
const message = require("./message"); const message = require("./message");
@ -41,6 +42,8 @@ class pmHandler{
*/ */
this.namespace = io.of('/pm'); this.namespace = io.of('/pm');
this.chatPreprocessor = new chatPreprocessor(null, null);
//Handle connections from private messaging namespace //Handle connections from private messaging namespace
this.namespace.on("connection", this.handleConnection.bind(this) ); this.namespace.on("connection", this.handleConnection.bind(this) );
} }
@ -80,39 +83,25 @@ class pmHandler{
async handlePM(data, socket){ async handlePM(data, socket){
try{ try{
//Create empty list of recipients //Check recipients
let recipients = []; const recipients = this.checkRecipients(data.recipients, socket);
//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);
}
}
//If we don't have any valid recipients //If we don't have any valid recipients
if(recipients.length <= 0){ if(recipients.length <= 0){
//Drop that shit //Drop that shit
return; return false;
} }
//Sanatize Message //preprocess message
const msg = this.sanatizeMessage(data.msg); const preprocessedMessage = await this.chatPreprocessor.preprocess(socket, data);
//If we have an invalid message
if(msg == null){
//Drop that shit
return;
}
//Create message object and relay it off to the recipients //Create message object and relay it off to the recipients
this.relayPMObj(new message( this.relayPMObj(new message(
socket.user.user, socket.user.user,
recipients, recipients,
msg, preprocessedMessage.message,
[] preprocessedMessage.chatType,
preprocessedMessage.links
)); ));
//If something fucked up //If something fucked up
@ -144,26 +133,21 @@ class pmHandler{
return this.namespace.adapter.rooms.get(user) != null; return this.namespace.adapter.rooms.get(user) != null;
} }
/** checkRecipients(input, socket){
* Sanatizes and Validates a single message, Temporary until we get commandPreprocessor split up. //Create empty recipients array
* @param {String} msg - message to validate/sanatize let recipients = [];
* @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
//Trim and Sanatize for XSS //For each requested recipient
msg = validator.trim(validator.escape(msg)); for(let user of input){
//If the given user is online and didn't send the message
//Return whether or not the shit was too long if(this.checkPresence(user) && user != socket.user.user){
if(validator.isLength(msg, {min: 0, max: 255})){ //Add the recipient to the list
//If it's valid return the message recipients.push(user);
return msg; }
} }
//if not return nothing //return recipients
return null; return recipients;
} }
} }