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) { 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)) { dbot.api.ignore.isUserIgnoring(event.server, event.user, commandName, function(isIgnoring) {
event.reply(dbot.t('command_ban', {'user': event.user})); dbot.api.ignore.isUserBanned(event.server, event.user, commandName, function(isBanned) {
} else { if(isBanned) {
dbot.api.ignore.isUserIgnoring(event.server, event.user, commandName, function(isIgnoring) { event.reply(dbot.t('command_ban', {'user': event.user}));
if(!isIgnoring && this.api.hasAccess(event.user, commandName) && dbot.commands[commandName].disabled !== true) { } 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];
@ -51,10 +53,10 @@ var command = function(dbot) {
event.reply(dbot.t('syntax_error')); event.reply(dbot.t('syntax_error'));
} }
} }
} }
} }
}.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) {
this.db.read('ignores', user.id, function(err, ignores) { if(user) {
var isIgnoring = false; this.db.read('ignores', user.id, function(err, ignores) {
if(ignores) { callback(false, user, ignores);
if(_.has(dbot.commands, item)) { });
item = moduleName = dbot.commands[item].module; } else {
} callback(true, null, null);
if(_.include(ignores.ignores, item)) { }
isIgnoring = true;
}
}
callback(isIgnoring);
});
}.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,93 +80,99 @@ 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('unignore_usage', {
event.reply(dbot.t('invalid_unignore', { 'user': event.user })); 'user': event.user,
} else if(_.isUndefined(module)) { 'modules': ignores.ignores.join(', ')
event.reply(dbot.t('unignore_usage', { }));
'user': event.user, } else {
'modules': ignores.ignores.join(', ') 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 { } else {
if(_.include(ignores.ignores, module)) { event.reply(dbot.t('invalid_unignore', { 'user': event.user }));
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));
}.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(_.has(dbot.db.bans, user) && _.include(dbot.db.bans[user], module)) {
event.reply(dbot.t('already_banned', {
'user': event.user,
'banned': user
})); }));
return;
} }
};
if(_.has(dbot.db.bans, event.params[1])) { if(module == '*' || _.include(dbot.config.moduleNames, item) || _.include(dbot.commands, item)) {
dbot.db.bans[event.params[1]].push(module); this.api.getUserIgnores(event.server, user, function(err, user, ignores) {
} else { if(!err) {
dbot.db.bans[event.params[1]] = [module]; if(!ignores) {
} this.db.create('ignores', user.id, {
'id': user.id,
event.reply(dbot.t('banned_success', { 'ignores': [],
'user': event.user, 'bans': [ item ]
'banned': user, }, banCreated);
'module': module } 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 { } 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) {
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 {
event.reply(dbot.t('invalid_unban', { event.reply(dbot.t('invalid_unban', {
'user': event.user, '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) { '~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';