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'),
|
2013-03-09 18:02:33 +01:00
|
|
|
_ = require('underscore')._,
|
|
|
|
ent = require('ent');
|
2013-01-13 16:30:53 +01:00
|
|
|
|
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 = {};
|
2013-04-12 21:03:01 +02:00
|
|
|
this.handlers = [];
|
2013-01-15 00:11:50 +01:00
|
|
|
this.fetchTitle = function(event, link) {
|
2013-04-05 12:30:47 +02:00
|
|
|
var limit = 1000000,
|
2013-03-25 19:23:35 +01:00
|
|
|
size = 0,
|
2013-04-13 00:38:00 +02:00
|
|
|
page = request(link.replace('https', 'http'), 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);
|
2013-02-20 11:28:47 +01:00
|
|
|
if(title && title.length < 140) {
|
2013-03-19 02:50:10 +01:00
|
|
|
event.reply(ent.decode(title[1]).trim());
|
2012-12-30 18:36:52 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2013-03-25 19:23:35 +01:00
|
|
|
|
|
|
|
page.on('data', function(chunk) {
|
|
|
|
size += chunk.length;
|
|
|
|
if(size > limit) {
|
|
|
|
page.abort();
|
|
|
|
}
|
|
|
|
});
|
2012-12-30 18:36:52 +01:00
|
|
|
};
|
2013-04-12 21:03:01 +02:00
|
|
|
|
|
|
|
this.api = {
|
2013-04-12 21:11:55 +02:00
|
|
|
'addHandler': function(name, regex, handler) {
|
|
|
|
this.handlers.push({
|
|
|
|
'name': name,
|
|
|
|
'regex': regex,
|
|
|
|
'callback': handler
|
|
|
|
});
|
2013-04-12 21:03:01 +02:00
|
|
|
}
|
|
|
|
};
|
2013-01-31 18:46:15 +01:00
|
|
|
|
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
|
|
|
},
|
2013-01-30 02:37:28 +01:00
|
|
|
|
|
|
|
'~xkcd': function(event) {
|
2013-01-31 18:46:15 +01:00
|
|
|
var comicId = event.params[1] || "";
|
|
|
|
|
|
|
|
if(comicId == "*") {
|
2013-01-30 18:28:34 +01:00
|
|
|
request("http://xkcd.com/info.0.json", function(error, response, body){
|
2013-01-31 18:46:15 +01:00
|
|
|
try {
|
|
|
|
if(response.statusCode == "200") {
|
|
|
|
data = JSON.parse(body);
|
|
|
|
event.params[1] = (Math.floor(Math.random() * data.num) + 1);
|
|
|
|
dbot.commands['~xkcd'](event);
|
|
|
|
}
|
|
|
|
} catch(err) { };
|
|
|
|
});
|
|
|
|
} else {
|
2013-01-31 18:53:58 +01:00
|
|
|
if(comicId !== "") {
|
2013-01-30 18:28:34 +01:00
|
|
|
comicId = comicId + "/";
|
|
|
|
}
|
2013-01-31 18:46:15 +01:00
|
|
|
|
2013-01-30 18:28:34 +01:00
|
|
|
var link = "http://xkcd.com/"+comicId+"info.0.json";
|
|
|
|
request(link, function(error, response, body) {
|
2013-01-31 18:46:15 +01:00
|
|
|
try {
|
|
|
|
if (response.statusCode == "200") {
|
|
|
|
data = JSON.parse(body);
|
|
|
|
event.reply(dbot.t("xkcd",data));
|
|
|
|
} else {
|
|
|
|
event.reply(dbot.t("no-hits"));
|
|
|
|
}
|
|
|
|
} catch(err) { };
|
2013-01-30 18:31:06 +01:00
|
|
|
});
|
|
|
|
}
|
2013-01-31 18:20:51 +01:00
|
|
|
|
2013-01-30 18:31:06 +01:00
|
|
|
},
|
2013-01-31 18:46:15 +01:00
|
|
|
|
2013-01-22 20:52:26 +01:00
|
|
|
'~ud': function(event) {
|
2013-01-30 18:31:06 +01:00
|
|
|
var query = event.input[1];
|
2013-01-23 19:56:07 +01:00
|
|
|
var reqUrl = 'http://api.urbandictionary.com/v0/define?term=' + encodeURI(query);
|
2013-01-31 18:46:15 +01:00
|
|
|
|
2013-01-22 20:52:26 +01:00
|
|
|
request(reqUrl, function(error, response, body) {
|
2013-01-31 18:46:15 +01:00
|
|
|
try {
|
2013-01-30 18:31:06 +01:00
|
|
|
var result = JSON.parse(body);
|
|
|
|
if(_.has(result, 'result_type') && result.result_type != 'no_results') {
|
2013-04-12 20:33:49 +02:00
|
|
|
event.reply(result.list[0].word + ': ' + result.list[0].definition.split('\n')[0]);
|
2013-01-30 18:31:06 +01:00
|
|
|
} else {
|
|
|
|
event.reply(event.user + ': No definition found.');
|
|
|
|
}
|
2013-01-31 18:46:15 +01:00
|
|
|
} catch(err) { }
|
2013-01-22 20:52:26 +01:00
|
|
|
});
|
2012-12-11 17:04:52 +01:00
|
|
|
}
|
|
|
|
};
|
2013-01-24 11:05:50 +01:00
|
|
|
commands['~ud'].regex = [/~ud (.+)/, 2];
|
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];
|
|
|
|
if(dbot.config.link.autoTitle == true) {
|
2013-04-12 21:03:01 +02:00
|
|
|
var handlerFound = false;
|
|
|
|
for(var i=0;i<this.handlers.length;i++) {
|
|
|
|
var matches = this.handlers[i].regex.exec(urlMatches[0]);
|
|
|
|
if(matches) {
|
2013-04-12 21:11:55 +02:00
|
|
|
this.handlers[i].callback(event, matches, this.handlers[i].name);
|
2013-04-12 21:03:01 +02:00
|
|
|
handlerFound = true; break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(!handlerFound) 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
|
|
|
};
|