summaryrefslogtreecommitdiff
path: root/server/chat.go
blob: d91ca55316ca0ec51bce8e16f83a186856c44cc1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package server

import (
	"strings"
	"git.citrons.xyz/metronode/classic"
)

func processChatMessage(message string) []classic.Packet {
	var (
		packets []classic.Packet
		in = strings.NewReader(message)
		line strings.Builder
		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()
			}
		}
		packet := &classic.Message {
			Message: classic.PadString(line.String()),
		}
		packets = append(packets, packet)
		line.Reset()
		line.WriteString("&7> &" + string([]byte{color}))
	}
	return packets
}