mpp-server-dev2/oldsrc/Message.js

517 lines
16 KiB
JavaScript
Raw Normal View History

2023-03-31 16:43:24 +02:00
const Quota = require("./Quota");
2020-04-07 09:55:16 +02:00
const User = require("./User.js");
2022-03-29 06:41:30 +02:00
const Channel = require("./Channel.js");
2023-03-31 16:43:24 +02:00
const RoomSettings = require("./RoomSettings");
const Database = require("./Database");
const { MOTDGenerator } = require("./MOTDGenerator");
2023-04-01 07:05:12 +02:00
const Notification = require("./Notification");
2021-04-12 23:29:02 +02:00
2023-03-31 16:43:24 +02:00
module.exports = cl => {
2021-04-13 04:08:29 +02:00
cl.once("hi", (msg, admin) => {
if (msg.hasOwnProperty("password")) {
if (msg.password == "hideme") {
cl.hidden = true;
}
}
2023-03-31 16:43:24 +02:00
2021-04-13 04:08:29 +02:00
let m = {};
2021-04-12 23:29:02 +02:00
m.m = "hi";
2022-08-16 15:42:09 +02:00
m.motd = MOTDGenerator.getCurrentMOTD();
2021-04-12 23:29:02 +02:00
m.t = Date.now();
m.u = {
name: cl.user.name,
_id: cl.user._id,
id: cl.participantId,
color: cl.user.color
};
2021-05-09 10:52:41 +02:00
m.v = "2.0";
2021-04-12 23:29:02 +02:00
cl.sendArray([m]);
});
2020-04-07 09:55:16 +02:00
cl.on("t", msg => {
if (msg.hasOwnProperty("e") && !isNaN(msg.e))
2023-03-31 16:43:24 +02:00
cl.sendArray([
{
m: "t",
t: Date.now(),
e: msg.e
}
]);
2021-04-12 23:29:02 +02:00
});
2020-04-07 09:55:16 +02:00
cl.on("ch", msg => {
2023-03-31 16:43:24 +02:00
if (typeof msg.set !== "object") msg.set = {};
2021-04-12 23:29:02 +02:00
2023-03-31 16:43:24 +02:00
if (typeof msg._id == "string") {
2020-04-07 09:55:16 +02:00
if (msg._id.length > 512) return;
2020-04-07 23:02:35 +02:00
if (!cl.staticQuotas.room.attempt()) return;
2021-04-12 23:29:02 +02:00
2021-05-10 04:02:04 +02:00
cl.user.checkFlags();
2020-04-07 09:55:16 +02:00
cl.setChannel(msg._id, msg.set);
2021-04-12 23:29:02 +02:00
2020-04-07 23:02:35 +02:00
let param;
2020-04-07 09:55:16 +02:00
if (cl.channel.isLobby(cl.channel._id)) {
2021-03-01 05:04:31 +01:00
param = Quota.N_PARAMS_LOBBY;
2020-04-07 09:55:16 +02:00
} else {
if (!(cl.user._id == cl.channel.crown.userId)) {
2021-03-01 05:04:31 +01:00
param = Quota.N_PARAMS_NORMAL;
2020-04-07 09:55:16 +02:00
} else {
2021-03-01 05:04:31 +01:00
param = Quota.N_PARAMS_RIDICULOUS;
2020-04-07 09:55:16 +02:00
}
}
2023-03-31 16:43:24 +02:00
2021-03-01 05:04:31 +01:00
param.m = "nq";
2021-05-09 10:52:41 +02:00
setTimeout(() => {
cl.sendArray([param]);
}, 1000);
2020-04-07 09:55:16 +02:00
}
2021-04-12 23:29:02 +02:00
});
2020-04-07 23:02:35 +02:00
cl.on("m", (msg, admin) => {
2022-06-17 08:36:49 +02:00
// nobody will see our cursor if we're not somewhere
2023-03-31 16:43:24 +02:00
if (!("channel" in cl)) return;
2022-06-17 08:36:49 +02:00
// check against cursor rate limit
2021-05-28 23:15:00 +02:00
if (!cl.quotas.cursor.attempt() && !admin) return;
2022-06-17 08:36:49 +02:00
// if we are nobody, we don't have a cursor
if (!cl.participantId) return;
// no values? null, not undefined
2020-04-07 09:55:16 +02:00
if (!msg.hasOwnProperty("x")) msg.x = null;
if (!msg.hasOwnProperty("y")) msg.y = null;
2022-01-26 01:44:26 +01:00
if (isNaN(parseFloat(msg.x))) msg.x = null;
if (isNaN(parseFloat(msg.y))) msg.y = null;
2022-06-17 08:36:49 +02:00
let m = {
p: cl.participantId,
x: msg.x,
y: msg.y
2023-03-31 16:43:24 +02:00
};
2022-06-17 08:36:49 +02:00
cl.channel.emit("m", m);
2021-04-12 23:29:02 +02:00
});
2020-04-07 09:55:16 +02:00
2020-04-07 23:02:35 +02:00
cl.on("chown", (msg, admin) => {
if (!cl.quotas.chown.attempt() && !admin) return;
2020-04-07 09:55:16 +02:00
if (!(cl.channel && cl.participantId)) return;
2021-04-12 23:29:02 +02:00
2020-04-07 09:55:16 +02:00
//console.log((Date.now() - cl.channel.crown.time))
//console.log(!(cl.channel.crown.userId != cl.user._id), !((Date.now() - cl.channel.crown.time) > 15000));
2021-04-12 23:29:02 +02:00
2022-07-09 06:38:32 +02:00
if (!cl.channel.crown && !admin) {
2023-03-31 16:43:24 +02:00
if (
!(cl.channel.crown.userId == cl.user._id) &&
!(Date.now() - cl.channel.crown.time > 15000)
)
return;
2022-07-09 06:38:32 +02:00
}
2021-04-12 23:29:02 +02:00
2020-04-07 09:55:16 +02:00
if (msg.hasOwnProperty("id")) {
// console.log(cl.channel.crown)
2021-04-12 23:29:02 +02:00
if (!admin) {
2023-03-31 16:43:24 +02:00
if (
cl.user._id == cl.channel.crown.userId ||
cl.channel.crowndropped
) {
2021-04-12 23:29:02 +02:00
cl.channel.chown(msg.id);
if (msg.id == cl.user.id) {
2023-03-31 16:43:24 +02:00
param = Quota.N_PARAMS_RIDICULOUS;
2021-04-12 23:29:02 +02:00
param.m = "nq";
2023-01-15 01:48:04 +01:00
cl.sendArray([param]);
2021-04-12 23:29:02 +02:00
}
}
} else {
2020-04-07 09:55:16 +02:00
cl.channel.chown(msg.id);
2020-04-07 23:02:35 +02:00
if (msg.id == cl.user.id) {
2023-03-31 16:43:24 +02:00
param = Quota.N_PARAMS_RIDICULOUS;
2020-04-07 23:02:35 +02:00
param.m = "nq";
2023-01-15 01:48:04 +01:00
cl.sendArray([param]);
2020-04-07 23:02:35 +02:00
}
2021-04-12 23:29:02 +02:00
}
2020-04-07 09:55:16 +02:00
} else {
2021-04-12 23:29:02 +02:00
if (!admin) {
2023-03-31 16:43:24 +02:00
if (
cl.user._id == cl.channel.crown.userId ||
cl.channel.crowndropped
) {
2021-04-12 23:29:02 +02:00
cl.channel.chown();
2023-03-31 16:43:24 +02:00
param = Quota.N_PARAMS_NORMAL;
2021-04-12 23:29:02 +02:00
param.m = "nq";
2023-01-15 01:48:04 +01:00
cl.sendArray([param]);
2021-04-12 23:29:02 +02:00
}
} else {
2020-04-07 09:55:16 +02:00
cl.channel.chown();
2023-03-31 16:43:24 +02:00
param = Quota.N_PARAMS_RIDICULOUS;
2020-04-07 23:02:35 +02:00
param.m = "nq";
2021-04-12 23:29:02 +02:00
cl.sendArray([param]);
}
2020-04-07 09:55:16 +02:00
}
2021-04-12 23:29:02 +02:00
});
2021-04-13 00:19:16 +02:00
cl.on("chset", (msg, admin) => {
2020-04-07 09:55:16 +02:00
if (!(cl.channel && cl.participantId)) return;
2021-04-13 00:19:16 +02:00
if (!cl.channel.crown && !admin) return;
if (!admin) {
if (!(cl.user._id == cl.channel.crown.userId)) return;
}
2023-03-31 16:43:24 +02:00
if (!msg.hasOwnProperty("set") || !msg.set)
msg.set = new RoomSettings(cl.channel.settings, "user");
2022-06-17 08:36:49 +02:00
cl.channel.settings.changeSettings(msg.set, admin);
2022-07-09 11:06:51 +02:00
// cl.channel.updateCh();
2023-03-31 16:43:24 +02:00
cl.channel.emit("update");
2021-04-12 23:29:02 +02:00
});
2023-03-31 16:43:24 +02:00
cl.on("a", (msg, admin) => {
2020-04-07 09:55:16 +02:00
if (!(cl.channel && cl.participantId)) return;
2023-03-31 16:43:24 +02:00
if (cl.user.flags["cant_chat"]) return;
if (!msg.hasOwnProperty("message")) return;
if (typeof msg.message !== "string") return;
2020-04-07 09:55:16 +02:00
if (cl.channel.settings.chat) {
2023-03-31 16:43:24 +02:00
if (admin && msg.admin == true) {
cl.channel.adminChat(msg.message);
} else {
if (cl.channel.isLobby(cl.channel._id)) {
if (
!cl.quotas.chat.lobby.attempt() &&
!admin &&
!cl.user.hasFlag("no chat rate limit", true)
)
return;
} else {
if (!(cl.user._id == cl.channel.crown.userId)) {
if (
!cl.quotas.chat.normal.attempt() &&
!admin &&
!cl.user.hasFlag("no chat rate limit", true)
)
return;
} else {
if (
!cl.quotas.chat.insane.attempt() &&
!admin &&
!cl.user.hasFlag("no chat rate limit", true)
)
return;
}
}
cl.channel.emit("a", cl, msg);
}
2020-04-07 09:55:16 +02:00
}
2021-04-12 23:29:02 +02:00
});
2023-03-31 16:43:24 +02:00
cl.on("n", (msg, admin) => {
2020-04-07 09:55:16 +02:00
if (!(cl.channel && cl.participantId)) return;
2023-03-31 16:43:24 +02:00
if (!msg.hasOwnProperty("t") || !msg.hasOwnProperty("n")) return;
if (typeof msg.t != "number" || typeof msg.n != "object") return;
// if (cl.quotas.note && !admin) {
// if (!cl.quotas.note.attempt()) return;
// }
if (cl.channel.settings.crownsolo) {
if (
cl.channel.crown.userId == cl.user._id &&
!cl.channel.crowndropped
) {
cl.channel.playNote(cl, msg);
}
} else {
cl.channel.playNote(cl, msg);
}
2021-04-12 23:29:02 +02:00
});
2023-03-31 16:43:24 +02:00
cl.on("+ls", msg => {
2020-04-07 09:55:16 +02:00
if (!(cl.channel && cl.participantId)) return;
cl.server.roomlisteners.set(cl.connectionid, cl);
let rooms = [];
2023-03-31 16:43:24 +02:00
for (let room of Array.from(cl.server.channels.values())) {
2021-05-09 10:52:41 +02:00
let data = room.fetchChannelData().ch;
2020-04-07 09:55:16 +02:00
if (room.bans.get(cl.user._id)) {
data.banned = true;
}
if (room.settings.visible) rooms.push(data);
}
2023-03-31 16:43:24 +02:00
cl.sendArray([
{
m: "ls",
c: true,
u: rooms
}
]);
2021-04-12 23:29:02 +02:00
});
2023-03-31 16:43:24 +02:00
cl.on("-ls", msg => {
2020-04-07 09:55:16 +02:00
if (!(cl.channel && cl.participantId)) return;
cl.server.roomlisteners.delete(cl.connectionid);
2021-04-12 23:29:02 +02:00
});
2022-07-04 00:38:42 +02:00
cl.on("userset", (msg, admin) => {
2020-04-07 09:55:16 +02:00
if (!(cl.channel && cl.participantId)) return;
if (!msg.hasOwnProperty("set") || !msg.set) msg.set = {};
2023-03-31 16:43:24 +02:00
if (msg.set.hasOwnProperty("name") && typeof msg.set.name == "string") {
2022-07-04 00:38:42 +02:00
cl.userset(msg.set.name, admin);
2020-04-07 09:55:16 +02:00
}
2021-04-12 23:29:02 +02:00
});
2023-03-31 16:43:24 +02:00
cl.on("kickban", (msg, admin) => {
2022-07-09 11:12:13 +02:00
if (!admin) {
if (cl.channel.crown == null) return;
if (!(cl.channel && cl.participantId)) return;
if (!cl.channel.crown.userId) return;
if (!(cl.user._id == cl.channel.crown.userId)) return;
}
2023-03-31 16:43:24 +02:00
if (msg.hasOwnProperty("_id") && typeof msg._id == "string") {
2020-04-07 23:02:35 +02:00
if (!cl.quotas.kickban.attempt() && !admin) return;
2020-04-07 09:55:16 +02:00
let _id = msg._id;
2023-01-15 01:48:04 +01:00
let ms = msg.ms || 36e5;
2020-04-07 09:55:16 +02:00
cl.channel.kickban(_id, ms);
}
2021-04-12 23:29:02 +02:00
});
2023-03-31 16:43:24 +02:00
cl.on("unban", (msg, admin) => {
2022-07-09 11:12:13 +02:00
if (!admin) {
if (cl.channel.crown == null) return;
if (!(cl.channel && cl.participantId)) return;
if (!cl.channel.crown.userId) return;
if (!(cl.user._id == cl.channel.crown.userId)) return;
}
2023-03-31 16:43:24 +02:00
if (msg.hasOwnProperty("_id") && typeof msg._id == "string") {
2022-07-09 11:12:13 +02:00
if (!cl.quotas.kickban.attempt() && !admin) return;
let _id = msg._id;
cl.channel.unban(_id);
}
});
2020-04-07 09:55:16 +02:00
cl.on("bye", msg => {
2021-05-10 04:02:04 +02:00
cl.user.stopFlagEvents();
2020-04-07 09:55:16 +02:00
cl.destroy();
2021-04-12 23:29:02 +02:00
});
2020-04-07 23:02:35 +02:00
cl.on("admin message", msg => {
2022-09-11 07:51:53 +02:00
// if (!(cl.channel && cl.participantId)) return;
2023-03-31 16:43:24 +02:00
if (!msg.hasOwnProperty("password") || !msg.hasOwnProperty("msg"))
return;
if (typeof msg.msg != "object") return;
2020-04-07 09:55:16 +02:00
if (msg.password !== cl.server.adminpass) return;
cl.ws.emit("message", JSON.stringify([msg.msg]), true);
2021-04-12 23:29:02 +02:00
});
2020-04-07 09:55:16 +02:00
//admin only stuff
2022-08-16 15:42:09 +02:00
// TODO move all admin messages to their own stream
2023-03-31 16:43:24 +02:00
cl.on("color", (msg, admin) => {
2020-04-07 09:55:16 +02:00
if (!admin) return;
2022-09-11 07:51:53 +02:00
if (!msg.color) return;
// if (typeof cl.channel.verifyColor(msg.color) != 'string') return;
2023-03-31 16:43:24 +02:00
if (!msg.hasOwnProperty("id") && !msg.hasOwnProperty("_id")) return;
2021-05-09 22:34:22 +02:00
cl.server.connections.forEach(c => {
if (c.destroied) return;
if (c.user._id !== msg._id && c.participantId !== msg.id) return;
2020-04-07 09:55:16 +02:00
2021-05-09 22:34:22 +02:00
c.user.color = msg.color;
require("./Database").updateUser(c.user._id, c.user);
2022-03-29 03:32:56 +02:00
cl.channel.updateParticipant(c.user._id, c.user);
2021-05-09 22:34:22 +02:00
});
2021-04-02 20:02:04 +02:00
});
2023-03-31 16:43:24 +02:00
cl.on("eval", (msg, admin) => {
2021-04-02 20:02:04 +02:00
if (!admin) return;
2023-03-31 16:43:24 +02:00
if (!msg.hasOwnProperty("str")) return;
2021-04-02 20:02:04 +02:00
cl.server.ev(msg.str);
});
2020-04-07 09:55:16 +02:00
2023-03-31 16:43:24 +02:00
cl.on("notification", (msg, admin) => {
2021-04-02 20:02:04 +02:00
if (!admin) return;
2023-03-31 16:43:24 +02:00
if (
!msg.hasOwnProperty("id") ||
(!msg.hasOwnProperty("targetChannel") &&
!msg.hasOwnProperty("targetUser")) ||
!msg.hasOwnProperty("target") ||
!msg.hasOwnProperty("duration")
)
return;
2021-05-09 10:52:41 +02:00
let id = msg.id;
let targetChannel = msg.targetChannel;
let targetUser = msg.targetUser;
let target = msg.target;
let duration = msg.duration;
let klass;
let title;
let text;
let html;
2023-04-01 07:05:12 +02:00
let chat = msg.chat;
2021-05-09 10:52:41 +02:00
if (msg.hasOwnProperty("class")) {
klass = msg.class;
}
if (!msg.hasOwnProperty("html")) {
2023-03-31 16:43:24 +02:00
if (!msg.hasOwnProperty("title") || !msg.hasOwnProperty("text"))
return;
2021-05-09 10:52:41 +02:00
title = msg.title;
text = msg.text;
} else {
html = msg.html;
}
2023-04-01 07:05:12 +02:00
new Notification(cl.server, {
cl,
id,
targetChannel,
targetUser,
target,
class: klass,
2023-03-31 16:43:24 +02:00
title,
text,
html,
2023-04-01 07:05:12 +02:00
chat,
duration
}).send();
2021-05-09 10:52:41 +02:00
});
2023-03-31 16:43:24 +02:00
cl.on("user_flag", (msg, admin) => {
2021-05-09 10:52:41 +02:00
if (!admin) return;
2023-03-31 16:43:24 +02:00
if (
!msg.hasOwnProperty("_id") ||
!msg.hasOwnProperty("key") ||
!msg.hasOwnProperty("value")
)
return;
cl.server.connections.forEach(usr => {
if (
usr.channel &&
usr.participantId &&
usr.user &&
(usr.user._id == msg._id || usr.participantId == msg.id)
) {
if (!usr.hasOwnProperty("user")) return;
2021-05-09 22:34:22 +02:00
if (msg.key == "remove") {
delete usr.user.flags[msg.key];
usr.user.flags[msg.key] = undefined;
return;
}
2021-05-09 10:52:41 +02:00
usr.user.flags[msg.key] = msg.value;
2021-05-10 04:02:04 +02:00
Database.updateUser(usr.user._id, usr.user);
2021-05-09 10:52:41 +02:00
usr.user.checkFlags();
}
});
});
2023-03-31 16:43:24 +02:00
cl.on("channel_flag", (msg, admin) => {
2021-05-09 10:52:41 +02:00
if (!admin) return;
2023-03-31 16:43:24 +02:00
if (
!msg.hasOwnProperty("_id") ||
!msg.hasOwnProperty("key") ||
!msg.hasOwnProperty("value")
)
return;
2022-03-29 03:32:56 +02:00
try {
2023-03-31 16:43:24 +02:00
let ch = cl.server.channels.get(msg._id);
2022-03-29 03:32:56 +02:00
ch.flags[msg.key] = msg.value;
2023-03-31 16:43:24 +02:00
ch.emit("flag " + msg.key, msg.value);
} catch (err) {
2022-03-29 03:32:56 +02:00
console.error(err);
}
});
2023-03-31 16:43:24 +02:00
cl.on("room_flag", (msg, admin) => {
2022-06-18 06:44:10 +02:00
if (!admin) return;
2023-03-31 16:43:24 +02:00
cl.emit("channel_flag", msg, admin);
});
2022-06-18 06:44:10 +02:00
2023-03-31 16:43:24 +02:00
cl.on("clear_chat", (msg, admin) => {
2022-03-29 03:32:56 +02:00
if (!admin) return;
2022-03-29 06:41:30 +02:00
cl.channel.setChatArray([]);
2022-03-29 03:32:56 +02:00
});
2023-03-31 16:43:24 +02:00
cl.on("sudo", (msg, admin) => {
2022-03-29 03:32:56 +02:00
if (!admin) return;
2023-03-31 16:43:24 +02:00
if (typeof msg._id !== "string") return;
if (typeof msg.msg !== "object") return;
2022-03-29 03:32:56 +02:00
if (!msg.msg.m) return;
cl.server.connections.forEach(c => {
if (c.user._id !== msg._id) return;
c.emit(msg.msg.m, msg.msg);
});
2021-04-02 20:02:04 +02:00
});
2022-03-29 19:37:04 +02:00
2023-03-31 16:43:24 +02:00
cl.on("subscribe to admin stream", (msg, admin) => {
2022-05-24 20:22:19 +02:00
// if (!admin) return;
2023-03-31 16:43:24 +02:00
if (!("password" in msg)) return;
2022-05-24 20:22:19 +02:00
if (msg.password !== cl.server.adminpass) return;
cl.isSubscribedToAdminStream = true;
let interval = 8000;
2023-03-31 16:43:24 +02:00
if ("interval_ms" in msg) interval = msg["interval_ms"];
2022-08-16 12:23:21 +02:00
cl.sendAdminData();
2022-05-24 20:22:19 +02:00
cl.adminStreamInterval = setInterval(() => {
if (cl.isSubscribedToAdminStream == true) cl.sendAdminData();
}, interval);
});
2023-03-31 16:43:24 +02:00
cl.on("unsubscribe from admin stream", (msg, admin) => {
2022-05-24 20:22:19 +02:00
// if (!admin) return;
2023-03-31 16:43:24 +02:00
if (!("password" in msg)) return;
2022-05-24 20:22:19 +02:00
if (msg.password !== cl.server.adminpass) return;
cl.isSubscribedToAdminStream = false;
if (cl.adminStreamInterval) {
clearInterval(cl.adminStreamInterval);
cl.adminStreamInterval = undefined;
while (cl.adminStreamInterval !== undefined) {
cl.adminStreamInterval = undefined;
}
}
2022-03-29 19:37:04 +02:00
});
2022-06-17 08:36:49 +02:00
2023-03-31 16:43:24 +02:00
cl.on("channel message", (msg, admin) => {
2022-06-17 08:36:49 +02:00
if (!admin) return;
2023-03-31 16:43:24 +02:00
if (!msg.hasOwnProperty("msg")) return;
if (typeof msg.msg != "object") return;
if (typeof msg.msg.m != "string") return;
2022-06-17 08:36:49 +02:00
if (!cl.channel) return;
2023-03-31 16:43:24 +02:00
if (!msg.hasOwnProperty("_id")) msg._id = cl.channel._id;
2022-06-17 08:36:49 +02:00
2023-03-31 16:43:24 +02:00
let ch = cl.server.channels.get(msg._id);
2022-06-17 08:36:49 +02:00
if (!ch) return;
2022-07-09 06:38:32 +02:00
ch.emit(msg.msg.m, msg.msg);
2022-06-17 08:36:49 +02:00
});
2022-07-04 00:38:42 +02:00
2023-03-31 16:43:24 +02:00
cl.on("name", (msg, admin) => {
2022-07-04 00:38:42 +02:00
if (!admin) return;
2023-03-31 16:43:24 +02:00
if (!msg.hasOwnProperty("_id")) return;
if (!msg.hasOwnProperty("name")) return;
2022-07-09 06:44:49 +02:00
for (const [mapID, conn] of cl.server.connections) {
if (!conn.user) return;
2022-07-04 00:38:42 +02:00
if (conn.user._id == msg._id) {
2022-07-09 06:44:49 +02:00
let c = conn;
c.userset(msg.name, true);
2022-07-04 00:38:42 +02:00
}
}
});
2022-07-09 11:06:51 +02:00
2023-03-31 16:43:24 +02:00
cl.on("restart", (msg, admin) => {
2022-07-09 11:06:51 +02:00
if (!admin) return;
cl.server.restart(msg.notification);
});
2023-04-01 07:05:12 +02:00
cl.on("ipban", (msg, admin) => {
if (!admin) return;
if (!msg.ip) return;
cl.server.banIP(msg.ip);
});
cl.on("ipunban", (msg, admin) => {
if (!admin) return;
if (!msg.ip) return;
cl.server.unbanIP(msg.ip);
});
2023-03-31 16:43:24 +02:00
};