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"
|
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-03-19 18:02:46 +00:00
|
|
|
"github.com/jmoiron/sqlx"
|
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 {
|
2016-03-30 14:00:20 +00:00
|
|
|
Bot bot.Bot
|
2016-05-20 20:28:48 +00:00
|
|
|
db *sqlx.DB
|
2019-01-21 21:26:54 +00:00
|
|
|
cfg *config.Config
|
2019-01-25 15:04:38 +00:00
|
|
|
|
|
|
|
quiet bool
|
2012-08-26 23:24:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewAdminPlugin 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{
|
2019-02-05 18:33:18 +00:00
|
|
|
Bot: b,
|
|
|
|
db: b.DB(),
|
|
|
|
cfg: b.Config(),
|
2012-08-26 23:24:31 +00:00
|
|
|
}
|
2019-02-05 18:58:12 +00:00
|
|
|
b.Register(p, bot.Message, p.message)
|
|
|
|
b.Register(p, bot.Help, p.help)
|
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,
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
|
2019-01-25 15:04:38 +00:00
|
|
|
if p.quiet {
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2019-01-21 21:26:54 +00:00
|
|
|
if !message.Command {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2019-01-25 15:04:38 +00:00
|
|
|
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))
|
2019-05-27 23:21:53 +00:00
|
|
|
p.Bot.Send(conn, bot.Message, message.Channel, "Okay. I'll be back later.")
|
2019-01-25 15:04:38 +00:00
|
|
|
p.quiet = true
|
|
|
|
go func() {
|
|
|
|
select {
|
|
|
|
case <-time.After(dur):
|
|
|
|
p.quiet = false
|
2019-03-07 16:35:42 +00:00
|
|
|
log.Info().Msg("Waking up from nap.")
|
2019-01-25 15:04:38 +00:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2019-01-21 21:26:54 +00:00
|
|
|
parts := strings.Split(body, " ")
|
|
|
|
if parts[0] == "set" && len(parts) > 2 && forbiddenKeys[parts[1]] {
|
2019-05-27 23:21:53 +00:00
|
|
|
p.Bot.Send(conn, bot.Message, message.Channel, "You cannot access that key")
|
2019-01-21 21:26:54 +00:00
|
|
|
return true
|
|
|
|
} else if parts[0] == "set" && len(parts) > 2 {
|
|
|
|
p.cfg.Set(parts[1], strings.Join(parts[2:], " "))
|
2019-05-27 23:21:53 +00:00
|
|
|
p.Bot.Send(conn, bot.Message, message.Channel, fmt.Sprintf("Set %s", parts[1]))
|
2019-01-21 21:26:54 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
if parts[0] == "get" && len(parts) == 2 && forbiddenKeys[parts[1]] {
|
2019-05-27 23:21:53 +00:00
|
|
|
p.Bot.Send(conn, bot.Message, message.Channel, "You cannot access that key")
|
2019-01-21 21:26:54 +00:00
|
|
|
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-05-27 23:21:53 +00:00
|
|
|
p.Bot.Send(conn, bot.Message, message.Channel, fmt.Sprintf("%s: %s", parts[1], v))
|
2019-01-21 21:26:54 +00:00
|
|
|
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-05-27 23:21:53 +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-05-27 23:21:53 +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
|
|
|
|
2016-01-15 06:12:26 +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-05-27 23:21:53 +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-05-27 23:21:53 +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-05-27 23:21:53 +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-05-27 23:21:53 +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 {
|
|
|
|
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
|
|
|
}
|