hardlounge/server/plugins/irc-events/mode.ts

149 lines
3.2 KiB
TypeScript
Raw Normal View History

import _ from "lodash";
import {IrcEventHandler} from "../../client";
import Msg, {MessageType} from "../../models/msg";
2014-09-13 21:29:45 +00:00
export default <IrcEventHandler>function (irc, network) {
2017-04-01 08:33:17 +00:00
const client = this;
// The following saves the channel key based on channel mode instead of
// extracting it from `/join #channel key`. This lets us not have to
// temporarily store the key until successful join, but also saves the key
// if a key is set or changed while being on the channel.
irc.on("channel info", function (data) {
2017-04-01 08:33:17 +00:00
if (!data.modes) {
return;
}
const targetChan = network.getChannel(data.channel);
2017-04-01 08:33:17 +00:00
if (typeof targetChan === "undefined") {
return;
}
data.modes.forEach((mode) => {
2017-04-01 08:33:17 +00:00
const text = mode.mode;
const add = text[0] === "+";
const char = text[1];
if (char === "k") {
targetChan.key = add ? mode.param : "";
client.save();
}
});
const msg = new Msg({
type: MessageType.MODE_CHANNEL,
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions
text: `${data.raw_modes} ${data.raw_params.join(" ")}`,
});
targetChan.pushMessage(client, msg);
2017-04-01 08:33:17 +00:00
});
2021-12-04 10:42:36 +00:00
irc.on("user info", function (data) {
const serverChan = network.channels[0];
const msg = new Msg({
type: MessageType.MODE_USER,
2021-12-04 10:42:36 +00:00
raw_modes: data.raw_modes,
self: false,
showInActive: true,
});
serverChan.pushMessage(client, msg);
});
irc.on("mode", function (data) {
2017-04-01 08:33:17 +00:00
let targetChan;
2016-03-08 09:42:38 +00:00
if (data.target === irc.user.nick) {
targetChan = network.channels[0];
} else {
2016-03-20 14:28:47 +00:00
targetChan = network.getChannel(data.target);
2016-03-08 09:42:38 +00:00
if (typeof targetChan === "undefined") {
return;
2014-09-13 21:29:45 +00:00
}
2016-03-08 09:42:38 +00:00
}
const msg = new Msg({
time: data.time,
type: MessageType.MODE,
from: targetChan.getUser(data.nick),
text: `${data.raw_modes} ${data.raw_params.join(" ")}`,
self: data.nick === irc.user.nick,
});
2019-12-30 10:10:21 +00:00
const users: string[] = [];
2019-12-30 10:10:21 +00:00
for (const param of data.raw_params) {
2019-12-30 10:10:21 +00:00
if (targetChan.findUser(param)) {
users.push(param);
}
}
2019-12-30 10:10:21 +00:00
if (users.length > 0) {
msg.users = users;
2019-12-30 10:10:21 +00:00
}
targetChan.pushMessage(client, msg);
let usersUpdated = false;
const userModeSortPriority = {};
2017-04-01 08:33:17 +00:00
const supportsMultiPrefix = network.irc.network.cap.isEnabled("multi-prefix");
irc.network.options.PREFIX.forEach((prefix, index) => {
userModeSortPriority[prefix.symbol] = index;
});
data.modes.forEach((mode) => {
const add = mode.mode[0] === "+";
const char = mode.mode[1];
2017-04-01 08:33:17 +00:00
if (char === "k") {
targetChan.key = add ? mode.param : "";
client.save();
}
if (!mode.param) {
2017-04-01 08:33:17 +00:00
return;
}
2017-07-11 14:30:47 +00:00
const user = targetChan.findUser(mode.param);
if (!user) {
2017-04-01 08:33:17 +00:00
return;
}
usersUpdated = true;
if (!supportsMultiPrefix) {
2017-04-01 08:33:17 +00:00
return;
}
const changedMode = network.serverOptions.PREFIX.modeToSymbol[char];
if (!add) {
_.pull(user.modes, changedMode);
} else if (!user.modes.includes(changedMode)) {
user.modes.push(changedMode);
user.modes.sort(function (a, b) {
return userModeSortPriority[a] - userModeSortPriority[b];
});
}
2017-04-01 08:33:17 +00:00
});
if (!usersUpdated) {
return;
}
if (!supportsMultiPrefix) {
// TODO: This is horrible
irc.raw("NAMES", data.target);
} else {
client.emit("users", {
chan: targetChan.id,
});
}
2014-09-13 21:29:45 +00:00
});
};