2013-01-14 17:56:34 +01:00
|
|
|
var fs = require('fs'),
|
|
|
|
_ = require('underscore')._,
|
|
|
|
sys = require('sys'),
|
|
|
|
exec = require('child_process').exec;
|
|
|
|
|
|
|
|
var commands = function(dbot) {
|
2013-01-21 01:10:01 +01:00
|
|
|
var noChangeConfig = [ 'servers', 'name', 'moduleNames' ];
|
|
|
|
|
2013-01-27 18:53:50 +01:00
|
|
|
var getCurrentConfig = function(configKey) {
|
2013-01-20 17:35:17 +01:00
|
|
|
var defaultConfigPath = dbot.config;
|
|
|
|
var userConfigPath = dbot.db.config;
|
|
|
|
|
|
|
|
if(configKey) {
|
|
|
|
var configKey = configKey.split('.');
|
|
|
|
for(var i=0;i<configKey.length-1;i++) {
|
|
|
|
if(_.has(defaultConfigPath, configKey[i])) {
|
|
|
|
if(!_.has(userConfigPath, configKey[i])) {
|
|
|
|
userConfigPath[configKey[i]] = {};
|
|
|
|
}
|
|
|
|
userConfigPath = userConfigPath[configKey[i]];
|
|
|
|
defaultConfigPath = defaultConfigPath[configKey[i]];
|
|
|
|
} else {
|
|
|
|
return false;
|
2013-01-20 16:49:31 +01:00
|
|
|
}
|
|
|
|
}
|
2013-03-21 09:42:03 +01:00
|
|
|
}
|
2013-01-20 16:49:31 +01:00
|
|
|
|
2013-01-27 18:53:50 +01:00
|
|
|
var currentOption;
|
2013-03-21 09:42:03 +01:00
|
|
|
if(configKey && configKey.length != 1) {
|
2013-01-27 18:53:50 +01:00
|
|
|
configKey = _.last(configKey);
|
|
|
|
if(_.has(userConfigPath, configKey) && !_.isUndefined(userConfigPath[configKey])) {
|
|
|
|
currentOption = userConfigPath[configKey];
|
|
|
|
} else if(_.has(defaultConfigPath, configKey)) {
|
|
|
|
currentOption = defaultConfigPath[configKey];
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
currentOption = defaultConfigPath[configKey];
|
|
|
|
}
|
|
|
|
|
2013-01-20 16:49:31 +01:00
|
|
|
return {
|
|
|
|
'user': userConfigPath,
|
2013-01-27 18:53:50 +01:00
|
|
|
'default': defaultConfigPath,
|
|
|
|
'value': currentOption
|
2013-01-20 16:49:31 +01:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2013-01-14 17:56:34 +01:00
|
|
|
var commands = {
|
|
|
|
// Join a channel
|
|
|
|
'join': function(event) {
|
|
|
|
var channel = event.params[1];
|
|
|
|
if(_.has(event.allChannels, channel)) {
|
|
|
|
event.reply(dbot.t('already_in_channel', {'channel': channel}));
|
|
|
|
} 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(!_.has(event.allChannels, channel)) {
|
|
|
|
event.reply(dbot.t('not_in_channel', {'channel': channel}));
|
|
|
|
} 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(!_.has(event.allChannels, channel)) {
|
|
|
|
channel = event.channel.name;
|
|
|
|
}
|
|
|
|
dbot.instance.mode(event, channel, ' +o ' + event.user);
|
|
|
|
},
|
|
|
|
|
|
|
|
// Do a git pull and reload
|
|
|
|
'greload': function(event) {
|
|
|
|
exec("git pull", function (error, stdout, stderr) {
|
|
|
|
exec("git submodule update", function (error, stdout, stderr) {
|
|
|
|
event.reply(dbot.t('gpull'));
|
|
|
|
commands.reload(event);
|
|
|
|
event.message = 'version';
|
|
|
|
event.action = 'PRIVMSG';
|
|
|
|
event.params = event.message.split(' ');
|
|
|
|
dbot.instance.emit(event);
|
|
|
|
}.bind(this));
|
|
|
|
}.bind(this));
|
|
|
|
},
|
|
|
|
|
|
|
|
// 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(_.has(dbot.modules, input.split("/")[0])){
|
|
|
|
cmd += "modules/"+input;
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
cmd += input;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
exec(cmd, function(error, stdout, stderr){
|
|
|
|
if(stdout.length > 0){
|
|
|
|
event.reply(stdout);
|
|
|
|
}
|
|
|
|
else{
|
2013-04-30 11:16:29 +02:00
|
|
|
event.reply(dbot.t("no_version"));
|
2013-01-14 17:56:34 +01:00
|
|
|
}
|
|
|
|
}.bind(this));
|
|
|
|
},
|
|
|
|
|
2013-01-23 23:32:17 +01:00
|
|
|
'status': function(event) {
|
|
|
|
var moduleName = event.params[1];
|
|
|
|
if(_.has(dbot.status, moduleName)) {
|
|
|
|
var status = dbot.status[moduleName];
|
|
|
|
if(status === true) {
|
2013-04-30 11:16:29 +02:00
|
|
|
event.reply(dbot.t("status_good",{"module":moduleName, "reason": status}));
|
2013-01-23 23:32:17 +01:00
|
|
|
} else {
|
2013-04-30 11:16:29 +02:00
|
|
|
event.reply(dbot.t("status_bad",{"module":moduleName, "reason": status}));
|
2013-01-23 23:32:17 +01:00
|
|
|
}
|
|
|
|
} else {
|
2013-04-30 11:16:29 +02:00
|
|
|
event.reply(dbot.t("status_unloaded"));
|
2013-01-23 23:32:17 +01:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2013-01-14 17:56:34 +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'));
|
|
|
|
},
|
|
|
|
|
|
|
|
// Say something in a channel
|
|
|
|
'say': function(event) {
|
|
|
|
var channel = event.params[1];
|
|
|
|
if(event.params[1] === "@") {
|
2013-04-30 11:16:29 +02:00
|
|
|
channel = event.channel.name;
|
2013-01-14 17:56:34 +01:00
|
|
|
}
|
|
|
|
var message = event.params.slice(2).join(' ');
|
|
|
|
dbot.say(event.server, channel, message);
|
|
|
|
},
|
|
|
|
|
|
|
|
// Load new module
|
|
|
|
'load': function(event) {
|
|
|
|
var moduleName = event.params[1];
|
|
|
|
if(!_.include(dbot.config.moduleNames, moduleName)) {
|
|
|
|
dbot.config.moduleNames.push(moduleName);
|
|
|
|
dbot.reloadModules();
|
2013-01-23 23:32:17 +01:00
|
|
|
if(dbot.status[moduleName] === true) {
|
|
|
|
event.reply(dbot.t('load_module', {'moduleName': moduleName}));
|
|
|
|
} else {
|
2013-04-30 11:16:29 +02:00
|
|
|
event.reply(dbot.t("load_failed",{"module": moduleName}));
|
2013-01-23 23:32:17 +01:00
|
|
|
}
|
2013-01-14 17:56:34 +01:00
|
|
|
} else {
|
|
|
|
if(moduleName == 'web') {
|
|
|
|
event.reply(dbot.t('already_loaded_web'));
|
|
|
|
} else {
|
|
|
|
event.reply(dbot.t('already_loaded', {'moduleName': moduleName}));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
// Unload a loaded module
|
|
|
|
'unload': function(event) {
|
|
|
|
var moduleNames = dbot.config.moduleNames;
|
|
|
|
var moduleName = event.params[1];
|
|
|
|
if(_.include(moduleNames, moduleName)) {
|
|
|
|
var moduleDir = '../' + moduleName + '/';
|
2013-03-21 00:34:43 +01:00
|
|
|
try {
|
|
|
|
var cacheKey = require.resolve(moduleDir + moduleName);
|
|
|
|
delete require.cache[cacheKey];
|
|
|
|
} catch(err) { }
|
2013-01-14 17:56:34 +01:00
|
|
|
dbot.config.moduleNames = _.without(dbot.config.moduleNames, moduleName);
|
|
|
|
dbot.reloadModules();
|
|
|
|
|
|
|
|
event.reply(dbot.t('unload_module', {'moduleName': moduleName}));
|
|
|
|
} else {
|
|
|
|
event.reply(dbot.t('unload_error', {'moduleName': moduleName}));
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2013-01-20 16:17:44 +01:00
|
|
|
/*** Config options ***/
|
|
|
|
|
|
|
|
'setconfig': function(event) {
|
2013-01-27 21:26:14 +01:00
|
|
|
var configPathString = event.params[1],
|
|
|
|
configKey = _.last(configPathString.split('.')),
|
|
|
|
newOption = event.params[2];
|
2013-01-20 16:17:44 +01:00
|
|
|
|
2013-01-21 01:10:01 +01:00
|
|
|
if(!_.include(noChangeConfig, configKey)) {
|
2013-01-27 18:53:50 +01:00
|
|
|
var configPath = getCurrentConfig(configPathString);
|
|
|
|
|
|
|
|
if(configPath == false || _.isUndefined(configPath.value)) {
|
2013-04-30 11:16:29 +02:00
|
|
|
event.reply(dbot.t("no_config_key"));
|
2013-01-21 01:10:01 +01:00
|
|
|
return;
|
|
|
|
}
|
2013-01-27 18:53:50 +01:00
|
|
|
var currentOption = configPath.value;
|
2013-01-20 16:17:44 +01:00
|
|
|
|
2013-01-21 01:10:01 +01:00
|
|
|
// Convert to boolean type if config item boolean
|
|
|
|
if(_.isBoolean(currentOption)) {
|
|
|
|
newOption = (newOption == "true");
|
|
|
|
}
|
2013-01-20 16:17:44 +01:00
|
|
|
|
2013-04-30 18:44:39 +02:00
|
|
|
// Convert to integer type is config item integer
|
|
|
|
if(_.isNumber(currentOption)) {
|
|
|
|
newOption = parseInt(newOption);
|
|
|
|
}
|
|
|
|
|
2013-01-21 01:10:01 +01:00
|
|
|
if(_.isArray(currentOption)) {
|
2013-04-30 11:16:29 +02:00
|
|
|
event.reply(dbot.t("config_array",{"alternate": "pushconfig"}));
|
2013-01-21 01:10:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
event.reply(configPathString + ": " + currentOption + " -> " + newOption);
|
|
|
|
configPath['user'][configKey] = newOption;
|
|
|
|
dbot.reloadModules();
|
|
|
|
} else {
|
2013-04-30 11:16:29 +02:00
|
|
|
event.reply(dbot.t("config_lock"));
|
2013-01-21 01:10:01 +01:00
|
|
|
}
|
2013-01-20 17:17:22 +01:00
|
|
|
},
|
|
|
|
|
2013-01-27 21:26:14 +01:00
|
|
|
'pushconfig': function(event) {
|
|
|
|
var configPathString = event.params[1],
|
|
|
|
configKey = _.last(configPathString.split('.')),
|
|
|
|
newOption = event.params[2];
|
|
|
|
|
|
|
|
if(!_.include(noChangeConfig, configKey)) {
|
|
|
|
var configPath = getCurrentConfig(configPathString);
|
|
|
|
if(configPath == false || _.isUndefined(configPath.value)) {
|
2013-04-30 11:16:29 +02:00
|
|
|
event.reply(dbot.t("no_config_key"));
|
2013-01-27 21:26:14 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
var currentArray = configPath.value;
|
|
|
|
|
|
|
|
if(!_.isArray(currentArray)) {
|
2013-04-30 11:16:29 +02:00
|
|
|
event.reply(dbot.t("config_array",{"alternate": "setconfig"}));
|
|
|
|
return;
|
2013-01-27 21:26:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
event.reply(configPathString + ": " + currentArray + " << " + newOption);
|
|
|
|
currentArray.push(newOption);
|
|
|
|
dbot.reloadModules();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2013-01-20 17:17:22 +01:00
|
|
|
'showconfig': function(event) {
|
|
|
|
var configPathString = event.params[1];
|
2013-01-27 18:53:50 +01:00
|
|
|
var configPath = getCurrentConfig(configPathString);
|
2013-01-20 17:35:17 +01:00
|
|
|
|
|
|
|
if(configPathString) {
|
|
|
|
var configKey = _.last(configPathString.split('.'));
|
2013-04-30 11:16:29 +02:00
|
|
|
if(configKey) {
|
|
|
|
event.reply(dbot.t("no_config_path"));
|
2013-01-20 17:35:17 +01:00
|
|
|
return;
|
|
|
|
}
|
2013-01-20 17:17:22 +01:00
|
|
|
|
2013-01-27 21:26:14 +01:00
|
|
|
if(_.isArray(configPath.value)) {
|
|
|
|
event.reply(configKey + ': ' + configPath.value);
|
|
|
|
} else if(_.isObject(configPath.value)) {
|
2013-04-30 11:16:29 +02:00
|
|
|
event.reply(dbot.t("config_keys_location",{"path":configPathString,"value":Object.keys(configPath.value)}));
|
2013-01-20 17:35:17 +01:00
|
|
|
} else {
|
2013-01-27 18:53:50 +01:00
|
|
|
event.reply(configKey + ': ' + configPath.value);
|
2013-01-20 17:17:22 +01:00
|
|
|
}
|
2013-01-20 17:35:17 +01:00
|
|
|
} else {
|
2013-04-30 11:16:29 +02:00
|
|
|
event.reply(dbot.t("config_keys_location",{"path":"root","value":Object.keys(configPath['default'])}));
|
2013-01-20 17:17:22 +01:00
|
|
|
}
|
2013-01-14 17:56:34 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-03-22 11:58:40 +01:00
|
|
|
_.each(commands, function(command) {
|
|
|
|
command.access = 'admin';
|
|
|
|
});
|
|
|
|
|
2013-01-20 17:17:22 +01:00
|
|
|
commands['showconfig'].access = 'moderator';
|
2013-01-14 17:56:34 +01:00
|
|
|
commands['join'].access = 'moderator';
|
|
|
|
commands['part'].access = 'moderator';
|
|
|
|
commands['opme'].access = 'moderator';
|
|
|
|
commands['say'].access = 'moderator';
|
|
|
|
|
|
|
|
return commands;
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.fetch = function(dbot) {
|
|
|
|
return commands(dbot);
|
|
|
|
}
|