]> git.example.dev Git - binbsis50.git/commitdiff
added ability for privileged users to unban players
authorLuigi Pinca <luigipinca@gmail.com>
Sat, 31 May 2014 15:28:30 +0000 (17:28 +0200)
committerLuigi Pinca <luigipinca@gmail.com>
Sat, 31 May 2014 15:28:30 +0000 (17:28 +0200)
lib/rooms.js
lib/sparks.js
lib/utils.js
public/js/app.js

index 846900fca9d5a02f85c49c2367c0288b505bb0b6..49c998dbca5ebf546bd8613585d203ab03fa5c19 100644 (file)
@@ -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);
index a78229d66a4aeb46a0016156a865a8beb948105d..263b4a85a3281d1d1f679849b643b331072752f3 100644 (file)
@@ -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);
index 40eaefb91256ccd208282f99c75b76f41718d182..dba428920e968eea3d214842472c0176acb280f3 100644 (file)
@@ -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);
+        });
+      });
+    });
+  });
+};
index 298818b4c740197bfaee64caa8b17d77432c0b44..7383f03ef739ed9a645c182c3c181e6c60650a44 100644 (file)
     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]]) {
       checkrecipient: true,
       fn: punishPlayer('ban'),
       minargs: 1,
-      usage: 'usage: /ban &lt;player name&gt; [message] [duration]'
+      usage: 'usage: /ban &lt;player&gt; [&lt;message&gt;] [&lt;duration&gt;]'
     },
     clear: {
       fn: function() {
       minargs: 0
     },
     ignore: {
-      checkrecipient: true, // Assume that the first argument (argv[0]) is the recipient
+      checkrecipient: true,
       fn: ignorePlayer,
       minargs: 1,
-      usage: 'usage: /ignore &lt;player name&gt;'
+      usage: 'usage: /ignore &lt;player&gt;'
     },
     kick: {
       checkrecipient: true,
       fn: punishPlayer('kick'),
       minargs: 1,
-      usage: 'usage: /kick &lt;player name&gt; [message]'
+      usage: 'usage: /kick &lt;player&gt; [&lt;message&gt;]'
+    },
+    unban: {
+      fn: unbanPlayer,
+      minargs: 1,
+      usage: 'usage: /unban &lt;IP&gt;|:list'
     },
     unignore: {
       checkrecipient: true,
       fn: unignorePlayer,
       minargs: 1,
-      usage: 'usage: /unignore &lt;player name&gt;'
+      usage: 'usage: /unignore &lt;player&gt;'
     }
   };