3
0
mirror of https://github.com/reality/dbot.git synced 2024-11-24 04:49:25 +01:00

* ~ban and ~unban now fully databankerised and tested [#331]

* Refactored ~ignore and ~unignore
* Refactored API
* Command uses new ban/ignore APIs, removed its own isBanned
* Ignore is now aware of servers [#188]
This commit is contained in:
reality 2013-04-11 20:12:29 +00:00
parent 9c38316483
commit 223b20b598
4 changed files with 140 additions and 123 deletions

View File

@ -2,18 +2,6 @@ var _ = require('underscore')._;
var api = function(dbot) { var api = function(dbot) {
return { return {
'isBanned': function(user, command) {
var banned = false;
if(_.has(dbot.db.bans, user)) {
if(_.include(dbot.db.bans[user], command) ||
_.include(dbot.db.bans[user], dbot.commands[command].module) ||
_.include(dbot.db.bans[user], '*')) {
banned = true;
}
}
return banned;
},
/** /**
* Does the user have the correct access level to use the command? * Does the user have the correct access level to use the command?
*/ */

View File

@ -21,11 +21,13 @@ var command = function(dbot) {
} }
} }
if(this.api.isBanned(event.user, commandName)) {
event.reply(dbot.t('command_ban', {'user': event.user}));
} else {
dbot.api.ignore.isUserIgnoring(event.server, event.user, commandName, function(isIgnoring) { dbot.api.ignore.isUserIgnoring(event.server, event.user, commandName, function(isIgnoring) {
if(!isIgnoring && this.api.hasAccess(event.user, commandName) && dbot.commands[commandName].disabled !== true) { dbot.api.ignore.isUserBanned(event.server, event.user, commandName, function(isBanned) {
if(isBanned) {
event.reply(dbot.t('command_ban', {'user': event.user}));
} else if(!isIgnoring &&
this.api.hasAccess(event.user, commandName) &&
dbot.commands[commandName].disabled !== true) {
if(this.api.applyRegex(commandName, event)) { if(this.api.applyRegex(commandName, event)) {
try { try {
var command = dbot.commands[commandName]; var command = dbot.commands[commandName];
@ -54,7 +56,7 @@ var command = function(dbot) {
} }
} }
}.bind(this)); }.bind(this));
} }.bind(this));
}.bind(this); }.bind(this);
this.on = 'PRIVMSG'; this.on = 'PRIVMSG';
}; };

View File

@ -5,21 +5,28 @@ var _ = require('underscore')._,
NotImplementedError = databank.NotImplementedError; NotImplementedError = databank.NotImplementedError;
var api = function(dbot) { var api = function(dbot) {
return { return {
// Is user ignoring command/module?
'isUserIgnoring': function(server, user, item, callback) { 'isUserIgnoring': function(server, user, item, callback) {
this.internalAPI.isUserImpeded(server, user, item, 'ignores', callback);
},
// Is user banned from command/module?
'isUserBanned': function(server, user, item, callback) {
this.internalAPI.isUserImpeded(server, user, item, 'bans', callback);
},
// Resolve a nick and return their user and ignores object
'getUserIgnores': function(server, user, callback) {
dbot.api.users.resolveUser(server, user, function(user) { dbot.api.users.resolveUser(server, user, function(user) {
if(user) {
this.db.read('ignores', user.id, function(err, ignores) { this.db.read('ignores', user.id, function(err, ignores) {
var isIgnoring = false; callback(false, user, ignores);
if(ignores) {
if(_.has(dbot.commands, item)) {
item = moduleName = dbot.commands[item].module;
}
if(_.include(ignores.ignores, item)) {
isIgnoring = true;
}
}
callback(isIgnoring);
}); });
} else {
callback(true, null, null);
}
}.bind(this)); }.bind(this));
} }
}; };

View File

@ -10,6 +10,23 @@ var _ = require('underscore')._,
NoSuchThingError = databank.NoSuchThingError; NoSuchThingError = databank.NoSuchThingError;
var ignore = function(dbot) { var ignore = function(dbot) {
this.internalAPI = {
'isUserImpeded': function(server, user, item, by, callback) {
this.api.getUserIgnores(server, user, function(err, user, ignores) {
var isImpeded = false;
if(!err && ignores) {
if(_.has(dbot.commands, item)) {
item = dbot.commands[item].module;
}
if(_.include(ignores[by], item)) {
isImpeded = true;
}
}
callback(isImpeded);
});
}.bind(this)
};
var commands = { var commands = {
'~ignore': function(event) { '~ignore': function(event) {
var module = event.params[1]; var module = event.params[1];
@ -19,6 +36,15 @@ var ignore = function(dbot) {
}) })
.pluck('name') .pluck('name')
.value(); .value();
var ignoreCreated = function(err, result) {
if(!err) {
dbot.instance.ignoreTag(event.user, module);
event.reply(dbot.t('ignored', {
'user': event.user,
'module': module
}));
}
};
if(_.isUndefined(module)) { if(_.isUndefined(module)) {
event.reply(dbot.t('ignore_usage', { event.reply(dbot.t('ignore_usage', {
@ -27,38 +53,23 @@ var ignore = function(dbot) {
})); }));
} else { } else {
if(module == '*' || _.include(ignorableModules, module)) { if(module == '*' || _.include(ignorableModules, module)) {
dbot.api.users.resolveUser(event.server, event.user, function(user) { this.api.getUserIgnores(event.server, event.user, function(err, user, ignores) {
this.db.read('ignores', user.id, function(err, ignores) { if(!err) {
if(ignores == null) { if(!ignores) {
this.db.create('ignores', user.id, { this.db.create('ignores', user.id, {
'id': user.id, 'id': user.id,
'ignores': [ module ] 'ignores': [ module ],
}, function(err, result) { 'bans': []
if(!err) { }, ignoreCreated);
dbot.instance.ignoreTag(event.user, module);
event.reply(dbot.t('ignored', {
'user': event.user,
'module': module
}));
}
});
} else { } else {
if(!_.include(ignores.ignores, module)) { if(!_.include(ignores.ignores, module)) {
ignores.ignores.push(module); ignores.ignores.push(module);
this.db.save('ignores', user.id, ignores, function(err) { this.db.save('ignores', user.id, ignores, ignoreCreated);
if(!err) {
dbot.instance.ignoreTag(event.user, module);
event.reply(dbot.t('ignored', {
'user': event.user,
'module': module
}));
}
});
} else { } else {
event.reply(dbot.t('already_ignoring', { 'user': event.user })); event.reply(dbot.t('already_ignoring', { 'user': event.user }));
} }
} }
}.bind(this)); }
}.bind(this)); }.bind(this));
} else { } else {
event.reply(dbot.t('invalid_ignore', { 'user': event.user })); event.reply(dbot.t('invalid_ignore', { 'user': event.user }));
@ -69,11 +80,8 @@ var ignore = function(dbot) {
'~unignore': function(event) { '~unignore': function(event) {
var module = event.params[1]; var module = event.params[1];
dbot.api.users.resolveUser(event.server, event.user, function(user) { this.api.getUserIgnores(event.server, event.user, function(err, user, ignores) {
this.db.read('ignores', user.id, function(err, ignores) { if(err || !ignores || _.isUndefined(module)) {
if(!ignores) {
event.reply(dbot.t('invalid_unignore', { 'user': event.user }));
} else if(_.isUndefined(module)) {
event.reply(dbot.t('unignore_usage', { event.reply(dbot.t('unignore_usage', {
'user': event.user, 'user': event.user,
'modules': ignores.ignores.join(', ') 'modules': ignores.ignores.join(', ')
@ -95,67 +103,76 @@ var ignore = function(dbot) {
} }
} }
}.bind(this)); }.bind(this));
}.bind(this));
}, },
'~ban': function(event) { '~ban': function(event) {
var user = event.params[1]; var user = event.input[1],
var module = event.params[2]; item = event.input[2];
var banCreated = function(err, result) {
if(_.isUndefined(user) || _.isUndefined(module)) { if(!err) {
event.reply(dbot.t('ban_usage', {'user': event.user})); event.reply(dbot.t('banned_success', {
return; 'user': event.user,
'banned': user,
'module': item
}));
} }
};
if(module == '*' || _.include(dbot.config.moduleNames, module) || _.include(dbot.commands, module)) { if(module == '*' || _.include(dbot.config.moduleNames, item) || _.include(dbot.commands, item)) {
if(_.has(dbot.db.bans, user) && _.include(dbot.db.bans[user], module)) { this.api.getUserIgnores(event.server, user, function(err, user, ignores) {
if(!err) {
if(!ignores) {
this.db.create('ignores', user.id, {
'id': user.id,
'ignores': [],
'bans': [ item ]
}, banCreated);
} else {
if(!_.include(ignores.bans, item)) {
ignores.bans.push(item);
this.db.save('ignores', user.id, ignores, banCreated);
} else {
event.reply(dbot.t('already_banned', { event.reply(dbot.t('already_banned', {
'user': event.user, 'user': event.user,
'banned': user 'banned': user
})); }));
return;
} }
if(_.has(dbot.db.bans, event.params[1])) {
dbot.db.bans[event.params[1]].push(module);
} else {
dbot.db.bans[event.params[1]] = [module];
} }
}
event.reply(dbot.t('banned_success', { }.bind(this));
'user': event.user,
'banned': user,
'module': module
}));
} else { } else {
event.reply(dbot.t('invalid_ban', { 'user': event.user })); event.reply(dbot.t('invalid_ban', { 'user': event.user }));
} }
}, },
'~unban': function(event) { '~unban': function(event) {
var bannedModules = []; var nick = event.input[1];
var item = event.input[2];
var user = event.params[1]; this.api.getUserIgnores(event.server, nick, function(err, user, ignores) {
var module = event.params[2]; if(err || !ignores) {
event.reply(dbot.t('invalid_unban', {
if(_.isUndefined(user) || _.isUndefined(module)) { 'user': event.user,
event.reply(dbot.t('unban_usage', {'user': event.user})); 'banned': nick
}));
} else { } else {
if(_.has(dbot.db.bans, user) && _.include(dbot.db.bans[user], module)) { if(_.include(ignores.bans, item)) {
dbot.db.bans[user].splice(dbot.db.bans[user].indexOf(module), 1); ignores.bans = _.without(ignores.bans, item);
this.db.save('ignores', user.id, ignores, function(err) {
event.reply(dbot.t('unbanned_success', { event.reply(dbot.t('unbanned_success', {
'user': event.user, 'user': event.user,
'banned': user, 'banned': nick,
'module': module 'module': item
})); }));
});
} else { } else {
event.reply(dbot.t('invalid_unban', { event.reply(dbot.t('invalid_unban', {
'user': event.user, 'user': event.user,
'banned': user 'banned': nick
})); }));
} }
} }
}.bind(this));
}, },
'~ignorechannel': function(event) { '~ignorechannel': function(event) {
@ -204,6 +221,9 @@ var ignore = function(dbot) {
} }
}; };
commands['~ban'].regex = [/^~ban ([^ ]+) ([^ ]+)$/, 3];
commands['~unban'].regex = [/^~unban ([^ ]+) ([^ ]+)$/, 3];
commands['~ban'].access = 'moderator'; commands['~ban'].access = 'moderator';
commands['~unban'].access = 'moderator'; commands['~unban'].access = 'moderator';
commands['~ignorechannel'].access = 'moderator'; commands['~ignorechannel'].access = 'moderator';