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

100 lines
2.3 KiB
JavaScript
Raw Normal View History

2014-09-13 21:29:45 +00:00
var Chan = require("../../models/chan");
var Msg = require("../../models/msg");
var Helper = require("../../helper");
2014-09-13 21:29:45 +00:00
module.exports = function(irc, network) {
var client = this;
var config = Helper.getConfig();
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 15:46:32 +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);
});
function handleMessage(data) {
// Server messages go to server window, no questions asked
if (data.from_server) {
chan = network.channels[0];
} else {
var target = data.target;
var targetedAtUser = false;
// If the message is targeted at us, use sender as target instead
if (target.toLowerCase() === irc.user.nick.toLowerCase()) {
targetedAtUser = true;
target = data.nick;
}
2016-03-20 14:28:47 +00: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 && !targetedAtUser) {
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
});
}
}
2014-09-13 21:29:45 +00:00
}
var self = data.nick === irc.user.nick;
// Self messages are never highlighted
// Non-self messages are highlighted as soon as the nick is detected
2016-03-15 09:41:11 +00:00
var highlight = !self && data.message.split(" ").some(function(w) {
return (w.replace(/^@/, "").toLowerCase().indexOf(irc.user.nick.toLowerCase()) === 0);
});
2015-09-30 22:39:57 +00:00
if (chan.id !== client.activeChannel) {
chan.unread++;
if (highlight) {
chan.highlight = true;
}
}
2014-09-13 21:29:45 +00:00
var msg = new Msg({
type: data.type,
2016-03-12 09:36:55 +00:00
time: data.time,
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
});
chan.messages.push(msg);
if (config.maxHistory >= 0 && chan.messages.length > config.maxHistory) {
chan.messages.splice(0, chan.messages.length - config.maxHistory);
}
2014-09-13 21:29:45 +00:00
client.emit("msg", {
chan: chan.id,
msg: msg
});
}
2014-09-13 21:29:45 +00:00
};