From 476c1353792824d2420c938c5331b09be93ff175 Mon Sep 17 00:00:00 2001 From: Simon Ser Date: Mon, 13 Jul 2020 17:22:24 +0200 Subject: [PATCH] Extract commands to separate file --- commands.js | 86 ++++++++++++++++++++++++++++++++++++++ components/app.js | 102 +++++++--------------------------------------- state.js | 2 + 3 files changed, 102 insertions(+), 88 deletions(-) create mode 100644 commands.js diff --git a/commands.js b/commands.js new file mode 100644 index 0000000..ae120d5 --- /dev/null +++ b/commands.js @@ -0,0 +1,86 @@ +import { SERVER_BUFFER } from "/state.js"; + +export default { + "query": (app, args) => { + var nick = args[0]; + if (!nick) { + throw new Error("Missing nickname"); + } + app.open(nick); + }, + "quit": (app, args) => { + if (window.localStorage) { + localStorage.removeItem("autoconnect"); + } + app.client.close(); + }, + "query": (app, args) => { + var nick = args[0]; + if (!nick) { + throw new Error("Missing nickname"); + } + app.open(nick); + }, + "close": (app, args) => { + var target = app.state.activeBuffer; + if (!target || target == SERVER_BUFFER) { + throw new Error("Not in a user or channel buffer"); + } + app.close(target); + }, + "join": (app, args) => { + var channel = args[0]; + if (!channel) { + throw new Error("Missing channel name"); + } + app.client.send({ command: "JOIN", params: [channel] }); + }, + "part": (app, args) => { + var reason = args.join(" "); + var channel = app.state.activeBuffer; + if (!channel || !app.isChannel(channel)) { + throw new Error("Not in a channel"); + } + var params = [channel]; + if (reason) { + params.push(reason); + } + app.client.send({ command: "PART", params }); + }, + "msg": (app, args) => { + var target = args[0]; + var text = args.slice(1).join(" "); + app.client.send({ command: "PRIVMSG", params: [target, text] }); + }, + "me": (app, args) => { + var action = args.join(" "); + var target = app.state.activeBuffer; + if (!target) { + throw new Error("Not in a buffer"); + } + var text = `\x01ACTION ${action}\x01`; + app.privmsg(target, text); + }, + "nick": (app, args) => { + var newNick = args[0]; + app.client.send({ command: "NICK", params: [newNick] }); + }, + "buffer": (app, args) => { + var name = args[0]; + if (!app.state.buffers.has(name)) { + throw new Error("Unknown buffer"); + } + app.switchBuffer(name); + }, + "topic": (app, args) => { + var channel = app.state.activeBuffer; + if (!channel || !app.isChannel(channel)) { + throw new Error("Not in a channel"); + } + var params = [channel]; + if (args.length > 0) { + params.push(args.join(" ")); + } + app.client.send({ command: "TOPIC", params }); + }, +}; diff --git a/components/app.js b/components/app.js index 545ebb2..aa93ac0 100644 --- a/components/app.js +++ b/components/app.js @@ -8,9 +8,9 @@ import Connect from "/components/connect.js"; import Composer from "/components/composer.js"; import ScrollManager from "/components/scroll-manager.js"; import { html, Component, createRef } from "/lib/index.js"; -import { BufferType, Status, Unread } from "/state.js"; +import { SERVER_BUFFER, BufferType, Status, Unread } from "/state.js"; +import commands from "/commands.js"; -const SERVER_BUFFER = "*"; const CHATHISTORY_PAGE_SIZE = 100; var messagesCount = 0; @@ -503,93 +503,19 @@ export default class App extends Component { executeCommand(s) { var parts = s.split(" "); - var cmd = parts[0].toLowerCase().slice(1); + var name = parts[0].toLowerCase().slice(1); var args = parts.slice(1); - switch (cmd) { - case "quit": - if (window.localStorage) { - localStorage.removeItem("autoconnect"); - } - this.client.close(); - break; - case "query": - var nick = args[0]; - if (!nick) { - console.error("Missing nickname"); - return; - } - this.open(nick); - break; - case "close": - var target = this.state.activeBuffer; - if (!target || target == SERVER_BUFFER) { - console.error("Not in a user or channel buffer"); - return; - } - this.close(target); - break; - case "join": - var channel = args[0]; - if (!channel) { - console.error("Missing channel name"); - return; - } - this.client.send({ command: "JOIN", params: [channel] }); - break; - case "part": - var reason = args.join(" "); - var channel = this.state.activeBuffer; - if (!channel || !this.isChannel(channel)) { - console.error("Not in a channel"); - return; - } - var params = [channel]; - if (reason) { - params.push(reason); - } - this.client.send({ command: "PART", params }); - break; - case "msg": - var target = args[0]; - var text = args.slice(1).join(" "); - this.client.send({ command: "PRIVMSG", params: [target, text] }); - break; - case "me": - var action = args.join(" "); - var target = this.state.activeBuffer; - if (!target) { - console.error("Not in a buffer"); - return; - } - var text = `\x01ACTION ${action}\x01`; - this.privmsg(target, text); - break; - case "nick": - var newNick = args[0]; - this.client.send({ command: "NICK", params: [newNick] }); - break; - case "buffer": - var name = args[0]; - if (!this.state.buffers.has(name)) { - console.error("Unknown buffer"); - return; - } - this.switchBuffer(name); - break; - case "topic": - var channel = this.state.activeBuffer; - if (!channel || !this.isChannel(channel)) { - console.error("Not in a channel"); - return; - } - var params = [channel]; - if (args.length > 0) { - params.push(args.join(" ")); - } - this.client.send({ command: "TOPIC", params }); - break; - default: - console.error("Unknwon command '" + cmd + "'"); + + var cmd = commands[name]; + if (!cmd) { + console.error("Unknwon command '" + name + "'"); + return; + } + + try { + cmd(this, args); + } catch (err) { + console.error(err); } } diff --git a/state.js b/state.js index 4ef6567..b6dcce4 100644 --- a/state.js +++ b/state.js @@ -1,3 +1,5 @@ +export const SERVER_BUFFER = "*"; + export const BufferType = { SERVER: "server", CHANNEL: "channel",