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

87 lines
2 KiB
JavaScript
Raw Normal View History

2014-09-13 23:29:45 +02:00
var _ = require("lodash");
var Chan = require("../../models/chan");
var Msg = require("../../models/msg");
var Helper = require("../../helper");
2014-09-13 23:29:45 +02:00
module.exports = function(irc, network) {
var client = this;
var config = Helper.getConfig();
irc.on("notice", function(data) {
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);
});
function handleMessage(data) {
// First, try to find current target
var chan = _.find(network.channels, {name: data.target});
2014-09-13 23:29:45 +02:00
if (typeof chan === "undefined") {
// If current target doesn't exist, try to find by nick
chan = _.find(network.channels, {name: data.nick});
// If neither target or nick channels exist, create one for the nick
if (typeof chan === "undefined") {
chan = new Chan({
type: Chan.Type.QUERY,
name: data.nick
});
network.channels.push(chan);
client.emit("join", {
network: network.id,
chan: chan
});
}
2014-09-13 23:29:45 +02:00
}
// Server messages go to server window
if (data.from_server) {
chan = network.channels[0];
}
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 10:41:11 +01:00
var highlight = !self && data.message.split(" ").some(function(w) {
return (w.replace(/^@/, "").toLowerCase().indexOf(irc.user.nick.toLowerCase()) === 0);
});
2015-10-01 00:39:57 +02:00
if (chan.id !== client.activeChannel) {
chan.unread++;
if (highlight) {
chan.highlight = true;
}
}
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.messages.push(msg);
if (config.maxHistory >= 0 && chan.messages.length > config.maxHistory) {
chan.messages.splice(0, chan.messages.length - config.maxHistory);
}
2014-09-13 23:29:45 +02:00
client.emit("msg", {
chan: chan.id,
msg: msg
});
}
2014-09-13 23:29:45 +02:00
};