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

45 lines
1.1 KiB
JavaScript
Raw Normal View History

2014-06-29 21:41:02 +02: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) {
var target = data.to;
2014-09-11 20:05:33 +02:00
if (target.toLowerCase() == irc.me.toLowerCase()) {
2014-08-27 16:34:03 +02:00
target = data.from;
}
var chan = _.findWhere(network.channels, {name: target});
2014-06-29 21:41:02 +02: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
});
}
var type = "";
var text = data.message;
if (text.split(" ")[0] === "\u0001ACTION") {
type = Msg.Type.ACTION;
text = text.replace(/\u0001|ACTION/g, "");
}
text.split(" ").forEach(function(w) {
if (w.indexOf(irc.me) === 0) type += " highlight";
});
var msg = new Msg({
type: type || Msg.Type.MESSAGE,
from: data.from,
text: text
});
chan.messages.push(msg);
client.emit("msg", {
chan: chan.id,
msg: msg
});
});
2014-06-27 01:05:47 +02:00
};