<commit_before>"use strict";

const cheerio = require("cheerio");
const Msg = require("../../models/msg");
const request = require("request");
const Helper = require("../../helper");
const es = require("event-stream");

process.setMaxListeners(0);

module.exports = function(client, chan, originalMsg) {
	if (!Helper.config.prefetch) {
		return;
	}

	const links = originalMsg.text
		.replace(/\x02|\x1D|\x1F|\x16|\x0F|\x03(?:[0-9]{1,2}(?:,[0-9]{1,2})?)?/g, "")
		.split(" ")
		.filter((w) => /^https?:\/\//.test(w));

	if (links.length === 0) {
		return;
	}

	const msg = new Msg({
		type: Msg.Type.TOGGLE,
		time: originalMsg.time,
		self: originalMsg.self,
	});
	chan.pushMessage(client, msg);

	const link = escapeHeader(links[0]);
	fetch(link, function(res) {
		if (res === null) {
			return;
		}

		parse(msg, link, res, client);
	});
};

function parse(msg, url, res, client) {
	var toggle = msg.toggle = {
		id: msg.id,
		type: "",
		head: "",
		body: "",
		thumb: "",
		link: url,
	};

	switch (res.type) {
	case "text/html":
		var $ = cheerio.load(res.text);
		toggle.type = "link";
		toggle.head =
			$("meta[property=\"og:title\"]").attr("content")
			|| $("title").text()
			|| "No title found.";
		toggle.body =
			$("meta[property=\"og:description\"]").attr("content")
			|| $("meta[name=\"description\"]").attr("content")
			|| "No description found.";
		toggle.thumb =
			$("meta[property=\"og:image\"]").attr("content")
			|| $("meta[name=\"twitter:image:src\"]").attr("content")
			|| "";

		// Make sure thumbnail is a valid url
		if (!/^https?:\/\//.test(toggle.thumb)) {
			toggle.thumb = "";
		}

		// Verify that thumbnail pic exists and is under allowed size
		if (toggle.thumb.length) {
			fetch(escapeHeader(toggle.thumb), (resThumb) => {
				if (!(/^image\/.+/.test(resThumb.type)) || resThumb.size > (Helper.config.prefetchMaxImageSize * 1024)) {
					toggle.thumb = "";
				}

				client.emit("toggle", toggle);
			});

			return;
		}

		break;

	case "image/png":
	case "image/gif":
	case "image/jpg":
	case "image/jpeg":
		if (res.size < (Helper.config.prefetchMaxImageSize * 1024)) {
			toggle.type = "image";
		} else {
			return;
		}
		break;

	default:
		return;
	}

	client.emit("toggle", toggle);
}

function fetch(url, cb) {
	let req;
	try {
		req = request.get({
			url: url,
			maxRedirects: 5,<commit_msg>Do not display preview if there is nothing to preview<commit_after>"use strict";

const cheerio = require("cheerio");
const Msg = require("../../models/msg");
const request = require("request");
const Helper = require("../../helper");
const es = require("event-stream");

process.setMaxListeners(0);

module.exports = function(client, chan, originalMsg) {
	if (!Helper.config.prefetch) {
		return;
	}

	const links = originalMsg.text
		.replace(/\x02|\x1D|\x1F|\x16|\x0F|\x03(?:[0-9]{1,2}(?:,[0-9]{1,2})?)?/g, "")
		.split(" ")
		.filter((w) => /^https?:\/\//.test(w));

	if (links.length === 0) {
		return;
	}

	const msg = new Msg({
		type: Msg.Type.TOGGLE,
		time: originalMsg.time,
		self: originalMsg.self,
	});
	chan.pushMessage(client, msg);

	const link = escapeHeader(links[0]);
	fetch(link, function(res) {
		if (res === null) {
			return;
		}

		parse(msg, link, res, client);
	});
};

function parse(msg, url, res, client) {
	var toggle = msg.toggle = {
		id: msg.id,
		type: "",
		head: "",
		body: "",
		thumb: "",
		link: url,
	};

	switch (res.type) {
	case "text/html":
		var $ = cheerio.load(res.text);
		toggle.type = "link";
		toggle.head =
			$("meta[property=\"og:title\"]").attr("content")
			|| $("title").text()
			|| "";
		toggle.body =
			$("meta[property=\"og:description\"]").attr("content")
			|| $("meta[name=\"description\"]").attr("content")
			|| "";
		toggle.thumb =
			$("meta[property=\"og:image\"]").attr("content")
			|| $("meta[name=\"twitter:image:src\"]").attr("content")
			|| "";

		// Make sure thumbnail is a valid url
		if (!/^https?:\/\//.test(toggle.thumb)) {
			toggle.thumb = "";
		}

		// Verify that thumbnail pic exists and is under allowed size
		if (toggle.thumb.length) {
			fetch(escapeHeader(toggle.thumb), (resThumb) => {
				if (resThumb === null
				|| !(/^image\/.+/.test(resThumb.type))
				|| resThumb.size > (Helper.config.prefetchMaxImageSize * 1024)) {
					toggle.thumb = "";
				}

				emitToggle(client, toggle);
			});

			return;
		}

		break;

	case "image/png":
	case "image/gif":
	case "image/jpg":
	case "image/jpeg":
		if (res.size < (Helper.config.prefetchMaxImageSize * 1024)) {
			toggle.type = "image";
		} else {
			return;
		}
		break;

	default:
		return;
	}

	emitToggle(client, toggle);
}

function emitToggle(client, toggle) {
	// If there is no title but there is preview or description, set title
	// otherwise bail out and show no preview
	if (!toggle.head.length) {
		if (toggle.thumb.length || toggle.body.length) {
			toggle.head = "Untitled page";
		} else {
			return;
		}
	}

	client.emit("toggle", toggle);
}

function fetch(url, cb) {
	let req;
	try {
		req = request.get({
			url: url,
			maxRedirects: 5,
{'fraction_non_alphanumeric': 0.21595407759303245, 'fraction_numerical': 0.011480601741884403, 'mean_word_length': 3.318803418803419, 'pattern_counts': {'":': 10, '<': 5, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7462657', 'n_tokens_mistral': 2084, 'n_tokens_neox': 1845, 'n_words': 373}