Skip to content

Instantly share code, notes, and snippets.

@whiskers75
Created August 13, 2014 16:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save whiskers75/ef23f93b9441e14c4c76 to your computer and use it in GitHub Desktop.
Save whiskers75/ef23f93b9441e14c4c76 to your computer and use it in GitHub Desktop.
var IotaServ = {};
IotaServ.onLoad = function(){};
IotaServ.eob = function() {
IotaServ.o.join('#services');
}
IotaServ.privmsg = function(from, target, message) {
message = message.split(' ');
message[0] = message[0].toLowerCase();
if (message[0] == 'hug') {
if (!message[1] || !message[2]) {
IotaServ.o.notice(from.id, 'Invalid syntax for HUG.');
IotaServ.o.notice(from.id, 'Syntax: HUG [user] [channel]');
return;
}
var hugged = IotaServ.s.objs.findByAttr('name', message[1]);
if (!hugged) {
IotaServ.o.notice(from.id, 'Failed to hug: The requested user does not exist.');
return;
}
if (message[2].indexOf('#') == -1 || message[2].indexOf(',') != -1) {
IotaServ.o.notice(from.id, 'The channel requested isn\'t valid.');
IotaServ.o.notice(from.id, 'Syntax: HUG [user] [channel]');
return;
}
IotaServ.o.privmsg(message[2], '\x01ACTION hands ' + message[1] + ' a hug from ' + from.name);
IotaServ.o.notice(from.id, 'Hug sent.');
}
if (message[0] == 'kill') {
if (!message[1]) {
IotaServ.o.notice(from.id, 'Invalid syntax for KILL.');
IotaServ.o.notice(from.id, 'Syntax: KILL [user]');
return;
}
if (from.modes.indexOf('a') == -1) {
IotaServ.o.notice(from.id, 'Insufficient permissions for KILL.');
return;
}
var killed = IotaServ.s.objs.findByAttr('name', message[1]);
if (!killed) {
IotaServ.o.notice(from.id, 'Kill failed: Unknown user');
return;
}
if (killed.isService) {
IotaServ.o.notice(from.id, 'Kill failed: That is an IRC Service');
IotaServ.o.privmsg('#services', from.name + ' KILL:FAIL: ' + message[1] + ' is a service');
return;
}
if (killed.modes.indexOf('I') != -1) {
IotaServ.o.notice(from.id, 'Kill failed: That user is immune');
IotaServ.o.privmsg('#services', from.name + ' KILL:FAIL: ' + message[1] + ' is immune');
return;
}
IotaServ.o.privmsg('#services', from.name + ' KILL: ' + message[1]);
IotaServ.o.kill(killed.id);
IotaServ.o.notice(from.id, 'Kill sent.');
}
if (message[0] == 'pls') {
if (!message[1] || !message[2]) {
IotaServ.o.notice(from.id, 'Invalid syntax for PLS.');
IotaServ.o.notice(from.id, 'Syntax: PLS [user] [channel]');
return;
}
var plsd = IotaServ.s.objs.findByAttr('name', message[1]);
if (!plsd) {
IotaServ.o.notice(from.id, 'PLS failed: Unknown user');
return;
}
if (message[2].indexOf('#') == -1 || message[2].indexOf(',') != -1) {
IotaServ.o.notice(from.id, 'The channel requested isn\'t valid.');
IotaServ.o.notice(from.id, 'Syntax: PLS [user] [channel]');
return;
}
IotaServ.o.privmsg(message[2], '\x01ACTION slaps ' + message[1] + ' around a bit with a great big trout from ' + from.name + '!');
IotaServ.o.notice(from.id, 'Trout sent.');
}
if (message[0] == 'chghost') {
if (!message[1] || !message[2]) {
IotaServ.o.notice(from.id, 'Invalid syntax for CHGHOST.');
IotaServ.o.notice(from.id, 'Syntax: CHGHOST [user] [host]');
return;
}
var hosted = IotaServ.s.objs.findByAttr('name', message[1]);
if (!hosted) {
IotaServ.o.notice(from.id, 'CHGHOST failed: Unknown user');
return;
}
IotaServ.o.chghost(hosted.id, message[2]);
IotaServ.o.privmsg('#services', from.name + ' CHGHOST: \x02' + message[1] + '\x02 to ' + message[2]);
IotaServ.o.notice(from.id, 'A host change request has been submitted.');
}
if (message[0] == 'help') {
IotaServ.o.notice(from.id, '\x02*** IotaServ help ***\x02');
IotaServ.o.notice(from.id, 'Part of whiskers75\'s service suite!');
IotaServ.o.notice(from.id, '');
IotaServ.o.notice(from.id, 'The following commands are available:');
IotaServ.o.notice(from.id, '\x02HUG\x02 [user] [channel] - hand a user a hug');
IotaServ.o.notice(from.id, '\x02PLS\x02 [user] [channel] - slap a user');
IotaServ.o.notice(from.id, '\x02KILL\x02 [user] - forcefully disconnect a user (requires +a)');
IotaServ.o.notice(from.id, '\x02CHGHOST\x02 [user] [host] - change a user\'s hostname');
IotaServ.o.notice(from.id, '\x02*** End of IotaServ help ***\x02');
}
}
module.exports = IotaServ;
var ts6 = require('./ts6.js');
var fs = require('fs');
var server = new ts6.Server({host: 'whsk.uk', port: 194, sid: '42D', pass: '***', sname: 'serv.js', sdesc: 'whiskers75\'s Node.js services'});
var ServServ = server.mkserv('ServServ', 'ServServ', 'serv.js', 'Service Services');
var services = {};
var servobjs = {};
var eob_done = false;
server.reload = function() {
ServServ.privmsg('#services', 'Loading services...');
var oldservs = Object.keys(services);
delete services;
var list = fs.readdirSync('./services');
var reloadedservs = [];
list.forEach(function(item) {
if (item.split('.')[1] == 'js') {
try {
var reload = false;
if (require.cache[__dirname + '/services/' + item]) {
reload = true;
delete require.cache[__dirname + '/services/' + item];
}
services[item.split('.')[0]] = require(__dirname + '/services/' + item);
if (servobjs[item.split('.')[0]]) {
services[item.split('.')[0]].o = servobjs[item.split('.')[0]].recreate();
}
else {
services[item.split('.')[0]].o = server.mkserv(item.split('.')[0], item.split('.')[0], 'serv.js', 'Loaded from ' + item);
}
services[item.split('.')[0]].onLoad();
if (eob_done) services[item.split('.')[0]].eob();
services[item.split('.')[0]].s = server;
servobjs[item.split('.')[0]] = services[item.split('.')[0]].o;
if (reload) reloadedservs.push(item.split('.')[0]);
ServServ.privmsg('#services', '✓ ' + (reload ? 'Reloaded' : 'Loaded') + ' ' + item);
}
catch (fail) {
ServServ.privmsg('#services', '✗ Failed to load ' + item);
console.log(fail.stack);
}
}
});
Object.keys(servobjs).forEach(function(srv) {
if (reloadedservs.indexOf(srv) == -1 && oldservs.indexOf(srv) != -1) {
servobjs[srv].send('QUIT :Service unloaded');
server.objs.splice(server.objs.indexOf(server.objs.findByAttr('name', srv)), 1);
delete servobjs[srv];
}
})
};
server.reload();
server.on('eob', function() {
eob_done = true;
ServServ.join('#services');
Object.keys(services).forEach(function(srv) {
services[srv].eob();
});
});
server.on('privmsg', function(from, target, message) {
Object.keys(services).forEach(function(srv) {
if (target && target.id == servobjs[srv].id) {
console.log(srv);
services[srv].privmsg(from, target, message);
}
});
});
process.on('SIGHUP', server.reload);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment