aboutsummaryrefslogtreecommitdiffstats
path: root/server/src/commands
diff options
context:
space:
mode:
authormarzavec <admin@marzavec.com>2018-03-10 08:47:00 +0100
committermarzavec <admin@marzavec.com>2018-03-10 08:47:00 +0100
commitfde6895720a4f417283b9e375583967b504de2f3 (patch)
treef5c8d9a188572d759456831d574bef9881d5c0be /server/src/commands
downloadhackchat-fde6895720a4f417283b9e375583967b504de2f3.tar.gz
hackchat-fde6895720a4f417283b9e375583967b504de2f3.zip
initial commit
Diffstat (limited to '')
-rw-r--r--server/src/commands/admin/addmod.js47
-rw-r--r--server/src/commands/admin/listusers.js41
-rw-r--r--server/src/commands/admin/reload.js34
-rw-r--r--server/src/commands/admin/saveconfig.js34
-rw-r--r--server/src/commands/admin/shout.js25
-rw-r--r--server/src/commands/core/chat.js56
-rw-r--r--server/src/commands/core/help.js33
-rw-r--r--server/src/commands/core/invite.js64
-rw-r--r--server/src/commands/core/join.js128
-rw-r--r--server/src/commands/core/showcase.js46
-rw-r--r--server/src/commands/core/stats.js55
-rw-r--r--server/src/commands/mod/ban.js61
-rw-r--r--server/src/commands/mod/kick.js74
-rw-r--r--server/src/commands/mod/unban.js34
14 files changed, 732 insertions, 0 deletions
diff --git a/server/src/commands/admin/addmod.js b/server/src/commands/admin/addmod.js
new file mode 100644
index 0000000..dba5aba
--- /dev/null
+++ b/server/src/commands/admin/addmod.js
@@ -0,0 +1,47 @@
+/*
+
+*/
+
+'use strict';
+
+exports.run = async (core, server, socket, data) => {
+ if (socket.uType != 'admin') {
+ // ignore if not admin
+ return;
+ }
+
+ let mod = {
+ trip: data.trip
+ }
+
+ core.config.mods.push(mod); // purposely not using `config.set()` to avoid auto-save
+
+ for (let client of server.clients) {
+ if (typeof client.trip !== 'undefined' && client.trip === data.trip) {
+ client.uType = 'mod';
+
+ server.reply({
+ cmd: 'info',
+ text: 'You are now a mod.'
+ }, client);
+ }
+ }
+
+ server.reply({
+ cmd: 'info',
+ text: `Added mod trip: ${data.trip}`
+ }, socket);
+
+ server.broadcast({
+ cmd: 'info',
+ text: `Added mod trip: ${data.trip}`
+ }, { uType: 'mod' });
+};
+
+exports.requiredData = ['trip'];
+
+exports.info = {
+ name: 'addmod',
+ usage: 'addmod {trip}',
+ description: 'Adds target trip to the config as a mod and upgrades the socket type'
+};
diff --git a/server/src/commands/admin/listusers.js b/server/src/commands/admin/listusers.js
new file mode 100644
index 0000000..a853518
--- /dev/null
+++ b/server/src/commands/admin/listusers.js
@@ -0,0 +1,41 @@
+/*
+
+*/
+
+'use strict';
+
+exports.run = async (core, server, socket, data) => {
+ if (socket.uType != 'admin') {
+ // ignore if not admin
+ return;
+ }
+
+ let channels = {};
+ for (var client of server.clients) {
+ if (client.channel) {
+ if (!channels[client.channel]) {
+ channels[client.channel] = [];
+ }
+ channels[client.channel].push(client.nick);
+ }
+ }
+
+ let lines = [];
+ for (let channel in channels) {
+ lines.push(`?${channel} ${channels[channel].join(", ")}`);
+ }
+
+ let text = '';
+ text += lines.join("\n");
+
+ server.reply({
+ cmd: 'info',
+ text: text
+ }, socket);
+};
+
+exports.info = {
+ name: 'listusers',
+ usage: 'listusers',
+ description: 'Outputs all current channels and sockets in those channels'
+};
diff --git a/server/src/commands/admin/reload.js b/server/src/commands/admin/reload.js
new file mode 100644
index 0000000..7aefbcf
--- /dev/null
+++ b/server/src/commands/admin/reload.js
@@ -0,0 +1,34 @@
+/*
+
+*/
+
+'use strict';
+
+exports.run = async (core, server, socket, data) => {
+ if (socket.uType != 'admin') {
+ // ignore if not admin
+ return;
+ }
+
+ let loadResult = core.managers.dynamicImports.reloadDirCache('src/commands');
+ loadResult += core.commands.loadCommands();
+
+ if (loadResult == '')
+ loadResult = 'Commands reloaded without errors!';
+
+ server.reply({
+ cmd: 'info',
+ text: loadResult
+ }, socket);
+
+ server.broadcast({
+ cmd: 'info',
+ text: loadResult
+ }, { uType: 'mod' });
+};
+
+exports.info = {
+ name: 'reload',
+ usage: 'reload',
+ description: '(Re)loads any new commands into memory, outputs errors if any'
+};
diff --git a/server/src/commands/admin/saveconfig.js b/server/src/commands/admin/saveconfig.js
new file mode 100644
index 0000000..e1a3ebe
--- /dev/null
+++ b/server/src/commands/admin/saveconfig.js
@@ -0,0 +1,34 @@
+/*
+
+*/
+
+'use strict';
+
+exports.run = async (core, server, socket, data) => {
+ if (socket.uType != 'admin') {
+ // ignore if not admin
+ return;
+ }
+
+ let saveResult = core.managers.config.save();
+
+ if (!saveResult) {
+ server.reply({
+ cmd: 'warn',
+ text: 'Failed to save config, check logs.'
+ }, client);
+
+ return;
+ }
+
+ server.broadcast({
+ cmd: 'info',
+ text: 'Config saved!'
+ }, { uType: 'mod' });
+};
+
+exports.info = {
+ name: 'saveconfig',
+ usage: 'saveconfig',
+ description: 'Saves current config'
+};
diff --git a/server/src/commands/admin/shout.js b/server/src/commands/admin/shout.js
new file mode 100644
index 0000000..c3cfded
--- /dev/null
+++ b/server/src/commands/admin/shout.js
@@ -0,0 +1,25 @@
+/*
+
+*/
+
+'use strict';
+
+exports.run = async (core, server, socket, data) => {
+ if (socket.uType != 'admin') {
+ // ignore if not admin
+ return;
+ }
+
+ server.broadcast( {
+ cmd: 'info',
+ text: `Server Notice: ${data.text}`
+ }, {});
+};
+
+exports.requiredData = ['text'];
+
+exports.info = {
+ name: 'shout',
+ usage: 'shout {text}',
+ description: 'Displays passed text to every client connected'
+};
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'
+};
diff --git a/server/src/commands/mod/ban.js b/server/src/commands/mod/ban.js
new file mode 100644
index 0000000..fde1ad8
--- /dev/null
+++ b/server/src/commands/mod/ban.js
@@ -0,0 +1,61 @@
+/*
+
+*/
+
+'use strict';
+
+exports.run = async (core, server, socket, data) => {
+ if (socket.uType == 'user') {
+ // ignore if not mod or admin
+ return;
+ }
+
+ let targetNick = String(data.nick);
+ let badClient = null;
+ for (let client of server.clients) {
+ // Find badClient's socket
+ if (client.channel == socket.channel && client.nick == targetNick) {
+ badClient = client;
+ break;
+ }
+ }
+
+ if (!badClient) {
+ server.reply({
+ cmd: 'warn',
+ text: 'Could not find user in channel'
+ }, socket);
+
+ return;
+ }
+
+ if (badClient.uType !== 'user') {
+ server.reply({
+ cmd: 'warn',
+ text: 'Cannot ban other mods, how rude'
+ }, socket);
+
+ return;
+ }
+
+ // TODO: ratelimiting here
+ // TODO: add reference to banned users nick or unban by nick cmd
+ //POLICE.arrest(getAddress(badClient))
+ // TODO: add event to log?
+ console.log(`${socket.nick} [${socket.trip}] banned ${targetNick} in ${socket.channel}`);
+ server.broadcast({
+ cmd: 'info',
+ text: `Banned ${targetNick}`
+ }, { channel: socket.channel });
+ badClient.close();
+
+ core.managers.stats.increment('users-banned');
+};
+
+exports.requiredData = ['nick'];
+
+exports.info = {
+ name: 'ban',
+ usage: 'ban {nick}',
+ description: 'Disconnects the target nickname in the same channel as calling socket & adds to ratelimiter'
+};
diff --git a/server/src/commands/mod/kick.js b/server/src/commands/mod/kick.js
new file mode 100644
index 0000000..5cd524d
--- /dev/null
+++ b/server/src/commands/mod/kick.js
@@ -0,0 +1,74 @@
+/*
+
+*/
+
+'use strict';
+
+exports.run = async (core, server, socket, data) => {
+ if (socket.uType == 'user') {
+ // ignore if not mod or admin
+ return;
+ }
+
+ let targetNick = String(data.nick);
+ let badClient = null;
+ for (let client of server.clients) {
+ // Find badClient's socket
+ if (client.channel == socket.channel && client.nick == targetNick) {
+ badClient = client;
+ break;
+ }
+ }
+
+ if (!badClient) {
+ server.reply({
+ cmd: 'warn',
+ text: 'Could not find user in channel'
+ }, socket);
+
+ return;
+ }
+
+ if (badClient.uType !== 'user') {
+ server.reply({
+ cmd: 'warn',
+ text: 'Cannot kick other mods, how rude'
+ }, socket);
+
+ return;
+ }
+
+ // TODO: add event to log?
+ let newChannel = Math.random().toString(36).substr(2, 8);
+ badClient.channel = newChannel;
+
+ console.log(`${socket.nick} [${socket.trip}] kicked ${targetNick} in ${socket.channel}`);
+
+ // remove socket from same-channel client
+ server.broadcast({
+ cmd: 'onlineRemove',
+ nick: targetNick
+ }, { channel: socket.channel });
+
+ // publicly broadcast event (TODO: should this be supressed?)
+ server.broadcast({
+ cmd: 'info',
+ text: `Kicked ${targetNick}`
+ }, { channel: socket.channel });
+
+ // inform mods with where they were sent
+ server.broadcast({
+ cmd: 'info',
+ text: `${targetNick} was banished to ?${newChannel}`
+ }, { channel: socket.channel, uType: 'mod' });
+
+ core.managers.stats.increment('users-banned');
+};
+
+exports.requiredData = ['nick'];
+
+exports.info = {
+ name: 'kick',
+ usage: 'kick {nick}',
+ description: 'Forces target client into another channel without announcing change'
+};
diff --git a/server/src/commands/mod/unban.js b/server/src/commands/mod/unban.js
new file mode 100644
index 0000000..cc1016a
--- /dev/null
+++ b/server/src/commands/mod/unban.js
@@ -0,0 +1,34 @@
+/*
+
+*/
+
+'use strict';
+
+exports.run = async (core, server, socket, data) => {
+ if (socket.uType == 'user') {
+ // ignore if not mod or admin
+ return;
+ }
+
+ let ip = String(data.ip);
+ let nick = String(data.nick); // for future upgrade
+
+ // TODO: remove ip from ratelimiter
+ // POLICE.pardon(ip)
+ console.log(`${socket.nick} [${socket.trip}] unbanned ${/*nick || */ip} in ${socket.channel}`);
+
+ server.reply({
+ cmd: 'info',
+ text: `Unbanned ${/*nick || */ip}`
+ }, socket);
+
+ core.managers.stats.decrement('users-banned');
+};
+
+exports.requiredData = ['ip'];
+
+exports.info = {
+ name: 'unban',
+ usage: 'unban {ip}',
+ description: 'Removes target ip from the ratelimiter'
+};