Added basic serverside command logic (no commands yet)
This commit is contained in:
parent
358d01d730
commit
4c1f0f10a7
|
|
@ -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!
|
||||
|
||||
//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
|
||||
|
|
|
|||
48
src/app/channel/commandPreprocessor.js
Normal file
48
src/app/channel/commandPreprocessor.js
Normal 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);
|
||||
}
|
||||
}
|
||||
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue