diff --git a/modules/admin/admin.js b/modules/admin/admin.js index 30d9188..d2cc575 100644 --- a/modules/admin/admin.js +++ b/modules/admin/admin.js @@ -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})); diff --git a/modules/command/command.js b/modules/command/command.js index 492358b..968e578 100644 --- a/modules/command/command.js +++ b/modules/command/command.js @@ -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')); diff --git a/modules/dent/dent.js b/modules/dent/dent.js index 2361cfa..3d5844e 100644 --- a/modules/dent/dent.js +++ b/modules/dent/dent.js @@ -16,7 +16,6 @@ var dent = function(dbot) { }, function(error, response, body) { event.reply('Status posted (probably).'); - console.log(body); }); } }; diff --git a/modules/ignore/ignore.js b/modules/ignore/ignore.js index 14fdd5a..a3c99cd 100644 --- a/modules/ignore/ignore.js +++ b/modules/ignore/ignore.js @@ -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(', ')})); + if(_.isUndefined(module)) { + 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})); - } 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 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] = { }; diff --git a/modules/users/users.js b/modules/users/users.js index b96f9c8..9ea6fdd 100644 --- a/modules/users/users.js +++ b/modules/users/users.js @@ -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) { - knownUsers.aliases[alias] = newUser; - } + _.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(); + }); } }; }; diff --git a/modules/web/web.js b/modules/web/web.js index 234bfa3..b3e632b 100644 --- a/modules/web/web.js +++ b/modules/web/web.js @@ -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) { diff --git a/run.js b/run.js index 22e7e0b..f9418dd 100644 --- a/run.js +++ b/run.js @@ -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.'); - try { - this.config = JSON.parse(fs.readFileSync('config.json.sample', 'utf-8')); - } catch(err) { - console.log('Error loading sample config. Bugger off. Stopping.'); - process.exit(); - } + console.log('Config file is invalid. 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)); + + try { + var defaultConfig = JSON.parse(fs.readFileSync('config.json.sample', 'utf-8')); + } catch(err) { + console.log('Error loading sample config. Bugger off this should not even be edited. Stopping.'); + process.exit(); + } + + // Load missing config directives from sample file + _.defaults(this.config, defaultConfig); var rawDB; try { @@ -57,18 +55,15 @@ 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]; - this.instance.addConnection(name, server.server, server.port, - this.config.admin, function(event) { - var server = this.config.servers[event.server]; - for(var i=0;i