Merge pull request #46 from MikeIsAStar/do-not-consider-a-string-of-length-zero-as-profane

[NAS] Do not consider a string of length zero as profane
This commit is contained in:
Palapeli 2024-03-09 06:01:07 -05:00 committed by GitHub
commit 8ffcdcec5f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 29 additions and 13 deletions

View File

@ -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)

View File

@ -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
}

View File

@ -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)))

View File

@ -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) {