3
0
mirror of https://github.com/reality/dbot.git synced 2024-11-30 16:09:27 +01:00
dbot/modules/link/link.js

165 lines
5.5 KiB
JavaScript
Raw Normal View History

/**
* 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
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.handlers = [];
this.api = {
'addHandler': function(name, regex, handler) {
this.handlers.push({
'name': name,
'regex': regex,
'callback': handler
});
},
'getTitle': function(link, callback) {
var limit = 1000000,
size = 0,
page = request(link.replace('https', 'http'), 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 && title.length < 140) {
callback(ent.decode(title[1]).trim());
}
}
});
page.on('data', function(chunk) {
size += chunk.length;
if(size > limit) {
page.abort();
}
});
},
'udLookup': function(query, callback) {
var reqUrl = 'http://api.urbandictionary.com/v0/define?term=' +
encodeURI(query);
request(reqUrl, function(error, response, body) {
try {
var result = JSON.parse(body);
if(_.has(result, 'result_type') && result.result_type != 'no_results') {
callback(result.list[0].word, result.list[0].definition.split('\n')[0]);
} else {
callback(false);
}
} catch(err) { callback(false); }
});
},
'parseLink': function(link, callback) {
var handler = false;
for(var i=0;i<this.handlers.length;i++) {
var matches = this.handlers[i].regex.exec(link);
if(matches) {
2013-08-06 00:11:25 +02:00
console.log(this.handlers[i].name);
handler = this.handlers[i];
break;
}
}
if(handler) {
this.handlers[i].callback(matches, this.handlers[i].name, function(parsed) {
callback(parsed);
});
} else {
this.api.getTitle(link, function(title) {
callback(dbot.t('link', { 'link': title } ));
});
}
}
};
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);
if(urlMatches !== null) {
link = urlMatches[0];
}
}
2013-05-20 17:24:24 +02:00
this.api.getTitle(link, function(title) {
2013-05-21 19:45:58 +02:00
event.reply(dbot.t('link', { 'link': title} ));
});
2013-01-22 20:52:26 +01:00
},
'~xkcd': function(event) {
var comicId = event.params[1] || "";
if(comicId == "*") {
request("http://xkcd.com/info.0.json", function(error, response, body){
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 !== "") {
comicId = comicId + "/";
}
var link = "http://xkcd.com/"+comicId+"info.0.json";
request(link, function(error, response, body) {
try {
if (response.statusCode == "200") {
data = JSON.parse(body);
2013-05-21 19:45:58 +02:00
event.reply(dbot.t("xkcd", data));
} else {
event.reply(dbot.t("no-hits"));
}
} catch(err) { };
});
}
2013-01-31 18:20:51 +01:00
},
2013-01-22 20:52:26 +01:00
'~ud': function(event) {
var query = event.input[1];
this.api.udLookup(query, function(word, definition) {
if(word) {
event.reply(word + ': ' + definition);
} else {
event.reply(event.user + ': No definition found.');
}
2013-01-22 20:52:26 +01:00
});
}
};
commands['~ud'].regex = [/~ud (.+)/, 2];
2013-01-15 00:11:50 +01:00
this.commands = commands;
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];
2013-08-06 00:11:25 +02:00
console.log('DEBUG: got a link');
if(this.config.autoTitle == true) {
this.api.parseLink(urlMatches[0], function(result) {
event.reply(result);
});
}
2013-01-15 00:11:50 +01:00
}
}.bind(this);
this.on = 'PRIVMSG';
};
exports.fetch = function(dbot) {
2013-01-15 00:11:50 +01:00
return new link(dbot);
};