2014-09-13 21:29:45 +00:00
|
|
|
var _ = require("lodash");
|
|
|
|
var Chan = require("../../models/chan");
|
|
|
|
var Msg = require("../../models/msg");
|
2016-04-03 19:43:11 +00:00
|
|
|
var Helper = require("../../helper");
|
2014-09-13 21:29:45 +00:00
|
|
|
|
|
|
|
module.exports = function(irc, network) {
|
|
|
|
var client = this;
|
2016-04-03 19:43:11 +00:00
|
|
|
var config = Helper.getConfig();
|
|
|
|
|
2016-03-07 21:09:42 +00:00
|
|
|
irc.on("notice", function(data) {
|
|
|
|
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);
|
|
|
|
});
|
|
|
|
|
|
|
|
function handleMessage(data) {
|
|
|
|
var target = data.target;
|
|
|
|
if (target.toLowerCase() === irc.user.nick.toLowerCase()) {
|
|
|
|
target = data.nick;
|
2014-09-13 21:29:45 +00:00
|
|
|
}
|
2014-09-21 16:46:43 +00:00
|
|
|
|
2016-02-14 17:09:51 +00:00
|
|
|
var chan = _.find(network.channels, {name: target});
|
2014-09-13 21:29:45 +00:00
|
|
|
if (typeof chan === "undefined") {
|
|
|
|
chan = new Chan({
|
|
|
|
type: Chan.Type.QUERY,
|
2016-03-07 21:09:42 +00:00
|
|
|
name: data.nick
|
2014-09-13 21:29:45 +00:00
|
|
|
});
|
|
|
|
network.channels.push(chan);
|
|
|
|
client.emit("join", {
|
|
|
|
network: network.id,
|
|
|
|
chan: chan
|
|
|
|
});
|
|
|
|
}
|
2014-09-21 16:46:43 +00:00
|
|
|
|
2016-03-07 21:09:42 +00:00
|
|
|
var self = data.nick === irc.user.nick;
|
2014-09-21 16:46:43 +00:00
|
|
|
|
2016-03-08 06:50:52 +00:00
|
|
|
// Self messages are never highlighted
|
|
|
|
// Non-self messages are highlighted as soon as the nick is detected
|
2016-03-07 21:09:42 +00:00
|
|
|
var highlight = !self && data.msg.split(" ").some(function(w) {
|
2016-03-08 09:26:43 +00:00
|
|
|
return (w.replace(/^@/, "").toLowerCase().indexOf(irc.user.nick.toLowerCase()) === 0);
|
2016-03-08 06:50:52 +00:00
|
|
|
});
|
2014-09-21 16:46:43 +00:00
|
|
|
|
2015-09-30 22:39:57 +00:00
|
|
|
if (chan.id !== client.activeChannel) {
|
2014-09-21 16:46:43 +00:00
|
|
|
chan.unread++;
|
2016-03-13 16:05:05 +00:00
|
|
|
|
|
|
|
if (highlight) {
|
|
|
|
chan.highlight = true;
|
|
|
|
}
|
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,
|
|
|
|
mode: chan.getMode(data.nick),
|
|
|
|
from: data.nick,
|
|
|
|
text: data.msg,
|
2016-02-23 10:38:51 +00:00
|
|
|
self: self,
|
|
|
|
highlight: highlight
|
2014-09-13 21:29:45 +00:00
|
|
|
});
|
|
|
|
chan.messages.push(msg);
|
2016-04-03 19:43:11 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
});
|
2016-03-07 21:09:42 +00:00
|
|
|
}
|
2014-09-13 21:29:45 +00:00
|
|
|
};
|