2016-10-09 15:14:02 -04:00
|
|
|
"use strict";
|
|
|
|
|
2017-04-01 04:33:17 -04:00
|
|
|
const _ = require("lodash");
|
|
|
|
const Chan = require("../../models/chan");
|
|
|
|
const Msg = require("../../models/msg");
|
2014-09-13 17:29:45 -04:00
|
|
|
|
|
|
|
module.exports = function(irc, network) {
|
2017-04-01 04:33:17 -04: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);
|
|
|
|
if (typeof targetChan === "undefined") {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-04-08 08:34:31 -04:00
|
|
|
data.modes.forEach((mode) => {
|
2017-04-01 04:33:17 -04: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 17:29:45 -04:00
|
|
|
irc.on("mode", function(data) {
|
2017-04-01 04:33:17 -04:00
|
|
|
let targetChan;
|
2016-03-08 04:42:38 -05:00
|
|
|
|
|
|
|
if (data.target === irc.user.nick) {
|
|
|
|
targetChan = network.channels[0];
|
|
|
|
} else {
|
2016-03-20 10:28:47 -04:00
|
|
|
targetChan = network.getChannel(data.target);
|
2016-03-08 04:42:38 -05:00
|
|
|
if (typeof targetChan === "undefined") {
|
|
|
|
return;
|
2014-09-13 17:29:45 -04:00
|
|
|
}
|
2016-03-08 04:42:38 -05:00
|
|
|
}
|
|
|
|
|
2017-04-01 04:33:17 -04:00
|
|
|
let usersUpdated;
|
2017-04-08 08:34:31 -04:00
|
|
|
const userModeSortPriority = {};
|
2017-04-01 04:33:17 -04:00
|
|
|
const supportsMultiPrefix = network.irc.network.cap.isEnabled("multi-prefix");
|
2016-09-24 12:34:35 -04:00
|
|
|
|
2016-10-12 03:55:40 -04:00
|
|
|
irc.network.options.PREFIX.forEach((prefix, index) => {
|
2016-09-24 12:34:35 -04:00
|
|
|
userModeSortPriority[prefix.symbol] = index;
|
|
|
|
});
|
2016-03-16 16:54:11 -04:00
|
|
|
|
2017-04-08 08:34:31 -04:00
|
|
|
data.modes.forEach((mode) => {
|
2017-04-01 04:33:17 -04:00
|
|
|
let text = mode.mode;
|
|
|
|
const add = text[0] === "+";
|
|
|
|
const char = text[1];
|
|
|
|
|
|
|
|
if (char === "k") {
|
|
|
|
targetChan.key = add ? mode.param : "";
|
|
|
|
client.save();
|
|
|
|
}
|
2016-09-24 12:34:35 -04:00
|
|
|
|
2016-03-08 04:42:38 -05:00
|
|
|
if (mode.param) {
|
|
|
|
text += " " + mode.param;
|
|
|
|
}
|
|
|
|
|
2017-04-01 04:33:17 -04:00
|
|
|
const msg = new Msg({
|
2016-03-12 04:36:55 -05:00
|
|
|
time: data.time,
|
2014-09-13 17:29:45 -04:00
|
|
|
type: Msg.Type.MODE,
|
2016-03-08 04:42:38 -05:00
|
|
|
mode: (targetChan.type !== Chan.Type.LOBBY && targetChan.getMode(data.nick)) || "",
|
|
|
|
from: data.nick,
|
|
|
|
text: text,
|
2017-11-15 01:35:15 -05:00
|
|
|
self: data.nick === irc.user.nick,
|
2014-09-13 17:29:45 -04:00
|
|
|
});
|
2016-04-19 06:20:18 -04:00
|
|
|
targetChan.pushMessage(client, msg);
|
2016-09-24 12:34:35 -04:00
|
|
|
|
|
|
|
if (!mode.param) {
|
2017-04-01 04:33:17 -04:00
|
|
|
return;
|
2016-09-24 12:34:35 -04:00
|
|
|
}
|
|
|
|
|
2017-07-11 10:30:47 -04:00
|
|
|
const user = targetChan.findUser(mode.param);
|
2016-09-24 12:34:35 -04:00
|
|
|
if (!user) {
|
2017-04-01 04:33:17 -04:00
|
|
|
return;
|
2016-09-24 12:34:35 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
usersUpdated = true;
|
|
|
|
|
|
|
|
if (!supportsMultiPrefix) {
|
2017-04-01 04:33:17 -04:00
|
|
|
return;
|
2016-09-24 12:34:35 -04:00
|
|
|
}
|
|
|
|
|
2017-04-01 04:33:17 -04:00
|
|
|
const changedMode = network.prefixLookup[char];
|
2016-09-24 12:34:35 -04:00
|
|
|
|
|
|
|
if (!add) {
|
|
|
|
_.pull(user.modes, changedMode);
|
|
|
|
} else if (user.modes.indexOf(changedMode) === -1) {
|
|
|
|
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 04:33:17 -04:00
|
|
|
});
|
2016-03-16 16:54:11 -04:00
|
|
|
|
2016-09-24 12:34:35 -04:00
|
|
|
if (!usersUpdated) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!supportsMultiPrefix) {
|
2016-03-16 16:54:11 -04:00
|
|
|
// TODO: This is horrible
|
|
|
|
irc.raw("NAMES", data.target);
|
2016-09-24 12:34:35 -04:00
|
|
|
} else {
|
|
|
|
targetChan.sortUsers(irc);
|
|
|
|
|
|
|
|
client.emit("users", {
|
2017-11-15 01:35:15 -05:00
|
|
|
chan: targetChan.id,
|
2016-09-24 12:34:35 -04:00
|
|
|
});
|
2016-03-16 16:54:11 -04:00
|
|
|
}
|
2014-09-13 17:29:45 -04:00
|
|
|
});
|
|
|
|
};
|