From 5d30261770c753dc37fa20b01079383323c2fe11 Mon Sep 17 00:00:00 2001 From: reality Date: Fri, 12 Apr 2013 19:03:01 +0000 Subject: [PATCH] Create addHandler API for link module and have dent use it. Removed its own listener. [#344] --- modules/dent/dent.js | 19 ++++++------------- modules/link/link.js | 17 ++++++++++++++++- 2 files changed, 22 insertions(+), 14 deletions(-) diff --git a/modules/dent/dent.js b/modules/dent/dent.js index fcbb8a4..6071932 100644 --- a/modules/dent/dent.js +++ b/modules/dent/dent.js @@ -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) { diff --git a/modules/link/link.js b/modules/link/link.js index 6cfcd9b..3657a38 100644 --- a/modules/link/link.js +++ b/modules/link/link.js @@ -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