commit 777b0ac
Hubert Hirtz
·
2021-05-20 19:04:17 +0000 UTC
parent 7835d39
Separate command handling from plain messages - So that lone slashes can be recognised - Also handle two slashes and pass them as one in plain input
1 files changed,
+24,
-23
+24,
-23
1@@ -24,11 +24,6 @@ var commands commandSet
2
3 func init() {
4 commands = commandSet{
5- "": {
6- MinArgs: 1,
7- MaxArgs: 1,
8- Handle: commandDo,
9- },
10 "HELP": {
11 AllowHome: true,
12 MaxArgs: 1,
13@@ -127,20 +122,19 @@ func init() {
14 }
15 }
16
17-func commandDo(app *App, buffer string, args []string) (err error) {
18- app.s.PrivMsg(buffer, args[0])
19+func noCommand(app *App, buffer, content string) {
20+ app.s.PrivMsg(buffer, content)
21 if !app.s.HasCapability("echo-message") {
22 buffer, line, _ := app.formatMessage(irc.MessageEvent{
23 User: app.s.Nick(),
24 Target: buffer,
25 TargetIsChannel: true,
26 Command: "PRIVMSG",
27- Content: args[0],
28+ Content: content,
29 Time: time.Now(),
30 })
31 app.win.AddLine(buffer, false, line)
32 }
33- return
34 }
35
36 func commandDoHelp(app *App, buffer string, args []string) (err error) {
37@@ -360,14 +354,17 @@ func commandDoTopic(app *App, buffer string, args []string) (err error) {
38 return
39 }
40
41-func parseCommand(s string) (command, args string) {
42+func parseCommand(s string) (command, args string, isCommand bool) {
43 if s == "" {
44- return
45+ return "", "", false
46 }
47
48 if s[0] != '/' {
49- args = s
50- return
51+ return "", s, false
52+ }
53+ if s[1] == '/' {
54+ // Input starts with two slashes.
55+ return "", s[1:], false
56 }
57
58 i := strings.IndexByte(s, ' ')
59@@ -375,35 +372,39 @@ func parseCommand(s string) (command, args string) {
60 i = len(s)
61 }
62
63+ isCommand = true
64 command = strings.ToUpper(s[1:i])
65 args = strings.TrimLeft(s[i:], " ")
66-
67 return
68 }
69
70 func (app *App) handleInput(buffer, content string) error {
71- cmdName, rawArgs := parseCommand(content)
72-
73 if content == "" {
74 return nil
75 }
76
77+ cmdName, rawArgs, isCommand := parseCommand(content)
78+ if !isCommand {
79+ noCommand(app, buffer, content)
80+ return nil
81+ }
82+ if cmdName == "" {
83+ return fmt.Errorf("lone slash at the begining")
84+ }
85+
86 var chosenCMDName string
87- var ok bool
88+ var found bool
89 for key := range commands {
90- if cmdName == "" && key != "" {
91- continue
92- }
93 if !strings.HasPrefix(key, cmdName) {
94 continue
95 }
96- if ok {
97+ if found {
98 return fmt.Errorf("ambiguous command %q (could mean %v or %v)", cmdName, chosenCMDName, key)
99 }
100 chosenCMDName = key
101- ok = true
102+ found = true
103 }
104- if !ok {
105+ if !found {
106 return fmt.Errorf("command %q doesn't exist", cmdName)
107 }
108