diff --git a/src/client.js b/src/client.js index e571191a..dea63512 100644 --- a/src/client.js +++ b/src/client.js @@ -24,7 +24,6 @@ var events = [ "mode", "motd", "message", - "link", "names", "nick", "part", diff --git a/src/plugins/irc-events/link.js b/src/plugins/irc-events/link.js index ff2ceac9..2502446d 100644 --- a/src/plugins/irc-events/link.js +++ b/src/plugins/irc-events/link.js @@ -1,45 +1,37 @@ "use strict"; -var cheerio = require("cheerio"); -var Msg = require("../../models/msg"); -var request = require("request"); -var Helper = require("../../helper"); -var es = require("event-stream"); +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(irc, network) { - var client = this; - irc.on("privmsg", function(data) { - if (!Helper.config.prefetch) { - return; - } +module.exports = function(client, chan, originalMsg) { + if (!Helper.config.prefetch) { + return; + } - const links = data.message - .replace(/\x02|\x1D|\x1F|\x16|\x0F|\x03(?:[0-9]{1,2}(?:,[0-9]{1,2})?)?/g, "") - .split(" ") - .filter(w => /^https?:\/\//.test(w)); + 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; - } + if (links.length === 0) { + return; + } - var chan = network.getChannel(data.target); - if (typeof chan === "undefined") { - return; - } + let msg = new Msg({ + type: Msg.Type.TOGGLE, + time: originalMsg.time, + self: originalMsg.self, + }); + chan.pushMessage(client, msg); - var msg = new Msg({ - self: data.nick === irc.user.nick, - type: Msg.Type.TOGGLE, - time: data.time, // msg handles it if it isn't defined - }); - chan.pushMessage(client, msg); - - var link = escapeHeader(links[0]); - fetch(link, function(res) { - parse(msg, link, res, client); - }); + const link = escapeHeader(links[0]); + fetch(link, function(res) { + parse(msg, link, res, client); }); }; @@ -51,7 +43,6 @@ function parse(msg, url, res, client) { body: "", thumb: "", link: url, - time: msg.time, }; switch (res.type) { diff --git a/src/plugins/irc-events/message.js b/src/plugins/irc-events/message.js index 5eb231fe..1107594a 100644 --- a/src/plugins/irc-events/message.js +++ b/src/plugins/irc-events/message.js @@ -1,7 +1,8 @@ "use strict"; -var Chan = require("../../models/chan"); -var Msg = require("../../models/msg"); +const Chan = require("../../models/chan"); +const Msg = require("../../models/msg"); +const LinkPrefetch = require("./link"); module.exports = function(irc, network) { var client = this; @@ -89,5 +90,7 @@ module.exports = function(irc, network) { highlight: highlight }); chan.pushMessage(client, msg, !self); + + LinkPrefetch(client, chan, msg); } }; diff --git a/test/plugins/link.js b/test/plugins/link.js index 5267dd58..4eab22f1 100644 --- a/test/plugins/link.js +++ b/test/plugins/link.js @@ -21,16 +21,16 @@ describe("Link plugin", function() { }); it("should be able to fetch basic information about URLs", function(done) { - link.call(this.irc, this.irc, this.network); + let message = this.irc.createMessage({ + text: "http://localhost:9002/basic" + }); + + link(this.irc, this.network.channels[0], message); this.app.get("/basic", function(req, res) { res.send("test"); }); - this.irc.createMessage({ - message: "http://localhost:9002/basic" - }); - this.irc.once("toggle", function(data) { assert.equal(data.head, "test"); done(); diff --git a/test/util.js b/test/util.js index 0b14f3f1..88e3b409 100644 --- a/test/util.js +++ b/test/util.js @@ -18,12 +18,12 @@ util.inherits(MockClient, EventEmitter); MockClient.prototype.createMessage = function(opts) { var message = _.extend({ - message: "dummy message", + text: "dummy message", nick: "test-user", target: "#test-channel" }, opts); - this.emit("privmsg", message); + return message; }; module.exports = {