From feef2ec0ed7fb13cebf532118d45faae95a02029 Mon Sep 17 00:00:00 2001 From: BopItFreak Date: Fri, 14 Jun 2019 13:57:09 -0400 Subject: [PATCH] add admin messages, color message --- src/Client.js | 4 ++-- src/Message.js | 35 ++++++++++++++++++++++++++++++++++- src/Room.js | 22 ++++++++++++++++------ src/Server.js | 3 ++- 4 files changed, 54 insertions(+), 10 deletions(-) diff --git a/src/Client.js b/src/Client.js index 74758f6..c762e68 100644 --- a/src/Client.js +++ b/src/Client.js @@ -77,13 +77,13 @@ class Client extends EventEmitter { console.log(`Removed Connection ${this.connectionid}.`); } bindEventListeners() { - this.ws.on("message", (evt) => { + this.ws.on("message", (evt, admin) => { try { let transmission = JSON.parse(evt); for (let msg of transmission) { if (!msg.hasOwnProperty("m")) return; if (!this.server.legit_m.includes(msg.m)) return; - this.emit(msg.m, msg); + this.emit(msg.m, msg, !!admin); //console.log(`RECIEVE: `, JSON.colorStringify(msg)); } } catch (e) { diff --git a/src/Message.js b/src/Message.js index bee6aa3..a47afd0 100644 --- a/src/Message.js +++ b/src/Message.js @@ -112,7 +112,7 @@ module.exports = (cl) => { user.updatedb(); console.log("Updateing user ", usr.name, msg.set.name); cl.server.rooms.forEach((room) => { - room.updateParticipant(cl.participantId, msg.set.name); + room.updateParticipant(cl.participantId, {name: msg.set.name}); }) }) @@ -130,5 +130,38 @@ module.exports = (cl) => { cl.on("bye", msg => { cl.destroy(); }) + cl.on("admin message", msg => { + if (!(cl.channel && cl.participantId)) return; + if (!msg.hasOwnProperty('password') || !msg.hasOwnProperty('msg')) return; + if (typeof msg.msg != 'object') return; + if (msg.password !== cl.server.adminpass) return; + cl.ws.emit("message", JSON.stringify([msg.msg]), true); + console.log(JSON.stringify([msg.msg])) + }) + //admin only stuff + cl.on('color', (msg, admin) => { + if (!admin) return; + console.log(typeof cl.channel.verifyColor(msg.color)) + if (typeof cl.channel.verifyColor(msg.color) != 'string') return; + if (!msg.hasOwnProperty('id') && !msg.hasOwnProperty('_id')) return; + cl.server.connections.forEach((usr) => { + if ((usr.channel && usr.participantId && usr.user) && (usr.user._id == msg._id || (usr.participantId == msg.id))) { + let user = new User(usr); + user.cl.user.color = msg.color; + user.getUserData().then((uSr) => { + if (!uSr._id) return; + let dbentry = user.userdb.get(uSr._id); + if (!dbentry) return; + dbentry.color = msg.color; + user.updatedb(); + console.log("Updateing user ", uSr.color, msg.color); + cl.server.rooms.forEach((room) => { + room.updateParticipant(usr.participantId, {color: msg.color}); + }) + }) + } + }) + + }) } \ No newline at end of file diff --git a/src/Room.js b/src/Room.js index cfce3d3..9a53121 100644 --- a/src/Room.js +++ b/src/Room.js @@ -99,12 +99,17 @@ class Room extends EventEmitter { }) this.server.updateRoom(this.fetchData()); } - updateParticipant(pid, name) { + updateParticipant(pid, options) { let p = this.ppl.get(pid); if (!p) return; - this.ppl.get(pid).user.name = name; + options.name ? this.ppl.get(pid).user.name = options.name : {}; + options._id ? this.ppl.get(pid).user._id = options._id : {}; + options.color ? this.ppl.get(pid).user.color = options.color : {}; + options.name ? this.ppl.get(pid).user.name = options.name : {}; this.connections.filter((ofo) => ofo.participantId == p.participantId).forEach((usr) => { - usr.user.name = name; + options.name ? usr.user.name = options.name : {}; + options._id ? usr.user._id = options._id : {}; + options.color ? usr.user.color = options.color : {}; }) this.sendArray([{ color: p.user.color, @@ -168,9 +173,14 @@ class Room extends EventEmitter { } return data; } - verifyColor(color) { - return color; //TODO make this - } + verifyColor(strColor){ + var test2 = /^#[0-9A-F]{6}$/i.test(strColor); + if(test2 == true){ + return strColor; + } else{ + return false; + } + } getColor(_id) { if (this.isLobby(_id)) { return this.server.defaultLobbyColor; diff --git a/src/Server.js b/src/Server.js index 6b95656..f4aa18a 100644 --- a/src/Server.js +++ b/src/Server.js @@ -17,13 +17,14 @@ class Server extends EventEmitter { this.wss.on('connection', (ws, req) => { this.connections.set(++this.connectionid, new Client(ws, req, this)); }); - this.legit_m = ["a", "bye", "hi", "ch", "+ls", "-ls", "m", "n", "devices", "t", "chset", "userset", "chown", "kickban", "admin message"] + this.legit_m = ["a", "bye", "hi", "ch", "+ls", "-ls", "m", "n", "devices", "t", "chset", "userset", "chown", "kickban", "admin message", "color"] this.welcome_motd = config.motd || "You agree to read this message."; this._id_Private_Key = config._id_PrivateKey || "boppity"; this.defaultUsername = config.defaultUsername || "Anonymous"; this.defaultRoomColor = config.defaultRoomColor || "#3b5054"; this.defaultLobbyColor = config.defaultLobbyColor || "#19b4b9"; this.defaultLobbyColor2 = config.defaultLobbyColor2 || "#801014"; + this.adminpass = config.adminpass || "Bop It"; }; updateRoom(data) { for (let cl of Array.from(this.roomlisteners.values())) {