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

Merge pull request #130 from reality/underscore

underscore merged into master [#81]
This commit is contained in:
Luke Slater 2013-01-13 08:20:12 -08:00
commit 459e7290a3
15 changed files with 523 additions and 529 deletions

View File

@ -3,16 +3,17 @@
* Description: Set of commands which only one who is a DepressionBot * Description: Set of commands which only one who is a DepressionBot
* administrator can run - as such, it has its own command execution listener. * administrator can run - as such, it has its own command execution listener.
*/ */
var fs = require('fs'); var fs = require('fs'),
var sys = require('sys') _ = require('underscore')._,
var exec = require('child_process').exec; sys = require('sys'),
exec = require('child_process').exec;
var admin = function(dbot) { var admin = function(dbot) {
var commands = { var commands = {
// Join a channel // Join a channel
'join': function(event) { 'join': function(event) {
var channel = event.params[1]; var channel = event.params[1];
if(event.allChannels.hasOwnProperty(channel)) { if(_.has(event.allChannels, channel)) {
event.reply(dbot.t('already_in_channel', {'channel': channel})); event.reply(dbot.t('already_in_channel', {'channel': channel}));
} else { } else {
dbot.instance.join(event, channel); dbot.instance.join(event, channel);
@ -23,7 +24,7 @@ var admin = function(dbot) {
// Leave a channel // Leave a channel
'part': function(event) { 'part': function(event) {
var channel = event.params[1]; var channel = event.params[1];
if(!event.allChannels.hasOwnProperty(channel)) { if(!_.has(event.allChannels, channel)) {
event.reply(dbot.t('not_in_channel', {'channel': channel})); event.reply(dbot.t('not_in_channel', {'channel': channel}));
} else { } else {
event.instance.part(event, channel); event.instance.part(event, channel);
@ -36,7 +37,7 @@ var admin = function(dbot) {
var channel = event.params[1]; var channel = event.params[1];
// If given channel isn't valid just op in current one. // If given channel isn't valid just op in current one.
if(!event.allChannels.hasOwnProperty(channel)) { if(!_.has(event.allChannels, channel)) {
channel = event.channel.name; channel = event.channel.name;
} }
dbot.instance.mode(event, channel, ' +o ' + event.user); dbot.instance.mode(event, channel, ' +o ' + event.user);
@ -61,7 +62,7 @@ var admin = function(dbot) {
var cmd = "git log --pretty=format:'%h (%s): %ar' -n 1 -- "; var cmd = "git log --pretty=format:'%h (%s): %ar' -n 1 -- ";
if(event.params[1]){ if(event.params[1]){
var input = event.params[1].trim(); var input = event.params[1].trim();
if(dbot.modules.hasOwnProperty(input.split("/")[0])){ if(_.has(dbot.modules, input.split("/")[0])){
cmd += "modules/"+input; cmd += "modules/"+input;
} }
else{ else{
@ -99,7 +100,7 @@ var admin = function(dbot) {
// Load new module // Load new module
'load': function(event) { 'load': function(event) {
var moduleName = event.params[1]; var moduleName = event.params[1];
if(!dbot.config.moduleNames.include(moduleName)) { if(!_.include(dbot.config.moduleNames, moduleName)) {
dbot.config.moduleNames.push(moduleName); dbot.config.moduleNames.push(moduleName);
dbot.reloadModules(); dbot.reloadModules();
event.reply(dbot.t('load_module', {'moduleName': moduleName})); event.reply(dbot.t('load_module', {'moduleName': moduleName}));
@ -116,13 +117,11 @@ var admin = function(dbot) {
'unload': function(event) { 'unload': function(event) {
var moduleNames = dbot.config.moduleNames; var moduleNames = dbot.config.moduleNames;
var moduleName = event.params[1]; var moduleName = event.params[1];
if(moduleNames.include(moduleName)) { if(_.include(moduleNames, moduleName)) {
var moduleDir = '../' + moduleName + '/'; var moduleDir = '../' + moduleName + '/';
var cacheKey = require.resolve(moduleDir + moduleName); var cacheKey = require.resolve(moduleDir + moduleName);
delete require.cache[cacheKey]; delete require.cache[cacheKey];
dbot.config.moduleNames = _.without(dbot.config.moduleNames, moduleName);
var moduleIndex = moduleNames.indexOf(moduleName);
moduleNames.splice(moduleIndex, 1);
dbot.reloadModules(); dbot.reloadModules();
event.reply(dbot.t('unload_module', {'moduleName': moduleName})); event.reply(dbot.t('unload_module', {'moduleName': moduleName}));
@ -136,7 +135,7 @@ var admin = function(dbot) {
var username = event.params[1]; var username = event.params[1];
var command = event.params[2]; var command = event.params[2];
if(!dbot.db.bans.hasOwnProperty(command)) { if(!_.has(dbot.db.bans, command)) {
dbot.db.bans[command] = [ ]; dbot.db.bans[command] = [ ];
} }
dbot.db.bans[command].push(username); dbot.db.bans[command].push(username);
@ -147,8 +146,10 @@ var admin = function(dbot) {
'unban': function(event) { 'unban': function(event) {
var username = event.params[1]; var username = event.params[1];
var command = event.params[2]; var command = event.params[2];
if(dbot.db.bans.hasOwnProperty(command) && dbot.db.bans[command].include(username)) { if(_.has(dbot.db.bans, command) && _.include(dbot.db.bans[command], username)) {
dbot.db.bans[command].splice(dbot.db.bans[command].indexOf(username), 1); _.reject(dbot.db.bans[command], function(bans) {
return bans == username;
}, this);
event.reply(dbot.t('unbanned', {'user': username, 'command': command})); event.reply(dbot.t('unbanned', {'user': username, 'command': command}));
} else { } else {
event.reply(dbot.t('unban_error', {'user': username})); event.reply(dbot.t('unban_error', {'user': username}));

View File

@ -4,14 +4,15 @@
* command and then runs that command, given the user isn't banned from or * command and then runs that command, given the user isn't banned from or
* ignoring that command. * ignoring that command.
*/ */
var _ = require('underscore')._;
var command = function(dbot) { var command = function(dbot) {
/** /**
* Is user banned from using command? * Is user banned from using command?
*/ */
var isBanned = function(user, command) { var isBanned = function(user, command) {
var banned = false; var banned = false;
if(dbot.db.bans.hasOwnProperty(command)) { if(_.has(dbot.db.bans, command)) {
if(dbot.db.bans[command].include(user) || dbot.db.bans['*'].include(user)) { if(_.include(dbot.db.bans[command], user) || _.include(dbot.db.bans['*'], user)) {
banned = true; banned = true;
} }
} }
@ -26,12 +27,12 @@ var command = function(dbot) {
var accessNeeded = dbot.commands[command].access; var accessNeeded = dbot.commands[command].access;
if(accessNeeded == 'admin') { if(accessNeeded == 'admin') {
if(!dbot.config.admins.include(user)) { if(!_.include(dbot.config.admins, user)) {
access = false; access = false;
} }
} else if(accessNeeded == 'moderator') { } else if(accessNeeded == 'moderator') {
if(!dbot.config.moderators.include(user) && if(!_.include(dbot.config.moderators, user) &&
!dbot.config.admins.include(user)) { !_.include(dbot.config.admins, user)) {
access = false; access = false;
} }
} }
@ -43,9 +44,9 @@ var command = function(dbot) {
* Is user ignoring command? * Is user ignoring command?
*/ */
var isIgnoring = function(user, command) { var isIgnoring = function(user, command) {
var module = dbot.commandMap[command]; var module = dbot.commands[command].module;
var ignoring = false; var ignoring = false;
if(dbot.db.ignores.hasOwnProperty(user) && dbot.db.ignores[user].include(module)) { if(_.has(dbot.db.ignores, user) && _.include(dbot.db.ignores[user], module)) {
ignoring = true; ignoring = true;
} }
return ignoring; return ignoring;
@ -57,7 +58,7 @@ var command = function(dbot) {
*/ */
var applyRegex = function(commandName, event) { var applyRegex = function(commandName, event) {
var applies = false; var applies = false;
if(dbot.commands[commandName].hasOwnProperty('regex')) { if(_.has(dbot.commands[commandName], 'regex')) {
var cRegex = dbot.commands[commandName].regex; var cRegex = dbot.commands[commandName].regex;
var q = event.message.valMatch(cRegex[0], cRegex[1]); var q = event.message.valMatch(cRegex[0], cRegex[1]);
if(q) { if(q) {
@ -77,7 +78,7 @@ var command = function(dbot) {
'commands': { 'commands': {
'~usage': function(event) { '~usage': function(event) {
var commandName = event.params[1]; var commandName = event.params[1];
if(dbot.usage.hasOwnProperty(commandName)) { if(_.has(dbot.usage, commandName)) {
event.reply(dbot.t('usage', { event.reply(dbot.t('usage', {
'command': commandName, 'command': commandName,
'usage': dbot.usage[commandName] 'usage': dbot.usage[commandName]
@ -91,11 +92,11 @@ var command = function(dbot) {
'~help': function(event) { '~help': function(event) {
var moduleName = event.params[1]; var moduleName = event.params[1];
if(!dbot.modules.hasOwnProperty(moduleName)) { if(!_.has(dbot.modules, moduleName)) {
var moduleName = dbot.commandMap[moduleName]; var moduleName = dbot.commands[moduleName].module;
} }
if(moduleName && dbot.config[moduleName].hasOwnProperty('help')) { if(moduleName && _.has(dbot.config[moduleName], 'help')) {
var help = dbot.config[moduleName].help; var help = dbot.config[moduleName].help;
event.reply(dbot.t('help_link', { event.reply(dbot.t('help_link', {
'module': moduleName, 'module': moduleName,
@ -115,7 +116,7 @@ var command = function(dbot) {
*/ */
'listener': function(event) { 'listener': function(event) {
var commandName = event.params[0]; var commandName = event.params[0];
if(!dbot.commands.hasOwnProperty(commandName)) { if(!_.has(dbot.commands, commandName)) {
commandName = '~'; commandName = '~';
} }
@ -138,7 +139,7 @@ var command = function(dbot) {
dbot.save(); dbot.save();
} else { } else {
if(commandName !== '~') { if(commandName !== '~') {
if(dbot.usage.hasOwnProperty(commandName)){ if(_.has(dbot.usage, commandName)) {
event.reply('Usage: ' + dbot.usage[commandName]); event.reply('Usage: ' + dbot.usage[commandName]);
} else { } else {
event.reply(dbot.t('syntax_error')); event.reply(dbot.t('syntax_error'));

View File

@ -16,7 +16,6 @@ var dent = function(dbot) {
}, },
function(error, response, body) { function(error, response, body) {
event.reply('Status posted (probably).'); event.reply('Status posted (probably).');
console.log(body);
}); });
} }
}; };

View File

@ -4,54 +4,69 @@
* and commands from certain modules. It also populates the JSBot instance with * and commands from certain modules. It also populates the JSBot instance with
* this information, since that actually performs the ignorance. * this information, since that actually performs the ignorance.
*/ */
var _ = require('underscore')._;
var ignore = function(dbot) { var ignore = function(dbot) {
var commands = { var commands = {
'~ignore': function(event) { '~ignore': function(event) {
var ignorableModules = dbot.modules.filter(function(module) {
if(module.ignorable != null && module.ignorable == true) {
return true;
}
});
var module = event.params[1]; var module = event.params[1];
var ignorableModules = _.chain(dbot.modules)
.filter(function(module) {
return module.ignorable !== null && module.ignorable === true;
})
.pluck('name')
.value();
if(module === undefined) { if(_.isUndefined(module)) {
event.reply(dbot.t('ignore_usage', {'user': event.user, 'modules': ignorableModules.join(', ')})); event.reply(dbot.t('ignore_usage', {
'user': event.user,
'modules': ignorableModules.join(', ')
}));
} else { } else {
if(ignorableModules.include(module)) { if(_.include(ignorableModules, module)) {
if(dbot.db.ignores.hasOwnProperty(event.user) && dbot.db.ignores[event.user].include(module)) { if(_.has(dbot.db.ignores, event.user) && _.include(dbot.db.ignores[event.user], module)) {
event.reply(dbot.t('already_ignoring', {'user': event.user})); event.reply(dbot.t('already_ignoring', { 'user': event.user }));
} else { } else {
if(dbot.db.ignores.hasOwnProperty(module)) { if(_.has(dbot.db.ignores, module)) {
dbot.db.ignores[event.user].push(module); dbot.db.ignores[event.user].push(module);
} else { } else {
dbot.db.ignores[event.user] = [module]; dbot.db.ignores[event.user] = [module];
} }
dbot.instance.ignoreTag(event.user, module); dbot.instance.ignoreTag(event.user, module);
event.reply(dbot.t('ignored', {'user': event.user, 'module': module})); event.reply(dbot.t('ignored', {
'user': event.user,
'module': module
}));
} }
} else { } else {
event.reply(dbot.t('invalid_ignore', {'user': event.user})); event.reply(dbot.t('invalid_ignore', { 'user': event.user }));
} }
} }
}, },
'~unignore': function(event) { '~unignore': function(event) {
var ignoredModules = []; var ignoredModules = [];
if(dbot.db.ignores.hasOwnProperty(event.user)) { if(_.has(dbot.db.ignores, event.user)) {
ignoredModules = dbot.db.ignores[event.user]; ignoredModules = dbot.db.ignores[event.user];
} }
var module = event.params[1]; var module = event.params[1];
if(module === undefined) { if(_.isUndefined(module)) {
event.reply(dbot.t('unignore_usage', {'user': event.user, 'modules': ignoredModules.join(', ')})); event.reply(dbot.t('unignore_usage', {
'user': event.user,
'modules': ignoredModules.join(', ')
}));
} else { } else {
if(ignoredModules.include(module) == false) { if(_.include(ignoredModules, module)) {
event.reply(dbot.t('invalid_unignore', {'user': event.user}));
} else {
dbot.db.ignores[event.user].splice(dbot.db.ignores[event.user].indexOf(module), 1); dbot.db.ignores[event.user].splice(dbot.db.ignores[event.user].indexOf(module), 1);
dbot.instance.removeIgnore(event.user, module) dbot.instance.removeIgnore(event.user, module)
event.reply(dbot.t('unignored', {'user': event.user, 'module': module})); event.reply(dbot.t('unignored', {
'user': event.user,
'module': module
}));
} else {
event.reply(dbot.t('invalid_unignore', { 'user': event.user }));
} }
} }
} }
@ -64,13 +79,11 @@ var ignore = function(dbot) {
'onLoad': function() { 'onLoad': function() {
dbot.instance.clearIgnores(); dbot.instance.clearIgnores();
for(var user in dbot.db.ignores) { _.each(dbot.db.ignores, function(ignores, user) {
if(dbot.db.ignores.hasOwnProperty(user)) { _.each(ignores, function(ignore) {
for(var i=0;i<dbot.db.ignores[user].length;i++) { dbot.instance.ignoreTag(user, ignore);
dbot.instance.ignoreTag(user, dbot.db.ignores[user][i]); }, this);
} }, this);
}
}
} }
}; };
}; };

View File

@ -1,3 +1,5 @@
var _ = require('underscore')._;
var kick = function(dbot) { var kick = function(dbot) {
var commands = { var commands = {
// Give the number of times a given user has been kicked and has kicked // Give the number of times a given user has been kicked and has kicked
@ -5,27 +7,35 @@ var kick = function(dbot) {
'~kickcount': function(event) { '~kickcount': function(event) {
var username = event.params[1]; var username = event.params[1];
if(!dbot.db.kicks.hasOwnProperty(username)) { if(!_.has(dbot.db.kicks, username)) {
var kicks = '0'; var kicks = '0';
} else { } else {
var kicks = dbot.db.kicks[username]; var kicks = dbot.db.kicks[username];
} }
if(!dbot.db.kickers.hasOwnProperty(username)) { if(!_.has(dbot.db.kickers, username)) {
var kicked = '0'; var kicked = '0';
} else { } else {
var kicked = dbot.db.kickers[username]; var kicked = dbot.db.kickers[username];
} }
event.reply(dbot.t('user_kicks', {'user': username, 'kicks': kicks, 'kicked': kicked})); event.reply(dbot.t('user_kicks', {
'user': username,
'kicks': kicks,
'kicked': kicked
}));
}, },
// Output a list of the people who have been kicked the most and those // Output a list of the people who have been kicked the most and those
// who have kicked other people the most. // who have kicked other people the most.
'~kickstats': function(event) { '~kickstats': function(event) {
var orderedKickLeague = function(list, topWhat) { var orderedKickLeague = function(list, topWhat) {
var kickArr = Object.prototype.sort(list, function(key, obj) { return obj[key]; }); var kickArr = _.chain(list)
kickArr = kickArr.slice(kickArr.length - 10).reverse(); .pairs()
.sortBy(function(kick) { return kick[1] })
.reverse()
.first(10)
.value();
var kickString = "Top " + topWhat + ": "; var kickString = "Top " + topWhat + ": ";
for(var i=0;i<kickArr.length;i++) { for(var i=0;i<kickArr.length;i++) {
@ -48,23 +58,26 @@ var kick = function(dbot) {
'listener': function(event) { 'listener': function(event) {
if(event.kickee == dbot.config.name) { if(event.kickee == dbot.config.name) {
dbot.instance.join(event, event.channel); dbot.instance.join(event, event.channel);
event.reply(dbot.t('kicked_dbot', {'botname': dbot.config.name})); event.reply(dbot.t('kicked_dbot', { 'botname': dbot.config.name }));
dbot.db.kicks[dbot.config.name] += 1; dbot.db.kicks[dbot.config.name] += 1;
} else { } else {
if(!dbot.db.kicks.hasOwnProperty(event.kickee)) { if(!_.has(dbot.db.kicks, event.kickee)) {
dbot.db.kicks[event.kickee] = 1; dbot.db.kicks[event.kickee] = 1;
} else { } else {
dbot.db.kicks[event.kickee] += 1; dbot.db.kicks[event.kickee] += 1;
} }
if(!dbot.db.kickers.hasOwnProperty(event.user)) { if(!_.has(dbot.db.kickers, event.user)) {
dbot.db.kickers[event.user] = 1; dbot.db.kickers[event.user] = 1;
} else { } else {
dbot.db.kickers[event.user] += 1; dbot.db.kickers[event.user] += 1;
} }
event.reply(event.kickee + '-- (' + dbot.t('user_kicks', event.reply(event.kickee + '-- (' + dbot.t('user_kicks', {
{'user': event.kickee, 'kicks': dbot.db.kicks[event.kickee], 'kicked': dbot.db.kickers[event.kickee]}) + ')'); 'user': event.kickee,
'kicks': dbot.db.kicks[event.kickee],
'kicked': dbot.db.kickers[event.kickee]
}) + ')');
} }
}, },
on: 'KICK' on: 'KICK'

View File

@ -3,7 +3,9 @@
* Description: Stores recent channel links, with commands to retrieve * Description: Stores recent channel links, with commands to retrieve
* information about links. * information about links.
*/ */
var request = require('request'); var request = require('request'),
_ = require('underscore')._;
var link = function(dbot) { var link = function(dbot) {
var urlRegex = /(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/ig; var urlRegex = /(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/ig;
var links = {}; var links = {};
@ -22,7 +24,7 @@ var link = function(dbot) {
var commands = { var commands = {
'~title': function(event) { '~title': function(event) {
var link = links[event.channel.name]; var link = links[event.channel.name];
if(event.params[1] !== undefined) { if(_.isUndefined(event.params[1])) {
var urlMatches = event.params[1].match(urlRegex); var urlMatches = event.params[1].match(urlRegex);
if(urlMatches !== null) { if(urlMatches !== null) {
link = urlMatches[0]; link = urlMatches[0];

View File

@ -1,3 +1,4 @@
{ {
"help": "http://github.com/reality/depressionbot/blob/master/modules/poll/README.md" "help": "http://github.com/reality/depressionbot/blob/master/modules/poll/README.md",
"dbKeys": [ "polls" ]
} }

View File

@ -1,68 +1,63 @@
var _ = require('underscore')._;
var poll = function(dbot) { var poll = function(dbot) {
var polls = dbot.db.polls; var polls = dbot.db.polls;
var commands = { var commands = {
'~newpoll': function(event) { '~newpoll': function(event) {
var av = event.input[1] != undefined; var name = event.input[1],
var name = event.input[2]; options = event.input[2].split(','),
var options = event.input[3].split(','); description = event.input[3];
var description = event.input[4];
if(name === undefined || name === 'help') { if(_.has(polls, name)) {
event.reply(dbot.t('newpoll_usage')); event.reply(dbot.t('poll_exists', { 'name': name }));
} else { } else {
if(polls.hasOwnProperty(name)) { polls[name] = {
event.reply(dbot.t('poll_exists', {'name': name})); 'name': name,
} else { 'description': description,
if(av) { 'owner': event.user,
polls[name] = { 'votes': {},
'av': av, 'votees': {}
'name': name, };
'description': description, for(var i=0;i<options.length;i++) {
'owner': event.user, polls[name]['votes'][options[i]] = 0;
'votes': {},
'options': []
};
for(var i=0;i<options.length;i++) {
polls[name].options.push(options[i]);
}
} else {
polls[name] = {
'av': av,
'name': name,
'description': description,
'owner': event.user,
'votes': {},
'votees': {}
};
for(var i=0;i<options.length;i++) {
polls[name]['votes'][options[i]] = 0;
}
}
event.reply(dbot.t('poll_created', {'name': name, 'description': description,
'url': dbot.t('url', {'host': dbot.config.web.webHost,
'port': dbot.config.web.webPort, 'path': 'polls/' + name})}));
} }
event.reply(dbot.t('poll_created', {
'name': name,
'description': description,
'url': dbot.t('url', {
'host': dbot.config.web.webHost,
'port': dbot.config.web.webPort,
'path': 'polls/' + name
})
}));
} }
}, },
'~addoption': function(event) { '~addoption': function(event) {
var name = event.input[1]; var name = event.input[1],
var option = event.input[2]; option = event.input[2];
if(polls.hasOwnProperty(name)) { if(_.has(polls, name)) {
if(polls[name].owner === event.user) { if(polls[name].owner === event.user) {
if(!polls[name].votes.hasOwnProperty(name)) { if(!_.has(polls[name].votes, name)) {
polls[name]['votes'][option] = 0; polls[name]['votes'][option] = 0;
event.reply(dbot.t('option_added', {'user': event.user, event.reply(dbot.t('option_added', {
'name': name, 'option': option})); 'user': event.user,
'name': name,
'option': option
}));
} else { } else {
event.reply(dbot.t('option_exists', {'option': option, event.reply(dbot.t('option_exists', {
'name': name, 'user': event.user})); 'option': option,
'name': name,
'user': event.user
}));
} }
} else { } else {
event.reply(dbot.t('not_poll_owner', {'user': event.user, event.reply(dbot.t('not_poll_owner', {
'name': name})); 'user': event.user,
'name': name
}));
} }
} else { } else {
event.reply(dbot.t('poll_unexistent', {'name': name})); event.reply(dbot.t('poll_unexistent', {'name': name}));
@ -70,147 +65,114 @@ var poll = function(dbot) {
}, },
'~rmoption': function(event) { '~rmoption': function(event) {
var name = event.input[1]; var name = event.input[1],
var option = event.input[2]; option = event.input[2];
if(polls.hasOwnProperty(name)) { if(_.has(polls, name)) {
if(polls[name].owner === event.user) { if(polls[name].owner === event.user) {
if(polls[name].votes.hasOwnProperty(option)) { if(_.has(polls[name].votes, option)) {
delete polls[name]['votes'][option]; delete polls[name]['votes'][option];
event.reply(dbot.t('option_removed', {'user': event.user, event.reply(dbot.t('option_removed', {
'name': name, 'option': option})); 'user': event.user,
'name': name,
'option': option
}));
} else { } else {
event.reply(dbot.t('invalid_vote', {'vote': option})); event.reply(dbot.t('invalid_vote', { 'vote': option }));
} }
} else { } else {
event.reply(dbot.t('not_poll_owner', {'name': name})); event.reply(dbot.t('not_poll_owner', { 'name': name }));
} }
} else { } else {
event.reply(dbot.t('poll_unexistent', {'name': name})); event.reply(dbot.t('poll_unexistent', { 'name': name }));
} }
}, },
'~vote': function(event) { '~vote': function(event) {
var name = event.input[1]; var name = event.input[1],
var vote = event.input[2]; vote = event.input[2];
if(polls.hasOwnProperty(name)) { if(_.has(polls, name)) {
if(polls[name].av) { if(_.has(polls[name].votes, vote)) {
var prefs = vote.split(','); if(_.has(polls[name].votees, event.user)) {
prefs = prefs.uniq();
var valid = true;
prefs.each(function(pref) {
valid = valid && polls[name].options.indexOf(pref) != -1;
});
if(valid){
if(polls[name].votes.hasOwnProperty(event.user)) {
polls[name].votes[event.user] = prefs;
event.reply(dbot.t('av_changed_vote', {'vote': prefs.join(','), 'poll': name, 'user': event.user}));
} else {
polls[name].votes[event.user] = prefs;
event.reply(dbot.t('av_voted', {'vote': prefs.join(','), 'poll': name, 'user': event.user}));
}
} else {
event.reply(dbot.t('invalid_vote', {'vote': vote}));
}
} else {
if(polls[name].votes.hasOwnProperty(vote)) {
if(polls[name].votees.hasOwnProperty(event.user)) {
var oldVote = polls[name].votees[event.user]; var oldVote = polls[name].votees[event.user];
polls[name].votes[oldVote]--; polls[name].votes[oldVote]--;
polls[name].votes[vote]++; polls[name].votes[vote]++;
polls[name].votees[event.user] = vote; polls[name].votees[event.user] = vote;
event.reply(dbot.t('changed_vote', {'vote': vote, 'poll': name,
'count': polls[name].votes[vote], 'user': event.user})); event.reply(dbot.t('changed_vote', {
'vote': vote,
'poll': name,
'count': polls[name].votes[vote],
'user': event.user
}));
} else { } else {
polls[name].votes[vote]++; polls[name].votes[vote]++;
polls[name].votees[event.user] = vote; polls[name].votees[event.user] = vote;
event.reply(dbot.t('voted', {'vote': vote, 'poll': name, event.reply(dbot.t('voted', {
'count': polls[name].votes[vote], 'user': event.user})); 'vote': vote,
'poll': name,
'count': polls[name].votes[vote],
'user': event.user
}));
} }
} else { } else {
event.reply(dbot.t('invalid_vote', {'vote': vote})); event.reply(dbot.t('invalid_vote', { 'vote': vote }));
} }
}
} else { } else {
event.reply(dbot.t('poll_unexistent', {'name': name})); event.reply(dbot.t('poll_unexistent', { 'name': name }));
} }
}, },
'~pdesc': function(event) { '~pdesc': function(event) {
var name = event.input[1]; var name = event.input[1];
if(polls.hasOwnProperty(name)) {
event.reply(dbot.t('poll_describe', {'name': name, 'description': polls[name].description, if(_.has(polls, name)) {
'url': dbot.t('url', {'host': dbot.config.web.webHost, 'port': event.reply(dbot.t('poll_describe', {
dbot.config.web.webPort, 'path': 'polls/' + name})})); 'name': name,
'description': polls[name].description,
'url': dbot.t('url', {
'host': dbot.config.web.webHost,
'port': dbot.config.web.webPort,
'path': 'polls/' + name
})
}));
} else { } else {
event.reply(dbot.t('poll_unexistent', {'name': name})); event.reply(dbot.t('poll_unexistent', { 'name': name }));
} }
}, },
'~count': function(event) { '~count': function(event) {
var name = event.input[1]; var name = event.input[1];
if(polls.hasOwnProperty(name)) { if(_.has(polls, name)) {
var order; var order;
if(polls[name].av) { var votesArr = [];
var finished = false;
var rounds = [];
var eliminated = [];
var voted;
for(var roundn = 0; roundn < polls[name].options.length; roundn++) {
var roundLoser;
// Populate candidates for this round
rounds[roundn] = {};
polls[name].options.each(function (option) {
if(eliminated.indexOf(option) == -1)
rounds[roundn][option] = 0;
});
// Count votes
polls[name].votes.withAll(function (name, vote) {
voted = false;
vote.each(function (pref) {
if(!voted && rounds[roundn].hasOwnProperty(pref)) {
rounds[roundn][pref]++;
voted = true;
}
});
});
// Find the loser
var min = polls[name].votes.length() + 1;
rounds[roundn].withAll(function (option, count) {
if(count < min) {
roundLoser = option;
min = count;
}
});
// Eliminate loser
eliminated.push(roundLoser);
}
order = eliminated.reverse().join(', ')
} else {
var votesArr = [];
polls[name].votes.withAll(function(option, count) {
votesArr.push([option, count]);
});
votesArr = votesArr.sort(function(a, b) { return b[1] - a[1]; }); var order = _.chain(polls[name].votes)
.pairs()
order = votesArr.map(function(vote) { return vote[0]; }); .sortBy(function(option) { return option[1] })
.reverse()
.value();
var orderString = "";
for(var i=0;i<order.length;i++) {
orderString += order[i][0] +
" (" + order[i][1] + "), ";
} }
event.reply(dbot.t('count', {'poll': name, 'description': polls[name].description, 'places': order})); orderString = orderString.slice(0, -2);
event.reply(dbot.t('count', {
'poll': name,
'description': polls[name].description,
'places': orderString
}));
} else { } else {
event.reply(dbot.t('poll_unexistent', {'name': name})); event.reply(dbot.t('poll_unexistent', {'name': name}));
} }
} }
}; };
commands['~newpoll'].regex = [/~newpoll (av )?([^ ]+) options=([^ ]+) (.+)/, 5]; commands['~newpoll'].regex = [/~newpoll ([^ ]+) options=([^ ]+) (.+)/, 4];
commands['~addoption'].regex = [/~addoption ([^ ]+) ([^ ]+)/, 3]; commands['~addoption'].regex = [/~addoption ([^ ]+) ([^ ]+)/, 3];
commands['~rmoption'].regex = [/~rmoption ([^ ]+) ([^ ]+)/, 3]; commands['~rmoption'].regex = [/~rmoption ([^ ]+) ([^ ]+)/, 3];
commands['~vote'].regex = [/~vote ([^ ]+) ([^ ]+)/, 3]; commands['~vote'].regex = [/~vote ([^ ]+) ([^ ]+)/, 3];
@ -221,24 +183,35 @@ var poll = function(dbot) {
// Shows the results of a poll // Shows the results of a poll
'/polls/:key': function(req, res) { '/polls/:key': function(req, res) {
var key = req.params.key.toLowerCase(); var key = req.params.key.toLowerCase();
if(dbot.db.polls.hasOwnProperty(key) && dbot.db.polls[key].hasOwnProperty('description')) { if(_.has(dbot.db.polls, key)) {
// tally the votes var totalVotes = _.reduce(dbot.db.polls[key].votes,
var totalVotes = 0; function(memo, option) {
for( var v in dbot.db.polls[key].votes ) { return memo += option;
var N = Number(dbot.db.polls[key].votes[v]); }, 0);
if( !isNaN(N) ) { res.render('polls', {
totalVotes += N; 'name': dbot.config.name,
} 'description': dbot.db.polls[key].description,
} 'votees': Object.keys(dbot.db.polls[key].votees),
res.render('polls', { 'name': dbot.config.name, 'description': dbot.db.polls[key].description, 'votees': Object.keys(dbot.db.polls[key].votees), 'options': dbot.db.polls[key].votes, locals: { 'totalVotes': totalVotes, 'url_regex': RegExp.prototype.url_regex() } }); 'options': dbot.db.polls[key].votes,
locals: {
'totalVotes': totalVotes,
'url_regex': RegExp.prototype.url_regex()
}
});
} else { } else {
res.render('error', { 'name': dbot.config.name, 'message': 'No polls under that key.' }); res.render('error', {
'name': dbot.config.name,
'message': 'No polls under that key.'
});
} }
}, },
// Lists all of the polls // Lists all of the polls
'/polls': function(req, res) { '/polls': function(req, res) {
res.render('polllist', { 'name': dbot.config.name, 'polllist': Object.keys(dbot.db.polls) }); res.render('polllist', {
'name': dbot.config.name,
'polllist': Object.keys(dbot.db.polls)
});
}, },
}; };

View File

@ -1,22 +1,24 @@
var _ = require('underscore')._;
var quotes = function(dbot) { var quotes = function(dbot) {
var name = 'quotes'; var quotes = dbot.db.quoteArrs,
var quotes = dbot.db.quoteArrs; addStack = [],
var addStack = []; rmAllowed = true,
var rmAllowed = true; rmCache = dbot.sessionData.rmCache,
rmTimer;
dbot.sessionData.rmCache = []; dbot.sessionData.rmCache = [];
var rmCache = dbot.sessionData.rmCache;
var rmTimer;
// Retrieve a random quote from a given category, interpolating any quote // Retrieve a random quote from a given category, interpolating any quote
// references (~~QUOTE CATEGORY~~) within it // references (~~QUOTE CATEGORY~~) within it
var interpolatedQuote = function(event, key, quoteTree) { var interpolatedQuote = function(event, key, quoteTree) {
if(quoteTree !== undefined && quoteTree.indexOf(key) != -1) { if(!_.isUndefined(quoteTree) && quoteTree.indexOf(key) != -1) {
return ''; return '';
} else if(quoteTree === undefined) { } else if(_.isUndefined(quoteTree)) {
quoteTree = []; quoteTree = [];
} }
var quoteString = quotes[key].random(); var index = _.random(0, quotes[key].length - 1);
var quoteString = quotes[key][index];
// Parse quote interpolations // Parse quote interpolations
var quoteRefs = quoteString.match(/~~([\d\w\s-]*)~~/g); var quoteRefs = quoteString.match(/~~([\d\w\s-]*)~~/g);
@ -25,10 +27,10 @@ var quotes = function(dbot) {
while(quoteRefs && (thisRef = quoteRefs.shift()) !== undefined) { while(quoteRefs && (thisRef = quoteRefs.shift()) !== undefined) {
var cleanRef = dbot.cleanNick(thisRef.replace(/^~~/,'').replace(/~~$/,'').trim()); var cleanRef = dbot.cleanNick(thisRef.replace(/^~~/,'').replace(/~~$/,'').trim());
if(cleanRef === '-nicks-') { if(cleanRef === '-nicks-') {
var randomNick = Object.keys(event.channel.nicks).random(); var randomNick = _.keys(event.channel.nicks)[_.random(0, _.size(event.channel.nicks) -1)];
quoteString = quoteString.replace("~~" + cleanRef + "~~", randomNick); quoteString = quoteString.replace("~~" + cleanRef + "~~", randomNick);
quoteTree.pop(); quoteTree.pop();
} else if(quotes.hasOwnProperty(cleanRef)) { } else if(_.has(quotes, cleanRef)) {
quoteTree.push(key); quoteTree.push(key);
quoteString = quoteString.replace("~~" + cleanRef + "~~", quoteString = quoteString.replace("~~" + cleanRef + "~~",
interpolatedQuote(event, cleanRef, quoteTree.slice())); interpolatedQuote(event, cleanRef, quoteTree.slice()));
@ -45,17 +47,20 @@ var quotes = function(dbot) {
rmAllowed = true; rmAllowed = true;
}); });
rmCache.push({'key': key, 'quote': quote}); rmCache.push({
'key': key,
'quote': quote
});
dbot.timers.clearTimeout(rmTimer); dbot.timers.clearTimeout(rmTimer);
if(rmCache.length < dbot.config.quotes.rmLimit) { if(rmCache.length < dbot.config.quotes.rmLimit) {
rmTimer = dbot.timers.addOnceTimer(600000, function() { rmTimer = dbot.timers.addOnceTimer(600000, function() {
rmCache.length = 0; // lol what rmCache.length = 0; // lol what
}); });
} else { } else {
for(var i=0;i<dbot.config.admins.length;i++) { _.each(dbot.config.admins, function(admin) {
dbot.say(event.server, dbot.config.admins[i], dbot.say(event.server, admin, dbot.t('rm_cache_limit'));
dbot.t('rm_cache_limit')); });
}
} }
}; };
@ -68,9 +73,9 @@ var quotes = function(dbot) {
} }
if(key.charAt(0) !== '_') { // lol if(key.charAt(0) !== '_') { // lol
if(quotes.hasOwnProperty(key)) { if(_.has(quotes, key)) {
return interpolatedQuote(event, key); return interpolatedQuote(event, key);
} else if(quotes.hasOwnProperty(altKey)) { } else if(_.has(quotes, altKey)) {
return interpolatedQuote(event, altKey); return interpolatedQuote(event, altKey);
} else { } else {
return false; return false;
@ -106,7 +111,7 @@ var quotes = function(dbot) {
'~rmdeny': function(event) { '~rmdeny': function(event) {
var rmCacheCount = rmCache.length; var rmCacheCount = rmCache.length;
for(var i=0;i<rmCacheCount;i++) { for(var i=0;i<rmCacheCount;i++) {
if(!quotes.hasOwnProperty(rmCache[i].key)) { if(!_.has(quotes, rmCache[i].key)) {
quotes[rmCache[i].key] = []; quotes[rmCache[i].key] = [];
} }
quotes[rmCache[i].key].push(rmCache[i].quote); quotes[rmCache[i].key].push(rmCache[i].quote);
@ -131,12 +136,16 @@ var quotes = function(dbot) {
// Shows a list of the biggest categories // Shows a list of the biggest categories
'~qstats': function(event) { '~qstats': function(event) {
var qSizes = Object.prototype.sort(quotes, function(key, obj) { return obj[key].length }); var qSizes = _.chain(quotes)
qSizes = qSizes.slice(qSizes.length - 10).reverse(); .pairs()
.sortBy(function(category) { return category[1].length })
.reverse()
.first(10)
.value();
var qString = dbot.t('large_categories'); var qString = dbot.t('large_categories');
for(var i=0;i<qSizes.length;i++) { for(var i=0;i<qSizes.length;i++) {
qString += qSizes[i][0] + " (" + qSizes[i][1] + "), "; qString += qSizes[i][0] + " (" + qSizes[i][1].length + "), ";
} }
event.reply(qString.slice(0, -2)); event.reply(qString.slice(0, -2));
@ -146,19 +155,20 @@ var quotes = function(dbot) {
'~qsearch': function(event) { '~qsearch': function(event) {
var haystack = event.input[1].trim().toLowerCase(); var haystack = event.input[1].trim().toLowerCase();
var needle = event.input[2]; var needle = event.input[2];
if(quotes.hasOwnProperty(haystack)) { if(_.has(quotes, haystack)) {
var matches = []; var matches = _.filter(quotes[haystack], function(quote) {
quotes[haystack].each(function(quote) { return _.indexOf(quote, needle) != -1;
if(quote.indexOf(needle) != -1) { }, this);
matches.push(quote);
}
}.bind(this));
if(matches.length == 0) { if(matches.length == 0) {
event.reply(dbot.t('no_results')); event.reply(dbot.t('no_results'));
} else { } else {
event.reply(dbot.t('search_results', {'category': haystack, 'needle': needle, event.reply(dbot.t('search_results', {
'quote': matches.random(), 'matches': matches.length})); 'category': haystack,
'needle': needle,
'quote': matches.random(),
'matches': matches.length
}));
} }
} else { } else {
event.reply(dbot.t('empty_category')); event.reply(dbot.t('empty_category'));
@ -166,16 +176,19 @@ var quotes = function(dbot) {
}, },
'~rmlast': function(event) { '~rmlast': function(event) {
if(rmAllowed == true || dbot.config.admins.include(event.user)) { if(rmAllowed == true || _.include(dbot.config.admins, event.user)) {
var key = event.input[1].trim().toLowerCase(); var key = event.input[1].trim().toLowerCase();
if(quotes.hasOwnProperty(key)) { if(_.has(quotes, key)) {
var quote = quotes[key].pop(); var quote = quotes[key].pop();
if(quotes[key].length === 0) { if(quotes[key].length === 0) {
delete quotes[key]; delete quotes[key];
} }
resetRemoveTimer(event, key, quote); resetRemoveTimer(event, key, quote);
event.reply(dbot.t('removed_from', {'quote': quote, 'category': key})); event.reply(dbot.t('removed_from', {
'quote': quote,
'category': key
}));
} else { } else {
event.reply(dbot.t('no_quotes', {'category': q[1]})); event.reply(dbot.t('no_quotes', {'category': q[1]}));
} }
@ -185,11 +198,11 @@ var quotes = function(dbot) {
}, },
'~rm': function(event) { '~rm': function(event) {
if(rmAllowed == true || dbot.config.admins.include(event.user)) { if(rmAllowed == true || _.include(dbot.config.admins, event.user)) {
var key = event.input[1].trim().toLowerCase(); var key = event.input[1].trim().toLowerCase();
var quote = event.input[2]; var quote = event.input[2];
if(quotes.hasOwnProperty(key)) { if(_.has(quotes, key)) {
var category = quotes[key]; var category = quotes[key];
var index = category.indexOf(quote); var index = category.indexOf(quote);
if(index !== -1) { if(index !== -1) {
@ -215,51 +228,59 @@ var quotes = function(dbot) {
var input = event.message.valMatch(/^~qcount ([\d\w\s-]*)/, 2); var input = event.message.valMatch(/^~qcount ([\d\w\s-]*)/, 2);
if(input) { // Give quote count for named category if(input) { // Give quote count for named category
var key = input[1].trim().toLowerCase(); var key = input[1].trim().toLowerCase();
if(quotes.hasOwnProperty(key)) { if(_.has(quotes, key)) {
event.reply(dbot.t('quote_count', {'category': key, 'count': quotes[key].length})); event.reply(dbot.t('quote_count', {
'category': key,
'count': quotes[key].length
}));
} else { } else {
event.reply(dbot.t('no_quotes', {'category': key})); event.reply(dbot.t('no_quotes', { 'category': key }));
} }
} else { // Give total quote count } else { // Give total quote count
var totalQuoteCount = 0; var totalQuoteCount = _.reduce(quotes, function(memo, category) {
for(var category in quotes) { return memo + category.length;
if(quotes.hasOwnProperty(category)) { }, 0);
totalQuoteCount += quotes[category].length; event.reply(dbot.t('total_quotes', { 'count': totalQuoteCount }));
}
}
event.reply(dbot.t('total_quotes', {'count': totalQuoteCount}));
} }
}, },
'~qadd': function(event) { '~qadd': function(event) {
var key = event.input[1].toLowerCase(); var key = event.input[1].toLowerCase();
var text = event.input[2]; var text = event.input[2];
if(!Object.isArray(quotes[key])) { if(!_.isArray(quotes[key])) {
quotes[key] = []; quotes[key] = [];
} }
if(quotes[key].include(text)) { if(_.include(quotes[key], text)) {
event.reply(dbot.t('quote_exists')); event.reply(dbot.t('quote_exists'));
} else { } else {
quotes[key].push(text); quotes[key].push(text);
rmAllowed = true; rmAllowed = true;
event.reply(dbot.t('quote_saved', {'category': key, 'count': quotes[key].length})); event.reply(dbot.t('quote_saved', {
'category': key,
'count': quotes[key].length
}));
} }
}, },
'~rq': function(event) { '~rq': function(event) {
var rQuote = Object.keys(quotes).random(); var category = _.keys(quotes)[_.random(0, _.size(quotes) -1)];
event.reply(rQuote + ': ' + interpolatedQuote(event, rQuote)); event.reply(category + ': ' + interpolatedQuote(event, category));
}, },
'~link': function(event) { '~link': function(event) {
var key = event.params[1].trim().toLowerCase(); var key = event.params[1].trim().toLowerCase();
if(quotes.hasOwnProperty(key)) { if(_.has(quotes, key)) {
event.reply(dbot.t('quote_link', {'category': key, event.reply(dbot.t('quote_link', {
'url': dbot.t('url', {'host': dbot.config.web.webHost, 'category': key,
'port': dbot.config.web.webPort, 'path': 'quotes/' + key})})); 'url': dbot.t('url', {
'host': dbot.config.web.webHost,
'port': dbot.config.web.webPort,
'path': 'quotes/' + key
})
}));
} else { } else {
event.reply(dbot.t('category_not_found', {'category': key})); event.reply(dbot.t('category_not_found', { 'category': key }));
} }
}, },
}; };
@ -278,7 +299,7 @@ var quotes = function(dbot) {
// Lists quotes in a category // Lists quotes in a category
'/quotes/:key': function(req, res) { '/quotes/:key': function(req, res) {
var key = req.params.key.toLowerCase(); var key = req.params.key.toLowerCase();
if(dbot.db.quoteArrs.hasOwnProperty(key)) { if(_.has(dbot.db.quoteArrs, key)) {
res.render('quotes', { 'name': dbot.config.name, 'quotes': dbot.db.quoteArrs[key], locals: { 'url_regex': RegExp.prototype.url_regex() } }); res.render('quotes', { 'name': dbot.config.name, 'quotes': dbot.db.quoteArrs[key], locals: { 'url_regex': RegExp.prototype.url_regex() } });
} else { } else {
res.render('error', { 'name': dbot.config.name, 'message': 'No quotes under that key.' }); res.render('error', { 'name': dbot.config.name, 'message': 'No quotes under that key.' });

View File

@ -1,3 +1,5 @@
var _ = require('underscore')._;
var report = function(dbot) { var report = function(dbot) {
var commands = { var commands = {
'~report': function(event) { '~report': function(event) {
@ -5,29 +7,21 @@ var report = function(dbot) {
var nick = event.input[2]; var nick = event.input[2];
var reason = event.input[3]; var reason = event.input[3];
if(event.allChannels.hasOwnProperty(channelName)) { if(_.has(event.allChannels, channelName)) {
var channel = event.allChannels[channelName]; var channel = event.allChannels[channelName];
if(channel.nicks.hasOwnProperty(nick)) { if(_.has(channel.nicks, nick)) {
var ops = []; var ops = _.filter(channel.nicks, function(user) {
for(var possibOps in channel.nicks) { return user.op;
if(channel.nicks[possibOps].op == true) { });
ops.push(possibOps);
}
}
// Does the channel have an admin channel? _.each(ops, function(user) {
if(event.allChannels.hasOwnProperty('#' + channelName)) { dbot.say(event.server, user.name, dbot.t('report', {
ops.push('#' + channelName);
}
for(var i=0;i<ops.length;i++) {
dbot.say(event.server, ops[i], dbot.t('report', {
'reporter': event.user, 'reporter': event.user,
'reported': nick, 'reported': nick,
'channel': channelName, 'channel': channelName,
'reason': reason 'reason': reason
})); }));
} }, this);
event.reply(dbot.t('reported', { 'reported': nick })); event.reply(dbot.t('reported', { 'reported': nick }));
} else { } else {

View File

@ -1,8 +1,83 @@
var _ = require('underscore')._;
var allGroupings = function(arr) {
if (arr.length == 0) {
return []; /* short-circuit the empty-array case */
}
var groupings = [];
for(var n=1;n<=arr.length;n++) {
for(var i=0;i<(arr.length-(n-1));i++) {
groupings.push(arr.slice(i, i+n));
}
}
return groupings;
}
var distance = function(s1, s2) {
// Calculate Levenshtein distance between two strings
//
// version: 1109.2015
// discuss at: http://phpjs.org/functions/levenshtein
// + original by: Carlos R. L. Rodrigues (http://www.jsfromhell.com)
// + bugfixed by: Onno Marsman
// + revised by: Andrea Giammarchi (http://webreflection.blogspot.com)
// + reimplemented by: Brett Zamir (http://brett-zamir.me)
// + reimplemented by: Alexander M Beedie
if (s1 == s2) {
return 0;
}
var s1_len = s1.length;
var s2_len = s2.length;
if (s1_len === 0) {
return s2_len; }
if (s2_len === 0) {
return s1_len;
}
// BEGIN STATIC
var split = false;
try {
split = !('0')[0];
} catch (e) {
split = true; // Earlier IE may not support access by string index
}
// END STATIC
if (split) {
s1 = s1.split(''); s2 = s2.split('');
}
var v0 = new Array(s1_len + 1);
var v1 = new Array(s1_len + 1);
var s1_idx = 0,
s2_idx = 0,
cost = 0;
for (s1_idx = 0; s1_idx < s1_len + 1; s1_idx++) { v0[s1_idx] = s1_idx;
}
var char_s1 = '',
char_s2 = '';
for (s2_idx = 1; s2_idx <= s2_len; s2_idx++) { v1[0] = s2_idx;
char_s2 = s2[s2_idx - 1];
for (s1_idx = 0; s1_idx < s1_len; s1_idx++) {
char_s1 = s1[s1_idx]; cost = (char_s1 == char_s2) ? 0 : 1;
var m_min = v0[s1_idx + 1] + 1;
var b = v1[s1_idx] + 1;
var c = v0[s1_idx] + cost;
if (b < m_min) { m_min = b;
}
if (c < m_min) {
m_min = c;
} v1[s1_idx + 1] = m_min;
}
var v_tmp = v0;
v0 = v1;
v1 = v_tmp; }
return v0[s1_len];
};
var spelling = function(dbot) { var spelling = function(dbot) {
var last = {}; var last = {};
var correct = function (event, correction, candidate, output_callback) { var correct = function (event, correction, candidate, output_callback) {
var rawCandidates = last[event.channel.name][candidate].split(' ').allGroupings(); var rawCandidates = allGroupings(last[event.channel.name][candidate].split(' '));
var candidates = []; var candidates = [];
for(var i=0;i<rawCandidates.length;i++) { for(var i=0;i<rawCandidates.length;i++) {
candidates.push(rawCandidates[i].join(' ')); candidates.push(rawCandidates[i].join(' '));
@ -11,10 +86,10 @@ var spelling = function(dbot) {
var winnerDistance = Infinity; var winnerDistance = Infinity;
for(var i=0;i<candidates.length;i++) { for(var i=0;i<candidates.length;i++) {
var distance = String.prototype.distance(correction.toLowerCase(), candidates[i].toLowerCase()); var d = distance(correction.toLowerCase(), candidates[i].toLowerCase());
if((distance < winnerDistance) && (distance > 0)) { if((d < winnerDistance) && (d > 0)) {
winner = candidates[i]; winner = candidates[i];
winnerDistance = distance; winnerDistance = d;
} }
} }
@ -51,7 +126,7 @@ var spelling = function(dbot) {
event.reply(dbot.t('spelling_other', e)); event.reply(dbot.t('spelling_other', e));
}); });
} else { } else {
if(last.hasOwnProperty(event.channel.name)) { if(_.has(last, event.channel.name)) {
last[event.channel.name][event.user] = event.message; last[event.channel.name][event.user] = event.message;
} else { } else {
last[event.channel.name] = { }; last[event.channel.name] = { };

View File

@ -2,15 +2,16 @@
* Name: Users * Name: Users
* Description: Track known users * Description: Track known users
*/ */
var web = require('./web'); var web = require('./web'),
_ = require('underscore')._;
var users = function(dbot) { var users = function(dbot) {
var knownUsers = dbot.db.knownUsers; var knownUsers = dbot.db.knownUsers;
var getServerUsers = function(server) { var getServerUsers = function(server) {
if(!knownUsers.hasOwnProperty(server)) { if(!_.has(knownUsers, server)) {
knownUsers[server] = { 'users': [], 'aliases': {}, 'channelUsers': {} }; knownUsers[server] = { 'users': [], 'aliases': {}, 'channelUsers': {} };
} }
if(!knownUsers[server].hasOwnProperty('channelUsers')) { if(!_.has(knownUsers[server], 'channelUsers')) {
knownUsers[server].channelUsers = {}; knownUsers[server].channelUsers = {};
} }
return knownUsers[server]; return knownUsers[server];
@ -18,43 +19,39 @@ var users = function(dbot) {
var updateAliases = function(event, oldUser, newUser) { var updateAliases = function(event, oldUser, newUser) {
var knownUsers = getServerUsers(event.server); var knownUsers = getServerUsers(event.server);
for(var alias in knownUsers.aliases) { _.each(knownUsers.aliases, function(user, alias) {
if(knownUsers.aliases.hasOwnProperty(alias)) { if(user == oldUser) {
if(knownUsers.aliases[alias] === oldUser) { knownUsers.aliases[alias] = newUser;
knownUsers.aliases[alias] = newUser;
}
} }
} }, this);
} };
var updateChannels = function(event, oldUser, newUser) { var updateChannels = function(event, oldUser, newUser) {
var channelUsers = getServerUsers(event.server).channelUsers; var channelUsers = getServerUsers(event.server).channelUsers;
channelUsers.each(function(channel) { channelUsers = _.each(channelUsers, function(channel, channelName) {
if(channel.include(oldUser)) { channelUsers[channelName] = _.without(channel, oldUser);
channel.splice(channel.indexOf(oldUser), 1); channelUsers[channelName].push(newUser);
channel.push(newUser); }, this);
} };
}.bind(this));
}
dbot.instance.addListener('366', 'users', function(event) { dbot.instance.addListener('366', 'users', function(event) {
var knownUsers = getServerUsers(event.server); var knownUsers = getServerUsers(event.server);
if(!knownUsers.channelUsers.hasOwnProperty(event.channel.name)) { if(!_.has(knownUsers.channelUsers, event.channel.name)) {
knownUsers.channelUsers[event.channel.name] = []; knownUsers.channelUsers[event.channel.name] = [];
} }
var channelUsers = knownUsers.channelUsers[event.channel.name]; var channelUsers = knownUsers.channelUsers[event.channel.name];
event.channel.nicks.each(function(nick) { _.each(event.channel.nicks, function(nick) {
nick = nick.name; nick = nick.name;
if(api.isKnownUser(event.server, nick)) { if(api.isKnownUser(event.server, nick)) {
nick = api.resolveUser(event.server, nick); nick = api.resolveUser(event.server, nick);
} else { } else {
knownUsers.users.push(nick); knownUsers.users.push(nick);
} }
if(!channelUsers.include(nick)) { if(!_.include(channelUsers, nick)) {
channelUsers.push(nick); channelUsers.push(nick);
} }
}.bind(this)); }, this);
}); });
@ -62,7 +59,7 @@ var users = function(dbot) {
'resolveUser': function(server, nick, useLowercase) { 'resolveUser': function(server, nick, useLowercase) {
var knownUsers = getServerUsers(server); var knownUsers = getServerUsers(server);
var user = nick; var user = nick;
if(!knownUsers.users.include(nick) && knownUsers.aliases.hasOwnProperty(nick)) { if(!_.include(knownUsers.users, nick) && _.has(knownUsers.aliases, nick)) {
user = knownUsers.aliases[nick]; user = knownUsers.aliases[nick];
} }
@ -72,23 +69,29 @@ var users = function(dbot) {
'isKnownUser': function(server, nick) { 'isKnownUser': function(server, nick) {
var knownUsers = getServerUsers(server); var knownUsers = getServerUsers(server);
return (knownUsers.users.include(nick) || knownUsers.aliases.hasOwnProperty(nick)); return (_.include(knownUsers.users, nick) || _.has(knownUsers.aliases, nick));
} }
}; };
var commands = { var commands = {
'~alias': function(event) { '~alias': function(event) {
var knownUsers = getServerUsers(event.server); var knownUsers = getServerUsers(event.server),
var alias = event.params[1].trim(); alias = event.params[1].trim();
if(knownUsers.users.include(alias)) {
var aliasCount = 0; if(_.include(knownUsers.users, alias)) {
knownUsers.aliases.each(function(primaryUser) { var aliasCount = _.reduce(knownUsers.aliases, function(memo, user) {
if(primaryUser == alias) aliasCount += 1; if(user == alias) return memo += 1;
}.bind(this)); }, 0, this);
event.reply(dbot.t('primary', { 'user': alias, 'count': aliasCount }));
} else if(knownUsers.aliases.hasOwnProperty(alias)) { event.reply(dbot.t('primary', {
event.reply(dbot.t('alias', { 'alias': alias, 'user': alias,
'user': knownUsers.aliases[alias] })); 'count': aliasCount
}));
} else if(_.has(knownUsers.aliases, alias)) {
event.reply(dbot.t('alias', {
'alias': alias,
'user': knownUsers.aliases[alias]
}));
} else { } else {
event.reply(dbot.t('unknown_alias', { 'alias': alias })); event.reply(dbot.t('unknown_alias', { 'alias': alias }));
} }
@ -98,12 +101,11 @@ var users = function(dbot) {
var knownUsers = getServerUsers(event.server); var knownUsers = getServerUsers(event.server);
var newParent = event.params[1]; var newParent = event.params[1];
if(knownUsers.aliases.hasOwnProperty(newParent)) { if(_.has(knownUsers.aliases, newParent)) {
var newAlias = knownUsers.aliases[newParent]; var newAlias = knownUsers.aliases[newParent];
// Replace users entry with new primary user // Replace user entry
var usersIndex = knownUsers.users.indexOf(newAlias); knownUsers.users = _.without(knownUsers.users, newAlias);
knownUsers.users.splice(usersIndex, 1);
knownUsers.users.push(newParent); knownUsers.users.push(newParent);
// Replace channels entries with new primary user // Replace channels entries with new primary user
@ -116,12 +118,14 @@ var users = function(dbot) {
// Update aliases to point to new primary user // Update aliases to point to new primary user
updateAliases(event, newAlias, newParent); updateAliases(event, newAlias, newParent);
event.reply(dbot.t('aliasparentset', { 'newParent': newParent, event.reply(dbot.t('aliasparentset', {
'newAlias': newAlias })); 'newParent': newParent,
'newAlias': newAlias
}));
dbot.api.stats.fixStats(event.server, newAlias); dbot.api.stats.fixStats(event.server, newAlias);
} else { } else {
event.reply(dbot.t('unknown_alias', { 'alias': newParent})); event.reply(dbot.t('unknown_alias', { 'alias': newParent }));
} }
}, },
@ -130,8 +134,8 @@ var users = function(dbot) {
var primaryUser = event.params[1]; var primaryUser = event.params[1];
var secondaryUser = event.params[2]; var secondaryUser = event.params[2];
if(knownUsers.users.include(primaryUser) && knownUsers.users.include(secondaryUser)) { if(_.include(knownUsers.users, primaryUser) && _.include(knownUsers.users, secondaryUser)) {
knownUsers.users.splice(knownUsers.users.indexOf(secondaryUser), 1); knownUsers.users = _.without(knownUsers.users, secondaryUser);
knownUsers.aliases[secondaryUser] = primaryUser; knownUsers.aliases[secondaryUser] = primaryUser;
updateAliases(event, secondaryUser, primaryUser); updateAliases(event, secondaryUser, primaryUser);
updateChannels(event, secondaryUser, primaryUser); updateChannels(event, secondaryUser, primaryUser);
@ -163,7 +167,7 @@ var users = function(dbot) {
var nick = event.user; var nick = event.user;
if(event.action == 'JOIN') { if(event.action == 'JOIN') {
if(!knownUsers.channelUsers.hasOwnProperty(event.channel.name)) { if(!_.has(knownUsers.channelUsers, event.channel.name)) {
knownUsers.channelUsers[event.channel.name] = []; knownUsers.channelUsers[event.channel.name] = [];
} }
var channelUsers = knownUsers.channelUsers[event.channel.name]; var channelUsers = knownUsers.channelUsers[event.channel.name];
@ -173,15 +177,15 @@ var users = function(dbot) {
} else { } else {
knownUsers.users.push(nick); knownUsers.users.push(nick);
} }
if(!channelUsers.include(nick)) { if(!_.include(channelUsers, nick)) {
channelUsers.push(nick); channelUsers.push(nick);
} }
} else if(event.action == 'NICK') { } else if(event.action == 'NICK') {
var newNick = event.params.substr(1); var newNick = event.params.substr(1);
if(knownUsers.aliases.hasOwnProperty(event.user)) { if(_.has(knownUsers.aliases, event.user)) {
knownUsers.aliases[newNick] = knownUsers.aliases[event.user]; knownUsers.aliases[newNick] = knownUsers.aliases[event.user];
} else { } else {
if(!knownUsers.users.include(newNick)) { if(!_.include(knownUsers.users, newNick)) {
knownUsers.aliases[newNick] = event.user; knownUsers.aliases[newNick] = event.user;
} }
} }
@ -192,11 +196,9 @@ var users = function(dbot) {
'onLoad': function() { 'onLoad': function() {
// Trigger updateNickLists to stat current users in channel // Trigger updateNickLists to stat current users in channel
var connections = dbot.instance.connections; var connections = dbot.instance.connections;
for(var conn in connections) { _.each(connections, function(connection) {
if(connections.hasOwnProperty(conn)) { connection.updateNickLists();
connections[conn].updateNickLists(); });
}
}
} }
}; };
}; };

View File

@ -1,4 +1,5 @@
var express = require('express'), var express = require('express'),
_ = require('underscore')._,
fs = require('fs'); fs = require('fs');
var webInterface = function(dbot) { var webInterface = function(dbot) {
@ -16,7 +17,7 @@ var webInterface = function(dbot) {
var reloadPages = function(pages) { var reloadPages = function(pages) {
for(var p in pages) { for(var p in pages) {
if( pages.hasOwnProperty(p) ) { if(_.has(pages, p)) {
var func = pages[p]; var func = pages[p];
var mod = func.module; var mod = func.module;
app.get(p, (function(req, resp) { app.get(p, (function(req, resp) {

165
run.js
View File

@ -1,29 +1,27 @@
var fs = require('fs'); var fs = require('fs'),
var timers = require('./timer'); _ = require('underscore')._,
var jsbot = require('./jsbot/jsbot'); timers = require('./timer'),
jsbot = require('./jsbot/jsbot');
require('./snippets'); require('./snippets');
var DBot = function(timers) { var DBot = function(timers) {
// Load external files // Load config
var requiredConfigKeys = [ 'name', 'servers', 'admins', 'moderators', 'moduleNames', 'language', 'debugMode' ];
try { try {
this.config = JSON.parse(fs.readFileSync('config.json', 'utf-8')); this.config = JSON.parse(fs.readFileSync('config.json', 'utf-8'));
} catch(err) { } catch(err) {
console.log('Config file is screwed up. Attempting to load defaults.'); console.log('Config file is invalid. Stopping');
try { process.exit();
this.config = JSON.parse(fs.readFileSync('config.json.sample', 'utf-8'));
} catch(err) {
console.log('Error loading sample config. Bugger off. Stopping.');
process.exit();
}
} }
requiredConfigKeys.each(function(key) {
if(!this.config.hasOwnProperty(key)) { try {
console.log('Error: Please set a value for ' + key + ' in ' + var defaultConfig = JSON.parse(fs.readFileSync('config.json.sample', 'utf-8'));
'config.json. Stopping.'); } catch(err) {
process.exit(); console.log('Error loading sample config. Bugger off this should not even be edited. Stopping.');
} process.exit();
}.bind(this)); }
// Load missing config directives from sample file
_.defaults(this.config, defaultConfig);
var rawDB; var rawDB;
try { try {
@ -57,18 +55,15 @@ var DBot = function(timers) {
// Populate bot properties with config data // Populate bot properties with config data
// Create JSBot and connect to each server // Create JSBot and connect to each server
this.instance = jsbot.createJSBot(this.config.name); this.instance = jsbot.createJSBot(this.config.name);
for(var name in this.config.servers) { _.each(this.config.servers, function(server, name) {
if(this.config.servers.hasOwnProperty(name)) { this.instance.addConnection(name, server.server, server.port,
var server = this.config.servers[name]; this.config.admin, function(event) {
this.instance.addConnection(name, server.server, server.port, var server = this.config.servers[event.server];
this.config.admin, function(event) { for(var i=0;i<server.channels.length;i++) {
var server = this.config.servers[event.server]; this.instance.join(event, server.channels[i]);
for(var i=0;i<server.channels.length;i++) { }
this.instance.join(event, server.channels[i]); }.bind(this), server.nickserv, server.password);
} }, this);
}.bind(this), server.nickserv, server.password);
}
}
// Load the modules and connect to the server // Load the modules and connect to the server
this.reloadModules(); this.reloadModules();
@ -83,9 +78,9 @@ DBot.prototype.say = function(server, channel, message) {
// Format given stored string in config language // Format given stored string in config language
DBot.prototype.t = function(string, formatData) { DBot.prototype.t = function(string, formatData) {
var formattedString; var formattedString;
if(this.strings.hasOwnProperty(string)) { if(_.has(this.strings, string)) {
var lang = this.config.language; var lang = this.config.language;
if(!this.strings[string].hasOwnProperty(lang)) { if(!_.has(this.strings[string], lang)) {
lang = "english"; lang = "english";
} }
@ -163,17 +158,19 @@ DBot.prototype.reloadModules = function() {
// Load the module config data // Load the module config data
var config = {}; var config = {};
try { try {
var config = JSON.parse(fs.readFileSync(moduleDir + 'config.json', 'utf-8')) config = JSON.parse(fs.readFileSync(moduleDir + 'config.json', 'utf-8'))
this.config[name] = config;
for(var i=0;i<config.dbKeys.length;i++) {
if(!this.db.hasOwnProperty(config.dbKeys[i])) {
this.db[config.dbKeys[i]] = {};
}
}
} catch(err) { } catch(err) {
// Invalid or no config data // Invalid or no config data
} }
this.config[name] = config;
_.each(config.dbKeys, function(dbKey) {
if(!_.has(this.db, dbKey)) {
this.db[dbKey] = {};
}
}, this);
// Load the module itself // Load the module itself
var rawModule = require(moduleDir + name); var rawModule = require(moduleDir + name);
var module = rawModule.fetch(this); var module = rawModule.fetch(this);
@ -182,14 +179,13 @@ DBot.prototype.reloadModules = function() {
module.name = name; module.name = name;
if(module.listener) { if(module.listener) {
var listenOn = module.on; if(!_.isArray(module.on)) {
if(!(listenOn instanceof Array)) { module.on = [ module.on ];
listenOn = [listenOn];
} }
listenOn.each(function(on) { _.each(module.on, function(on) {
this.instance.addListener(on, module.name, module.listener); this.instance.addListener(on, module.name, module.listener);
}.bind(this)); }, this);
} }
if(module.onLoad) { if(module.onLoad) {
@ -198,36 +194,21 @@ DBot.prototype.reloadModules = function() {
// Load module commands // Load module commands
if(module.commands) { if(module.commands) {
var newCommands = module.commands; _.extend(this.commands, module.commands);
for(key in newCommands) { _.each(module.commands, function(command, commandName) {
if(newCommands.hasOwnProperty(key) && Object.prototype.isFunction(newCommands[key])) { command.module = name;
this.commands[key] = newCommands[key]; if(_.has(config, 'commands') && _.has(config.commands, commandName)) {
this.commandMap[key] = name; _.extend(command, config.commands[commandName]);
} }
} }, this);
}
// Load module commands with properties specified in config
if(module.commands && config.commands) {
for(key in config.commands) {
if(newCommands.hasOwnProperty(key)) {
for(var prop in config.commands[key]) {
newCommands[key][prop] = config.commands[key][prop];
}
}
}
} }
// Load module web bits // Load module web bits
if(module.pages) { if(module.pages) {
var newpages = module.pages; _.extend(this.pages, module.pages);
for(var key in newpages) _.each(module.pages, function(page) {
{ page.module = name;
if(newpages.hasOwnProperty(key) && Object.prototype.isFunction(newpages[key])) { }, this);
this.pages[key] = newpages[key];
this.pages[key].module = module;
}
}
} }
// Load module API // Load module API
@ -236,67 +217,57 @@ DBot.prototype.reloadModules = function() {
} }
// Load the module usage data // Load the module usage data
var usage = {};
try { try {
var usage = JSON.parse(fs.readFileSync(moduleDir + 'usage.json', 'utf-8')); usage = JSON.parse(fs.readFileSync(moduleDir + 'usage.json', 'utf-8'));
for(key in usage) {
if(usage.hasOwnProperty(key)) {
if(this.usage.hasOwnProperty(key)) {
console.log('Usage key clash for ' + key + ' in ' + name);
} else {
this.usage[key] = usage[key];
}
}
}
} catch(err) { } catch(err) {
// Invalid or no usage info // Invalid or no usage info
} }
_.extend(this.usage, usage);
// Load the module string data // Load the module string data
var strings = {};
try { try {
var strings = JSON.parse(fs.readFileSync(moduleDir + 'strings.json', 'utf-8')); strings = JSON.parse(fs.readFileSync(moduleDir + 'strings.json', 'utf-8'));
for(key in strings) {
if(strings.hasOwnProperty(key)) {
if(this.strings.hasOwnProperty(key)) {
console.log('Strings key clash for ' + key + ' in ' + name);
} else {
this.strings[key] = strings[key];
}
}
}
} catch(err) { } catch(err) {
// Invalid or no string info // Invalid or no string info
} }
_.extend(this.strings, strings);
// Provide toString for module name
module.toString = function() { module.toString = function() {
return this.name; return this.name;
} }
this.modules[module.name] = module; this.modules[module.name] = module;
} catch(err) { } catch(err) {
console.log(this.t('module_load_error', {'moduleName': name})); console.log(this.t('module_load_error', {'moduleName': name}));
if(this.config.debugMode) { if(this.config.debugMode) {
console.log('MODULE ERROR (' + name + '): ' + err.stack ); console.log('MODULE ERROR (' + name + '): ' + err.stack );
} } else {
else {
console.log('MODULE ERROR (' + name + '): ' + err ); console.log('MODULE ERROR (' + name + '): ' + err );
} }
} }
}.bind(this)); }.bind(this));
this.reloadPages(); this.reloadPages();
this.save(); this.save();
}; };
// I honestly don't know what the fuck this is meant to do. Why is it getting a
// reference to all the pages?
DBot.prototype.reloadPages = function() { DBot.prototype.reloadPages = function() {
for( var m in this.modules ) { _.each(this.modules, function(module) {
if( Object.prototype.isFunction(this.modules[m].reloadPages)) { if(_.isFunction(module.reloadPages)) {
this.modules[m].reloadPages(this.pages); module.reloadPages(this.pages);
} }
} }, this);
} }
DBot.prototype.cleanNick = function(key) { DBot.prototype.cleanNick = function(key) {
key = key.toLowerCase(); key = key.toLowerCase();
while(key.endsWith("_")) { while(key.endsWith("_")) {
if(this.db.quoteArrs.hasOwnProperty(key)) { if(_.has(this.db.quoteArrs, key)) {
return key; return key;
} }
key = key.substring(0, key.length-1); key = key.substring(0, key.length-1);

View File

@ -35,19 +35,6 @@ Array.prototype.sum = function() {
return sum; return sum;
}; };
Array.prototype.allGroupings = function() {
if (this.length == 0) {
return []; /* short-circuit the empty-array case */
}
var groupings = [];
for(var n=1;n<=this.length;n++) {
for(var i=0;i<(this.length-(n-1));i++) {
groupings.push(this.slice(i, i+n));
}
}
return groupings;
}
Array.prototype.uniq = function() { Array.prototype.uniq = function() {
var hash = {} var hash = {}
var result = []; var result = [];
@ -79,66 +66,6 @@ String.prototype.startsWith = function(needle) {
return needle === this.slice(0, needle.length); return needle === this.slice(0, needle.length);
}; };
String.prototype.distance = function(s1, s2) {
// Calculate Levenshtein distance between two strings
//
// version: 1109.2015
// discuss at: http://phpjs.org/functions/levenshtein // + original by: Carlos R. L. Rodrigues (http://www.jsfromhell.com)
// + bugfixed by: Onno Marsman
// + revised by: Andrea Giammarchi (http://webreflection.blogspot.com)
// + reimplemented by: Brett Zamir (http://brett-zamir.me)
// + reimplemented by: Alexander M Beedie // * example 1: levenshtein('Kevin van Zonneveld', 'Kevin van Sommeveld');
// * returns 1: 3
if (s1 == s2) {
return 0;
}
var s1_len = s1.length;
var s2_len = s2.length;
if (s1_len === 0) {
return s2_len; }
if (s2_len === 0) {
return s1_len;
}
// BEGIN STATIC
var split = false;
try {
split = !('0')[0];
} catch (e) { split = true; // Earlier IE may not support access by string index
}
// END STATIC
if (split) {
s1 = s1.split(''); s2 = s2.split('');
}
var v0 = new Array(s1_len + 1);
var v1 = new Array(s1_len + 1);
var s1_idx = 0,
s2_idx = 0,
cost = 0;
for (s1_idx = 0; s1_idx < s1_len + 1; s1_idx++) { v0[s1_idx] = s1_idx;
}
var char_s1 = '',
char_s2 = '';
for (s2_idx = 1; s2_idx <= s2_len; s2_idx++) { v1[0] = s2_idx;
char_s2 = s2[s2_idx - 1];
for (s1_idx = 0; s1_idx < s1_len; s1_idx++) {
char_s1 = s1[s1_idx]; cost = (char_s1 == char_s2) ? 0 : 1;
var m_min = v0[s1_idx + 1] + 1;
var b = v1[s1_idx] + 1;
var c = v0[s1_idx] + cost;
if (b < m_min) { m_min = b;
}
if (c < m_min) {
m_min = c;
} v1[s1_idx + 1] = m_min;
}
var v_tmp = v0;
v0 = v1;
v1 = v_tmp; }
return v0[s1_len];
}
String.prototype.format = function() { // format takes either multiple indexed arguments, or a single object, whose keys/values will be used String.prototype.format = function() { // format takes either multiple indexed arguments, or a single object, whose keys/values will be used
var targetStr = this; var targetStr = this;
var replacements = [].splice.call(arguments, 0); var replacements = [].splice.call(arguments, 0);