2012-12-11 17:04:52 +01:00
|
|
|
/**
|
|
|
|
* 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 = {};
|
2012-12-30 18:36:52 +01:00
|
|
|
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>(.*)<\/title>/, 2);
|
|
|
|
if(title) {
|
|
|
|
event.reply(title[1]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
2012-12-11 17:04:52 +01:00
|
|
|
|
|
|
|
var commands = {
|
|
|
|
'~title': function(event) {
|
|
|
|
var link = links[event.channel.name];
|
|
|
|
if(event.params[1] !== undefined) {
|
|
|
|
var urlMatches = event.params[1].match(urlRegex);
|
|
|
|
if(urlMatches !== null) {
|
|
|
|
link = urlMatches[0];
|
|
|
|
}
|
|
|
|
}
|
2012-12-30 18:36:52 +01:00
|
|
|
fetchTitle(event, link);
|
2012-12-11 17:04:52 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
return {
|
|
|
|
'name': 'link',
|
|
|
|
'ignorable': true,
|
|
|
|
'commands': commands,
|
|
|
|
|
|
|
|
'listener': function(event) {
|
|
|
|
var urlMatches = event.message.match(urlRegex);
|
|
|
|
if(urlMatches !== null) {
|
|
|
|
links[event.channel.name] = urlMatches[0];
|
2012-12-28 00:18:41 +01:00
|
|
|
|
|
|
|
if(dbot.config.link.autoTitle == true) {
|
2012-12-30 18:36:52 +01:00
|
|
|
fetchTitle(event, urlMatches[0]);
|
2012-12-28 00:18:41 +01:00
|
|
|
}
|
2012-12-11 17:04:52 +01:00
|
|
|
}
|
|
|
|
},
|
|
|
|
'on': 'PRIVMSG'
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.fetch = function(dbot) {
|
|
|
|
return link(dbot);
|
|
|
|
};
|