From 87f8839a33ba9c53c50abb6b6cc42a6f02bdf6f9 Mon Sep 17 00:00:00 2001 From: Zorchenhimer Date: Sat, 23 Mar 2019 23:27:56 -0400 Subject: [PATCH 01/13] Only send Chat and Event messages to temp clients These should be the only messages that need to be sent to temp clients for now. --- chatroom.go | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/chatroom.go b/chatroom.go index a699fd6..c6f4366 100644 --- a/chatroom.go +++ b/chatroom.go @@ -328,6 +328,11 @@ func (cr *ChatRoom) Broadcast() { go send(msg, client) } + // Only send Chat and Event stuff to temp clients + if msg.Type != common.DTChat && msg.Type != common.DTEvent { + break + } + data, err := msg.ToJSON() if err != nil { fmt.Printf("Error converting ChatData to ChatDataJSON: %v\n", err) From 2f252d5ae83aaeb2dd06d4c2b13adb7847d1f64b Mon Sep 17 00:00:00 2001 From: Zorchenhimer Date: Sun, 24 Mar 2019 15:15:28 -0400 Subject: [PATCH 02/13] Strip @ prefix on names in commands Names can be passed to commands with @ prefixed to them. When forcing a color change on another user the @ explicitly defines the name, instead of simply being stripped off. This will allow the a user named "red" to have their color changed: `/color @red blue` A bunch of the color command code has changed with this to make things less ambiguous and to add some other checks. `/color red #FF0000` and `/color @red red` will change the color of the user "red" to the color red while `/color red red`will return with an error. Note that the color an name arguments can be in any order. Resolves #64 --- chatcommands.go | 107 +++++++++++++++++++++++++++++++++++++----------- chatroom.go | 2 +- common/utils.go | 2 +- 3 files changed, 84 insertions(+), 27 deletions(-) diff --git a/chatcommands.go b/chatcommands.go index 7354b59..c46c95e 100644 --- a/chatcommands.go +++ b/chatcommands.go @@ -94,16 +94,18 @@ var commands = &CommandControl{ return "Missing name to change to." } - newName := args[0] + newName := strings.TrimLeft(args[0], "@") oldName := cl.name forced := false + + // Two arguments to force a name change on another user: `/nick OldName NewName` if len(args) == 2 { if !cl.IsAdmin { return "Only admins can do that PeepoSus" } - oldName = args[0] - newName = args[1] + oldName = strings.TrimLeft(args[0], "@") + newName = strings.TrimLeft(args[1], "@") forced = true } @@ -182,18 +184,19 @@ var commands = &CommandControl{ return "You can only unmod yourself, not others." } - if len(args) == 0 || (len(args) == 1 && args[0] == cl.name) { + if len(args) == 0 || (len(args) == 1 && strings.TrimLeft(args[0], "@") == cl.name) { cl.Unmod() cl.belongsTo.AddModNotice(cl.name + " has unmodded themselves") return "You have unmodded yourself." } + name := strings.TrimLeft(args[0], "@") - if err := cl.belongsTo.Unmod(args[0]); err != nil { + if err := cl.belongsTo.Unmod(name); err != nil { return err.Error() } - cl.belongsTo.AddModNotice(cl.name + " has unmodded " + args[0]) - return fmt.Sprintf(`%s has been unmodded.`, args[0]) + cl.belongsTo.AddModNotice(cl.name + " has unmodded " + name) + return "" }, }, @@ -203,7 +206,7 @@ var commands = &CommandControl{ if len(args) == 0 { return "Missing name to kick." } - return cl.belongsTo.Kick(args[0]) + return cl.belongsTo.Kick(strings.TrimLeft(args[0], "@")) }, }, @@ -213,8 +216,10 @@ var commands = &CommandControl{ if len(args) == 0 { return "missing name to ban." } - fmt.Printf("[ban] Attempting to ban %s\n", strings.Join(args, "")) - return cl.belongsTo.Ban(args[0]) + + name := strings.TrimLeft(args[0], "@") + fmt.Printf("[ban] Attempting to ban %s\n", name) + return cl.belongsTo.Ban(name) }, }, @@ -224,13 +229,14 @@ var commands = &CommandControl{ if len(args) == 0 { return "missing name to unban." } - fmt.Printf("[ban] Attempting to unban %s\n", strings.Join(args, "")) + name := strings.TrimLeft(args[0], "@") + fmt.Printf("[ban] Attempting to unban %s\n", name) - err := settings.RemoveBan(args[0]) + err := settings.RemoveBan(name) if err != nil { return err.Error() } - cl.belongsTo.AddModNotice(cl.name + " has unbanned " + args[0]) + cl.belongsTo.AddModNotice(cl.name + " has unbanned " + name) return "" }, }, @@ -252,11 +258,13 @@ var commands = &CommandControl{ if len(args) == 0 { return "Missing user to mod." } - if err := cl.belongsTo.Mod(args[0]); err != nil { + + name := strings.TrimLeft(args[0], "@") + if err := cl.belongsTo.Mod(name); err != nil { return err.Error() } - cl.belongsTo.AddModNotice(cl.name + " has modded " + args[0]) - return fmt.Sprintf(`%s has been modded.`, args[0]) + cl.belongsTo.AddModNotice(cl.name + " has modded " + name) + return "" }, }, @@ -387,22 +395,71 @@ func getHelp(lvl common.CommandLevel) map[string]string { var cmdColor = Command{ HelpText: "Change user color.", Function: func(cl *Client, args []string) string { + if len(args) > 2 { + return "Too many arguments!" + } + // If the caller is priviledged enough, they can change the color of another user - if len(args) == 2 && (cl.IsMod || cl.IsAdmin) { - color := "" - name := "" - for _, s := range args { - if common.IsValidColor(s) { - color = s - } else { - name = s - } + if len(args) == 2 { + if !(cl.IsMod || cl.IsAdmin) { + return "You cannot change someone else's color. PeepoSus" } + + name, color := "", "" + + if strings.ToLower(args[0]) == strings.ToLower(args[1]) || + (common.IsValidColor(args[0]) && common.IsValidColor(args[1])) { + return "Name and color are ambiguous. Prefix the name with '@' or color with '#'" + } + + // Check for explicit name + if strings.HasPrefix(args[0], "@") { + name = strings.TrimLeft(args[0], "@") + color = args[1] + fmt.Println("Found explicit name: ", name) + } else if strings.HasPrefix(args[1], "@") { + name = strings.TrimLeft(args[1], "@") + color = args[0] + fmt.Println("Found explicit name: ", name) + + // Check for explicit color + } else if strings.HasPrefix(args[0], "#") { + name = strings.TrimPrefix(args[1], "@") // this shouldn't be needed, but just in case. + color = args[0] + fmt.Println("Found explicit color: ", color) + } else if strings.HasPrefix(args[1], "#") { + name = strings.TrimPrefix(args[0], "@") // this shouldn't be needed, but just in case. + color = args[1] + fmt.Println("Found explicit color: ", color) + + // Guess + } else if common.IsValidColor(args[0]) { + name = strings.TrimPrefix(args[1], "@") + color = args[0] + fmt.Println("Guessed name: ", name, " and color: ", color) + } else if common.IsValidColor(args[1]) { + name = strings.TrimPrefix(args[0], "@") + color = args[1] + fmt.Println("Guessed name: ", name, " and color: ", color) + } + + if name == "" { + return "Cannot determine name. Prefix name with @." + } + if color == "" { + return "Cannot determine color. Prefix name with @." + } + if color == "" { fmt.Printf("[color:mod] %s missing color\n", cl.name) return "Missing color" } + if name == "" { + fmt.Printf("[color:mod] %s missing name\n", cl.name) + return "Missing name" + } + if err := cl.belongsTo.ForceColorChange(name, color); err != nil { return err.Error() } diff --git a/chatroom.go b/chatroom.go index c6f4366..94e847e 100644 --- a/chatroom.go +++ b/chatroom.go @@ -85,7 +85,7 @@ func (cr *ChatRoom) Join(name, uid string) (*Client, error) { return nil, errors.New("connection is missing from temp connections") } - if !common.IsValidName(name) || common.IsValidColor(name) { + if !common.IsValidName(name) { return nil, UserFormatError{Name: name} } diff --git a/common/utils.go b/common/utils.go index 808af11..e08ed0a 100644 --- a/common/utils.go +++ b/common/utils.go @@ -12,5 +12,5 @@ var usernameRegex *regexp.Regexp = regexp.MustCompile(`^[0-9a-zA-Z_-]+$`) // and is not a valid color name func IsValidName(name string) bool { return 3 <= len(name) && len(name) <= 36 && - usernameRegex.MatchString(name) && !IsValidColor(name) + usernameRegex.MatchString(name) } From 60f3ade2eeee88f56791c84ee79bdb2d4c0305a7 Mon Sep 17 00:00:00 2001 From: Zorchenhimer Date: Sun, 24 Mar 2019 16:38:42 -0400 Subject: [PATCH 03/13] Fix clientsMtx lock without unlock in Broadcast() Reorganize the lock/unlock pairs to be more consistent and to fix a case with an uneven lock/unlock. --- chatroom.go | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/chatroom.go b/chatroom.go index 94e847e..27ba2cd 100644 --- a/chatroom.go +++ b/chatroom.go @@ -330,6 +330,10 @@ func (cr *ChatRoom) Broadcast() { // Only send Chat and Event stuff to temp clients if msg.Type != common.DTChat && msg.Type != common.DTEvent { + // Put this here instead of having two lock/unlock blocks. We want + // to avoid a case where a client is removed from the temp users + // and added to the clients between the two blocks. + cr.clientsMtx.Unlock() break } @@ -347,7 +351,6 @@ func (cr *ChatRoom) Broadcast() { }(conn, uuid) } } - cr.clientsMtx.Unlock() case msg := <-cr.modqueue: cr.clientsMtx.Lock() From 5c87d70d3e537537f5156e70a67773e7026969e9 Mon Sep 17 00:00:00 2001 From: joeyak Date: Sun, 24 Mar 2019 17:12:04 -0400 Subject: [PATCH 04/13] Change unlock to be consistant with the message break if --- chatroom.go | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/chatroom.go b/chatroom.go index 27ba2cd..363e947 100644 --- a/chatroom.go +++ b/chatroom.go @@ -340,16 +340,18 @@ func (cr *ChatRoom) Broadcast() { data, err := msg.ToJSON() if err != nil { fmt.Printf("Error converting ChatData to ChatDataJSON: %v\n", err) - } else { - for uuid, conn := range cr.tempConn { - go func(c *chatConnection, suid string) { - err = c.WriteData(data) - if err != nil { - fmt.Printf("Error writing data to connection: %v\n", err) - delete(cr.tempConn, suid) - } - }(conn, uuid) - } + cr.clientsMtx.Unlock() + break + } + + for uuid, conn := range cr.tempConn { + go func(c *chatConnection, suid string) { + err = c.WriteData(data) + if err != nil { + fmt.Printf("Error writing data to connection: %v\n", err) + delete(cr.tempConn, suid) + } + }(conn, uuid) } cr.clientsMtx.Unlock() case msg := <-cr.modqueue: From cd34480bba7c33e7ffff4e25ab41aa690f849dc1 Mon Sep 17 00:00:00 2001 From: joeyak Date: Sun, 24 Mar 2019 09:24:57 -0400 Subject: [PATCH 05/13] 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) } From fdbf39f00c8ee8a51804f79aa67eb86dc2b95d62 Mon Sep 17 00:00:00 2001 From: Zorchenhimer Date: Sun, 24 Mar 2019 18:51:39 -0400 Subject: [PATCH 06/13] Add some logging Functions added: - LogErrorf() - LogErrorln() - LogChatf() - LogChatln() - LogInfof() - LogInfoln() - LogDebugf() - LogDebugln() - LogDevf() - LogDevln() New settings configure the logging: LogLevel and LogFile. LogLevel can be set to one of: error, chat, info, or debug and will default to error. LogFile is an optional file to write to. Providing a file will not prevent output in the console. LogDevf() and LogDevln() only compile when the "dev" flag passed to build. This will cause Travis-CI to fail the build if it finds any calls to either function. --- .gitignore | 3 + Makefile | 19 ++-- chatclient.go | 12 +-- chatcommands.go | 58 ++++++------ chatroom.go | 32 +++---- common/emotes.go | 7 +- common/logging.go | 211 ++++++++++++++++++++++++++++++++++++++++++ common/logging_dev.go | 19 ++++ connection.go | 3 +- handlers.go | 44 ++++----- main.go | 20 ++-- settings.go | 18 +++- 12 files changed, 351 insertions(+), 95 deletions(-) create mode 100644 common/logging.go create mode 100644 common/logging_dev.go diff --git a/.gitignore b/.gitignore index 44bcd13..48df202 100644 --- a/.gitignore +++ b/.gitignore @@ -12,6 +12,9 @@ # Output of the go coverage tool, specifically when used with LiteIDE *.out +# Log files +*.log + # GoCode debug file debug diff --git a/Makefile b/Makefile index 8eda388..c51bb8b 100644 --- a/Makefile +++ b/Makefile @@ -1,15 +1,22 @@ -.PHONY: fmt vet get clean +TAGS= + +.PHONY: fmt vet get clean dev setdev all: fmt vet MovieNight MovieNight.exe static/main.wasm +setdev: + $(eval export TAGS=-tags "dev") + +dev: setdev all + MovieNight.exe: *.go common/*.go - GOOS=windows GOARCH=amd64 go build -o MovieNight.exe + GOOS=windows GOARCH=amd64 go build -o MovieNight.exe $(TAGS) MovieNight: *.go common/*.go - GOOS=linux GOARCH=386 go build -o MovieNight + GOOS=linux GOARCH=386 go build -o MovieNight $(TAGS) static/main.wasm: wasm/*.go common/*.go - GOOS=js GOARCH=wasm go build -o ./static/main.wasm wasm/*.go + GOOS=js GOARCH=wasm go build -o ./static/main.wasm $(TAGS) wasm/*.go clean: -rm MovieNight.exe MovieNight ./static/main.wasm @@ -23,5 +30,5 @@ get: go get golang.org/x/tools/cmd/goimports vet: - go vet ./... - GOOS=js GOARCH=wasm go vet ./... + go vet $(TAGS) ./... + GOOS=js GOARCH=wasm go vet $(TAGS) ./... diff --git a/chatclient.go b/chatclient.go index de07ff3..f0751cb 100644 --- a/chatclient.go +++ b/chatclient.go @@ -24,13 +24,13 @@ type Client struct { func (cl *Client) NewMsg(data common.ClientData) { switch data.Type { case common.CdAuth: - fmt.Printf("[chat|hidden] <%s> get auth level\n", cl.name) + LogChatf("[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) + LogErrorf("Error sending auth level to client: %v\n", err) } case common.CdUsers: - fmt.Printf("[chat|hidden] <%s> get list of users\n", cl.name) + common.LogChatf("[chat|hidden] <%s> get list of users\n", cl.name) names := chat.GetNames() idx := -1 @@ -42,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 users to client: %v\n", err) + common.LogErrorf("Error sending chat data: %v\n", err) } case common.CdMessage: msg := html.EscapeString(data.Message) @@ -68,7 +68,7 @@ func (cl *Client) NewMsg(data common.ClientData) { common.CmdlUser, common.MsgCommandResponse)) if err != nil { - fmt.Printf("Error command results %v\n", err) + common.LogErrorf("Error command results %v\n", err) } return } @@ -79,7 +79,7 @@ func (cl *Client) NewMsg(data common.ClientData) { msg = msg[0:400] } - fmt.Printf("[chat] <%s> %q\n", cl.name, msg) + common.LogChatf("[chat] <%s> %q\n", cl.name, msg) // Enable links for mods and admins if cl.CmdLevel >= common.CmdlMod { diff --git a/chatcommands.go b/chatcommands.go index 81c9bc0..db8b867 100644 --- a/chatcommands.go +++ b/chatcommands.go @@ -61,19 +61,19 @@ var commands = &CommandControl{ if settings.AdminPassword == pw { cl.CmdLevel = common.CmdlAdmin cl.belongsTo.AddModNotice(cl.name + " used the admin password") - fmt.Printf("[auth] %s used the admin password\n", cl.name) + common.LogInfof("[auth] %s used the admin password\n", cl.name) return "Admin rights granted." } if cl.belongsTo.redeemModPass(pw) { cl.CmdLevel = common.CmdlMod cl.belongsTo.AddModNotice(cl.name + " used a mod password") - fmt.Printf("[auth] %s used a mod password\n", cl.name) + common.LogInfof("[auth] %s used a mod password\n", cl.name) return "Moderator privileges granted." } cl.belongsTo.AddModNotice(cl.name + " attempted to auth without success") - fmt.Printf("[auth] %s gave an invalid password\n", cl.name) + common.LogInfof("[auth] %s gave an invalid password\n", cl.name) return "Invalid password." }, }, @@ -217,7 +217,7 @@ var commands = &CommandControl{ } name := strings.TrimLeft(args[0], "@") - fmt.Printf("[ban] Attempting to ban %s\n", name) + common.LogInfof("[ban] Attempting to ban %s\n", name) return cl.belongsTo.Ban(name) }, }, @@ -229,7 +229,7 @@ var commands = &CommandControl{ return "missing name to unban." } name := strings.TrimLeft(args[0], "@") - fmt.Printf("[ban] Attempting to unban %s\n", name) + common.LogInfof("[ban] Attempting to unban %s\n", name) err := settings.RemoveBan(name) if err != nil { @@ -243,7 +243,7 @@ var commands = &CommandControl{ common.CNPurge.String(): Command{ HelpText: "Purge the chat.", Function: func(cl *Client, args []string) string { - fmt.Println("[purge] clearing chat") + common.LogInfoln("[purge] clearing chat") cl.belongsTo.AddCmdMsg(common.CmdPurgeChat, nil) return "" }, @@ -282,12 +282,12 @@ var commands = &CommandControl{ cl.SendServerMessage("Reloading emotes") num, err := common.LoadEmotes() if err != nil { - fmt.Printf("Unbale to reload emotes: %s\n", err) + common.LogErrorf("Unbale to reload emotes: %s\n", err) return fmt.Sprintf("ERROR: %s", err) } cl.belongsTo.AddModNotice(cl.name + " has reloaded emotes") - fmt.Printf("Loaded %d emotes\n", num) + common.LogInfof("Loaded %d emotes\n", num) return fmt.Sprintf("Emotes loaded: %d", num) }, }, @@ -302,17 +302,17 @@ var commands = &CommandControl{ }, common.CNIP.String(): Command{ - HelpText: "list users and IP in the server console", + HelpText: "List users and IP in the server console. Requires logging level to be set to info or above.", Function: func(cl *Client, args []string) string { cl.belongsTo.clientsMtx.Lock() - fmt.Println("Clients:") + common.LogInfoln("Clients:") for uuid, client := range cl.belongsTo.clients { - fmt.Printf(" [%s] %s %s\n", uuid, client.name, client.conn.Host()) + common.LogInfof(" [%s] %s %s\n", uuid, client.name, client.conn.Host()) } - fmt.Println("TmpConn:") + common.LogInfoln("TmpConn:") for uuid, conn := range cl.belongsTo.tempConn { - fmt.Printf(" [%s] %s\n", uuid, conn.Host()) + common.LogInfof(" [%s] %s\n", uuid, conn.Host()) } cl.belongsTo.clientsMtx.Unlock() return "see console for output" @@ -327,33 +327,33 @@ func (cc *CommandControl) RunCommand(command string, args []string, sender *Clie // Look for user command if userCmd, ok := cc.user[cmd]; ok { - fmt.Printf("[user] %s /%s %s\n", sender.name, command, strings.Join(args, " ")) + common.LogInfof("[user] %s /%s %s\n", sender.name, command, strings.Join(args, " ")) return userCmd.Function(sender, args) } // Look for mod command if modCmd, ok := cc.mod[cmd]; ok { if sender.CmdLevel >= common.CmdlMod { - fmt.Printf("[mod] %s /%s %s\n", sender.name, command, strings.Join(args, " ")) + common.LogInfof("[mod] %s /%s %s\n", sender.name, command, strings.Join(args, " ")) return modCmd.Function(sender, args) } - fmt.Printf("[mod REJECTED] %s /%s %s\n", sender.name, command, strings.Join(args, " ")) + common.LogInfof("[mod REJECTED] %s /%s %s\n", sender.name, command, strings.Join(args, " ")) return "You are not a mod Jebaited" } // Look for admin command if adminCmd, ok := cc.admin[cmd]; ok { if sender.CmdLevel == common.CmdlAdmin { - fmt.Printf("[admin] %s /%s %s\n", sender.name, command, strings.Join(args, " ")) + common.LogInfof("[admin] %s /%s %s\n", sender.name, command, strings.Join(args, " ")) return adminCmd.Function(sender, args) } - fmt.Printf("[admin REJECTED] %s /%s %s\n", sender.name, command, strings.Join(args, " ")) + common.LogInfof("[admin REJECTED] %s /%s %s\n", sender.name, command, strings.Join(args, " ")) return "You are not the admin Jebaited" } // Command not found - fmt.Printf("[cmd] %s /%s %s\n", sender.name, command, strings.Join(args, " ")) + common.LogInfof("[cmd] %s /%s %s\n", sender.name, command, strings.Join(args, " ")) return "Invalid command." } @@ -416,31 +416,31 @@ var cmdColor = Command{ if strings.HasPrefix(args[0], "@") { name = strings.TrimLeft(args[0], "@") color = args[1] - fmt.Println("Found explicit name: ", name) + common.LogDebugln("[color:mod] Found explicit name: ", name) } else if strings.HasPrefix(args[1], "@") { name = strings.TrimLeft(args[1], "@") color = args[0] - fmt.Println("Found explicit name: ", name) + common.LogDebugln("[color:mod] Found explicit name: ", name) // Check for explicit color } else if strings.HasPrefix(args[0], "#") { name = strings.TrimPrefix(args[1], "@") // this shouldn't be needed, but just in case. color = args[0] - fmt.Println("Found explicit color: ", color) + common.LogDebugln("[color:mod] Found explicit color: ", color) } else if strings.HasPrefix(args[1], "#") { name = strings.TrimPrefix(args[0], "@") // this shouldn't be needed, but just in case. color = args[1] - fmt.Println("Found explicit color: ", color) + common.LogDebugln("[color:mod] Found explicit color: ", color) // Guess } else if common.IsValidColor(args[0]) { name = strings.TrimPrefix(args[1], "@") color = args[0] - fmt.Println("Guessed name: ", name, " and color: ", color) + common.LogDebugln("[color:mod] Guessed name: ", name, " and color: ", color) } else if common.IsValidColor(args[1]) { name = strings.TrimPrefix(args[0], "@") color = args[1] - fmt.Println("Guessed name: ", name, " and color: ", color) + common.LogDebugln("[color:mod] Guessed name: ", name, " and color: ", color) } if name == "" { @@ -451,12 +451,12 @@ var cmdColor = Command{ } if color == "" { - fmt.Printf("[color:mod] %s missing color\n", cl.name) + common.LogInfof("[color:mod] %s missing color\n", cl.name) return "Missing color" } if name == "" { - fmt.Printf("[color:mod] %s missing name\n", cl.name) + common.LogInfof("[color:mod] %s missing name\n", cl.name) return "Missing name" } @@ -469,7 +469,7 @@ var cmdColor = Command{ // Don't allow an unprivilaged user to change their color if // it was changed by a mod if cl.IsColorForced { - fmt.Printf("[color] %s tried to change a forced color\n", cl.name) + common.LogInfof("[color] %s tried to change a forced color\n", cl.name) return "You are not allowed to change your color." } @@ -484,7 +484,7 @@ var cmdColor = Command{ } cl.color = args[0] - fmt.Printf("[color] %s new color: %s\n", cl.name, cl.color) + common.LogInfof("[color] %s new color: %s\n", cl.name, cl.color) return "Color changed successfully." }, } diff --git a/chatroom.go b/chatroom.go index 7d0b4cd..1177ae5 100644 --- a/chatroom.go +++ b/chatroom.go @@ -45,7 +45,7 @@ func newChatRoom() (*ChatRoom, error) { if err != nil { return nil, fmt.Errorf("error loading emotes: %s", err) } - fmt.Printf("Loaded %d emotes\n", num) + common.LogInfof("Loaded %d emotes\n", num) //the "heartbeat" for broadcasting messages go cr.Broadcast() @@ -113,10 +113,10 @@ func (cr *ChatRoom) Join(name, uid string) (*Client, error) { cr.clients[uid] = client delete(cr.tempConn, uid) - fmt.Printf("[join] %s %s\n", host, name) + common.LogChatf("[join] %s %s\n", host, name) playingCommand, err := common.NewChatCommand(common.CmdPlaying, []string{cr.playing, cr.playingLink}).ToJSON() if err != nil { - fmt.Printf("Unable to encode playing command on join: %s\n", err) + common.LogErrorf("Unable to encode playing command on join: %s\n", err) } else { client.Send(playingCommand) } @@ -132,7 +132,7 @@ func (cr *ChatRoom) Leave(name, color string) { client, suid, err := cr.getClient(name) if err != nil { - fmt.Printf("[leave] Unable to get client suid %v\n", err) + common.LogErrorf("[leave] Unable to get client suid %v\n", err) return } host := client.Host() @@ -141,7 +141,7 @@ func (cr *ChatRoom) Leave(name, color string) { cr.delClient(suid) cr.AddEventMsg(common.EvLeave, name, color) - fmt.Printf("[leave] %s %s\n", host, name) + common.LogChatf("[leave] %s %s\n", host, name) } // kicked from the chatroom @@ -168,7 +168,7 @@ func (cr *ChatRoom) Kick(name string) string { cr.delClient(suid) cr.AddEventMsg(common.EvKick, name, color) - fmt.Printf("[kick] %s %s has been kicked\n", host, name) + common.LogInfof("[kick] %s %s has been kicked\n", host, name) return "" } @@ -178,7 +178,7 @@ func (cr *ChatRoom) Ban(name string) string { client, suid, err := cr.getClient(name) if err != nil { - fmt.Printf("[ban] Unable to get client for name %q\n", name) + common.LogErrorf("[ban] Unable to get client for name %q\n", name) return "Cannot find that name" } @@ -206,7 +206,7 @@ func (cr *ChatRoom) Ban(name string) string { err = settings.AddBan(host, names) if err != nil { - fmt.Printf("[BAN] Error banning %q: %s\n", name, err) + common.LogErrorf("[BAN] Error banning %q: %s\n", name, err) cr.AddEventMsg(common.EvKick, name, color) } else { cr.AddEventMsg(common.EvBan, name, color) @@ -229,7 +229,7 @@ func (cr *ChatRoom) AddMsg(from *Client, isAction, isServer bool, msg string) { select { case cr.queue <- common.NewChatMessage(from.name, from.color, msg, from.CmdLevel, t): default: - fmt.Println("Unable to queue chat message. Channel full.") + common.LogErrorln("Unable to queue chat message. Channel full.") } } @@ -237,7 +237,7 @@ func (cr *ChatRoom) AddCmdMsg(command common.CommandType, args []string) { select { case cr.queue <- common.NewChatCommand(command, args): default: - fmt.Println("Unable to queue command message. Channel full.") + common.LogErrorln("Unable to queue command message. Channel full.") } } @@ -245,7 +245,7 @@ func (cr *ChatRoom) AddModNotice(message string) { select { case cr.modqueue <- common.NewChatMessage("", "", message, common.CmdlUser, common.MsgNotice): default: - fmt.Println("Unable to queue notice. Channel full.") + common.LogErrorln("Unable to queue notice. Channel full.") } } @@ -253,7 +253,7 @@ func (cr *ChatRoom) AddEventMsg(event common.EventType, name, color string) { select { case cr.queue <- common.NewChatEvent(event, name, color): default: - fmt.Println("Unable to queue event message. Channel full.") + common.LogErrorln("Unable to queue event message. Channel full.") } } @@ -310,7 +310,7 @@ func (cr *ChatRoom) Broadcast() { send := func(data common.ChatData, client *Client) { err := client.SendChatData(data) if err != nil { - fmt.Printf("Error sending data to client: %v\n", err) + common.LogErrorf("Error sending data to client: %v\n", err) } } @@ -333,7 +333,7 @@ func (cr *ChatRoom) Broadcast() { data, err := msg.ToJSON() if err != nil { - fmt.Printf("Error converting ChatData to ChatDataJSON: %v\n", err) + common.LogErrorf("Error converting ChatData to ChatDataJSON: %v\n", err) cr.clientsMtx.Unlock() break } @@ -342,7 +342,7 @@ func (cr *ChatRoom) Broadcast() { go func(c *chatConnection, suid string) { err = c.WriteData(data) if err != nil { - fmt.Printf("Error writing data to connection: %v\n", err) + common.LogErrorf("Error writing data to connection: %v\n", err) delete(cr.tempConn, suid) } }(conn, uuid) @@ -484,7 +484,7 @@ func (cr *ChatRoom) changeName(oldName, newName string, forced bool) error { if currentClient != nil { currentClient.name = newName - fmt.Printf("%q -> %q\n", oldName, newName) + common.LogDebugf("%q -> %q\n", oldName, newName) if forced { cr.AddEventMsg(common.EvNameChangeForced, oldName+":"+newName, currentClient.color) diff --git a/common/emotes.go b/common/emotes.go index f2ff30b..b330c7b 100644 --- a/common/emotes.go +++ b/common/emotes.go @@ -47,14 +47,15 @@ func LoadEmotes() (int, error) { globbed_files := []string(emotePNGs) globbed_files = append(globbed_files, emoteGIFs...) - fmt.Println("Loading emotes...") + LogInfoln("Loading emotes...") + emInfo := []string{} for _, file := range globbed_files { file = filepath.Base(file) key := file[0 : len(file)-4] newEmotes[key] = file - fmt.Printf("%s ", key) + emInfo = append(emInfo, key) } Emotes = newEmotes - fmt.Println("") + LogInfoln(strings.Join(emInfo, " ")) return len(Emotes), nil } diff --git a/common/logging.go b/common/logging.go new file mode 100644 index 0000000..8bbd5c1 --- /dev/null +++ b/common/logging.go @@ -0,0 +1,211 @@ +package common + +import ( + "fmt" + "io" + "log" + "os" +) + +var loglevel LogLevel + +type LogLevel string + +const ( + LLError LogLevel = "error" // only log errors + LLChat LogLevel = "chat" // log chat and commands + LLInfo LogLevel = "info" // log info messages (not quite debug, but not chat) + LLDebug LogLevel = "debug" // log everything +) + +const ( + logPrefixError string = "[ERROR] " + logPrefixChat string = "[CHAT] " + logPrefixInfo string = "[INFO] " + logPrefixDebug string = "[DEBUG] " + logPrefixDev string = "[DEV] " +) + +var ( + logError *log.Logger + logChat *log.Logger + logInfo *log.Logger + logDebug *log.Logger + logDev *log.Logger +) + +//func ParseLogLevel(input string) LogLevel { +// switch LogLevel(input) { +// case LLError, LLChat, LLInfo, LLDebug: +// return LogLevel(input) +// default: +// return LLError +// } +//} + +func SetupLogging(level LogLevel, file string) error { + switch level { + case LLDebug: + if file == "" { + logError = log.New(os.Stderr, logPrefixError, log.LstdFlags) + logChat = log.New(os.Stdout, logPrefixChat, log.LstdFlags) + logDebug = log.New(os.Stdout, logPrefixDebug, log.LstdFlags) + logInfo = log.New(os.Stdout, logPrefixInfo, log.LstdFlags) + } else { + f, err := os.OpenFile(file, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644) + if err != nil { + return fmt.Errorf("Unable to open log file for writing: %s", err) + } + logError = log.New(io.MultiWriter(os.Stderr, f), logPrefixError, log.LstdFlags) + logChat = log.New(io.MultiWriter(os.Stdout, f), logPrefixChat, log.LstdFlags) + logInfo = log.New(io.MultiWriter(os.Stdout, f), logPrefixInfo, log.LstdFlags) + logDebug = log.New(io.MultiWriter(os.Stdout, f), logPrefixDebug, log.LstdFlags) + } + case LLChat: + logDebug = nil + if file == "" { + logError = log.New(os.Stderr, logPrefixError, log.LstdFlags) + logChat = log.New(os.Stdout, logPrefixChat, log.LstdFlags) + logInfo = log.New(os.Stdout, logPrefixInfo, log.LstdFlags) + } else { + f, err := os.OpenFile(file, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644) + if err != nil { + return fmt.Errorf("Unable to open log file for writing: %s", err) + } + logError = log.New(io.MultiWriter(os.Stderr, f), logPrefixError, log.LstdFlags) + logChat = log.New(io.MultiWriter(os.Stdout, f), logPrefixChat, log.LstdFlags) + logInfo = log.New(io.MultiWriter(os.Stdout, f), logPrefixInfo, log.LstdFlags) + } + + case LLInfo: + logDebug = nil + logChat = nil + if file == "" { + logError = log.New(os.Stderr, logPrefixError, log.LstdFlags) + logInfo = log.New(os.Stdout, logPrefixInfo, log.LstdFlags) + } else { + f, err := os.OpenFile(file, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644) + if err != nil { + return fmt.Errorf("Unable to open log file for writing: %s", err) + } + logError = log.New(io.MultiWriter(os.Stderr, f), logPrefixError, log.LstdFlags) + logInfo = log.New(io.MultiWriter(os.Stdout, f), logPrefixInfo, log.LstdFlags) + } + + // Default to error + default: + logChat = nil + logDebug = nil + logInfo = nil + if file == "" { + logError = log.New(os.Stderr, logPrefixError, log.LstdFlags) + } else { + f, err := os.OpenFile(file, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644) + if err != nil { + return fmt.Errorf("Unable to open log file for writing: %s", err) + } + logError = log.New(io.MultiWriter(os.Stderr, f), logPrefixError, log.LstdFlags) + } + } + return nil +} + +func LogErrorf(format string, v ...interface{}) { + if logError == nil { + panic("Logging not setup!") + } + + logError.Printf(format, v...) +} + +func LogErrorln(v ...interface{}) { + if logError == nil { + panic("Logging not setup!") + } + + logError.Println(v...) +} + +func LogChatf(format string, v ...interface{}) { + // if logError isn't set to something, logging wasn't setup. + if logError == nil { + panic("Logging not setup!") + } + + // logging chat and commands is turned off. + if logChat == nil { + return + } + + logChat.Printf(format, v...) +} + +func LogChatln(v ...interface{}) { + // if logError isn't set to something, logging wasn't setup. + if logError == nil { + panic("Logging not setup!") + } + + // logging chat and commands is turned off. + if logChat == nil { + return + } + + logChat.Println(v...) +} + +func LogInfof(format string, v ...interface{}) { + // if logError isn't set to something, logging wasn't setup. + if logError == nil { + panic("Logging not setup!") + } + + // logging info is turned off. + if logInfo == nil { + return + } + + logInfo.Printf(format, v...) +} + +func LogInfoln(v ...interface{}) { + // if logError isn't set to something, logging wasn't setup. + if logError == nil { + panic("Logging not setup!") + } + + // logging info is turned off. + if logInfo == nil { + return + } + + logInfo.Println(v...) +} + +func LogDebugf(format string, v ...interface{}) { + // if logError isn't set to something, logging wasn't setup. + if logError == nil { + panic("Logging not setup!") + } + + // logging debug is turned off. + if logDebug == nil { + return + } + + logDebug.Printf(format, v...) +} + +func LogDebugln(v ...interface{}) { + // if logError isn't set to something, logging wasn't setup. + if logError == nil { + panic("Logging not setup!") + } + + // logging debug is turned off. + if logDebug == nil { + return + } + + logDebug.Println(v...) +} diff --git a/common/logging_dev.go b/common/logging_dev.go new file mode 100644 index 0000000..32c9646 --- /dev/null +++ b/common/logging_dev.go @@ -0,0 +1,19 @@ +// +build dev + +package common + +func LogDevf(format string, v ...interface{}) { + if logError == nil { + panic("Logging not setup!") + } + + logError.Printf(format, v...) +} + +func LogDevln(v ...interface{}) { + if logError == nil { + panic("Logging not setup!") + } + + logError.Println(v...) +} diff --git a/connection.go b/connection.go index 715b412..f2f8cc7 100644 --- a/connection.go +++ b/connection.go @@ -6,6 +6,7 @@ import ( "sync" "github.com/gorilla/websocket" + "github.com/zorchenhimer/MovieNight/common" ) type chatConnection struct { @@ -31,7 +32,7 @@ func (cc *chatConnection) WriteData(data interface{}) error { err := cc.WriteJSON(data) if err != nil { if operr, ok := err.(*net.OpError); ok { - fmt.Println("OpError: " + operr.Err.Error()) + common.LogDebugln("OpError: " + operr.Err.Error()) } return fmt.Errorf("Error writing data to %s %s: %v", cc.clientName, cc.Host(), err) } diff --git a/handlers.go b/handlers.go index 4a3ebfb..710535a 100644 --- a/handlers.go +++ b/handlers.go @@ -1,7 +1,6 @@ package main import ( - "fmt" "html/template" "io" "net/http" @@ -58,7 +57,7 @@ func wsStaticFiles(w http.ResponseWriter, r *http.Request) { } goodPath := r.URL.Path[8:len(r.URL.Path)] - fmt.Printf("[static] serving %q from folder ./static/\n", goodPath) + common.LogDebugf("[static] serving %q from folder ./static/\n", goodPath) http.ServeFile(w, r, "./static/"+goodPath) } @@ -70,7 +69,7 @@ func wsWasmFile(w http.ResponseWriter, r *http.Request) { func wsImages(w http.ResponseWriter, r *http.Request) { base := filepath.Base(r.URL.Path) - fmt.Println("[img] ", base) + common.LogDebugln("[img] ", base) http.ServeFile(w, r, "./static/img/"+base) } @@ -91,7 +90,7 @@ func wsHandler(w http.ResponseWriter, r *http.Request) { conn, err := upgrader.Upgrade(w, r, nil) if err != nil { - fmt.Println("Error upgrading to websocket:", err) + common.LogErrorln("Error upgrading to websocket:", err) return } @@ -107,7 +106,7 @@ func wsHandler(w http.ResponseWriter, r *http.Request) { uid, err := chat.JoinTemp(chatConn) if err != nil { - fmt.Printf("[handler] could not do a temp join, %v\n", err) + common.LogErrorf("[handler] could not do a temp join, %v\n", err) conn.Close() } @@ -117,7 +116,7 @@ func wsHandler(w http.ResponseWriter, r *http.Request) { var data common.ClientData err := chatConn.ReadData(&data) if err != nil { - fmt.Printf("[handler] Client closed connection: %s\n", conn.RemoteAddr().String()) + common.LogInfof("[handler] Client closed connection: %s\n", conn.RemoteAddr().String()) conn.Close() return } @@ -126,14 +125,14 @@ func wsHandler(w http.ResponseWriter, r *http.Request) { if err != nil { switch err.(type) { case UserFormatError, UserTakenError: - fmt.Printf("[handler|%s] %v\n", errorName(err), err) + common.LogInfof("[handler|%s] %v\n", errorName(err), err) case BannedUserError: - fmt.Printf("[handler|%s] %v\n", errorName(err), err) + common.LogInfof("[handler|%s] %v\n", errorName(err), err) // close connection since banned users shouldn't be connecting conn.Close() default: // for now all errors not caught need to be warned - fmt.Printf("[handler|uncaught] %v\n", err) + common.LogErrorf("[handler|uncaught] %v\n", err) conn.Close() } } @@ -156,7 +155,7 @@ func wsHandler(w http.ResponseWriter, r *http.Request) { func handleHelpTemplate(w http.ResponseWriter, r *http.Request) { t, err := template.ParseFiles("./static/base.html", "./static/help.html") if err != nil { - fmt.Printf("Error parsing template file, %v\n", err) + common.LogErrorf("Error parsing template file, %v\n", err) return } @@ -182,14 +181,14 @@ func handleHelpTemplate(w http.ResponseWriter, r *http.Request) { err = t.Execute(w, data) if err != nil { - fmt.Printf("Error executing file, %v", err) + common.LogErrorf("Error executing file, %v", err) } } func handleIndexTemplate(w http.ResponseWriter, r *http.Request) { t, err := template.ParseFiles("./static/base.html", "./static/main.html") if err != nil { - fmt.Printf("Error parsing template file, %v\n", err) + common.LogErrorf("Error parsing template file, %v\n", err) return } @@ -220,7 +219,7 @@ func handleIndexTemplate(w http.ResponseWriter, r *http.Request) { err = t.Execute(w, data) if err != nil { - fmt.Printf("Error executing file, %v", err) + common.LogErrorf("Error executing file, %v", err) } } @@ -228,22 +227,22 @@ func handlePublish(conn *rtmp.Conn) { streams, _ := conn.Streams() l.Lock() - fmt.Println("request string->", conn.URL.RequestURI()) + common.LogDebugln("request string->", conn.URL.RequestURI()) urlParts := strings.Split(strings.Trim(conn.URL.RequestURI(), "/"), "/") - fmt.Println("urlParts->", urlParts) + common.LogDebugln("urlParts->", urlParts) if len(urlParts) > 2 { - fmt.Println("Extra garbage after stream key") + common.LogErrorln("Extra garbage after stream key") return } if len(urlParts) != 2 { - fmt.Println("Missing stream key") + common.LogErrorln("Missing stream key") return } if urlParts[1] != settings.GetStreamKey() { - fmt.Println("Due to key not match, denied stream") + common.LogErrorln("Stream key is incorrect. Denying stream.") return //If key not match, deny stream } @@ -259,13 +258,13 @@ func handlePublish(conn *rtmp.Conn) { } l.Unlock() if ch == nil { - fmt.Println("Unable to start stream, channel is nil.") + common.LogErrorln("Unable to start stream, channel is nil.") return } - fmt.Println("Stream started") + common.LogInfoln("Stream started") avutil.CopyPackets(ch.que, conn) - fmt.Println("Stream finished") + common.LogInfoln("Stream finished") l.Lock() delete(channels, streamPath) @@ -303,7 +302,8 @@ func handleDefault(w http.ResponseWriter, r *http.Request) { avutil.CopyFile(muxer, cursor) } else { if r.URL.Path != "/" { - fmt.Println("[http 404] ", r.URL.Path) + // not really an error for the server, but for the client. + common.LogInfoln("[http 404] ", r.URL.Path) http.NotFound(w, r) } else { handleIndexTemplate(w, r) diff --git a/main.go b/main.go index 517ff4f..d5e86e5 100644 --- a/main.go +++ b/main.go @@ -2,13 +2,13 @@ package main import ( "flag" - "fmt" "net/http" "os" "os/signal" "github.com/nareix/joy4/format" "github.com/nareix/joy4/format/rtmp" + "github.com/zorchenhimer/MovieNight/common" ) var ( @@ -35,7 +35,7 @@ func main() { // Load emotes before starting server. var err error if chat, err = newChatRoom(); err != nil { - fmt.Println(err) + common.LogErrorln(err) os.Exit(1) } @@ -43,15 +43,17 @@ func main() { addr = settings.ListenAddress } + common.LogDevln("This should break things") + // A stream key was passed on the command line. Use it, but don't save // it over the stream key in the settings.json file. if sKey != "" { settings.SetTempKey(sKey) } - fmt.Println("Stream key: ", settings.GetStreamKey()) - fmt.Println("Admin password: ", settings.AdminPassword) - fmt.Println("Listen and serve ", addr) + common.LogInfoln("Stream key: ", settings.GetStreamKey()) + common.LogInfoln("Admin password: ", settings.AdminPassword) + common.LogInfoln("Listen and serve ", addr) go startServer() go startRmtpServer() @@ -66,7 +68,8 @@ func startRmtpServer() { } err := server.ListenAndServe() if err != nil { - fmt.Printf("Error trying to start server: %v\n", err) + // If the server cannot start, don't pretend we can continue. + panic("Error trying to start rtmp server: " + err.Error()) } } @@ -87,7 +90,8 @@ func startServer() { err := http.ListenAndServe(addr, nil) if err != nil { - fmt.Printf("Error trying to start rmtp server: %v\n", err) + // If the server cannot start, don't pretend we can continue. + panic("Error trying to start chat/http server: " + err.Error()) } } @@ -95,7 +99,7 @@ func handleInterrupt(exit chan bool) { ch := make(chan os.Signal) signal.Notify(ch, os.Interrupt) <-ch - fmt.Println("Closing server") + common.LogInfoln("Closing server") if settings.StreamStats { stats.Print() } diff --git a/settings.go b/settings.go index 7fa4626..dc47eb5 100644 --- a/settings.go +++ b/settings.go @@ -9,6 +9,8 @@ import ( "strings" "sync" "time" + + "github.com/zorchenhimer/MovieNight/common" ) var settings *Settings @@ -27,6 +29,8 @@ type Settings struct { StreamKey string ListenAddress string Bans []BanInfo + LogLevel common.LogLevel + LogFile string } type BanInfo struct { @@ -45,8 +49,8 @@ func init() { panic("Missing stream key is settings.json") } - if settings.TitleLength <= 0 { - settings.TitleLength = 50 + if err = common.SetupLogging(settings.LogLevel, settings.LogFile); err != nil { + panic("Unable to setup logger: " + err.Error()) } // Save admin password to file @@ -79,8 +83,14 @@ func LoadSettings(filename string) (*Settings, error) { if err != nil { return nil, fmt.Errorf("unable to generate admin password: %s", err) } + + // Don't use LogInfof() here. Log isn't setup yet when LoadSettings() is called from init(). fmt.Printf("Settings reloaded. New admin password: %s\n", s.AdminPassword) + if s.TitleLength <= 0 { + s.TitleLength = 50 + } + return s, nil } @@ -122,7 +132,7 @@ func (s *Settings) AddBan(host string, names []string) error { } settings.Bans = append(settings.Bans, b) - fmt.Printf("[BAN] %q (%s) has been banned.\n", strings.Join(names, ", "), host) + common.LogInfof("[BAN] %q (%s) has been banned.\n", strings.Join(names, ", "), host) return settings.Save() } @@ -136,7 +146,7 @@ func (s *Settings) RemoveBan(name string) error { for _, b := range s.Bans { for _, n := range b.Names { if n == name { - fmt.Printf("[ban] Removed ban for %s [%s]\n", b.IP, n) + common.LogInfof("[ban] Removed ban for %s [%s]\n", b.IP, n) } else { newBans = append(newBans, b) } From 5d10fdfea4f0760c4524d140fb94c61d4e637895 Mon Sep 17 00:00:00 2001 From: Zorchenhimer Date: Sun, 24 Mar 2019 19:03:01 -0400 Subject: [PATCH 07/13] Forgot to remove a DevLogLn(), and two fixes Good news, it breaks Travis like it's supposed to! I also forgot to add the `common` namespace during a rebase. Whoops. Resolves #46 --- chatclient.go | 4 ++-- main.go | 2 -- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/chatclient.go b/chatclient.go index f0751cb..edd9902 100644 --- a/chatclient.go +++ b/chatclient.go @@ -24,10 +24,10 @@ type Client struct { func (cl *Client) NewMsg(data common.ClientData) { switch data.Type { case common.CdAuth: - LogChatf("[chat|hidden] <%s> get auth level\n", cl.name) + common.LogChatf("[chat|hidden] <%s> get auth level\n", cl.name) err := cl.SendChatData(common.NewChatHiddenMessage(data.Type, cl.CmdLevel)) if err != nil { - LogErrorf("Error sending auth level to client: %v\n", err) + common.LogErrorf("Error sending auth level to client: %v\n", err) } case common.CdUsers: common.LogChatf("[chat|hidden] <%s> get list of users\n", cl.name) diff --git a/main.go b/main.go index d5e86e5..6cdee49 100644 --- a/main.go +++ b/main.go @@ -43,8 +43,6 @@ func main() { addr = settings.ListenAddress } - common.LogDevln("This should break things") - // A stream key was passed on the command line. Use it, but don't save // it over the stream key in the settings.json file. if sKey != "" { From d419f643798b3e859e7229585e0808346f8a603b Mon Sep 17 00:00:00 2001 From: joeyak Date: Sun, 24 Mar 2019 20:09:55 -0400 Subject: [PATCH 08/13] goimports found a line with an extra tab --- chatroom.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/chatroom.go b/chatroom.go index 1177ae5..aa720af 100644 --- a/chatroom.go +++ b/chatroom.go @@ -342,7 +342,7 @@ func (cr *ChatRoom) Broadcast() { go func(c *chatConnection, suid string) { err = c.WriteData(data) if err != nil { - common.LogErrorf("Error writing data to connection: %v\n", err) + common.LogErrorf("Error writing data to connection: %v\n", err) delete(cr.tempConn, suid) } }(conn, uuid) From 86207ecd48efa0857f70faa8a8bcdd7d608efe22 Mon Sep 17 00:00:00 2001 From: joeyak Date: Sun, 24 Mar 2019 23:43:30 -0400 Subject: [PATCH 09/13] Added spoilers in chat If a user clicks the spoiler, it will change color and the background will revert. closes #62 --- Makefile | 7 +++++-- chatclient.go | 45 ++++++++++++++++++++++++++++++++++--------- chatclient_test.go | 23 ++++++++++++++++++++++ chatroom.go | 11 +++++++++-- common/colors.go | 6 +++++- common/logging.go | 11 ----------- common/logging_dev.go | 23 ++++++++++++---------- settings.go | 6 +++++- static/css/site.css | 16 +++++++++++++++ 9 files changed, 112 insertions(+), 36 deletions(-) create mode 100644 chatclient_test.go diff --git a/Makefile b/Makefile index c51bb8b..8ad7e2f 100644 --- a/Makefile +++ b/Makefile @@ -1,8 +1,8 @@ TAGS= -.PHONY: fmt vet get clean dev setdev +.PHONY: fmt vet get clean dev setdev test -all: fmt vet MovieNight MovieNight.exe static/main.wasm +all: fmt vet test MovieNight MovieNight.exe static/main.wasm setdev: $(eval export TAGS=-tags "dev") @@ -32,3 +32,6 @@ get: vet: go vet $(TAGS) ./... GOOS=js GOARCH=wasm go vet $(TAGS) ./... + +test: + go test $(TAGS) ./... \ No newline at end of file diff --git a/chatclient.go b/chatclient.go index edd9902..244daeb 100644 --- a/chatclient.go +++ b/chatclient.go @@ -10,6 +10,12 @@ import ( "github.com/zorchenhimer/MovieNight/common" ) +var ( + regexSpoiler = regexp.MustCompile(`\|\|(.*?)\|\|`) + spoilerStart = `` + spoilerEnd = `` +) + type Client struct { name string // Display name conn *chatConnection @@ -18,6 +24,7 @@ type Client struct { CmdLevel common.CommandLevel IsColorForced bool IsNameForced bool + regexName *regexp.Regexp } //Client has a new message to broadcast @@ -49,6 +56,10 @@ func (cl *Client) NewMsg(data common.ClientData) { msg = removeDumbSpaces(msg) msg = strings.Trim(msg, " ") + // Add the spoiler tag outside of the command vs message statement + // because the /me command outputs to the messages + msg = addSpoilerTags(msg) + // Don't send zero-length messages if len(msg) == 0 { return @@ -94,7 +105,11 @@ func (cl *Client) NewMsg(data common.ClientData) { func (cl *Client) SendChatData(data common.ChatData) error { // Colorize name on chat messages if data.Type == common.DTChat { - data = replaceColorizedName(data, cl) + var err error + data = cl.replaceColorizedName(data) + if err != nil { + return fmt.Errorf("could not colorize name: %v", err) + } } cd, err := data.ToJSON() @@ -164,6 +179,24 @@ func (cl *Client) Host() string { return cl.conn.Host() } +func (cl *Client) setName(s string) error { + regex, err := regexp.Compile(fmt.Sprintf("(%s|@%s)", s, s)) + if err != nil { + return fmt.Errorf("could not compile regex: %v", err) + } + + cl.name = s + cl.regexName = regex + return nil +} + +func (cl *Client) replaceColorizedName(chatData common.ChatData) common.ChatData { + data := chatData.Data.(common.DataMessage) + data.Message = cl.regexName.ReplaceAllString(data.Message, `$1`) + chatData.Data = data + return chatData +} + var dumbSpaces = []string{ "\n", "\t", @@ -187,12 +220,6 @@ func removeDumbSpaces(msg string) string { return newMsg } -func replaceColorizedName(chatData common.ChatData, client *Client) common.ChatData { - data := chatData.Data.(common.DataMessage) - - data.Message = regexp.MustCompile(fmt.Sprintf(`(%s|@%s)`, client.name, client.name)). - ReplaceAllString(data.Message, `$1`) - - chatData.Data = data - return chatData +func addSpoilerTags(msg string) string { + return regexSpoiler.ReplaceAllString(msg, fmt.Sprintf(`%s$1%s`, spoilerStart, spoilerEnd)) } diff --git a/chatclient_test.go b/chatclient_test.go new file mode 100644 index 0000000..d757cd1 --- /dev/null +++ b/chatclient_test.go @@ -0,0 +1,23 @@ +package main + +import "testing" + +func TestClient_addSpoilerTag(t *testing.T) { + data := [][]string{ + {"||||", spoilerStart + spoilerEnd}, + {"|||||", spoilerStart + spoilerEnd + "|"}, + {"||||||", spoilerStart + spoilerEnd + "||"}, + {"|||||||", spoilerStart + spoilerEnd + "|||"}, + {"||||||||", spoilerStart + spoilerEnd + spoilerStart + spoilerEnd}, + {"||test||", spoilerStart + "test" + spoilerEnd}, + {"|| ||", spoilerStart + " " + spoilerEnd}, + {"|s|||", "|s|||"}, + } + + for i := range data { + s := addSpoilerTags(data[i][0]) + if s != data[i][1] { + t.Errorf("expected %#v, got %#v with %#v", data[i][1], s, data[i][0]) + } + } +} diff --git a/chatroom.go b/chatroom.go index aa720af..aebde3e 100644 --- a/chatroom.go +++ b/chatroom.go @@ -98,12 +98,16 @@ func (cr *ChatRoom) Join(name, uid string) (*Client, error) { conn.clientName = name client := &Client{ - name: name, conn: conn, belongsTo: cr, color: common.RandomColor(), } + err := client.setName(name) + if err != nil { + return nil, fmt.Errorf("could not set client name to %#v: %v", name, err) + } + host := client.Host() if banned, names := settings.IsBanned(host); banned { @@ -483,7 +487,10 @@ func (cr *ChatRoom) changeName(oldName, newName string, forced bool) error { } if currentClient != nil { - currentClient.name = newName + err := currentClient.setName(newName) + if err != nil { + return fmt.Errorf("could not set client name to %#v: %v", newName, err) + } common.LogDebugf("%q -> %q\n", oldName, newName) if forced { diff --git a/common/colors.go b/common/colors.go index 32890b0..57fb7b1 100644 --- a/common/colors.go +++ b/common/colors.go @@ -42,6 +42,10 @@ var colors = []string{ "whitesmoke", "yellow", "yellowgreen", } +var ( + regexColor = regexp.MustCompile(`^#([0-9A-Fa-f]{3}){1,2}$`) +) + // IsValidColor takes a string s and compares it against a list of css color names. // It also accepts hex codes in the form of #000 (RGB), to #00000000 (RRGGBBAA), with A // being the alpha value @@ -53,7 +57,7 @@ func IsValidColor(s string) bool { } } - if regexp.MustCompile(`^#([0-9A-Fa-f]{3}){1,2}$`).MatchString(s) { + if regexColor.MatchString(s) { c, err := colorful.Hex(s) if err != nil { return false diff --git a/common/logging.go b/common/logging.go index 8bbd5c1..01bf824 100644 --- a/common/logging.go +++ b/common/logging.go @@ -23,7 +23,6 @@ const ( logPrefixChat string = "[CHAT] " logPrefixInfo string = "[INFO] " logPrefixDebug string = "[DEBUG] " - logPrefixDev string = "[DEV] " ) var ( @@ -31,18 +30,8 @@ var ( logChat *log.Logger logInfo *log.Logger logDebug *log.Logger - logDev *log.Logger ) -//func ParseLogLevel(input string) LogLevel { -// switch LogLevel(input) { -// case LLError, LLChat, LLInfo, LLDebug: -// return LogLevel(input) -// default: -// return LLError -// } -//} - func SetupLogging(level LogLevel, file string) error { switch level { case LLDebug: diff --git a/common/logging_dev.go b/common/logging_dev.go index 32c9646..1539835 100644 --- a/common/logging_dev.go +++ b/common/logging_dev.go @@ -2,18 +2,21 @@ package common -func LogDevf(format string, v ...interface{}) { - if logError == nil { - panic("Logging not setup!") - } +import ( + "log" + "os" +) - logError.Printf(format, v...) +var logDev *log.Logger + +func init() { + logDev = log.New(os.Stdout, "[DEV]", log.LstdFlags) +} + +func LogDevf(format string, v ...interface{}) { + logDev.Printf(format, v...) } func LogDevln(v ...interface{}) { - if logError == nil { - panic("Logging not setup!") - } - - logError.Println(v...) + logDev.Println(v...) } diff --git a/settings.go b/settings.go index dc47eb5..dccd9fb 100644 --- a/settings.go +++ b/settings.go @@ -49,7 +49,7 @@ func init() { panic("Missing stream key is settings.json") } - if err = common.SetupLogging(settings.LogLevel, settings.LogFile); err != nil { + if err = settings.SetupLogging(); err != nil { panic("Unable to setup logger: " + err.Error()) } @@ -184,3 +184,7 @@ func (s *Settings) GetStreamKey() string { } return s.StreamKey } + +func (s *Settings) SetupLogging() error { + return common.SetupLogging(s.LogLevel, s.LogFile) +} diff --git a/static/css/site.css b/static/css/site.css index c708c98..7b53c86 100644 --- a/static/css/site.css +++ b/static/css/site.css @@ -116,6 +116,22 @@ span.svmsg { color: var(--var-contrast-color); } +.spoiler { + border-radius: 3px; + padding: 0px 3px; +} + +.spoiler *, +.spoiler { + background: var(--var-popout-color); + color: var(--var-popout-color); +} + +.spoiler-active { + background: var(--var-background-color); + color: aqua; +} + .range-div { margin-bottom: 5px; display: flex; From f60bd3301137618b2f9c14c132197bf0d478e0d6 Mon Sep 17 00:00:00 2001 From: joeyak Date: Sun, 24 Mar 2019 23:54:24 -0400 Subject: [PATCH 10/13] Remove inits since it is startup code --- common/logging_dev.go | 6 +----- main.go | 29 +++++++++++++++++++++++------ settings.go | 20 -------------------- 3 files changed, 24 insertions(+), 31 deletions(-) diff --git a/common/logging_dev.go b/common/logging_dev.go index 1539835..9dccfc1 100644 --- a/common/logging_dev.go +++ b/common/logging_dev.go @@ -7,11 +7,7 @@ import ( "os" ) -var logDev *log.Logger - -func init() { - logDev = log.New(os.Stdout, "[DEV]", log.LstdFlags) -} +var logDev *log.Logger = log.New(os.Stdout, "[DEV]", log.LstdFlags) func LogDevf(format string, v ...interface{}) { logDev.Printf(format, v...) diff --git a/main.go b/main.go index 6cdee49..ebca812 100644 --- a/main.go +++ b/main.go @@ -14,21 +14,38 @@ import ( var ( addr string sKey string - stats streamStats + stats = newStreamStats() ) -func init() { - format.RegisterAll() +func setupSettings() { + var err error + settings, err = LoadSettings("settings.json") + if err != nil { + panic("Unable to load settings: " + err.Error()) + } + if len(settings.StreamKey) == 0 { + panic("Missing stream key is settings.json") + } - flag.StringVar(&addr, "l", ":8089", "host:port of the MovieNight") - flag.StringVar(&sKey, "k", "", "Stream key, to protect your stream") + if err = settings.SetupLogging(); err != nil { + panic("Unable to setup logger: " + err.Error()) + } - stats = newStreamStats() + // Save admin password to file + if err = settings.Save(); err != nil { + panic("Unable to save settings: " + err.Error()) + } } func main() { + flag.StringVar(&addr, "l", ":8089", "host:port of the MovieNight") + flag.StringVar(&sKey, "k", "", "Stream key, to protect your stream") flag.Parse() + format.RegisterAll() + + setupSettings() + exit := make(chan bool) go handleInterrupt(exit) diff --git a/settings.go b/settings.go index dccd9fb..278c523 100644 --- a/settings.go +++ b/settings.go @@ -39,26 +39,6 @@ type BanInfo struct { When time.Time } -func init() { - var err error - settings, err = LoadSettings("settings.json") - if err != nil { - panic("Unable to load settings: " + err.Error()) - } - if len(settings.StreamKey) == 0 { - panic("Missing stream key is settings.json") - } - - if err = settings.SetupLogging(); err != nil { - panic("Unable to setup logger: " + err.Error()) - } - - // Save admin password to file - if err = settings.Save(); err != nil { - panic("Unable to save settings: " + err.Error()) - } -} - func LoadSettings(filename string) (*Settings, error) { raw, err := ioutil.ReadFile(filename) if err != nil { From 24b5152c38671a43cce934692d708d85aed9d03d Mon Sep 17 00:00:00 2001 From: joeyak Date: Mon, 25 Mar 2019 23:32:44 -0400 Subject: [PATCH 11/13] Moving suggestion code to a separate file in prep for issue #9 --- wasm/main.go | 146 +----------------------------------------- wasm/suggestions.go | 152 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 153 insertions(+), 145 deletions(-) create mode 100644 wasm/suggestions.go diff --git a/wasm/main.go b/wasm/main.go index f253ead..dab4572 100644 --- a/wasm/main.go +++ b/wasm/main.go @@ -10,151 +10,7 @@ import ( "github.com/zorchenhimer/MovieNight/common" ) -const ( - keyTab = 9 - keyEnter = 13 - keyUp = 38 - keyDown = 40 -) - -var ( - currentName string - auth common.CommandLevel - names []string - filteredNames []string -) - -// The returned value is a bool deciding to prevent the event from propagating -func processMessageKey(this js.Value, v []js.Value) interface{} { - if len(filteredNames) == 0 || currentName == "" { - return false - } - - startIdx := v[0].Get("target").Get("selectionStart").Int() - keyCode := v[0].Get("keyCode").Int() - switch keyCode { - case keyUp, keyDown: - newidx := 0 - for i, n := range filteredNames { - if n == currentName { - newidx = i - if keyCode == keyDown { - newidx = i + 1 - if newidx == len(filteredNames) { - newidx-- - } - } else if keyCode == keyUp { - newidx = i - 1 - if newidx < 0 { - newidx = 0 - } - } - break - } - } - currentName = filteredNames[newidx] - case keyTab, keyEnter: - msg := js.Get("msg") - val := msg.Get("value").String() - newval := val[:startIdx] - - if i := strings.LastIndex(newval, "@"); i != -1 { - newval = newval[:i+1] + currentName - } - - endVal := val[startIdx:] - if len(val) == startIdx || val[startIdx:][0] != ' ' { - // insert a space into val so selection indexing can be one line - endVal = " " + endVal - } - msg.Set("value", newval+endVal) - msg.Set("selectionStart", len(newval)+1) - msg.Set("selectionEnd", len(newval)+1) - - // Clear out filtered names since it is no longer needed - filteredNames = nil - default: - // We only want to handle the caught keys, so return early - return false - } - - updateSuggestionDiv() - return true -} - -func processMessage(v []js.Value) { - msg := js.Get("msg") - text := strings.ToLower(msg.Get("value").String()) - startIdx := msg.Get("selectionStart").Int() - - filteredNames = nil - if len(text) != 0 { - if len(names) > 0 { - var caretIdx int - textParts := strings.Split(text, " ") - - for i, word := range textParts { - // Increase caret index at beginning if not first word to account for spaces - if i != 0 { - caretIdx++ - } - - // It is possible to have a double space " ", which will lead to an - // empty string element in the slice. Also check that the index of the - // cursor is between the start of the word and the end - if len(word) > 0 && word[0] == '@' && - caretIdx <= startIdx && startIdx <= caretIdx+len(word) { - // fill filtered first so the "modifier" keys can modify it - for _, n := range names { - if len(word) == 1 || strings.HasPrefix(strings.ToLower(n), word[1:]) { - filteredNames = append(filteredNames, n) - } - } - } - - if len(filteredNames) > 0 { - currentName = "" - break - } - - caretIdx += len(word) - } - } - } - - updateSuggestionDiv() -} - -func updateSuggestionDiv() { - const selectedClass = ` class="selectedName"` - - var divs []string - if len(filteredNames) > 0 { - // set current name to first if not set already - if currentName == "" { - currentName = filteredNames[0] - } - - var hasCurrentName bool - divs = make([]string, len(filteredNames)) - - // Create inner body of html - for i := range filteredNames { - divs[i] = "" - } - - if !hasCurrentName { - divs[0] = divs[0][:4] + selectedClass + divs[0][4:] - } - } - // The \n is so it's easier to read th source in web browsers for the dev - js.Get("suggestions").Set("innerHTML", strings.Join(divs, "\n")) -} +var auth common.CommandLevel func recieve(v []js.Value) { if len(v) == 0 { diff --git a/wasm/suggestions.go b/wasm/suggestions.go new file mode 100644 index 0000000..8757370 --- /dev/null +++ b/wasm/suggestions.go @@ -0,0 +1,152 @@ +package main + +import ( + "strings" + + "github.com/dennwc/dom/js" +) + +const ( + keyTab = 9 + keyEnter = 13 + keyUp = 38 + keyDown = 40 +) + +var ( + currentName string + names []string + filteredNames []string +) + +// The returned value is a bool deciding to prevent the event from propagating +func processMessageKey(this js.Value, v []js.Value) interface{} { + if len(filteredNames) == 0 || currentName == "" { + return false + } + + startIdx := v[0].Get("target").Get("selectionStart").Int() + keyCode := v[0].Get("keyCode").Int() + switch keyCode { + case keyUp, keyDown: + newidx := 0 + for i, n := range filteredNames { + if n == currentName { + newidx = i + if keyCode == keyDown { + newidx = i + 1 + if newidx == len(filteredNames) { + newidx-- + } + } else if keyCode == keyUp { + newidx = i - 1 + if newidx < 0 { + newidx = 0 + } + } + break + } + } + currentName = filteredNames[newidx] + case keyTab, keyEnter: + msg := js.Get("msg") + val := msg.Get("value").String() + newval := val[:startIdx] + + if i := strings.LastIndex(newval, "@"); i != -1 { + newval = newval[:i+1] + currentName + } + + endVal := val[startIdx:] + if len(val) == startIdx || val[startIdx:][0] != ' ' { + // insert a space into val so selection indexing can be one line + endVal = " " + endVal + } + msg.Set("value", newval+endVal) + msg.Set("selectionStart", len(newval)+1) + msg.Set("selectionEnd", len(newval)+1) + + // Clear out filtered names since it is no longer needed + filteredNames = nil + default: + // We only want to handle the caught keys, so return early + return false + } + + updateSuggestionDiv() + return true +} + +func processMessage(v []js.Value) { + msg := js.Get("msg") + text := strings.ToLower(msg.Get("value").String()) + startIdx := msg.Get("selectionStart").Int() + + filteredNames = nil + if len(text) != 0 { + if len(names) > 0 { + var caretIdx int + textParts := strings.Split(text, " ") + + for i, word := range textParts { + // Increase caret index at beginning if not first word to account for spaces + if i != 0 { + caretIdx++ + } + + // It is possible to have a double space " ", which will lead to an + // empty string element in the slice. Also check that the index of the + // cursor is between the start of the word and the end + if len(word) > 0 && word[0] == '@' && + caretIdx <= startIdx && startIdx <= caretIdx+len(word) { + // fill filtered first so the "modifier" keys can modify it + for _, n := range names { + if len(word) == 1 || strings.HasPrefix(strings.ToLower(n), word[1:]) { + filteredNames = append(filteredNames, n) + } + } + } + + if len(filteredNames) > 0 { + currentName = "" + break + } + + caretIdx += len(word) + } + } + } + + updateSuggestionDiv() +} + +func updateSuggestionDiv() { + const selectedClass = ` class="selectedName"` + + var divs []string + if len(filteredNames) > 0 { + // set current name to first if not set already + if currentName == "" { + currentName = filteredNames[0] + } + + var hasCurrentName bool + divs = make([]string, len(filteredNames)) + + // Create inner body of html + for i := range filteredNames { + divs[i] = "" + } + + if !hasCurrentName { + divs[0] = divs[0][:4] + selectedClass + divs[0][4:] + } + } + // The \n is so it's easier to read th source in web browsers for the dev + js.Get("suggestions").Set("innerHTML", strings.Join(divs, "\n")) +} From 6018b67c25f1490bc13490e017578cb8d1dde825 Mon Sep 17 00:00:00 2001 From: Zorchenhimer Date: Tue, 26 Mar 2019 11:43:09 -0400 Subject: [PATCH 12/13] Add tags file to .gitignore Add the Vim tags file to the .gitignore. It's an auto-generated file that doesn't need to be in git. --- .gitignore | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.gitignore b/.gitignore index 48df202..d387c94 100644 --- a/.gitignore +++ b/.gitignore @@ -32,3 +32,6 @@ settings.json # Autobuilt wasm files static/main.wasm + +# tags for vim +tags From d2b8f3d7be0bb86c08e64c22147b4b77d32c98e4 Mon Sep 17 00:00:00 2001 From: Zorchenhimer Date: Tue, 26 Mar 2019 11:44:19 -0400 Subject: [PATCH 13/13] Remove panic()'s from setupSettings() Now that this is in a called function, instead of an init(), return an error instead of panicking. --- main.go | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/main.go b/main.go index ebca812..aeada1a 100644 --- a/main.go +++ b/main.go @@ -17,24 +17,26 @@ var ( stats = newStreamStats() ) -func setupSettings() { +func setupSettings() error { var err error settings, err = LoadSettings("settings.json") if err != nil { - panic("Unable to load settings: " + err.Error()) + return fmt.Errorf("Unable to load settings: %s", err) } if len(settings.StreamKey) == 0 { - panic("Missing stream key is settings.json") + return fmt.Errorf("Missing stream key is settings.json") } if err = settings.SetupLogging(); err != nil { - panic("Unable to setup logger: " + err.Error()) + return fmt.Errorf("Unable to setup logger: %s", err) } // Save admin password to file if err = settings.Save(); err != nil { - panic("Unable to save settings: " + err.Error()) + return fmt.Errorf("Unable to save settings: %s", err) } + + return nil } func main() { @@ -44,7 +46,10 @@ func main() { format.RegisterAll() - setupSettings() + if err := setupSettings(); err != nil { + fmt.Printf("Error loading settings: %v\n", err) + os.Exit(1) + } exit := make(chan bool) go handleInterrupt(exit)