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 (
|
2016-01-15 06:12:26 +00:00
|
|
|
"log"
|
2012-08-26 23:24:31 +00:00
|
|
|
"math/rand"
|
|
|
|
"strings"
|
|
|
|
"time"
|
2016-01-15 06:12:26 +00:00
|
|
|
|
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"
|
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
|
2012-08-26 23:24:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewAdminPlugin creates a new AdminPlugin with the Plugin interface
|
2016-03-30 14:00:20 +00:00
|
|
|
func New(bot bot.Bot) *AdminPlugin {
|
2012-08-26 23:24:31 +00:00
|
|
|
p := &AdminPlugin{
|
|
|
|
Bot: bot,
|
2016-05-20 20:28:48 +00:00
|
|
|
db: bot.DB(),
|
2012-08-26 23:24:31 +00:00
|
|
|
}
|
|
|
|
p.LoadData()
|
|
|
|
return p
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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.
|
2016-04-01 14:20:03 +00:00
|
|
|
func (p *AdminPlugin) Message(message msg.Message) bool {
|
2012-08-26 23:24:31 +00:00
|
|
|
body := message.Body
|
|
|
|
|
2016-05-24 14:07:36 +00:00
|
|
|
if len(body) > 0 && body[0] == '$' {
|
2012-08-26 23:24:31 +00:00
|
|
|
return p.handleVariables(message)
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2016-04-01 14:20:03 +00:00
|
|
|
func (p *AdminPlugin) handleVariables(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 {
|
|
|
|
p.Bot.SendMessage(message.Channel, "I'm broke and need attention in my variable creation code.")
|
|
|
|
log.Println("[admin]: ", err)
|
|
|
|
} else {
|
|
|
|
p.Bot.SendMessage(message.Channel, "Removed.")
|
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
2016-01-15 06:12:26 +00:00
|
|
|
p.Bot.SendMessage(message.Channel, "I'm broke and need attention in my variable creation code.")
|
2016-05-20 20:28:48 +00:00
|
|
|
log.Println("[admin]: ", err)
|
2012-08-26 23:24:31 +00:00
|
|
|
return true
|
|
|
|
}
|
2016-05-20 20:28:48 +00:00
|
|
|
|
|
|
|
if count > 0 {
|
|
|
|
p.Bot.SendMessage(message.Channel, "I've already got that one.")
|
|
|
|
} else {
|
|
|
|
_, err := p.db.Exec(`INSERT INTO variables (name, value) VALUES (?, ?)`, variable, value)
|
|
|
|
if err != nil {
|
|
|
|
p.Bot.SendMessage(message.Channel, "I'm broke and need attention in my variable creation code.")
|
|
|
|
log.Println("[admin]: ", err)
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
p.Bot.SendMessage(message.Channel, "Added.")
|
|
|
|
}
|
2012-08-26 23:24:31 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// LoadData imports any configuration data into the plugin. This is not strictly necessary other
|
|
|
|
// than the fact that the Plugin interface demands it exist. This may be deprecated at a later
|
|
|
|
// date.
|
|
|
|
func (p *AdminPlugin) LoadData() {
|
|
|
|
// This bot has no data to load
|
|
|
|
rand.Seed(time.Now().Unix())
|
|
|
|
}
|
|
|
|
|
|
|
|
// Help responds to help requests. Every plugin must implement a help function.
|
|
|
|
func (p *AdminPlugin) Help(channel string, parts []string) {
|
|
|
|
p.Bot.SendMessage(channel, "This does super secret things that you're not allowed to know about.")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Empty event handler because this plugin does not do anything on event recv
|
2016-04-01 14:20:03 +00:00
|
|
|
func (p *AdminPlugin) Event(kind string, message msg.Message) bool {
|
2012-08-26 23:24:31 +00:00
|
|
|
return false
|
|
|
|
}
|
2013-05-08 00:08:18 +00:00
|
|
|
|
|
|
|
// Handler for bot's own messages
|
2016-04-01 14:20:03 +00:00
|
|
|
func (p *AdminPlugin) BotMessage(message msg.Message) bool {
|
2013-05-08 00:08:18 +00:00
|
|
|
return false
|
|
|
|
}
|
2013-06-01 17:10:15 +00:00
|
|
|
|
|
|
|
// Register any web URLs desired
|
|
|
|
func (p *AdminPlugin) RegisterWeb() *string {
|
|
|
|
return nil
|
|
|
|
}
|
2017-10-31 18:14:45 +00:00
|
|
|
|
|
|
|
func (p *AdminPlugin) ReplyMessage(message msg.Message, identifier string) bool { return false }
|