2016-01-17 18:00:44 +00:00
|
|
|
// © 2013 the CatBase Authors under the WTFPL. See AUTHORS for the list of authors.
|
2013-12-10 23:37:07 +00:00
|
|
|
|
2016-03-24 17:32:40 +00:00
|
|
|
package admin
|
2012-08-26 23:24:31 +00:00
|
|
|
|
|
|
|
import (
|
2019-01-21 21:26:54 +00:00
|
|
|
"fmt"
|
2021-12-20 17:40:10 +00:00
|
|
|
bh "github.com/timshannon/bolthold"
|
2020-04-21 20:51:25 +00:00
|
|
|
"os"
|
2020-06-09 17:48:56 +00:00
|
|
|
"regexp"
|
2012-08-26 23:24:31 +00:00
|
|
|
"strings"
|
2019-01-25 15:04:38 +00:00
|
|
|
"time"
|
2016-01-15 06:12:26 +00:00
|
|
|
|
2019-03-07 16:35:42 +00:00
|
|
|
"github.com/rs/zerolog/log"
|
|
|
|
|
2016-01-17 18:00:44 +00:00
|
|
|
"github.com/velour/catbase/bot"
|
2016-04-01 14:20:03 +00:00
|
|
|
"github.com/velour/catbase/bot/msg"
|
2019-01-21 21:26:54 +00:00
|
|
|
"github.com/velour/catbase/config"
|
2012-08-26 23:24:31 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// This is a admin plugin to serve as an example and quick copy/paste for new plugins.
|
|
|
|
|
|
|
|
type AdminPlugin struct {
|
2021-12-20 17:40:10 +00:00
|
|
|
bot bot.Bot
|
|
|
|
store *bh.Store
|
|
|
|
cfg *config.Config
|
2019-01-25 15:04:38 +00:00
|
|
|
|
|
|
|
quiet bool
|
2012-08-26 23:24:31 +00:00
|
|
|
}
|
|
|
|
|
2021-02-01 11:32:41 +00:00
|
|
|
// New creates a new AdminPlugin with the Plugin interface
|
2019-02-05 18:33:18 +00:00
|
|
|
func New(b bot.Bot) *AdminPlugin {
|
2012-08-26 23:24:31 +00:00
|
|
|
p := &AdminPlugin{
|
2021-12-20 17:40:10 +00:00
|
|
|
bot: b,
|
|
|
|
store: b.Store(),
|
|
|
|
cfg: b.Config(),
|
2012-08-26 23:24:31 +00:00
|
|
|
}
|
2021-02-01 11:32:41 +00:00
|
|
|
|
|
|
|
b.RegisterRegex(p, bot.Message, comeBackRegex, p.comeBackCmd)
|
|
|
|
b.RegisterRegexCmd(p, bot.Message, shutupRegex, p.shutupCmd)
|
|
|
|
b.RegisterRegexCmd(p, bot.Message, addBlacklistRegex, p.isAdmin(p.addBlacklistCmd))
|
|
|
|
b.RegisterRegexCmd(p, bot.Message, rmBlacklistRegex, p.isAdmin(p.rmBlacklistCmd))
|
|
|
|
b.RegisterRegexCmd(p, bot.Message, addWhitelistRegex, p.isAdmin(p.addWhitelistCmd))
|
|
|
|
b.RegisterRegexCmd(p, bot.Message, rmWhitelistRegex, p.isAdmin(p.rmWhitelistCmd))
|
|
|
|
b.RegisterRegexCmd(p, bot.Message, allWhitelistRegex, p.isAdmin(p.allWhitelistCmd))
|
|
|
|
b.RegisterRegexCmd(p, bot.Message, allUnWhitelistRegex, p.isAdmin(p.allUnWhitelistCmd))
|
|
|
|
b.RegisterRegexCmd(p, bot.Message, getWhitelistRegex, p.isAdmin(p.getWhitelistCmd))
|
|
|
|
b.RegisterRegexCmd(p, bot.Message, getPluginsRegex, p.isAdmin(p.getPluginsCmd))
|
|
|
|
b.RegisterRegexCmd(p, bot.Message, rebootRegex, p.isAdmin(p.rebootCmd))
|
|
|
|
b.RegisterRegexCmd(p, bot.Message, passwordRegex, p.passwordCmd)
|
|
|
|
b.RegisterRegex(p, bot.Message, variableSetRegex, p.variableSetCmd)
|
|
|
|
b.RegisterRegex(p, bot.Message, variableUnSetRegex, p.variableUnSetCmd)
|
|
|
|
b.RegisterRegexCmd(p, bot.Message, unsetConfigRegex, p.isAdmin(p.unsetConfigCmd))
|
|
|
|
b.RegisterRegexCmd(p, bot.Message, setConfigRegex, p.isAdmin(p.setConfigCmd))
|
|
|
|
b.RegisterRegexCmd(p, bot.Message, pushConfigRegex, p.isAdmin(p.pushConfigCmd))
|
|
|
|
b.RegisterRegexCmd(p, bot.Message, setKeyConfigRegex, p.isAdmin(p.setKeyConfigCmd))
|
|
|
|
b.RegisterRegexCmd(p, bot.Message, getConfigRegex, p.isAdmin(p.getConfigCmd))
|
|
|
|
|
2019-02-05 18:58:12 +00:00
|
|
|
b.Register(p, bot.Help, p.help)
|
2019-06-02 13:28:13 +00:00
|
|
|
p.registerWeb()
|
2021-02-01 11:32:41 +00:00
|
|
|
|
2012-08-26 23:24:31 +00:00
|
|
|
return p
|
|
|
|
}
|
|
|
|
|
2019-01-21 21:26:54 +00:00
|
|
|
var forbiddenKeys = map[string]bool{
|
|
|
|
"twitch.authorization": true,
|
|
|
|
"twitch.clientid": true,
|
|
|
|
"untappd.token": true,
|
|
|
|
"slack.token": true,
|
2020-05-01 16:27:47 +00:00
|
|
|
"meme.memes": true,
|
2019-01-21 21:26:54 +00:00
|
|
|
}
|
|
|
|
|
2021-02-01 11:32:41 +00:00
|
|
|
var shutupRegex = regexp.MustCompile(`(?i)^shut up$`)
|
|
|
|
var comeBackRegex = regexp.MustCompile(`(?i)^come back$`)
|
2020-10-09 16:00:10 +00:00
|
|
|
|
2021-02-01 11:32:41 +00:00
|
|
|
var addBlacklistRegex = regexp.MustCompile(`(?i)disable plugin (?P<plugin>.*)$`)
|
|
|
|
var rmBlacklistRegex = regexp.MustCompile(`(?i)enable plugin (?P<plugin>.*)$`)
|
2012-08-26 23:24:31 +00:00
|
|
|
|
2021-02-01 11:32:41 +00:00
|
|
|
var addWhitelistRegex = regexp.MustCompile(`(?i)^whitelist plugin (?P<plugin>.*)$`)
|
|
|
|
var rmWhitelistRegex = regexp.MustCompile(`(?i)^unwhitelist plugin (?P<plugin>.*)$`)
|
|
|
|
var allWhitelistRegex = regexp.MustCompile(`(?i)^whitelist all$`)
|
|
|
|
var allUnWhitelistRegex = regexp.MustCompile(`(?i)^unwhitelist all$`)
|
|
|
|
var getWhitelistRegex = regexp.MustCompile(`(?i)^list whitelist$`)
|
|
|
|
var getPluginsRegex = regexp.MustCompile(`(?i)^list plugins$`)
|
2019-01-25 15:04:38 +00:00
|
|
|
|
2021-02-01 11:32:41 +00:00
|
|
|
var rebootRegex = regexp.MustCompile(`^reboot$`)
|
|
|
|
var passwordRegex = regexp.MustCompile(`(?i)^password$`)
|
2012-08-26 23:24:31 +00:00
|
|
|
|
2021-02-01 11:32:41 +00:00
|
|
|
var variableSetRegex = regexp.MustCompile(`(?i)^\$(?P<var>\S+)\s?=\s?(?P<value>\S+)$`)
|
|
|
|
var variableUnSetRegex = regexp.MustCompile(`(?i)^\$(?P<var>\S+)\s?!=\s?(?P<value>\S+)$`)
|
2019-01-21 21:26:54 +00:00
|
|
|
|
2021-02-01 11:32:41 +00:00
|
|
|
var unsetConfigRegex = regexp.MustCompile(`(?i)^unset (?P<key>\S+)$`)
|
|
|
|
var setConfigRegex = regexp.MustCompile(`(?i)^set (?P<key>\S+) (?P<value>.*)$`)
|
|
|
|
var pushConfigRegex = regexp.MustCompile(`(?i)^push (?P<key>\S+) (?P<value>.*)$`)
|
|
|
|
var setKeyConfigRegex = regexp.MustCompile(`(?i)^setkey (?P<key>\S+) (?P<name>\S+) (?P<value>.*)$`)
|
|
|
|
var getConfigRegex = regexp.MustCompile(`(?i)^get (?P<key>\S+)$`)
|
2020-04-29 21:45:53 +00:00
|
|
|
|
2021-02-01 11:32:41 +00:00
|
|
|
func (p *AdminPlugin) isAdmin(rh bot.ResponseHandler) bot.ResponseHandler {
|
|
|
|
return func(r bot.Request) bool {
|
|
|
|
if !p.bot.CheckAdmin(r.Msg.User.Name) {
|
|
|
|
log.Debug().Msgf("User %s is not an admin", r.Msg.User.Name)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return rh(r)
|
2019-01-25 15:04:38 +00:00
|
|
|
}
|
2021-02-01 11:32:41 +00:00
|
|
|
}
|
2019-01-25 15:04:38 +00:00
|
|
|
|
2021-02-01 11:32:41 +00:00
|
|
|
func (p *AdminPlugin) shutupCmd(r bot.Request) bool {
|
|
|
|
dur := time.Duration(p.cfg.GetInt("quietDuration", 5)) * time.Minute
|
|
|
|
log.Info().Msgf("Going to sleep for %v, %v", dur, time.Now().Add(dur))
|
|
|
|
leaveMsg := p.bot.Config().Get("admin.shutup", "Okay. I'll be back later.")
|
|
|
|
p.bot.Send(r.Conn, bot.Message, r.Msg.Channel, leaveMsg)
|
|
|
|
p.quiet = true
|
|
|
|
p.bot.SetQuiet(true)
|
|
|
|
go func() {
|
|
|
|
select {
|
|
|
|
case <-time.After(dur):
|
|
|
|
p.quiet = false
|
|
|
|
p.bot.SetQuiet(false)
|
|
|
|
log.Info().Msg("Waking up from nap.")
|
|
|
|
backMsg := p.bot.Config().Get("admin.backmsg", "I'm back, bitches.")
|
|
|
|
p.bot.Send(r.Conn, bot.Message, r.Msg.Channel, backMsg)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
return true
|
|
|
|
}
|
2020-10-09 16:53:50 +00:00
|
|
|
|
2021-02-01 11:32:41 +00:00
|
|
|
func (p *AdminPlugin) comeBackCmd(r bot.Request) bool {
|
|
|
|
p.quiet = false
|
|
|
|
p.bot.SetQuiet(false)
|
|
|
|
backMsg := p.bot.Config().Get("admin.comeback", "Okay, I'm back.")
|
|
|
|
p.bot.Send(r.Conn, bot.Message, r.Msg.Channel, backMsg)
|
|
|
|
return true
|
|
|
|
}
|
2020-10-09 16:53:50 +00:00
|
|
|
|
2021-02-01 11:32:41 +00:00
|
|
|
func (p *AdminPlugin) addBlacklistCmd(r bot.Request) bool {
|
|
|
|
plugin := r.Values["plugin"]
|
|
|
|
if err := p.addBlacklist(r.Msg.Channel, plugin); err != nil {
|
|
|
|
p.bot.Send(r.Conn, bot.Message, r.Msg.Channel, fmt.Sprintf("I couldn't add that item: %s", err))
|
|
|
|
log.Error().Err(err).Msgf("error adding blacklist item")
|
2020-06-09 17:48:56 +00:00
|
|
|
return true
|
|
|
|
}
|
2021-02-01 11:32:41 +00:00
|
|
|
p.bot.Send(r.Conn, bot.Message, r.Msg.Channel, fmt.Sprintf("%s disabled. Use `!enable plugin %s` to re-enable it.", plugin, plugin))
|
|
|
|
return true
|
|
|
|
}
|
2020-06-09 17:48:56 +00:00
|
|
|
|
2021-02-01 11:32:41 +00:00
|
|
|
func (p *AdminPlugin) rmBlacklistCmd(r bot.Request) bool {
|
|
|
|
plugin := r.Values["plugin"]
|
|
|
|
if err := p.rmBlacklist(r.Msg.Channel, plugin); err != nil {
|
|
|
|
p.bot.Send(r.Conn, bot.Message, r.Msg.Channel, fmt.Sprintf("I couldn't remove that item: %s", err))
|
|
|
|
log.Error().Err(err).Msgf("error removing blacklist item")
|
2020-06-09 17:48:56 +00:00
|
|
|
return true
|
|
|
|
}
|
2021-02-01 11:32:41 +00:00
|
|
|
p.bot.Send(r.Conn, bot.Message, r.Msg.Channel, fmt.Sprintf("%s enabled. Use `!disable plugin %s` to disable it.", plugin, plugin))
|
|
|
|
return true
|
|
|
|
}
|
2020-06-09 17:48:56 +00:00
|
|
|
|
2021-02-01 11:32:41 +00:00
|
|
|
func (p *AdminPlugin) addWhitelistCmd(r bot.Request) bool {
|
|
|
|
plugin := r.Values["plugin"]
|
|
|
|
if err := p.addWhitelist(plugin); err != nil {
|
|
|
|
p.bot.Send(r.Conn, bot.Message, r.Msg.Channel, fmt.Sprintf("I couldn't whitelist that item: %s", err))
|
|
|
|
log.Error().Err(err).Msgf("error adding whitelist item")
|
2020-10-09 16:00:10 +00:00
|
|
|
return true
|
|
|
|
}
|
2021-02-01 11:32:41 +00:00
|
|
|
p.bot.Send(r.Conn, bot.Message, r.Msg.Channel, fmt.Sprintf("%s enabled. Use `!unwhitelist plugin %s` to disable it.", plugin, plugin))
|
|
|
|
return true
|
|
|
|
}
|
2020-10-09 16:00:10 +00:00
|
|
|
|
2021-02-01 11:32:41 +00:00
|
|
|
func (p *AdminPlugin) rmWhitelistCmd(r bot.Request) bool {
|
|
|
|
plugin := r.Values["plugin"]
|
|
|
|
if err := p.rmWhitelist(plugin); err != nil {
|
|
|
|
p.bot.Send(r.Conn, bot.Message, r.Msg.Channel, fmt.Sprintf("I couldn't unwhitelist that item: %s", err))
|
|
|
|
log.Error().Err(err).Msgf("error removing whitelist item")
|
2020-10-09 16:00:10 +00:00
|
|
|
return true
|
|
|
|
}
|
2021-02-01 11:32:41 +00:00
|
|
|
p.bot.Send(r.Conn, bot.Message, r.Msg.Channel, fmt.Sprintf("%s disabled. Use `!whitelist plugin %s` to enable it.", plugin, plugin))
|
|
|
|
return true
|
|
|
|
}
|
2020-10-09 16:00:10 +00:00
|
|
|
|
2021-02-01 11:32:41 +00:00
|
|
|
func (p *AdminPlugin) allWhitelistCmd(r bot.Request) bool {
|
|
|
|
plugins := p.bot.GetPluginNames()
|
|
|
|
for _, plugin := range plugins {
|
2020-10-09 16:00:10 +00:00
|
|
|
if err := p.addWhitelist(plugin); err != nil {
|
2021-02-01 11:32:41 +00:00
|
|
|
p.bot.Send(r.Conn, bot.Message, r.Msg.Channel, fmt.Sprintf("I couldn't whitelist that item: %s", err))
|
2020-10-09 16:00:10 +00:00
|
|
|
log.Error().Err(err).Msgf("error adding whitelist item")
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
2021-02-01 11:32:41 +00:00
|
|
|
p.bot.Send(r.Conn, bot.Message, r.Msg.Channel, "Enabled all plugins")
|
|
|
|
return true
|
|
|
|
}
|
2020-10-09 16:00:10 +00:00
|
|
|
|
2021-02-01 11:32:41 +00:00
|
|
|
func (p *AdminPlugin) allUnWhitelistCmd(r bot.Request) bool {
|
|
|
|
plugins := p.bot.GetPluginNames()
|
|
|
|
for _, plugin := range plugins {
|
|
|
|
if plugin == "admin" {
|
|
|
|
continue
|
|
|
|
}
|
2020-10-09 16:00:10 +00:00
|
|
|
if err := p.rmWhitelist(plugin); err != nil {
|
2021-02-01 11:32:41 +00:00
|
|
|
p.bot.Send(r.Conn, bot.Message, r.Msg.Channel, fmt.Sprintf("I couldn't unwhitelist that item: %s", err))
|
2020-10-09 16:00:10 +00:00
|
|
|
log.Error().Err(err).Msgf("error removing whitelist item")
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
2021-02-01 11:32:41 +00:00
|
|
|
p.bot.Send(r.Conn, bot.Message, r.Msg.Channel, "Disabled all plugins")
|
|
|
|
return true
|
|
|
|
}
|
2020-10-09 16:00:10 +00:00
|
|
|
|
2021-02-01 11:32:41 +00:00
|
|
|
func (p *AdminPlugin) getWhitelistCmd(r bot.Request) bool {
|
|
|
|
list := p.bot.GetWhitelist()
|
|
|
|
p.bot.Send(r.Conn, bot.Message, r.Msg.Channel, fmt.Sprintf("Whitelist: %v", list))
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *AdminPlugin) getPluginsCmd(r bot.Request) bool {
|
|
|
|
plugins := p.bot.GetPluginNames()
|
|
|
|
p.bot.Send(r.Conn, bot.Message, r.Msg.Channel, fmt.Sprintf("Plugins: %v", plugins))
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *AdminPlugin) rebootCmd(r bot.Request) bool {
|
|
|
|
p.bot.Send(r.Conn, bot.Message, r.Msg.Channel, "brb")
|
|
|
|
log.Info().Msgf("Got reboot command")
|
|
|
|
os.Exit(0)
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *AdminPlugin) passwordCmd(r bot.Request) bool {
|
|
|
|
p.bot.Send(r.Conn, bot.Message, r.Msg.Channel, p.bot.GetPassword())
|
|
|
|
return true
|
|
|
|
}
|
2020-10-09 16:00:10 +00:00
|
|
|
|
2021-02-01 11:32:41 +00:00
|
|
|
func (p *AdminPlugin) variableSetCmd(r bot.Request) bool {
|
2021-12-20 17:40:10 +00:00
|
|
|
key := strings.ToLower(r.Values["var"])
|
2021-02-01 11:32:41 +00:00
|
|
|
value := r.Values["value"]
|
2021-12-20 17:40:10 +00:00
|
|
|
variable := bot.Variable{key, value}
|
2021-02-01 11:32:41 +00:00
|
|
|
|
2021-12-20 17:40:10 +00:00
|
|
|
count, err := p.store.Count(bot.Variable{}, bh.Where("value").Eq(value))
|
2021-02-01 11:32:41 +00:00
|
|
|
if err != nil {
|
|
|
|
p.bot.Send(r.Conn, bot.Message, r.Msg.Channel, "I'm broke and need attention in my variable creation code.")
|
|
|
|
log.Error().Err(err)
|
2020-10-09 16:00:10 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2021-02-01 11:32:41 +00:00
|
|
|
if count > 0 {
|
|
|
|
p.bot.Send(r.Conn, bot.Message, r.Msg.Channel, "I've already got that one.")
|
|
|
|
} else {
|
2021-12-20 17:40:10 +00:00
|
|
|
err := p.store.Insert(bh.NextSequence(), variable)
|
2021-02-01 11:32:41 +00:00
|
|
|
if err != nil {
|
|
|
|
p.bot.Send(r.Conn, bot.Message, r.Msg.Channel, "I'm broke and need attention in my variable creation code.")
|
|
|
|
log.Error().Err(err)
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
p.bot.Send(r.Conn, bot.Message, r.Msg.Channel, "Added.")
|
2019-06-13 14:04:06 +00:00
|
|
|
}
|
2021-02-01 11:32:41 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *AdminPlugin) variableUnSetCmd(r bot.Request) bool {
|
|
|
|
variable := strings.ToLower(r.Values["var"])
|
|
|
|
value := r.Values["value"]
|
2019-06-13 14:04:06 +00:00
|
|
|
|
2021-12-20 17:40:10 +00:00
|
|
|
err := p.store.Delete(variable, bot.Variable{})
|
|
|
|
p.store.DeleteMatching(bot.Variable{}, bh.Where("name").Eq(variable).And("value").Eq(value))
|
2021-02-01 11:32:41 +00:00
|
|
|
if err != nil {
|
|
|
|
p.bot.Send(r.Conn, bot.Message, r.Msg.Channel, "I'm broke and need attention in my variable creation code.")
|
|
|
|
log.Error().Err(err)
|
|
|
|
} else {
|
|
|
|
p.bot.Send(r.Conn, bot.Message, r.Msg.Channel, "Removed.")
|
2020-05-01 16:31:27 +00:00
|
|
|
}
|
|
|
|
|
2021-02-01 11:32:41 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *AdminPlugin) unsetConfigCmd(r bot.Request) bool {
|
|
|
|
key := r.Values["key"]
|
|
|
|
if forbiddenKeys[key] {
|
|
|
|
p.bot.Send(r.Conn, bot.Message, r.Msg.Channel, "You cannot access that key")
|
2020-01-29 17:18:28 +00:00
|
|
|
return true
|
2021-02-01 11:32:41 +00:00
|
|
|
}
|
|
|
|
if err := p.cfg.Unset(key); err != nil {
|
|
|
|
p.bot.Send(r.Conn, bot.Message, r.Msg.Channel, fmt.Sprintf("Unset error: %s", err))
|
2020-04-29 21:36:34 +00:00
|
|
|
return true
|
2019-01-21 21:26:54 +00:00
|
|
|
}
|
2021-02-01 11:32:41 +00:00
|
|
|
p.bot.Send(r.Conn, bot.Message, r.Msg.Channel, fmt.Sprintf("Unset %s", key))
|
|
|
|
return true
|
|
|
|
}
|
2020-01-29 17:18:28 +00:00
|
|
|
|
2021-02-01 11:32:41 +00:00
|
|
|
func (p *AdminPlugin) setConfigCmd(r bot.Request) bool {
|
|
|
|
key, value := r.Values["key"], r.Values["value"]
|
|
|
|
if forbiddenKeys[key] {
|
|
|
|
p.bot.Send(r.Conn, bot.Message, r.Msg.Channel, "You cannot access that key")
|
2019-01-21 21:26:54 +00:00
|
|
|
return true
|
2021-02-01 11:32:41 +00:00
|
|
|
}
|
|
|
|
if err := p.cfg.Set(key, value); err != nil {
|
|
|
|
p.bot.Send(r.Conn, bot.Message, r.Msg.Channel, fmt.Sprintf("Set error: %s", err))
|
2019-01-21 21:26:54 +00:00
|
|
|
return true
|
|
|
|
}
|
2021-02-01 11:32:41 +00:00
|
|
|
p.bot.Send(r.Conn, bot.Message, r.Msg.Channel, fmt.Sprintf("Set %s", key))
|
|
|
|
return true
|
2012-08-26 23:24:31 +00:00
|
|
|
}
|
|
|
|
|
2021-02-01 11:32:41 +00:00
|
|
|
func (p *AdminPlugin) pushConfigCmd(r bot.Request) bool {
|
|
|
|
key, value := r.Values["key"], r.Values["value"]
|
|
|
|
if forbiddenKeys[key] {
|
|
|
|
p.bot.Send(r.Conn, bot.Message, r.Msg.Channel, "You cannot access that key")
|
2016-05-20 20:28:48 +00:00
|
|
|
return true
|
|
|
|
}
|
2021-02-01 11:32:41 +00:00
|
|
|
items := p.cfg.GetArray(key, []string{})
|
|
|
|
items = append(items, value)
|
|
|
|
if err := p.cfg.Set(key, strings.Join(items, ";;")); err != nil {
|
|
|
|
p.bot.Send(r.Conn, bot.Message, r.Msg.Channel, fmt.Sprintf("Set error: %s", err))
|
|
|
|
return true
|
2012-08-26 23:24:31 +00:00
|
|
|
}
|
2021-02-01 11:32:41 +00:00
|
|
|
p.bot.Send(r.Conn, bot.Message, r.Msg.Channel, fmt.Sprintf("Set %s", key))
|
|
|
|
return true
|
|
|
|
}
|
2012-08-26 23:24:31 +00:00
|
|
|
|
2021-02-01 11:32:41 +00:00
|
|
|
func (p *AdminPlugin) setKeyConfigCmd(r bot.Request) bool {
|
|
|
|
key, name, value := r.Values["key"], r.Values["name"], r.Values["value"]
|
|
|
|
if forbiddenKeys[key] {
|
|
|
|
p.bot.Send(r.Conn, bot.Message, r.Msg.Channel, "You cannot access that key")
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
items := p.cfg.GetMap(key, map[string]string{})
|
|
|
|
items[name] = value
|
|
|
|
if err := p.cfg.SetMap(key, items); err != nil {
|
|
|
|
p.bot.Send(r.Conn, bot.Message, r.Msg.Channel, fmt.Sprintf("Set error: %s", err))
|
2012-08-26 23:24:31 +00:00
|
|
|
return true
|
|
|
|
}
|
2021-02-01 11:32:41 +00:00
|
|
|
p.bot.Send(r.Conn, bot.Message, r.Msg.Channel, fmt.Sprintf("Set %s", key))
|
|
|
|
return true
|
|
|
|
}
|
2016-05-20 20:28:48 +00:00
|
|
|
|
2021-02-01 11:32:41 +00:00
|
|
|
func (p *AdminPlugin) getConfigCmd(r bot.Request) bool {
|
|
|
|
key := r.Values["key"]
|
|
|
|
if forbiddenKeys[key] {
|
|
|
|
p.bot.Send(r.Conn, bot.Message, r.Msg.Channel, "You cannot access that key")
|
|
|
|
return true
|
2016-05-20 20:28:48 +00:00
|
|
|
}
|
2021-02-01 11:32:41 +00:00
|
|
|
v := p.cfg.Get(key, "<unknown>")
|
|
|
|
p.bot.Send(r.Conn, bot.Message, r.Msg.Channel, fmt.Sprintf("%s: %s", key, v))
|
2012-08-26 23:24:31 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// Help responds to help requests. Every plugin must implement a help function.
|
2019-05-27 23:21:53 +00:00
|
|
|
func (p *AdminPlugin) help(conn bot.Connector, kind bot.Kind, m msg.Message, args ...interface{}) bool {
|
2019-06-02 13:28:13 +00:00
|
|
|
p.bot.Send(conn, bot.Message, m.Channel, "This does super secret things that you're not allowed to know about.")
|
2019-02-05 18:33:18 +00:00
|
|
|
return true
|
2013-05-08 00:08:18 +00:00
|
|
|
}
|
2019-06-02 13:28:13 +00:00
|
|
|
|
2020-10-09 16:00:10 +00:00
|
|
|
func (p *AdminPlugin) addWhitelist(plugin string) error {
|
|
|
|
return p.modList(`insert or replace into pluginWhitelist values (?)`, "", plugin)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *AdminPlugin) rmWhitelist(plugin string) error {
|
|
|
|
if plugin == "admin" {
|
|
|
|
return fmt.Errorf("you cannot disable the admin plugin")
|
|
|
|
}
|
|
|
|
return p.modList(`delete from pluginWhitelist where name=?`, "", plugin)
|
|
|
|
}
|
|
|
|
|
2020-06-09 17:48:56 +00:00
|
|
|
func (p *AdminPlugin) addBlacklist(channel, plugin string) error {
|
|
|
|
if plugin == "admin" {
|
|
|
|
return fmt.Errorf("you cannot disable the admin plugin")
|
|
|
|
}
|
2020-10-09 16:00:10 +00:00
|
|
|
return p.modList(`insert or replace into pluginBlacklist values (?, ?)`, channel, plugin)
|
2020-06-09 17:48:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p *AdminPlugin) rmBlacklist(channel, plugin string) error {
|
2020-10-09 16:00:10 +00:00
|
|
|
return p.modList(`delete from pluginBlacklist where channel=? and name=?`, channel, plugin)
|
2020-06-09 17:48:56 +00:00
|
|
|
}
|
|
|
|
|
2020-10-09 16:00:10 +00:00
|
|
|
func (p *AdminPlugin) modList(query, channel, plugin string) error {
|
|
|
|
if channel == "" && plugin != "" {
|
|
|
|
channel = plugin // hack
|
|
|
|
}
|
2020-06-09 17:48:56 +00:00
|
|
|
plugins := p.bot.GetPluginNames()
|
|
|
|
for _, pp := range plugins {
|
|
|
|
if pp == plugin {
|
2021-12-20 17:40:10 +00:00
|
|
|
// todo: yikes
|
|
|
|
//if _, err := p.db.Exec(query, channel, plugin); err != nil {
|
|
|
|
// return fmt.Errorf("%w", err)
|
|
|
|
//}
|
2020-10-09 16:00:10 +00:00
|
|
|
err := p.bot.RefreshPluginWhitelist()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("%w", err)
|
|
|
|
}
|
|
|
|
err = p.bot.RefreshPluginBlacklist()
|
2020-06-09 17:48:56 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("%w", err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
err := fmt.Errorf("unknown plugin named '%s'", plugin)
|
|
|
|
return err
|
|
|
|
}
|