Change auth level checking to be on command level

The server sends new auth level to user when modded
closes #60
This commit is contained in:
joeyak 2019-03-24 09:24:57 -04:00
parent 5c87d70d3e
commit cd34480bba
7 changed files with 59 additions and 51 deletions

View File

@ -15,8 +15,7 @@ type Client struct {
conn *chatConnection conn *chatConnection
belongsTo *ChatRoom belongsTo *ChatRoom
color string color string
IsMod bool CmdLevel common.CommandLevel
IsAdmin bool
IsColorForced bool IsColorForced bool
IsNameForced bool IsNameForced bool
} }
@ -24,6 +23,12 @@ type Client struct {
//Client has a new message to broadcast //Client has a new message to broadcast
func (cl *Client) NewMsg(data common.ClientData) { func (cl *Client) NewMsg(data common.ClientData) {
switch data.Type { 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: case common.CdUsers:
fmt.Printf("[chat|hidden] <%s> get list of users\n", cl.name) 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:]...))) err := cl.SendChatData(common.NewChatHiddenMessage(data.Type, append(names[:idx], names[idx+1:]...)))
if err != nil { 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: case common.CdMessage:
msg := html.EscapeString(data.Message) msg := html.EscapeString(data.Message)
@ -60,7 +65,7 @@ func (cl *Client) NewMsg(data common.ClientData) {
if response != "" { if response != "" {
err := cl.SendChatData(common.NewChatMessage("", "", err := cl.SendChatData(common.NewChatMessage("", "",
common.ParseEmotes(response), common.ParseEmotes(response),
common.CmdUser, common.CmdlUser,
common.MsgCommandResponse)) common.MsgCommandResponse))
if err != nil { if err != nil {
fmt.Printf("Error command results %v\n", err) 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) fmt.Printf("[chat] <%s> %q\n", cl.name, msg)
// Enable links for mods and admins // Enable links for mods and admins
if cl.IsMod || cl.IsAdmin { if cl.CmdLevel >= common.CmdlMod {
msg = formatLinks(msg) msg = formatLinks(msg)
} }
@ -108,7 +113,7 @@ func (cl *Client) Send(data common.ChatDataJSON) error {
} }
func (cl *Client) SendServerMessage(s string) 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 { if err != nil {
return fmt.Errorf("could send server message to %s: message - %#v: %v", cl.name, s, err) 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() { func (cl *Client) Mod() {
cl.IsMod = true if cl.CmdLevel < common.CmdlMod {
cl.CmdLevel = common.CmdlMod
}
} }
func (cl *Client) Unmod() { func (cl *Client) Unmod() {
cl.IsMod = false cl.CmdLevel = common.CmdlUser
} }
func (cl *Client) Host() string { func (cl *Client) Host() string {

View File

@ -52,22 +52,21 @@ var commands = &CommandControl{
common.CNAuth.String(): Command{ common.CNAuth.String(): Command{
HelpText: "Authenticate to admin", HelpText: "Authenticate to admin",
Function: func(cl *Client, args []string) string { Function: func(cl *Client, args []string) string {
if cl.IsAdmin { if cl.CmdLevel == common.CmdlAdmin {
return "You are already authenticated." return "You are already authenticated."
} }
pw := html.UnescapeString(strings.Join(args, " ")) pw := html.UnescapeString(strings.Join(args, " "))
if settings.AdminPassword == pw { if settings.AdminPassword == pw {
cl.IsMod = true cl.CmdLevel = common.CmdlAdmin
cl.IsAdmin = true
cl.belongsTo.AddModNotice(cl.name + " used the admin password") cl.belongsTo.AddModNotice(cl.name + " used the admin password")
fmt.Printf("[auth] %s used the admin password\n", cl.name) fmt.Printf("[auth] %s used the admin password\n", cl.name)
return "Admin rights granted." return "Admin rights granted."
} }
if cl.belongsTo.redeemModPass(pw) { if cl.belongsTo.redeemModPass(pw) {
cl.IsMod = true cl.CmdLevel = common.CmdlMod
cl.belongsTo.AddModNotice(cl.name + " used a mod password") cl.belongsTo.AddModNotice(cl.name + " used a mod password")
fmt.Printf("[auth] %s used a mod password\n", cl.name) fmt.Printf("[auth] %s used a mod password\n", cl.name)
return "Moderator privileges granted." return "Moderator privileges granted."
@ -100,7 +99,7 @@ var commands = &CommandControl{
// Two arguments to force a name change on another user: `/nick OldName NewName` // Two arguments to force a name change on another user: `/nick OldName NewName`
if len(args) == 2 { if len(args) == 2 {
if !cl.IsAdmin { if cl.CmdLevel != common.CmdlAdmin {
return "Only admins can do that PeepoSus" return "Only admins can do that PeepoSus"
} }
@ -109,7 +108,7 @@ var commands = &CommandControl{
forced = true 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." 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{ common.CNUnmod.String(): Command{
HelpText: "Revoke a user's moderator privilages. Moderators can only unmod themselves.", HelpText: "Revoke a user's moderator privilages. Moderators can only unmod themselves.",
Function: func(cl *Client, args []string) string { 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." 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 // Look for mod command
if modCmd, ok := cc.mod[cmd]; ok { 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, " ")) fmt.Printf("[mod] %s /%s %s\n", sender.name, command, strings.Join(args, " "))
return modCmd.Function(sender, args) return modCmd.Function(sender, args)
} }
@ -345,7 +344,7 @@ func (cc *CommandControl) RunCommand(command string, args []string, sender *Clie
// Look for admin command // Look for admin command
if adminCmd, ok := cc.admin[cmd]; ok { 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, " ")) fmt.Printf("[admin] %s /%s %s\n", sender.name, command, strings.Join(args, " "))
return adminCmd.Function(sender, 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 { func cmdHelp(cl *Client, args []string) string {
url := "/help" url := "/help"
if cl.IsMod {
url = "/help?mod=1" if cl.CmdLevel >= common.CmdlMod {
url += "?mod=1"
} }
if cl.IsAdmin { if cl.CmdLevel == common.CmdlAdmin {
url = "/help?mod=1&admin=1" url += "&admin=1"
} }
cl.SendChatData(common.NewChatCommand(common.CmdHelp, []string{url})) 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 { func getHelp(lvl common.CommandLevel) map[string]string {
var cmdList map[string]Command var cmdList map[string]Command
switch lvl { switch lvl {
case common.CmdUser: case common.CmdlUser:
cmdList = commands.user cmdList = commands.user
case common.CmdMod: case common.CmdlMod:
cmdList = commands.mod cmdList = commands.mod
case common.CmdAdmin: case common.CmdlAdmin:
cmdList = commands.admin 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 the caller is priviledged enough, they can change the color of another user
if len(args) == 2 { if len(args) == 2 {
if !(cl.IsMod || cl.IsAdmin) { if cl.CmdLevel == common.CmdlUser {
return "You cannot change someone else's color. PeepoSus" return "You cannot change someone else's color. PeepoSus"
} }
@ -492,6 +492,9 @@ var cmdColor = Command{
var cmdWhoAmI = Command{ var cmdWhoAmI = Command{
HelpText: "Shows debug user info", HelpText: "Shows debug user info",
Function: func(cl *Client, args []string) string { 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)
}, },
} }

View File

@ -154,11 +154,11 @@ func (cr *ChatRoom) Kick(name string) string {
return "Unable to get client for name " + name return "Unable to get client for name " + name
} }
if client.IsMod { if client.CmdLevel == common.CmdlMod {
return "You cannot kick another mod." return "You cannot kick another mod."
} }
if client.IsAdmin { if client.CmdLevel == common.CmdlAdmin {
return "Jebaited No." return "Jebaited No."
} }
@ -182,7 +182,7 @@ func (cr *ChatRoom) Ban(name string) string {
return "Cannot find that name" return "Cannot find that name"
} }
if client.IsAdmin { if client.CmdLevel == common.CmdlAdmin {
return "You cannot ban an admin Jebaited" 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 t = common.MsgServer
} }
lvl := common.CmdUser
if from.IsMod {
lvl = common.CmdMod
}
if from.IsAdmin {
lvl = common.CmdAdmin
}
select { 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: default:
fmt.Println("Unable to queue chat message. Channel full.") 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) { func (cr *ChatRoom) AddModNotice(message string) {
select { select {
case cr.modqueue <- common.NewChatMessage("", "", message, common.CmdUser, common.MsgNotice): case cr.modqueue <- common.NewChatMessage("", "", message, common.CmdlUser, common.MsgNotice):
default: default:
fmt.Println("Unable to queue notice. Channel full.") fmt.Println("Unable to queue notice. Channel full.")
} }
@ -288,8 +280,10 @@ func (cr *ChatRoom) Mod(name string) error {
return err return err
} }
client.IsMod = true if client.CmdLevel < common.CmdlMod {
client.SendServerMessage(`You have been modded.`) client.CmdLevel = common.CmdlMod
client.SendServerMessage(`You have been modded.`)
}
return nil return nil
} }
@ -357,7 +351,7 @@ func (cr *ChatRoom) Broadcast() {
case msg := <-cr.modqueue: case msg := <-cr.modqueue:
cr.clientsMtx.Lock() cr.clientsMtx.Lock()
for _, client := range cr.clients { for _, client := range cr.clients {
if client.IsMod || client.IsAdmin { if client.CmdLevel >= common.CmdlMod {
send(msg, client) send(msg, client)
} }
} }

View File

@ -111,9 +111,9 @@ func (dc DataMessage) HTML() string {
default: default:
badge := "" badge := ""
switch dc.Level { switch dc.Level {
case CmdMod: case CmdlMod:
badge = `<img src="/static/img/mod.png" class="badge" />` badge = `<img src="/static/img/mod.png" class="badge" />`
case CmdAdmin: case CmdlAdmin:
badge = `<img src="/static/img/admin.png" class="badge" />` badge = `<img src="/static/img/admin.png" class="badge" />`
} }
return `<div>` + badge + `<span class="name" style="color:` + dc.Color + `">` + dc.From + return `<div>` + badge + `<span class="name" style="color:` + dc.Color + `">` + dc.From +

View File

@ -7,7 +7,7 @@ const (
CdMessage ClientDataType = iota // a normal message from the client meant to be broadcast CdMessage ClientDataType = iota // a normal message from the client meant to be broadcast
CdUsers // get a list of users CdUsers // get a list of users
CdPing // ping the server to keep the connection alive 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 type DataType int
@ -36,9 +36,9 @@ type CommandLevel int
// Command access levels // Command access levels
const ( const (
CmdUser CommandLevel = iota CmdlUser CommandLevel = iota
CmdMod CmdlMod
CmdAdmin CmdlAdmin
) )
type EventType int type EventType int

View File

@ -169,15 +169,15 @@ func handleHelpTemplate(w http.ResponseWriter, r *http.Request) {
data := Data{ data := Data{
Title: "Help", Title: "Help",
Commands: getHelp(common.CmdUser), Commands: getHelp(common.CmdlUser),
} }
if len(r.URL.Query().Get("mod")) > 0 { 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 { if len(r.URL.Query().Get("admin")) > 0 {
data.AdminCommands = getHelp(common.CmdAdmin) data.AdminCommands = getHelp(common.CmdlAdmin)
} }
err = t.Execute(w, data) err = t.Execute(w, data)

View File

@ -19,6 +19,7 @@ const (
var ( var (
currentName string currentName string
auth common.CommandLevel
names []string names []string
filteredNames []string filteredNames []string
) )
@ -182,6 +183,8 @@ func recieve(v []js.Value) {
for _, i := range h.Data.([]interface{}) { for _, i := range h.Data.([]interface{}) {
names = append(names, i.(string)) names = append(names, i.(string))
} }
case common.CdAuth:
auth = h.Data.(common.CommandLevel)
} }
case common.DTEvent: case common.DTEvent:
d := chat.Data.(common.DataEvent) d := chat.Data.(common.DataEvent)
@ -277,6 +280,7 @@ func isValidName(this js.Value, v []js.Value) interface{} {
func debugValues(v []js.Value) { func debugValues(v []js.Value) {
fmt.Printf("currentName %#v\n", currentName) fmt.Printf("currentName %#v\n", currentName)
fmt.Printf("auth %#v\n", auth)
fmt.Printf("names %#v\n", names) fmt.Printf("names %#v\n", names)
fmt.Printf("filteredNames %#v\n", filteredNames) fmt.Printf("filteredNames %#v\n", filteredNames)
} }