2013-01-14 17:56:34 +01:00
|
|
|
var fs = require('fs'),
|
|
|
|
_ = require('underscore')._,
|
|
|
|
sys = require('sys'),
|
2013-08-25 01:06:53 +02:00
|
|
|
process = require('process'),
|
2013-01-14 17:56:34 +01:00
|
|
|
exec = require('child_process').exec;
|
|
|
|
|
|
|
|
var commands = function(dbot) {
|
2013-01-21 01:10:01 +01:00
|
|
|
var noChangeConfig = [ 'servers', 'name', 'moduleNames' ];
|
2013-04-20 15:50:47 +02:00
|
|
|
|
2013-01-14 17:56:34 +01:00
|
|
|
var commands = {
|
|
|
|
// Join a channel
|
2013-06-20 01:07:38 +02:00
|
|
|
'~join': function(event) {
|
2013-01-14 17:56:34 +01:00
|
|
|
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
|
2013-06-20 01:07:38 +02:00
|
|
|
'~part': function(event) {
|
2013-01-14 17:56:34 +01:00
|
|
|
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
|
2013-06-20 01:07:38 +02:00
|
|
|
'~opme': function(event) {
|
2013-01-14 17:56:34 +01:00
|
|
|
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
|
2013-06-20 01:07:38 +02:00
|
|
|
'~greload': function(event) {
|
2013-01-14 17:56:34 +01:00
|
|
|
exec("git pull", function (error, stdout, stderr) {
|
|
|
|
exec("git submodule update", function (error, stdout, stderr) {
|
|
|
|
event.reply(dbot.t('gpull'));
|
2013-12-29 19:23:35 +01:00
|
|
|
commands['~reload'](event);
|
|
|
|
commands['~version'](event);
|
2013-01-14 17:56:34 +01:00
|
|
|
}.bind(this));
|
|
|
|
}.bind(this));
|
|
|
|
},
|
|
|
|
|
|
|
|
// Display commit information for part of dbot
|
2013-06-20 01:07:38 +02:00
|
|
|
'~version': function(event){
|
2013-01-14 17:56:34 +01:00
|
|
|
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-06-20 01:07:38 +02:00
|
|
|
'~status': function(event) {
|
2013-01-23 23:32:17 +01:00
|
|
|
var moduleName = event.params[1];
|
|
|
|
if(_.has(dbot.status, moduleName)) {
|
|
|
|
var status = dbot.status[moduleName];
|
|
|
|
if(status === true) {
|
2013-05-06 17:53:42 +02:00
|
|
|
event.reply(dbot.t('status_good', {
|
|
|
|
'module': moduleName,
|
|
|
|
'reason': status
|
|
|
|
}));
|
2013-01-23 23:32:17 +01:00
|
|
|
} else {
|
2013-05-06 17:53:42 +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.
|
2013-06-20 01:07:38 +02:00
|
|
|
'~reload': function(event) {
|
2013-01-14 17:56:34 +01:00
|
|
|
dbot.db = JSON.parse(fs.readFileSync('db.json', 'utf-8'));
|
|
|
|
dbot.reloadModules();
|
2013-05-06 17:53:42 +02:00
|
|
|
process.nextTick(function() {
|
|
|
|
event.reply(dbot.t('reload'));
|
|
|
|
});
|
2013-01-14 17:56:34 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
// Say something in a channel
|
2013-06-20 01:07:38 +02:00
|
|
|
'~say': function(event) {
|
2013-01-14 17:56:34 +01:00
|
|
|
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
|
2013-06-20 01:07:38 +02:00
|
|
|
'~load': function(event) {
|
2013-01-14 17:56:34 +01:00
|
|
|
var moduleName = event.params[1];
|
|
|
|
if(!_.include(dbot.config.moduleNames, moduleName)) {
|
2013-06-05 23:14:21 +02:00
|
|
|
dbot.customConfig.moduleNames.push(moduleName);
|
|
|
|
this.internalAPI.saveConfig();
|
2013-01-14 17:56:34 +01:00
|
|
|
dbot.reloadModules();
|
2013-05-06 17:53:42 +02:00
|
|
|
process.nextTick(function() {
|
|
|
|
if(dbot.status[moduleName] === true) {
|
|
|
|
event.reply(dbot.t('load_module', { 'moduleName': moduleName }));
|
|
|
|
} else {
|
|
|
|
event.reply(dbot.t('load_failed', { 'module': moduleName }));
|
|
|
|
}
|
|
|
|
});
|
2013-01-14 17:56:34 +01:00
|
|
|
} else {
|
|
|
|
if(moduleName == 'web') {
|
|
|
|
event.reply(dbot.t('already_loaded_web'));
|
|
|
|
} else {
|
2013-05-06 17:53:42 +02:00
|
|
|
event.reply(dbot.t('already_loaded', { 'moduleName': moduleName }));
|
2013-01-14 17:56:34 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
// Unload a loaded module
|
2013-06-20 01:07:38 +02:00
|
|
|
'~unload': function(event) {
|
2013-01-14 17:56:34 +01:00
|
|
|
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-06-05 23:14:21 +02:00
|
|
|
|
|
|
|
dbot.customConfig.moduleNames = _.without(dbot.config.moduleNames, moduleName);
|
|
|
|
this.internalAPI.saveConfig();
|
2013-01-14 17:56:34 +01:00
|
|
|
dbot.reloadModules();
|
|
|
|
|
2013-05-06 17:53:42 +02:00
|
|
|
process.nextTick(function() {
|
|
|
|
event.reply(dbot.t('unload_module', { 'moduleName': moduleName }));
|
|
|
|
});
|
2013-01-14 17:56:34 +01:00
|
|
|
} else {
|
2013-05-06 17:53:42 +02:00
|
|
|
event.reply(dbot.t('unload_error', { 'moduleName': moduleName }));
|
2013-01-14 17:56:34 +01:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2013-01-20 16:17:44 +01:00
|
|
|
/*** Config options ***/
|
|
|
|
|
2013-06-20 01:07:38 +02:00
|
|
|
'~setconfig': function(event) {
|
2013-04-20 15:50:47 +02:00
|
|
|
var configPath = event.input[1],
|
|
|
|
newOption = event.input[2];
|
|
|
|
|
2013-12-29 19:13:59 +01:00
|
|
|
event.reply(event.input[1]);
|
|
|
|
|
2013-04-20 15:50:47 +02:00
|
|
|
if(!_.include(noChangeConfig, configPath)) {
|
|
|
|
this.internalAPI.getCurrentConfig(configPath, function(config) {
|
2013-04-22 16:46:30 +02:00
|
|
|
if(config !== null) {
|
2013-04-20 15:50:47 +02:00
|
|
|
// Convert to boolean type if config item boolean
|
|
|
|
if(_.isBoolean(config)) {
|
|
|
|
newOption = (newOption == "true");
|
|
|
|
}
|
|
|
|
|
2013-04-30 18:47:47 +02:00
|
|
|
// Convert to integer type is config item integer
|
2013-05-17 14:02:38 +02:00
|
|
|
if(_.isNumber(config)) {
|
2013-04-30 18:47:47 +02:00
|
|
|
newOption = parseInt(newOption);
|
|
|
|
}
|
|
|
|
|
2013-05-17 14:02:38 +02:00
|
|
|
if(_.isArray(config)) {
|
2013-04-30 18:47:47 +02:00
|
|
|
event.reply(dbot.t("config_array", { "alternate": "pushconfig" }));
|
2013-04-30 15:53:16 +02:00
|
|
|
}
|
2013-04-20 15:50:47 +02:00
|
|
|
} else {
|
2013-07-25 00:33:03 +02:00
|
|
|
event.reply(dbot.t("no_config_key", {'path': configPath}));
|
2013-07-25 00:27:50 +02:00
|
|
|
configPath = configPath.split('.');
|
|
|
|
if(_.has(dbot.config.modules, configPath[0])) {
|
2013-07-14 16:55:15 +02:00
|
|
|
configPath.splice(0, 0, 'modules');
|
2013-07-25 00:38:52 +02:00
|
|
|
event.input[1] = configPath.join('.');
|
2013-12-29 19:23:35 +01:00
|
|
|
commands['~setconfig'](event);
|
2013-07-14 16:55:15 +02:00
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
event.reply(dbot.t('new_config_key', { 'key': configPath }));
|
|
|
|
}
|
2013-04-20 15:50:47 +02:00
|
|
|
}
|
2013-06-05 23:52:14 +02:00
|
|
|
|
|
|
|
this.internalAPI.setConfig(configPath, newOption, function(err) {
|
|
|
|
event.reply(configPath + ": " + config + " -> " + newOption);
|
|
|
|
});
|
2013-04-20 15:50:47 +02:00
|
|
|
}.bind(this));
|
2013-01-21 01:10:01 +01:00
|
|
|
} 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-06-20 01:07:38 +02:00
|
|
|
'~pushconfig': function(event) {
|
2013-04-20 15:50:47 +02:00
|
|
|
var configPath = event.input[1],
|
|
|
|
newOption = event.input[2];
|
|
|
|
|
|
|
|
if(!_.include(noChangeConfig, configPath)) {
|
|
|
|
this.internalAPI.getCurrentConfig(configPath, function(config) {
|
2013-04-22 16:46:30 +02:00
|
|
|
if(config !== null) {
|
2013-04-20 15:50:47 +02:00
|
|
|
if(_.isArray(config)) {
|
|
|
|
event.reply(configPath + ": " + config + " << " + newOption);
|
|
|
|
config.push(newOption);
|
2013-06-06 00:15:17 +02:00
|
|
|
this.internalAPI.setConfig(configPath, config, function(err) {});
|
2013-04-20 15:50:47 +02:00
|
|
|
} else {
|
2013-04-30 15:53:16 +02:00
|
|
|
event.reply(dbot.t("config_array", { "alternate": "setconfig" }));
|
2013-04-20 15:50:47 +02:00
|
|
|
}
|
|
|
|
} else {
|
2013-06-06 00:15:17 +02:00
|
|
|
event.reply(dbot.t("no_config_key", { 'path': configPath }));
|
2013-04-20 15:50:47 +02:00
|
|
|
}
|
|
|
|
}.bind(this));
|
|
|
|
} else {
|
2013-04-30 15:53:16 +02:00
|
|
|
event.reply(dbot.t("config_lock"));
|
2013-01-27 21:26:14 +01:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2013-06-20 01:07:38 +02:00
|
|
|
'~showconfig': function(event) {
|
2013-04-20 15:50:47 +02:00
|
|
|
var configPath = event.params[1];
|
|
|
|
if(configPath) {
|
|
|
|
this.internalAPI.getCurrentConfig(configPath, function(config) {
|
2013-04-22 16:46:30 +02:00
|
|
|
if(config !== null) {
|
2013-04-20 15:50:47 +02:00
|
|
|
if(_.isArray(config)) {
|
2013-04-30 15:53:16 +02:00
|
|
|
event.reply(dbot.t("config_keys_location", {
|
|
|
|
"path": configPath,
|
|
|
|
"value": config
|
|
|
|
}));
|
2013-04-20 15:50:47 +02:00
|
|
|
} else if(_.isObject(config)) {
|
2013-04-30 15:53:16 +02:00
|
|
|
event.reply(dbot.t("config_keys_location", {
|
|
|
|
"path": configPath,
|
|
|
|
"value": _.keys(config)
|
|
|
|
}));
|
2013-04-20 15:50:47 +02:00
|
|
|
} else {
|
2013-04-30 15:53:16 +02:00
|
|
|
event.reply(dbot.t("config_keys_location", {
|
|
|
|
"path": configPath,
|
|
|
|
"value": config
|
|
|
|
}));
|
2013-04-20 15:50:47 +02:00
|
|
|
}
|
|
|
|
} else {
|
2013-06-04 02:06:41 +02:00
|
|
|
event.reply(dbot.t("no_config_key", {'path': configPath}));
|
2013-06-30 20:25:45 +02:00
|
|
|
|
2013-06-04 02:06:41 +02:00
|
|
|
configPath = configPath.split('.');
|
2013-06-30 20:25:45 +02:00
|
|
|
if(_.has(dbot.config.modules, configPath[0])) {
|
|
|
|
configPath.splice(0, 0, 'modules');
|
|
|
|
} else {
|
|
|
|
configPath.pop();
|
|
|
|
}
|
|
|
|
|
2013-06-04 02:06:41 +02:00
|
|
|
event.params[1] = configPath.join('.');
|
2013-12-29 19:23:35 +01:00
|
|
|
commands['~showconfig'](event);
|
2013-04-20 15:50:47 +02:00
|
|
|
}
|
2013-06-04 02:06:41 +02:00
|
|
|
}.bind(this));
|
2013-01-20 17:35:17 +01:00
|
|
|
} else {
|
2013-04-30 15:53:16 +02:00
|
|
|
event.reply(dbot.t("config_keys_location", {
|
|
|
|
"path": "root",
|
|
|
|
"value": _.keys(dbot.config)
|
|
|
|
}));
|
2013-01-20 17:17:22 +01:00
|
|
|
}
|
2013-05-10 20:12:40 +02:00
|
|
|
},
|
|
|
|
|
2013-06-21 20:32:00 +02:00
|
|
|
'~savemodules': function(event) {
|
2013-05-10 20:12:40 +02:00
|
|
|
fs.readFile('config.json', 'utf-8', function(err, config) {
|
|
|
|
config = JSON.parse(config);
|
|
|
|
config.moduleNames = _.keys(dbot.modules);
|
|
|
|
fs.writeFile('config.json', JSON.stringify(config, null, ' '), function() {
|
|
|
|
event.reply(dbot.t('modules_saved', { 'modules': _.keys(dbot.modules) }));
|
|
|
|
});
|
|
|
|
});
|
2013-08-25 01:06:53 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
'~die': function(event) {
|
|
|
|
event.reply('BRB coconut hunting...');
|
2013-08-25 01:11:34 +02:00
|
|
|
setTimeout(3000, function() {
|
|
|
|
process.exit(0);
|
|
|
|
});
|
2013-05-10 20:12:40 +02: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-06-20 01:07:38 +02:00
|
|
|
commands['~showconfig'].access = 'moderator';
|
|
|
|
commands['~join'].access = 'moderator';
|
|
|
|
commands['~part'].access = 'moderator';
|
|
|
|
commands['~opme'].access = 'moderator';
|
|
|
|
commands['~say'].access = 'moderator';
|
2013-01-14 17:56:34 +01:00
|
|
|
|
2013-12-29 19:19:17 +01:00
|
|
|
commands['~pushconfig'].regex = [/pushconfig ([^ ]+) ([^ ]+)/, 3];
|
|
|
|
commands['~setconfig'].regex = [/setconfig ([^ ]+) ([^ ]+)/, 3];
|
2013-04-20 15:50:47 +02:00
|
|
|
|
2013-01-14 17:56:34 +01:00
|
|
|
return commands;
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.fetch = function(dbot) {
|
|
|
|
return commands(dbot);
|
|
|
|
}
|