gamja-old/commands.js

109 lines
2.8 KiB
JavaScript
Raw Normal View History

2021-03-03 03:58:09 -05:00
import { SERVER_BUFFER, BufferType } from "./state.js";
2020-07-13 11:22:24 -04:00
2021-01-22 11:36:53 -05:00
function getActiveClient(app) {
var buf = app.state.buffers.get(app.state.activeBuffer);
if (!buf) {
return null;
}
return app.clients.get(buf.network);
}
2020-07-13 11:22:24 -04:00
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");
}
2021-03-03 03:58:09 -05:00
app.close({ name: SERVER_BUFFER });
2020-07-13 11:22:24 -04:00
},
"query": (app, args) => {
var nick = args[0];
if (!nick) {
throw new Error("Missing nickname");
}
app.open(nick);
},
"close": (app, args) => {
2021-03-03 03:58:09 -05:00
var activeBuffer = app.state.buffers.get(app.state.activeBuffer);
if (!activeBuffer || activeBuffer.type == BufferType.SERVER) {
2020-07-13 11:22:24 -04:00
throw new Error("Not in a user or channel buffer");
}
2021-03-03 03:58:09 -05:00
app.close(activeBuffer.id);
2020-07-13 11:22:24 -04:00
},
"join": (app, args) => {
var channel = args[0];
if (!channel) {
throw new Error("Missing channel name");
}
2021-01-22 11:36:53 -05:00
getActiveClient(app).send({ command: "JOIN", params: [channel] });
2020-07-13 11:22:24 -04:00
},
"part": (app, args) => {
var reason = args.join(" ");
2021-03-03 03:58:09 -05:00
var activeBuffer = app.state.buffers.get(app.state.activeBuffer);
if (!activeBuffer || !app.isChannel(activeBuffer.name)) {
2020-07-13 11:22:24 -04:00
throw new Error("Not in a channel");
}
2021-03-03 03:58:09 -05:00
var params = [activeBuffer.name];
2020-07-13 11:22:24 -04:00
if (reason) {
params.push(reason);
}
2021-01-22 11:36:53 -05:00
getActiveClient(app).send({ command: "PART", params });
2020-07-13 11:22:24 -04:00
},
"msg": (app, args) => {
var target = args[0];
var text = args.slice(1).join(" ");
2021-01-22 11:36:53 -05:00
getActiveClient(app).send({ command: "PRIVMSG", params: [target, text] });
2020-07-13 11:22:24 -04:00
},
"me": (app, args) => {
var action = args.join(" ");
2021-03-03 03:58:09 -05:00
var activeBuffer = app.state.buffers.get(app.state.activeBuffer);
if (!activeBuffer) {
2020-07-13 11:22:24 -04:00
throw new Error("Not in a buffer");
}
var text = `\x01ACTION ${action}\x01`;
2021-03-03 03:58:09 -05:00
app.privmsg(activeBuffer.name, text);
2020-07-13 11:22:24 -04:00
},
"nick": (app, args) => {
var newNick = args[0];
2021-01-22 11:36:53 -05:00
getActiveClient(app).send({ command: "NICK", params: [newNick] });
2020-07-13 11:22:24 -04:00
},
2020-08-13 05:04:39 -04:00
"notice": (app, args) => {
var target = args[0];
var text = args.slice(1).join(" ");
2021-01-22 11:36:53 -05:00
getActiveClient(app).send({ command: "NOTICE", params: [target, text] });
2020-08-13 05:04:39 -04:00
},
2020-07-13 11:22:24 -04:00
"buffer": (app, args) => {
var name = args[0];
2021-01-21 16:15:33 -05:00
for (var buf of app.state.buffers.values()) {
if (buf.name === name) {
app.switchBuffer(buf);
return;
}
2020-07-13 11:22:24 -04:00
}
2021-01-21 16:15:33 -05:00
throw new Error("Unknown buffer");
2020-07-13 11:22:24 -04:00
},
"topic": (app, args) => {
2021-03-03 03:58:09 -05:00
var activeBuffer = app.state.buffers.get(app.state.activeBuffer);
if (!activeBuffer || !app.isChannel(activeBuffer.name)) {
2020-07-13 11:22:24 -04:00
throw new Error("Not in a channel");
}
2021-03-03 03:58:09 -05:00
var params = [activeBuffer.name];
2020-07-13 11:22:24 -04:00
if (args.length > 0) {
params.push(args.join(" "));
}
2021-01-22 11:36:53 -05:00
getActiveClient(app).send({ command: "TOPIC", params });
2020-07-13 11:22:24 -04:00
},
2021-01-12 04:35:38 -05:00
"reconnect": (app, args) => {
app.reconnect();
2021-01-12 04:35:38 -05:00
},
"disconnect": (app, args) => {
app.disconnect();
2021-01-12 04:35:38 -05:00
},
2020-07-13 11:22:24 -04:00
};