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");
|
2014-12-21 23:26:55 +00:00
|
|
|
var es = require('event-stream');
|
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 = [];
|
|
|
|
var split = data.message.split(" ");
|
|
|
|
_.each(split, function(w) {
|
|
|
|
var match = w.indexOf("http://") === 0 || w.indexOf("https://") === 0;
|
|
|
|
if (match) {
|
|
|
|
links.push(w);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
if (links.length === 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var self = data.to.toLowerCase() == irc.me.toLowerCase();
|
|
|
|
var chan = _.findWhere(network.channels, {name: self ? data.from : data.to});
|
|
|
|
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
|
|
|
|
});
|
|
|
|
|
2014-12-11 23:48:43 +00:00
|
|
|
var link = links[0];
|
|
|
|
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) {
|
|
|
|
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
|
|
|
|
};
|
|
|
|
|
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 =
|
|
|
|
$('meta[name=description]').attr('content')
|
|
|
|
|| $('meta[property="og:description"]').attr('content')
|
|
|
|
|| "No description found.";
|
2014-12-11 23:48:43 +00:00
|
|
|
toggle.thumb =
|
2014-10-14 18:51:27 +00:00
|
|
|
$('meta[property="og:image"]').attr('content')
|
|
|
|
|| $('meta[name="twitter:image:src"]').attr('content')
|
|
|
|
|| "";
|
2014-09-27 19:17:05 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case "image/png":
|
|
|
|
case "image/gif":
|
|
|
|
case "image/jpg":
|
|
|
|
case "image/jpeg":
|
2014-09-27 23:47:04 +00:00
|
|
|
toggle.type = "image";
|
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) {
|
|
|
|
var req = request.get(url);
|
2014-12-21 23:26:55 +00:00
|
|
|
var length = 0;
|
2014-12-23 01:06:11 +00:00
|
|
|
var limit = 1024 * 10;
|
|
|
|
req
|
|
|
|
.on('response', function(res) {
|
|
|
|
if (!(/(text\/html|application\/json)/.test(res.headers['content-type']))) {
|
|
|
|
res.req.abort();
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.on('error', function() {})
|
|
|
|
.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;
|
|
|
|
try {
|
|
|
|
body = JSON.parse(data);
|
|
|
|
} catch(e) {
|
|
|
|
body = {};
|
|
|
|
}
|
|
|
|
data = {
|
|
|
|
text: data,
|
|
|
|
body: body,
|
|
|
|
type: req.response.headers['content-type'].split(/ *; */).shift()
|
|
|
|
};
|
|
|
|
cb(data);
|
|
|
|
}));
|
2014-09-27 19:17:05 +00:00
|
|
|
}
|