/
commands.go
165 lines (135 loc) · 3.61 KB
/
commands.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package command
import (
"reflect"
"regexp"
"strings"
"container/list"
log "github.com/sirupsen/logrus"
"github.com/ssalvatori/zbot-telegram-go/db"
"github.com/ssalvatori/zbot-telegram-go/user"
"github.com/ssalvatori/zbot-telegram-go/utils"
)
// Levels command definition
type Levels struct {
Ignore int
Lock int
Append int
Learn int
Forget int
Who int
LevelAdd int
LevelDel int
Top int
Stats int
Version int
Ping int
Last int
Rand int
Find int
Get int
Search int
External int
}
var (
DisabledCommands []string
)
// CommandsList list of commandElement
type CommandsList struct {
List *list.List
}
type commandElement struct {
requiredLevel int
command zbotCommand
cmdString string
}
// Chain add a command and the required level to use it to the list of command
func (cmdList *CommandsList) Chain(cmdDefinition string, cmd interface{}, level int) *CommandsList {
newCommand := &commandElement{
command: cmd.(zbotCommand),
requiredLevel: level,
cmdString: cmdDefinition,
}
cmdList.List.PushBack(newCommand)
return cmdList
}
// Run commands against a msg for a given user
func (cmdList *CommandsList) Run(cmd string, msg string, user user.User) string {
var output string
var err error
for e := cmdList.List.Front(); e != nil; e = e.Next() {
output, err = e.Value.(*commandElement).command.(zbotCommand).ProcessText(msg, user)
if err != nil {
output = err.Error()
continue
}
return output
}
return output
}
// zbotCommand interface to be implemented by each command
type zbotCommand interface {
ProcessText(text string, username user.User) (string, error)
}
type HandlerCommand interface {
ProcessText(text string, username user.User) string
}
func getTerms(items []db.DefinitionItem) []string {
var terms []string
for _, item := range items {
if item.Term != "" {
terms = append(terms, item.Term)
}
}
return terms
}
// SetDisabledCommands get the disabled commands from binary json
/*
func SetDisabledCommands(dataBinaryContent []byte) {
var c []string
err := json.Unmarshal(dataBinaryContent, &c)
if err != nil {
log.Debug("No disabled commands")
return
}
DisabledCommands = c
sort.Strings(DisabledCommands)
}
*/
// GetCommandInformation this will parse the text and return the commands and the level minimum to use it or 0 when is
// not defined a level
func GetCommandInformation(text string) string {
commandPattern := regexp.MustCompile(`^!(\S*)\s*.*`)
commandName := ""
if commandPattern.MatchString(text) {
cmd := commandPattern.FindStringSubmatch(text)
commandName = strings.ToLower(cmd[1])
}
return commandName
}
func CheckPermission(command string, user user.User, requiredLevel int) bool {
log.Debug("Checking permission for [", command, "] and user ", user.Username)
if user.Level >= requiredLevel {
return true
}
return false
}
// IsCommandDisabled check if a command is in the disable list
func IsCommandDisabled(commandName string) bool {
log.Debug("Checking if [", commandName, "] is disabled")
//TODO BUG check DisabledCommands before check the array
if utils.InArray(commandName, DisabledCommands) {
return true
}
return false
}
// GetMinimumLevel get the minimum level required for a git command, if it is not defined return 0
func GetMinimumLevel(commandName string, minimumLevels Levels) int {
log.Debug("Getting mininum level for ", commandName)
field, ok := reflect.TypeOf(&minimumLevels).Elem().FieldByName(strings.Title(commandName))
if !ok {
return 0
}
r := reflect.ValueOf(&minimumLevels)
f := reflect.Indirect(r).FieldByName(field.Name)
return int(f.Int())
}