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;
|
2018-04-27 01:12:20 +02:00
|
|
|
this.links = {};
|
2013-04-12 21:03:01 +02:00
|
|
|
this.handlers = [];
|
2018-04-27 01:12:20 +02:00
|
|
|
|
2013-04-12 21:03:01 +02:00
|
|
|
this.api = {
|
2013-04-12 21:11:55 +02:00
|
|
|
'addHandler': function(name, regex, handler) {
|
2018-04-27 01:12:20 +02:00
|
|
|
this.handlers.push({
|
|
|
|
'name': name,
|
|
|
|
'regex': regex,
|
|
|
|
'callback': handler
|
2013-04-12 21:11:55 +02:00
|
|
|
});
|
2013-05-20 17:05:00 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
'getTitle': function(link, callback) {
|
|
|
|
var limit = 1000000,
|
|
|
|
size = 0,
|
|
|
|
page = request(link.replace('https', 'http'), function(error, response, body) {
|
|
|
|
if(!error && response.statusCode == 200) {
|
2015-02-04 19:52:35 +01:00
|
|
|
body = body.replace(/(\r\n|\n\r|\n)/gim, " ");
|
2018-04-14 19:38:28 +02:00
|
|
|
var title = body.valMatch(/<title>(.*?)<\\?\/title>/, 2);
|
2013-05-20 17:05:00 +02:00
|
|
|
if(title && title.length < 140) {
|
|
|
|
callback(ent.decode(title[1]).trim());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
page.on('data', function(chunk) {
|
|
|
|
size += chunk.length;
|
|
|
|
if(size > limit) {
|
|
|
|
page.abort();
|
|
|
|
}
|
|
|
|
});
|
2013-05-27 19:16:23 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
'udLookup': function(query, callback) {
|
2018-04-27 01:12:20 +02:00
|
|
|
var reqUrl = 'http://api.urbandictionary.com/v0/define?term=' +
|
|
|
|
encodeURI(query);
|
2013-05-27 19:16:23 +02:00
|
|
|
|
|
|
|
request(reqUrl, function(error, response, body) {
|
2018-08-27 00:08:01 +02:00
|
|
|
console.log(reqUrl);
|
|
|
|
|
2013-05-27 19:16:23 +02:00
|
|
|
try {
|
|
|
|
var result = JSON.parse(body);
|
2018-08-27 00:08:01 +02:00
|
|
|
if(_.has(result, 'list') && result.list.length > 0) {
|
2013-05-27 19:16:23 +02:00
|
|
|
callback(result.list[0].word, result.list[0].definition.split('\n')[0]);
|
|
|
|
} else {
|
|
|
|
callback(false);
|
|
|
|
}
|
2018-08-27 00:08:01 +02:00
|
|
|
} catch(err) {
|
|
|
|
console.log(err);
|
|
|
|
callback(false);
|
|
|
|
}
|
2013-05-27 19:16:23 +02:00
|
|
|
});
|
|
|
|
|
2013-07-25 23:56:44 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
'parseLink': function(link, callback) {
|
|
|
|
var handler = false;
|
|
|
|
for(var i=0;i<this.handlers.length;i++) {
|
2018-04-27 01:12:20 +02:00
|
|
|
var matches = this.handlers[i].regex.exec(link);
|
2013-07-25 23:56:44 +02:00
|
|
|
if(matches) {
|
2013-08-06 00:11:25 +02:00
|
|
|
console.log(this.handlers[i].name);
|
2013-07-25 23:56:44 +02:00
|
|
|
handler = this.handlers[i];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2018-04-27 01:12:20 +02:00
|
|
|
|
2013-07-25 23:56:44 +02:00
|
|
|
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 } ));
|
|
|
|
});
|
|
|
|
}
|
2013-04-12 21:03:01 +02:00
|
|
|
}
|
|
|
|
};
|
2018-04-27 01:12:20 +02: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-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-05-20 17:05:00 +02:00
|
|
|
});
|
2013-01-22 20:52:26 +01:00
|
|
|
},
|
2018-04-27 01:12:20 +02:00
|
|
|
|
2013-01-30 02:37:28 +01:00
|
|
|
'~xkcd': function(event) {
|
2018-04-27 01:12:20 +02:00
|
|
|
//var comicId = event.params[1] || "";
|
|
|
|
var comicId = event.params.slice(1).join(' ');
|
2013-01-31 18:46:15 +01:00
|
|
|
|
|
|
|
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) { };
|
2018-04-27 01:12:20 +02:00
|
|
|
});
|
2013-01-31 18:46:15 +01:00
|
|
|
} else {
|
2018-04-26 23:14:18 +02:00
|
|
|
if (isNaN(parseInt(comicId))) {
|
|
|
|
request({
|
2018-04-27 01:12:20 +02:00
|
|
|
url: 'http://www.explainxkcd.com/wiki/api.php',
|
|
|
|
qs: {
|
|
|
|
action: 'query',
|
|
|
|
format: 'json',
|
|
|
|
generator: 'search',
|
|
|
|
gsrwhat: 'text',
|
|
|
|
gsrsearch: comicId,
|
|
|
|
prop: 'info|categories',
|
|
|
|
gsrlimit: 50
|
|
|
|
},
|
|
|
|
json: true
|
2018-04-26 23:14:18 +02:00
|
|
|
}, function(err, res, body) {
|
2018-04-27 01:12:20 +02:00
|
|
|
if(!body) {
|
|
|
|
event.reply(dbot.t("no-hits"));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var pages = _.values(body.query.pages);
|
|
|
|
|
|
|
|
// page titles must be of the format "####: $$$$$$"
|
|
|
|
pages = _.filter(pages, p => p.title.indexOf(':') > 0);
|
|
|
|
|
|
|
|
if (pages.length > 0) {
|
|
|
|
// See if any of these matches are exact title matches
|
|
|
|
var match = false;
|
|
|
|
_.each(pages, function(p) {
|
|
|
|
var title = p.title.slice(p.title.indexOf(':')+2).trim();
|
|
|
|
if(title.toLowerCase() == comicId.toLowerCase()) {
|
|
|
|
match = p;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
if (match) {
|
|
|
|
// We got a match! Get the ID and let's get tf out of here.
|
|
|
|
comicId = match.title.slice(0, match.title.indexOf(':'));
|
|
|
|
} else {
|
|
|
|
comicId = pages[0].title.slice(0, pages[0].title.indexOf(':'));
|
|
|
|
}
|
2018-04-26 23:14:18 +02:00
|
|
|
|
|
|
|
var link = "http://xkcd.com/"+comicId+"/info.0.json";
|
|
|
|
request(link, function(error, response, body) {
|
|
|
|
try {
|
|
|
|
if (response.statusCode == "200") {
|
|
|
|
data = JSON.parse(body);
|
|
|
|
event.reply(dbot.t("xkcd", data));
|
|
|
|
} else {
|
|
|
|
event.reply(dbot.t("no-hits"));
|
|
|
|
}
|
|
|
|
} catch(err) { };
|
|
|
|
});
|
2018-04-27 01:12:20 +02:00
|
|
|
|
|
|
|
|
|
|
|
} else {
|
|
|
|
event.reply(dbot.t("no-hits"));
|
|
|
|
}
|
2018-04-26 23:14:18 +02:00
|
|
|
});
|
|
|
|
} else {
|
|
|
|
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);
|
|
|
|
event.reply(dbot.t("xkcd", data));
|
|
|
|
} else {
|
|
|
|
event.reply(dbot.t("no-hits"));
|
|
|
|
}
|
|
|
|
} catch(err) { };
|
|
|
|
});
|
|
|
|
}
|
2013-01-30 18:31:06 +01:00
|
|
|
}
|
|
|
|
},
|
2018-04-27 01:12:20 +02: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-31 18:46:15 +01:00
|
|
|
|
2013-05-27 19:16:23 +02:00
|
|
|
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
|
|
|
});
|
2012-12-11 17:04:52 +01:00
|
|
|
}
|
|
|
|
};
|
2013-12-29 19:38:24 +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];
|
2021-08-29 20:24:53 +02:00
|
|
|
if(dbot.config.debugMode) {
|
2013-08-06 00:11:25 +02:00
|
|
|
console.log('DEBUG: got a link');
|
2021-08-29 20:24:53 +02:00
|
|
|
}
|
2013-06-04 02:10:12 +02:00
|
|
|
if(this.config.autoTitle == true) {
|
2013-07-25 23:56:44 +02:00
|
|
|
this.api.parseLink(urlMatches[0], function(result) {
|
2018-04-27 01:12:20 +02:00
|
|
|
event.reply(result);
|
2013-07-25 23:56:44 +02:00
|
|
|
});
|
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
|
|
|
};
|