2014-09-13 17:29:45 -04:00
|
|
|
var _ = require("lodash");
|
|
|
|
var Chan = require("../../models/chan");
|
|
|
|
var Msg = require("../../models/msg");
|
|
|
|
|
|
|
|
module.exports = function(irc, network) {
|
|
|
|
var client = this;
|
|
|
|
irc.on("message", function(data) {
|
2015-09-30 18:39:57 -04:00
|
|
|
if (data.message.indexOf("\u0001") === 0 && data.message.substring(0, 7) !== "\u0001ACTION") {
|
2014-09-27 11:46:32 -04:00
|
|
|
// Hide ctcp messages.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-09-13 17:29:45 -04:00
|
|
|
var target = data.to;
|
2015-09-30 18:39:57 -04:00
|
|
|
if (target.toLowerCase() === irc.me.toLowerCase()) {
|
2014-09-13 17:29:45 -04:00
|
|
|
target = data.from;
|
|
|
|
}
|
2014-09-21 12:46:43 -04:00
|
|
|
|
2016-02-14 12:09:51 -05:00
|
|
|
var chan = _.find(network.channels, {name: target});
|
2014-09-13 17:29:45 -04:00
|
|
|
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
|
|
|
|
});
|
|
|
|
}
|
2014-09-21 12:46:43 -04:00
|
|
|
|
2016-02-23 05:38:51 -05:00
|
|
|
var type = Msg.Type.MESSAGE;
|
2014-09-13 17:29:45 -04:00
|
|
|
var text = data.message;
|
2016-02-23 05:38:51 -05:00
|
|
|
var textSplit = text.split(" ");
|
|
|
|
if (textSplit[0] === "\u0001ACTION") {
|
2014-09-13 17:29:45 -04:00
|
|
|
type = Msg.Type.ACTION;
|
2014-10-02 10:25:31 -04:00
|
|
|
text = text.replace(/^\u0001ACTION|\u0001$/g, "");
|
2014-09-13 17:29:45 -04:00
|
|
|
}
|
2014-09-21 12:46:43 -04:00
|
|
|
|
2016-03-07 01:09:42 -05:00
|
|
|
var self = (data.from.toLowerCase() === irc.me.toLowerCase());
|
2014-09-21 12:46:43 -04:00
|
|
|
|
2016-03-08 01:50:52 -05:00
|
|
|
// 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);
|
|
|
|
});
|
2014-09-21 12:46:43 -04:00
|
|
|
|
2015-09-30 18:39:57 -04:00
|
|
|
if (chan.id !== client.activeChannel) {
|
2014-09-21 12:46:43 -04:00
|
|
|
chan.unread++;
|
|
|
|
}
|
|
|
|
|
2014-10-04 08:31:45 -04:00
|
|
|
var name = data.from;
|
2014-09-13 17:29:45 -04:00
|
|
|
var msg = new Msg({
|
2016-02-23 05:38:51 -05:00
|
|
|
type: type,
|
2014-10-04 08:31:45 -04:00
|
|
|
mode: chan.getMode(name),
|
|
|
|
from: name,
|
2014-09-14 13:06:56 -04:00
|
|
|
text: text,
|
2016-02-23 05:38:51 -05:00
|
|
|
self: self,
|
|
|
|
highlight: highlight
|
2014-09-13 17:29:45 -04:00
|
|
|
});
|
|
|
|
chan.messages.push(msg);
|
|
|
|
client.emit("msg", {
|
|
|
|
chan: chan.id,
|
|
|
|
msg: msg
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|