Skip to content

Commit

Permalink
Merge pull request thelounge#1003 from thelounge/channel-keys
Browse files Browse the repository at this point in the history
Store channel keys
  • Loading branch information
astorije authored Apr 1, 2017
2 parents bb9dc8b + 7fd8546 commit e942672
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 26 deletions.
3 changes: 2 additions & 1 deletion src/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,8 @@ Client.prototype.connect = function(args) {
}

channels.push(new Chan({
name: chan.name
name: chan.name,
key: chan.key || "",
}));
});

Expand Down
1 change: 1 addition & 0 deletions src/models/chan.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ function Chan(attr) {
id: id++,
messages: [],
name: "",
key: "",
topic: "",
type: Chan.Type.CHANNEL,
firstUnread: 0,
Expand Down
3 changes: 2 additions & 1 deletion src/models/network.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,8 @@ Network.prototype.export = function() {
})
.map(function(chan) {
return _.pick(chan, [
"name"
"name",
"key",
]);
});

Expand Down
2 changes: 1 addition & 1 deletion src/plugins/irc-events/connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ module.exports = function(irc, network) {
}

setTimeout(function() {
network.irc.join(chan.name);
network.irc.join(chan.name, chan.key);
}, delay);
delay += 1000;
});
Expand Down
3 changes: 3 additions & 0 deletions src/plugins/irc-events/join.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ module.exports = function(irc, network) {
network: network.id,
chan: chan
});

// Request channels' modes
network.irc.raw("MODE", chan.name);
}
chan.users.push(new User({nick: data.nick}));
chan.sortUsers(irc);
Expand Down
70 changes: 51 additions & 19 deletions src/plugins/irc-events/mode.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,40 @@
"use strict";

var _ = require("lodash");
var Chan = require("../../models/chan");
var Msg = require("../../models/msg");
const _ = require("lodash");
const Chan = require("../../models/chan");
const Msg = require("../../models/msg");

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) {
var targetChan;
let targetChan;

if (data.target === irc.user.nick) {
targetChan = network.channels[0];
Expand All @@ -18,23 +45,29 @@ module.exports = function(irc, network) {
}
}

var usersUpdated;
var supportsMultiPrefix = network.irc.network.cap.isEnabled("multi-prefix");
var userModeSortPriority = {};
let usersUpdated;
let userModeSortPriority = {};
const supportsMultiPrefix = network.irc.network.cap.isEnabled("multi-prefix");

irc.network.options.PREFIX.forEach((prefix, index) => {
userModeSortPriority[prefix.symbol] = index;
});

for (var i = 0; i < data.modes.length; i++) {
var mode = data.modes[i];
var text = mode.mode;
data.modes.forEach(mode => {
let text = mode.mode;
const add = text[0] === "+";
const char = text[1];

if (char === "k") {
targetChan.key = add ? mode.param : "";
client.save();
}

if (mode.param) {
text += " " + mode.param;
}

var msg = new Msg({
const msg = new Msg({
time: data.time,
type: Msg.Type.MODE,
mode: (targetChan.type !== Chan.Type.LOBBY && targetChan.getMode(data.nick)) || "",
Expand All @@ -45,22 +78,21 @@ module.exports = function(irc, network) {
targetChan.pushMessage(client, msg);

if (!mode.param) {
continue;
return;
}

var user = _.find(targetChan.users, {name: mode.param});
const user = _.find(targetChan.users, {name: mode.param});
if (!user) {
continue;
return;
}

usersUpdated = true;

if (!supportsMultiPrefix) {
continue;
return;
}

var add = mode.mode[0] === "+";
var changedMode = network.prefixLookup[mode.mode[1]];
const changedMode = network.prefixLookup[char];

if (!add) {
_.pull(user.modes, changedMode);
Expand All @@ -73,7 +105,7 @@ module.exports = function(irc, network) {

// TODO: remove in future
user.mode = (user.modes && user.modes[0]) || "";
}
});

if (!usersUpdated) {
return;
Expand Down
12 changes: 8 additions & 4 deletions test/models/network.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@ describe("Network", function() {
awayMessage: "I am away",
name: "networkName",
channels: [
new Chan({name: "#thelounge"}),
new Chan({name: "&foobar"}),
new Chan({name: "#thelounge", key: ""}),
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: "PrivateChat", type: Chan.Type.QUERY}),
]
Expand All @@ -35,8 +37,10 @@ describe("Network", function() {
ip: null,
hostname: null,
channels: [
{name: "#thelounge"},
{name: "&foobar"},
{name: "#thelounge", key: ""},
{name: "&foobar", key: ""},
{name: "#secret", key: "foo"},
{name: "&secure", key: "bar"},
]
});
});
Expand Down

0 comments on commit e942672

Please sign in to comment.