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(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);
}

View file

@ -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.
*/

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!
//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;
}
}