Update README.md
[KisSync.git] / src / channel / accesscontrol.js
blobed75a790f4cfb0c042f3ee7d1834abc054bdecf6
1 var ChannelModule = require("./module");
2 var Flags = require("../flags");
4 function AccessControlModule(_channel) {
5 ChannelModule.apply(this, arguments);
8 AccessControlModule.prototype = Object.create(ChannelModule.prototype);
10 AccessControlModule.prototype.onUserPreJoin = function (user, data, cb) {
11 var chan = this.channel,
12 opts = this.channel.modules.options;
13 if (user.socket.disconnected) {
14 return cb("User disconnected", ChannelModule.DENY);
17 if (opts.get("password") !== false && data.pw !== opts.get("password")) {
18 user.socket.on("disconnect", function () {
19 if (!user.is(Flags.U_IN_CHANNEL)) {
20 cb("User disconnected", ChannelModule.DENY);
22 });
24 if (user.is(Flags.U_LOGGED_IN) && user.account.effectiveRank >= 2) {
25 cb(null, ChannelModule.PASSTHROUGH);
26 user.socket.emit("cancelNeedPassword");
27 } else {
28 user.socket.emit("needPassword", typeof data.pw !== "undefined");
29 /* Option 1: log in as a moderator */
30 user.waitFlag(Flags.U_HAS_CHANNEL_RANK, function () {
31 if (user.is(Flags.U_IN_CHANNEL)) {
32 return;
35 if (user.account.effectiveRank >= 2) {
36 cb(null, ChannelModule.PASSTHROUGH);
37 user.socket.emit("cancelNeedPassword");
39 });
41 /* Option 2: Enter correct password */
42 var pwListener = function (pw) {
43 if (chan.dead || user.is(Flags.U_IN_CHANNEL)) {
44 return;
47 if (pw !== opts.get("password")) {
48 user.socket.emit("needPassword", true);
49 return;
52 user.socket.emit("cancelNeedPassword");
53 cb(null, ChannelModule.PASSTHROUGH);
56 user.socket.on("channelPassword", pwListener);
58 } else {
59 cb(null, ChannelModule.PASSTHROUGH);
63 module.exports = AccessControlModule;