working
This commit is contained in:
59
pkg/protocol/commands.go
Normal file
59
pkg/protocol/commands.go
Normal 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)
|
||||
}
|
||||
Reference in New Issue
Block a user