From 1c98747c6202ee7cba78265e3520ad165e8fc0e2 Mon Sep 17 00:00:00 2001 From: Evert Prants Date: Tue, 19 Oct 2021 19:58:21 +0300 Subject: [PATCH] use nBot command argument parser for now --- simplecommands/plugin.json | 2 +- simplecommands/plugin.ts | 65 +++++++++++++++++++++++++++++++++++++- squeebot.repo.json | 2 +- 3 files changed, 66 insertions(+), 3 deletions(-) diff --git a/simplecommands/plugin.json b/simplecommands/plugin.json index a928308..559aea3 100644 --- a/simplecommands/plugin.json +++ b/simplecommands/plugin.json @@ -3,7 +3,7 @@ "name": "simplecommands", "description": "Official Simplistic Commands API for Squeebot 3", "tags": ["handler", "commands", "api"], - "version": "1.1.3", + "version": "1.1.4", "dependencies": ["control?", "permissions?"], "npmDependencies": [] } diff --git a/simplecommands/plugin.ts b/simplecommands/plugin.ts index 9c1df8a..7279b7b 100644 --- a/simplecommands/plugin.ts +++ b/simplecommands/plugin.ts @@ -223,7 +223,7 @@ class SqueebotCommandsAPIPlugin extends Plugin { // Start executing for (const spec of permitted) { - const argv: any[] = separate.slice(1); + const argv: any[] = this.splitArguments(text).slice(1); if (spec.plugin === this.name) { argv.unshift(plugins); } @@ -469,6 +469,69 @@ class SqueebotCommandsAPIPlugin extends Plugin { ); } + private splitArguments(str: string): string[] { + const strArray = str.split(''); + const strARGS: string[] = []; + let strARGC = 0; + let strChar = ''; + let isString = false; + let escape = false; + let escaped = false; + + function addToArgs(append: string): void { + if (!strARGS[strARGC]) { + strARGS[strARGC] = ''; + } + strARGS[strARGC] += append; + } + + for (strChar in strArray) { + if (escaped) { + escaped = false; + } + if (escape) { + escape = false; + escaped = true; + } + switch (strArray[strChar]) { + case '\\': + if (!escaped) { + escape = true; + } else { + addToArgs('\\'); + } + break; + case '"': + if (!escaped) { + if (!isString) { + isString = true; + } else { + isString = false; + } + } else { + addToArgs('"'); + } + break; + case ' ': + if (!isString) { + strARGC++; + } else if (isString) { + if (escaped) { + addToArgs('\\'); + } + addToArgs(' '); + } + break; + default: + if (escaped) { + addToArgs('\\'); + } + addToArgs(strArray[strChar]); + } + } + return strARGS; + } + initialize(): void { this.registerCommand({ plugin: this.name, diff --git a/squeebot.repo.json b/squeebot.repo.json index e1d1333..443944f 100644 --- a/squeebot.repo.json +++ b/squeebot.repo.json @@ -15,7 +15,7 @@ }, { "name": "simplecommands", - "version": "1.1.3" + "version": "1.1.4" }, { "name": "xprotocol",