Skip to content

Commit

Permalink
Make dispatchers dynamic and reloadable.
Browse files Browse the repository at this point in the history
  • Loading branch information
guipn committed Mar 23, 2012
1 parent 6ddec97 commit 58edfbd
Show file tree
Hide file tree
Showing 5 changed files with 301 additions and 201 deletions.
212 changes: 26 additions & 186 deletions commands.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
var irc = require('./irc.js'),
gbk = require('./gbooks.js'),
rfc = require('./ietf.js'),
utl = require('./util.js'),
cmd = exports,
authd = {}; // People authentified

cmd.prefix = /^\./;
cmd.pub = {};
cmd.prv = {};


function tokenize(msg) {
Expand All @@ -17,198 +12,43 @@ function tokenize(msg) {
}


cmd.dispatch = function (dispatcher, packet) {

var tokens = tokenize(packet.message),
name = tokens[0];

if (typeof dispatcher[name] === 'undefined') {
return;
}
cmd.load = function (dispatcher, packet) {

if (dispatcher[name].restricted === false) {
dispatcher[name](tokens, packet);
}
else if (typeof authd[packet.hostmask] !== 'undefined') {
dispatcher[name](tokens, packet);
}

};
var dispatcherFile = packet.options.dispatcherDir +
'/' + dispatcher + '.js',
absolutePath = require.resolve(dispatcherFile);



cmd.prv.auth = function (tokens, packet) {

var correctPass = packet.options.operators[packet.sender];

if (typeof packet.options.
operators[packet.sender] === 'undefined') {
return;
}
delete require.cache[absolutePath];

if (tokens[1] !== correctPass) {
packet.network.send(
irc.outbound.say(packet.sender, 'Invalid password.')
);
}
else {
authd[packet.hostmask] = true;

packet.network.send(
irc.outbound.say(packet.sender, 'You are now authentified.')
);
}
};

cmd.prv.auth.restricted = false;

cmd[dispatcher] = require(dispatcherFile);

}

cmd.pub.gbooks = function (tokens, packet) {

var opt = packet.options;
cmd.dispatch = function (dispatcher, packet) {

function sendBookResult(book) {
var msg = book ?
utl.interp('"{title}" - {authors}; Rating: {rating} ' +
'({ratings}); {pages} pages; {link}',
{
title: book.title,
authors: book.authors.join(', '),
rating: book.avgRating,
ratings: book.ratingsCount,
pages: book.pageCount,
link: book.link
}) :
'No results found on Google Books.';
var tokens = tokenize(packet.message),
name = tokens[0],
commandFun;

packet.network.send(
irc.outbound.say(packet.channel, packet.sender + ': ' + msg)
);
packet.authd = authd;
packet.cmd = cmd;

if (typeof cmd[dispatcher] === 'undefined') {
cmd.load(dispatcher, packet);
}

tokens.shift();

gbk.getAPIKey(opt.gBooksAPIKey, function (key) {
gbk.search(key, tokens.join(' '), sendBookResult);
});

};

cmd.pub.gbooks.restricted = true;



cmd.pub.rfc = function(tokens, packet) {

tokens.shift();
rfc.search(tokens.join(' '), function (link) {

var msg = utl.interp('{who}: {lnk}',
{
who: packet.sender,
lnk: link
});

packet.network.send(
irc.outbound.say(packet.channel, msg)
);
});
};

cmd.pub.rfc.restricted = false;



cmd.prv.rfc = function(tokens, packet) {

tokens.shift();
rfc.search(tokens.join(' '), function (link) {
packet.network.send(
irc.outbound.say(packet.sender, link)
);
});
};

cmd.prv.rfc.restricted = false;



cmd.pub.quit = cmd.prv.quit = function (tokens, packet) {

var quitmsg = tokens[1] || "";

console.log('Quitting by order of ' + packet.sender);
packet.network.send(irc.outbound.quit(quitmsg));
process.exit();
console.log(e.message);
};

cmd.pub.quit.restricted = true;



cmd.pub.join = cmd.prv.join = function (tokens, packet) {

packet.network.send(irc.outbound.join(tokens[1]));

};

cmd.pub.join.restricted = true;
cmd.prv.join.restricted = true;



cmd.pub.part = cmd.prv.part = function (tokens, packet) {

if (tokens[1]) {
packet.network.send(irc.outbound.part(tokens[1]));
}
else if (packet.channel) {
packet.network.send(irc.outbound.part(packet.channel));
commandFun = cmd[dispatcher][name];

if (typeof commandFun === 'undefined') {
return;
}
};

cmd.pub.part.restricted = true;
cmd.prv.part.restricted = true;



cmd.pub.ignore = cmd.prv.ignore = function (tokens, packet) {

packet.ignored[tokens[1]] = true;
packet.network.send(irc.outbound.say(packet.sender, 'Ignoring ' +
tokens[1] + '.'));
};

cmd.pub.ignore.restricted = true;
cmd.prv.ignore.restricted = true;



cmd.pub.unignore = cmd.prv.unignore = function (tokens, packet) {
delete(packet.ignored[tokens[1]]);

packet.network.send(
irc.outbound.say(
packet.sender,
tokens[1] + ' is no longer being ignored.'
)
);
};

cmd.pub.unignore.restricted = true;
cmd.prv.unignore.restricted = true;



cmd.prv.say = function (tokens, packet) {

var where = tokens[1],
what = tokens[2];

packet.network.send(irc.outbound.say(where, what));
if (commandFun.restricted === false) {
commandFun(tokens, packet);
}
else if (typeof authd[packet.hostmask] !== 'undefined') {
commandFun(tokens, packet);
}

};

cmd.prv.say.restricted = true;
23 changes: 11 additions & 12 deletions config.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
{
"server": "irc.freenode.net",
"port": "6667",
"nick": "mutalirc",
"pass": "mutalirc",
"owner": "guidj0s",
"operators": {
"guidj0s": "test"
},
"version": "1.0",
"channels": ["#guidj0s"],
"gBooksAPIKey": "gbk",
"ignore": ["punk", "phunk"]
"server": "irc.freenode.net",
"port": "6667",
"nick": "mutalirc",
"pass": "mutalirc",
"owner": "guidj0s",
"operators": { "guidj0s": "test" },
"version": "1.0",
"channels": ["#guidj0s"],
"ignore": ["punk", "phunk"],
"gBooksAPIKey": "gbk",
"dispatcherDir": "./dispatchers"
}
125 changes: 125 additions & 0 deletions dispatchers/public.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
var pub = exports,
irc = require('../irc.js'),
rfc = require('../ietf.js'),
utl = require('../util.js'),
gbk = require('../gbooks.js');



pub.gbooks = function (tokens, packet) {

var opt = packet.options;

function sendBookResult(book) {
var msg = book ?
utl.interp('"{title}" - {authors}; Rating: {rating} ' +
'({ratings}); {pages} pages; {link}',
{
title: book.title,
authors: book.authors.join(', '),
rating: book.avgRating,
ratings: book.ratingsCount,
pages: book.pageCount,
link: book.link
}) :
'No results found on Google Books.';

packet.network.send(
irc.outbound.say(packet.channel, packet.sender + ': ' + msg)
);
}

tokens.shift();

gbk.getAPIKey(opt.gBooksAPIKey, function (key) {
gbk.search(key, tokens.join(' '), sendBookResult);
});

};

pub.gbooks.restricted = true;



pub.rfc = function(tokens, packet) {

tokens.shift();
rfc.search(tokens.join(' '), function (link) {

var msg = utl.interp('{who}: {lnk}',
{
who: packet.sender,
lnk: link
});

packet.network.send(
irc.outbound.say(packet.channel, msg)
);
});
};

pub.rfc.restricted = false;



pub.quit = function (tokens, packet) {

var quitmsg = tokens[1] || "";

console.log('Quitting by order of ' + packet.sender);
packet.network.send(irc.outbound.quit(quitmsg));
process.exit();
console.log(e.message);
};

pub.quit.restricted = true;



pub.join = function (tokens, packet) {

packet.network.send(irc.outbound.join(tokens[1]));

};

pub.join.restricted = true;



pub.part = function (tokens, packet) {

if (tokens[1]) {
packet.network.send(irc.outbound.part(tokens[1]));
}
else if (packet.channel) {
packet.network.send(irc.outbound.part(packet.channel));
}
};

pub.part.restricted = true;



pub.ignore = function (tokens, packet) {

packet.ignored[tokens[1]] = true;
packet.network.send(irc.outbound.say(packet.sender, 'Ignoring ' +
tokens[1] + '.'));
};

pub.ignore.restricted = true;



pub.unignore = function (tokens, packet) {
delete(packet.ignored[tokens[1]]);

packet.network.send(
irc.outbound.say(
packet.sender,
tokens[1] + ' is no longer being ignored.'
)
);
};

pub.unignore.restricted = true;
Loading

0 comments on commit 58edfbd

Please sign in to comment.