-
Notifications
You must be signed in to change notification settings - Fork 0
/
msgs.go
116 lines (102 loc) · 2.82 KB
/
msgs.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package cmds
import (
"strings"
"github.com/Clinet/clinet_services"
)
func CmdHandler(serviceMsg *services.Message, service services.Service) (string, []*CmdResp, error) {
if serviceMsg == nil || serviceMsg.Content == "" {
return "", nil, services.Error("no message content to parse")
}
cmdPrefix := service.CmdPrefix()
msgContent := strings.ReplaceAll(serviceMsg.Content, "\r\n", "\n")
msgs := strings.Split(msgContent, "\n")
//TODO: Loop msgs to create and handle msg
rawMsg := msgs[0]
if cmdPrefix != "" {
if len(rawMsg) <= len(cmdPrefix) {
return "", nil, nil
}
if string(rawMsg[:len(cmdPrefix)]) != cmdPrefix {
return "", nil, nil
}
rawMsg = strings.Replace(rawMsg, cmdPrefix, "", 1) //Replace only the leftmost instance of the cmdPrefix
}
msg := strings.Split(rawMsg, " ")
cmd := GetCmd(msg[0])
if cmd == nil {
return "", nil, nil
}
cmdArgs := cmd.Args
isArgs := false
if len(msg) > 1 {
for i := 1; i < len(msg); i++ {
kv := make([]string, 0)
if strings.Contains(msg[i], ":") {
isArgs = true
kv = strings.Split(msg[i], ":")
if len(kv) != 2 {
return "", nil, services.Error("cmd arg must be key:value pair")
}
} else if strings.Contains(msg[i], "=") {
isArgs = true
kv = strings.Split(msg[i], "=")
if len(kv) != 2 {
return "", nil, services.Error("cmd arg must be key=value pair")
}
} else {
if !isArgs {
cmd = cmd.GetSubCmd(msg[i])
if cmd == nil {
return "", nil, services.Error("unknown subcommand %s", msg[i])
}
} else {
return "", nil, services.Error("must specify value for %s with %s:value or %s=value", msg[i], msg[i], msg[i])
}
}
if len(kv) != 0 {
setValue := false
for j := 0; j < len(cmdArgs); j++ {
if cmdArgs[j].Name == kv[0] {
cmdArgs[j].Value = kv[1]
setValue = true
break
}
}
if !setValue {
return "", nil, services.Error("cmd arg %s does not exist", kv[0])
}
}
}
}
if len(cmd.Subcommands) > 0 {
return "", nil, services.Error("must specify one of available subcommands")
}
for i := 0; i < len(cmdArgs); i++ {
if cmdArgs[i].Required && cmdArgs[i].Value == nil {
return "", nil, services.Error("must specify cmd arg %s", cmdArgs[i].Name)
}
}
user := &services.User{
ServerID: serviceMsg.ServerID,
UserID: serviceMsg.UserID,
}
channel := &services.Channel{
ServerID: serviceMsg.ServerID,
ChannelID: serviceMsg.ChannelID,
}
server := &services.Server{
ServerID: serviceMsg.ServerID,
}
cmdCtx := NewCmdCtx().
SetAlias(cmd.Name).
SetUser(user).
SetChannel(channel).
SetServer(server).
SetMessage(serviceMsg).
SetService(service).
AddArgs(cmdArgs...)
cmdBuilder := &CmdBuilderCommand{Command: cmd, Context: cmdCtx}
cmdRuntime := CmdBatch(cmdBuilder)
cmdResps := cmdRuntime.Run()
return cmd.Name, cmdResps, nil
}