From a15915e8fc262bf3aafac3fc05b7af9d58899ba3 Mon Sep 17 00:00:00 2001 From: Juliusz Chroboczek Date: Sun, 17 Jan 2021 21:52:26 +0100 Subject: [PATCH] Tweak the autokick code. --- README | 3 ++- group/group.go | 46 ++++++++++++++++++++++++---------------------- 2 files changed, 26 insertions(+), 23 deletions(-) diff --git a/README b/README index 834bf04..d65abb6 100644 --- a/README +++ b/README @@ -243,7 +243,8 @@ the group. - `autolock`: if true, the group will start locked and become locked whenever there are no clients with operator privileges; - `autokick`: if true, all clients will be kicked out whenever there are - no clients with operator privileges. + no clients with operator privileges; this is not recommended, prefer + the `autolock` option instead; - `redirect`: if set, then attempts to join the group will be redirected to the given URL; most other fields are ignored in this case; - `codecs`: this is a list of codecs allowed in this group. The default diff --git a/group/group.go b/group/group.go index cc5527a..451e93f 100644 --- a/group/group.go +++ b/group/group.go @@ -438,6 +438,8 @@ func AddClient(group string, c Client) (*Group, error) { g.mu.Lock() defer g.mu.Unlock() + clients := g.getClientsUnlocked(nil) + if !c.OverridePermissions(g) { perms, err := g.description.GetPermission(group, c) if err != nil { @@ -446,12 +448,29 @@ func AddClient(group string, c Client) (*Group, error) { c.SetPermissions(perms) - if !perms.Op && g.locked != nil { - m := *g.locked - if m == "" { - m = "this group is locked" + if !perms.Op { + if g.locked != nil { + m := *g.locked + if m == "" { + m = "this group is locked" + } + return nil, UserError(m) + } + if g.description.Autokick { + ops := false + for _, c := range clients { + if c.Permissions().Op { + ops = true + break + } + } + if !ops { + return nil, UserError( + "there are no operators " + + "in this group", + ) + } } - return nil, UserError(m) } if !perms.Op && g.description.MaxClients > 0 { @@ -461,27 +480,10 @@ func AddClient(group string, c Client) (*Group, error) { } } - clients := g.getClientsUnlocked(nil) - if g.clients[c.Id()] != nil { return nil, ProtocolError("duplicate client id") } - if !c.Permissions().Op && g.description.Autokick { - ops := false - for _, c := range clients { - if c.Permissions().Op { - ops = true - break - } - } - if !ops { - return nil, UserError( - "there are no operators in this group", - ) - } - } - g.clients[c.Id()] = c g.timestamp = time.Now()