2012-12-11 17:04:52 +01:00
|
|
|
/**
|
|
|
|
* 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 admin = function(dbot) {
|
|
|
|
var commands = {
|
|
|
|
// Join a channel
|
|
|
|
'join': function(event) {
|
|
|
|
var channel = event.params[1];
|
|
|
|
if(event.allChannels.hasOwnProperty(channel)) {
|
2012-12-30 17:41:10 +01:00
|
|
|
event.reply(dbot.t('already_in_channel', {'channel': channel}));
|
2012-12-11 17:04:52 +01:00
|
|
|
} else {
|
|
|
|
dbot.instance.join(event, channel);
|
|
|
|
event.reply(dbot.t('join', {'channel': channel}));
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
// Leave a channel
|
|
|
|
'part': function(event) {
|
|
|
|
var channel = event.params[1];
|
|
|
|
if(!event.allChannels.hasOwnProperty(channel)) {
|
2012-12-30 17:41:10 +01:00
|
|
|
event.reply(dbot.t('not_in_channel', {'channel': channel}));
|
2012-12-11 17:04:52 +01:00
|
|
|
} else {
|
|
|
|
event.instance.part(event, channel);
|
|
|
|
event.reply(dbot.t('part', {'channel': channel}));
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
// Op admin caller in given channel
|
|
|
|
'opme': function(event) {
|
|
|
|
var channel = event.params[1];
|
|
|
|
|
|
|
|
// If given channel isn't valid just op in current one.
|
|
|
|
if(!event.allChannels.hasOwnProperty(channel)) {
|
|
|
|
channel = event.channel.name;
|
|
|
|
}
|
|
|
|
dbot.instance.mode(event, channel, ' +o ' + event.user);
|
|
|
|
},
|
|
|
|
|
|
|
|
// Do a git pull and reload
|
|
|
|
'greload': function(event) {
|
2013-01-04 17:13:45 +01:00
|
|
|
exec("git pull", function (error, stdout, stderr) {
|
|
|
|
exec("git submodule update", function (error, stdout, stderr) {
|
|
|
|
event.reply(dbot.t('gpull'));
|
|
|
|
commands.reload(event);
|
2013-01-05 23:20:09 +01:00
|
|
|
event.message = 'version';
|
|
|
|
event.action = 'PRIVMSG';
|
|
|
|
event.params = event.message.split(' ');
|
|
|
|
dbot.instance.emit(event);
|
2013-01-04 17:13:45 +01:00
|
|
|
}.bind(this));
|
2012-12-11 17:04:52 +01:00
|
|
|
}.bind(this));
|
2013-01-05 23:04:59 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
// Display commit information for part of dbot
|
|
|
|
'version': function(event){
|
|
|
|
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])){
|
|
|
|
cmd += "modules/"+input;
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
cmd += input;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
exec(cmd, function(error, stdout, stderr){
|
|
|
|
if(stdout.length > 0){
|
|
|
|
event.reply(stdout);
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
event.reply("No version information or queried module not loaded");
|
|
|
|
}
|
|
|
|
}.bind(this));
|
2012-12-11 17:04:52 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
// Reload DB, translations and modules.
|
|
|
|
'reload': function(event) {
|
|
|
|
dbot.db = JSON.parse(fs.readFileSync('db.json', 'utf-8'));
|
|
|
|
dbot.reloadModules();
|
|
|
|
event.reply(dbot.t('reload'));
|
|
|
|
},
|
|
|
|
|
2012-12-30 17:41:10 +01:00
|
|
|
// Say something in a channel
|
2012-12-11 17:04:52 +01:00
|
|
|
'say': function(event) {
|
|
|
|
var channel = event.params[1];
|
|
|
|
if(event.params[1] === "@") {
|
|
|
|
var channel = event.channel.name;
|
|
|
|
}
|
|
|
|
var message = event.params.slice(2).join(' ');
|
|
|
|
dbot.say(event.server, channel, message);
|
|
|
|
},
|
|
|
|
|
|
|
|
// Load new module
|
|
|
|
'load': function(event) {
|
|
|
|
var moduleName = event.params[1];
|
2012-12-17 18:18:31 +01:00
|
|
|
dbot.config.moduleNames.push(moduleName);
|
2012-12-11 17:04:52 +01:00
|
|
|
dbot.reloadModules();
|
|
|
|
event.reply(dbot.t('load_module', {'moduleName': moduleName}));
|
|
|
|
},
|
|
|
|
|
|
|
|
// Unload a loaded module
|
|
|
|
'unload': function(event) {
|
2012-12-17 18:18:31 +01:00
|
|
|
var moduleNames = dbot.config.moduleNames;
|
2012-12-11 17:04:52 +01:00
|
|
|
var moduleName = event.params[1];
|
2012-12-17 18:18:31 +01:00
|
|
|
if(moduleNames.include(moduleName)) {
|
2012-12-11 21:27:18 +01:00
|
|
|
var moduleDir = '../' + moduleName + '/';
|
|
|
|
var cacheKey = require.resolve(moduleDir + moduleName);
|
2012-12-11 17:04:52 +01:00
|
|
|
delete require.cache[cacheKey];
|
|
|
|
|
2012-12-17 18:18:31 +01:00
|
|
|
var moduleIndex = moduleNames.indexOf(moduleName);
|
|
|
|
moduleNames.splice(moduleIndex, 1);
|
2012-12-11 17:04:52 +01:00
|
|
|
dbot.reloadModules();
|
|
|
|
|
|
|
|
event.reply(dbot.t('unload_module', {'moduleName': moduleName}));
|
|
|
|
} else {
|
|
|
|
event.reply(dbot.t('unload_error', {'moduleName': moduleName}));
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
// 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.db.bans[command].push(username);
|
|
|
|
event.reply(dbot.t('banned', {'user': username, 'command': command}));
|
|
|
|
},
|
|
|
|
|
|
|
|
// 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 {
|
|
|
|
event.reply(dbot.t('unban_error', {'user': username}));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-01-12 10:38:13 +01:00
|
|
|
commands['greload'].access = 'admin';
|
|
|
|
commands['reload'].access = 'admin';
|
|
|
|
commands['unload'].access = 'admin';
|
|
|
|
commands['load'].access = 'admin';
|
|
|
|
commands['join'].access = 'moderator';
|
|
|
|
commands['part'].access = 'moderator';
|
|
|
|
commands['opme'].access = 'moderator';
|
|
|
|
commands['say'].access = 'moderator';
|
|
|
|
commands['ban'].access = 'moderator';
|
|
|
|
commands['unban'].access = 'moderator';
|
|
|
|
|
2012-12-11 17:04:52 +01:00
|
|
|
return {
|
|
|
|
'name': 'admin',
|
|
|
|
'ignorable': false,
|
2013-01-12 10:38:13 +01:00
|
|
|
'commands': commands
|
2012-12-11 17:04:52 +01:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.fetch = function(dbot) {
|
|
|
|
return admin(dbot);
|
|
|
|
};
|