aboutsummaryrefslogtreecommitdiff
path: root/pkg/server/utils
diff options
context:
space:
mode:
authorFuwn <[email protected]>2021-07-04 02:56:14 -0700
committerFuwn <[email protected]>2021-07-04 02:56:14 -0700
commit38ddbd97122ca71777c500df101f1274b3f11900 (patch)
tree9ff64f8446646413a4832fd666d94454ad01f603 /pkg/server/utils
downloadmunch-38ddbd97122ca71777c500df101f1274b3f11900.tar.xz
munch-38ddbd97122ca71777c500df101f1274b3f11900.zip
feat(munch): :star:
Diffstat (limited to 'pkg/server/utils')
-rw-r--r--pkg/server/utils/decode.go24
-rw-r--r--pkg/server/utils/encode.go70
-rw-r--r--pkg/server/utils/hex.go31
3 files changed, 125 insertions, 0 deletions
diff --git a/pkg/server/utils/decode.go b/pkg/server/utils/decode.go
new file mode 100644
index 0000000..e819b31
--- /dev/null
+++ b/pkg/server/utils/decode.go
@@ -0,0 +1,24 @@
+// Copyright (C) 2021-2021 The Whirlsplash Collective
+// SPDX-License-Identifier: GPL-3.0-only
+
+package utils
+
+import "strconv"
+
+type Text struct {
+ Author string
+ Message string
+}
+
+// Decode a TEXT command from hexadecimal to a Text struct
+func DecodeText(data []byte) Text {
+ authorLength, _ := strconv.ParseInt(strconv.Itoa(int(data[4])), 16, 32)
+ authorLast := authorLength + 5
+ messageLength, _ := strconv.ParseInt(strconv.Itoa(int(data[authorLast])), 16, 32)
+ messageStart := authorLast + 1
+
+ return Text{
+ Author: string(data[5:(authorLast)]),
+ Message: string(data[messageStart : messageStart+(messageLength)]),
+ }
+}
diff --git a/pkg/server/utils/encode.go b/pkg/server/utils/encode.go
new file mode 100644
index 0000000..86e5500
--- /dev/null
+++ b/pkg/server/utils/encode.go
@@ -0,0 +1,70 @@
+// Copyright (C) 2021-2021 The Whirlsplash Collective
+// SPDX-License-Identifier: GPL-3.0-only
+
+package utils
+
+const (
+ AutoServer = iota
+ RoomServer
+)
+
+func EncodeSessionInitialization(username string, password string, serverType int) []byte {
+ data := ""
+
+ // Data length
+ if serverType == AutoServer {
+ data += "\x2b"
+ } else {
+ data += "\x2c"
+ }
+
+ // Command header and other stuff we don't need to worry about
+ data += "\x01\x06\x03\x02\x32\x34\x09\x0a\x32\x30\x32\x30\x30\x33\x31" +
+ "\x32\x30\x30"
+
+ if serverType == RoomServer {
+ data += "\x07\x02\x32\x34"
+ }
+
+ data +=
+ // Username
+ "\x02" + string(rune(len(username))) + ISO88591ToString(username) +
+
+ // Password
+ "\x06" + string(rune(len(password))) + ISO88591ToString(password)
+
+ if serverType == AutoServer {
+ data += "\x0c\x01\x31"
+ }
+
+ return []byte(data)
+}
+
+func EncodeBuddyListUpdate(buddy string) []byte {
+ // Command header
+ data := "\x01\x1d"
+
+ // Buddy UTF-8 length and UTF-8
+ data += string(rune(len(buddy))) + ISO88591ToString(buddy)
+
+ // Buddy "add"
+ data += "\x01"
+
+ // Data length
+ data = (string(rune(len(data) + 1))) + data
+
+ return []byte(data)
+}
+
+func EncodePropertyUpdate(avatar string) []byte {
+ // Command header and extra stuff we don't need to worry about
+ data := "\x01\x0f\x00\x05\x40\x01"
+
+ // Avatar UTF-8 length and UTF-8
+ data += string(rune(len("avatar:"+avatar))) + ISO88591ToString("avatar:"+avatar)
+
+ // Data length
+ data = (string(rune(len(data) + 1))) + data
+
+ return []byte(data)
+}
diff --git a/pkg/server/utils/hex.go b/pkg/server/utils/hex.go
new file mode 100644
index 0000000..a5393aa
--- /dev/null
+++ b/pkg/server/utils/hex.go
@@ -0,0 +1,31 @@
+package utils
+
+import "unicode/utf8"
+
+// https://stackoverflow.com/a/43461796/14452787
+//
+// I cannot explain the joy that flew through me once I found this ISO-8859-1
+// (Latin-1) to UTF-8 converter. I was looking for a valid solution on how to
+// insert the username and password into a hex-escaped string sequence for
+// EncodeSessionInitialization for hours and this finally solved all of my
+// problems.
+func ISO88591ToString(iso string) string {
+ var utf []rune
+ for i := 0; i < len(iso); i++ {
+ r := iso[i]
+ if utf == nil {
+ if r < utf8.RuneSelf {
+ continue
+ }
+ utf = make([]rune, len(iso))
+ for j, r := range iso[:i] {
+ utf[j] = rune(r)
+ }
+ }
+ utf[i] = rune(r)
+ }
+ if utf == nil {
+ return string(iso)
+ }
+ return string(utf)
+}