From 4dd85ddae4a9a9405310a837ffe1a932748017c1 Mon Sep 17 00:00:00 2001 From: MikeIsAStar <99037623+MikeIsAStar@users.noreply.github.com> Date: Fri, 23 Feb 2024 02:00:00 -0500 Subject: [PATCH] [NAS] Do not consider a string of length zero as profane --- gpcm/login.go | 4 +++- gpcm/motd.go | 6 +----- nas/main.go | 5 ++++- nas/profanity.go | 27 +++++++++++++++++++++------ 4 files changed, 29 insertions(+), 13 deletions(-) diff --git a/gpcm/login.go b/gpcm/login.go index 0023dd6..e429732 100644 --- a/gpcm/login.go +++ b/gpcm/login.go @@ -315,7 +315,9 @@ func (g *GameSpySession) login(command common.GameSpyCommand) { } if g.GameName == "mariokartwii" { - if motd, err := GetMessageOfTheDay(); err == nil { + if motd, err := GetMessageOfTheDay(); err != nil { + logging.Info(g.ModuleName, err) + } else { motdUTF16 := utf16.Encode([]rune(motd)) motdByteArray := common.UTF16ToByteArray(motdUTF16) otherValues["wwfc_motd"] = common.Base64DwcEncoding.EncodeToString(motdByteArray) diff --git a/gpcm/motd.go b/gpcm/motd.go index 3a0c657..b0b6c04 100644 --- a/gpcm/motd.go +++ b/gpcm/motd.go @@ -2,7 +2,6 @@ package gpcm import ( "os" - "strings" ) var motdFilepath = "./motd.txt" @@ -13,8 +12,5 @@ func GetMessageOfTheDay() (string, error) { return "", err } - strContents := string(contents) - strContents = strings.TrimSpace(strContents) - - return strContents, nil + return string(contents), nil } diff --git a/nas/main.go b/nas/main.go index e2f03fe..30d7228 100644 --- a/nas/main.go +++ b/nas/main.go @@ -49,7 +49,10 @@ func StartServer() { go startHTTPSProxy(config) } - CacheProfanityFile() + err = CacheProfanityFile() + if err != nil { + logging.Info("NAS", err) + } logging.Notice("NAS", "Starting HTTP server on", address) panic(nhttp.ListenAndServe(address, http.HandlerFunc(handleRequest))) diff --git a/nas/profanity.go b/nas/profanity.go index 587bee1..ce425a9 100644 --- a/nas/profanity.go +++ b/nas/profanity.go @@ -1,6 +1,7 @@ package nas import ( + "bufio" "errors" "os" "strings" @@ -9,15 +10,29 @@ import ( var profanityFilePath = "./profanity.txt" var profanityFileLines []string = nil -func CacheProfanityFile() bool { - contents, err := os.ReadFile(profanityFilePath) +func CacheProfanityFile() error { + file, err := os.Open(profanityFilePath) if err != nil { - return false + return err + } + defer file.Close() + + scanner := bufio.NewScanner(file) + for scanner.Scan() { + line := scanner.Text() + + if line == "" { + continue + } + + profanityFileLines = append(profanityFileLines, line) } - lines := strings.Split(string(contents), "\n") - profanityFileLines = lines - return true + if profanityFileLines == nil { + return errors.New("the file '" + profanityFilePath + "' is empty") + } + + return nil } func IsBadWord(word string) (bool, error) {