2014-09-13 21:29:45 +00: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) {
|
2014-10-02 14:46:51 +00:00
|
|
|
if (data.message.indexOf("\u0001") === 0 && data.message.substring(0, 7) != "\u0001ACTION") {
|
2014-09-27 15:46:32 +00:00
|
|
|
// Hide ctcp messages.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-09-13 21:29:45 +00:00
|
|
|
var target = data.to;
|
|
|
|
if (target.toLowerCase() == irc.me.toLowerCase()) {
|
|
|
|
target = data.from;
|
|
|
|
}
|
2014-09-21 16:46:43 +00:00
|
|
|
|
2014-09-13 21:29:45 +00:00
|
|
|
var chan = _.findWhere(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
|
|
|
|
});
|
|
|
|
}
|
2014-09-21 16:46:43 +00:00
|
|
|
|
2014-09-13 21:29:45 +00:00
|
|
|
var type = "";
|
|
|
|
var text = data.message;
|
|
|
|
if (text.split(" ")[0] === "\u0001ACTION") {
|
|
|
|
type = Msg.Type.ACTION;
|
2014-10-02 14:25:31 +00:00
|
|
|
text = text.replace(/^\u0001ACTION|\u0001$/g, "");
|
2014-09-13 21:29:45 +00:00
|
|
|
}
|
2014-09-21 16:46:43 +00:00
|
|
|
|
2014-09-13 21:29:45 +00:00
|
|
|
text.split(" ").forEach(function(w) {
|
|
|
|
if (w.indexOf(irc.me) === 0) type += " highlight";
|
|
|
|
});
|
2014-09-21 16:46:43 +00:00
|
|
|
|
2014-09-14 18:49:42 +00:00
|
|
|
var self = false;
|
|
|
|
if (data.from.toLowerCase() == irc.me.toLowerCase()) {
|
|
|
|
self = true;
|
2014-09-14 17:06:56 +00:00
|
|
|
}
|
2014-09-21 16:46:43 +00:00
|
|
|
|
|
|
|
if (chan.id != client.activeChannel) {
|
|
|
|
chan.unread++;
|
|
|
|
}
|
|
|
|
|
2014-09-13 21:29:45 +00:00
|
|
|
var msg = new Msg({
|
|
|
|
type: type || Msg.Type.MESSAGE,
|
|
|
|
from: data.from,
|
2014-09-14 17:06:56 +00:00
|
|
|
text: text,
|
2014-09-14 18:49:42 +00:00
|
|
|
self: self
|
2014-09-13 21:29:45 +00:00
|
|
|
});
|
|
|
|
chan.messages.push(msg);
|
|
|
|
client.emit("msg", {
|
|
|
|
chan: chan.id,
|
|
|
|
msg: msg
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|