gamja-old/commands.js

105 lines
2.7 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 {
2021-03-07 15:06:26 -05:00
"buffer": (app, args) => {
var name = args[0];
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-03-07 15:06:26 -05:00
throw new Error("Unknown buffer");
2020-07-13 11:22:24 -04:00
},
"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
},
2021-03-07 15:06:26 -05:00
"disconnect": (app, args) => {
app.disconnect();
},
"help": (app, args) => {
app.openHelp();
},
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
},
"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
},
2021-03-07 15:06:26 -05:00
"msg": (app, args) => {
var target = args[0];
var text = args.slice(1).join(" ");
getActiveClient(app).send({ command: "PRIVMSG", params: [target, 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
},
2021-03-07 15:06:26 -05:00
"part": (app, args) => {
var reason = args.join(" ");
var activeBuffer = app.state.buffers.get(app.state.activeBuffer);
if (!activeBuffer || !app.isChannel(activeBuffer.name)) {
throw new Error("Not in a channel");
2020-07-13 11:22:24 -04:00
}
2021-03-07 15:06:26 -05:00
var params = [activeBuffer.name];
if (reason) {
params.push(reason);
}
getActiveClient(app).send({ command: "PART", params });
},
"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.close({ name: SERVER_BUFFER });
},
"reconnect": (app, args) => {
app.reconnect();
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
},
};