2022-06-18 20:25:21 -04:00
|
|
|
import _ from "lodash";
|
|
|
|
import {IrcEventHandler} from "../../client";
|
2016-10-09 15:14:02 -04:00
|
|
|
|
2022-06-18 20:25:21 -04:00
|
|
|
import Msg, {MessageType} from "../../models/msg";
|
2014-09-13 17:29:45 -04:00
|
|
|
|
2022-06-18 20:25:21 -04:00
|
|
|
export default <IrcEventHandler>function (irc, network) {
|
2017-04-01 04:33:17 -04: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.
|
2020-03-21 16:55:36 -04:00
|
|
|
irc.on("channel info", function (data) {
|
2017-04-01 04:33:17 -04:00
|
|
|
if (!data.modes) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const targetChan = network.getChannel(data.channel);
|
2018-02-20 02:28:04 -05:00
|
|
|
|
2017-04-01 04:33:17 -04:00
|
|
|
if (typeof targetChan === "undefined") {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-04-08 08:34:31 -04:00
|
|
|
data.modes.forEach((mode) => {
|
2017-04-01 04:33:17 -04:00
|
|
|
const text = mode.mode;
|
|
|
|
const add = text[0] === "+";
|
|
|
|
const char = text[1];
|
|
|
|
|
|
|
|
if (char === "k") {
|
|
|
|
targetChan.key = add ? mode.param : "";
|
|
|
|
client.save();
|
|
|
|
}
|
|
|
|
});
|
2019-03-07 05:57:31 -05:00
|
|
|
|
|
|
|
const msg = new Msg({
|
2022-06-18 20:25:21 -04:00
|
|
|
type: MessageType.MODE_CHANNEL,
|
|
|
|
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions
|
2019-12-30 05:06:54 -05:00
|
|
|
text: `${data.raw_modes} ${data.raw_params.join(" ")}`,
|
2019-03-07 05:57:31 -05:00
|
|
|
});
|
|
|
|
targetChan.pushMessage(client, msg);
|
2017-04-01 04:33:17 -04:00
|
|
|
});
|
|
|
|
|
2021-12-04 05:42:36 -05:00
|
|
|
irc.on("user info", function (data) {
|
|
|
|
const serverChan = network.channels[0];
|
|
|
|
|
|
|
|
const msg = new Msg({
|
2022-06-18 20:25:21 -04:00
|
|
|
type: MessageType.MODE_USER,
|
2021-12-04 05:42:36 -05:00
|
|
|
raw_modes: data.raw_modes,
|
|
|
|
self: false,
|
|
|
|
showInActive: true,
|
|
|
|
});
|
|
|
|
serverChan.pushMessage(client, msg);
|
|
|
|
});
|
|
|
|
|
2020-03-21 16:55:36 -04:00
|
|
|
irc.on("mode", function (data) {
|
2017-04-01 04:33:17 -04:00
|
|
|
let targetChan;
|
2016-03-08 04:42:38 -05:00
|
|
|
|
|
|
|
if (data.target === irc.user.nick) {
|
|
|
|
targetChan = network.channels[0];
|
|
|
|
} else {
|
2016-03-20 10:28:47 -04:00
|
|
|
targetChan = network.getChannel(data.target);
|
2018-02-20 02:28:04 -05:00
|
|
|
|
2016-03-08 04:42:38 -05:00
|
|
|
if (typeof targetChan === "undefined") {
|
|
|
|
return;
|
2014-09-13 17:29:45 -04:00
|
|
|
}
|
2016-03-08 04:42:38 -05:00
|
|
|
}
|
|
|
|
|
2019-12-30 05:06:54 -05:00
|
|
|
const msg = new Msg({
|
|
|
|
time: data.time,
|
2022-06-18 20:25:21 -04:00
|
|
|
type: MessageType.MODE,
|
2019-12-30 05:06:54 -05:00
|
|
|
from: targetChan.getUser(data.nick),
|
|
|
|
text: `${data.raw_modes} ${data.raw_params.join(" ")}`,
|
|
|
|
self: data.nick === irc.user.nick,
|
|
|
|
});
|
2019-12-30 05:10:21 -05:00
|
|
|
|
2022-06-18 20:25:21 -04:00
|
|
|
const users: string[] = [];
|
2019-12-30 05:10:21 -05:00
|
|
|
|
2020-07-29 03:29:51 -04:00
|
|
|
for (const param of data.raw_params) {
|
2019-12-30 05:10:21 -05:00
|
|
|
if (targetChan.findUser(param)) {
|
|
|
|
users.push(param);
|
|
|
|
}
|
2020-07-29 03:29:51 -04:00
|
|
|
}
|
2019-12-30 05:10:21 -05:00
|
|
|
|
2020-07-29 03:29:51 -04:00
|
|
|
if (users.length > 0) {
|
|
|
|
msg.users = users;
|
2019-12-30 05:10:21 -05:00
|
|
|
}
|
|
|
|
|
2019-12-30 05:06:54 -05:00
|
|
|
targetChan.pushMessage(client, msg);
|
|
|
|
|
2022-06-18 20:25:21 -04:00
|
|
|
let usersUpdated = false;
|
2017-04-08 08:34:31 -04:00
|
|
|
const userModeSortPriority = {};
|
2017-04-01 04:33:17 -04:00
|
|
|
const supportsMultiPrefix = network.irc.network.cap.isEnabled("multi-prefix");
|
2016-09-24 12:34:35 -04:00
|
|
|
|
2016-10-12 03:55:40 -04:00
|
|
|
irc.network.options.PREFIX.forEach((prefix, index) => {
|
2016-09-24 12:34:35 -04:00
|
|
|
userModeSortPriority[prefix.symbol] = index;
|
|
|
|
});
|
2016-03-16 16:54:11 -04:00
|
|
|
|
2017-04-08 08:34:31 -04:00
|
|
|
data.modes.forEach((mode) => {
|
2019-12-30 05:06:54 -05:00
|
|
|
const add = mode.mode[0] === "+";
|
|
|
|
const char = mode.mode[1];
|
2017-04-01 04:33:17 -04:00
|
|
|
|
|
|
|
if (char === "k") {
|
|
|
|
targetChan.key = add ? mode.param : "";
|
|
|
|
client.save();
|
|
|
|
}
|
2016-09-24 12:34:35 -04:00
|
|
|
|
|
|
|
if (!mode.param) {
|
2017-04-01 04:33:17 -04:00
|
|
|
return;
|
2016-09-24 12:34:35 -04:00
|
|
|
}
|
|
|
|
|
2017-07-11 10:30:47 -04:00
|
|
|
const user = targetChan.findUser(mode.param);
|
2018-02-20 02:28:04 -05:00
|
|
|
|
2016-09-24 12:34:35 -04:00
|
|
|
if (!user) {
|
2017-04-01 04:33:17 -04:00
|
|
|
return;
|
2016-09-24 12:34:35 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
usersUpdated = true;
|
|
|
|
|
|
|
|
if (!supportsMultiPrefix) {
|
2017-04-01 04:33:17 -04:00
|
|
|
return;
|
2016-09-24 12:34:35 -04:00
|
|
|
}
|
|
|
|
|
2021-07-21 03:30:07 -04:00
|
|
|
const changedMode = network.serverOptions.PREFIX.modeToSymbol[char];
|
2016-09-24 12:34:35 -04:00
|
|
|
|
|
|
|
if (!add) {
|
|
|
|
_.pull(user.modes, changedMode);
|
2017-11-22 01:39:32 -05:00
|
|
|
} else if (!user.modes.includes(changedMode)) {
|
2016-09-24 12:34:35 -04:00
|
|
|
user.modes.push(changedMode);
|
2020-03-21 16:55:36 -04:00
|
|
|
user.modes.sort(function (a, b) {
|
2016-09-24 12:34:35 -04:00
|
|
|
return userModeSortPriority[a] - userModeSortPriority[b];
|
|
|
|
});
|
|
|
|
}
|
2017-04-01 04:33:17 -04:00
|
|
|
});
|
2016-03-16 16:54:11 -04:00
|
|
|
|
2016-09-24 12:34:35 -04:00
|
|
|
if (!usersUpdated) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!supportsMultiPrefix) {
|
2016-03-16 16:54:11 -04:00
|
|
|
// TODO: This is horrible
|
|
|
|
irc.raw("NAMES", data.target);
|
2016-09-24 12:34:35 -04:00
|
|
|
} else {
|
|
|
|
client.emit("users", {
|
2017-11-15 01:35:15 -05:00
|
|
|
chan: targetChan.id,
|
2016-09-24 12:34:35 -04:00
|
|
|
});
|
2016-03-16 16:54:11 -04:00
|
|
|
}
|
2014-09-13 17:29:45 -04:00
|
|
|
});
|
|
|
|
};
|