This commit is contained in:
Jose Luis Montañes Ojados
2026-01-15 16:49:16 +01:00
commit 47b8173045
23 changed files with 2864 additions and 0 deletions

59
pkg/protocol/commands.go Normal file
View File

@@ -0,0 +1,59 @@
package protocol
import (
"strings"
)
// Command Parsing Helpers
func ParseCommand(data []byte) (string, map[string]string) {
s := string(data)
parts := strings.Split(s, " ")
cmd := parts[0]
args := make(map[string]string)
for _, p := range parts[1:] {
kv := strings.SplitN(p, "=", 2)
if len(kv) == 2 {
val := Unescape(kv[1])
args[kv[0]] = val
} else {
args[p] = ""
}
}
return cmd, args
}
// Unescape TS3 string
func Unescape(s string) string {
r := strings.NewReplacer(
`\/`, `/`,
`\s`, ` `,
`\p`, `|`,
`\a`, "\a",
`\b`, "\b",
`\f`, "\f",
`\n`, "\n",
`\r`, "\r",
`\t`, "\t",
`\v`, "\v",
`\\`, `\`,
)
return r.Replace(s)
}
func Escape(s string) string {
r := strings.NewReplacer(
`\`, `\\`,
`/`, `\/`,
` `, `\s`,
`|`, `\p`,
"\a", `\a`,
"\b", `\b`,
"\f", `\f`,
"\n", `\n`,
"\r", `\r`,
"\t", `\t`,
"\v", `\v`,
)
return r.Replace(s)
}