Improve the way chat filter imports are handled

This commit is contained in:
calzoneman 2014-01-19 01:45:20 -06:00
parent 9c989f7ed7
commit 6471969f55
5 changed files with 65 additions and 29 deletions

View file

@ -2124,6 +2124,62 @@ Channel.prototype.handleToggleLock = function (user) {
this.handleSetLock(user, { locked: !this.playlistLock });
};
/**
* Imports a list of chat filters, replacing the current list
*/
Channel.prototype.importFilters = function (filters) {
this.filters = filters;
this.sendChatFilters(this.users);
};
/**
* Handles a user message to import a list of chat filters
*/
Channel.prototype.handleImportFilters = function (user, data) {
// TODO change to filterimport
if (!this.hasPermission(user, "filteredit")) {
return;
}
if (!(data instanceof Array)) {
return;
}
this.filters = data.map(this.validateChatFilter.bind(this))
.filter(function (f) { return f !== false; });
this.sendChatFilters(this.users);
};
/**
* Validates data for a chat filter
*/
Channel.prototype.validateChatFilter = function (f) {
if (typeof f.source !== "string" || typeof f.flags !== "string" ||
typeof f.replace !== "string") {
return false;
}
if (typeof f.name !== "string") {
f.name = f.source;
}
f.replace = f.replace.substring(0, 1000);
f.flags = f.flags.substring(0, 4);
// TODO XSS prevention
try {
new RegExp(f.source, f.flags);
} catch (e) {
return false;
}
var filter = new Filter(f.name, f.source, f.flags, f.replace);
filter.active = Boolean(f.active);
filter.filterlinks = Boolean(f.filterlinks);
return filter;
};
/**
* Updates a chat filter, or adds a new one if the filter does not exist
*/
@ -2163,29 +2219,11 @@ Channel.prototype.handleUpdateFilter = function (user, f) {
return;
}
if (typeof f.source !== "string" || typeof f.flags !== "string" ||
typeof f.replace !== "string") {
filter = this.validateChatFilter(f);
if (!filter) {
return;
}
if (typeof f.name !== "string") {
f.name = f.source;
}
f.replace = f.replace.substring(0, 1000);
f.flags = f.flags.substring(0, 4);
// TODO XSS prevention
try {
new RegExp(f.source, f.flags);
} catch (e) {
return;
}
var filter = new Filter(f.name, f.source, f.flags, f.replace);
filter.active = Boolean(f.active);
filter.filterlinks = Boolean(f.filterlinks);
this.logger.log("%%% " + user.name + " updated filter: " + f.name);
this.updateFilter(filter);
};

View file

@ -369,6 +369,10 @@ User.prototype.initChannelCallbacks = function () {
self.channel.handleUpdateFilter(self, data);
});
wrap("importFilters", function (data) {
self.channel.handleImportFilters(self, data);
});
// REMOVE FILTER
// https://www.youtube.com/watch?v=SxUU3zncVmI
wrapTypecheck("removeFilter", function (data) {

View file

@ -14,7 +14,7 @@ html(lang="en")
ul.nav.navbar-nav
mixin navdefaultlinks(cname)
li: a(href="#", onclick="javascript:showUserOptions()") Options
li: a(href="#", onclick="javascript:$('#channeloptions').modal()") Channel Settings
li: a#showchansettings(href="#", onclick="javascript:$('#channeloptions').modal()") Channel Settings
mixin navloginlogout(cname)
section#mainpage
.container

View file

@ -600,12 +600,5 @@ $("#cs-chatfilters-import").click(function () {
return;
}
var entries = $("#cs-chatfilters table").data("entries") || [];
entries.forEach(function (f) {
socket.emit("removeFilter", f);
});
data.forEach(function (f) {
socket.emit("updateFilter", f);
});
socket.emit("importFilters", data);
});

View file

@ -789,6 +789,7 @@ function handlePermissionChange() {
handleModPermissions();
}
setVisible("#showchansettings", CLIENT.rank >= 2);
setVisible("#playlistmanagerwrap", CLIENT.rank >= 1);
setVisible("#modflair", CLIENT.rank >= 2);
setVisible("#adminflair", CLIENT.rank >= 255);