hardlounge/src/plugins/irc-events/mode.js

54 lines
1.2 KiB
JavaScript
Raw Normal View History

2014-09-13 21:29:45 +00:00
var _ = require("lodash");
2016-03-08 09:42:38 +00:00
var Chan = require("../../models/chan");
2014-09-13 21:29:45 +00:00
var Msg = require("../../models/msg");
module.exports = function(irc, network) {
var client = this;
irc.on("mode", function(data) {
2016-03-08 09:42:38 +00:00
var targetChan;
if (data.target === irc.user.nick) {
targetChan = network.channels[0];
} else {
targetChan = _.find(network.channels, {name: data.target});
if (typeof targetChan === "undefined") {
return;
2014-09-13 21:29:45 +00:00
}
2016-03-08 09:42:38 +00:00
}
var usersUpdated;
2016-03-08 09:42:38 +00:00
for (var i = 0; i < data.modes.length; i++) {
var mode = data.modes[i];
var text = mode.mode;
if (mode.param) {
text += " " + mode.param;
var user = _.find(targetChan.users, {name: mode.param});
if (typeof user !== "undefined") {
usersUpdated = true;
}
2016-03-08 09:42:38 +00:00
}
2014-09-13 21:29:45 +00:00
var msg = new Msg({
2016-03-12 09:36:55 +00:00
time: data.time,
2014-09-13 21:29:45 +00:00
type: Msg.Type.MODE,
2016-03-08 09:42:38 +00:00
mode: (targetChan.type !== Chan.Type.LOBBY && targetChan.getMode(data.nick)) || "",
from: data.nick,
text: text,
self: data.nick === irc.user.nick
2014-09-13 21:29:45 +00:00
});
2016-03-08 09:42:38 +00:00
targetChan.messages.push(msg);
2014-09-13 21:29:45 +00:00
client.emit("msg", {
2016-03-08 09:42:38 +00:00
chan: targetChan.id,
2014-09-13 21:29:45 +00:00
msg: msg,
});
}
if (usersUpdated) {
// TODO: This is horrible
irc.raw("NAMES", data.target);
}
2014-09-13 21:29:45 +00:00
});
};