Permalink
Browse files

Fixing admin plugin and adding allow/deny for chans/nicks

  • Loading branch information...
1 parent 819fbea commit d4b4fe31d1ab82bf7630c24c1819b9ee7f41e52e @draggor draggor committed Mar 31, 2011
Showing with 254 additions and 42 deletions.
  1. +5 −4 example/bot.js
  2. +87 −6 index.js
  3. +1 −1 plugins/admin/cmd.js
  4. +126 −31 plugins/farkle/index.js
  5. +35 −0 plugins/farkle/test.js
View
@@ -6,15 +6,16 @@ process.on('uncaughtException', function(err) {
console.log('Uncaught Exception: ' + err);
});
-b = new bot('irc.freenode.net', 'nodeboy1184-2', {
+b = new bot('irc.freenode.net', 'nodeboy1184', {
+ port: 6667,
userName: 'nodeboy',
realName: 'nodeboy',
-// debug: true,
+ debug: true,
secure: false,
- channels: ['#dracolair']
+ channels: ['#farkle', '#dracolair']
});
-b.loadPlugin('admin');
+b.loadPlugin('admin', {nick: ['draggor']});
//b.loadPlugin('dice');
var r = repl.start();
View
@@ -3,6 +3,16 @@ var irc = require('irc')
, util = require('./util')
;
+Array.prototype.has = function(item, c) {
+ c = c || function(a, b) { return a === b; };
+ for(var i = 0; i < this.length; i++) {
+ if(c(this[i], item)) {
+ return i;
+ }
+ }
+ return -1;
+};
+
function bot(server, nick, options) {
this.server = server;
this.nick = nick;
@@ -11,27 +21,87 @@ function bot(server, nick, options) {
this.throttle = options.throttle || 2000;
this.client = new irc.Client(server, nick, options);
this.say = util.throttle(irc.Client.prototype.say, this.throttle, this.client);
+ this.listeners = {};
}
function sanitize(str) {
return str.replace(/\.\.\//g, '');
}
-bot.prototype.modifyListeners = function(plugin, func) {
- var mod = func + 'Listener';
+bot.prototype.addListeners = function(plugin) {
+ var opt = plugin.options
+ , req = []
+ , reqf = function(listener) { return listener; }
+ ;
+ if(opt.chan) {
+ req.push(function(from, to, msg) {
+ return !!~opt.chan.indexOf(to.toLowerCase());
+ });
+ } else if (opt['!chan']) {
+ req.push(function(from, to, msg) {
+ return !~opt.chan.indexOf(to.toLowerCase());
+ });
+ }
+ if(opt.nick) {
+ req.push(function(from, to, msg) {
+ return !!~opt.nick.indexOf(from.toLowerCase());
+ });
+ } else if (opt['!nick']) {
+ req.push(function(from, to, msg) {
+ return !~opt.nick.indexOf(from.toLowerCase());
+ });
+ }
+ if(opt.chan || opt.nick) {
+ reqf = function(listener) {
+ return function(from, to, msg) {
+ if(!!~req.map(function(f) { return f(from, to, msg); }).indexOf(true)) {
+ listener(from, to, msg);
+ }
+ };
+ };
+ }
for(var i in plugin.listeners) {
var listener = plugin.listeners[i];
if(typeof listener === 'function') {
- this.modifyListener(i, listener, mod);
+ var f = reqf(listener);
+ this.listeners[opt.prefix + i] = f;
+ this.modifyListener(i, f, 'add');
} else {
for(var j in listener) {
- this.modifyListener(i, listener[j], mod);
+ var f = reqf(listener[j]);
+ this.listeners[opt.prefix + i] = this.listeners[opt.prefix + i] || [];
+ this.listeners[opt.prefix + i].push(f);
+ this.modifyListener(i, f, 'add');
}
}
}
};
+bot.prototype.removeListeners = function(plugin) {
+ for(var i in plugin.listeners) {
+ var listener = this.listeners[plugin.options.prefix + i];
+ if(typeof listener === 'function') {
+ this.modifyListener(i, listener, 'remove');
+ } else {
+ for(var j in listener) {
+ this.modifyListener(i, listener[j], 'remove');
+ }
+ }
+ delete this.listeners[plugin.options.prefix + i];
+ }
+
+}
+
+bot.prototype.chanListener = function(func) {
+ return function(from, to, msg) {
+ if(to[0] === '#' && to !== this.nick) {
+ func(from, to, msg);
+ }
+ };
+};
+
bot.prototype.modifyListener = function(name, func, mod) {
+ mod += 'Listener';
switch(name) {
case 'message':
this.client[mod]('message', func);
@@ -42,6 +112,15 @@ bot.prototype.modifyListener = function(name, func, mod) {
}
};
+bot.prototype.getPlugin = function(name) {
+ var cleanName = './plugins' + sanitize(name)
+ , full = require.resolve(cleanName)
+ , pl = require.cache[full]
+ ;
+
+ return pl;
+};
+
bot.prototype.loadPlugin = function(name, options) {
var cleanName = './plugins/' + sanitize(name)
, full = require.resolve(cleanName)
@@ -61,11 +140,12 @@ bot.prototype.loadPlugin = function(name, options) {
pl = require(full);
pl.options = options || {};
+ pl.options.prefix = name;
pl.bot = this;
this.plugins[cleanName] = pl;
- this.modifyListeners(pl, 'add');
+ this.addListeners(pl);
};
bot.prototype.unloadPlugin = function(name, options) {
@@ -74,9 +154,10 @@ bot.prototype.unloadPlugin = function(name, options) {
;
pl.options = options || {};
+ pl.options.prefix = name;
if(pl) {
- this.modifyListeners(pl, 'remove');
+ this.removeListeners(pl);
delete this.plugins[cleanName];
}
};
View
@@ -10,7 +10,7 @@ exports.run = function(info) {
};
function listArgsCommand(func, info) {
- var sp = info.rest.split(' ')
+ var sp = info.rest.toLowerCase().split(' ')
, name = sp.shift()
, args = {}
;
Oops, something went wrong.

0 comments on commit d4b4fe3

Please sign in to comment.