Create addHandler API for link module and have dent use it. Removed its own listener. [#344]

This commit is contained in:
reality 2013-04-12 19:03:01 +00:00
parent e667d95ca8
commit 5d30261770
2 changed files with 22 additions and 14 deletions

View File

@ -64,20 +64,13 @@ var dent = function(dbot) {
}
}.bind(this));
}
}.bind(this);
this.listener = function(event) {
for (s in this.StatusRegex) {
if (this.StatusRegex.hasOwnProperty(s)) {
var matches = this.StatusRegex[s].exec(event.message);
if (matches != null) {
this.lookup(event, matches[1], s);
}
}
}
}.bind(this);
this.on = 'PRIVMSG';
for(s in this.StatusRegex) {
dbot.api.link.addHandler(this.StatusRegex[s], function(event, matches) {
this.lookup(event, matches[1], s);
}.bind(this));
}
}.bind(this);
};
exports.fetch = function(dbot) {

View File

@ -10,6 +10,7 @@ var request = require('request'),
var link = function(dbot) {
this.urlRegex = /(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/ig;
this.links = {};
this.handlers = [];
this.fetchTitle = function(event, link) {
var limit = 1000000,
size = 0,
@ -30,6 +31,12 @@ var link = function(dbot) {
}
});
};
this.api = {
'addHandler': function(regex, handler) {
this.handlers.push({ 'regex': regex, 'callback': handler });
}
};
var commands = {
'~title': function(event) {
@ -100,7 +107,15 @@ var link = function(dbot) {
if(urlMatches !== null) {
this.links[event.channel.name] = urlMatches[0];
if(dbot.config.link.autoTitle == true) {
this.fetchTitle(event, urlMatches[0]);
var handlerFound = false;
for(var i=0;i<this.handlers.length;i++) {
var matches = this.handlers[i].regex.exec(urlMatches[0]);
if(matches) {
this.handlers[i].callback(event, matches);
handlerFound = true; break;
}
}
if(!handlerFound) this.fetchTitle(event, urlMatches[0]);
}
}
}.bind(this);