From 3aab234110fa9ca699812c20388e29fac765a053 Mon Sep 17 00:00:00 2001 From: Luke Slater Date: Sat, 26 May 2012 22:28:34 +0100 Subject: [PATCH] Links module which can retrieve titles --- modules/link.js | 55 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 modules/link.js diff --git a/modules/link.js b/modules/link.js new file mode 100644 index 0000000..6eeaf88 --- /dev/null +++ b/modules/link.js @@ -0,0 +1,55 @@ +/** + * Module Name: Link + * Description: Stores recent channel links, with commands to retrieve + * information about links. + */ +var request = require('request'); +var link = function(dbot) { + var urlRegex = /(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/ig; + var links = {}; + + var commands = { + '~title': function(event) { + var link = links[event.channel]; + if(event.params[1] !== undefined) { + var urlMatches = event.params[1].match(urlRegex); + if(urlMatches !== null) { + link = urlMatches[0]; + } + } + + request(link, function (error, response, body) { + if(!error && response.statusCode == 200) { + var title = body.valMatch(/(.*)<\/title>/, 2); + if(title) { + event.reply(title[1]); + } else { + event.reply('no title found'); + } + } + }); + } + }; + + return { + 'name': 'js', + 'ignorable': true, + + 'onLoad': function() { + return commands; + }, + + 'listener': function(event) { + var urlMatches = event.message.match(urlRegex); + if(urlMatches !== null) { + links[event.channel] = urlMatches[0]; + } + }, + + 'on': 'PRIVMSG' + }; +}; + +exports.fetch = function(dbot) { + return link(dbot); +};