From 7d8220d838f82f7ad1ffedbcbd3b955aef6d71a2 Mon Sep 17 00:00:00 2001
From: marzavec <admin@marzavec.com>
Date: Sat, 10 Mar 2018 22:41:17 -0800
Subject: stabilized modules and server cmd field

---
 server/src/commands/core/chat.js     | 16 +++++++++----
 server/src/commands/core/help.js     | 45 +++++++++++++++++++++++++-----------
 server/src/commands/core/invite.js   | 12 ++++++----
 server/src/commands/core/join.js     |  8 +++++--
 server/src/commands/core/showcase.js |  4 +++-
 server/src/commands/core/stats.js    |  1 -
 6 files changed, 60 insertions(+), 26 deletions(-)

(limited to 'server/src/commands/core')

diff --git a/server/src/commands/core/chat.js b/server/src/commands/core/chat.js
index 4fe3b80..c086077 100644
--- a/server/src/commands/core/chat.js
+++ b/server/src/commands/core/chat.js
@@ -4,15 +4,23 @@
 
 'use strict';
 
-exports.run = async (core, server, socket, data) => {
-  // process text
-  let text = String(data.text);
+function parseText(text) {
+  if (typeof text !== 'string') {
+    return false;
+  }
+
   // 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");
+
+  return text;
+}
+
+exports.run = async (core, server, socket, data) => {
+  let text = parseText(data.text);
   if (!text) {
-    // lets not send empty text?
+    // lets not send objects or empty text, yea?
     return;
   }
 
diff --git a/server/src/commands/core/help.js b/server/src/commands/core/help.js
index 17478d0..7b5237c 100644
--- a/server/src/commands/core/help.js
+++ b/server/src/commands/core/help.js
@@ -4,19 +4,38 @@
 
 'use strict';
 
+const stripIndents = require('common-tags').stripIndents;
+
 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
-    }
+  // verify passed arguments
+  let typeDt = typeof data.type;
+  let catDt = typeof data.category;
+  let cmdDt = typeof data.command;
+  if (typeDt !== 'undefined' && typeDt !== 'string' ) {
+    return;
+  } else if (catDt !== 'undefined' && catDt !== 'string' ) {
+    return;
+  } else if (cmdDt !== 'undefined' && cmdDt !== 'string' ) {
+    return;
+  }
+
+  // set default reply
+  let reply = stripIndents`Help usage:
+    Show all categories -> { cmd: 'help', type: 'categories' }
+    Show all commands in category -> { cmd: 'help', category: '<category name>' }
+    Show specific command -> { cmd: 'help', command: '<command name>' }`;
+
+  if (typeDt !== 'undefined') {
+    let categories = core.commands.categories().sort();
+    reply = `Command Categories:\n${categories.map(c => `- ${c.replace('../src/commands/', '')}`).join('\n')}`;
+  } else if (catDt !== 'undefined') {
+    let catCommands = core.commands.all('../src/commands/' + data.category).sort((a, b) => a.info.name.localeCompare(b.info.name));
+    reply = `${data.category} commands:\n${catCommands.map(c => `- ${c.info.name}`).join('\n')}`;
+  } else if (cmdDt !== 'undefined') {
+    let command = core.commands.get(data.command);
+    reply = stripIndents`
+      Usage: ${command.info.usage || command.info.name}
+      Description: ${command.info.description || '¯\_(ツ)_/¯'}`;
   }
 
   server.reply({
@@ -28,6 +47,6 @@ exports.run = async (core, server, socket, data) => {
 // optional parameters are marked, all others are required
 exports.info = {
   name: 'help', // actual command name
-  usage: 'help ([type:categories] | [type:command])',
+  usage: 'help ([ type:categories] | [category:<category name> | command:<command name> ])',
   description: 'Outputs information about the servers current protocol'
 };
diff --git a/server/src/commands/core/invite.js b/server/src/commands/core/invite.js
index 1c70ac1..7d162d6 100644
--- a/server/src/commands/core/invite.js
+++ b/server/src/commands/core/invite.js
@@ -9,14 +9,16 @@ function verifyNickname(nick) {
 }
 
 exports.run = async (core, server, socket, data) => {
-  let targetNick = String(data.nick);
+  if (typeof data.nick !== 'string') {
+    return;
+  }
 
-  if (!verifyNickname(targetNick)) {
+  if (!verifyNickname(data.nick)) {
     // Not a valid nickname? Chances are we won't find them
     return;
   }
 
-  if (targetNick == socket.nick) {
+  if (data.nick == socket.nick) {
     // TODO: reply with something witty? They invited themself
     return;
   }
@@ -36,7 +38,7 @@ exports.run = async (core, server, socket, data) => {
     cmd: 'info',
     text: `${socket.nick} invited you to ?${channel}`
   };
-  let inviteSent = server.broadcast( payload, { channel: socket.channel, nick: targetNick });
+  let inviteSent = server.broadcast( payload, { channel: socket.channel, nick: data.nick });
 
   if (!inviteSent) {
     server.reply({
@@ -49,7 +51,7 @@ exports.run = async (core, server, socket, data) => {
 
   server.reply({
     cmd: 'info',
-    text: `You invited ${targetNick} to ?${channel}`
+    text: `You invited ${data.nick} to ?${channel}`
   }, socket);
 
   core.managers.stats.increment('invites-sent');
diff --git a/server/src/commands/core/join.js b/server/src/commands/core/join.js
index 6a65851..609e39d 100644
--- a/server/src/commands/core/join.js
+++ b/server/src/commands/core/join.js
@@ -32,14 +32,18 @@ exports.run = async (core, server, socket, data) => {
     return;
   }
 
-  let channel = String(data.channel).trim();
+  if (typeof data.channel !== 'string' || typeof data.nick !== 'string') {
+    return;
+  }
+
+  let channel = data.channel.trim();
   if (!channel) {
     // Must join a non-blank channel
     return;
   }
 
   // Process nickname
-  let nick = String(data.nick);
+  let nick = data.nick;
   let nickArray = nick.split('#', 2);
   nick = nickArray[0].trim();
 
diff --git a/server/src/commands/core/showcase.js b/server/src/commands/core/showcase.js
index aaa474c..5d15ea2 100644
--- a/server/src/commands/core/showcase.js
+++ b/server/src/commands/core/showcase.js
@@ -16,6 +16,7 @@ const createReply = (echoInput) => {
 
 // `exports.run()` is required and will always be passed (core, server, socket, data)
 // be sure it's asyn too
+// this is the main function
 exports.run = async (core, server, socket, data) => {
 
   server.reply({
@@ -35,12 +36,13 @@ exports.init = (core) => {
 }
 
 // optional, if `data.echo` is missing `exports.run()` will never be called & the user will be alerted
+// remember; this will only verify that the data is not undefined, not the type of data
 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
+  usage: 'showcase {echo}', // used for help output, can be ommited if no parameters are required
   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
index 841675f..ba28319 100644
--- a/server/src/commands/core/stats.js
+++ b/server/src/commands/core/stats.js
@@ -50,6 +50,5 @@ exports.run = async (core, server, socket, data) => {
 
 exports.info = {
   name: 'stats',
-  usage: 'stats',
   description: 'Sends back current server stats to the calling client'
 };
-- 
cgit v1.2.1