From e2dc5b6fbb6adf6f379ef0123c214a196211c305 Mon Sep 17 00:00:00 2001 From: citrons Date: Sun, 1 Jun 2025 15:38:17 -0500 Subject: joining channels --- client/command.go | 78 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 78 insertions(+) create mode 100644 client/command.go (limited to 'client/command.go') diff --git a/client/command.go b/client/command.go new file mode 100644 index 0000000..f505f9a --- /dev/null +++ b/client/command.go @@ -0,0 +1,78 @@ +package main + +func isCommand(text string) (bool, string) { + if text[0] == '/' { + if len(text) > 1 && text[1] == '/' { + text = text[1:] + return false, text + } + return true, text + } + return false, text +} + +func (a *application) processCommand(text string) { + text = text[1:] + args := []string {""} + escaped := false + quoted := false + for _, c := range text { + if escaped { + args[len(args) - 1] += string(c) + continue + } + switch c { + case '\\': + escaped = true + case '"': + quoted = !quoted + case ' ': + if !quoted { + if args[len(args) - 1] != "" { + args = append(args, "") + } + break + } + fallthrough + default: + args[len(args) - 1] += string(c) + } + } + if len(text) > len(args[0]) { + text = text[len(args[0]) + 1:] + } else { + text = "" + } + a.doCommand(args[0], args[1:], text) +} + +func (a *application) doCommand(command string, args []string, text string) { + if !a.connected { + return + } + argN := func(n int) { + if len(args) != n { + a.cmdWindow.err( + "%s: expected %d arguments, was %d", command, n, len(args), + ) + } + } + switch command { + case "nick": + a.setNick(text) + case "join": + argN(1) + if a.authenticated { + a.join(args[0]) + } + case "create": + argN(1) + if a.authenticated { + a.createChannel(args[0]) + } + case "quit": + a.quit = true + default: + a.cmdWindow.err("unknown command: /" + command) + } +} -- cgit v1.2.3