thelounge/src/plugins/irc-events/message.js
Maxime Poulin 7209bcd58a Add config option to limit in-memory history size
This adds a (temporary?) config option to limit the amount of messages stored per channel to avoid the server's memory usage to grow as channels fills up with messages.
2016-04-06 03:29:35 -04:00

79 lines
1.8 KiB
JavaScript

var _ = require("lodash");
var Chan = require("../../models/chan");
var Msg = require("../../models/msg");
var Helper = require("../../helper");
module.exports = function(irc, network) {
var client = this;
var config = Helper.getConfig();
irc.on("message", function(data) {
if (data.message.indexOf("\u0001") === 0 && data.message.substring(0, 7) !== "\u0001ACTION") {
// Hide ctcp messages.
return;
}
var target = data.to;
if (target.toLowerCase() === irc.me.toLowerCase()) {
target = data.from;
}
var chan = _.find(network.channels, {name: target});
if (typeof chan === "undefined") {
chan = new Chan({
type: Chan.Type.QUERY,
name: data.from
});
network.channels.push(chan);
client.emit("join", {
network: network.id,
chan: chan
});
}
var type = Msg.Type.MESSAGE;
var text = data.message;
var textSplit = text.split(" ");
if (textSplit[0] === "\u0001ACTION") {
type = Msg.Type.ACTION;
text = text.replace(/^\u0001ACTION|\u0001$/g, "");
}
var self = (data.from.toLowerCase() === irc.me.toLowerCase());
// Self messages are never highlighted
// Non-self messages are highlighted as soon as the nick is detected
var highlight = !self && textSplit.some(function(w) {
return (w.replace(/^@/, "").toLowerCase().indexOf(irc.me.toLowerCase()) === 0);
});
if (chan.id !== client.activeChannel) {
chan.unread++;
if (highlight) {
chan.highlight = true;
}
}
var name = data.from;
var msg = new Msg({
type: type,
mode: chan.getMode(name),
from: name,
text: text,
self: self,
highlight: highlight
});
chan.messages.push(msg);
if (config.maxHistory >= 0 && chan.messages.length > config.maxHistory) {
chan.messages.splice(0, chan.messages.length - config.maxHistory);
}
client.emit("msg", {
chan: chan.id,
msg: msg
});
});
};