-
Notifications
You must be signed in to change notification settings - Fork 3
/
subcommands.go
118 lines (106 loc) · 2.7 KB
/
subcommands.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
117
118
package helpers
import (
"fmt"
"strings"
tb "gopkg.in/telebot.v3"
)
type handlerFunc func(m *tb.Message, params ...string)
type SubcommandHandler struct {
base string
quotedSingleParams bool
mappings map[string]handlerFunc
}
func MakeSubcommandHandler(base string, quotedSingleParams bool) *SubcommandHandler {
return &SubcommandHandler{
base: base,
mappings: make(map[string]handlerFunc),
quotedSingleParams: quotedSingleParams,
}
}
func (sh *SubcommandHandler) Add(command string, handler handlerFunc) *SubcommandHandler {
if strings.Contains(command, " ") {
LogLocalf(WARN, nil, "Subcommand '%s' contains a space. This most probably won't work with space (' ') separator", command)
}
_, exists := sh.mappings[command]
if exists {
LogLocalf(WARN, nil, "Subcommand '%s' already exists. Performing remapping! Please check whether this is desired behavior.", command)
}
sh.mappings[command] = handler
return sh
}
func (sh *SubcommandHandler) Handle(m *tb.Message) ([]string, error) {
commandRemainder := strings.TrimSpace(strings.TrimPrefix(m.Text, sh.base))
parameters := strings.Split(commandRemainder, " ")
var subcommand string
if len(parameters) > 0 {
subcommand = parameters[0]
}
fn, exists := sh.mappings[subcommand]
if !exists {
return parameters, fmt.Errorf("subcommand '%s' has not been mapped with this SubcommandHandler(%s)", subcommand, sh.base)
}
if len(parameters) <= 1 {
fn(m)
return parameters, nil
}
remainingCommand := strings.TrimSpace(strings.TrimPrefix(commandRemainder, subcommand))
parameters = SplitQuotedCommand(remainingCommand)
if ArraysEqual(parameters, []string{}) {
return parameters, fmt.Errorf("this remainingCommand could not be split: '%s'", remainingCommand)
}
fn(m, parameters...)
return parameters, nil
}
type TV struct {
T string
Value string
}
func ExtractTypeValue(params ...string) (*TV, error) {
e := &TV{}
if len(params) < 1 || len(params) > 2 {
return nil, fmt.Errorf("unexpected count of parameters")
}
if len(params) >= 1 {
e.T = params[0]
}
if len(params) >= 2 {
e.Value = params[1]
}
return e, nil
}
func SplitQuotedCommand(s string) (res []string) {
isEscaped := false
isQuoted := false
split := ""
for _, c := range s {
if isEscaped {
isEscaped = false
split += string(c)
continue
}
if c == '\\' {
isEscaped = true
continue
}
if c == '"' || c == '“' {
isQuoted = !isQuoted
continue
}
if c == ' ' && !isQuoted {
if split == "" {
continue
}
res = append(res, split)
split = ""
continue
}
split += string(c)
}
if split != "" {
res = append(res, split)
}
if isEscaped || isQuoted {
return []string{}
}
return
}