Merge pull request #1003 from thelounge/channel-keys

Store channel keys
This commit is contained in:
Jérémie Astori 2017-04-01 15:35:56 -04:00 committed by GitHub
commit 1b32bf6820
7 changed files with 68 additions and 26 deletions

View File

@ -159,7 +159,8 @@ Client.prototype.connect = function(args) {
} }
channels.push(new Chan({ channels.push(new Chan({
name: chan.name name: chan.name,
key: chan.key || "",
})); }));
}); });

View File

@ -19,6 +19,7 @@ function Chan(attr) {
id: id++, id: id++,
messages: [], messages: [],
name: "", name: "",
key: "",
topic: "", topic: "",
type: Chan.Type.CHANNEL, type: Chan.Type.CHANNEL,
firstUnread: 0, firstUnread: 0,

View File

@ -87,7 +87,8 @@ Network.prototype.export = function() {
}) })
.map(function(chan) { .map(function(chan) {
return _.pick(chan, [ return _.pick(chan, [
"name" "name",
"key",
]); ]);
}); });

View File

@ -47,7 +47,7 @@ module.exports = function(irc, network) {
} }
setTimeout(function() { setTimeout(function() {
network.irc.join(chan.name); network.irc.join(chan.name, chan.key);
}, delay); }, delay);
delay += 1000; delay += 1000;
}); });

View File

@ -18,6 +18,9 @@ module.exports = function(irc, network) {
network: network.id, network: network.id,
chan: chan chan: chan
}); });
// Request channels' modes
network.irc.raw("MODE", chan.name);
} }
chan.users.push(new User({nick: data.nick})); chan.users.push(new User({nick: data.nick}));
chan.sortUsers(irc); chan.sortUsers(irc);

View File

@ -1,13 +1,40 @@
"use strict"; "use strict";
var _ = require("lodash"); const _ = require("lodash");
var Chan = require("../../models/chan"); const Chan = require("../../models/chan");
var Msg = require("../../models/msg"); const Msg = require("../../models/msg");
module.exports = function(irc, network) { module.exports = function(irc, network) {
var client = this; 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;
}
data.modes.forEach(mode => {
const text = mode.mode;
const add = text[0] === "+";
const char = text[1];
if (char === "k") {
targetChan.key = add ? mode.param : "";
client.save();
}
});
});
irc.on("mode", function(data) { irc.on("mode", function(data) {
var targetChan; let targetChan;
if (data.target === irc.user.nick) { if (data.target === irc.user.nick) {
targetChan = network.channels[0]; targetChan = network.channels[0];
@ -18,23 +45,29 @@ module.exports = function(irc, network) {
} }
} }
var usersUpdated; let usersUpdated;
var supportsMultiPrefix = network.irc.network.cap.isEnabled("multi-prefix"); let userModeSortPriority = {};
var userModeSortPriority = {}; const supportsMultiPrefix = network.irc.network.cap.isEnabled("multi-prefix");
irc.network.options.PREFIX.forEach((prefix, index) => { irc.network.options.PREFIX.forEach((prefix, index) => {
userModeSortPriority[prefix.symbol] = index; userModeSortPriority[prefix.symbol] = index;
}); });
for (var i = 0; i < data.modes.length; i++) { data.modes.forEach(mode => {
var mode = data.modes[i]; let text = mode.mode;
var text = mode.mode; const add = text[0] === "+";
const char = text[1];
if (char === "k") {
targetChan.key = add ? mode.param : "";
client.save();
}
if (mode.param) { if (mode.param) {
text += " " + mode.param; text += " " + mode.param;
} }
var msg = new Msg({ const msg = new Msg({
time: data.time, time: data.time,
type: Msg.Type.MODE, type: Msg.Type.MODE,
mode: (targetChan.type !== Chan.Type.LOBBY && targetChan.getMode(data.nick)) || "", mode: (targetChan.type !== Chan.Type.LOBBY && targetChan.getMode(data.nick)) || "",
@ -45,22 +78,21 @@ module.exports = function(irc, network) {
targetChan.pushMessage(client, msg); targetChan.pushMessage(client, msg);
if (!mode.param) { if (!mode.param) {
continue; return;
} }
var user = _.find(targetChan.users, {name: mode.param}); const user = _.find(targetChan.users, {name: mode.param});
if (!user) { if (!user) {
continue; return;
} }
usersUpdated = true; usersUpdated = true;
if (!supportsMultiPrefix) { if (!supportsMultiPrefix) {
continue; return;
} }
var add = mode.mode[0] === "+"; const changedMode = network.prefixLookup[char];
var changedMode = network.prefixLookup[mode.mode[1]];
if (!add) { if (!add) {
_.pull(user.modes, changedMode); _.pull(user.modes, changedMode);
@ -73,7 +105,7 @@ module.exports = function(irc, network) {
// TODO: remove in future // TODO: remove in future
user.mode = (user.modes && user.modes[0]) || ""; user.mode = (user.modes && user.modes[0]) || "";
} });
if (!usersUpdated) { if (!usersUpdated) {
return; return;

View File

@ -13,8 +13,10 @@ describe("Network", function() {
awayMessage: "I am away", awayMessage: "I am away",
name: "networkName", name: "networkName",
channels: [ channels: [
new Chan({name: "#thelounge"}), new Chan({name: "#thelounge", key: ""}),
new Chan({name: "&foobar"}), new Chan({name: "&foobar", key: ""}),
new Chan({name: "#secret", key: "foo"}),
new Chan({name: "&secure", key: "bar"}),
new Chan({name: "Channel List", type: Chan.Type.SPECIAL}), new Chan({name: "Channel List", type: Chan.Type.SPECIAL}),
new Chan({name: "PrivateChat", type: Chan.Type.QUERY}), new Chan({name: "PrivateChat", type: Chan.Type.QUERY}),
] ]
@ -35,8 +37,10 @@ describe("Network", function() {
ip: null, ip: null,
hostname: null, hostname: null,
channels: [ channels: [
{name: "#thelounge"}, {name: "#thelounge", key: ""},
{name: "&foobar"}, {name: "&foobar", key: ""},
{name: "#secret", key: "foo"},
{name: "&secure", key: "bar"},
] ]
}); });
}); });