hardlounge/src/plugins/irc-events/link.js

192 lines
4.3 KiB
JavaScript
Raw Normal View History

"use strict";
const cheerio = require("cheerio");
const request = require("request");
const Helper = require("../../helper");
2017-06-26 09:01:55 +00:00
const findLinks = require("../../../client/js/libs/handlebars/ircmessageparser/findLinks");
const es = require("event-stream");
2014-09-27 19:17:05 +00:00
2015-04-29 19:55:13 +00:00
process.setMaxListeners(0);
2017-06-26 09:01:55 +00:00
module.exports = function(client, chan, msg) {
if (!Helper.config.prefetch) {
return;
}
2017-06-26 09:01:55 +00:00
// Remove all IRC formatting characters before searching for links
const cleanText = msg.text.replace(/\x02|\x1D|\x1F|\x16|\x0F|\x03(?:[0-9]{1,2}(?:,[0-9]{1,2})?)?/g, "");
// We will only try to prefetch http(s) links
const links = findLinks(cleanText).filter((w) => /^https?:\/\//.test(w.link));
if (links.length === 0) {
return;
}
2017-06-26 09:01:55 +00:00
const link = escapeHeader(links[0].link);
fetch(link, function(res) {
if (res === null) {
return;
}
parse(msg, link, res, client);
2014-09-27 19:17:05 +00:00
});
};
2014-09-27 23:47:04 +00:00
function parse(msg, url, res, client) {
2017-06-26 09:01:55 +00:00
const preview = {
2014-09-27 23:47:04 +00:00
type: "",
head: "",
body: "",
thumb: "",
2016-11-25 17:57:47 +00:00
link: url,
2014-09-27 23:47:04 +00:00
};
2014-09-27 19:17:05 +00:00
switch (res.type) {
case "text/html":
var $ = cheerio.load(res.text);
2017-06-26 09:01:55 +00:00
preview.type = "link";
preview.head =
2017-06-22 19:41:05 +00:00
$("meta[property=\"og:title\"]").attr("content")
|| $("title").text()
|| "";
2017-06-26 09:01:55 +00:00
preview.body =
$("meta[property=\"og:description\"]").attr("content")
|| $("meta[name=\"description\"]").attr("content")
|| "";
2017-06-26 09:01:55 +00:00
preview.thumb =
2015-09-30 22:39:57 +00:00
$("meta[property=\"og:image\"]").attr("content")
|| $("meta[name=\"twitter:image:src\"]").attr("content")
2014-10-14 18:51:27 +00:00
|| "";
// Make sure thumbnail is a valid url
2017-06-26 09:01:55 +00:00
if (!/^https?:\/\//.test(preview.thumb)) {
preview.thumb = "";
}
// Verify that thumbnail pic exists and is under allowed size
2017-06-26 09:01:55 +00:00
if (preview.thumb.length) {
fetch(escapeHeader(preview.thumb), (resThumb) => {
if (resThumb === null
|| !(/^image\/.+/.test(resThumb.type))
|| resThumb.size > (Helper.config.prefetchMaxImageSize * 1024)) {
2017-06-26 09:01:55 +00:00
preview.thumb = "";
}
2017-06-26 09:01:55 +00:00
emitPreview(client, msg, preview);
});
return;
}
2014-09-27 19:17:05 +00:00
break;
case "image/png":
case "image/gif":
case "image/jpg":
case "image/jpeg":
if (res.size < (Helper.config.prefetchMaxImageSize * 1024)) {
2017-06-26 09:01:55 +00:00
preview.type = "image";
} else {
return;
}
2014-09-27 19:17:05 +00:00
break;
default:
return;
}
2014-09-27 23:47:04 +00:00
2017-06-26 09:01:55 +00:00
emitPreview(client, msg, preview);
}
2017-06-26 09:01:55 +00:00
function emitPreview(client, msg, preview) {
// If there is no title but there is preview or description, set title
// otherwise bail out and show no preview
2017-06-26 09:01:55 +00:00
if (!preview.head.length && preview.type === "link") {
if (preview.thumb.length || preview.body.length) {
preview.head = "Untitled page";
} else {
return;
}
}
2017-06-26 09:01:55 +00:00
msg.preview = preview;
client.emit("msg:preview", {
id: msg.id,
preview: preview
});
2014-09-27 19:17:05 +00:00
}
function fetch(url, cb) {
2016-10-09 08:54:44 +00:00
let req;
2015-01-04 02:58:12 +00:00
try {
2016-10-09 08:54:44 +00:00
req = request.get({
url: url,
maxRedirects: 5,
timeout: 5000,
headers: {
"User-Agent": "Mozilla/5.0 (compatible; The Lounge IRC Client; +https://github.com/thelounge/lounge)"
}
});
2015-09-30 22:39:57 +00:00
} catch (e) {
return cb(null);
2015-01-04 02:58:12 +00:00
}
var length = 0;
var limit = Helper.config.prefetchMaxImageSize * 1024;
2014-12-23 01:06:11 +00:00
req
2015-09-30 22:39:57 +00:00
.on("response", function(res) {
if (!(/^image\/.+/.test(res.headers["content-type"]))) {
// if not image, limit download to 50kb, since we need only meta tags
// twitter.com sends opengraph meta tags within ~20kb of data for individual tweets
limit = 1024 * 50;
2014-12-23 01:06:11 +00:00
}
})
2015-09-30 22:39:57 +00:00
.on("error", function() {})
2014-12-23 01:06:11 +00:00
.pipe(es.map(function(data, next) {
length += data.length;
if (length > limit) {
req.response.req.abort();
}
next(null, data);
}))
.pipe(es.wait(function(err, data) {
if (err) {
return cb(null);
}
if (req.response.statusCode < 200 || req.response.statusCode > 299) {
return cb(null);
}
let type;
let size = parseInt(req.response.headers["content-length"], 10) || length;
if (size < length) {
size = length;
2014-12-23 01:06:11 +00:00
}
try {
2015-09-30 22:39:57 +00:00
type = req.response.headers["content-type"].split(/ *; */).shift();
} catch (e) {
type = {};
}
2014-12-23 01:06:11 +00:00
data = {
text: data,
type: type,
size: size
2014-12-23 01:06:11 +00:00
};
cb(data);
}));
2014-09-27 19:17:05 +00:00
}
// https://github.com/request/request/issues/2120
// https://github.com/nodejs/node/issues/1693
// https://github.com/alexeyten/descript/commit/50ee540b30188324198176e445330294922665fc
function escapeHeader(header) {
return header
.replace(/([\uD800-\uDBFF][\uDC00-\uDFFF])+/g, encodeURI)
.replace(/[\uD800-\uDFFF]/g, "")
.replace(/[\u0000-\u001F\u007F-\uFFFF]+/g, encodeURI);
}