3
0
mirror of https://github.com/reality/dbot.git synced 2024-11-23 20:39: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) {
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?
*/

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) {
if(!isIgnoring && this.api.hasAccess(event.user, commandName) && dbot.commands[commandName].disabled !== true) {
dbot.api.ignore.isUserIgnoring(event.server, event.user, commandName, function(isIgnoring) {
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)) {
try {
var command = dbot.commands[commandName];
@ -51,10 +53,10 @@ var command = function(dbot) {
event.reply(dbot.t('syntax_error'));
}
}
}
}
}
}.bind(this));
}
}.bind(this));
}.bind(this);
this.on = 'PRIVMSG';
};

View File

@ -5,21 +5,28 @@ var _ = require('underscore')._,
NotImplementedError = databank.NotImplementedError;
var api = function(dbot) {
return {
// Is user ignoring command/module?
'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) {
this.db.read('ignores', user.id, function(err, ignores) {
var isIgnoring = false;
if(ignores) {
if(_.has(dbot.commands, item)) {
item = moduleName = dbot.commands[item].module;
}
if(_.include(ignores.ignores, item)) {
isIgnoring = true;
}
}
callback(isIgnoring);
});
if(user) {
this.db.read('ignores', user.id, function(err, ignores) {
callback(false, user, ignores);
});
} else {
callback(true, null, null);
}
}.bind(this));
}
};

View File

@ -10,6 +10,23 @@ var _ = require('underscore')._,
NoSuchThingError = databank.NoSuchThingError;
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 = {
'~ignore': function(event) {
var module = event.params[1];
@ -19,6 +36,15 @@ var ignore = function(dbot) {
})
.pluck('name')
.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)) {
event.reply(dbot.t('ignore_usage', {
@ -27,38 +53,23 @@ var ignore = function(dbot) {
}));
} else {
if(module == '*' || _.include(ignorableModules, module)) {
dbot.api.users.resolveUser(event.server, event.user, function(user) {
this.db.read('ignores', user.id, function(err, ignores) {
if(ignores == null) {
this.api.getUserIgnores(event.server, event.user, function(err, user, ignores) {
if(!err) {
if(!ignores) {
this.db.create('ignores', user.id, {
'id': user.id,
'ignores': [ module ]
}, function(err, result) {
if(!err) {
dbot.instance.ignoreTag(event.user, module);
event.reply(dbot.t('ignored', {
'user': event.user,
'module': module
}));
}
});
'ignores': [ module ],
'bans': []
}, ignoreCreated);
} else {
if(!_.include(ignores.ignores, module)) {
ignores.ignores.push(module);
this.db.save('ignores', user.id, ignores, function(err) {
if(!err) {
dbot.instance.ignoreTag(event.user, module);
event.reply(dbot.t('ignored', {
'user': event.user,
'module': module
}));
}
});
this.db.save('ignores', user.id, ignores, ignoreCreated);
} else {
event.reply(dbot.t('already_ignoring', { 'user': event.user }));
}
}
}.bind(this));
}
}.bind(this));
} else {
event.reply(dbot.t('invalid_ignore', { 'user': event.user }));
@ -69,93 +80,99 @@ var ignore = function(dbot) {
'~unignore': function(event) {
var module = event.params[1];
dbot.api.users.resolveUser(event.server, event.user, function(user) {
this.db.read('ignores', user.id, function(err, ignores) {
if(!ignores) {
event.reply(dbot.t('invalid_unignore', { 'user': event.user }));
} else if(_.isUndefined(module)) {
event.reply(dbot.t('unignore_usage', {
'user': event.user,
'modules': ignores.ignores.join(', ')
}));
this.api.getUserIgnores(event.server, event.user, function(err, user, ignores) {
if(err || !ignores || _.isUndefined(module)) {
event.reply(dbot.t('unignore_usage', {
'user': event.user,
'modules': ignores.ignores.join(', ')
}));
} else {
if(_.include(ignores.ignores, module)) {
ignores.ignores = _.without(ignores.ignores, module);
this.db.save('ignores', user.id, ignores, function(err) {
if(!err) {
dbot.instance.removeIgnore(event.user, module)
event.reply(dbot.t('unignored', {
'user': event.user,
'module': module
}));
}
});
} else {
if(_.include(ignores.ignores, module)) {
ignores.ignores = _.without(ignores.ignores, module);
this.db.save('ignores', user.id, ignores, function(err) {
if(!err) {
dbot.instance.removeIgnore(event.user, module)
event.reply(dbot.t('unignored', {
'user': event.user,
'module': module
}));
}
});
} else {
event.reply(dbot.t('invalid_unignore', { 'user': event.user }));
}
}
}.bind(this));
event.reply(dbot.t('invalid_unignore', { 'user': event.user }));
}
}
}.bind(this));
},
'~ban': function(event) {
var user = event.params[1];
var module = event.params[2];
if(_.isUndefined(user) || _.isUndefined(module)) {
event.reply(dbot.t('ban_usage', {'user': event.user}));
return;
}
if(module == '*' || _.include(dbot.config.moduleNames, module) || _.include(dbot.commands, module)) {
if(_.has(dbot.db.bans, user) && _.include(dbot.db.bans[user], module)) {
event.reply(dbot.t('already_banned', {
'user': event.user,
'banned': user
var user = event.input[1],
item = event.input[2];
var banCreated = function(err, result) {
if(!err) {
event.reply(dbot.t('banned_success', {
'user': event.user,
'banned': user,
'module': item
}));
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', {
'user': event.user,
'banned': user,
'module': module
}));
if(module == '*' || _.include(dbot.config.moduleNames, item) || _.include(dbot.commands, item)) {
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', {
'user': event.user,
'banned': user
}));
}
}
}
}.bind(this));
} else {
event.reply(dbot.t('invalid_ban', {'user': event.user}));
event.reply(dbot.t('invalid_ban', { 'user': event.user }));
}
},
'~unban': function(event) {
var bannedModules = [];
var nick = event.input[1];
var item = event.input[2];
var user = event.params[1];
var module = event.params[2];
if(_.isUndefined(user) || _.isUndefined(module)) {
event.reply(dbot.t('unban_usage', {'user': event.user}));
} else {
if(_.has(dbot.db.bans, user) && _.include(dbot.db.bans[user], module)) {
dbot.db.bans[user].splice(dbot.db.bans[user].indexOf(module), 1);
event.reply(dbot.t('unbanned_success', {
'user': event.user,
'banned': user,
'module': module
}));
} else {
this.api.getUserIgnores(event.server, nick, function(err, user, ignores) {
if(err || !ignores) {
event.reply(dbot.t('invalid_unban', {
'user': event.user,
'banned': user
'banned': nick
}));
} else {
if(_.include(ignores.bans, item)) {
ignores.bans = _.without(ignores.bans, item);
this.db.save('ignores', user.id, ignores, function(err) {
event.reply(dbot.t('unbanned_success', {
'user': event.user,
'banned': nick,
'module': item
}));
});
} else {
event.reply(dbot.t('invalid_unban', {
'user': event.user,
'banned': nick
}));
}
}
}
}.bind(this));
},
'~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['~unban'].access = 'moderator';
commands['~ignorechannel'].access = 'moderator';