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
* administrator can run - as such, it has its own command execution listener.
*/
var fs = require('fs');
var sys = require('sys')
var exec = require('child_process').exec;
var fs = require('fs'),
_ = require('underscore')._,
sys = require('sys'),
exec = require('child_process').exec;
var admin = function(dbot) {
var commands = {
// Join a channel
'join': function(event) {
var channel = event.params[1];
if(event.allChannels.hasOwnProperty(channel)) {
if(_.has(event.allChannels, channel)) {
event.reply(dbot.t('already_in_channel', {'channel': channel}));
} else {
dbot.instance.join(event, channel);
@ -23,7 +24,7 @@ var admin = function(dbot) {
// Leave a channel
'part': function(event) {
var channel = event.params[1];
if(!event.allChannels.hasOwnProperty(channel)) {
if(!_.has(event.allChannels, channel)) {
event.reply(dbot.t('not_in_channel', {'channel': channel}));
} else {
event.instance.part(event, channel);
@ -36,7 +37,7 @@ var admin = function(dbot) {
var channel = event.params[1];
// 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;
}
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 -- ";
if(event.params[1]){
var input = event.params[1].trim();
if(dbot.modules.hasOwnProperty(input.split("/")[0])){
if(_.has(dbot.modules, input.split("/")[0])){
cmd += "modules/"+input;
}
else{
@ -99,7 +100,7 @@ var admin = function(dbot) {
// Load new module
'load': function(event) {
var moduleName = event.params[1];
if(!dbot.config.moduleNames.include(moduleName)) {
if(!_.include(dbot.config.moduleNames, moduleName)) {
dbot.config.moduleNames.push(moduleName);
dbot.reloadModules();
event.reply(dbot.t('load_module', {'moduleName': moduleName}));
@ -116,13 +117,11 @@ var admin = function(dbot) {
'unload': function(event) {
var moduleNames = dbot.config.moduleNames;
var moduleName = event.params[1];
if(moduleNames.include(moduleName)) {
if(_.include(moduleNames, moduleName)) {
var moduleDir = '../' + moduleName + '/';
var cacheKey = require.resolve(moduleDir + moduleName);
delete require.cache[cacheKey];
var moduleIndex = moduleNames.indexOf(moduleName);
moduleNames.splice(moduleIndex, 1);
dbot.config.moduleNames = _.without(dbot.config.moduleNames, moduleName);
dbot.reloadModules();
event.reply(dbot.t('unload_module', {'moduleName': moduleName}));
@ -136,7 +135,7 @@ var admin = function(dbot) {
var username = event.params[1];
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].push(username);
@ -147,8 +146,10 @@ var admin = function(dbot) {
'unban': function(event) {
var username = event.params[1];
var command = event.params[2];
if(dbot.db.bans.hasOwnProperty(command) && dbot.db.bans[command].include(username)) {
dbot.db.bans[command].splice(dbot.db.bans[command].indexOf(username), 1);
if(_.has(dbot.db.bans, command) && _.include(dbot.db.bans[command], username)) {
_.reject(dbot.db.bans[command], function(bans) {
return bans == username;
}, this);
event.reply(dbot.t('unbanned', {'user': username, 'command': command}));
} else {
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
* ignoring that command.
*/
var _ = require('underscore')._;
var command = function(dbot) {
/**
* Is user banned from using command?
*/
var isBanned = function(user, command) {
var banned = false;
if(dbot.db.bans.hasOwnProperty(command)) {
if(dbot.db.bans[command].include(user) || dbot.db.bans['*'].include(user)) {
if(_.has(dbot.db.bans, command)) {
if(_.include(dbot.db.bans[command], user) || _.include(dbot.db.bans['*'], user)) {
banned = true;
}
}
@ -26,12 +27,12 @@ var command = function(dbot) {
var accessNeeded = dbot.commands[command].access;
if(accessNeeded == 'admin') {
if(!dbot.config.admins.include(user)) {
if(!_.include(dbot.config.admins, user)) {
access = false;
}
} else if(accessNeeded == 'moderator') {
if(!dbot.config.moderators.include(user) &&
!dbot.config.admins.include(user)) {
if(!_.include(dbot.config.moderators, user) &&
!_.include(dbot.config.admins, user)) {
access = false;
}
}
@ -43,9 +44,9 @@ var command = function(dbot) {
* Is user ignoring command?
*/
var isIgnoring = function(user, command) {
var module = dbot.commandMap[command];
var module = dbot.commands[command].module;
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;
}
return ignoring;
@ -57,7 +58,7 @@ var command = function(dbot) {
*/
var applyRegex = function(commandName, event) {
var applies = false;
if(dbot.commands[commandName].hasOwnProperty('regex')) {
if(_.has(dbot.commands[commandName], 'regex')) {
var cRegex = dbot.commands[commandName].regex;
var q = event.message.valMatch(cRegex[0], cRegex[1]);
if(q) {
@ -77,7 +78,7 @@ var command = function(dbot) {
'commands': {
'~usage': function(event) {
var commandName = event.params[1];
if(dbot.usage.hasOwnProperty(commandName)) {
if(_.has(dbot.usage, commandName)) {
event.reply(dbot.t('usage', {
'command': commandName,
'usage': dbot.usage[commandName]
@ -91,11 +92,11 @@ var command = function(dbot) {
'~help': function(event) {
var moduleName = event.params[1];
if(!dbot.modules.hasOwnProperty(moduleName)) {
var moduleName = dbot.commandMap[moduleName];
if(!_.has(dbot.modules, 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;
event.reply(dbot.t('help_link', {
'module': moduleName,
@ -115,7 +116,7 @@ var command = function(dbot) {
*/
'listener': function(event) {
var commandName = event.params[0];
if(!dbot.commands.hasOwnProperty(commandName)) {
if(!_.has(dbot.commands, commandName)) {
commandName = '~';
}
@ -138,7 +139,7 @@ var command = function(dbot) {
dbot.save();
} else {
if(commandName !== '~') {
if(dbot.usage.hasOwnProperty(commandName)){
if(_.has(dbot.usage, commandName)) {
event.reply('Usage: ' + dbot.usage[commandName]);
} else {
event.reply(dbot.t('syntax_error'));

View File

@ -16,7 +16,6 @@ var dent = function(dbot) {
},
function(error, response, body) {
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
* this information, since that actually performs the ignorance.
*/
var _ = require('underscore')._;
var ignore = function(dbot) {
var commands = {
'~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 ignorableModules = _.chain(dbot.modules)
.filter(function(module) {
return module.ignorable !== null && module.ignorable === true;
})
.pluck('name')
.value();
if(module === undefined) {
event.reply(dbot.t('ignore_usage', {'user': event.user, 'modules': ignorableModules.join(', ')}));
if(_.isUndefined(module)) {
event.reply(dbot.t('ignore_usage', {
'user': event.user,
'modules': ignorableModules.join(', ')
}));
} else {
if(ignorableModules.include(module)) {
if(dbot.db.ignores.hasOwnProperty(event.user) && dbot.db.ignores[event.user].include(module)) {
event.reply(dbot.t('already_ignoring', {'user': event.user}));
if(_.include(ignorableModules, module)) {
if(_.has(dbot.db.ignores, event.user) && _.include(dbot.db.ignores[event.user], module)) {
event.reply(dbot.t('already_ignoring', { 'user': event.user }));
} else {
if(dbot.db.ignores.hasOwnProperty(module)) {
if(_.has(dbot.db.ignores, module)) {
dbot.db.ignores[event.user].push(module);
} else {
dbot.db.ignores[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 {
event.reply(dbot.t('invalid_ignore', {'user': event.user}));
event.reply(dbot.t('invalid_ignore', { 'user': event.user }));
}
}
},
'~unignore': function(event) {
var ignoredModules = [];
if(dbot.db.ignores.hasOwnProperty(event.user)) {
if(_.has(dbot.db.ignores, event.user)) {
ignoredModules = dbot.db.ignores[event.user];
}
var module = event.params[1];
if(module === undefined) {
event.reply(dbot.t('unignore_usage', {'user': event.user, 'modules': ignoredModules.join(', ')}));
} else {
if(ignoredModules.include(module) == false) {
event.reply(dbot.t('invalid_unignore', {'user': event.user}));
if(_.isUndefined(module)) {
event.reply(dbot.t('unignore_usage', {
'user': event.user,
'modules': ignoredModules.join(', ')
}));
} else {
if(_.include(ignoredModules, module)) {
dbot.db.ignores[event.user].splice(dbot.db.ignores[event.user].indexOf(module), 1);
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() {
dbot.instance.clearIgnores();
for(var user in dbot.db.ignores) {
if(dbot.db.ignores.hasOwnProperty(user)) {
for(var i=0;i<dbot.db.ignores[user].length;i++) {
dbot.instance.ignoreTag(user, dbot.db.ignores[user][i]);
}
}
}
_.each(dbot.db.ignores, function(ignores, user) {
_.each(ignores, function(ignore) {
dbot.instance.ignoreTag(user, ignore);
}, this);
}, this);
}
};
};

View File

@ -1,3 +1,5 @@
var _ = require('underscore')._;
var kick = function(dbot) {
var commands = {
// 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) {
var username = event.params[1];
if(!dbot.db.kicks.hasOwnProperty(username)) {
if(!_.has(dbot.db.kicks, username)) {
var kicks = '0';
} else {
var kicks = dbot.db.kicks[username];
}
if(!dbot.db.kickers.hasOwnProperty(username)) {
if(!_.has(dbot.db.kickers, username)) {
var kicked = '0';
} else {
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
// who have kicked other people the most.
'~kickstats': function(event) {
var orderedKickLeague = function(list, topWhat) {
var kickArr = Object.prototype.sort(list, function(key, obj) { return obj[key]; });
kickArr = kickArr.slice(kickArr.length - 10).reverse();
var kickArr = _.chain(list)
.pairs()
.sortBy(function(kick) { return kick[1] })
.reverse()
.first(10)
.value();
var kickString = "Top " + topWhat + ": ";
for(var i=0;i<kickArr.length;i++) {
@ -48,23 +58,26 @@ var kick = function(dbot) {
'listener': function(event) {
if(event.kickee == dbot.config.name) {
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;
} else {
if(!dbot.db.kicks.hasOwnProperty(event.kickee)) {
if(!_.has(dbot.db.kicks, event.kickee)) {
dbot.db.kicks[event.kickee] = 1;
} else {
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;
} else {
dbot.db.kickers[event.user] += 1;
}
event.reply(event.kickee + '-- (' + dbot.t('user_kicks',
{'user': event.kickee, 'kicks': dbot.db.kicks[event.kickee], 'kicked': dbot.db.kickers[event.kickee]}) + ')');
event.reply(event.kickee + '-- (' + dbot.t('user_kicks', {
'user': event.kickee,
'kicks': dbot.db.kicks[event.kickee],
'kicked': dbot.db.kickers[event.kickee]
}) + ')');
}
},
on: 'KICK'

View File

@ -3,7 +3,9 @@
* Description: Stores recent channel links, with commands to retrieve
* information about links.
*/
var request = require('request');
var request = require('request'),
_ = require('underscore')._;
var link = function(dbot) {
var urlRegex = /(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/ig;
var links = {};
@ -22,7 +24,7 @@ var link = function(dbot) {
var commands = {
'~title': function(event) {
var link = links[event.channel.name];
if(event.params[1] !== undefined) {
if(_.isUndefined(event.params[1])) {
var urlMatches = event.params[1].match(urlRegex);
if(urlMatches !== null) {
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,33 +1,17 @@
var _ = require('underscore')._;
var poll = function(dbot) {
var polls = dbot.db.polls;
var commands = {
'~newpoll': function(event) {
var av = event.input[1] != undefined;
var name = event.input[2];
var options = event.input[3].split(',');
var description = event.input[4];
var name = event.input[1],
options = event.input[2].split(','),
description = event.input[3];
if(name === undefined || name === 'help') {
event.reply(dbot.t('newpoll_usage'));
} else {
if(polls.hasOwnProperty(name)) {
event.reply(dbot.t('poll_exists', {'name': name}));
} else {
if(av) {
polls[name] = {
'av': av,
'name': name,
'description': description,
'owner': event.user,
'votes': {},
'options': []
};
for(var i=0;i<options.length;i++) {
polls[name].options.push(options[i]);
}
if(_.has(polls, name)) {
event.reply(dbot.t('poll_exists', { 'name': name }));
} else {
polls[name] = {
'av': av,
'name': name,
'description': description,
'owner': event.user,
@ -37,32 +21,43 @@ var poll = function(dbot) {
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) {
var name = event.input[1];
var option = event.input[2];
var name = event.input[1],
option = event.input[2];
if(polls.hasOwnProperty(name)) {
if(_.has(polls, name)) {
if(polls[name].owner === event.user) {
if(!polls[name].votes.hasOwnProperty(name)) {
if(!_.has(polls[name].votes, name)) {
polls[name]['votes'][option] = 0;
event.reply(dbot.t('option_added', {'user': event.user,
'name': name, 'option': option}));
event.reply(dbot.t('option_added', {
'user': event.user,
'name': name,
'option': option
}));
} else {
event.reply(dbot.t('option_exists', {'option': option,
'name': name, 'user': event.user}));
event.reply(dbot.t('option_exists', {
'option': option,
'name': name,
'user': event.user
}));
}
} else {
event.reply(dbot.t('not_poll_owner', {'user': event.user,
'name': name}));
event.reply(dbot.t('not_poll_owner', {
'user': event.user,
'name': name
}));
}
} else {
event.reply(dbot.t('poll_unexistent', {'name': name}));
@ -70,147 +65,114 @@ var poll = function(dbot) {
},
'~rmoption': function(event) {
var name = event.input[1];
var option = event.input[2];
var name = event.input[1],
option = event.input[2];
if(polls.hasOwnProperty(name)) {
if(_.has(polls, name)) {
if(polls[name].owner === event.user) {
if(polls[name].votes.hasOwnProperty(option)) {
if(_.has(polls[name].votes, option)) {
delete polls[name]['votes'][option];
event.reply(dbot.t('option_removed', {'user': event.user,
'name': name, 'option': option}));
event.reply(dbot.t('option_removed', {
'user': event.user,
'name': name,
'option': option
}));
} else {
event.reply(dbot.t('invalid_vote', {'vote': option}));
event.reply(dbot.t('invalid_vote', { 'vote': option }));
}
} else {
event.reply(dbot.t('not_poll_owner', {'name': name}));
event.reply(dbot.t('not_poll_owner', { 'name': name }));
}
} else {
event.reply(dbot.t('poll_unexistent', {'name': name}));
event.reply(dbot.t('poll_unexistent', { 'name': name }));
}
},
'~vote': function(event) {
var name = event.input[1];
var vote = event.input[2];
var name = event.input[1],
vote = event.input[2];
if(polls.hasOwnProperty(name)) {
if(polls[name].av) {
var prefs = vote.split(',');
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)) {
if(_.has(polls, name)) {
if(_.has(polls[name].votes, vote)) {
if(_.has(polls[name].votees, event.user)) {
var oldVote = polls[name].votees[event.user];
polls[name].votes[oldVote]--;
polls[name].votes[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 {
polls[name].votes[vote]++;
polls[name].votees[event.user] = vote;
event.reply(dbot.t('voted', {'vote': vote, 'poll': name,
'count': polls[name].votes[vote], 'user': event.user}));
event.reply(dbot.t('voted', {
'vote': vote,
'poll': name,
'count': polls[name].votes[vote],
'user': event.user
}));
}
} else {
event.reply(dbot.t('invalid_vote', {'vote': vote}));
}
event.reply(dbot.t('invalid_vote', { 'vote': vote }));
}
} else {
event.reply(dbot.t('poll_unexistent', {'name': name}));
event.reply(dbot.t('poll_unexistent', { 'name': name }));
}
},
'~pdesc': function(event) {
var name = event.input[1];
if(polls.hasOwnProperty(name)) {
event.reply(dbot.t('poll_describe', {'name': name, 'description': polls[name].description,
'url': dbot.t('url', {'host': dbot.config.web.webHost, 'port':
dbot.config.web.webPort, 'path': 'polls/' + name})}));
if(_.has(polls, name)) {
event.reply(dbot.t('poll_describe', {
'name': name,
'description': polls[name].description,
'url': dbot.t('url', {
'host': dbot.config.web.webHost,
'port': dbot.config.web.webPort,
'path': 'polls/' + name
})
}));
} else {
event.reply(dbot.t('poll_unexistent', {'name': name}));
event.reply(dbot.t('poll_unexistent', { 'name': name }));
}
},
'~count': function(event) {
var name = event.input[1];
if(polls.hasOwnProperty(name)) {
if(_.has(polls, name)) {
var order;
if(polls[name].av) {
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()
.sortBy(function(option) { return option[1] })
.reverse()
.value();
order = votesArr.map(function(vote) { return vote[0]; });
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 {
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['~rmoption'].regex = [/~rmoption ([^ ]+) ([^ ]+)/, 3];
commands['~vote'].regex = [/~vote ([^ ]+) ([^ ]+)/, 3];
@ -221,24 +183,35 @@ var poll = function(dbot) {
// Shows the results of a poll
'/polls/:key': function(req, res) {
var key = req.params.key.toLowerCase();
if(dbot.db.polls.hasOwnProperty(key) && dbot.db.polls[key].hasOwnProperty('description')) {
// tally the votes
var totalVotes = 0;
for( var v in dbot.db.polls[key].votes ) {
var N = Number(dbot.db.polls[key].votes[v]);
if( !isNaN(N) ) {
totalVotes += N;
if(_.has(dbot.db.polls, key)) {
var totalVotes = _.reduce(dbot.db.polls[key].votes,
function(memo, option) {
return memo += option;
}, 0);
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()
}
}
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() } });
});
} 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
'/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 name = 'quotes';
var quotes = dbot.db.quoteArrs;
var addStack = [];
var rmAllowed = true;
var quotes = dbot.db.quoteArrs,
addStack = [],
rmAllowed = true,
rmCache = dbot.sessionData.rmCache,
rmTimer;
dbot.sessionData.rmCache = [];
var rmCache = dbot.sessionData.rmCache;
var rmTimer;
// Retrieve a random quote from a given category, interpolating any quote
// references (~~QUOTE CATEGORY~~) within it
var interpolatedQuote = function(event, key, quoteTree) {
if(quoteTree !== undefined && quoteTree.indexOf(key) != -1) {
if(!_.isUndefined(quoteTree) && quoteTree.indexOf(key) != -1) {
return '';
} else if(quoteTree === undefined) {
} else if(_.isUndefined(quoteTree)) {
quoteTree = [];
}
var quoteString = quotes[key].random();
var index = _.random(0, quotes[key].length - 1);
var quoteString = quotes[key][index];
// Parse quote interpolations
var quoteRefs = quoteString.match(/~~([\d\w\s-]*)~~/g);
@ -25,10 +27,10 @@ var quotes = function(dbot) {
while(quoteRefs && (thisRef = quoteRefs.shift()) !== undefined) {
var cleanRef = dbot.cleanNick(thisRef.replace(/^~~/,'').replace(/~~$/,'').trim());
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);
quoteTree.pop();
} else if(quotes.hasOwnProperty(cleanRef)) {
} else if(_.has(quotes, cleanRef)) {
quoteTree.push(key);
quoteString = quoteString.replace("~~" + cleanRef + "~~",
interpolatedQuote(event, cleanRef, quoteTree.slice()));
@ -45,17 +47,20 @@ var quotes = function(dbot) {
rmAllowed = true;
});
rmCache.push({'key': key, 'quote': quote});
rmCache.push({
'key': key,
'quote': quote
});
dbot.timers.clearTimeout(rmTimer);
if(rmCache.length < dbot.config.quotes.rmLimit) {
rmTimer = dbot.timers.addOnceTimer(600000, function() {
rmCache.length = 0; // lol what
});
} else {
for(var i=0;i<dbot.config.admins.length;i++) {
dbot.say(event.server, dbot.config.admins[i],
dbot.t('rm_cache_limit'));
}
_.each(dbot.config.admins, function(admin) {
dbot.say(event.server, admin, dbot.t('rm_cache_limit'));
});
}
};
@ -68,9 +73,9 @@ var quotes = function(dbot) {
}
if(key.charAt(0) !== '_') { // lol
if(quotes.hasOwnProperty(key)) {
if(_.has(quotes, key)) {
return interpolatedQuote(event, key);
} else if(quotes.hasOwnProperty(altKey)) {
} else if(_.has(quotes, altKey)) {
return interpolatedQuote(event, altKey);
} else {
return false;
@ -106,7 +111,7 @@ var quotes = function(dbot) {
'~rmdeny': function(event) {
var rmCacheCount = rmCache.length;
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].push(rmCache[i].quote);
@ -131,12 +136,16 @@ var quotes = function(dbot) {
// Shows a list of the biggest categories
'~qstats': function(event) {
var qSizes = Object.prototype.sort(quotes, function(key, obj) { return obj[key].length });
qSizes = qSizes.slice(qSizes.length - 10).reverse();
var qSizes = _.chain(quotes)
.pairs()
.sortBy(function(category) { return category[1].length })
.reverse()
.first(10)
.value();
var qString = dbot.t('large_categories');
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));
@ -146,19 +155,20 @@ var quotes = function(dbot) {
'~qsearch': function(event) {
var haystack = event.input[1].trim().toLowerCase();
var needle = event.input[2];
if(quotes.hasOwnProperty(haystack)) {
var matches = [];
quotes[haystack].each(function(quote) {
if(quote.indexOf(needle) != -1) {
matches.push(quote);
}
}.bind(this));
if(_.has(quotes, haystack)) {
var matches = _.filter(quotes[haystack], function(quote) {
return _.indexOf(quote, needle) != -1;
}, this);
if(matches.length == 0) {
event.reply(dbot.t('no_results'));
} else {
event.reply(dbot.t('search_results', {'category': haystack, 'needle': needle,
'quote': matches.random(), 'matches': matches.length}));
event.reply(dbot.t('search_results', {
'category': haystack,
'needle': needle,
'quote': matches.random(),
'matches': matches.length
}));
}
} else {
event.reply(dbot.t('empty_category'));
@ -166,16 +176,19 @@ var quotes = function(dbot) {
},
'~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();
if(quotes.hasOwnProperty(key)) {
if(_.has(quotes, key)) {
var quote = quotes[key].pop();
if(quotes[key].length === 0) {
delete quotes[key];
}
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 {
event.reply(dbot.t('no_quotes', {'category': q[1]}));
}
@ -185,11 +198,11 @@ var quotes = function(dbot) {
},
'~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 quote = event.input[2];
if(quotes.hasOwnProperty(key)) {
if(_.has(quotes, key)) {
var category = quotes[key];
var index = category.indexOf(quote);
if(index !== -1) {
@ -215,51 +228,59 @@ var quotes = function(dbot) {
var input = event.message.valMatch(/^~qcount ([\d\w\s-]*)/, 2);
if(input) { // Give quote count for named category
var key = input[1].trim().toLowerCase();
if(quotes.hasOwnProperty(key)) {
event.reply(dbot.t('quote_count', {'category': key, 'count': quotes[key].length}));
if(_.has(quotes, key)) {
event.reply(dbot.t('quote_count', {
'category': key,
'count': quotes[key].length
}));
} else {
event.reply(dbot.t('no_quotes', {'category': key}));
event.reply(dbot.t('no_quotes', { 'category': key }));
}
} else { // Give total quote count
var totalQuoteCount = 0;
for(var category in quotes) {
if(quotes.hasOwnProperty(category)) {
totalQuoteCount += quotes[category].length;
}
}
event.reply(dbot.t('total_quotes', {'count': totalQuoteCount}));
var totalQuoteCount = _.reduce(quotes, function(memo, category) {
return memo + category.length;
}, 0);
event.reply(dbot.t('total_quotes', { 'count': totalQuoteCount }));
}
},
'~qadd': function(event) {
var key = event.input[1].toLowerCase();
var text = event.input[2];
if(!Object.isArray(quotes[key])) {
if(!_.isArray(quotes[key])) {
quotes[key] = [];
}
if(quotes[key].include(text)) {
if(_.include(quotes[key], text)) {
event.reply(dbot.t('quote_exists'));
} else {
quotes[key].push(text);
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) {
var rQuote = Object.keys(quotes).random();
event.reply(rQuote + ': ' + interpolatedQuote(event, rQuote));
var category = _.keys(quotes)[_.random(0, _.size(quotes) -1)];
event.reply(category + ': ' + interpolatedQuote(event, category));
},
'~link': function(event) {
var key = event.params[1].trim().toLowerCase();
if(quotes.hasOwnProperty(key)) {
event.reply(dbot.t('quote_link', {'category': key,
'url': dbot.t('url', {'host': dbot.config.web.webHost,
'port': dbot.config.web.webPort, 'path': 'quotes/' + key})}));
if(_.has(quotes, key)) {
event.reply(dbot.t('quote_link', {
'category': key,
'url': dbot.t('url', {
'host': dbot.config.web.webHost,
'port': dbot.config.web.webPort,
'path': 'quotes/' + key
})
}));
} 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
'/quotes/:key': function(req, res) {
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() } });
} else {
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 commands = {
'~report': function(event) {
@ -5,29 +7,21 @@ var report = function(dbot) {
var nick = event.input[2];
var reason = event.input[3];
if(event.allChannels.hasOwnProperty(channelName)) {
if(_.has(event.allChannels, channelName)) {
var channel = event.allChannels[channelName];
if(channel.nicks.hasOwnProperty(nick)) {
var ops = [];
for(var possibOps in channel.nicks) {
if(channel.nicks[possibOps].op == true) {
ops.push(possibOps);
}
}
if(_.has(channel.nicks, nick)) {
var ops = _.filter(channel.nicks, function(user) {
return user.op;
});
// Does the channel have an admin channel?
if(event.allChannels.hasOwnProperty('#' + channelName)) {
ops.push('#' + channelName);
}
for(var i=0;i<ops.length;i++) {
dbot.say(event.server, ops[i], dbot.t('report', {
_.each(ops, function(user) {
dbot.say(event.server, user.name, dbot.t('report', {
'reporter': event.user,
'reported': nick,
'channel': channelName,
'reason': reason
}));
}
}, this);
event.reply(dbot.t('reported', { 'reported': nick }));
} 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 last = {};
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 = [];
for(var i=0;i<rawCandidates.length;i++) {
candidates.push(rawCandidates[i].join(' '));
@ -11,10 +86,10 @@ var spelling = function(dbot) {
var winnerDistance = Infinity;
for(var i=0;i<candidates.length;i++) {
var distance = String.prototype.distance(correction.toLowerCase(), candidates[i].toLowerCase());
if((distance < winnerDistance) && (distance > 0)) {
var d = distance(correction.toLowerCase(), candidates[i].toLowerCase());
if((d < winnerDistance) && (d > 0)) {
winner = candidates[i];
winnerDistance = distance;
winnerDistance = d;
}
}
@ -51,7 +126,7 @@ var spelling = function(dbot) {
event.reply(dbot.t('spelling_other', e));
});
} else {
if(last.hasOwnProperty(event.channel.name)) {
if(_.has(last, event.channel.name)) {
last[event.channel.name][event.user] = event.message;
} else {
last[event.channel.name] = { };

View File

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

View File

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

145
run.js
View File

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

View File

@ -35,19 +35,6 @@ Array.prototype.sum = function() {
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() {
var hash = {}
var result = [];
@ -79,66 +66,6 @@ String.prototype.startsWith = function(needle) {
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
var targetStr = this;
var replacements = [].splice.call(arguments, 0);