diff --git a/modules/admin.js b/modules/admin.js index 36e3c1e..3c15948 100644 --- a/modules/admin.js +++ b/modules/admin.js @@ -1,147 +1,136 @@ +/** + * Module Name: Admin + * 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 adminCommands = function(dbot) { - var dbot = dbot; - +var admin = function(dbot) { var commands = { - 'join': function(data, params) { - dbot.instance.join(params[1]); - dbot.say(data.channel, 'Joined ' + params[1]); + // Join a channel + 'join': function(event) { + var channel = event.params[1]; + dbot.instance.join(event, channel); + event.reply(dbot.t('join', {'channel': channel})); }, - 'opme': function(data, params) { - dbot.instance.send('MODE ' + params[1] + ' +o ', data.user); + // Leave a channel + 'part': function(event) { + var channel = event.params[1]; + event.instance.part(event, channel); + event.reply(dbot.t('part', {'channel': channel})); }, - 'part': function(data, params) { - dbot.instance.part(params[1]); + // Op admin caller in given channel + 'opme': function(event) { + event.channel = event.params[1]; + dbot.instance.mode(event, '+o ' + event.user); }, // Do a git pull and reload - 'greload': function(data, params) { - var child; - - child = exec("git pull", function (error, stdout, stderr) { - console.log(stderr); - dbot.say(data.channel, dbot.t('gpull')); - commands.reload(data, params); + 'greload': function(event) { + var child = exec("git pull", function (error, stdout, stderr) { + event.reply(dbot.t('gpull')); + commands.reload(event); }.bind(this)); }, - 'reload': function(data, params) { + // Reload DB, translations and modules. + 'reload': function(event) { dbot.db = JSON.parse(fs.readFileSync('db.json', 'utf-8')); dbot.strings = JSON.parse(fs.readFileSync('strings.json', 'utf-8')); dbot.reloadModules(); - dbot.say(data.channel, dbot.t('reload')); + event.reply(dbot.t('reload')); }, - 'say': function(data, params) { - if (params[1] === "@") { - var c = data.channel; - } else { - var c = params[1]; - } - var m = params.slice(2).join(' '); - dbot.say(c, m); + // Say something in a channel (TODO probably doesn't work.) + 'say': function(event) { + var channel = event.params[1]; + if(event.params[1] === "@") { + var channel = event.channel; + } + var message = event.params.slice(2).join(' '); + dbot.say(event.server, channel, message); }, - 'act': function(data, params) { - if (params[1] === "@") { - var c = data.channel; - } else { - var c = params[1]; - } - var m = params.slice(2).join(' '); - dbot.act(c, m); - }, - - 'load': function(data, params) { - dbot.moduleNames.push(params[1]); + // Load new module + 'load': function(event) { + var moduleName = event.params[1]; + dbot.moduleNames.push(moduleName); dbot.reloadModules(); - dbot.say(data.channel, dbot.t('load_module', {'moduleName': params[1]})); + event.reply(dbot.t('load_module', {'moduleName': moduleName})); }, - 'unload': function(data, params) { - if(dbot.moduleNames.include(params[1])) { - var cacheKey = require.resolve('../modules/' + params[1]); + // Unload a loaded module + 'unload': function(event) { + var moduleName = event.params[1]; + if(dbot.moduleNames.include(moduleName)) { + var cacheKey = require.resolve('../modules/' + moduleName); delete require.cache[cacheKey]; - var moduleIndex = dbot.moduleNames.indexOf(params[1]); + var moduleIndex = dbot.moduleNames.indexOf(moduleName); dbot.moduleNames.splice(moduleIndex, 1); - dbot.reloadModules(); - dbot.say(data.channel, dbot.t('unload_module', {'moduleName': params[1]})); + + event.reply(dbot.t('unload_module', {'moduleName': moduleName})); } else { - dbot.say(data.channel, dbot.t('unload_error', {'moduleName': params[1]})); + event.reply(dbot.t('unload_error', {'moduleName': moduleName})); } }, - 'ban': function(data, params) { - if(dbot.db.bans.hasOwnProperty(params[2])) { - dbot.db.bans[params[2]].push(params[1]); - } else { - dbot.db.bans[params[2]] = [ params[1] ]; + // Ban user from command or * + 'ban': function(event) { + var username = event.params[1]; + var command = event.params[2]; + + if(!dbot.db.bans.hasOwnProperty(command)) { + dbot.db.bans[command] = [ ]; } - dbot.say(data.channel, dbot.t('banned', {'user': params[1], 'command': params[2]})); + dbot.db.bans[command].push(username); + event.reply(dbot.t('banned', {'user': username, 'command': command})); }, - 'unban': function(data, params) { - if(dbot.db.bans.hasOwnProperty(params[2]) && dbot.db.bans[params[2]].include(params[1])) { - dbot.db.bans[params[2]].splice(dbot.db.bans[params[2]].indexOf(params[1]), 1); - dbot.say(data.channel, dbot.t('unbanned', {'user': params[1], 'command': params[2]})); + // Unban a user from command or * + '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); + event.reply(dbot.t('unbanned', {'user': username, 'command': command})); } else { - dbot.say(data.channel, dbot.t('unban_error', {'user': params[1]})); + event.reply(dbot.t('unban_error', {'user': username})); } }, - 'modehate': function(data, params) { - dbot.db.modehate.push(params[1]); - dbot.say(data.channel, dbot.t('modehate', {'user': params[1]})); - }, - - 'unmodehate': function(data, params) { - dbot.db.modehate.splice(dbot.db.modehate.indexOf(params[1]), 1); - dbot.say(data.channel, dbot.t('unmodehate', {'user': params[1]})); - }, - - 'lock': function(data, params) { - dbot.db.locks.push(params[1]); - dbot.say(data.channel, dbot.t('qlock', {'category': params[1]})); + // Lock quote category so quotes can't be removed + 'lock': function(event) { + var category = event.params[1]; + dbot.db.locks.push(category); + event.reply(dbot.t('qlock', {'category': category})); } }; return { - // These commands are implemented as their own listener so it can easily - // check whether the user is the admin, and so that the admin can't ban - // themselves and then not be able to rectify it... - 'listener': function(data) { - if(data.channel == dbot.name) data.channel = data.user; + 'name': 'admin', - params = data.message.split(' '); - if(commands.hasOwnProperty(params[0]) && dbot.admin.include(data.user)) { - commands[params[0]](data, params); + /** + * Run the appropriate admin command given the input (and user). + */ + 'listener': function(event) { + var commandName = event.params[0]; + if(commands.hasOwnProperty(commandName) && dbot.admin.include(event.user)) { + commands[commandName](event); dbot.save(); } }, - 'onLoad': function() { - return { - '~resetadmin': function(data, params) { - dbot.admin = dbot.config.admin; - } - } - }, - 'on': 'PRIVMSG', - - 'name': 'admin', - 'ignorable': false }; }; exports.fetch = function(dbot) { - return adminCommands(dbot); + return admin(dbot); }; diff --git a/modules/modehate.js b/modules/modehate.js deleted file mode 100644 index 4bc25eb..0000000 --- a/modules/modehate.js +++ /dev/null @@ -1,23 +0,0 @@ -var modehate = function(dbot) { - var dbot = dbot; - - return { - 'listener': function(data, params) { - if(data.raw[0].indexOf('-oooo') != -1) { - dbot.instance.send('KICK #42 ' + data.user + ' :gtfo - mass deop protection'); - } else if(dbot.db.modehate.include(data.user) && data.raw[0].indexOf('-o') != -1) { - dbot.instance.send('KICK ' + data.channel + ' ' + data.user + ' :gtfo'); - } - }, - - 'on': 'MODE', - - 'name': 'modehate', - - 'ignorable': false - }; -}; - -exports.fetch = function(dbot) { - return modehate(dbot); -}; diff --git a/run.js b/run.js index d0fbbc4..3f7af54 100644 --- a/run.js +++ b/run.js @@ -53,7 +53,7 @@ var DBot = function(timers) { // Populate bot properties with config data this.name = this.config.name || 'dbox'; this.admin = this.config.admin || [ 'reality' ]; - this.moduleNames = this.config.modules || [ 'command', 'js', 'quotes' ]; + this.moduleNames = this.config.modules || [ 'command', 'js', 'quotes', 'admin' ]; this.language = this.config.language || 'english'; // It's the user's responsibility to fill this data structure up properly in @@ -89,8 +89,8 @@ var DBot = function(timers) { }; // Say something in a channel -DBot.prototype.say = function(channel, data) { - this.instance.say(channel, data); +DBot.prototype.say = function(server, channel, message) { + this.instance.say(server, channel, message); }; // Format given stored string in config language @@ -103,9 +103,9 @@ DBot.prototype.t = function(string, formatData) { return this.strings[string][lang].format(formatData); }; -DBot.prototype.act = function(channel, data) { +/*DBot.prototype.act = function(channel, data) { this.instance.send('PRIVMSG', channel, ':\001ACTION ' + data + '\001'); -} +}*/ // Save the database file DBot.prototype.save = function() { diff --git a/strings.json b/strings.json index 6761598..e624319 100644 --- a/strings.json +++ b/strings.json @@ -189,5 +189,11 @@ }, "search_results": { "english": "{category} ({needle}): '{quote}' [{matches} results]" + }, + "join": { + "english": "Joined {channel}" + }, + "part": { + "english": "Left {channel}" } }