lirc
Version:
An old IRC client for node
122 lines (105 loc) • 2.69 kB
JavaScript
// Generated by CoffeeScript 1.12.7
(function() {
var botnet, cluster, fs, lirc, path;
path = require('path');
fs = require('fs');
cluster = require('cluster');
lirc = require('../lirc');
module.exports = lirc.botnet = botnet = {};
botnet.bots = {};
botnet.cfg = {
botsDir: path.join(path.dirname(process.mainModule.filename), '/bots')
};
botnet.kill = function(name) {
var bot, id, ref;
if (name == null) {
name = '';
}
if (!name) {
return false;
}
ref = lirc.botnet.bots;
for (id in ref) {
bot = ref[id];
if (!bot.name) {
continue;
}
if (name.toLowerCase() === bot.name.toLowerCase()) {
bot.process.kill();
delete lirc.botnet.bots[id];
return true;
}
}
return false;
};
botnet.restart = function(name) {
var bot, botName, id, ref;
if (name == null) {
name = '';
}
if (!name) {
return false;
}
ref = lirc.botnet.bots;
for (id in ref) {
bot = ref[id];
if (!bot.name) {
continue;
}
botName = bot.name.toLowerCase();
if (name.toLowerCase() === botName) {
bot.process.kill();
delete lirc.botnet.bots[id];
botnet.spawn(name);
return true;
}
}
return false;
};
botnet.spawn = function(botPath) {
var botPathLower, useLower, worker;
botPathLower = botPath.toLowerCase();
if (cluster.isMaster) {
if (fs.existsSync(botPath) || fs.existsSync(path.join(botnet.cfg.botsDir, botPath)) || (useLower = fs.existsSync(path.join(botnet.cfg.botsDir, botPathLower)))) {
botPath = botPathLower;
worker = cluster.fork({
lirc_botPath: botPath
});
botnet.bots[worker.id] = worker;
lirc.bind(botnet.listeners.master, worker);
}
return true;
}
return false;
};
botnet.run = function(botPath) {
var dir;
if (botPath == null) {
botPath = '';
}
if (!botPath) {
if (process.env.lirc_botPath != null) {
botPath = process.env.lirc_botPath;
} else {
lirc.error('err', "botnet.run(), cant find bot to run");
}
}
dir = path.join(botnet.cfg.botsDir, botPath);
if (fs.existsSync(dir)) {
return require(dir);
} else {
if (cluster.isWorker) {
return cluster.worker.kill();
}
}
};
botnet.listeners = {
master: require('./listeners/master'),
worker: require('./listeners/worker')
};
require('./emitter');
if (cluster.isWorker) {
lirc.bind(botnet.listeners.worker, process);
}
lirc.botnet = module.exports = botnet;
}).call(this);