2016-10-09 19:14:02 +00:00
|
|
|
"use strict";
|
|
|
|
|
2016-12-09 20:46:53 +00:00
|
|
|
const cheerio = require("cheerio");
|
|
|
|
const request = require("request");
|
2017-08-13 09:58:27 +00:00
|
|
|
const url = require("url");
|
2017-12-30 10:46:51 +00:00
|
|
|
const mime = require("mime-types");
|
2016-12-09 20:46:53 +00:00
|
|
|
const Helper = require("../../helper");
|
2017-09-28 08:58:43 +00:00
|
|
|
const cleanIrcMessage = require("../../../client/js/libs/handlebars/ircmessageparser/cleanIrcMessage");
|
2017-06-26 09:01:55 +00:00
|
|
|
const findLinks = require("../../../client/js/libs/handlebars/ircmessageparser/findLinks");
|
2017-07-06 15:33:09 +00:00
|
|
|
const storage = require("../storage");
|
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) {
|
2016-12-09 20:46:53 +00:00
|
|
|
if (!Helper.config.prefetch) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-06-26 09:01:55 +00:00
|
|
|
// Remove all IRC formatting characters before searching for links
|
2017-09-28 08:58:43 +00:00
|
|
|
const cleanText = cleanIrcMessage(msg.text);
|
2017-06-26 09:01:55 +00:00
|
|
|
|
|
|
|
// We will only try to prefetch http(s) links
|
|
|
|
const links = findLinks(cleanText).filter((w) => /^https?:\/\//.test(w.link));
|
2016-12-09 20:46:53 +00:00
|
|
|
|
|
|
|
if (links.length === 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-07-21 05:28:51 +00:00
|
|
|
msg.previews = Array.from(new Set( // Remove duplicate links
|
2017-07-06 06:16:01 +00:00
|
|
|
links.map((link) => escapeHeader(link.link))
|
2017-07-21 05:28:51 +00:00
|
|
|
)).map((link) => ({
|
|
|
|
type: "loading",
|
|
|
|
head: "",
|
|
|
|
body: "",
|
|
|
|
thumb: "",
|
|
|
|
link: link,
|
2017-07-24 06:01:25 +00:00
|
|
|
shown: true,
|
2017-07-21 05:28:51 +00:00
|
|
|
})).slice(0, 5); // Only preview the first 5 URLs in message to avoid abuse
|
2017-06-23 17:49:45 +00:00
|
|
|
|
2017-07-21 05:28:51 +00:00
|
|
|
msg.previews.forEach((preview) => {
|
|
|
|
fetch(preview.link, function(res) {
|
2017-07-19 05:26:29 +00:00
|
|
|
if (res === null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-07-21 05:28:51 +00:00
|
|
|
parse(msg, preview, res, client);
|
2017-07-06 06:16:01 +00:00
|
|
|
});
|
2017-07-19 05:26:29 +00:00
|
|
|
});
|
2014-09-27 19:17:05 +00:00
|
|
|
};
|
|
|
|
|
2017-07-21 05:28:51 +00:00
|
|
|
function parse(msg, preview, res, client) {
|
2014-09-27 19:17:05 +00:00
|
|
|
switch (res.type) {
|
|
|
|
case "text/html":
|
2017-07-06 15:33:09 +00:00
|
|
|
var $ = cheerio.load(res.data);
|
2017-06-26 09:01:55 +00:00
|
|
|
preview.type = "link";
|
|
|
|
preview.head =
|
2017-11-26 22:34:28 +00:00
|
|
|
$('meta[property="og:title"]').attr("content")
|
2017-06-22 19:41:05 +00:00
|
|
|
|| $("title").text()
|
2017-06-26 06:27:51 +00:00
|
|
|
|| "";
|
2017-06-26 09:01:55 +00:00
|
|
|
preview.body =
|
2017-11-26 22:34:28 +00:00
|
|
|
$('meta[property="og:description"]').attr("content")
|
|
|
|
|| $('meta[name="description"]').attr("content")
|
2017-06-26 06:27:51 +00:00
|
|
|
|| "";
|
2017-06-26 09:01:55 +00:00
|
|
|
preview.thumb =
|
2017-11-26 22:34:28 +00:00
|
|
|
$('meta[property="og:image"]').attr("content")
|
|
|
|
|| $('meta[name="twitter:image:src"]').attr("content")
|
|
|
|
|| $('link[rel="image_src"]').attr("href")
|
2014-10-14 18:51:27 +00:00
|
|
|
|| "";
|
2017-06-22 19:32:13 +00:00
|
|
|
|
2017-08-13 09:58:27 +00:00
|
|
|
if (preview.thumb.length) {
|
|
|
|
preview.thumb = url.resolve(preview.link, preview.thumb);
|
|
|
|
}
|
|
|
|
|
2017-06-22 19:32:13 +00:00
|
|
|
// Make sure thumbnail is a valid url
|
2017-06-26 09:01:55 +00:00
|
|
|
if (!/^https?:\/\//.test(preview.thumb)) {
|
|
|
|
preview.thumb = "";
|
2017-06-22 19:32:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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) => {
|
2017-06-26 06:27:51 +00:00
|
|
|
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-22 19:32:13 +00:00
|
|
|
}
|
|
|
|
|
2017-07-06 15:33:09 +00:00
|
|
|
handlePreview(client, msg, preview, resThumb);
|
2017-06-22 19:32:13 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-09-27 19:17:05 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case "image/png":
|
|
|
|
case "image/gif":
|
|
|
|
case "image/jpg":
|
|
|
|
case "image/jpeg":
|
2017-11-27 13:50:35 +00:00
|
|
|
case "image/webp":
|
2017-07-06 15:33:09 +00:00
|
|
|
if (res.size > (Helper.config.prefetchMaxImageSize * 1024)) {
|
2017-12-28 16:28:49 +00:00
|
|
|
preview.type = "error";
|
|
|
|
preview.head = "Large image";
|
|
|
|
preview.body = "Image is greater than your max image size. Click to view.";
|
|
|
|
} else {
|
|
|
|
preview.type = "image";
|
|
|
|
preview.thumb = preview.link;
|
2015-09-30 22:15:53 +00:00
|
|
|
}
|
2017-07-06 15:33:09 +00:00
|
|
|
|
2014-09-27 19:17:05 +00:00
|
|
|
break;
|
|
|
|
|
2017-12-06 22:27:35 +00:00
|
|
|
case "audio/midi":
|
|
|
|
case "audio/mpeg":
|
|
|
|
case "audio/mpeg3":
|
|
|
|
case "audio/ogg":
|
|
|
|
case "audio/wav":
|
|
|
|
case "audio/x-mid":
|
|
|
|
case "audio/x-midi":
|
|
|
|
case "audio/x-mpeg":
|
|
|
|
case "audio/x-mpeg-3":
|
|
|
|
if (!preview.link.startsWith("https://")) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
preview.type = "audio";
|
|
|
|
preview.res = res.type;
|
2017-12-09 23:25:01 +00:00
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case "video/webm":
|
|
|
|
case "video/ogg":
|
|
|
|
case "video/mp4":
|
|
|
|
if (!preview.link.startsWith("https://")) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
preview.res = res.type;
|
|
|
|
preview.type = "video";
|
|
|
|
|
2017-12-06 22:27:35 +00:00
|
|
|
break;
|
|
|
|
|
2014-09-27 19:17:05 +00:00
|
|
|
default:
|
|
|
|
return;
|
|
|
|
}
|
2014-09-27 23:47:04 +00:00
|
|
|
|
2017-07-06 15:33:09 +00:00
|
|
|
handlePreview(client, msg, preview, res);
|
|
|
|
}
|
|
|
|
|
|
|
|
function handlePreview(client, msg, preview, res) {
|
|
|
|
if (!preview.thumb.length || !Helper.config.prefetchStorage) {
|
|
|
|
return emitPreview(client, msg, preview);
|
|
|
|
}
|
|
|
|
|
2017-12-30 10:46:51 +00:00
|
|
|
// Get the correct file extension for the provided content-type
|
|
|
|
// This is done to prevent user-input being stored in the file name (extension)
|
|
|
|
const extension = mime.extension(res.type);
|
|
|
|
|
|
|
|
if (!extension) {
|
|
|
|
// For link previews, drop the thumbnail
|
|
|
|
// For other types, do not display preview at all
|
|
|
|
if (preview.type !== "link") {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
preview.thumb = "";
|
|
|
|
return emitPreview(client, msg, preview);
|
|
|
|
}
|
|
|
|
|
|
|
|
storage.store(res.data, extension, (uri) => {
|
2017-08-13 09:58:27 +00:00
|
|
|
preview.thumb = uri;
|
2017-07-06 15:33:09 +00:00
|
|
|
|
|
|
|
emitPreview(client, msg, preview);
|
|
|
|
});
|
2017-06-26 06:27:51 +00:00
|
|
|
}
|
|
|
|
|
2017-06-26 09:01:55 +00:00
|
|
|
function emitPreview(client, msg, preview) {
|
2017-06-26 06:27:51 +00:00
|
|
|
// 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";
|
2017-06-26 06:27:51 +00:00
|
|
|
} else {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-26 09:01:55 +00:00
|
|
|
client.emit("msg:preview", {
|
|
|
|
id: msg.id,
|
2017-11-15 06:35:15 +00:00
|
|
|
preview: preview,
|
2017-06-26 09:01:55 +00:00
|
|
|
});
|
2014-09-27 19:17:05 +00:00
|
|
|
}
|
|
|
|
|
2017-08-13 09:58:27 +00:00
|
|
|
function fetch(uri, 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({
|
2017-08-13 09:58:27 +00:00
|
|
|
url: uri,
|
2016-03-25 09:45:39 +00:00
|
|
|
maxRedirects: 5,
|
|
|
|
timeout: 5000,
|
2016-01-24 10:43:00 +00:00
|
|
|
headers: {
|
2017-11-15 06:35:15 +00:00
|
|
|
"User-Agent": "Mozilla/5.0 (compatible; The Lounge IRC Client; +https://github.com/thelounge/lounge)",
|
|
|
|
},
|
2016-01-24 10:43:00 +00:00
|
|
|
});
|
2015-09-30 22:39:57 +00:00
|
|
|
} catch (e) {
|
2017-06-23 17:49:45 +00:00
|
|
|
return cb(null);
|
2015-01-04 02:58:12 +00:00
|
|
|
}
|
2017-11-10 20:44:14 +00:00
|
|
|
|
2017-09-21 07:20:24 +00:00
|
|
|
const buffers = [];
|
2017-11-10 20:44:14 +00:00
|
|
|
let length = 0;
|
|
|
|
let 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) {
|
2017-09-24 11:37:24 +00:00
|
|
|
if (/^image\/.+/.test(res.headers["content-type"])) {
|
|
|
|
// response is an image
|
|
|
|
// if Content-Length header reports a size exceeding the prefetch limit, abort fetch
|
|
|
|
const contentLength = parseInt(res.headers["content-length"], 10) || 0;
|
|
|
|
if (contentLength > limit) {
|
|
|
|
req.abort();
|
|
|
|
}
|
2017-12-09 23:25:01 +00:00
|
|
|
} else if (/^(audio|video)\/.+/.test(res.headers["content-type"])) {
|
2017-12-06 22:27:35 +00:00
|
|
|
req.abort(); // ensure server doesn't download the audio file
|
2017-09-24 11:37:24 +00:00
|
|
|
} else {
|
2017-06-26 06:34:56 +00:00
|
|
|
// 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
|
|
|
}
|
|
|
|
})
|
2017-11-25 18:59:12 +00:00
|
|
|
.on("error", () => cb(null))
|
2017-09-21 07:20:24 +00:00
|
|
|
.on("data", (data) => {
|
2014-12-23 01:06:11 +00:00
|
|
|
length += data.length;
|
2017-09-21 07:20:24 +00:00
|
|
|
buffers.push(data);
|
|
|
|
|
2014-12-23 01:06:11 +00:00
|
|
|
if (length > limit) {
|
2017-09-21 07:20:24 +00:00
|
|
|
req.abort();
|
2017-06-23 17:49:45 +00:00
|
|
|
}
|
2017-09-21 07:20:24 +00:00
|
|
|
})
|
|
|
|
.on("end", () => {
|
2017-06-23 17:49:45 +00:00
|
|
|
if (req.response.statusCode < 200 || req.response.statusCode > 299) {
|
|
|
|
return cb(null);
|
2016-05-01 09:41:17 +00:00
|
|
|
}
|
|
|
|
|
2017-07-06 15:33:09 +00:00
|
|
|
let type = "";
|
2017-06-21 05:51:14 +00:00
|
|
|
let size = parseInt(req.response.headers["content-length"], 10) || length;
|
|
|
|
|
|
|
|
if (size < length) {
|
|
|
|
size = length;
|
2014-12-23 01:06:11 +00:00
|
|
|
}
|
2017-06-21 05:51:14 +00:00
|
|
|
|
2017-07-06 15:33:09 +00:00
|
|
|
if (req.response.headers["content-type"]) {
|
2015-09-30 22:39:57 +00:00
|
|
|
type = req.response.headers["content-type"].split(/ *; */).shift();
|
2015-01-03 09:03:43 +00:00
|
|
|
}
|
2017-07-06 15:33:09 +00:00
|
|
|
|
2017-09-21 07:20:24 +00:00
|
|
|
cb({
|
|
|
|
data: Buffer.concat(buffers, length),
|
2015-09-30 22:15:53 +00:00
|
|
|
type: type,
|
2017-11-15 06:35:15 +00:00
|
|
|
size: size,
|
2017-09-21 07:20:24 +00:00
|
|
|
});
|
|
|
|
});
|
2014-09-27 19:17:05 +00:00
|
|
|
}
|
2016-03-25 09:45:39 +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);
|
|
|
|
}
|