thelounge/server/plugins/irc-events/modelist.ts

85 lines
1.9 KiB
TypeScript
Raw Normal View History

import {IrcEventHandler} from "../../client";
2017-04-22 12:51:21 +00:00
2024-02-15 22:01:22 +00:00
import Msg from "../../models/msg";
import {MessageType} from "../../../shared/types/msg";
2024-02-24 10:13:11 +00:00
import {SpecialChanType, ChanType} from "../../../shared/types/chan";
2017-04-22 12:51:21 +00:00
export default <IrcEventHandler>function (irc, network) {
2017-04-22 12:51:21 +00:00
const client = this;
2019-04-14 11:44:44 +00:00
irc.on("banlist", (list) => {
const data = list.bans.map((ban) => ({
hostmask: ban.banned,
banned_by: ban.banned_by,
banned_at: ban.banned_at * 1000,
}));
handleList(SpecialChanType.BANLIST, "Ban list", list.channel, data);
2019-04-14 11:44:44 +00:00
});
irc.on("inviteList", (list) => {
const data = list.invites.map((invite) => ({
hostmask: invite.invited,
invited_by: invite.invited_by,
invited_at: invite.invited_at * 1000,
}));
handleList(SpecialChanType.INVITELIST, "Invite list", list.channel, data);
2019-04-14 11:44:44 +00:00
});
function handleList(
type: SpecialChanType,
name: string,
channel: string,
data: {
hostmask: string;
invited_by?: string;
inivted_at?: number;
}[]
) {
2019-04-14 11:44:44 +00:00
if (data.length === 0) {
2017-04-22 12:51:21 +00:00
const msg = new Msg({
time: new Date(),
type: MessageType.ERROR,
2019-04-14 11:44:44 +00:00
text: `${name} is empty`,
2017-04-22 12:51:21 +00:00
});
let chan = network.getChannel(channel);
2019-04-14 11:44:44 +00:00
// Send error to lobby if we receive empty list for a channel we're not in
if (typeof chan === "undefined") {
msg.showInActive = true;
chan = network.getLobby();
}
chan.pushMessage(client, msg, true);
2017-04-22 12:51:21 +00:00
return;
}
2019-04-14 11:44:44 +00:00
const chanName = `${name} for ${channel}`;
2017-04-22 12:51:21 +00:00
let chan = network.getChannel(chanName);
2017-04-22 12:51:21 +00:00
if (typeof chan === "undefined") {
chan = client.createChannel({
type: ChanType.SPECIAL,
2019-04-14 11:44:44 +00:00
special: type,
name: chanName,
2018-07-10 09:16:24 +00:00
data: data,
2017-04-22 12:51:21 +00:00
});
client.emit("join", {
network: network.uuid,
chan: chan.getFilteredClone(true),
2024-03-03 10:33:40 +00:00
shouldOpen: false,
index: network.addChannel(chan),
2017-04-22 12:51:21 +00:00
});
2018-07-10 09:16:24 +00:00
} else {
chan.data = data;
2017-04-22 12:51:21 +00:00
2018-07-10 09:16:24 +00:00
client.emit("msg:special", {
chan: chan.id,
data: data,
});
}
2019-04-14 11:44:44 +00:00
}
2017-04-22 12:51:21 +00:00
};