diff --git a/lib/channel-new.js b/lib/channel-new.js index 0e61eaf6..89f74298 100644 --- a/lib/channel-new.js +++ b/lib/channel-new.js @@ -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); }; diff --git a/lib/user.js b/lib/user.js index 4018b5b1..a3f862b7 100644 --- a/lib/user.js +++ b/lib/user.js @@ -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) { diff --git a/templates/channel.jade b/templates/channel.jade index 9a4a3a85..90c8af6e 100644 --- a/templates/channel.jade +++ b/templates/channel.jade @@ -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 diff --git a/www/assets/js/ui.js b/www/assets/js/ui.js index 119544a1..e25191f5 100644 --- a/www/assets/js/ui.js +++ b/www/assets/js/ui.js @@ -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); }); diff --git a/www/assets/js/util.js b/www/assets/js/util.js index b73e61d2..736ab86b 100644 --- a/www/assets/js/util.js +++ b/www/assets/js/util.js @@ -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);