diff options
| -rw-r--r-- | server/chat.go | 77 |
1 files changed, 41 insertions, 36 deletions
diff --git a/server/chat.go b/server/chat.go index d91ca55..4206d66 100644 --- a/server/chat.go +++ b/server/chat.go @@ -13,42 +13,9 @@ func processChatMessage(message string) []classic.Packet { word strings.Builder color byte = 'f' ) - for in.Len() > 0 { - for { - b, err := in.ReadByte() - if b > 127 { - b = '?' - } - if err == nil { - if b == '&' || b == '%' { - if word.Len() + 1 >= 64 { - line.WriteString(word.String()) - word.Reset() - } - next, err := in.ReadByte() - in.UnreadByte() - if err != nil { - word.WriteString("& ") - } else if (next >= '0' && next <= '9') || - (next >= 'a' && next <= 'f') { - b = '&' - color = next - } - } - word.WriteByte(b) - } - if line.Len() + word.Len() > 64 { - break - } - if err != nil { - line.WriteString(word.String()) - word.Reset() - break - } - if word.Len() >= 64 || b == ' ' { - line.WriteString(word.String()) - word.Reset() - } + endLine := func() { + if line.Len() == 0 { + return } packet := &classic.Message { Message: classic.PadString(line.String()), @@ -57,5 +24,43 @@ func processChatMessage(message string) []classic.Packet { line.Reset() line.WriteString("&7> &" + string([]byte{color})) } + endWord := func() { + if line.Len() + word.Len() > 64 { + endLine() + } + line.WriteString(word.String()) + word.Reset() + } + for in.Len() > 0 { + b, _ := in.ReadByte() + if b == '%' { + b = '&' + } + if b == '&' { + endWord() + word.WriteByte(b) + b, _ = in.ReadByte() + if (b >= '0' && b <= '9') || (b >= 'a' && b <= 'f') { + color = b + } + word.WriteByte(b) + endWord() + continue + } + if b == ' ' { + endWord() + if line.Len() > 0 { + word.WriteByte(b) + endWord() + } + continue + } + word.WriteByte(b) + if word.Len() >= 58 { + endWord() + } + } + endWord() + endLine() return packets } |
