CyTube/lib/channel/filters.js

301 lines
8 KiB
JavaScript
Raw Normal View History

2014-12-28 16:12:37 +00:00
var FilterList = require("cytubefilters");
2014-05-21 02:30:14 +00:00
var ChannelModule = require("./module");
var XSS = require("../xss");
2014-12-27 06:39:30 +00:00
var Logger = require("../logger");
2014-05-21 02:30:14 +00:00
2014-12-28 16:12:37 +00:00
/*
* Converts JavaScript-style replacements ($1, $2, etc.) with
* PCRE-style (\1, \2, etc.)
*/
function fixReplace(replace) {
return replace.replace(/\$(\d)/g, "\\$1");
}
2014-05-21 02:30:14 +00:00
function validateFilter(f) {
if (typeof f.source !== "string" || typeof f.flags !== "string" ||
typeof f.replace !== "string") {
2014-12-28 16:12:37 +00:00
return null;
2014-05-21 02:30:14 +00:00
}
if (typeof f.name !== "string") {
f.name = f.source;
}
2014-12-28 16:12:37 +00:00
f.replace = fixReplace(f.replace.substring(0, 1000));
2014-05-21 02:30:14 +00:00
f.replace = XSS.sanitizeHTML(f.replace);
f.flags = f.flags.substring(0, 4);
try {
2014-12-28 16:12:37 +00:00
FilterList.checkValidRegex(f.source);
2014-05-21 02:30:14 +00:00
} catch (e) {
2014-12-28 16:12:37 +00:00
return null;
2014-05-21 02:30:14 +00:00
}
2014-12-27 06:39:30 +00:00
var filter = {
name: f.name,
source: f.source,
replace: fixReplace(f.replace),
flags: f.flags,
active: !!f.active,
filterlinks: !!f.filterlinks
};
2014-05-21 02:30:14 +00:00
return filter;
}
2014-12-27 06:39:30 +00:00
function makeDefaultFilter(name, source, flags, replace) {
return {
name: name,
source: source,
flags: flags,
2014-12-28 16:12:37 +00:00
replace: replace,
2014-12-27 06:39:30 +00:00
active: true,
filterlinks: false
};
}
2014-05-21 02:30:14 +00:00
const DEFAULT_FILTERS = [
2014-12-28 16:12:37 +00:00
makeDefaultFilter("monospace", "`(.+?)`", "g", "<code>\\1</code>"),
makeDefaultFilter("bold", "\\*(.+?)\\*", "g", "<strong>\\1</strong>"),
makeDefaultFilter("italic", "_(.+?)_", "g", "<em>\\1</em>"),
makeDefaultFilter("strike", "~~(.+?)~~", "g", "<s>\\1</s>"),
makeDefaultFilter("inline spoiler", "\\[sp\\](.*?)\\[\\/sp\\]", "ig",
"<span class=\"spoiler\">\\1</span>")
2014-05-21 02:30:14 +00:00
];
function ChatFilterModule(channel) {
ChannelModule.apply(this, arguments);
2014-12-28 16:12:37 +00:00
this.filters = new FilterList();
2014-05-21 02:30:14 +00:00
}
ChatFilterModule.prototype = Object.create(ChannelModule.prototype);
ChatFilterModule.prototype.load = function (data) {
if ("filters" in data) {
2014-12-28 16:12:37 +00:00
var filters = data.filters.map(validateFilter).filter(function (f) {
return f !== null;
});
try {
this.filters = new FilterList(filters);
} catch (e) {
Logger.errlog.log("Filter load failed: " + e + " (channel:" +
this.channel.name);
this.channel.logger.log("Failed to load filters: " + e);
2014-05-21 02:30:14 +00:00
}
2014-12-28 16:12:37 +00:00
} else {
this.filters = new FilterList(DEFAULT_FILTERS);
2014-05-21 02:30:14 +00:00
}
};
ChatFilterModule.prototype.save = function (data) {
data.filters = this.filters.pack();
};
2014-05-24 06:09:36 +00:00
ChatFilterModule.prototype.packInfo = function (data, isAdmin) {
if (isAdmin) {
2014-12-27 06:39:30 +00:00
data.chatFilterCount = this.filters.length;
2014-05-24 06:09:36 +00:00
}
};
2014-05-21 02:30:14 +00:00
ChatFilterModule.prototype.onUserPostJoin = function (user) {
2014-12-27 06:39:30 +00:00
user.socket.on("addFilter", this.handleAddFilter.bind(this, user));
2014-05-21 02:30:14 +00:00
user.socket.on("updateFilter", this.handleUpdateFilter.bind(this, user));
user.socket.on("importFilters", this.handleImportFilters.bind(this, user));
user.socket.on("moveFilter", this.handleMoveFilter.bind(this, user));
user.socket.on("removeFilter", this.handleRemoveFilter.bind(this, user));
user.socket.on("requestChatFilters", this.handleRequestChatFilters.bind(this, user));
};
ChatFilterModule.prototype.sendChatFilters = function (users) {
var f = this.filters.pack();
var chan = this.channel;
users.forEach(function (u) {
if (chan.modules.permissions.canEditFilters(u)) {
u.socket.emit("chatFilters", f);
}
});
};
2014-12-27 06:39:30 +00:00
ChatFilterModule.prototype.handleAddFilter = function (user, data) {
if (typeof data !== "object") {
return;
}
if (!this.channel.modules.permissions.canEditFilters(user)) {
return;
}
2014-12-28 16:12:37 +00:00
try {
FilterList.checkValidRegex(data.source);
} catch (e) {
user.socket.emit("errorMsg", {
msg: "Invalid regex: " + e.message,
alert: true
});
return;
}
2014-12-27 06:39:30 +00:00
data = validateFilter(data);
if (!data) {
return;
}
try {
this.filters.addFilter(data);
} catch (e) {
user.socket.emit("errorMsg", {
msg: "Filter add failed: " + e.message,
alert: true
});
return;
}
2014-12-28 16:12:37 +00:00
user.socket.emit("addFilterSuccess");
2014-12-27 06:39:30 +00:00
var chan = this.channel;
chan.users.forEach(function (u) {
if (chan.modules.permissions.canEditFilters(u)) {
u.socket.emit("updateChatFilter", data);
}
});
chan.logger.log("[mod] " + user.getName() + " added filter: " + data.name + " -> " +
2014-12-28 16:12:37 +00:00
"s/" + data.source + "/" + data.replace + "/" + data.flags +
" active: " + data.active + ", filterlinks: " + data.filterlinks);
2014-12-27 06:39:30 +00:00
};
2014-05-21 02:30:14 +00:00
ChatFilterModule.prototype.handleUpdateFilter = function (user, data) {
if (typeof data !== "object") {
return;
}
if (!this.channel.modules.permissions.canEditFilters(user)) {
return;
}
2014-12-28 16:12:37 +00:00
try {
FilterList.checkValidRegex(data.source);
} catch (e) {
user.socket.emit("errorMsg", {
msg: "Invalid regex: " + e.message,
alert: true
});
return;
}
2014-12-27 06:39:30 +00:00
data = validateFilter(data);
if (!data) {
return;
}
try {
this.filters.updateFilter(data);
} catch (e) {
2014-12-28 16:12:37 +00:00
user.socket.emit("errorMsg", {
msg: "Filter update failed: " + e.message,
alert: true
});
2014-05-21 02:30:14 +00:00
return;
}
var chan = this.channel;
chan.users.forEach(function (u) {
if (chan.modules.permissions.canEditFilters(u)) {
u.socket.emit("updateChatFilter", data);
}
});
2014-12-27 06:39:30 +00:00
chan.logger.log("[mod] " + user.getName() + " updated filter: " + data.name + " -> " +
2014-12-28 16:12:37 +00:00
"s/" + data.source + "/" + data.replace + "/" + data.flags +
" active: " + data.active + ", filterlinks: " + data.filterlinks);
2014-05-21 02:30:14 +00:00
};
ChatFilterModule.prototype.handleImportFilters = function (user, data) {
if (!(data instanceof Array)) {
return;
}
/* Note: importing requires a different permission node than simply
updating/removing */
if (!this.channel.modules.permissions.canImportFilters(user)) {
return;
}
2014-12-27 06:39:30 +00:00
try {
this.filters = new FilterList(data.map(validateFilter).filter(function (f) {
2014-12-28 16:12:37 +00:00
return f !== null;
2014-12-27 06:39:30 +00:00
}));
} catch (e) {
user.socket.emit("errorMsg", {
msg: "Filter import failed: " + e.message,
alert: true
});
return;
}
2014-05-21 02:30:14 +00:00
this.channel.logger.log("[mod] " + user.getName() + " imported the filter list");
this.sendChatFilters(this.channel.users);
};
ChatFilterModule.prototype.handleRemoveFilter = function (user, data) {
if (typeof data !== "object") {
return;
}
if (!this.channel.modules.permissions.canEditFilters(user)) {
return;
}
if (typeof data.name !== "string") {
return;
}
2014-12-27 06:39:30 +00:00
try {
this.filters.removeFilter(data);
} catch (e) {
user.socket.emit("errorMsg", {
msg: "Filter removal failed: " + e.message,
alert: true
});
return;
}
2014-05-21 05:42:51 +00:00
var chan = this.channel;
chan.users.forEach(function (u) {
if (chan.modules.permissions.canEditFilters(u)) {
u.socket.emit("deleteChatFilter", data);
}
});
2014-12-28 16:12:37 +00:00
2014-05-21 02:30:14 +00:00
this.channel.logger.log("[mod] " + user.getName() + " removed filter: " + data.name);
};
ChatFilterModule.prototype.handleMoveFilter = function (user, data) {
if (typeof data !== "object") {
return;
}
if (!this.channel.modules.permissions.canEditFilters(user)) {
return;
}
if (typeof data.to !== "number" || typeof data.from !== "number") {
return;
}
2014-12-27 06:39:30 +00:00
try {
this.filters.moveFilter(data.from, data.to);
} catch (e) {
user.socket.emit("errorMsg", {
msg: "Filter move failed: " + e.message,
alert: true
});
return;
}
2014-05-21 02:30:14 +00:00
};
ChatFilterModule.prototype.handleRequestChatFilters = function (user) {
this.sendChatFilters([user]);
};
module.exports = ChatFilterModule;