2012-05-23 20:45:14 +02:00
|
|
|
/**
|
|
|
|
* Module Name: AutoShorten
|
|
|
|
* Description: Automatically shorten link over a certain length and post the
|
|
|
|
* short link to the channel.
|
|
|
|
*/
|
2012-04-14 07:08:54 +02:00
|
|
|
var http = require('http');
|
|
|
|
|
2012-04-14 07:06:40 +02:00
|
|
|
var autoshorten = function(dbot) {
|
|
|
|
return {
|
2012-06-05 01:17:51 +02:00
|
|
|
'name': 'autoshorten',
|
|
|
|
'ignorable': true,
|
|
|
|
|
2012-05-23 20:45:14 +02:00
|
|
|
'listener': function(event) {
|
2012-05-25 17:45:47 +02:00
|
|
|
var urlRegex = /(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/ig;
|
|
|
|
var urlMatches = event.message.match(urlRegex);
|
2012-04-15 23:04:58 +02:00
|
|
|
|
2012-05-25 17:45:47 +02:00
|
|
|
if(urlMatches !== null && urlMatches[0].length > 80) {
|
|
|
|
var url = urlMatches[0]; // Only doing one, screw you.
|
|
|
|
|
|
|
|
// TODO: Make this use a decent URL shortener. Mine is shit.
|
|
|
|
var options = {
|
2012-12-06 20:37:39 +01:00
|
|
|
'host': dbot.webHost,
|
2012-12-05 20:13:04 +01:00
|
|
|
'port': dbot.webPort,
|
2012-05-25 17:45:47 +02:00
|
|
|
'path': '/mkurl?url=' + escape(url)
|
|
|
|
};
|
2012-04-15 23:04:58 +02:00
|
|
|
|
2012-05-25 17:45:47 +02:00
|
|
|
http.get(options, function(res) {
|
|
|
|
res.setEncoding('utf8');
|
|
|
|
res.on('data', function (response) {
|
|
|
|
event.reply(dbot.t('shorten_link', {'user': event.user}) + JSON.parse(response).surl);
|
2012-04-14 07:26:48 +02:00
|
|
|
});
|
2012-05-25 17:45:47 +02:00
|
|
|
});
|
2012-04-14 07:06:40 +02:00
|
|
|
}
|
|
|
|
},
|
2012-06-05 01:17:51 +02:00
|
|
|
'on': 'PRIVMSG'
|
2012-04-14 07:06:40 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
exports.fetch = function(dbot) {
|
|
|
|
return autoshorten(dbot);
|
|
|
|
};
|