sync/lib/channel/customization.js

123 lines
3.1 KiB
JavaScript
Raw Normal View History

2014-05-21 02:30:14 +00:00
var ChannelModule = require("./module");
var XSS = require("../xss");
const TYPE_SETCSS = {
css: "string"
};
const TYPE_SETJS = {
js: "string"
};
const TYPE_SETMOTD = {
motd: "string"
};
function CustomizationModule(channel) {
ChannelModule.apply(this, arguments);
this.css = "";
this.js = "";
this.motd = {
motd: "",
html: ""
};
}
CustomizationModule.prototype = Object.create(ChannelModule.prototype);
CustomizationModule.prototype.load = function (data) {
if ("css" in data) {
this.css = data.css;
}
if ("js" in data) {
this.js = data.js;
}
if ("motd" in data) {
this.motd = {
motd: data.motd.motd || "",
html: data.motd.html || ""
};
}
};
CustomizationModule.prototype.save = function (data) {
data.css = this.css;
data.js = this.js;
data.motd = this.motd;
};
CustomizationModule.prototype.setMotd = function (motd) {
motd = XSS.sanitizeHTML(motd);
var html = motd.replace(/\n/g, "<br>");
this.motd = {
motd: motd,
html: html
};
this.sendMotd(this.channel.users);
};
CustomizationModule.prototype.onUserPostJoin = function (user) {
this.sendCSSJS([user]);
this.sendMotd([user]);
user.socket.typecheckedOn("setChannelCSS", TYPE_SETCSS, this.handleSetCSS.bind(this, user));
user.socket.typecheckedOn("setChannelJS", TYPE_SETJS, this.handleSetJS.bind(this, user));
user.socket.typecheckedOn("setMotd", TYPE_SETMOTD, this.handleSetMotd.bind(this, user));
};
CustomizationModule.prototype.sendCSSJS = function (users) {
var data = {
css: this.css,
js: this.js
};
users.forEach(function (u) {
u.socket.emit("channelCSSJS", data);
});
};
CustomizationModule.prototype.sendMotd = function (users) {
var data = this.motd;
users.forEach(function (u) {
u.socket.emit("setMotd", data);
});
};
CustomizationModule.prototype.handleSetCSS = function (user, data) {
if (!this.channel.modules.permissions.canSetCSS(user)) {
user.kick("Attempted setChannelCSS as non-admin");
return;
}
this.css = data.css.substring(0, 20000);
this.sendCSSJS(this.channel.users);
2014-08-09 18:41:12 +00:00
this.channel.logger.log("[mod] " + user.getName() + " updated the channel CSS");
2014-05-21 02:30:14 +00:00
};
CustomizationModule.prototype.handleSetJS = function (user, data) {
if (!this.channel.modules.permissions.canSetJS(user)) {
user.kick("Attempted setChannelJS as non-admin");
return;
}
this.js = data.js.substring(0, 20000);
this.sendCSSJS(this.channel.users);
2014-08-09 18:41:12 +00:00
this.channel.logger.log("[mod] " + user.getName() + " updated the channel JS");
2014-05-21 02:30:14 +00:00
};
CustomizationModule.prototype.handleSetMotd = function (user, data) {
if (!this.channel.modules.permissions.canEditMotd(user)) {
user.kick("Attempted setMotd with insufficient permission");
return;
}
var motd = data.motd.substring(0, 20000);
this.setMotd(motd);
2014-08-09 18:41:12 +00:00
this.channel.logger.log("[mod] " + user.getName() + " updated the MOTD");
2014-05-21 02:30:14 +00:00
};
module.exports = CustomizationModule;