Compare commits

..

3 Commits

Author SHA1 Message Date
Chris Sexton cee267dbb8 admin: leave and come back 2020-04-29 17:48:55 -04:00
Chris Sexton 1166fc732e admin: add setkey 2020-04-29 17:48:55 -04:00
Chris Sexton c7810e9f33 meme: resize images 2020-04-29 15:37:46 -04:00
9 changed files with 100 additions and 32 deletions

View File

@ -50,6 +50,8 @@ type bot struct {
password string password string
passwordCreated time.Time passwordCreated time.Time
quiet bool
} }
type EndPoint struct { type EndPoint struct {
@ -259,3 +261,7 @@ func (b *bot) GetPassword() string {
} }
return b.password return b.password
} }
func (b *bot) SetQuiet(status bool) {
b.quiet = status
}

View File

@ -54,6 +54,9 @@ func (b *bot) runCallback(conn Connector, plugin Plugin, evt Kind, message msg.M
// Send a message to the connection // Send a message to the connection
func (b *bot) Send(conn Connector, kind Kind, args ...interface{}) (string, error) { func (b *bot) Send(conn Connector, kind Kind, args ...interface{}) (string, error) {
if b.quiet {
return "", nil
}
return conn.Send(kind, args...) return conn.Send(kind, args...)
} }

View File

@ -68,6 +68,7 @@ type Bot interface {
DefaultConnector() Connector DefaultConnector() Connector
GetWebNavigation() []EndPoint GetWebNavigation() []EndPoint
GetPassword() string GetPassword() string
SetQuiet(bool)
} }
// Connector represents a server connection to a chat service // Connector represents a server connection to a chat service

View File

@ -33,6 +33,7 @@ func (mb *MockBot) Who(string) []user.User { return []user.User{} }
func (mb *MockBot) WhoAmI() string { return "tester" } func (mb *MockBot) WhoAmI() string { return "tester" }
func (mb *MockBot) DefaultConnector() Connector { return nil } func (mb *MockBot) DefaultConnector() Connector { return nil }
func (mb *MockBot) GetPassword() string { return "12345" } func (mb *MockBot) GetPassword() string { return "12345" }
func (mb *MockBot) SetQuiet(bool) {}
func (mb *MockBot) Send(c Connector, kind Kind, args ...interface{}) (string, error) { func (mb *MockBot) Send(c Connector, kind Kind, args ...interface{}) (string, error) {
switch kind { switch kind {
case Message: case Message:

View File

@ -160,6 +160,14 @@ func (c *Config) Set(key, value string) error {
return nil return nil
} }
func (c *Config) SetMap(key string, values map[string]string) error {
b, err := json.Marshal(values)
if err != nil {
return err
}
return c.Set(key, string(b))
}
func (c *Config) SetArray(key string, values []string) error { func (c *Config) SetArray(key string, values []string) error {
vals := strings.Join(values, ";;") vals := strings.Join(values, ";;")
return c.Set(key, vals) return c.Set(key, vals)

1
go.mod
View File

@ -35,6 +35,7 @@ require (
github.com/mattn/go-sqlite3 v1.11.0 github.com/mattn/go-sqlite3 v1.11.0
github.com/mmcdole/gofeed v1.0.0-beta2 github.com/mmcdole/gofeed v1.0.0-beta2
github.com/mmcdole/goxpp v0.0.0-20181012175147-0068e33feabf // indirect github.com/mmcdole/goxpp v0.0.0-20181012175147-0068e33feabf // indirect
github.com/nfnt/resize v0.0.0-20180221191011-83c6a9932646
github.com/nlopes/slack v0.6.0 github.com/nlopes/slack v0.6.0
github.com/olebedev/when v0.0.0-20190311101825-c3b538a97254 github.com/olebedev/when v0.0.0-20190311101825-c3b538a97254
github.com/robertkrimen/otto v0.0.0-20180617131154-15f95af6e78d // indirect github.com/robertkrimen/otto v0.0.0-20180617131154-15f95af6e78d // indirect

2
go.sum
View File

@ -88,6 +88,8 @@ github.com/mmcdole/gofeed v1.0.0-beta2 h1:CjQ0ADhAwNSb08zknAkGOEYqr8zfZKfrzgk9Bx
github.com/mmcdole/gofeed v1.0.0-beta2/go.mod h1:/BF9JneEL2/flujm8XHoxUcghdTV6vvb3xx/vKyChFU= github.com/mmcdole/gofeed v1.0.0-beta2/go.mod h1:/BF9JneEL2/flujm8XHoxUcghdTV6vvb3xx/vKyChFU=
github.com/mmcdole/goxpp v0.0.0-20181012175147-0068e33feabf h1:sWGE2v+hO0Nd4yFU/S/mDBM5plIU8v/Qhfz41hkDIAI= github.com/mmcdole/goxpp v0.0.0-20181012175147-0068e33feabf h1:sWGE2v+hO0Nd4yFU/S/mDBM5plIU8v/Qhfz41hkDIAI=
github.com/mmcdole/goxpp v0.0.0-20181012175147-0068e33feabf/go.mod h1:pasqhqstspkosTneA62Nc+2p9SOBBYAPbnmRRWPQ0V8= github.com/mmcdole/goxpp v0.0.0-20181012175147-0068e33feabf/go.mod h1:pasqhqstspkosTneA62Nc+2p9SOBBYAPbnmRRWPQ0V8=
github.com/nfnt/resize v0.0.0-20180221191011-83c6a9932646 h1:zYyBkD/k9seD2A7fsi6Oo2LfFZAehjjQMERAvZLEDnQ=
github.com/nfnt/resize v0.0.0-20180221191011-83c6a9932646/go.mod h1:jpp1/29i3P1S/RLdc7JQKbRpFeM1dOBd8T9ki5s+AY8=
github.com/nlopes/slack v0.6.0 h1:jt0jxVQGhssx1Ib7naAOZEZcGdtIhTzkP0nopK0AsRA= github.com/nlopes/slack v0.6.0 h1:jt0jxVQGhssx1Ib7naAOZEZcGdtIhTzkP0nopK0AsRA=
github.com/nlopes/slack v0.6.0/go.mod h1:JzQ9m3PMAqcpeCam7UaHSuBuupz7CmpjehYMayT6YOk= github.com/nlopes/slack v0.6.0/go.mod h1:JzQ9m3PMAqcpeCam7UaHSuBuupz7CmpjehYMayT6YOk=
github.com/olebedev/when v0.0.0-20190311101825-c3b538a97254 h1:JYoQR67E1vv1WGoeW8DkdFs7vrIEe/5wP+qJItd5tUE= github.com/olebedev/when v0.0.0-20190311101825-c3b538a97254 h1:JYoQR67E1vv1WGoeW8DkdFs7vrIEe/5wP+qJItd5tUE=

View File

@ -55,7 +55,7 @@ var forbiddenKeys = map[string]bool{
func (p *AdminPlugin) message(conn bot.Connector, k bot.Kind, message msg.Message, args ...interface{}) bool { func (p *AdminPlugin) message(conn bot.Connector, k bot.Kind, message msg.Message, args ...interface{}) bool {
body := message.Body body := message.Body
if p.quiet { if p.quiet && message.Body != "come back" {
return true return true
} }
@ -67,7 +67,16 @@ func (p *AdminPlugin) message(conn bot.Connector, k bot.Kind, message msg.Messag
return false return false
} }
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) == "reboot" { if strings.ToLower(body) == "reboot" {
p.bot.Send(conn, bot.Message, message.Channel, "brb")
log.Info().Msgf("Got reboot command") log.Info().Msgf("Got reboot command")
os.Exit(0) os.Exit(0)
} }
@ -75,13 +84,18 @@ func (p *AdminPlugin) message(conn bot.Connector, k bot.Kind, message msg.Messag
if strings.ToLower(body) == "shut up" { if strings.ToLower(body) == "shut up" {
dur := time.Duration(p.cfg.GetInt("quietDuration", 5)) * time.Minute dur := time.Duration(p.cfg.GetInt("quietDuration", 5)) * time.Minute
log.Info().Msgf("Going to sleep for %v, %v", dur, time.Now().Add(dur)) log.Info().Msgf("Going to sleep for %v, %v", dur, time.Now().Add(dur))
p.bot.Send(conn, bot.Message, message.Channel, "Okay. I'll be back later.") 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 p.quiet = true
p.bot.SetQuiet(true)
go func() { go func() {
select { select {
case <-time.After(dur): case <-time.After(dur):
p.quiet = false p.quiet = false
p.bot.SetQuiet(false)
log.Info().Msg("Waking up from nap.") log.Info().Msg("Waking up from nap.")
backMsg := p.bot.Config().Get("admin.backmsg", "I'm back, bitches.")
p.bot.Send(conn, bot.Message, message.Channel, backMsg)
} }
}() }()
return true return true
@ -119,6 +133,15 @@ func (p *AdminPlugin) message(conn bot.Connector, k bot.Kind, message msg.Messag
} }
p.bot.Send(conn, bot.Message, message.Channel, fmt.Sprintf("Set %s", parts[1])) p.bot.Send(conn, bot.Message, message.Channel, fmt.Sprintf("Set %s", parts[1]))
return true return true
} 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]] { if parts[0] == "get" && len(parts) == 2 && forbiddenKeys[parts[1]] {

View File

@ -13,6 +13,7 @@ import (
"github.com/fogleman/gg" "github.com/fogleman/gg"
"github.com/google/uuid" "github.com/google/uuid"
"github.com/nfnt/resize"
"github.com/rs/zerolog/log" "github.com/rs/zerolog/log"
"github.com/velour/catbase/bot" "github.com/velour/catbase/bot"
@ -90,38 +91,40 @@ func (p *MemePlugin) registerWeb(c bot.Connector) {
log.Debug().Strs("parts", parts).Msgf("Meme:\n%+v", r.PostForm.Get("text")) log.Debug().Strs("parts", parts).Msgf("Meme:\n%+v", r.PostForm.Get("text"))
w.WriteHeader(200) w.WriteHeader(200)
top, bottom := "", message
parts = strings.Split(message, "\n")
if len(parts) > 1 {
top, bottom = parts[0], parts[1]
}
id := p.genMeme(format, top, bottom)
baseURL := p.c.Get("BaseURL", `https://catbase.velour.ninja`)
u, _ := url.Parse(baseURL)
u.Path = path.Join(u.Path, "meme", "img", id)
log.Debug().Msgf("image is at %s", u.String())
p.bot.Send(c, bot.Message, channel, "", bot.ImageAttachment{
URL: u.String(),
AltTxt: fmt.Sprintf("%s: %s", from, message),
})
w.Write(nil) w.Write(nil)
m := msg.Message{
User: &user.User{
ID: from,
Name: from,
Admin: false,
},
Channel: channel,
ChannelName: channelName,
Body: message,
Command: isCmd,
Time: time.Now(),
}
p.bot.Receive(c, bot.Message, m) go func() {
top, bottom := "", message
parts = strings.Split(message, "\n")
if len(parts) > 1 {
top, bottom = parts[0], parts[1]
}
id := p.genMeme(format, top, bottom)
baseURL := p.c.Get("BaseURL", `https://catbase.velour.ninja`)
u, _ := url.Parse(baseURL)
u.Path = path.Join(u.Path, "meme", "img", id)
log.Debug().Msgf("image is at %s", u.String())
p.bot.Send(c, bot.Message, channel, "", bot.ImageAttachment{
URL: u.String(),
AltTxt: fmt.Sprintf("%s: %s", from, message),
})
m := msg.Message{
User: &user.User{
ID: from,
Name: from,
Admin: false,
},
Channel: channel,
ChannelName: channelName,
Body: message,
Command: isCmd,
Time: time.Now(),
}
p.bot.Receive(c, bot.Message, m)
}()
}) })
http.HandleFunc("/meme/img/", func(w http.ResponseWriter, r *http.Request) { http.HandleFunc("/meme/img/", func(w http.ResponseWriter, r *http.Request) {
@ -182,10 +185,30 @@ func (p *MemePlugin) genMeme(meme, top, bottom string) string {
log.Debug().Msgf("Attempting to download url: %s", u.String()) log.Debug().Msgf("Attempting to download url: %s", u.String())
img := DownloadTemplate(u) img := DownloadTemplate(u)
r := img.Bounds() r := img.Bounds()
w := r.Dx() w := r.Dx()
h := r.Dy() h := r.Dy()
maxSz := 750.0
if w > h {
scale := maxSz / float64(w)
w = int(float64(w) * scale)
h = int(float64(h) * scale)
} else {
scale := maxSz / float64(h)
w = int(float64(w) * scale)
h = int(float64(h) * scale)
}
log.Debug().Msgf("trynig to resize to %v, %v", w, h)
img = resize.Resize(uint(w), uint(h), img, resize.Lanczos3)
r = img.Bounds()
w = r.Dx()
h = r.Dy()
log.Debug().Msgf("resized to %v, %v", w, h)
m := gg.NewContext(w, h) m := gg.NewContext(w, h)
m.DrawImage(img, 0, 0) m.DrawImage(img, 0, 0)
fontLocation := p.c.Get("meme.font", "impact.ttf") fontLocation := p.c.Get("meme.font", "impact.ttf")