From: Luigi Pinca Date: Sat, 31 May 2014 15:28:30 +0000 (+0200) Subject: added ability for privileged users to unban players X-Git-Url: https://git.saalbach.dev/?a=commitdiff_plain;h=203069c88641874e01efdb2d2dc370e6fe519de0;p=binbsis50.git added ability for privileged users to unban players --- diff --git a/lib/rooms.js b/lib/rooms.js index 846900f..49c998d 100644 --- a/lib/rooms.js +++ b/lib/rooms.js @@ -300,7 +300,7 @@ Room.prototype.onGuess = function(spark, guess) { } // The user has guessed both track and artist - return spark.send('stoptrying'); + spark.send('stoptrying'); }; /** @@ -335,7 +335,7 @@ Room.prototype.onKick = function(who, why, executor, duration, callback) { } // Check if the sender can kick other players - if ((role || 0) < 1) { + if (role < 1) { return callback(false); } @@ -346,11 +346,7 @@ Room.prototype.onKick = function(who, why, executor, duration, callback) { var target = sparks[who]; if (duration) { - usersdb.setex(['ban:' + target.address.ip, duration, who], function(err) { - if (err) { - console.error(err.message); - } - }); + usersdb.setex('ban:' + target.address.ip, duration, who); } target.send('chatmsg', notice, 'binb', who); diff --git a/lib/sparks.js b/lib/sparks.js index a78229d..263b4a8 100644 --- a/lib/sparks.js +++ b/lib/sparks.js @@ -3,7 +3,7 @@ */ var config = require('../config') - , db = require('../lib/redis-clients').users + , db = require('./redis-clients').users , forwarded = require('forwarded-for') , fs = require('fs') , minify = require('uglify-js').minify @@ -183,6 +183,11 @@ var joinRoom = function(room, spark) { room.onKick(who, why, spark.nickname, callback); } }); + spark.on('unban', function(ip, callback) { + if (isString(ip) && isFunction(callback)) { + utils.unban(ip, spark, callback); + } + }); spark.on('unignore', function(who) { if (isString(who)) { room.onUnignore(who, spark.nickname); diff --git a/lib/utils.js b/lib/utils.js index 40eaefb..dba4289 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -1,3 +1,9 @@ +/** + * Module dependencies. + */ + +var db = require('./redis-clients').users; + /** * Convert the duration of a ban from minutes to seconds and return the value. * Default duration is 15 minutes. @@ -115,3 +121,52 @@ exports.sortParams = function(offset) { ]; return params; }; + +/** + * Handle `unban` command. + */ + +exports.unban = function(ip, spark, callback) { + var issuedby = spark.nickname; + + db.hget(['user:' + issuedby, 'role'], function(err, role) { + if (err) { + console.error(err.message); + // Fail silently in case of error + return callback(true); + } + + if (role < 1) { + return callback(false); + } + + // At this point consider the command successfully executed + callback(true); + + if (ip !== ':list') { + return db.del('ban:' + ip); + } + + // List all banned players + db.keys(['ban:*'], function(err, replies) { + if (err) { + return console.error(err.message); + } + + if (!replies.length) { + spark.send('chatmsg', 'the ban list is empty.', 'binb', issuedby); + return; + } + + replies.forEach(function(key) { + var bannedip = key.slice(4); + db.get([key], function(err, reply) { + if (err) { + return console.error(err.message); + } + spark.send('chatmsg', bannedip + ' → ' + reply, 'binb', issuedby); + }); + }); + }); + }); +}; diff --git a/public/js/app.js b/public/js/app.js index 298818b..7383f03 100644 --- a/public/js/app.js +++ b/public/js/app.js @@ -854,6 +854,20 @@ addChatEntry(outcome); }; + // Unban a player + var unbanPlayer = function(args, outcome) { + outcome.append('you are not allowed to unban a player.'); + if (!subscriber) { + return addChatEntry(outcome); + } + + primus.send('unban', args[0], function(success) { + if (!success) { + addChatEntry(outcome); + } + }); + }; + // Remove a player from the ignore list var unignorePlayer = function(args, outcome) { if (!ignoredplayers[args[0]]) { @@ -987,7 +1001,7 @@ checkrecipient: true, fn: punishPlayer('ban'), minargs: 1, - usage: 'usage: /ban <player name> [message] [duration]' + usage: 'usage: /ban <player> [<message>] [<duration>]' }, clear: { fn: function() { @@ -996,22 +1010,27 @@ minargs: 0 }, ignore: { - checkrecipient: true, // Assume that the first argument (argv[0]) is the recipient + checkrecipient: true, fn: ignorePlayer, minargs: 1, - usage: 'usage: /ignore <player name>' + usage: 'usage: /ignore <player>' }, kick: { checkrecipient: true, fn: punishPlayer('kick'), minargs: 1, - usage: 'usage: /kick <player name> [message]' + usage: 'usage: /kick <player> [<message>]' + }, + unban: { + fn: unbanPlayer, + minargs: 1, + usage: 'usage: /unban <IP>|:list' }, unignore: { checkrecipient: true, fn: unignorePlayer, minargs: 1, - usage: 'usage: /unignore <player name>' + usage: 'usage: /unignore <player>' } };