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

125 lines
2.6 KiB
JavaScript
Raw Normal View History

"use strict";
2017-04-01 10:33:17 +02:00
const _ = require("lodash");
const Msg = require("../../models/msg");
2014-09-13 23:29:45 +02:00
module.exports = function(irc, network) {
2017-04-01 10:33:17 +02:00
const client = this;
// The following saves the channel key based on channel mode instead of
// extracting it from `/join #channel key`. This lets us not have to
// temporarily store the key until successful join, but also saves the key
// if a key is set or changed while being on the channel.
irc.on("channel info", function(data) {
if (!data.modes) {
return;
}
const targetChan = network.getChannel(data.channel);
2017-04-01 10:33:17 +02:00
if (typeof targetChan === "undefined") {
return;
}
data.modes.forEach((mode) => {
2017-04-01 10:33:17 +02:00
const text = mode.mode;
const add = text[0] === "+";
const char = text[1];
if (char === "k") {
targetChan.key = add ? mode.param : "";
client.save();
}
});
});
2014-09-13 23:29:45 +02:00
irc.on("mode", function(data) {
2017-04-01 10:33:17 +02:00
let targetChan;
2016-03-08 10:42:38 +01:00
if (data.target === irc.user.nick) {
targetChan = network.channels[0];
} else {
2016-03-20 15:28:47 +01:00
targetChan = network.getChannel(data.target);
2016-03-08 10:42:38 +01:00
if (typeof targetChan === "undefined") {
return;
2014-09-13 23:29:45 +02:00
}
2016-03-08 10:42:38 +01:00
}
2017-04-01 10:33:17 +02:00
let usersUpdated;
const userModeSortPriority = {};
2017-04-01 10:33:17 +02:00
const supportsMultiPrefix = network.irc.network.cap.isEnabled("multi-prefix");
irc.network.options.PREFIX.forEach((prefix, index) => {
userModeSortPriority[prefix.symbol] = index;
});
data.modes.forEach((mode) => {
2017-04-01 10:33:17 +02:00
let text = mode.mode;
const add = text[0] === "+";
const char = text[1];
if (char === "k") {
targetChan.key = add ? mode.param : "";
client.save();
}
2016-03-08 10:42:38 +01:00
if (mode.param) {
text += " " + mode.param;
}
2017-04-01 10:33:17 +02:00
const msg = new Msg({
2016-03-12 10:36:55 +01:00
time: data.time,
2014-09-13 23:29:45 +02:00
type: Msg.Type.MODE,
from: targetChan.getUser(data.nick),
2016-03-08 10:42:38 +01:00
text: text,
self: data.nick === irc.user.nick,
2014-09-13 23:29:45 +02:00
});
targetChan.pushMessage(client, msg);
if (!mode.param) {
2017-04-01 10:33:17 +02:00
return;
}
2017-07-11 16:30:47 +02:00
const user = targetChan.findUser(mode.param);
if (!user) {
2017-04-01 10:33:17 +02:00
return;
}
usersUpdated = true;
if (!supportsMultiPrefix) {
2017-04-01 10:33:17 +02:00
return;
}
2017-04-01 10:33:17 +02:00
const changedMode = network.prefixLookup[char];
if (!add) {
_.pull(user.modes, changedMode);
} else if (!user.modes.includes(changedMode)) {
user.modes.push(changedMode);
user.modes.sort(function(a, b) {
return userModeSortPriority[a] - userModeSortPriority[b];
});
}
// TODO: remove in future
user.mode = (user.modes && user.modes[0]) || "";
2017-04-01 10:33:17 +02:00
});
if (!usersUpdated) {
return;
}
if (!supportsMultiPrefix) {
// TODO: This is horrible
irc.raw("NAMES", data.target);
} else {
client.emit("users", {
chan: targetChan.id,
});
}
2014-09-13 23:29:45 +02:00
});
};