2012-12-11 17:04:52 +01:00
|
|
|
/**
|
|
|
|
* Module Name: Link
|
|
|
|
* Description: Stores recent channel links, with commands to retrieve
|
|
|
|
* information about links.
|
|
|
|
*/
|
2013-01-13 16:30:53 +01:00
|
|
|
var request = require('request'),
|
|
|
|
_ = require('underscore')._;
|
|
|
|
|
2012-12-11 17:04:52 +01:00
|
|
|
var link = function(dbot) {
|
2013-01-15 00:11:50 +01:00
|
|
|
this.urlRegex = /(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/ig;
|
|
|
|
this.links = {};
|
|
|
|
this.fetchTitle = function(event, link) {
|
2013-01-22 20:52:26 +01:00
|
|
|
request(link, function(error, response, body) {
|
2012-12-30 18:36:52 +01:00
|
|
|
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) {
|
2013-01-15 00:11:50 +01:00
|
|
|
var link = this.links[event.channel.name];
|
|
|
|
if(!_.isUndefined(event.params[1])) {
|
|
|
|
var urlMatches = event.params[1].match(this.urlRegex);
|
2012-12-11 17:04:52 +01:00
|
|
|
if(urlMatches !== null) {
|
|
|
|
link = urlMatches[0];
|
|
|
|
}
|
|
|
|
}
|
2013-01-15 00:11:50 +01:00
|
|
|
this.fetchTitle(event, link);
|
2013-01-22 20:52:26 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
'~ud': function(event) {
|
|
|
|
var reqUrl = 'http://api.urbandictionary.com/v0/define?term=' + event.params[1];
|
|
|
|
request(reqUrl, function(error, response, body) {
|
|
|
|
var result = JSON.parse(body);
|
|
|
|
if(_.has(result, 'result_type') && result.result_type != 'no_results') {
|
|
|
|
event.reply(event.params[1] + ': ' + result.list[0].definition);
|
|
|
|
} else {
|
|
|
|
event.reply(event.user + ': No definition found.');
|
|
|
|
}
|
|
|
|
});
|
2012-12-11 17:04:52 +01:00
|
|
|
}
|
|
|
|
};
|
2013-01-15 00:11:50 +01:00
|
|
|
this.commands = commands;
|
2012-12-11 17:04:52 +01:00
|
|
|
|
2013-01-15 00:11:50 +01:00
|
|
|
this.listener = function(event) {
|
|
|
|
var urlMatches = event.message.match(this.urlRegex);
|
|
|
|
if(urlMatches !== null) {
|
|
|
|
this.links[event.channel.name] = urlMatches[0];
|
2012-12-28 00:18:41 +01:00
|
|
|
|
2013-01-15 00:11:50 +01:00
|
|
|
if(dbot.config.link.autoTitle == true) {
|
|
|
|
this.fetchTitle(event, urlMatches[0]);
|
2012-12-11 17:04:52 +01:00
|
|
|
}
|
2013-01-15 00:11:50 +01:00
|
|
|
}
|
|
|
|
}.bind(this);
|
|
|
|
this.on = 'PRIVMSG';
|
2012-12-11 17:04:52 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
exports.fetch = function(dbot) {
|
2013-01-15 00:11:50 +01:00
|
|
|
return new link(dbot);
|
2012-12-11 17:04:52 +01:00
|
|
|
};
|