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 "leave": argN(0) win := a.windowCache.Get(a.currentWindow) switch win.(type) { case *channelWindow: win.(*channelWindow).leaveChannel() } a.currentWindow = cmdWindowLocation {} case "create": argN(1) if a.authenticated { a.createChannel(args[0]) } case "quit": a.quit = true default: a.cmdWindow.err("unknown command: /" + command) } }