Extract commands to separate file

This commit is contained in:
Simon Ser 2020-07-13 17:22:24 +02:00
parent 9312edc20e
commit 476c135379
No known key found for this signature in database
GPG key ID: 0FDE7BE0E88F5E48
3 changed files with 102 additions and 88 deletions

86
commands.js Normal file
View file

@ -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 });
},
};

View file

@ -8,9 +8,9 @@ import Connect from "/components/connect.js";
import Composer from "/components/composer.js"; import Composer from "/components/composer.js";
import ScrollManager from "/components/scroll-manager.js"; import ScrollManager from "/components/scroll-manager.js";
import { html, Component, createRef } from "/lib/index.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; const CHATHISTORY_PAGE_SIZE = 100;
var messagesCount = 0; var messagesCount = 0;
@ -503,93 +503,19 @@ export default class App extends Component {
executeCommand(s) { executeCommand(s) {
var parts = s.split(" "); var parts = s.split(" ");
var cmd = parts[0].toLowerCase().slice(1); var name = parts[0].toLowerCase().slice(1);
var args = parts.slice(1); var args = parts.slice(1);
switch (cmd) {
case "quit": var cmd = commands[name];
if (window.localStorage) { if (!cmd) {
localStorage.removeItem("autoconnect"); console.error("Unknwon command '" + name + "'");
}
this.client.close();
break;
case "query":
var nick = args[0];
if (!nick) {
console.error("Missing nickname");
return; return;
} }
this.open(nick);
break; try {
case "close": cmd(this, args);
var target = this.state.activeBuffer; } catch (err) {
if (!target || target == SERVER_BUFFER) { console.error(err);
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 + "'");
} }
} }

View file

@ -1,3 +1,5 @@
export const SERVER_BUFFER = "*";
export const BufferType = { export const BufferType = {
SERVER: "server", SERVER: "server",
CHANNEL: "channel", CHANNEL: "channel",