Serverside support for custom chat filters

This commit is contained in:
calzoneman 2013-03-31 14:27:54 -05:00
parent 954751ec6c
commit ddc5016497
4 changed files with 95 additions and 20 deletions

View File

@ -43,9 +43,10 @@ var Channel = function(name) {
customcss: "" customcss: ""
}; };
this.filters = [ this.filters = [
[new RegExp("`([^`]+)`", "g"), "<code>$1</code>", true], [/`([^`]+)`/g , "<code>$1</code>" , true],
[new RegExp("\\*([^\\*]+)\\*", "g"), "<strong>$1</strong>", true], [/\*([^\*]+)\*/g , "<strong>$1</strong>", true],
[new RegExp(" _([^_]+)_", "g"), " <em>$1</em>", true] [/(^| )_([^_]+)_/g , "$1<em>$2</em>" , true],
[/\\\\([-a-zA-Z0-9]+)/g, "[](/$1)" , true]
]; ];
this.ipbans = {}; this.ipbans = {};
@ -67,6 +68,14 @@ var Channel = function(name) {
this.currentPosition = data.currentPosition - 1; this.currentPosition = data.currentPosition - 1;
this.playNext(); this.playNext();
this.opts = data.opts; this.opts = data.opts;
if(data.filters) {
this.filters = new Array(data.filters.length);
for(var i = 0; i < data.filters.length; i++) {
this.filters[i] = [new RegExp(data.filters[i][0]),
data.filters[i][1],
data.filters[i][2]];
}
}
} }
catch(e) { catch(e) {
Logger.errlog.log("Channel dump load failed: "); Logger.errlog.log("Channel dump load failed: ");
@ -426,17 +435,6 @@ Channel.prototype.userJoin = function(user) {
user.socket.emit("newPoll", this.poll.packUpdate()); user.socket.emit("newPoll", this.poll.packUpdate());
} }
user.socket.emit("channelOpts", this.opts); user.socket.emit("channelOpts", this.opts);
var ents = [];
for(var ip in this.ipbans) {
if(this.ipbans[ip] != null) {
ents.push({
ip: ip,
name: this.ipbans[ip][0],
banner: this.ipbans[ip][1]
});
}
}
user.socket.emit("banlist", {entries: ents});
if(user.playerReady) if(user.playerReady)
this.sendMediaUpdate(user); this.sendMediaUpdate(user);
this.logger.log("+++ /" + user.ip + " joined"); this.logger.log("+++ /" + user.ip + " joined");
@ -762,6 +760,31 @@ Channel.prototype.moveMedia = function(data) {
} }
} }
Channel.prototype.updateFilter = function(filter) {
var found = false;
for(var i = 0; i < this.filters.length; i++) {
if(this.filters[i][0].source == filter[0].source) {
found = true;
this.filters[i][1] = filter[1];
this.filters[i][2] = filter[2];
}
}
if(!found) {
this.filters.push(filter);
}
this.broadcastChatFilters();
}
Channel.prototype.removeFilter = function(regex) {
for(var i = 0; i < this.filters.length; i++) {
if(this.filters[i][0].source == regex) {
this.filters.splice(i, 1);
break;
}
}
this.broadcastChatFilters();
}
// Chat message from a user // Chat message from a user
Channel.prototype.chatMessage = function(user, msg) { Channel.prototype.chatMessage = function(user, msg) {
if(msg.indexOf("/") == 0) if(msg.indexOf("/") == 0)
@ -950,6 +973,28 @@ Channel.prototype.broadcastRankUpdate = function(user) {
rank: user.rank, rank: user.rank,
leader: this.leader == user leader: this.leader == user
}); });
// Rank specific stuff
if(Rank.hasPermission(user, "ipban")) {
var ents = [];
for(var ip in this.ipbans) {
if(this.ipbans[ip] != null) {
ents.push({
ip: ip,
name: this.ipbans[ip][0],
banner: this.ipbans[ip][1]
});
}
}
user.socket.emit("banlist", {entries: ents});
}
if(Rank.hasPermission(user, "chatFilter")) {
var filts = new Array(this.filters.length);
for(var i = 0; i < this.filters.length; i++) {
filts[i] = [this.filters[i][0].source, this.filters[i][1], this.filters[i][2]];
}
user.socket.emit("chatFilters", {filters: filts});
}
} }
Channel.prototype.broadcastPoll = function() { Channel.prototype.broadcastPoll = function() {
@ -979,7 +1024,23 @@ Channel.prototype.broadcastIpbans = function() {
}); });
} }
} }
this.sendAll("banlist", {entries: ents}); for(var i = 0; i < this.users.length; i++) {
if(Rank.hasPermission(this.users[i], "ipban")) {
this.users[i].socket.emit("banlist", {entries: ents});
}
}
}
Channel.prototype.broadcastChatFilters = function() {
var filts = new Array(this.filters.length);
for(var i = 0; i < this.filters.length; i++) {
filts[i] = [this.filters[i][0].source, this.filters[i][1], this.filters[i][2]];
}
for(var i = 0; i < this.users.length; i++) {
if(Rank.hasPermission(this.users[i], "ipban")) {
this.users[i].socket.emit("chatFilters", {filters: filts});
}
}
} }
// Send to ALL the clients! // Send to ALL the clients!

View File

@ -29,6 +29,7 @@ var permissions = {
shout : exports.Moderator, shout : exports.Moderator,
channelOpts : exports.Moderator, channelOpts : exports.Moderator,
jump : exports.Moderator, jump : exports.Moderator,
chatFilter : exports.Moderator,
search : exports.Guest, search : exports.Guest,
chat : exports.Guest, chat : exports.Guest,
}; };

View File

@ -56,10 +56,17 @@ function shutdown() {
Logger.syslog.log("Unloading channels..."); Logger.syslog.log("Unloading channels...");
for(var name in exports.channels) { for(var name in exports.channels) {
var chan = exports.channels[name]; var chan = exports.channels[name];
var filts = new Array(chan.filters.length);
for(var i = 0; i < chan.filters.length; i++) {
filts[i] = [chan.filters[i][0].source,
chan.filters[i][1],
chan.filters[i][2]];
}
var dump = { var dump = {
currentPosition: chan.currentPosition, currentPosition: chan.currentPosition,
queue: chan.queue, queue: chan.queue,
opts: chan.opts opts: chan.opts,
filters: filts
}; };
var text = JSON.stringify(dump); var text = JSON.stringify(dump);
fs.writeFileSync("chandump/" + name, text); fs.writeFileSync("chandump/" + name, text);

14
user.js
View File

@ -248,6 +248,16 @@ User.prototype.initCallbacks = function() {
this.channel.broadcastOpts(); this.channel.broadcastOpts();
} }
}.bind(this)); }.bind(this));
this.socket.on("chatFilter", function(data) {
if(data.cmd && data.cmd == "update" && this.channel != null) {
data.filter[0] = new RegExp(data.filter[0], "g");
this.channel.updateFilter(data.filter);
}
else if(data.cmd && data.cmd == "remove" && this.channel != null) {
this.channel.removeFilter(data.filter[0]);
}
}.bind(this));
} }
// Handle administration // Handle administration
@ -339,8 +349,6 @@ User.prototype.login = function(name, pw) {
}); });
if(this.channel != null) { if(this.channel != null) {
this.channel.logger.log(this.ip + " signed in as " + name); this.channel.logger.log(this.ip + " signed in as " + name);
if(this.rank >= Rank.Moderator)
this.channel.sendPlaylist(this);
this.channel.broadcastNewUser(this); this.channel.broadcastNewUser(this);
} }
} }
@ -365,8 +373,6 @@ User.prototype.login = function(name, pw) {
this.name = name; this.name = name;
if(this.channel != null) { if(this.channel != null) {
this.channel.logger.log(this.ip + " logged in as " + name); this.channel.logger.log(this.ip + " logged in as " + name);
if(this.rank >= Rank.Moderator)
this.channel.sendPlaylist(this);
this.channel.broadcastNewUser(this); this.channel.broadcastNewUser(this);
} }
} }