From b733564cb2e38bad6b4cace8d66b774101101d69 Mon Sep 17 00:00:00 2001 From: reality Date: Mon, 14 Jan 2013 23:11:50 +0000 Subject: [PATCH] fixt link [#131] --- modules/link/link.js | 42 ++++++++++++++++++++---------------------- 1 file changed, 20 insertions(+), 22 deletions(-) diff --git a/modules/link/link.js b/modules/link/link.js index 0c275eb..9965da6 100644 --- a/modules/link/link.js +++ b/modules/link/link.js @@ -7,9 +7,9 @@ var request = require('request'), _ = require('underscore')._; var link = function(dbot) { - var urlRegex = /(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/ig; - var links = {}; - var fetchTitle = function(event, link) { + this.urlRegex = /(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/ig; + this.links = {}; + this.fetchTitle = function(event, link) { request(link, function (error, response, body) { if(!error && response.statusCode == 200) { body = body.replace(/(\r\n|\n\r|\n)/gm, " "); @@ -23,36 +23,34 @@ var link = function(dbot) { var commands = { '~title': function(event) { - var link = links[event.channel.name]; - if(_.isUndefined(event.params[1])) { - var urlMatches = event.params[1].match(urlRegex); + var link = this.links[event.channel.name]; + if(!_.isUndefined(event.params[1])) { + var urlMatches = event.params[1].match(this.urlRegex); if(urlMatches !== null) { link = urlMatches[0]; } } - fetchTitle(event, link); + this.fetchTitle(event, link); } }; - return { - 'name': 'link', - 'ignorable': true, - 'commands': commands, + this.name = 'link'; + this.ignorable = true; + this.commands = commands; - 'listener': function(event) { - var urlMatches = event.message.match(urlRegex); - if(urlMatches !== null) { - links[event.channel.name] = urlMatches[0]; + this.listener = function(event) { + var urlMatches = event.message.match(this.urlRegex); + if(urlMatches !== null) { + this.links[event.channel.name] = urlMatches[0]; - if(dbot.config.link.autoTitle == true) { - fetchTitle(event, urlMatches[0]); - } + if(dbot.config.link.autoTitle == true) { + this.fetchTitle(event, urlMatches[0]); } - }, - 'on': 'PRIVMSG' - }; + } + }.bind(this); + this.on = 'PRIVMSG'; }; exports.fetch = function(dbot) { - return link(dbot); + return new link(dbot); };