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));
} }
}.bind(this);
this.listener = function(event) {
for(s in this.StatusRegex) { for(s in this.StatusRegex) {
if (this.StatusRegex.hasOwnProperty(s)) { dbot.api.link.addHandler(this.StatusRegex[s], function(event, matches) {
var matches = this.StatusRegex[s].exec(event.message);
if (matches != null) {
this.lookup(event, matches[1], s); this.lookup(event, matches[1], s);
} }.bind(this));
}
} }
}.bind(this); }.bind(this);
this.on = 'PRIVMSG';
}; };
exports.fetch = function(dbot) { exports.fetch = function(dbot) {

View File

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