Fix kickban

This commit is contained in:
Hri7566 2024-07-09 11:20:38 -04:00
parent e1937aea02
commit d61eeac221
1 changed files with 85 additions and 33 deletions

View File

@ -147,10 +147,6 @@ export class Channel extends EventEmitter {
if (msg.message.startsWith("/")) { if (msg.message.startsWith("/")) {
this.emit("command", msg, socket); this.emit("command", msg, socket);
} }
if (msg.message == "debug") {
this.logger.info(socket.getUUID(), socket.currentChannelID);
}
} catch (err) { } catch (err) {
this.logger.error(err); this.logger.error(err);
} }
@ -660,7 +656,7 @@ export class Channel extends EventEmitter {
* @param _id User ID to ban * @param _id User ID to ban
* @param t Time in millseconds to ban for * @param t Time in millseconds to ban for
**/ **/
public kickban(_id: string, t: number = 1000 * 60 * 30, banner?: string) { public async kickban(_id: string, t: number = 1000 * 60 * 30, banner?: string) {
const now = Date.now(); const now = Date.now();
if (t < 0 || t > 300 * 60 * 1000) return; if (t < 0 || t > 300 * 60 * 1000) return;
@ -672,6 +668,20 @@ export class Channel extends EventEmitter {
if (!banChannel) return; if (!banChannel) return;
// Check if they are on the server at all
let bannedPart: Participant | undefined;
const bannedUUIDs: string[] = [];
for (const sock of socketsBySocketID.values()) {
if (sock.getUserID() == _id) {
bannedUUIDs.push(sock.getUUID());
const part = sock.getParticipant();
if (part) bannedPart = part;
}
}
if (!bannedPart) return;
let isBanned = this.bans.map(b => b.userId).includes(_id); let isBanned = this.bans.map(b => b.userId).includes(_id);
let overwrite = false; let overwrite = false;
@ -681,30 +691,33 @@ export class Channel extends EventEmitter {
let uuidsToKick: string[] = []; let uuidsToKick: string[] = [];
for (const part of this.ppl) {
if (part._id !== _id) continue;
if (!overwrite) { if (!overwrite) {
this.bans.push({ this.bans.push({
userId: _id, userId: _id,
startTime: now, startTime: now,
endTime: now + t endTime: now + t
}); });
shouldUpdate = true;
} else { } else {
for (const ban of this.bans) { for (const ban of this.bans) {
if (ban.userId !== _id) continue; if (ban.userId !== _id) continue;
ban.startTime = now; ban.startTime = now;
ban.endTime = now + t; ban.endTime = now + t;
} }
}
uuidsToKick = [...uuidsToKick, ...part.uuids];
shouldUpdate = true; shouldUpdate = true;
} }
uuidsToKick = [...uuidsToKick, ...bannedUUIDs];
this.logger.debug("Banned UUIDs:", uuidsToKick);
for (const socket of socketsBySocketID.values()) { for (const socket of socketsBySocketID.values()) {
if (uuidsToKick.includes(socket.getUUID())) { this.logger.debug("Checking UUID:", socket.getUUID(), "| Result:", uuidsToKick.indexOf(socket.getUUID()) !== -1);
if (uuidsToKick.indexOf(socket.getUUID()) !== -1) {
socket.sendNotification({ socket.sendNotification({
title: "Notice", title: "Notice",
text: `Banned from "${this.getID()}" for ${Math.floor(t / 1000 / 60)} minutes.`, text: `Banned from "${this.getID()}" for ${Math.floor(t / 1000 / 60)} minutes.`,
@ -712,17 +725,32 @@ export class Channel extends EventEmitter {
target: "#room", target: "#room",
class: "short" class: "short"
}); });
// If they are here, move them to the ban channel
const ch = socket.getCurrentChannel();
if (ch) {
this.logger.debug("Current channel:", ch.getID(), "| We are:", this.getID());
if (ch.getID() == this.getID())
socket.setChannel(banChannel.getID()); socket.setChannel(banChannel.getID());
} }
} }
}
if (shouldUpdate) { if (shouldUpdate) {
this.emit("update", this); this.emit("update", this);
if (typeof banner !== "undefined") { if (typeof banner !== "undefined") {
const p = this.getParticipantListUnsanitized().find(p => p._id == banner);
const minutes = Math.floor(t / 1000 / 60);
if (p && bannedPart) {
await this.sendChat({
m: "a",
message: `Banned ${bannedPart.name} from the channel for ${minutes} minutes.`
}, p);
this.sendNotification({ this.sendNotification({
title: "Notice", title: "Notice",
text: `${this.getParticipantListUnsanitized().find(p => p._id == banner)?.name} banned ${this.getParticipantListUnsanitized().find(p => p._id == _id)?.name} from the channel for ${Math.floor(t / 1000 / 60)} minutes.`, text: `${p.name} banned ${bannedPart.name} from the channel for ${minutes} minutes.`,
duration: 7000, duration: 7000,
target: "#room", target: "#room",
class: "short" class: "short"
@ -731,7 +759,7 @@ export class Channel extends EventEmitter {
if (banner == _id) { if (banner == _id) {
this.sendNotification({ this.sendNotification({
title: "Certificate of Award", title: "Certificate of Award",
text: `Let it be known that ${this.getParticipantListUnsanitized().find(p => p._id == banner)?.name} kickbanned him/her self.`, text: `Let it be known that ${p.name} kickbanned him/her self.`,
duration: 7000, duration: 7000,
target: "#room" target: "#room"
}); });
@ -739,6 +767,7 @@ export class Channel extends EventEmitter {
} }
} }
} }
}
public isBanned(_id: string) { public isBanned(_id: string) {
const now = Date.now(); const now = Date.now();
@ -785,6 +814,29 @@ export class Channel extends EventEmitter {
html: notif.html html: notif.html
}]); }]);
} }
public async sendChat(msg: ServerEvents["a"], p: Participant) {
if (!msg.message) return;
if (msg.message.length > 512) return;
// Sanitize
msg.message = msg.message
.replace(/\p{C}+/gu, "")
.replace(/(\p{Mc}{5})\p{Mc}+/gu, "$1")
.trim();
let outgoing: ClientEvents["a"] = {
m: "a",
a: msg.message,
t: Date.now(),
p: p
};
this.sendArray([outgoing]);
this.chatHistory.push(outgoing);
await saveChatHistory(this.getID(), this.chatHistory);
}
} }
export default Channel; export default Channel;