diff --git a/.gitignore b/.gitignore index 61e80b6..508c672 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,5 @@ .env node_modules ssl/ -src/db/users.json +src/db/rooms.db .history diff --git a/Dockerfile b/Dockerfile deleted file mode 100644 index e69de29..0000000 diff --git a/config.js b/config.js index cc141df..727305a 100644 --- a/config.js +++ b/config.js @@ -9,7 +9,7 @@ module.exports = Object.seal({ // defaultLobbyColor2: "#801014", defaultLobbyColor2: "#276491", adminpass: process.env.ADMINPASS, - ssl: true, + ssl: process.env.SSL, defaultRoomSettings: { color: "#3b5054", color2: "#001014", diff --git a/package-lock.json b/package-lock.json index 2cdbfbb..508fb45 100644 --- a/package-lock.json +++ b/package-lock.json @@ -10,8 +10,10 @@ "license": "ISC", "dependencies": { "asyncconsole": "^1.3.9", + "chalk": "^4.1.1", "dotenv": "^8.2.0", "events": "^3.1.0", + "fancy-text-converter": "^1.0.9", "keccak": "^2.1.0", "level": "^7.0.0", "mongoose": "^5.12.7", @@ -56,6 +58,20 @@ "node": ">=10" } }, + "node_modules/ansi-styles": { + "version": "4.3.0", + "resolved": "https://registry.npmjs.org/ansi-styles/-/ansi-styles-4.3.0.tgz", + "integrity": "sha512-zbB9rCJAT1rbjiVDb2hqKFHNYLxgtk8NURxZ3IZwD3F6NtxbXZQCnnSi1Lkx+IDohdPlFp222wVALIheZJQSEg==", + "dependencies": { + "color-convert": "^2.0.1" + }, + "engines": { + "node": ">=8" + }, + "funding": { + "url": "https://github.com/chalk/ansi-styles?sponsor=1" + } + }, "node_modules/asyncconsole": { "version": "1.3.9", "resolved": "https://registry.npmjs.org/asyncconsole/-/asyncconsole-1.3.9.tgz", @@ -141,6 +157,37 @@ "node": ">=6" } }, + "node_modules/chalk": { + "version": "4.1.1", + "resolved": "https://registry.npmjs.org/chalk/-/chalk-4.1.1.tgz", + "integrity": "sha512-diHzdDKxcU+bAsUboHLPEDQiw0qEe0qd7SYUn3HgcFlWgbDcfLGswOHYeGrHKzG9z6UYf01d9VFMfZxPM1xZSg==", + "dependencies": { + "ansi-styles": "^4.1.0", + "supports-color": "^7.1.0" + }, + "engines": { + "node": ">=10" + }, + "funding": { + "url": "https://github.com/chalk/chalk?sponsor=1" + } + }, + "node_modules/color-convert": { + "version": "2.0.1", + "resolved": "https://registry.npmjs.org/color-convert/-/color-convert-2.0.1.tgz", + "integrity": "sha512-RRECPsj7iu/xb5oKYcsFHSppFNnsj/52OVTRKb4zP5onXwVF3zVmmToNcOfGC+CRDpfK/U584fMg38ZHCaElKQ==", + "dependencies": { + "color-name": "~1.1.4" + }, + "engines": { + "node": ">=7.0.0" + } + }, + "node_modules/color-name": { + "version": "1.1.4", + "resolved": "https://registry.npmjs.org/color-name/-/color-name-1.1.4.tgz", + "integrity": "sha512-dOy+3AuW3a2wNbZHIuMZpTcgjGuLU/uBL/ubcZF9OXbDo8ff4O8yVp5Bf0efS8uEoYo5q4Fx7dY9OgQGXgAsQA==" + }, "node_modules/colors": { "version": "1.4.0", "resolved": "https://registry.npmjs.org/colors/-/colors-1.4.0.tgz", @@ -228,11 +275,24 @@ "node": ">=0.8.x" } }, + "node_modules/fancy-text-converter": { + "version": "1.0.9", + "resolved": "https://registry.npmjs.org/fancy-text-converter/-/fancy-text-converter-1.0.9.tgz", + "integrity": "sha512-tFUAWpEfZOYhdsjILVu7c0PL9Ud9pTQmonm/2mdvFC7WcEHIYi9NYS5irJYFdBJDIRSqi64XV+IhHPc/ngxtyw==" + }, "node_modules/file-uri-to-path": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/file-uri-to-path/-/file-uri-to-path-1.0.0.tgz", "integrity": "sha512-0Zt+s3L7Vf1biwWZ29aARiVYLx7iMGnEUl9x33fbB/j3jR81u/O2LbqK+Bm1CDSNDKVtJ/YjwY7TUd5SkeLQLw==" }, + "node_modules/has-flag": { + "version": "4.0.0", + "resolved": "https://registry.npmjs.org/has-flag/-/has-flag-4.0.0.tgz", + "integrity": "sha512-EykJT/Q1KjTWctppgIAgfSO0tKVuZUjhgMr17kqTumMl6Afv3EISleU7qZUzoXDFTAHTDC4NOoG/ZxU3EvlMPQ==", + "engines": { + "node": ">=8" + } + }, "node_modules/ieee754": { "version": "1.2.1", "resolved": "https://registry.npmjs.org/ieee754/-/ieee754-1.2.1.tgz", @@ -705,6 +765,17 @@ "resolved": "https://registry.npmjs.org/safe-buffer/-/safe-buffer-5.1.2.tgz", "integrity": "sha512-Gd2UZBJDkXlY7GbJxfsE8/nvKkUEU1G38c1siN6QP6a9PT9MmHB8GnpscSmMJSoF8LOIrt8ud/wPtojys4G6+g==" }, + "node_modules/supports-color": { + "version": "7.2.0", + "resolved": "https://registry.npmjs.org/supports-color/-/supports-color-7.2.0.tgz", + "integrity": "sha512-qpCAvRl9stuOHveKsn7HncJRvv501qIacKzQlO/+Lwxc9+0q2wLyv4Dfvt80/DPn2pqOBsJdDiogXGR9+OvwRw==", + "dependencies": { + "has-flag": "^4.0.0" + }, + "engines": { + "node": ">=8" + } + }, "node_modules/util-deprecate": { "version": "1.0.2", "resolved": "https://registry.npmjs.org/util-deprecate/-/util-deprecate-1.0.2.tgz", @@ -766,6 +837,14 @@ "queue-microtask": "^1.2.3" } }, + "ansi-styles": { + "version": "4.3.0", + "resolved": "https://registry.npmjs.org/ansi-styles/-/ansi-styles-4.3.0.tgz", + "integrity": "sha512-zbB9rCJAT1rbjiVDb2hqKFHNYLxgtk8NURxZ3IZwD3F6NtxbXZQCnnSi1Lkx+IDohdPlFp222wVALIheZJQSEg==", + "requires": { + "color-convert": "^2.0.1" + } + }, "asyncconsole": { "version": "1.3.9", "resolved": "https://registry.npmjs.org/asyncconsole/-/asyncconsole-1.3.9.tgz", @@ -817,6 +896,28 @@ "resolved": "https://registry.npmjs.org/catering/-/catering-2.0.0.tgz", "integrity": "sha512-aD/WmxhGwUGsVPrj8C80vH7C7GphJilYVSdudoV4u16XdrLF7CVyfBmENsc4tLTVsJJzCRid8GbwJ7mcPLee6Q==" }, + "chalk": { + "version": "4.1.1", + "resolved": "https://registry.npmjs.org/chalk/-/chalk-4.1.1.tgz", + "integrity": "sha512-diHzdDKxcU+bAsUboHLPEDQiw0qEe0qd7SYUn3HgcFlWgbDcfLGswOHYeGrHKzG9z6UYf01d9VFMfZxPM1xZSg==", + "requires": { + "ansi-styles": "^4.1.0", + "supports-color": "^7.1.0" + } + }, + "color-convert": { + "version": "2.0.1", + "resolved": "https://registry.npmjs.org/color-convert/-/color-convert-2.0.1.tgz", + "integrity": "sha512-RRECPsj7iu/xb5oKYcsFHSppFNnsj/52OVTRKb4zP5onXwVF3zVmmToNcOfGC+CRDpfK/U584fMg38ZHCaElKQ==", + "requires": { + "color-name": "~1.1.4" + } + }, + "color-name": { + "version": "1.1.4", + "resolved": "https://registry.npmjs.org/color-name/-/color-name-1.1.4.tgz", + "integrity": "sha512-dOy+3AuW3a2wNbZHIuMZpTcgjGuLU/uBL/ubcZF9OXbDo8ff4O8yVp5Bf0efS8uEoYo5q4Fx7dY9OgQGXgAsQA==" + }, "colors": { "version": "1.4.0", "resolved": "https://registry.npmjs.org/colors/-/colors-1.4.0.tgz", @@ -885,11 +986,21 @@ "resolved": "https://registry.npmjs.org/events/-/events-3.3.0.tgz", "integrity": "sha512-mQw+2fkQbALzQ7V0MY0IqdnXNOeTtP4r0lN9z7AAawCXgqea7bDii20AYrIBrFd/Hx0M2Ocz6S111CaFkUcb0Q==" }, + "fancy-text-converter": { + "version": "1.0.9", + "resolved": "https://registry.npmjs.org/fancy-text-converter/-/fancy-text-converter-1.0.9.tgz", + "integrity": "sha512-tFUAWpEfZOYhdsjILVu7c0PL9Ud9pTQmonm/2mdvFC7WcEHIYi9NYS5irJYFdBJDIRSqi64XV+IhHPc/ngxtyw==" + }, "file-uri-to-path": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/file-uri-to-path/-/file-uri-to-path-1.0.0.tgz", "integrity": "sha512-0Zt+s3L7Vf1biwWZ29aARiVYLx7iMGnEUl9x33fbB/j3jR81u/O2LbqK+Bm1CDSNDKVtJ/YjwY7TUd5SkeLQLw==" }, + "has-flag": { + "version": "4.0.0", + "resolved": "https://registry.npmjs.org/has-flag/-/has-flag-4.0.0.tgz", + "integrity": "sha512-EykJT/Q1KjTWctppgIAgfSO0tKVuZUjhgMr17kqTumMl6Afv3EISleU7qZUzoXDFTAHTDC4NOoG/ZxU3EvlMPQ==" + }, "ieee754": { "version": "1.2.1", "resolved": "https://registry.npmjs.org/ieee754/-/ieee754-1.2.1.tgz", @@ -1221,6 +1332,14 @@ } } }, + "supports-color": { + "version": "7.2.0", + "resolved": "https://registry.npmjs.org/supports-color/-/supports-color-7.2.0.tgz", + "integrity": "sha512-qpCAvRl9stuOHveKsn7HncJRvv501qIacKzQlO/+Lwxc9+0q2wLyv4Dfvt80/DPn2pqOBsJdDiogXGR9+OvwRw==", + "requires": { + "has-flag": "^4.0.0" + } + }, "util-deprecate": { "version": "1.0.2", "resolved": "https://registry.npmjs.org/util-deprecate/-/util-deprecate-1.0.2.tgz", diff --git a/package.json b/package.json index f2f1e61..6f7c09d 100644 --- a/package.json +++ b/package.json @@ -23,8 +23,10 @@ "homepage": "https://github.com/BopItFreak/mpp-server#readme", "dependencies": { "asyncconsole": "^1.3.9", + "chalk": "^4.1.1", "dotenv": "^8.2.0", "events": "^3.1.0", + "fancy-text-converter": "^1.0.9", "keccak": "^2.1.0", "level": "^7.0.0", "mongoose": "^5.12.7", diff --git a/src/Client.js b/src/Client.js index facd7c9..1727bb2 100644 --- a/src/Client.js +++ b/src/Client.js @@ -120,7 +120,6 @@ class Client extends EventEmitter { this.connectionid; this.server.connections.delete(this.connectionid); this.destroied = true; - console.log(`Removed Connection ${this.connectionid}. user is ${this.user}`); } bindEventListeners() { diff --git a/src/Database.js b/src/Database.js index fe0c6e9..3024f6f 100644 --- a/src/Database.js +++ b/src/Database.js @@ -6,6 +6,9 @@ const UserModel = require('./UserModel'); const mongoose = require('mongoose'); const level = require('level'); const { db } = require('./UserModel'); +const Logger = require('./Logger'); + +var logger = new Logger("Database"); mongoose.connect(process.env.MONGO_URL, { useNewUrlParser: true, @@ -15,7 +18,7 @@ mongoose.connect(process.env.MONGO_URL, { console.error(err); return; } - console.log("Connected to Database"); + logger.log("Connected"); }); class Database { @@ -49,9 +52,6 @@ class Database { user = this.createUser(_id); } - console.log('user flags:'); - console.log(user.flags); - return user; } @@ -69,19 +69,16 @@ class Database { } static async updateUser(_id, data) { - UserModel.findById(_id, (err, doc) => { + UserModel.findOneAndUpdate({_id: _id}, data, {new: true}, (err, doc) => { if (err) { - console.error(err); + logger.error(err); return err; } - if (!doc) { - return console.warn('Could not find user to save.'); + if (doc == null) { + logger.warn('Could not find user to save.'); + return; } - - doc.set(data); - - doc.save(); }); } diff --git a/src/Logger.js b/src/Logger.js new file mode 100644 index 0000000..d719d1d --- /dev/null +++ b/src/Logger.js @@ -0,0 +1,25 @@ +const chalk = require('chalk'); + +module.exports = class Logger { + constructor (context) { + this.context = context; + } + + log(args) { + console.log(chalk.green(`[${this.context}]`), args); + } + + warn(args) { + console.warn(chalk.yellow(`[WARN] [${this.context}]`), args); + } + + error(args) { + console.error(chalk.red(`[ERR] [${this.context}]`), args); + } + + debug(args) { + if (process.env.DEBUG_ENABLED) { + console.log(chalk.blue(`[DEBUG] [${this.context}]`), args); + } + } +} \ No newline at end of file diff --git a/src/Message.js b/src/Message.js index 9a26cd4..bdc8d43 100644 --- a/src/Message.js +++ b/src/Message.js @@ -6,7 +6,6 @@ const Database = require('./Database'); module.exports = (cl) => { cl.once("hi", (msg, admin) => { - console.log('hi on') if (msg.hasOwnProperty("password")) { if (msg.password == "hideme") { cl.hidden = true; @@ -138,12 +137,12 @@ module.exports = (cl) => { if (typeof(msg.message) !== 'string') return; if (cl.channel.settings.chat) { if (cl.channel.isLobby(cl.channel._id)) { - if (!cl.quotas.chat.lobby.attempt() && !admin) return; + if (!cl.quotas.chat.lobby.attempt() && !admin && !cl.user.hasFlag('no rate chat limit', true)) return; } else { if (!(cl.user._id == cl.channel.crown.userId)) { - if (!cl.quotas.chat.normal.attempt() && !admin) return; + if (!cl.quotas.chat.normal.attempt() && !admin && !cl.user.hasFlag('no rate chat limit', true)) return; } else { - if (!cl.quotas.chat.insane.attempt() && !admin) return; + if (!cl.quotas.chat.insane.attempt() && !admin && !cl.user.hasFlag('no rate chat limit', true)) return; } } cl.channel.emit('a', cl, msg); @@ -240,26 +239,14 @@ module.exports = (cl) => { if (!admin) return; 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))) { - if (!usr.hasOwnProperty('user')) return; - let user = new User(usr, usr.user); - user.color = msg.color; - Database.getUserData(cl, cl.server).then((uSr) => { - if (!uSr._id) return; - let dbentry = Database.userdb.get(uSr._id); - if (!dbentry) return; - dbentry.color = msg.color; - Database.update(); - cl.server.rooms.forEach((room) => { - room.updateParticipant(usr.user._id, { - color: msg.color - }); - }) - }) - } - }) + cl.server.connections.forEach(c => { + if (c.destroied) return; + if (c.user._id !== msg._id && c.participantId !== msg.id) return; + c.user.color = msg.color; + require("./Database").updateUser(c.user._id, c.user); + c.channel.updateParticipant(c.user._id, c.user); + }); }); cl.on('eval', (msg, admin) => { @@ -306,6 +293,11 @@ module.exports = (cl) => { cl.server.connections.forEach((usr) => { if ((usr.channel && usr.participantId && usr.user) && (usr.user._id == msg._id || (usr.participantId == msg.id))) { if (!usr.hasOwnProperty('user')) return; + if (msg.key == "remove") { + delete usr.user.flags[msg.key]; + usr.user.flags[msg.key] = undefined; + return; + } usr.user.flags[msg.key] = msg.value; usr.user.checkFlags(); } diff --git a/src/Room.js b/src/Room.js index f81e1ae..d0c4845 100644 --- a/src/Room.js +++ b/src/Room.js @@ -1,12 +1,15 @@ const createKeccakHash = require('keccak'); const Crown = require('./Crown.js'); const Database = require('./Database.js'); +const Logger = require('./Logger.js'); const Quota = require("./Quota.js"); const RoomSettings = require('./RoomSettings.js'); +const ftc = require('fancy-text-converter'); class Room extends EventEmitter { constructor(server, _id, settings) { super(); + this.logger = new Logger(`Room - ${ftc.normalise(_id)}`); this._id = _id; this.server = server; this.crown; diff --git a/src/Server.js b/src/Server.js index 94bafc3..8ca4e29 100644 --- a/src/Server.js +++ b/src/Server.js @@ -4,11 +4,14 @@ const https = require("https"); const http = require("http"); const fs = require('fs'); const RoomSettings = require('./RoomSettings'); +const Logger = require("./Logger.js"); class Server extends EventEmitter { constructor(config) { super(); EventEmitter.call(this); + + this.logger = new Logger("Server"); if (config.ssl == true) { this.https_server = https.createServer({ @@ -38,7 +41,7 @@ class Server extends EventEmitter { }); } - this.defaultUsername = "Anonymous"; + this.defaultUsername = config.defaultUsername; this.defaultRoomSettings = new RoomSettings(config.defaultRoomSettings); this.lobbySettings = new RoomSettings(config.defaultRoomSettings); @@ -46,7 +49,7 @@ class Server extends EventEmitter { this.lobbySettings.color = config.defaultLobbyColor || "#9900ff"; this.lobbySettings.color2 = config.defaultLobbyColor2 || "#9900ff"; - console.log(`Server started on port ${config.port}`); + this.logger.log(`Server started on port ${config.port}`); this.connectionid = 0; this.connections = new Map(); this.roomlisteners = new Map();