3
0
mirror of https://github.com/reality/dbot.git synced 2025-02-02 15:44:33 +01:00

fixt link [#131]

This commit is contained in:
reality 2013-01-14 23:11:50 +00:00
parent f532aee4ab
commit b733564cb2

View File

@ -7,9 +7,9 @@ var request = require('request'),
_ = require('underscore')._; _ = require('underscore')._;
var link = function(dbot) { var link = function(dbot) {
var urlRegex = /(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/ig; this.urlRegex = /(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/ig;
var links = {}; this.links = {};
var fetchTitle = function(event, link) { this.fetchTitle = function(event, link) {
request(link, function (error, response, body) { request(link, function (error, response, body) {
if(!error && response.statusCode == 200) { if(!error && response.statusCode == 200) {
body = body.replace(/(\r\n|\n\r|\n)/gm, " "); body = body.replace(/(\r\n|\n\r|\n)/gm, " ");
@ -23,36 +23,34 @@ var link = function(dbot) {
var commands = { var commands = {
'~title': function(event) { '~title': function(event) {
var link = links[event.channel.name]; var link = this.links[event.channel.name];
if(_.isUndefined(event.params[1])) { if(!_.isUndefined(event.params[1])) {
var urlMatches = event.params[1].match(urlRegex); var urlMatches = event.params[1].match(this.urlRegex);
if(urlMatches !== null) { if(urlMatches !== null) {
link = urlMatches[0]; link = urlMatches[0];
} }
} }
fetchTitle(event, link); this.fetchTitle(event, link);
} }
}; };
return { this.name = 'link';
'name': 'link', this.ignorable = true;
'ignorable': true, this.commands = commands;
'commands': commands,
'listener': function(event) { this.listener = function(event) {
var urlMatches = event.message.match(urlRegex); var urlMatches = event.message.match(this.urlRegex);
if(urlMatches !== null) { if(urlMatches !== null) {
links[event.channel.name] = urlMatches[0]; this.links[event.channel.name] = urlMatches[0];
if(dbot.config.link.autoTitle == true) { if(dbot.config.link.autoTitle == true) {
fetchTitle(event, urlMatches[0]); this.fetchTitle(event, urlMatches[0]);
} }
} }
}, }.bind(this);
'on': 'PRIVMSG' this.on = 'PRIVMSG';
};
}; };
exports.fetch = function(dbot) { exports.fetch = function(dbot) {
return link(dbot); return new link(dbot);
}; };