From a371ff629d20c7f61cdfe8958dd02f2dfa73fe40 Mon Sep 17 00:00:00 2001 From: calzoneman Date: Tue, 6 Aug 2013 14:20:47 -0400 Subject: [PATCH] Allow channel admins to read channel logs --- channel.js | 60 ++++++++++++++++++++++++++++++++ user.js | 6 ++++ www/assets/js/callbacks.js | 13 +++++++ www/assets/js/channelsettings.js | 4 +++ www/assets/js/util.js | 1 + www/channeloptions.html | 4 +++ 6 files changed, 88 insertions(+) diff --git a/channel.js b/channel.js index 90f886f6..b3c8ecef 100644 --- a/channel.js +++ b/channel.js @@ -278,6 +278,66 @@ function incrementalDump(chan) { } } +Channel.prototype.readLog = function (filterIp, callback) { + var maxLen = 100000; // Most recent 100KB + var file = this.logger.filename; + fs.stat(file, function (err, data) { + if(err) { + callback(err, null); + return; + } + + var start = data.size - maxLen; + if(start < 0) { + start = 0; + } + var end = data.size - 1; + + var rs = fs.createReadStream(file, { + start: start, + end: end + }); + + var buffer = ""; + rs.on("data", function (data) { + buffer += data; + }); + + rs.on("end", function () { + if(filterIp) { + buffer = buffer.replace( + /(\d{1,3}\.){2}(\d{1,3})\.(\d{1,3})/g, + "x.x.$2.$3" + ); + } + + callback(false, buffer); + }); + }); +} + +Channel.prototype.tryReadLog = function (user) { + if(user.rank < 3) + return; + + var filterIp = true; + if(user.global_rank >= 255) + filterIp = false; + + this.readLog(filterIp, function (err, data) { + if(err) { + user.socket.emit("readChanLog", { + success: false + }); + } else { + user.socket.emit("readChanLog", { + success: true, + data: data + }); + } + }); +} + Channel.prototype.tryRegister = function(user) { if(this.registered) { ActionLog.record(user.ip, user.name, "channel-register-failure", [ diff --git a/user.js b/user.js index 47d2c225..f9924a64 100644 --- a/user.js +++ b/user.js @@ -521,6 +521,12 @@ User.prototype.initCallbacks = function() { }); }.bind(this)); + this.socket.on("readChanLog", function () { + if(this.channel !== null) { + this.channel.tryReadLog(this); + } + }.bind(this)); + this.socket.on("acp-init", function() { if(this.global_rank >= Rank.Siteadmin) this.server.acp.init(this); diff --git a/www/assets/js/callbacks.js b/www/assets/js/callbacks.js index 803a4e07..7c2945e3 100644 --- a/www/assets/js/callbacks.js +++ b/www/assets/js/callbacks.js @@ -497,6 +497,19 @@ Callbacks = { ); }, + readChanLog: function (data) { + var log = $("#chanlog_contents"); + if(log.length == 0) + return; + + if(data.success) { + log.text(data.data); + } else { + log.text("Error reading channel log"); + } + log.scrollTop(log.prop("scrollHeight")); + }, + voteskip: function(data) { if(data.count > 0) { $("#voteskip").text("Voteskip ("+data.count+"/"+data.need+")"); diff --git a/www/assets/js/channelsettings.js b/www/assets/js/channelsettings.js index 714b91be..90e37069 100644 --- a/www/assets/js/channelsettings.js +++ b/www/assets/js/channelsettings.js @@ -45,6 +45,10 @@ $("#show_channelranks").click(function() { socket.emit("requestChannelRanks"); }); + clickHandler("#show_chanlog", "#chanlog"); + $("#show_chanlog").click(function () { + socket.emit("readChanLog"); + }); genPermissionsEditor(); diff --git a/www/assets/js/util.js b/www/assets/js/util.js index 9b1179ba..2f56d101 100644 --- a/www/assets/js/util.js +++ b/www/assets/js/util.js @@ -812,6 +812,7 @@ function handleModPermissions() { setVisible("#jsedit_tab", CLIENT.rank >= 3); setVisible("#filteredit_tab", hasPermission("filteredit")); setVisible("#channelranks_tab", CLIENT.rank >= 3); + setVisible("#chanlog_tab", CLIENT.rank >= 3); setVisible("#chanopts_unregister_wrap", CLIENT.rank >= 10); } diff --git a/www/channeloptions.html b/www/channeloptions.html index 4af0e432..5af062d8 100644 --- a/www/channeloptions.html +++ b/www/channeloptions.html @@ -15,6 +15,7 @@
  • Ban List
  • Channel Ranks
  • Recent Connections
  • +
  • Channel Log

  • @@ -223,3 +224,6 @@ +
    +
    
    +