forked from GitHub/dbot
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));
|
||||||
}
|
}
|
||||||
}.bind(this);
|
|
||||||
|
|
||||||
this.listener = function(event) {
|
for(s in this.StatusRegex) {
|
||||||
for (s in this.StatusRegex) {
|
dbot.api.link.addHandler(this.StatusRegex[s], function(event, matches) {
|
||||||
if (this.StatusRegex.hasOwnProperty(s)) {
|
this.lookup(event, matches[1], s);
|
||||||
var matches = this.StatusRegex[s].exec(event.message);
|
}.bind(this));
|
||||||
if (matches != null) {
|
|
||||||
this.lookup(event, matches[1], s);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}.bind(this);
|
}.bind(this);
|
||||||
|
|
||||||
this.on = 'PRIVMSG';
|
|
||||||
};
|
};
|
||||||
|
|
||||||
exports.fetch = function(dbot) {
|
exports.fetch = function(dbot) {
|
||||||
|
@ -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);
|
||||||
|
Loading…
Reference in New Issue
Block a user