thelounge/src/plugins/irc-events/message.js

95 lines
2.1 KiB
JavaScript
Raw Normal View History

2014-09-13 23:29:45 +02:00
var Chan = require("../../models/chan");
var Msg = require("../../models/msg");
module.exports = function(irc, network) {
var client = this;
irc.on("notice", function(data) {
// Some servers send notices without any nickname
if (!data.nick) {
data.from_server = true;
data.nick = network.host;
}
data.type = Msg.Type.NOTICE;
handleMessage(data);
});
2014-09-27 17:46:32 +02: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);
});
irc.on("wallops", function(data) {
data.from_server = true;
data.type = Msg.Type.NOTICE;
handleMessage(data);
});
function handleMessage(data) {
var highlight = false;
var self = data.nick === irc.user.nick;
// 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-03-20 15:28:47 +01:00
var chan = network.getChannel(target);
if (typeof chan === "undefined") {
// Send notices that are not targeted at us into the server window
if (data.type === Msg.Type.NOTICE) {
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
});
}
}
// Query messages (unless self) always highlight
if (chan.type === Chan.Type.QUERY) {
highlight = !self;
}
2014-09-13 23:29:45 +02:00
}
// Self messages in channels are never highlighted
// Non-self messages are highlighted as soon as the nick is detected
if (!highlight && !self) {
2016-05-12 13:15:38 +02:00
highlight = network.highlightRegex.test(data.message);
}
if (!self && chan.id !== client.activeChannel) {
chan.unread++;
}
2014-09-13 23:29:45 +02:00
var msg = new Msg({
type: data.type,
2016-03-12 10:36:55 +01:00
time: data.time,
mode: chan.getMode(data.nick),
from: data.nick,
2016-03-15 10:41:11 +01:00
text: data.message,
2016-02-23 11:38:51 +01:00
self: self,
highlight: highlight
2014-09-13 23:29:45 +02:00
});
chan.pushMessage(client, msg);
}
2014-09-13 23:29:45 +02:00
};