2014-09-27 19:17:05 +00:00
|
|
|
var _ = require("lodash");
|
2014-09-27 20:39:14 +00:00
|
|
|
var cheerio = require("cheerio");
|
2014-09-27 19:17:05 +00:00
|
|
|
var Msg = require("../../models/msg");
|
2014-12-23 01:06:11 +00:00
|
|
|
var request = require("request");
|
2014-10-10 21:12:01 +00:00
|
|
|
var Helper = require("../../helper");
|
2015-09-30 22:39:57 +00:00
|
|
|
var es = require("event-stream");
|
2014-09-27 19:17:05 +00:00
|
|
|
|
2015-04-29 19:55:13 +00:00
|
|
|
process.setMaxListeners(0);
|
|
|
|
|
2014-09-27 19:17:05 +00:00
|
|
|
module.exports = function(irc, network) {
|
|
|
|
var client = this;
|
|
|
|
irc.on("message", function(data) {
|
2014-10-10 21:12:01 +00:00
|
|
|
var config = Helper.getConfig();
|
|
|
|
if (!config.prefetch) {
|
|
|
|
return;
|
|
|
|
}
|
2014-12-11 23:48:43 +00:00
|
|
|
|
2014-09-27 19:17:05 +00:00
|
|
|
var links = [];
|
2016-03-25 09:45:39 +00:00
|
|
|
var split = data.message.replace(/\x02|\x1D|\x1F|\x16|\x0F|\x03(?:[0-9]{1,2}(?:,[0-9]{1,2})?)?/g, "").split(" ");
|
2014-09-27 19:17:05 +00:00
|
|
|
_.each(split, function(w) {
|
2016-03-25 09:45:39 +00:00
|
|
|
if (/^https?:\/\//.test(w)) {
|
2014-09-27 19:17:05 +00:00
|
|
|
links.push(w);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
if (links.length === 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-09-30 22:39:57 +00:00
|
|
|
var self = data.to.toLowerCase() === irc.me.toLowerCase();
|
2016-02-14 17:09:51 +00:00
|
|
|
var chan = _.find(network.channels, {name: self ? data.from : data.to});
|
2014-09-27 19:17:05 +00:00
|
|
|
if (typeof chan === "undefined") {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var msg = new Msg({
|
|
|
|
type: Msg.Type.TOGGLE,
|
|
|
|
time: ""
|
|
|
|
});
|
|
|
|
chan.messages.push(msg);
|
|
|
|
client.emit("msg", {
|
|
|
|
chan: chan.id,
|
|
|
|
msg: msg
|
|
|
|
});
|
|
|
|
|
2016-03-25 09:45:39 +00:00
|
|
|
var link = escapeHeader(links[0]);
|
2014-12-11 23:48:43 +00:00
|
|
|
fetch(link, function(res) {
|
|
|
|
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) {
|
2015-09-30 22:15:53 +00:00
|
|
|
var config = Helper.getConfig();
|
2014-09-27 23:47:04 +00:00
|
|
|
var toggle = msg.toggle = {
|
|
|
|
id: msg.id,
|
|
|
|
type: "",
|
|
|
|
head: "",
|
|
|
|
body: "",
|
2014-10-12 01:42:30 +00:00
|
|
|
thumb: "",
|
2014-09-27 23:47:04 +00:00
|
|
|
link: url
|
|
|
|
};
|
|
|
|
|
2015-09-30 22:15:53 +00:00
|
|
|
if (!config.prefetchMaxImageSize) {
|
|
|
|
config.prefetchMaxImageSize = 512;
|
|
|
|
}
|
2014-09-27 19:17:05 +00:00
|
|
|
switch (res.type) {
|
|
|
|
case "text/html":
|
2014-12-21 23:26:55 +00:00
|
|
|
var $ = cheerio.load(res.text);
|
2014-09-27 23:47:04 +00:00
|
|
|
toggle.type = "link";
|
|
|
|
toggle.head = $("title").text();
|
2014-10-14 18:51:27 +00:00
|
|
|
toggle.body =
|
2015-09-30 22:39:57 +00:00
|
|
|
$("meta[name=description]").attr("content")
|
|
|
|
|| $("meta[property=\"og:description\"]").attr("content")
|
2014-10-14 18:51:27 +00:00
|
|
|
|| "No description found.";
|
2014-12-11 23:48:43 +00:00
|
|
|
toggle.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
|
|
|
|| "";
|
2014-09-27 19:17:05 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case "image/png":
|
|
|
|
case "image/gif":
|
|
|
|
case "image/jpg":
|
|
|
|
case "image/jpeg":
|
2015-09-30 22:15:53 +00:00
|
|
|
if (res.size < (config.prefetchMaxImageSize * 1024)) {
|
|
|
|
toggle.type = "image";
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return;
|
|
|
|
}
|
2014-09-27 19:17:05 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
return;
|
|
|
|
}
|
2014-09-27 23:47:04 +00:00
|
|
|
|
|
|
|
client.emit("toggle", toggle);
|
2014-09-27 19:17:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function fetch(url, cb) {
|
2015-01-04 02:58:12 +00:00
|
|
|
try {
|
2016-01-24 10:43:00 +00:00
|
|
|
var req = request.get({
|
|
|
|
url: url,
|
2016-03-25 09:45:39 +00:00
|
|
|
maxRedirects: 5,
|
|
|
|
timeout: 5000,
|
2016-01-24 10:43:00 +00:00
|
|
|
headers: {
|
2016-02-10 05:45:21 +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) {
|
2015-01-04 02:58:12 +00:00
|
|
|
return;
|
|
|
|
}
|
2014-12-21 23:26:55 +00:00
|
|
|
var length = 0;
|
2014-12-23 01:06:11 +00:00
|
|
|
var limit = 1024 * 10;
|
|
|
|
req
|
2015-09-30 22:39:57 +00:00
|
|
|
.on("response", function(res) {
|
|
|
|
if (!(/(text\/html|application\/json)/.test(res.headers["content-type"]))) {
|
|
|
|
res.req.abort();
|
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;
|
|
|
|
var body;
|
2015-01-03 09:03:43 +00:00
|
|
|
var type;
|
2015-09-30 22:15:53 +00:00
|
|
|
var size = req.response.headers["content-length"];
|
2014-12-23 01:06:11 +00:00
|
|
|
try {
|
|
|
|
body = JSON.parse(data);
|
2015-09-30 22:39:57 +00:00
|
|
|
} catch (e) {
|
2014-12-23 01:06:11 +00:00
|
|
|
body = {};
|
|
|
|
}
|
2015-01-03 09:03:43 +00:00
|
|
|
try {
|
2015-09-30 22:39:57 +00:00
|
|
|
type = req.response.headers["content-type"].split(/ *; */).shift();
|
|
|
|
} catch (e) {
|
2015-01-03 09:03:43 +00:00
|
|
|
type = {};
|
|
|
|
}
|
2014-12-23 01:06:11 +00:00
|
|
|
data = {
|
|
|
|
text: data,
|
|
|
|
body: body,
|
2015-09-30 22:15:53 +00:00
|
|
|
type: type,
|
|
|
|
size: size
|
2014-12-23 01:06:11 +00:00
|
|
|
};
|
|
|
|
cb(data);
|
|
|
|
}));
|
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);
|
|
|
|
}
|