From cd34480bba7c33e7ffff4e25ab41aa690f849dc1 Mon Sep 17 00:00:00 2001 From: joeyak Date: Sun, 24 Mar 2019 09:24:57 -0400 Subject: [PATCH] Change auth level checking to be on command level The server sends new auth level to user when modded closes #60 --- chatclient.go | 23 +++++++++++++++-------- chatcommands.go | 39 +++++++++++++++++++++------------------ chatroom.go | 26 ++++++++++---------------- common/chatdata.go | 4 ++-- common/constants.go | 8 ++++---- handlers.go | 6 +++--- wasm/main.go | 4 ++++ 7 files changed, 59 insertions(+), 51 deletions(-) diff --git a/chatclient.go b/chatclient.go index 9174ae0..de07ff3 100644 --- a/chatclient.go +++ b/chatclient.go @@ -15,8 +15,7 @@ type Client struct { conn *chatConnection belongsTo *ChatRoom color string - IsMod bool - IsAdmin bool + CmdLevel common.CommandLevel IsColorForced bool IsNameForced bool } @@ -24,6 +23,12 @@ type Client struct { //Client has a new message to broadcast func (cl *Client) NewMsg(data common.ClientData) { switch data.Type { + case common.CdAuth: + fmt.Printf("[chat|hidden] <%s> get auth level\n", cl.name) + err := cl.SendChatData(common.NewChatHiddenMessage(data.Type, cl.CmdLevel)) + if err != nil { + fmt.Printf("Error sending auth level to client: %v\n", err) + } case common.CdUsers: fmt.Printf("[chat|hidden] <%s> get list of users\n", cl.name) @@ -37,7 +42,7 @@ func (cl *Client) NewMsg(data common.ClientData) { err := cl.SendChatData(common.NewChatHiddenMessage(data.Type, append(names[:idx], names[idx+1:]...))) if err != nil { - fmt.Printf("Error sending chat data: %v\n", err) + fmt.Printf("Error sending users to client: %v\n", err) } case common.CdMessage: msg := html.EscapeString(data.Message) @@ -60,7 +65,7 @@ func (cl *Client) NewMsg(data common.ClientData) { if response != "" { err := cl.SendChatData(common.NewChatMessage("", "", common.ParseEmotes(response), - common.CmdUser, + common.CmdlUser, common.MsgCommandResponse)) if err != nil { fmt.Printf("Error command results %v\n", err) @@ -77,7 +82,7 @@ func (cl *Client) NewMsg(data common.ClientData) { fmt.Printf("[chat] <%s> %q\n", cl.name, msg) // Enable links for mods and admins - if cl.IsMod || cl.IsAdmin { + if cl.CmdLevel >= common.CmdlMod { msg = formatLinks(msg) } @@ -108,7 +113,7 @@ func (cl *Client) Send(data common.ChatDataJSON) error { } func (cl *Client) SendServerMessage(s string) error { - err := cl.SendChatData(common.NewChatMessage("", ColorServerMessage, s, common.CmdUser, common.MsgServer)) + err := cl.SendChatData(common.NewChatMessage("", ColorServerMessage, s, common.CmdlUser, common.MsgServer)) if err != nil { return fmt.Errorf("could send server message to %s: message - %#v: %v", cl.name, s, err) } @@ -146,11 +151,13 @@ func (cl *Client) Me(msg string) { } func (cl *Client) Mod() { - cl.IsMod = true + if cl.CmdLevel < common.CmdlMod { + cl.CmdLevel = common.CmdlMod + } } func (cl *Client) Unmod() { - cl.IsMod = false + cl.CmdLevel = common.CmdlUser } func (cl *Client) Host() string { diff --git a/chatcommands.go b/chatcommands.go index c46c95e..81c9bc0 100644 --- a/chatcommands.go +++ b/chatcommands.go @@ -52,22 +52,21 @@ var commands = &CommandControl{ common.CNAuth.String(): Command{ HelpText: "Authenticate to admin", Function: func(cl *Client, args []string) string { - if cl.IsAdmin { + if cl.CmdLevel == common.CmdlAdmin { return "You are already authenticated." } pw := html.UnescapeString(strings.Join(args, " ")) if settings.AdminPassword == pw { - cl.IsMod = true - cl.IsAdmin = true + cl.CmdLevel = common.CmdlAdmin cl.belongsTo.AddModNotice(cl.name + " used the admin password") fmt.Printf("[auth] %s used the admin password\n", cl.name) return "Admin rights granted." } if cl.belongsTo.redeemModPass(pw) { - cl.IsMod = true + cl.CmdLevel = common.CmdlMod cl.belongsTo.AddModNotice(cl.name + " used a mod password") fmt.Printf("[auth] %s used a mod password\n", cl.name) return "Moderator privileges granted." @@ -100,7 +99,7 @@ var commands = &CommandControl{ // Two arguments to force a name change on another user: `/nick OldName NewName` if len(args) == 2 { - if !cl.IsAdmin { + if cl.CmdLevel != common.CmdlAdmin { return "Only admins can do that PeepoSus" } @@ -109,7 +108,7 @@ var commands = &CommandControl{ forced = true } - if len(args) == 1 && cl.IsNameForced && !cl.IsAdmin { + if len(args) == 1 && cl.IsNameForced && cl.CmdLevel != common.CmdlAdmin { return "You cannot change your name once it has been changed by an admin." } @@ -180,7 +179,7 @@ var commands = &CommandControl{ common.CNUnmod.String(): Command{ HelpText: "Revoke a user's moderator privilages. Moderators can only unmod themselves.", Function: func(cl *Client, args []string) string { - if len(args) > 0 && !cl.IsAdmin && cl.name != args[0] { + if len(args) > 0 && cl.CmdLevel != common.CmdlAdmin && cl.name != args[0] { return "You can only unmod yourself, not others." } @@ -334,7 +333,7 @@ func (cc *CommandControl) RunCommand(command string, args []string, sender *Clie // Look for mod command if modCmd, ok := cc.mod[cmd]; ok { - if sender.IsMod || sender.IsAdmin { + if sender.CmdLevel >= common.CmdlMod { fmt.Printf("[mod] %s /%s %s\n", sender.name, command, strings.Join(args, " ")) return modCmd.Function(sender, args) } @@ -345,7 +344,7 @@ func (cc *CommandControl) RunCommand(command string, args []string, sender *Clie // Look for admin command if adminCmd, ok := cc.admin[cmd]; ok { - if sender.IsAdmin { + if sender.CmdLevel == common.CmdlAdmin { fmt.Printf("[admin] %s /%s %s\n", sender.name, command, strings.Join(args, " ")) return adminCmd.Function(sender, args) } @@ -360,12 +359,13 @@ func (cc *CommandControl) RunCommand(command string, args []string, sender *Clie func cmdHelp(cl *Client, args []string) string { url := "/help" - if cl.IsMod { - url = "/help?mod=1" + + if cl.CmdLevel >= common.CmdlMod { + url += "?mod=1" } - if cl.IsAdmin { - url = "/help?mod=1&admin=1" + if cl.CmdLevel == common.CmdlAdmin { + url += "&admin=1" } cl.SendChatData(common.NewChatCommand(common.CmdHelp, []string{url})) @@ -375,11 +375,11 @@ func cmdHelp(cl *Client, args []string) string { func getHelp(lvl common.CommandLevel) map[string]string { var cmdList map[string]Command switch lvl { - case common.CmdUser: + case common.CmdlUser: cmdList = commands.user - case common.CmdMod: + case common.CmdlMod: cmdList = commands.mod - case common.CmdAdmin: + case common.CmdlAdmin: cmdList = commands.admin } @@ -401,7 +401,7 @@ var cmdColor = Command{ // If the caller is priviledged enough, they can change the color of another user if len(args) == 2 { - if !(cl.IsMod || cl.IsAdmin) { + if cl.CmdLevel == common.CmdlUser { return "You cannot change someone else's color. PeepoSus" } @@ -492,6 +492,9 @@ var cmdColor = Command{ var cmdWhoAmI = Command{ HelpText: "Shows debug user info", Function: func(cl *Client, args []string) string { - return fmt.Sprintf("Name: %s IsMod: %t IsAdmin: %t", cl.name, cl.IsMod, cl.IsAdmin) + return fmt.Sprintf("Name: %s IsMod: %t IsAdmin: %t", + cl.name, + cl.CmdLevel >= common.CmdlMod, + cl.CmdLevel == common.CmdlAdmin) }, } diff --git a/chatroom.go b/chatroom.go index 363e947..7d0b4cd 100644 --- a/chatroom.go +++ b/chatroom.go @@ -154,11 +154,11 @@ func (cr *ChatRoom) Kick(name string) string { return "Unable to get client for name " + name } - if client.IsMod { + if client.CmdLevel == common.CmdlMod { return "You cannot kick another mod." } - if client.IsAdmin { + if client.CmdLevel == common.CmdlAdmin { return "Jebaited No." } @@ -182,7 +182,7 @@ func (cr *ChatRoom) Ban(name string) string { return "Cannot find that name" } - if client.IsAdmin { + if client.CmdLevel == common.CmdlAdmin { return "You cannot ban an admin Jebaited" } @@ -226,16 +226,8 @@ func (cr *ChatRoom) AddMsg(from *Client, isAction, isServer bool, msg string) { t = common.MsgServer } - lvl := common.CmdUser - if from.IsMod { - lvl = common.CmdMod - } - if from.IsAdmin { - lvl = common.CmdAdmin - } - select { - case cr.queue <- common.NewChatMessage(from.name, from.color, msg, lvl, t): + case cr.queue <- common.NewChatMessage(from.name, from.color, msg, from.CmdLevel, t): default: fmt.Println("Unable to queue chat message. Channel full.") } @@ -251,7 +243,7 @@ func (cr *ChatRoom) AddCmdMsg(command common.CommandType, args []string) { func (cr *ChatRoom) AddModNotice(message string) { select { - case cr.modqueue <- common.NewChatMessage("", "", message, common.CmdUser, common.MsgNotice): + case cr.modqueue <- common.NewChatMessage("", "", message, common.CmdlUser, common.MsgNotice): default: fmt.Println("Unable to queue notice. Channel full.") } @@ -288,8 +280,10 @@ func (cr *ChatRoom) Mod(name string) error { return err } - client.IsMod = true - client.SendServerMessage(`You have been modded.`) + if client.CmdLevel < common.CmdlMod { + client.CmdLevel = common.CmdlMod + client.SendServerMessage(`You have been modded.`) + } return nil } @@ -357,7 +351,7 @@ func (cr *ChatRoom) Broadcast() { case msg := <-cr.modqueue: cr.clientsMtx.Lock() for _, client := range cr.clients { - if client.IsMod || client.IsAdmin { + if client.CmdLevel >= common.CmdlMod { send(msg, client) } } diff --git a/common/chatdata.go b/common/chatdata.go index 58bb1df..961c0be 100644 --- a/common/chatdata.go +++ b/common/chatdata.go @@ -111,9 +111,9 @@ func (dc DataMessage) HTML() string { default: badge := "" switch dc.Level { - case CmdMod: + case CmdlMod: badge = `` - case CmdAdmin: + case CmdlAdmin: badge = `` } return `
` + badge + `` + dc.From + diff --git a/common/constants.go b/common/constants.go index f7546e1..e52d4d7 100644 --- a/common/constants.go +++ b/common/constants.go @@ -7,7 +7,7 @@ const ( CdMessage ClientDataType = iota // a normal message from the client meant to be broadcast CdUsers // get a list of users CdPing // ping the server to keep the connection alive - CdHelp // tells server to send help data again for buttons + CdAuth // get the auth levels of the user ) type DataType int @@ -36,9 +36,9 @@ type CommandLevel int // Command access levels const ( - CmdUser CommandLevel = iota - CmdMod - CmdAdmin + CmdlUser CommandLevel = iota + CmdlMod + CmdlAdmin ) type EventType int diff --git a/handlers.go b/handlers.go index 2589c0a..4a3ebfb 100644 --- a/handlers.go +++ b/handlers.go @@ -169,15 +169,15 @@ func handleHelpTemplate(w http.ResponseWriter, r *http.Request) { data := Data{ Title: "Help", - Commands: getHelp(common.CmdUser), + Commands: getHelp(common.CmdlUser), } if len(r.URL.Query().Get("mod")) > 0 { - data.ModCommands = getHelp(common.CmdMod) + data.ModCommands = getHelp(common.CmdlMod) } if len(r.URL.Query().Get("admin")) > 0 { - data.AdminCommands = getHelp(common.CmdAdmin) + data.AdminCommands = getHelp(common.CmdlAdmin) } err = t.Execute(w, data) diff --git a/wasm/main.go b/wasm/main.go index cdd5e7f..f253ead 100644 --- a/wasm/main.go +++ b/wasm/main.go @@ -19,6 +19,7 @@ const ( var ( currentName string + auth common.CommandLevel names []string filteredNames []string ) @@ -182,6 +183,8 @@ func recieve(v []js.Value) { for _, i := range h.Data.([]interface{}) { names = append(names, i.(string)) } + case common.CdAuth: + auth = h.Data.(common.CommandLevel) } case common.DTEvent: d := chat.Data.(common.DataEvent) @@ -277,6 +280,7 @@ func isValidName(this js.Value, v []js.Value) interface{} { func debugValues(v []js.Value) { fmt.Printf("currentName %#v\n", currentName) + fmt.Printf("auth %#v\n", auth) fmt.Printf("names %#v\n", names) fmt.Printf("filteredNames %#v\n", filteredNames) }