From fde6895720a4f417283b9e375583967b504de2f3 Mon Sep 17 00:00:00 2001 From: marzavec Date: Fri, 9 Mar 2018 23:47:00 -0800 Subject: initial commit --- server/src/commands/core/chat.js | 56 +++++++++++++++ server/src/commands/core/help.js | 33 +++++++++ server/src/commands/core/invite.js | 64 ++++++++++++++++++ server/src/commands/core/join.js | 128 +++++++++++++++++++++++++++++++++++ server/src/commands/core/showcase.js | 46 +++++++++++++ server/src/commands/core/stats.js | 55 +++++++++++++++ 6 files changed, 382 insertions(+) create mode 100644 server/src/commands/core/chat.js create mode 100644 server/src/commands/core/help.js create mode 100644 server/src/commands/core/invite.js create mode 100644 server/src/commands/core/join.js create mode 100644 server/src/commands/core/showcase.js create mode 100644 server/src/commands/core/stats.js (limited to 'server/src/commands/core') diff --git a/server/src/commands/core/chat.js b/server/src/commands/core/chat.js new file mode 100644 index 0000000..4fe3b80 --- /dev/null +++ b/server/src/commands/core/chat.js @@ -0,0 +1,56 @@ +/* + +*/ + +'use strict'; + +exports.run = async (core, server, socket, data) => { + // process text + let text = String(data.text); + // strip newlines from beginning and end + text = text.replace(/^\s*\n|^\s+$|\n\s*$/g, ''); + // replace 3+ newlines with just 2 newlines + text = text.replace(/\n{3,}/g, "\n\n"); + if (!text) { + // lets not send empty text? + return; + } + + let score = text.length / 83 / 4; + if (server._police.frisk(socket.remoteAddress, score)) { + server.reply({ + cmd: 'warn', + text: 'You are sending too much text. Wait a moment and try again.\nPress the up arrow key to restore your last message.' + }, socket); + + return; + } + + let payload = { + cmd: 'chat', + nick: socket.nick, + text: text + }; + + if (socket.uType == 'admin') { + payload.admin = true; + } else if (socket.uType == 'mod') { + payload.mod = true; + } + + if (socket.trip) { + payload.trip = socket.trip; + } + + server.broadcast( payload, { channel: socket.channel }); + + core.managers.stats.increment('messages-sent'); +}; + +exports.requiredData = ['text']; + +exports.info = { + name: 'chat', + usage: 'chat {text}', + description: 'Broadcasts passed `text` field to the calling users channel' +}; diff --git a/server/src/commands/core/help.js b/server/src/commands/core/help.js new file mode 100644 index 0000000..17478d0 --- /dev/null +++ b/server/src/commands/core/help.js @@ -0,0 +1,33 @@ +/* + +*/ + +'use strict'; + +exports.run = async (core, server, socket, data) => { + let reply = `Help usage: { cmd: 'help', type: 'categories'} or { cmd: 'help', type: 'commandname'}`; + + if (typeof data.type === 'undefined') { + // + } else { + if (data.type == 'categories') { + let categories = core.commands.categories(); + // TODO: bad output, fix this + reply = `Command Categories:\n${categories}`; + } else { + // TODO: finish this module later + } + } + + server.reply({ + cmd: 'info', + text: reply + }, socket); +}; + +// optional parameters are marked, all others are required +exports.info = { + name: 'help', // actual command name + usage: 'help ([type:categories] | [type:command])', + description: 'Outputs information about the servers current protocol' +}; diff --git a/server/src/commands/core/invite.js b/server/src/commands/core/invite.js new file mode 100644 index 0000000..1c70ac1 --- /dev/null +++ b/server/src/commands/core/invite.js @@ -0,0 +1,64 @@ +/* + +*/ + +'use strict'; + +function verifyNickname(nick) { + return /^[a-zA-Z0-9_]{1,24}$/.test(nick); +} + +exports.run = async (core, server, socket, data) => { + let targetNick = String(data.nick); + + if (!verifyNickname(targetNick)) { + // Not a valid nickname? Chances are we won't find them + return; + } + + if (targetNick == socket.nick) { + // TODO: reply with something witty? They invited themself + return; + } + + if (server._police.frisk(socket.remoteAddress, 2)) { + server.reply({ + cmd: 'warn', + text: 'You are sending invites too fast. Wait a moment before trying again.' + }, socket); + + return; + } + + let channel = Math.random().toString(36).substr(2, 8); + + let payload = { + cmd: 'info', + text: `${socket.nick} invited you to ?${channel}` + }; + let inviteSent = server.broadcast( payload, { channel: socket.channel, nick: targetNick }); + + if (!inviteSent) { + server.reply({ + cmd: 'warn', + text: 'Could not find user in channel' + }, socket); + + return; + } + + server.reply({ + cmd: 'info', + text: `You invited ${targetNick} to ?${channel}` + }, socket); + + core.managers.stats.increment('invites-sent'); +}; + +exports.requiredData = ['nick']; + +exports.info = { + name: 'invite', + usage: 'invite {nick}', + description: 'Generates a unique (more or less) room name and passes it to two clients' +}; diff --git a/server/src/commands/core/join.js b/server/src/commands/core/join.js new file mode 100644 index 0000000..6a65851 --- /dev/null +++ b/server/src/commands/core/join.js @@ -0,0 +1,128 @@ +/* + +*/ + +'use strict'; + +const crypto = require('crypto'); + +function hash(password) { + var sha = crypto.createHash('sha256'); + sha.update(password); + return sha.digest('base64').substr(0, 6); +} + +function verifyNickname(nick) { + return /^[a-zA-Z0-9_]{1,24}$/.test(nick); +} + +exports.run = async (core, server, socket, data) => { + if (server._police.frisk(socket.remoteAddress, 3)) { + server.reply({ + cmd: 'warn', + text: 'You are joining channels too fast. Wait a moment and try again.' + }, socket); + + return; + } + + if (typeof socket.channel !== 'undefined') { + // Calling socket already in a channel + // TODO: allow changing of channel without reconnection + return; + } + + let channel = String(data.channel).trim(); + if (!channel) { + // Must join a non-blank channel + return; + } + + // Process nickname + let nick = String(data.nick); + let nickArray = nick.split('#', 2); + nick = nickArray[0].trim(); + + if (!verifyNickname(nick)) { + server.reply({ + cmd: 'warn', + text: 'Nickname must consist of up to 24 letters, numbers, and underscores' + }, socket); + + return + } + + for (let client of server.clients) { + if (client.channel === channel) { + if (client.nick.toLowerCase() === nick.toLowerCase()) { + server.reply({ + cmd: 'warn', + text: 'Nickname taken' + }, socket); + + return; + } + } + } + + // TODO: Should we check for mod status first to prevent overwriting of admin status somehow? Meh, w/e, cba. + let uType = 'user'; + let trip = null; + let password = nickArray[1]; + if (nick.toLowerCase() == core.config.adminName.toLowerCase()) { + if (password != core.config.adminPass) { + server.reply({ + cmd: 'warn', + text: 'Gtfo' + }, socket); + + return; + } else { + uType = 'admin'; + trip = hash(password + core.config.tripSalt); + } + } else if (password) { + trip = hash(password + core.config.tripSalt); + } + + // TODO: Disallow moderator impersonation + for (let mod of core.config.mods) { + if (trip === mod.trip) + uType = 'mod'; + } + + // Announce the new user + server.broadcast({ + cmd: 'onlineAdd', + nick: nick, + trip: trip || 'null' + }, { channel: channel }); + + socket.uType = uType; + socket.nick = nick; + socket.channel = channel; + if (trip !== null) socket.trip = trip; + + // Reply with online user list + let nicks = []; + for (let client of server.clients) { + if (client.channel === channel) { + nicks.push(client.nick); + } + } + + server.reply({ + cmd: 'onlineSet', + nicks: nicks + }, socket); + + core.managers.stats.increment('users-joined'); +}; + +exports.requiredData = ['channel', 'nick']; + +exports.info = { + name: 'join', + usage: 'join {channel} {nick}', + description: 'Place calling socket into target channel with target nick & broadcast event to channel' +}; diff --git a/server/src/commands/core/showcase.js b/server/src/commands/core/showcase.js new file mode 100644 index 0000000..aaa474c --- /dev/null +++ b/server/src/commands/core/showcase.js @@ -0,0 +1,46 @@ +/* + +*/ + +'use strict'; + +// you can require() modules here + +// this function will only be only in the scope of the module +const createReply = (echoInput) => { + if (echoInput.length > 100) + echoInput = 'HOW ABOUT NO?'; + + return `You want me to echo: ${echoInput}?` +}; + +// `exports.run()` is required and will always be passed (core, server, socket, data) +// be sure it's asyn too +exports.run = async (core, server, socket, data) => { + + server.reply({ + cmd: 'info', + text: `SHOWCASE MODULE: ${core.showcase} - ${this.createReply(data.echo)}` + }, socket); + +}; + +// `exports.init()` is optional, and will only be run when the module is loaded into memory +// it will always be passed a reference to the global core class +// note: this will fire again if a reload is issued, keep that in mind +exports.init = (core) => { + if (typeof core.showcase === 'undefined') { + core.showcase = 'init is a handy place to put global data by assigning it to `core`'; + } +} + +// optional, if `data.echo` is missing `exports.run()` will never be called & the user will be alerted +exports.requiredData = ['echo']; + +// optional parameters are marked, all others are required +exports.info = { + name: 'showcase', // actual command name + aliases: ['templateModule'], // optional, an array of other names this module can be executed by + usage: 'showcase {echo}', // used for help output + description: 'Simple command module template & info' // used for help output +}; diff --git a/server/src/commands/core/stats.js b/server/src/commands/core/stats.js new file mode 100644 index 0000000..841675f --- /dev/null +++ b/server/src/commands/core/stats.js @@ -0,0 +1,55 @@ +/* + +*/ + +'use strict'; + +const stripIndents = require('common-tags').stripIndents; + +const formatTime = (time) => { + let seconds = time[0] + time[1] / 1e9; + + let minutes = Math.floor(seconds / 60); + seconds = seconds % 60; + + let hours = Math.floor(minutes / 60); + minutes = minutes % 60; + return `${hours.toFixed(0)}h ${minutes.toFixed(0)}m ${seconds.toFixed(0)}s`; +}; + +exports.run = async (core, server, socket, data) => { + let ips = {}; + let channels = {}; + for (let client of server.clients) { + if (client.channel) { + channels[client.channel] = true; + ips[client.remoteAddress] = true; + } + } + + let uniqueClientCount = Object.keys(ips).length; + let uniqueChannels = Object.keys(channels).length; + + ips = null; + channels = null; + + server.reply({ + cmd: 'info', + text: stripIndents`current-connections: ${uniqueClientCount} + current-channels: ${uniqueChannels} + users-joined: ${(core.managers.stats.get('users-joined') || 0)} + invites-sent: ${(core.managers.stats.get('invites-sent') || 0)} + messages-sent: ${(core.managers.stats.get('messages-sent') || 0)} + users-banned: ${(core.managers.stats.get('users-banned') || 0)} + stats-requested: ${(core.managers.stats.get('stats-requested') || 0)} + server-uptime: ${formatTime(process.hrtime(core.managers.stats.get('start-time')))}` + }, socket); + + core.managers.stats.increment('stats-requested'); +}; + +exports.info = { + name: 'stats', + usage: 'stats', + description: 'Sends back current server stats to the calling client' +}; -- cgit v1.2.1