diff --git a/acp.js b/acp.js index 5d7b81fa..0abdce94 100644 --- a/acp.js +++ b/acp.js @@ -7,7 +7,7 @@ module.exports = { init: function(user) { ActionLog.record(user.ip, user.name, "acp-init"); user.socket.on("acp-announce", function(data) { - ActionLog.record(user.ip, user.name, ["acp-announce", data]); + ActionLog.record(user.ip, user.name, "acp-announce", [data]); Server.announcement = data; Server.io.sockets.emit("announcement", data); }); @@ -18,13 +18,13 @@ module.exports = { }); user.socket.on("acp-global-ban", function(data) { - ActionLog.record(user.ip, user.name, ["acp-global-ban", data.ip]); + ActionLog.record(user.ip, user.name, "acp-global-ban", [data.ip]); Database.globalBanIP(data.ip, data.note); user.socket.emit("acp-global-banlist", Database.refreshGlobalBans()); }); user.socket.on("acp-global-unban", function(ip) { - ActionLog.record(user.ip, user.name, ["acp-global-unban", data.ip]); + ActionLog.record(user.ip, user.name, "acp-global-unban", [data.ip]); Database.globalUnbanIP(ip); user.socket.emit("acp-global-banlist", Database.refreshGlobalBans()); }); @@ -55,7 +55,7 @@ module.exports = { return; try { var hash = Database.generatePasswordReset(user.ip, data.name, data.email); - ActionLog.record(user.ip, user.name, ["acp-reset-password", data.name]); + ActionLog.record(user.ip, user.name, "acp-reset-password", [data.name]); } catch(e) { user.socket.emit("acp-reset-password", { @@ -90,7 +90,7 @@ module.exports = { if(!db) return; - ActionLog.record(user.ip, user.name, ["acp-set-rank", data]); + ActionLog.record(user.ip, user.name, "acp-set-rank", [data]); var query = Database.createQuery( "UPDATE registrations SET global_rank=? WHERE uname=?", [data.name, data.rank] @@ -139,7 +139,7 @@ module.exports = { user.socket.on("acp-actionlog-clear", function(data) { ActionLog.clear(data); - ActionLog.record(user.ip, user.name, "acp-actionlog-clear"); + ActionLog.record(user.ip, user.name, "acp-actionlog-clear", data); }); } } diff --git a/actionlog.js b/actionlog.js index 9f4701da..c2aec0c9 100644 --- a/actionlog.js +++ b/actionlog.js @@ -1,12 +1,14 @@ var fs = require("fs"); +var Logger = require("./logger"); var buffer = []; -exports.record = function(ip, name, action) { +exports.record = function(ip, name, action, args) { buffer.push(JSON.stringify({ ip: ip, name: name, action: action, + args: args ? args : [], time: Date.now() })); } @@ -24,11 +26,40 @@ exports.flush = function() { }); } -exports.clear = function() { - try { - fs.renameSync("action.log", "action-until-"+Date.now()+".log"); +exports.clear = function(actions) { + clearInterval(FLUSH_TMR); + var rs = fs.createReadStream("action.log"); + var ws = fs.createWriteStream("action.log.tmp"); + function handleLine(ln) { + try { + js = JSON.parse(ln); + if(actions.indexOf(js.action) == -1) + ws.write(ln + "\n"); + } + catch(e) { } } - catch(e) { } + var buffer = ""; + rs.on("data", function(chunk) { + buffer += chunk; + if(buffer.indexOf("\n") != -1) { + var lines = buffer.split("\n"); + buffer = lines[lines.length - 1]; + lines.length = lines.length - 1; + lines.forEach(handleLine); + } + }); + rs.on("end", function() { + handleLine(buffer); + ws.end(); + }); + try { + fs.renameSync("action.log.tmp", "action.log"); + } + catch(e) { + Logger.errlog.log("Failed to move action.log.tmp => action.log"); + Logger.errlog.log(e); + } + FLUSH_TMR = setInterval(exports.flush, 15000); } -setInterval(exports.flush, 15000); +var FLUSH_TMR = setInterval(exports.flush, 15000); diff --git a/api.js b/api.js index 20049d12..67da6a5b 100644 --- a/api.js +++ b/api.js @@ -429,7 +429,7 @@ function handleEmailChange(params, req, res) { var row = Auth.login(name, pw); if(row) { var success = Database.setUserEmail(name, email); - ActionLog.record(getClientIP(req), name, ["email-update", email]); + ActionLog.record(getClientIP(req), name, "email-update", [email]); sendJSON(res, { success: success, error: success ? "" : "Email update failed", diff --git a/channel.js b/channel.js index 214a1b0a..555fcdaf 100644 --- a/channel.js +++ b/channel.js @@ -269,14 +269,14 @@ function incrementalDump(chan) { Channel.prototype.tryRegister = function(user) { if(this.registered) { - ActionLog.record(user.ip, user.name, ["channel-register-failure", this.name]); + ActionLog.record(user.ip, user.name, "channel-register-failure", [this.name]); user.socket.emit("registerChannel", { success: false, error: "This channel is already registered" }); } else if(!user.loggedIn) { - ActionLog.record(user.ip, user.name, ["channel-register-failure", this.name]); + ActionLog.record(user.ip, user.name, "channel-register-failure", [this.name]); user.socket.emit("registerChannel", { success: false, error: "You must log in to register a channel" @@ -284,7 +284,7 @@ Channel.prototype.tryRegister = function(user) { } else if(!Rank.hasPermission(user, "registerChannel")) { - ActionLog.record(user.ip, user.name, ["channel-register-failure", this.name]); + ActionLog.record(user.ip, user.name, "channel-register-failure", [this.name]); user.socket.emit("registerChannel", { success: false, error: "You don't have permission to register this channel" @@ -292,7 +292,7 @@ Channel.prototype.tryRegister = function(user) { } else { if(Database.registerChannel(this.name)) { - ActionLog.record(user.ip, user.name, ["channel-register-success", this.name]); + ActionLog.record(user.ip, user.name, "channel-register-success", [this.name]); this.registered = true; this.initialized = true; this.saveDump(); diff --git a/www/acp.html b/www/acp.html index e4d949f1..8b5b36e7 100644 --- a/www/acp.html +++ b/www/acp.html @@ -46,6 +46,8 @@
IP Address | -Name | -Action | -Time | +IP Address | +Name | +Action | +Args | +Time |
---|