gamja-old/commands.js

423 lines
10 KiB
JavaScript
Raw Normal View History

import * as irc from "./lib/irc.js";
import { SERVER_BUFFER, BufferType, Unread } from "./state.js";
2020-07-13 11:22:24 -04:00
2021-01-22 11:36:53 -05:00
function getActiveClient(app) {
2021-06-10 12:11:11 -04:00
let buf = app.state.buffers.get(app.state.activeBuffer);
2021-01-22 11:36:53 -05:00
if (!buf) {
throw new Error("Not connected to server");
2021-01-22 11:36:53 -05:00
}
return app.clients.get(buf.server);
2021-01-22 11:36:53 -05:00
}
function getActiveTarget(app) {
2021-06-10 12:11:11 -04:00
let activeBuffer = app.state.buffers.get(app.state.activeBuffer);
if (!activeBuffer) {
throw new Error("Not in a buffer");
}
return activeBuffer.name;
}
function getActiveChannel(app) {
2021-06-10 12:11:11 -04:00
let activeBuffer = app.state.buffers.get(app.state.activeBuffer);
if (!activeBuffer || activeBuffer.type !== BufferType.CHANNEL) {
throw new Error("Not in a channel");
}
return activeBuffer.name;
}
2021-06-03 04:25:33 -04:00
function setUserHostMode(app, args, mode) {
2021-06-10 12:11:11 -04:00
let nick = args[0];
2021-06-03 04:11:48 -04:00
if (!nick) {
throw new Error("Missing nick");
}
2021-06-10 12:11:11 -04:00
let activeChannel = getActiveChannel(app);
let client = getActiveClient(app);
2021-06-03 04:11:48 -04:00
client.whois(nick).then((whois) => {
const info = whois[irc.RPL_WHOISUSER].params;
const user = info[2];
const host = info[3];
2021-06-03 04:25:33 -04:00
client.send({
command: "MODE",
params: [activeChannel, mode, `*!${user}@${host}`],
});
2021-06-03 04:11:48 -04:00
});
}
2021-05-27 16:02:08 -04:00
function markServerBufferUnread(app) {
2021-06-22 07:38:05 -04:00
let activeBuffer = app.state.buffers.get(app.state.activeBuffer);
if (!activeBuffer || activeBuffer.type === BufferType.SERVER) {
return;
}
app.setBufferState({ server: activeBuffer.server }, (buf) => {
return { unread: Unread.union(buf.unread, Unread.MESSAGE) };
});
}
2021-05-27 08:13:22 -04:00
const join = {
usage: "<name>",
description: "Join a channel",
execute: (app, args) => {
2021-06-10 12:11:11 -04:00
let channel = args[0];
2021-05-27 08:13:22 -04:00
if (!channel) {
throw new Error("Missing channel name");
}
app.open(channel);
2021-05-27 08:13:22 -04:00
},
};
2021-05-27 16:02:08 -04:00
const kick = {
usage: "<nick>",
description: "Remove a user from the channel",
execute: (app, args) => {
2021-06-10 12:11:11 -04:00
let nick = args[0];
let activeChannel = getActiveChannel(app);
let params = [activeChannel, nick];
2021-05-27 16:02:08 -04:00
if (args.length > 1) {
params.push(args.slice(1).join(" "));
}
getActiveClient(app).send({ command: "KICK", params });
},
};
function givemode(app, args, mode) {
// TODO: Handle several users at once
2021-06-10 12:11:11 -04:00
let nick = args[0];
if (!nick) {
throw new Error("Missing nick");
}
2021-06-10 12:11:11 -04:00
let activeChannel = getActiveChannel(app);
getActiveClient(app).send({
command: "MODE",
params: [activeChannel, mode, nick],
});
}
2020-07-13 11:22:24 -04:00
export default {
2021-06-03 04:11:48 -04:00
"ban": {
usage: "[nick]",
description: "Ban a user from the channel, or display the current ban list",
2021-06-03 04:11:48 -04:00
execute: (app, args) => {
if (args.length == 0) {
2021-06-10 12:11:11 -04:00
let activeChannel = getActiveChannel(app);
2021-06-03 04:11:48 -04:00
getActiveClient(app).send({
command: "MODE",
params: [activeChannel, "+b"],
2021-06-03 04:11:48 -04:00
});
} else {
2021-06-03 04:25:33 -04:00
return setUserHostMode(app, args, "+b");
2021-06-03 04:11:48 -04:00
}
},
},
2021-03-08 11:25:00 -05:00
"buffer": {
usage: "<name>",
description: "Switch to a buffer",
execute: (app, args) => {
2021-06-10 12:11:11 -04:00
let name = args[0];
for (let buf of app.state.buffers.values()) {
2021-03-08 11:25:00 -05:00
if (buf.name === name) {
app.switchBuffer(buf);
return;
}
2021-03-07 15:06:26 -05:00
}
2021-03-08 11:25:00 -05:00
throw new Error("Unknown buffer");
},
2020-07-13 11:22:24 -04:00
},
2021-03-08 11:25:00 -05:00
"close": {
description: "Close the current buffer",
execute: (app, args) => {
2021-06-10 12:11:11 -04:00
let activeBuffer = app.state.buffers.get(app.state.activeBuffer);
2021-03-08 11:25:00 -05:00
if (!activeBuffer || activeBuffer.type == BufferType.SERVER) {
throw new Error("Not in a user or channel buffer");
}
app.close(activeBuffer.id);
},
2020-07-13 11:22:24 -04:00
},
2021-05-27 13:48:49 -04:00
"deop": {
usage: "<nick>",
description: "Remove operator status for a user on this channel",
execute: (app, args) => givemode(app, args, "-o"),
},
"devoice": {
usage: "<nick>",
description: "Remove voiced status for a user on this channel",
execute: (app, args) => givemode(app, args, "-v"),
2021-05-27 13:48:49 -04:00
},
2021-03-08 11:25:00 -05:00
"disconnect": {
description: "Disconnect from the server",
execute: (app, args) => {
app.disconnect();
},
2021-03-07 15:06:26 -05:00
},
2021-03-08 11:25:00 -05:00
"help": {
description: "Show help menu",
execute: (app, args) => {
app.openHelp();
},
},
2021-05-28 12:21:05 -04:00
"invite": {
usage: "<nick>",
description: "Invite a user to the channel",
2021-05-28 12:21:05 -04:00
execute: (app, args) => {
2021-06-10 12:11:11 -04:00
let nick = args[0];
2021-05-28 12:21:05 -04:00
if (!nick) {
throw new Error("Missing nick");
}
2021-06-10 12:11:11 -04:00
let activeChannel = getActiveChannel(app);
2021-05-28 12:21:05 -04:00
getActiveClient(app).send({ command: "INVITE", params: [
nick, activeChannel,
2021-05-28 12:21:05 -04:00
]});
},
},
2021-05-27 08:13:22 -04:00
"j": join,
"join": join,
2021-05-27 16:02:08 -04:00
"kick": kick,
"kickban": {
usage: "<target>",
description: "Ban a user and removes them from the channel",
execute: (app, args) => {
2021-05-27 16:02:08 -04:00
kick.execute(app, args);
ban.execute(app, args);
},
},
2021-05-28 19:02:16 -04:00
"lusers": {
usage: "[<mask> [<target>]]",
description: "Request user statistics about the network",
2021-05-28 19:02:16 -04:00
execute: (app, args) => {
getActiveClient(app).send({ command: "LUSERS", params: args });
markServerBufferUnread(app);
2021-05-28 19:02:16 -04:00
},
},
2021-03-08 11:25:00 -05:00
"me": {
usage: "<action>",
description: "Send an action message to the current buffer",
execute: (app, args) => {
2021-06-10 12:11:11 -04:00
let action = args.join(" ");
let target = getActiveTarget(app);
let text = `\x01ACTION ${action}\x01`;
app.privmsg(target, text);
2021-03-08 11:25:00 -05:00
},
2020-07-13 11:22:24 -04:00
},
2021-05-23 05:41:04 -04:00
"mode": {
usage: "[target] [modes] [mode args...]",
description: "Query or change a channel or user mode",
2021-05-23 05:41:04 -04:00
execute: (app, args) => {
2021-06-10 12:11:11 -04:00
let target = args[0];
if (!target || target.startsWith("+") || target.startsWith("-")) {
2021-06-10 12:11:11 -04:00
let activeChannel = getActiveChannel(app);
args = [activeChannel, ...args];
2021-05-27 09:37:09 -04:00
}
2021-05-23 05:41:04 -04:00
getActiveClient(app).send({ command: "MODE", params: args });
},
},
2021-05-28 05:17:37 -04:00
"motd": {
usage: "[server]",
description: "Get the Message Of The Day",
execute: (app, args) => {
getActiveClient(app).send({ command: "MOTD", params: args });
markServerBufferUnread(app);
2021-05-28 05:17:37 -04:00
},
},
2021-03-08 11:25:00 -05:00
"msg": {
usage: "<target> <message>",
description: "Send a message to a nickname or a channel",
execute: (app, args) => {
2021-06-10 12:11:11 -04:00
let target = args[0];
let text = args.slice(1).join(" ");
2021-03-08 11:25:00 -05:00
getActiveClient(app).send({ command: "PRIVMSG", params: [target, text] });
},
2021-03-07 15:06:26 -05:00
},
2021-03-08 11:25:00 -05:00
"nick": {
usage: "<nick>",
description: "Change current nickname",
execute: (app, args) => {
2021-06-10 12:11:11 -04:00
let newNick = args[0];
2021-03-08 11:25:00 -05:00
getActiveClient(app).send({ command: "NICK", params: [newNick] });
},
2020-07-13 11:22:24 -04:00
},
2021-03-08 11:25:00 -05:00
"notice": {
usage: "<target> <message>",
description: "Send a notice to a nickname or a channel",
execute: (app, args) => {
2021-06-10 12:11:11 -04:00
let target = args[0];
let text = args.slice(1).join(" ");
2021-03-08 11:25:00 -05:00
getActiveClient(app).send({ command: "NOTICE", params: [target, text] });
},
2020-08-13 05:04:39 -04:00
},
2021-05-27 13:48:49 -04:00
"op": {
usage: "<nick>",
description: "Give a user operator status on this channel",
execute: (app, args) => givemode(app, args, "+o"),
2021-05-27 13:48:49 -04:00
},
2021-03-08 11:25:00 -05:00
"part": {
usage: "[reason]",
description: "Leave a channel",
execute: (app, args) => {
2021-06-10 12:11:11 -04:00
let reason = args.join(" ");
let activeChannel = getActiveChannel(app);
let params = [activeChannel];
2021-03-08 11:25:00 -05:00
if (reason) {
params.push(reason);
}
getActiveClient(app).send({ command: "PART", params });
},
2021-03-07 15:06:26 -05:00
},
2021-03-08 11:25:00 -05:00
"query": {
usage: "<nick> [message]",
2021-03-08 11:25:00 -05:00
description: "Open a buffer to send messages to a nickname",
execute: (app, args) => {
2021-06-10 12:11:11 -04:00
let nick = args[0];
2021-03-08 11:25:00 -05:00
if (!nick) {
throw new Error("Missing nickname");
}
app.open(nick);
if (args.length > 1) {
2021-09-06 10:53:25 -04:00
let text = args.slice(1).join(" ");
app.privmsg(nick, text);
}
2021-03-08 11:25:00 -05:00
},
2021-03-07 15:06:26 -05:00
},
2021-06-10 18:27:19 -04:00
"quiet": {
usage: "[nick]",
description: "Quiet a user in the channel, or display the current quiet list",
execute: (app, args) => {
if (args.length == 0) {
getActiveClient(app).send({
command: "MODE",
params: [getActiveChannel(app), "+q"],
});
} else {
return setUserHostMode(app, args, "+q");
}
},
},
2021-03-08 11:25:00 -05:00
"quit": {
description: "Quit",
execute: (app, args) => {
app.close({ name: SERVER_BUFFER });
},
2021-03-07 15:06:26 -05:00
},
"quote": {
usage: "<command>",
description: "Send a raw IRC command to the server",
execute: (app, args) => {
2021-06-10 12:11:11 -04:00
let msg;
try {
msg = irc.parseMessage(args.join(" "));
} catch (err) {
throw new Error("Failed to parse IRC command: " + err.message);
}
getActiveClient(app).send(msg);
},
},
2021-03-08 11:25:00 -05:00
"reconnect": {
description: "Reconnect to the server",
execute: (app, args) => {
app.reconnect();
},
2020-07-13 11:22:24 -04:00
},
2021-05-25 14:22:21 -04:00
"setname": {
usage: "<realname>",
description: "Change current realname",
execute: (app, args) => {
2021-06-10 12:11:11 -04:00
let newRealname = args.join(" ");
let client = getActiveClient(app);
2021-05-25 14:22:21 -04:00
if (!client.enabledCaps["setname"]) {
throw new Error("Server doesn't support changing the realname");
}
client.send({ command: "SETNAME", params: [newRealname] });
},
},
2021-05-27 08:36:46 -04:00
"stats": {
2021-05-28 07:58:39 -04:00
usage: "<query> [server]",
description: "Request server statistics",
2021-05-27 08:36:46 -04:00
execute: (app, args) => {
2021-06-10 12:11:11 -04:00
let query = args[0];
2021-05-27 08:36:46 -04:00
if (!query) {
throw new Error("Missing query");
}
2021-06-10 12:11:11 -04:00
let params = [query];
2021-05-27 08:36:46 -04:00
if (args.length > 1) {
params.push(args.slice(1).join(" "));
}
getActiveClient(app).send({ command: "STATS", params });
markServerBufferUnread(app);
2021-05-27 08:36:46 -04:00
},
},
2021-03-08 11:25:00 -05:00
"topic": {
usage: "<topic>",
description: "Change the topic of the current channel",
execute: (app, args) => {
2021-06-10 12:11:11 -04:00
let activeChannel = getActiveChannel(app);
let params = [activeChannel];
2021-03-08 11:25:00 -05:00
if (args.length > 0) {
params.push(args.join(" "));
}
getActiveClient(app).send({ command: "TOPIC", params });
},
2020-07-13 11:22:24 -04:00
},
2021-06-03 04:25:33 -04:00
"unban": {
usage: "<nick>",
description: "Remove a user from the ban list",
execute: (app, args) => {
return setUserHostMode(app, args, "-b");
},
},
2021-06-10 18:27:19 -04:00
"unquiet": {
usage: "<nick>",
description: "Remove a user from the quiet list",
execute: (app, args) => {
return setUserHostMode(app, args, "-q");
},
},
2021-06-11 07:02:04 -04:00
"unvoice": {
usage: "<nick>",
description: "Remove a user from the voiced list",
execute: (app, args) => givemode(app, args, "-v"),
},
"voice": {
usage: "<nick>",
description: "Give a user voiced status on this channel",
execute: (app, args) => givemode(app, args, "+v"),
},
2021-05-31 11:02:56 -04:00
"who": {
usage: "[<mask> [o]]",
description: "Retrieve a list of users",
execute: (app, args) => {
getActiveClient(app).send({ command: "WHO", params: args });
markServerBufferUnread(app);
2021-05-31 11:02:56 -04:00
},
},
"whois": {
usage: "<nick>",
description: "Retrieve information about a user",
execute: (app, args) => {
2021-06-10 12:11:11 -04:00
let nick = args[0];
if (!nick) {
throw new Error("Missing nick");
}
2021-05-31 11:02:56 -04:00
getActiveClient(app).send({ command: "WHOIS", params: [nick] });
markServerBufferUnread(app);
},
},
2021-07-03 13:24:20 -04:00
"whowas": {
usage: "<nick> [count]",
description: "Retrieve information about an offline user",
execute: (app, args) => {
if (args.length < 1) {
throw new Error("Missing nick");
}
getActiveClient(app).send({ command: "WHOWAS", params: args });
markServerBufferUnread(app);
},
},
"list": {
usage: "[filter]",
description: "Retrieve a list of channels from a network",
execute: (app, args) => {
getActiveClient(app).send({ command: "LIST", params: args });
markServerBufferUnread(app);
},
},
2020-07-13 11:22:24 -04:00
};