mirror of
https://github.com/reality/dbot.git
synced 2024-12-24 03:33:07 +01:00
Create addHandler API for link module and have dent use it. Removed its own listener. [#344]
This commit is contained in:
parent
e667d95ca8
commit
5d30261770
@ -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) {
|
||||
|
@ -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);
|
||||
|
Loading…
Reference in New Issue
Block a user