From fab4039fc1af7d0f34fe4ac66a83112aee43ccd9 Mon Sep 17 00:00:00 2001 From: calzoneman Date: Mon, 1 Apr 2013 16:02:09 -0500 Subject: [PATCH] Add MOTD and chat filter interface --- channel.js | 51 +++++++++++++++++++++------------- rank.js | 1 + server.js | 3 +- user.js | 26 ++++++++++++++---- www/assets/css/ytsync.css | 4 +++ www/assets/js/callbacks.js | 13 +++++++++ www/assets/js/client.js | 35 ++++++++++++++++++++++-- www/assets/js/functions.js | 56 ++++++++++++++++++++++++++++++++++++++ www/index.html | 33 ++++++++++++++++++++-- 9 files changed, 192 insertions(+), 30 deletions(-) diff --git a/channel.js b/channel.js index 8d0cabb9..eac5cead 100644 --- a/channel.js +++ b/channel.js @@ -48,6 +48,10 @@ var Channel = function(name) { [/(^| )_([^_]+)_/g , "$1$2" , true], [/\\\\([-a-zA-Z0-9]+)/g, "[](/$1)" , true] ]; + this.motd = { + motd: "", + html: "" + }; this.ipbans = {}; this.logger = new Logger.Logger("chanlogs/" + this.name + ".log"); @@ -76,6 +80,9 @@ var Channel = function(name) { data.filters[i][2]]; } } + if(data.motd) { + this.motd = data.motd; + } } catch(e) { Logger.errlog.log("Channel dump load failed: "); @@ -435,6 +442,7 @@ Channel.prototype.userJoin = function(user) { user.socket.emit("newPoll", this.poll.packUpdate()); } user.socket.emit("channelOpts", this.opts); + user.socket.emit("updateMotd", this.motd); if(user.playerReady) this.sendMediaUpdate(user); this.logger.log("+++ /" + user.ip + " joined"); @@ -797,10 +805,7 @@ Channel.prototype.chatMessage = function(user, msg) { this.sendMessage(user.name, msg, ""); } -Channel.prototype.sendMessage = function(username, msg, msgclass) { - // I don"t want HTML from strangers - msg = msg.replace(//g, ">"); - // Match URLs +Channel.prototype.filterMessage = function(msg) { msg = msg.replace(/(((https?)|(ftp))(:\/\/[0-9a-zA-Z\.]+(:[0-9]+)?[^\s$]+))/g, "$1"); // Apply other filters for(var i = 0; i < this.filters.length; i++) { @@ -810,12 +815,13 @@ Channel.prototype.sendMessage = function(username, msg, msgclass) { var replace = this.filters[i][1]; msg = msg.replace(regex, replace); } - // Chat modifier - monospace - //msg = msg.replace(/`([^`]+)`/g, "$1"); - // Bold - //msg = msg.replace(/\*\*([^\*]+)\*\*/g, "$1"); - // Italic - //msg = msg.replace(/\*([^\*]+)\*/g, "$1"); + return msg; +} + +Channel.prototype.sendMessage = function(username, msg, msgclass) { + // I don"t want HTML from strangers + msg = msg.replace(//g, ">"); + msg = this.filterMessage(msg); this.sendAll("chatMsg", { username: username, msg: msg, @@ -961,20 +967,13 @@ Channel.prototype.broadcastNewUser = function(user) { rank: user.rank, leader: this.leader == user }); + this.handleRankChange(user); } -// Someone"s rank changed, or their leadership status changed -Channel.prototype.broadcastRankUpdate = function(user) { +Channel.prototype.handleRankChange = function(user) { user.socket.emit("rank", { rank: user.rank }); - this.sendAll("updateUser", { - name: user.name, - rank: user.rank, - leader: this.leader == user - }); - - // Rank specific stuff if(Rank.hasPermission(user, "ipban")) { var ents = []; for(var ip in this.ipbans) { @@ -997,6 +996,16 @@ Channel.prototype.broadcastRankUpdate = function(user) { } } +// Someone"s rank changed, or their leadership status changed +Channel.prototype.broadcastRankUpdate = function(user) { + this.sendAll("updateUser", { + name: user.name, + rank: user.rank, + leader: this.leader == user + }); + this.handleRankChange(user); +} + Channel.prototype.broadcastPoll = function() { this.sendAll("newPoll", this.poll.packUpdate()); } @@ -1043,6 +1052,10 @@ Channel.prototype.broadcastChatFilters = function() { } } +Channel.prototype.broadcastMotd = function() { + this.sendAll("updateMotd", this.motd); +} + // Send to ALL the clients! Channel.prototype.sendAll = function(message, data) { io.sockets.in(this.name).emit(message, data); diff --git a/rank.js b/rank.js index 2598596e..51e9c460 100644 --- a/rank.js +++ b/rank.js @@ -30,6 +30,7 @@ var permissions = { channelOpts : exports.Moderator, jump : exports.Moderator, chatFilter : exports.Moderator, + updateMotd : exports.Moderator, search : exports.Guest, chat : exports.Guest, }; diff --git a/server.js b/server.js index 0f4937ff..41f54511 100644 --- a/server.js +++ b/server.js @@ -66,7 +66,8 @@ function shutdown() { currentPosition: chan.currentPosition, queue: chan.queue, opts: chan.opts, - filters: filts + filters: filts, + motd: chan.motd }; var text = JSON.stringify(dump); fs.writeFileSync("chandump/" + name, text); diff --git a/user.js b/user.js index 7cdef2d4..4d13fee6 100644 --- a/user.js +++ b/user.js @@ -250,12 +250,28 @@ User.prototype.initCallbacks = function() { }.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); + if(Rank.hasPermission(this, "chatFilter")) { + 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]); + } } - else if(data.cmd && data.cmd == "remove" && this.channel != null) { - this.channel.removeFilter(data.filter[0]); + }.bind(this)); + + this.socket.on("updateMotd", function(data) { + if(Rank.hasPermission(this, "updateMotd")) { + if(data.motd != undefined && this.channel != null) { + var html = data.motd.replace(/\n/g, "
"); + html = this.channel.filterMessage(html); + this.channel.motd = { + motd: data.motd, + html: html + }; + this.channel.broadcastMotd(); + } } }.bind(this)); } diff --git a/www/assets/css/ytsync.css b/www/assets/css/ytsync.css index 0c9d38c8..e19f8cba 100644 --- a/www/assets/css/ytsync.css +++ b/www/assets/css/ytsync.css @@ -124,3 +124,7 @@ .nick-highlight { background-color: #ddffdd; } + +#motdtext { + width: 100%; +} diff --git a/www/assets/js/callbacks.js b/www/assets/js/callbacks.js index 82069d65..2ad1edf6 100644 --- a/www/assets/js/callbacks.js +++ b/www/assets/js/callbacks.js @@ -26,6 +26,19 @@ function initCallbacks() { showAnnouncement(data.title, data.text); }); + socket.on("updateMotd", function(data) { + $("#motdtext").val(data.motd); + if(data.motd != "") + $("#motd").parent().css("display", ""); + else + $("#motd").parent().css("display", "none"); + $("#motd")[0].innerHTML = data.html; + }); + + socket.on("chatFilters", function(data) { + updateChatFilters(data.filters); + }); + socket.on("registerChannel", function(data) { if(data.success) { $("#chregnotice").remove(); diff --git a/www/assets/js/client.js b/www/assets/js/client.js index 18f721be..f67a345a 100644 --- a/www/assets/js/client.js +++ b/www/assets/js/client.js @@ -278,18 +278,47 @@ $("#opt_submit").click(function() { socket.emit("channelOpts", opts); }); +$("#updatemotd").click(function() { + var motd = $("#motdtext").val(); + socket.emit("updateMotd", { + motd: motd + }); +}); + $("#show_chancontrols").click(function() { - $("#show_chancontrols").parent().addClass("active"); $("#show_banlist").parent().removeClass("active"); - $("#banlist").hide(); + $("#show_motdeditor").parent().removeClass("active"); + $("#show_filtereditor").parent().removeClass("active"); + $("#show_chancontrols").parent().addClass("active"); + $(".modonly").hide(); $("#chancontrols").show(); }); $("#show_banlist").click(function() { $("#show_chancontrols").parent().removeClass("active"); + $("#show_motdeditor").parent().removeClass("active"); + $("#show_filtereditor").parent().removeClass("active"); $("#show_banlist").parent().addClass("active"); + $(".modonly").hide(); $("#banlist").show(); - $("#chancontrols").hide(); +}); + +$("#show_motdeditor").click(function() { + $("#show_chancontrols").parent().removeClass("active"); + $("#show_banlist").parent().removeClass("active"); + $("#show_filtereditor").parent().removeClass("active"); + $("#show_motdeditor").parent().addClass("active"); + $(".modonly").hide(); + $("#motdeditor").show(); +}); + +$("#show_filtereditor").click(function() { + $("#show_chancontrols").parent().removeClass("active"); + $("#show_banlist").parent().removeClass("active"); + $("#show_motdeditor").parent().removeClass("active"); + $("#show_filtereditor").parent().addClass("active"); + $(".modonly").hide(); + $("#filtereditor").show(); }); function searchLibrary() { diff --git a/www/assets/js/functions.js b/www/assets/js/functions.js index eb419281..e71b890b 100644 --- a/www/assets/js/functions.js +++ b/www/assets/js/functions.js @@ -675,6 +675,62 @@ function updateBanlist(entries) { } } +function updateChatFilters(entries) { + var tbl = $("#filtereditor table"); + if(tbl.children().length > 1) { + $(tbl.children()[1]).remove(); + } + for(var i = 0; i < entries.length; i++) { + var tr = $("").appendTo(tbl); + var remove = $(" + + +