2017-08-11 17:45:42 +02:00
|
|
|
package bot
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
|
2018-04-13 16:44:23 +02:00
|
|
|
"dev.sum7.eu/genofire/logmania/database"
|
2017-08-11 17:45:42 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
type Bot struct {
|
2018-04-15 01:52:08 +02:00
|
|
|
db *database.DB
|
|
|
|
commandsMap map[string]commandFunc
|
|
|
|
commands []string
|
2017-08-11 17:45:42 +02:00
|
|
|
}
|
|
|
|
|
2017-11-10 18:57:36 +01:00
|
|
|
func NewBot(db *database.DB) *Bot {
|
2017-08-11 17:45:42 +02:00
|
|
|
b := &Bot{
|
2017-11-10 18:57:36 +01:00
|
|
|
db: db,
|
2017-08-11 17:45:42 +02:00
|
|
|
}
|
2018-04-15 01:52:08 +02:00
|
|
|
b.commandsMap = map[string]commandFunc{
|
2017-08-11 17:45:42 +02:00
|
|
|
"help": b.help,
|
2018-04-15 01:52:08 +02:00
|
|
|
"send-add": b.addSend,
|
|
|
|
"send-list": b.listSend,
|
|
|
|
"send-del": b.delSend,
|
|
|
|
"hostname-set": b.addHostname,
|
2017-08-11 17:45:42 +02:00
|
|
|
"hostname-list": b.listHostname,
|
2018-04-15 01:52:08 +02:00
|
|
|
"hostname-del": b.delHostname,
|
2017-08-11 17:45:42 +02:00
|
|
|
"filter-set": b.setMaxfilter,
|
|
|
|
"filter-list": b.listMaxfilter,
|
2017-08-13 09:51:13 +02:00
|
|
|
"regex-add": b.addRegex,
|
|
|
|
"regex-list": b.listRegex,
|
2018-04-15 01:52:08 +02:00
|
|
|
"regex-del": b.delRegex,
|
2018-05-18 11:28:01 +02:00
|
|
|
"replace-add": b.addRegexReplace,
|
|
|
|
"replace-list": b.listRegexReplace,
|
|
|
|
"replace-del": b.delRegexReplace,
|
2018-04-15 01:52:08 +02:00
|
|
|
}
|
2018-04-18 14:32:28 +02:00
|
|
|
for k := range b.commandsMap {
|
2018-04-15 01:52:08 +02:00
|
|
|
b.commands = append(b.commands, k)
|
2017-08-11 17:45:42 +02:00
|
|
|
}
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *Bot) Handle(answer func(string), from, msg string) {
|
|
|
|
msgParts := strings.Split(msg, " ")
|
2017-08-11 19:59:19 +02:00
|
|
|
if len(msgParts[0]) <= 0 || msgParts[0][0] != '!' {
|
2017-08-11 17:45:42 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
cmdName := msgParts[0][1:]
|
2018-04-15 01:52:08 +02:00
|
|
|
if cmd, ok := b.commandsMap[cmdName]; ok {
|
2017-08-11 17:45:42 +02:00
|
|
|
cmd(answer, from, msgParts[1:])
|
|
|
|
} else {
|
|
|
|
answer(fmt.Sprintf("not found command: !%s", cmdName))
|
|
|
|
}
|
|
|
|
}
|