From e68cf45e65c7d5358c9fd3dc29ff4d37a6ccbcfe Mon Sep 17 00:00:00 2001 From: reality Date: Sat, 12 Jan 2013 10:38:40 +0000 Subject: [PATCH 01/15] Few tests and that --- run.js | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/run.js b/run.js index 22e7e0b..7f36b9b 100644 --- a/run.js +++ b/run.js @@ -1,6 +1,7 @@ -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) { @@ -17,13 +18,13 @@ var DBot = function(timers) { process.exit(); } } - requiredConfigKeys.each(function(key) { - if(!this.config.hasOwnProperty(key)) { + _.each(requiredConfigKeys, function(key) { + if(!_.has(this.config, key)) { console.log('Error: Please set a value for ' + key + ' in ' + 'config.json. Stopping.'); process.exit(); } - }.bind(this)); + }, this); var rawDB; try { @@ -58,7 +59,7 @@ var DBot = function(timers) { // 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)) { + if(_.has(this.config.servers, name)) { var server = this.config.servers[name]; this.instance.addConnection(name, server.server, server.port, this.config.admin, function(event) { From d9f8ff0c1f19a85c77b936da19fdce5470f03898 Mon Sep 17 00:00:00 2001 From: reality Date: Sat, 12 Jan 2013 14:58:54 +0000 Subject: [PATCH 02/15] lots of underscorisation in module loading --- run.js | 92 +++++++++++++++++++++------------------------------------- 1 file changed, 33 insertions(+), 59 deletions(-) diff --git a/run.js b/run.js index 7f36b9b..8e08e42 100644 --- a/run.js +++ b/run.js @@ -164,17 +164,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 Date: Sat, 12 Jan 2013 15:07:14 +0000 Subject: [PATCH 03/15] Load missing config directives from defaults file --- run.js | 31 ++++++++++++++----------------- 1 file changed, 14 insertions(+), 17 deletions(-) diff --git a/run.js b/run.js index 8e08e42..8e03fe9 100644 --- a/run.js +++ b/run.js @@ -5,26 +5,23 @@ var fs = require('fs'), 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(); } - _.each(requiredConfigKeys, function(key) { - if(!_.has(this.config, key)) { - console.log('Error: Please set a value for ' + key + ' in ' + - 'config.json. Stopping.'); - process.exit(); - } - }, 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 { @@ -172,7 +169,7 @@ DBot.prototype.reloadModules = function() { this.config[name] = config; _.each(config.dbKeys, function(dbKey) { - if(!_.has(this.db, dbKey( { + if(!_.has(this.db, dbKey)) { this.db[dbKey] = {}; } }, this); From 0769d0bd69b6f20c9f9746d12b4c5ed46b68a429 Mon Sep 17 00:00:00 2001 From: reality Date: Sat, 12 Jan 2013 16:14:17 +0000 Subject: [PATCH 04/15] Removed dependency on commandMap (I think ignore might still look at it for now). Underscorised command. [#81] --- modules/command/command.js | 29 +++++++++++++++-------------- run.js | 16 +++++++++------- 2 files changed, 24 insertions(+), 21 deletions(-) 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/run.js b/run.js index 8e03fe9..def9a5a 100644 --- a/run.js +++ b/run.js @@ -81,9 +81,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"; } @@ -257,18 +257,20 @@ DBot.prototype.reloadModules = function() { 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); From abaa1e7fa24963d19760a38ad0db710945e50618 Mon Sep 17 00:00:00 2001 From: reality Date: Sat, 12 Jan 2013 17:36:59 +0000 Subject: [PATCH 05/15] underscorise admin.js [#81] --- modules/admin/admin.js | 33 +++++++++++++++++++-------------- 1 file changed, 19 insertions(+), 14 deletions(-) diff --git a/modules/admin/admin.js b/modules/admin/admin.js index 30d9188..bb362ac 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,15 @@ 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 = _.reject(moduleNames, function(module) { + return module == moduleName; + }, this); + dbot.reloadModules(); event.reply(dbot.t('unload_module', {'moduleName': moduleName})); @@ -136,7 +139,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 +150,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})); From 08bd67f4f04d6a73e883a3d7beb7c5847348a9aa Mon Sep 17 00:00:00 2001 From: reality Date: Sat, 12 Jan 2013 21:40:16 +0000 Subject: [PATCH 06/15] incomplete underscorisation in quotes [#81] --- modules/quotes/quotes.js | 135 ++++++++++++++++++++++----------------- run.js | 21 +++--- 2 files changed, 87 insertions(+), 69 deletions(-) diff --git a/modules/quotes/quotes.js b/modules/quotes/quotes.js index a6baab7..96524c5 100644 --- a/modules/quotes/quotes.js +++ b/modules/quotes/quotes.js @@ -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 Date: Sat, 12 Jan 2013 21:54:02 +0000 Subject: [PATCH 07/15] Underscorisation of report module [#81] --- modules/report/report.js | 26 ++++++++++---------------- 1 file changed, 10 insertions(+), 16 deletions(-) diff --git a/modules/report/report.js b/modules/report/report.js index 77100a7..4a6b09c 100644 --- a/modules/report/report.js +++ b/modules/report/report.js @@ -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 Date: Sat, 12 Jan 2013 22:19:48 +0000 Subject: [PATCH 08/15] underscorisation for dent and ignore [#81] --- modules/dent/dent.js | 1 - modules/ignore/ignore.js | 67 ++++++++++++++++++++++++---------------- 2 files changed, 40 insertions(+), 28 deletions(-) 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 Date: Sat, 12 Jan 2013 22:53:13 +0000 Subject: [PATCH 09/15] underscorisation of kick [#81] --- modules/kick/kick.js | 33 +++++++++++++++++++++++---------- 1 file changed, 23 insertions(+), 10 deletions(-) diff --git a/modules/kick/kick.js b/modules/kick/kick.js index 150fb55..3480583 100644 --- a/modules/kick/kick.js +++ b/modules/kick/kick.js @@ -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 Date: Sat, 12 Jan 2013 23:31:53 +0000 Subject: [PATCH 10/15] Underscorise poll. Unfortunately some stuff went missing in the switch-over. Whoops. Oh I also fixed it. [#81] --- modules/poll/config.json | 3 +- modules/poll/poll.js | 287 ++++++++++++++++++--------------------- 2 files changed, 132 insertions(+), 158 deletions(-) diff --git a/modules/poll/config.json b/modules/poll/config.json index d3d8ca6..10db064 100644 --- a/modules/poll/config.json +++ b/modules/poll/config.json @@ -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" ] } diff --git a/modules/poll/poll.js b/modules/poll/poll.js index 988664f..0ab8cda 100644 --- a/modules/poll/poll.js +++ b/modules/poll/poll.js @@ -1,68 +1,63 @@ +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')); + if(_.has(polls, name)) { + event.reply(dbot.t('poll_exists', { 'name': name })); } 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 Date: Sun, 13 Jan 2013 15:26:44 +0000 Subject: [PATCH 11/15] Underscorise users module (was not pleasant) [#81] --- modules/admin/admin.js | 6 +-- modules/users/users.js | 104 +++++++++++++++++++++-------------------- 2 files changed, 54 insertions(+), 56 deletions(-) diff --git a/modules/admin/admin.js b/modules/admin/admin.js index bb362ac..d2cc575 100644 --- a/modules/admin/admin.js +++ b/modules/admin/admin.js @@ -121,11 +121,7 @@ var admin = function(dbot) { var moduleDir = '../' + moduleName + '/'; var cacheKey = require.resolve(moduleDir + moduleName); delete require.cache[cacheKey]; - - dbot.config.moduleNames = _.reject(moduleNames, function(module) { - return module == moduleName; - }, this); - + dbot.config.moduleNames = _.without(dbot.config.moduleNames, moduleName); dbot.reloadModules(); event.reply(dbot.t('unload_module', {'moduleName': moduleName})); diff --git a/modules/users/users.js b/modules/users/users.js index b96f9c8..3f82695 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,7 +134,7 @@ var users = function(dbot) { var primaryUser = event.params[1]; 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.aliases[secondaryUser] = primaryUser; updateAliases(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(); + }); } }; }; From f6d69fe137af74a2d187daf4c77aa49564248fb7 Mon Sep 17 00:00:00 2001 From: reality Date: Sun, 13 Jan 2013 15:27:45 +0000 Subject: [PATCH 12/15] without instead o splice --- modules/users/users.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/users/users.js b/modules/users/users.js index 3f82695..9ea6fdd 100644 --- a/modules/users/users.js +++ b/modules/users/users.js @@ -135,7 +135,7 @@ var users = function(dbot) { var secondaryUser = event.params[2]; 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; updateAliases(event, secondaryUser, primaryUser); updateChannels(event, secondaryUser, primaryUser); From 35e34874af3ac808356dfc04ee99270404cbd662 Mon Sep 17 00:00:00 2001 From: reality Date: Sun, 13 Jan 2013 15:30:53 +0000 Subject: [PATCH 13/15] link underscorised (easy) [#81] --- modules/link/link.js | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/modules/link/link.js b/modules/link/link.js index 7b6d3b2..0c275eb 100644 --- a/modules/link/link.js +++ b/modules/link/link.js @@ -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]; From cd5649a1e8b20ffea751989d0b0d99d7655f60a8 Mon Sep 17 00:00:00 2001 From: reality Date: Sun, 13 Jan 2013 15:45:07 +0000 Subject: [PATCH 14/15] underscorise spelling [#81] --- modules/spelling/spelling.js | 87 +++++++++++++++++++++++++++++++++--- snippets.js | 73 ------------------------------ 2 files changed, 81 insertions(+), 79 deletions(-) diff --git a/modules/spelling/spelling.js b/modules/spelling/spelling.js index 49b0403..5c955ef 100644 --- a/modules/spelling/spelling.js +++ b/modules/spelling/spelling.js @@ -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 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/snippets.js b/snippets.js index 6f703cd..f8fcfe4 100644 --- a/snippets.js +++ b/snippets.js @@ -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); From b512cc7ee98078ad570a5e9bb51010e16a37be0c Mon Sep 17 00:00:00 2001 From: reality Date: Sun, 13 Jan 2013 15:51:54 +0000 Subject: [PATCH 15/15] web underscorised [#81] --- modules/web/web.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) 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) {