hardlounge/lib/plugins/message.js

43 lines
1.0 KiB
JavaScript
Raw Normal View History

2014-06-19 15:28:53 +00:00
var _ = require("lodash");
var Chan = require("../models/chan");
var Msg = require("../models/msg");
var Network = require("../models/network");
2014-06-23 17:28:36 +00:00
module.exports = function(slate, network) {
var client = this;
slate.on("message", function(data) {
2014-06-19 15:28:53 +00:00
var target = data.to;
var chan = _.findWhere(network.channels, {name: target.charAt(0) == "#" ? target : data.from});
if (typeof chan === "undefined") {
chan = new Chan({
name: data.from,
type: "query",
});
network.addChan(chan);
2014-06-23 17:28:36 +00:00
client.emit("join", {
2014-06-19 15:28:53 +00:00
id: network.id,
chan: chan,
});
}
var type = "";
var text = data.message;
if (text.split(" ")[0] === "\u0001ACTION") {
type = "action";
text = text.replace(/\u0001|ACTION/g, "");
}
text.split(' ').forEach(function(w) {
2014-06-23 17:28:36 +00:00
if (w.indexOf(slate.me) == 0) type += " highlight";
2014-06-19 15:28:53 +00:00
});
var msg = new Msg({
type: type || "normal",
from: data.from,
text: text,
});
chan.addMsg(msg);
2014-06-23 17:28:36 +00:00
client.emit("msg", {
2014-06-19 15:28:53 +00:00
id: chan.id,
msg: msg,
});
});
};