diff options
| author | citrons <citrons@mondecitronne.com> | 2025-06-01 15:38:17 -0500 |
|---|---|---|
| committer | citrons <citrons@mondecitronne.com> | 2025-06-01 15:38:17 -0500 |
| commit | e2dc5b6fbb6adf6f379ef0123c214a196211c305 (patch) | |
| tree | cc976665dd2e0916af100b5c0f7f629cc1936b01 /client/command.go | |
| parent | e740e5478a43358fbbd79636483d000e01f88b7e (diff) | |
joining channels
Diffstat (limited to 'client/command.go')
| -rw-r--r-- | client/command.go | 78 |
1 files changed, 78 insertions, 0 deletions
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) + } +} |
