diff --git a/modules/link/link.js b/modules/link/link.js index b1ff7ac..435c6ec 100644 --- a/modules/link/link.js +++ b/modules/link/link.js @@ -7,6 +7,19 @@ var request = require('request'); var link = function(dbot) { var urlRegex = /(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/ig; var links = {}; + var fetchTitle = function(event, link) { + request(link, function (error, response, body) { + if(!error && response.statusCode == 200) { + body = body.replace(/(\r\n|\n\r|\n)/gm, " "); + var title = body.valMatch(/(.*)<\/title>/, 2); + if(title) { + event.reply(title[1]); + } else { + event.reply(dbot.t('title_not_found')); + } + } + }); + }; var commands = { '~title': function(event) { @@ -17,18 +30,7 @@ var link = function(dbot) { link = urlMatches[0]; } } - - request(link, function (error, response, body) { - if(!error && response.statusCode == 200) { - body = body.replace(/(\r\n|\n\r|\n)/gm, " "); - var title = body.valMatch(/<title>(.*)<\/title>/, 2); - if(title) { - event.reply(title[1]); - } else { - event.reply(dbot.t('title_not_found')); - } - } - }); + fetchTitle(event, link); } }; @@ -43,15 +45,7 @@ var link = function(dbot) { links[event.channel.name] = urlMatches[0]; if(dbot.config.link.autoTitle == true) { - request(urlMatches[0], function (error, response, body) { - if(!error && response.statusCode == 200) { - body = body.replace(/(\r\n|\n\r|\n)/gm, " "); - var title = body.valMatch(/<title>(.*)<\/title>/, 2); - if(title) { - event.reply(title[1]); - } - } - }); + fetchTitle(event, urlMatches[0]); } } },