catbase/plugins/admin/admin.go

406 lines
13 KiB
Go
Raw Normal View History

2016-01-17 18:00:44 +00:00
// © 2013 the CatBase Authors under the WTFPL. See AUTHORS for the list of authors.
package admin
2012-08-26 23:24:31 +00:00
import (
2019-06-02 13:28:13 +00:00
"encoding/json"
"fmt"
2019-06-02 13:28:13 +00:00
"net/http"
2020-04-21 20:51:25 +00:00
"os"
"regexp"
2012-08-26 23:24:31 +00:00
"strings"
"time"
2019-03-07 16:35:42 +00:00
"github.com/rs/zerolog/log"
2016-03-19 18:02:46 +00:00
"github.com/jmoiron/sqlx"
2020-05-01 16:27:47 +00:00
2016-01-17 18:00:44 +00:00
"github.com/velour/catbase/bot"
"github.com/velour/catbase/bot/msg"
"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 {
2019-06-02 13:28:13 +00:00
bot bot.Bot
2016-05-20 20:28:48 +00:00
db *sqlx.DB
cfg *config.Config
quiet bool
2012-08-26 23:24:31 +00:00
}
// NewAdminPlugin creates a new AdminPlugin with the Plugin interface
func New(b bot.Bot) *AdminPlugin {
2012-08-26 23:24:31 +00:00
p := &AdminPlugin{
2019-06-02 13:28:13 +00:00
bot: b,
db: b.DB(),
cfg: b.Config(),
2012-08-26 23:24:31 +00:00
}
b.Register(p, bot.Message, p.message)
b.Register(p, bot.Help, p.help)
2019-06-02 13:28:13 +00:00
p.registerWeb()
2012-08-26 23:24:31 +00:00
return p
}
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,
}
var addBlacklist = regexp.MustCompile(`(?i)disable plugin (.*)`)
var rmBlacklist = regexp.MustCompile(`(?i)enable plugin (.*)`)
2020-10-09 16:00:10 +00:00
var addWhitelist = regexp.MustCompile(`(?i)^whitelist plugin (.*)`)
var rmWhitelist = regexp.MustCompile(`(?i)^unwhitelist plugin (.*)`)
var allWhitelist = regexp.MustCompile(`(?i)^whitelist all`)
var allUnwhitelist = regexp.MustCompile(`(?i)^unwhitelist all`)
var getWhitelist = regexp.MustCompile(`(?i)^list whitelist`)
var getPlugins = regexp.MustCompile(`(?i)^list plugins`)
2012-08-26 23:24:31 +00:00
// Message responds to the bot hook on recieving messages.
// This function returns true if the plugin responds in a meaningful way to the users message.
// Otherwise, the function returns false and the bot continues execution of other plugins.
2019-05-27 23:21:53 +00:00
func (p *AdminPlugin) message(conn bot.Connector, k bot.Kind, message msg.Message, args ...interface{}) bool {
2012-08-26 23:24:31 +00:00
body := message.Body
2020-04-29 21:45:53 +00:00
if p.quiet && message.Body != "come back" {
return true
}
2016-05-24 14:07:36 +00:00
if len(body) > 0 && body[0] == '$' {
2019-05-27 23:21:53 +00:00
return p.handleVariables(conn, message)
2012-08-26 23:24:31 +00:00
}
if !message.Command {
return false
}
2020-04-29 21:45:53 +00:00
if p.quiet && message.Body == "come back" {
p.quiet = false
p.bot.SetQuiet(false)
backMsg := p.bot.Config().Get("admin.comeback", "Okay, I'm back.")
p.bot.Send(conn, bot.Message, message.Channel, backMsg)
return true
}
if strings.ToLower(body) == "shut up" {
dur := time.Duration(p.cfg.GetInt("quietDuration", 5)) * time.Minute
2019-03-07 16:35:42 +00:00
log.Info().Msgf("Going to sleep for %v, %v", dur, time.Now().Add(dur))
2020-04-29 21:45:53 +00:00
leaveMsg := p.bot.Config().Get("admin.shutup", "Okay. I'll be back later.")
p.bot.Send(conn, bot.Message, message.Channel, leaveMsg)
p.quiet = true
2020-04-29 21:45:53 +00:00
p.bot.SetQuiet(true)
go func() {
select {
case <-time.After(dur):
p.quiet = false
2020-04-29 21:45:53 +00:00
p.bot.SetQuiet(false)
2019-03-07 16:35:42 +00:00
log.Info().Msg("Waking up from nap.")
2020-04-29 21:45:53 +00:00
backMsg := p.bot.Config().Get("admin.backmsg", "I'm back, bitches.")
p.bot.Send(conn, bot.Message, message.Channel, backMsg)
}
}()
return true
}
2020-10-09 16:53:50 +00:00
if !p.bot.CheckAdmin(message.User.Name) {
log.Debug().Msgf("User %s is not an admin", message.User.Name)
return false
}
if strings.ToLower(body) == "reboot" {
p.bot.Send(conn, bot.Message, message.Channel, "brb")
log.Info().Msgf("Got reboot command")
os.Exit(0)
}
if addBlacklist.MatchString(body) {
submatches := addBlacklist.FindStringSubmatch(message.Body)
plugin := submatches[1]
if err := p.addBlacklist(message.Channel, plugin); err != nil {
p.bot.Send(conn, bot.Message, message.Channel, fmt.Sprintf("I couldn't add that item: %s", err))
log.Error().Err(err).Msgf("error adding blacklist item")
return true
}
p.bot.Send(conn, bot.Message, message.Channel, fmt.Sprintf("%s disabled. Use `!enable plugin %s` to re-enable it.", plugin, plugin))
return true
}
if rmBlacklist.MatchString(body) {
submatches := rmBlacklist.FindStringSubmatch(message.Body)
plugin := submatches[1]
if err := p.rmBlacklist(message.Channel, plugin); err != nil {
p.bot.Send(conn, bot.Message, message.Channel, fmt.Sprintf("I couldn't remove that item: %s", err))
log.Error().Err(err).Msgf("error removing blacklist item")
return true
}
p.bot.Send(conn, bot.Message, message.Channel, fmt.Sprintf("%s enabled. Use `!disable plugin %s` to disable it.", plugin, plugin))
return true
}
2020-10-09 16:00:10 +00:00
if allWhitelist.MatchString(body) {
plugins := p.bot.GetPluginNames()
for _, plugin := range plugins {
if err := p.addWhitelist(plugin); err != nil {
p.bot.Send(conn, bot.Message, message.Channel, fmt.Sprintf("I couldn't whitelist that item: %s", err))
log.Error().Err(err).Msgf("error adding whitelist item")
return true
}
}
p.bot.Send(conn, bot.Message, message.Channel, "Enabled all plugins")
return true
}
if allUnwhitelist.MatchString(body) {
plugins := p.bot.GetPluginNames()
for _, plugin := range plugins {
if plugin == "admin" {
continue
}
if err := p.rmWhitelist(plugin); err != nil {
p.bot.Send(conn, bot.Message, message.Channel, fmt.Sprintf("I couldn't unwhitelist that item: %s", err))
log.Error().Err(err).Msgf("error removing whitelist item")
return true
}
}
p.bot.Send(conn, bot.Message, message.Channel, "Disabled all plugins")
return true
}
if addWhitelist.MatchString(body) {
submatches := addWhitelist.FindStringSubmatch(message.Body)
plugin := submatches[1]
if err := p.addWhitelist(plugin); err != nil {
p.bot.Send(conn, bot.Message, message.Channel, fmt.Sprintf("I couldn't whitelist that item: %s", err))
log.Error().Err(err).Msgf("error adding whitelist item")
return true
}
p.bot.Send(conn, bot.Message, message.Channel, fmt.Sprintf("%s enabled. Use `!unwhitelist plugin %s` to disable it.", plugin, plugin))
return true
}
if rmWhitelist.MatchString(body) {
submatches := rmWhitelist.FindStringSubmatch(message.Body)
plugin := submatches[1]
if err := p.rmWhitelist(plugin); err != nil {
p.bot.Send(conn, bot.Message, message.Channel, fmt.Sprintf("I couldn't unwhitelist that item: %s", err))
log.Error().Err(err).Msgf("error removing whitelist item")
return true
}
p.bot.Send(conn, bot.Message, message.Channel, fmt.Sprintf("%s disabled. Use `!whitelist plugin %s` to enable it.", plugin, plugin))
return true
}
if getWhitelist.MatchString(body) {
list := p.bot.GetWhitelist()
p.bot.Send(conn, bot.Message, message.Channel, fmt.Sprintf("Whitelist: %v", list))
return true
}
if getPlugins.MatchString(body) {
plugins := p.bot.GetPluginNames()
p.bot.Send(conn, bot.Message, message.Channel, fmt.Sprintf("Plugins: %v", plugins))
return true
}
if strings.ToLower(body) == "password" {
p.bot.Send(conn, bot.Message, message.Channel, p.bot.GetPassword())
return true
}
2020-05-01 16:31:27 +00:00
verbs := map[string]bool{
"set": true,
"push": true,
"setkey": true,
}
parts := strings.Split(body, " ")
2020-05-01 16:31:27 +00:00
if verbs[parts[0]] && len(parts) > 2 && forbiddenKeys[parts[1]] {
2019-06-02 13:28:13 +00:00
p.bot.Send(conn, bot.Message, message.Channel, "You cannot access that key")
return true
} else if parts[0] == "unset" && len(parts) > 1 {
if err := p.cfg.Unset(parts[1]); err != nil {
p.bot.Send(conn, bot.Message, message.Channel, fmt.Sprintf("Unset error: %s", err))
return true
}
p.bot.Send(conn, bot.Message, message.Channel, fmt.Sprintf("Unset %s", parts[1]))
return true
} else if parts[0] == "set" && len(parts) > 2 {
if err := p.cfg.Set(parts[1], strings.Join(parts[2:], " ")); err != nil {
p.bot.Send(conn, bot.Message, message.Channel, fmt.Sprintf("Set error: %s", err))
return true
}
2019-06-02 13:28:13 +00:00
p.bot.Send(conn, bot.Message, message.Channel, fmt.Sprintf("Set %s", parts[1]))
return true
} else if parts[0] == "push" && len(parts) > 2 {
items := p.cfg.GetArray(parts[1], []string{})
items = append(items, strings.Join(parts[2:], ""))
if err := p.cfg.Set(parts[1], strings.Join(items, ";;")); err != nil {
p.bot.Send(conn, bot.Message, message.Channel, fmt.Sprintf("Set error: %s", err))
return true
}
p.bot.Send(conn, bot.Message, message.Channel, fmt.Sprintf("Set %s", parts[1]))
return true
2020-04-29 21:36:34 +00:00
} else if parts[0] == "setkey" && len(parts) > 3 {
items := p.cfg.GetMap(parts[1], map[string]string{})
items[parts[2]] = strings.Join(parts[3:], " ")
if err := p.cfg.SetMap(parts[1], items); err != nil {
p.bot.Send(conn, bot.Message, message.Channel, fmt.Sprintf("Set error: %s", err))
return true
}
p.bot.Send(conn, bot.Message, message.Channel, fmt.Sprintf("Set %s", parts[1]))
return true
}
if parts[0] == "get" && len(parts) == 2 && forbiddenKeys[parts[1]] {
2019-06-02 13:28:13 +00:00
p.bot.Send(conn, bot.Message, message.Channel, "You cannot access that key")
return true
} else if parts[0] == "get" && len(parts) == 2 {
2019-01-22 00:16:57 +00:00
v := p.cfg.Get(parts[1], "<unknown>")
2019-06-02 13:28:13 +00:00
p.bot.Send(conn, bot.Message, message.Channel, fmt.Sprintf("%s: %s", parts[1], v))
return true
}
2012-08-26 23:24:31 +00:00
return false
}
2019-05-27 23:21:53 +00:00
func (p *AdminPlugin) handleVariables(conn bot.Connector, message msg.Message) bool {
2016-05-20 20:28:48 +00:00
if parts := strings.SplitN(message.Body, "!=", 2); len(parts) == 2 {
variable := strings.ToLower(strings.TrimSpace(parts[0]))
value := strings.TrimSpace(parts[1])
_, err := p.db.Exec(`delete from variables where name=? and value=?`, variable, value)
if err != nil {
2019-06-02 13:28:13 +00:00
p.bot.Send(conn, bot.Message, message.Channel, "I'm broke and need attention in my variable creation code.")
2019-03-07 16:35:42 +00:00
log.Error().Err(err)
2016-05-20 20:28:48 +00:00
} else {
2019-06-02 13:28:13 +00:00
p.bot.Send(conn, bot.Message, message.Channel, "Removed.")
2016-05-20 20:28:48 +00:00
}
return true
}
2012-08-26 23:24:31 +00:00
parts := strings.SplitN(message.Body, "=", 2)
if len(parts) != 2 {
return false
}
2016-05-20 20:28:48 +00:00
variable := strings.ToLower(strings.TrimSpace(parts[0]))
value := strings.TrimSpace(parts[1])
2012-08-26 23:24:31 +00:00
var count int64
2016-05-20 20:28:48 +00:00
row := p.db.QueryRow(`select count(*) from variables where value = ?`, variable, value)
err := row.Scan(&count)
if err != nil {
2019-06-02 13:28:13 +00:00
p.bot.Send(conn, bot.Message, message.Channel, "I'm broke and need attention in my variable creation code.")
2019-03-07 16:35:42 +00:00
log.Error().Err(err)
2012-08-26 23:24:31 +00:00
return true
}
2016-05-20 20:28:48 +00:00
if count > 0 {
2019-06-02 13:28:13 +00:00
p.bot.Send(conn, bot.Message, message.Channel, "I've already got that one.")
2016-05-20 20:28:48 +00:00
} else {
_, err := p.db.Exec(`INSERT INTO variables (name, value) VALUES (?, ?)`, variable, value)
if err != nil {
2019-06-02 13:28:13 +00:00
p.bot.Send(conn, bot.Message, message.Channel, "I'm broke and need attention in my variable creation code.")
2019-03-07 16:35:42 +00:00
log.Error().Err(err)
2016-05-20 20:28:48 +00:00
return true
}
2019-06-02 13:28:13 +00:00
p.bot.Send(conn, bot.Message, message.Channel, "Added.")
2016-05-20 20:28:48 +00:00
}
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.")
return true
}
2019-06-02 13:28:13 +00:00
func (p *AdminPlugin) registerWeb() {
http.HandleFunc("/vars/api", p.handleWebAPI)
http.HandleFunc("/vars", p.handleWeb)
p.bot.RegisterWeb("/vars", "Variables")
}
func (p *AdminPlugin) handleWeb(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, varIndex)
2019-06-02 13:28:13 +00:00
}
func (p *AdminPlugin) handleWebAPI(w http.ResponseWriter, r *http.Request) {
var configEntries []struct {
Key string `json:"key"`
Value string `json:"value"`
}
q := `select key, value from config`
err := p.db.Select(&configEntries, q)
if err != nil {
log.Error().
Err(err).
Msg("Error getting config entries.")
w.WriteHeader(500)
fmt.Fprint(w, err)
return
}
for i, e := range configEntries {
if strings.Contains(e.Value, ";;") {
e.Value = strings.ReplaceAll(e.Value, ";;", ", ")
e.Value = fmt.Sprintf("[%s]", e.Value)
configEntries[i] = e
}
}
j, _ := json.Marshal(configEntries)
fmt.Fprintf(w, "%s", j)
}
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)
}
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)
}
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-10-09 16:00:10 +00:00
func (p *AdminPlugin) modList(query, channel, plugin string) error {
if channel == "" && plugin != "" {
channel = plugin // hack
}
plugins := p.bot.GetPluginNames()
for _, pp := range plugins {
if pp == plugin {
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()
if err != nil {
return fmt.Errorf("%w", err)
}
return nil
}
}
err := fmt.Errorf("unknown plugin named '%s'", plugin)
return err
}