From 7ac34c7d05e650d6cf7c2a65fa38bc0dc0421f52 Mon Sep 17 00:00:00 2001 From: Zorchenhimer Date: Sun, 22 Sep 2019 15:42:48 -0400 Subject: [PATCH] Fix name highlighting breaking emotes Fix name highlighting when emotes are involved. When the emotes were moved to subfolders the channel name was put into the URL of the emote image. If a user with that name joined the chat, all of the emotes in that folder highlighted the name in the URL, breaking the tag. This change removes the regex used to identify and replace the user's name and instead works on whole words delimited by spaces. --- chatclient.go | 24 +++++++++++++++--------- chatclient_test.go | 34 +++++++++++++++++++++++++++++++++- common/chatcommands.go | 2 -- 3 files changed, 48 insertions(+), 12 deletions(-) diff --git a/chatclient.go b/chatclient.go index 5516966..b843859 100644 --- a/chatclient.go +++ b/chatclient.go @@ -255,15 +255,10 @@ func (cl *Client) Host() string { } func (cl *Client) setName(s string) error { - // Case-insensitive search. Match whole words only (`\b` is word boundary). - regex, err := regexp.Compile(fmt.Sprintf(`(?i)\b(%s|@%s)\b`, s, s)) - if err != nil { - return fmt.Errorf("could not compile regex: %v", err) - } - cl.name = s - cl.regexName = regex - cl.conn.clientName = s + if cl.conn != nil { + cl.conn.clientName = s + } return nil } @@ -274,7 +269,18 @@ func (cl *Client) setColor(s string) error { func (cl *Client) replaceColorizedName(chatData common.ChatData) common.ChatData { data := chatData.Data.(common.DataMessage) - data.Message = cl.regexName.ReplaceAllString(data.Message, `$1`) + words := strings.Split(data.Message, " ") + newWords := []string{} + + for _, word := range words { + if strings.ToLower(word) == strings.ToLower(cl.name) || strings.ToLower(word) == strings.ToLower("@"+cl.name) { + newWords = append(newWords, ``+word+``) + } else { + newWords = append(newWords, word) + } + } + + data.Message = strings.Join(newWords, " ") chatData.Data = data return chatData } diff --git a/chatclient_test.go b/chatclient_test.go index d757cd1..d7dce99 100644 --- a/chatclient_test.go +++ b/chatclient_test.go @@ -1,6 +1,10 @@ package main -import "testing" +import ( + "testing" + + "github.com/zorchenhimer/MovieNight/common" +) func TestClient_addSpoilerTag(t *testing.T) { data := [][]string{ @@ -21,3 +25,31 @@ func TestClient_addSpoilerTag(t *testing.T) { } } } + +// Name highlighting should not interfere with emotes +func TestClient_emoteHighlight(t *testing.T) { + data := [][]string{ + {"zorchenhimer", `zorchenhimer`}, + {"@zorchenhimer", `@zorchenhimer`}, + {"Zorchenhimer", `Zorchenhimer`}, + {"@Zorchenhimer", `@Zorchenhimer`}, + {"hello zorchenhimer", `hello zorchenhimer`}, + {"hello zorchenhimer ass", `hello zorchenhimer ass`}, + {``, ``}, + {`zorchenhimer `, `zorchenhimer `}, + } + + client, err := NewClient(nil, nil, "Zorchenhimer", "#9547ff") + if err != nil { + t.Errorf("Client init error: %v", err) + } + + for _, d := range data { + chatData := client.replaceColorizedName(common.NewChatMessage(client.name, client.color, d[0], common.CmdlUser, common.MsgChat)) + if chatData.Data.(common.DataMessage).Message != d[1] { + t.Errorf("\nExpected:\n\t%s\nReceived\n\t%s", d[1], chatData.Data.(common.DataMessage).Message) + } else { + t.Logf("Passed %s", d[0]) + } + } +} diff --git a/common/chatcommands.go b/common/chatcommands.go index d86433f..84226bf 100644 --- a/common/chatcommands.go +++ b/common/chatcommands.go @@ -54,7 +54,6 @@ var ChatCommands = []ChatCommandNames{ CNStats, // Mod - CNSv, CNPlaying, CNUnmod, CNKick, CNBan, CNUnban, CNPurge, CNPin, CNSv, CNPlaying, CNUnmod, @@ -64,7 +63,6 @@ var ChatCommands = []ChatCommandNames{ CNPurge, // Admin - CNMod, CNReloadPlayer, CNReloadEmotes, CNModpass, CNRoomAccess, CNIP, CNMod, CNReloadPlayer, CNReloadEmotes,