2016-10-09 19:14:02 +00:00
|
|
|
"use strict";
|
|
|
|
|
2016-12-09 20:46:53 +00:00
|
|
|
const Chan = require("../../models/chan");
|
|
|
|
const Msg = require("../../models/msg");
|
|
|
|
const LinkPrefetch = require("./link");
|
2014-09-13 21:29:45 +00:00
|
|
|
|
|
|
|
module.exports = function(irc, network) {
|
|
|
|
var client = this;
|
2016-04-03 19:43:11 +00:00
|
|
|
|
2016-03-07 21:09:42 +00:00
|
|
|
irc.on("notice", function(data) {
|
2016-03-20 16:46:08 +00:00
|
|
|
// Some servers send notices without any nickname
|
|
|
|
if (!data.nick) {
|
|
|
|
data.from_server = true;
|
|
|
|
data.nick = network.host;
|
|
|
|
}
|
|
|
|
|
2016-03-07 21:09:42 +00:00
|
|
|
data.type = Msg.Type.NOTICE;
|
|
|
|
handleMessage(data);
|
|
|
|
});
|
2014-09-27 15:46:32 +00:00
|
|
|
|
2016-03-07 21:09:42 +00:00
|
|
|
irc.on("action", function(data) {
|
|
|
|
data.type = Msg.Type.ACTION;
|
|
|
|
handleMessage(data);
|
|
|
|
});
|
|
|
|
|
|
|
|
irc.on("privmsg", function(data) {
|
|
|
|
data.type = Msg.Type.MESSAGE;
|
|
|
|
handleMessage(data);
|
|
|
|
});
|
|
|
|
|
2016-09-30 21:29:49 +00:00
|
|
|
irc.on("wallops", function(data) {
|
|
|
|
data.from_server = true;
|
|
|
|
data.type = Msg.Type.NOTICE;
|
|
|
|
handleMessage(data);
|
|
|
|
});
|
|
|
|
|
2016-03-07 21:09:42 +00:00
|
|
|
function handleMessage(data) {
|
2016-10-09 08:54:44 +00:00
|
|
|
let chan;
|
|
|
|
let highlight = false;
|
|
|
|
const self = data.nick === irc.user.nick;
|
2016-04-22 16:38:59 +00:00
|
|
|
|
2016-03-17 18:55:51 +00:00
|
|
|
// Server messages go to server window, no questions asked
|
|
|
|
if (data.from_server) {
|
|
|
|
chan = network.channels[0];
|
|
|
|
} else {
|
|
|
|
var target = data.target;
|
|
|
|
|
|
|
|
// If the message is targeted at us, use sender as target instead
|
|
|
|
if (target.toLowerCase() === irc.user.nick.toLowerCase()) {
|
|
|
|
target = data.nick;
|
|
|
|
}
|
|
|
|
|
2016-10-09 08:54:44 +00:00
|
|
|
chan = network.getChannel(target);
|
2016-03-08 10:13:36 +00:00
|
|
|
if (typeof chan === "undefined") {
|
2016-03-20 16:46:08 +00:00
|
|
|
// Send notices that are not targeted at us into the server window
|
2016-03-22 11:08:10 +00:00
|
|
|
if (data.type === Msg.Type.NOTICE) {
|
2016-03-20 16:46:08 +00:00
|
|
|
chan = network.channels[0];
|
|
|
|
} else {
|
|
|
|
chan = new Chan({
|
|
|
|
type: Chan.Type.QUERY,
|
|
|
|
name: target
|
|
|
|
});
|
|
|
|
network.channels.push(chan);
|
|
|
|
client.emit("join", {
|
|
|
|
network: network.id,
|
|
|
|
chan: chan
|
|
|
|
});
|
|
|
|
}
|
2016-03-08 10:13:36 +00:00
|
|
|
}
|
2016-05-16 19:46:22 +00:00
|
|
|
|
|
|
|
// Query messages (unless self) always highlight
|
|
|
|
if (chan.type === Chan.Type.QUERY) {
|
|
|
|
highlight = !self;
|
|
|
|
}
|
2014-09-13 21:29:45 +00:00
|
|
|
}
|
2014-09-21 16:46:43 +00:00
|
|
|
|
2016-05-16 19:46:22 +00:00
|
|
|
// Self messages in channels are never highlighted
|
2016-03-08 06:50:52 +00:00
|
|
|
// Non-self messages are highlighted as soon as the nick is detected
|
2016-04-22 16:38:59 +00:00
|
|
|
if (!highlight && !self) {
|
2016-05-12 11:15:38 +00:00
|
|
|
highlight = network.highlightRegex.test(data.message);
|
2016-04-22 16:38:59 +00:00
|
|
|
}
|
2014-09-21 16:46:43 +00:00
|
|
|
|
2014-09-13 21:29:45 +00:00
|
|
|
var msg = new Msg({
|
2016-03-07 21:09:42 +00:00
|
|
|
type: data.type,
|
2016-03-12 09:36:55 +00:00
|
|
|
time: data.time,
|
2016-03-07 21:09:42 +00:00
|
|
|
mode: chan.getMode(data.nick),
|
|
|
|
from: data.nick,
|
2016-03-15 09:41:11 +00:00
|
|
|
text: data.message,
|
2016-02-23 10:38:51 +00:00
|
|
|
self: self,
|
|
|
|
highlight: highlight
|
2014-09-13 21:29:45 +00:00
|
|
|
});
|
2016-09-25 06:41:10 +00:00
|
|
|
chan.pushMessage(client, msg, !self);
|
2016-12-09 20:46:53 +00:00
|
|
|
|
|
|
|
LinkPrefetch(client, chan, msg);
|
2016-03-07 21:09:42 +00:00
|
|
|
}
|
2014-09-13 21:29:45 +00:00
|
|
|
};
|