2020-04-28 15:32:52 +00:00
|
|
|
package meme
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2020-05-01 16:27:47 +00:00
|
|
|
"encoding/json"
|
2020-04-28 19:00:44 +00:00
|
|
|
"fmt"
|
2020-05-01 16:27:47 +00:00
|
|
|
"html/template"
|
2020-04-28 15:32:52 +00:00
|
|
|
"image"
|
|
|
|
"image/png"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
"path"
|
2020-05-01 16:27:47 +00:00
|
|
|
"sort"
|
2020-04-28 15:32:52 +00:00
|
|
|
"strings"
|
2020-04-29 14:57:00 +00:00
|
|
|
"time"
|
2020-04-28 15:32:52 +00:00
|
|
|
|
|
|
|
"github.com/fogleman/gg"
|
|
|
|
"github.com/google/uuid"
|
2020-04-29 19:35:13 +00:00
|
|
|
"github.com/nfnt/resize"
|
2020-04-28 15:32:52 +00:00
|
|
|
"github.com/rs/zerolog/log"
|
|
|
|
|
|
|
|
"github.com/velour/catbase/bot"
|
|
|
|
"github.com/velour/catbase/bot/msg"
|
2020-04-29 14:57:00 +00:00
|
|
|
"github.com/velour/catbase/bot/user"
|
2020-04-28 15:32:52 +00:00
|
|
|
"github.com/velour/catbase/config"
|
|
|
|
)
|
|
|
|
|
|
|
|
type MemePlugin struct {
|
|
|
|
bot bot.Bot
|
|
|
|
c *config.Config
|
|
|
|
|
2020-04-29 15:29:54 +00:00
|
|
|
images cachedImages
|
|
|
|
}
|
|
|
|
|
|
|
|
type cachedImage struct {
|
|
|
|
created time.Time
|
|
|
|
repr []byte
|
|
|
|
}
|
|
|
|
|
|
|
|
var horizon = 24 * 7
|
|
|
|
|
|
|
|
type cachedImages map[string]*cachedImage
|
|
|
|
|
|
|
|
func (ci cachedImages) cleanup() {
|
|
|
|
for key, img := range ci {
|
|
|
|
if time.Now().After(img.created.Add(time.Hour * time.Duration(horizon))) {
|
|
|
|
delete(ci, key)
|
|
|
|
}
|
|
|
|
}
|
2020-04-28 15:32:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func New(b bot.Bot) *MemePlugin {
|
|
|
|
mp := &MemePlugin{
|
|
|
|
bot: b,
|
|
|
|
c: b.Config(),
|
2020-04-29 15:29:54 +00:00
|
|
|
images: make(cachedImages),
|
2020-04-28 15:32:52 +00:00
|
|
|
}
|
|
|
|
|
2020-04-29 15:29:54 +00:00
|
|
|
horizon = mp.c.GetInt("meme.horizon", horizon)
|
|
|
|
|
2020-04-28 15:32:52 +00:00
|
|
|
b.Register(mp, bot.Message, mp.message)
|
|
|
|
b.Register(mp, bot.Help, mp.help)
|
|
|
|
mp.registerWeb(b.DefaultConnector())
|
|
|
|
|
|
|
|
return mp
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *MemePlugin) message(c bot.Connector, kind bot.Kind, message msg.Message, args ...interface{}) bool {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *MemePlugin) help(c bot.Connector, kind bot.Kind, message msg.Message, args ...interface{}) bool {
|
2020-05-01 16:27:47 +00:00
|
|
|
webRoot := p.c.Get("BaseURL", "https://catbase.velour.ninja")
|
2020-04-28 15:32:52 +00:00
|
|
|
formats := p.c.GetMap("meme.memes", defaultFormats)
|
|
|
|
msg := "Use `/meme [format] [text]` to create a meme.\nI know the following formats:"
|
2020-05-01 14:39:42 +00:00
|
|
|
msg += "\n`[format]` can be a URL"
|
2020-05-04 21:29:06 +00:00
|
|
|
msg += fmt.Sprintf("\nor a format from the list of %d pre-made memes listed on the website", len(formats))
|
|
|
|
msg += fmt.Sprintf("\nHead over to %s/meme to view and add new meme formats", webRoot)
|
|
|
|
msg += "\nYou can use `_` as a placeholder for empty text and a newline to separate top vs bottom."
|
2020-04-28 15:32:52 +00:00
|
|
|
p.bot.Send(c, bot.Message, message.Channel, msg)
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *MemePlugin) registerWeb(c bot.Connector) {
|
2020-05-01 16:27:47 +00:00
|
|
|
http.HandleFunc("/slash/meme", p.slashMeme(c))
|
|
|
|
http.HandleFunc("/meme/img/", p.img)
|
|
|
|
http.HandleFunc("/meme/all", p.all)
|
|
|
|
http.HandleFunc("/meme/add", p.addMeme)
|
2020-05-05 20:23:12 +00:00
|
|
|
http.HandleFunc("/meme/rm", p.rmMeme)
|
2020-05-01 16:27:47 +00:00
|
|
|
http.HandleFunc("/meme", p.webRoot)
|
|
|
|
p.bot.RegisterWeb("/meme", "Memes")
|
|
|
|
}
|
|
|
|
|
|
|
|
type webResp struct {
|
|
|
|
Name string `json:"name"`
|
|
|
|
URL string `json:"url"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type webResps []webResp
|
|
|
|
|
|
|
|
func (w webResps) Len() int { return len(w) }
|
|
|
|
func (w webResps) Swap(i, j int) { w[i], w[j] = w[j], w[i] }
|
|
|
|
|
|
|
|
type ByName struct{ webResps }
|
|
|
|
|
|
|
|
func (s ByName) Less(i, j int) bool { return s.webResps[i].Name < s.webResps[j].Name }
|
|
|
|
|
|
|
|
func (p *MemePlugin) all(w http.ResponseWriter, r *http.Request) {
|
|
|
|
memes := p.c.GetMap("meme.memes", defaultFormats)
|
|
|
|
values := webResps{}
|
|
|
|
for n, u := range memes {
|
|
|
|
|
|
|
|
realURL, err := url.Parse(u)
|
|
|
|
if err != nil || realURL.Scheme == "" {
|
|
|
|
realURL, _ = url.Parse("https://imgflip.com/s/meme/" + u)
|
|
|
|
}
|
|
|
|
values = append(values, webResp{n, realURL.String()})
|
|
|
|
}
|
2020-05-05 20:23:12 +00:00
|
|
|
sort.Sort(ByName{values})
|
2020-05-01 16:27:47 +00:00
|
|
|
|
|
|
|
out, err := json.Marshal(values)
|
|
|
|
if err != nil {
|
|
|
|
w.WriteHeader(500)
|
|
|
|
log.Error().Err(err).Msgf("could not serve all memes route")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
w.Write(out)
|
|
|
|
}
|
|
|
|
|
2020-05-05 20:23:12 +00:00
|
|
|
func mkCheckError(w http.ResponseWriter) func(error) bool {
|
|
|
|
return func(err error) bool {
|
2020-05-01 16:27:47 +00:00
|
|
|
if err != nil {
|
2020-05-05 20:23:12 +00:00
|
|
|
log.Error().Err(err).Msgf("meme failed")
|
2020-05-01 16:27:47 +00:00
|
|
|
w.WriteHeader(500)
|
|
|
|
e, _ := json.Marshal(err)
|
|
|
|
w.Write(e)
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
2020-05-05 20:23:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p *MemePlugin) rmMeme(w http.ResponseWriter, r *http.Request) {
|
|
|
|
if r.Method != http.MethodDelete {
|
|
|
|
w.WriteHeader(405)
|
|
|
|
fmt.Fprintf(w, "Incorrect HTTP method")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
checkError := mkCheckError(w)
|
|
|
|
decoder := json.NewDecoder(r.Body)
|
|
|
|
values := webResp{}
|
|
|
|
err := decoder.Decode(&values)
|
|
|
|
if checkError(err) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
formats := p.c.GetMap("meme.memes", defaultFormats)
|
|
|
|
delete(formats, values.Name)
|
|
|
|
err = p.c.SetMap("meme.memes", formats)
|
|
|
|
checkError(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *MemePlugin) addMeme(w http.ResponseWriter, r *http.Request) {
|
|
|
|
if r.Method != http.MethodPost {
|
|
|
|
w.WriteHeader(405)
|
|
|
|
fmt.Fprintf(w, "Incorrect HTTP method")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
checkError := mkCheckError(w)
|
2020-05-01 16:27:47 +00:00
|
|
|
decoder := json.NewDecoder(r.Body)
|
|
|
|
values := webResp{}
|
|
|
|
err := decoder.Decode(&values)
|
|
|
|
if checkError(err) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
formats := p.c.GetMap("meme.memes", defaultFormats)
|
|
|
|
formats[values.Name] = values.URL
|
|
|
|
err = p.c.SetMap("meme.memes", formats)
|
|
|
|
checkError(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *MemePlugin) webRoot(w http.ResponseWriter, r *http.Request) {
|
|
|
|
var tpl = template.Must(template.New("factoidIndex").Parse(string(memeIndex)))
|
|
|
|
tpl.Execute(w, struct{ Nav []bot.EndPoint }{p.bot.GetWebNavigation()})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *MemePlugin) img(w http.ResponseWriter, r *http.Request) {
|
|
|
|
_, file := path.Split(r.URL.Path)
|
|
|
|
id := file
|
|
|
|
if img, ok := p.images[id]; ok {
|
|
|
|
w.Write(img.repr)
|
|
|
|
} else {
|
|
|
|
w.WriteHeader(404)
|
|
|
|
w.Write([]byte("not found"))
|
|
|
|
}
|
|
|
|
p.images.cleanup()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *MemePlugin) slashMeme(c bot.Connector) http.HandlerFunc {
|
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
2020-04-28 15:32:52 +00:00
|
|
|
r.ParseForm()
|
|
|
|
log.Debug().Msgf("Meme:\n%+v", r.PostForm.Get("text"))
|
|
|
|
channel := r.PostForm.Get("channel_id")
|
2020-04-29 14:57:00 +00:00
|
|
|
channelName := r.PostForm.Get("channel_name")
|
|
|
|
from := r.PostForm.Get("user_name")
|
2020-05-01 13:11:34 +00:00
|
|
|
text := r.PostForm.Get("text")
|
2020-04-28 15:32:52 +00:00
|
|
|
log.Debug().Msgf("channel: %s", channel)
|
|
|
|
|
2020-05-01 13:11:34 +00:00
|
|
|
parts := strings.SplitN(text, " ", 2)
|
|
|
|
if len(parts) != 2 {
|
|
|
|
log.Debug().Msgf("Bad meme request: %s, %s", from, text)
|
|
|
|
p.bot.Send(c, bot.Message, channel, fmt.Sprintf("%s tried to send me a bad meme request.", from))
|
|
|
|
return
|
|
|
|
}
|
2020-04-29 14:57:00 +00:00
|
|
|
isCmd, message := bot.IsCmd(p.c, parts[1])
|
2020-04-29 15:58:14 +00:00
|
|
|
format := parts[0]
|
2020-04-29 14:57:00 +00:00
|
|
|
|
2020-05-05 20:23:12 +00:00
|
|
|
for _, bully := range p.c.GetArray("meme.bully", []string{}) {
|
|
|
|
if format == bully {
|
|
|
|
formats := p.c.GetMap("meme.memes", defaultFormats)
|
|
|
|
format = randEntry(formats)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-01 13:11:34 +00:00
|
|
|
log.Debug().Strs("parts", parts).Msgf("Meme:\n%+v", text)
|
2020-04-28 15:32:52 +00:00
|
|
|
w.WriteHeader(200)
|
|
|
|
w.Write(nil)
|
2020-04-29 14:57:00 +00:00
|
|
|
|
2020-04-29 19:35:13 +00:00
|
|
|
go func() {
|
|
|
|
top, bottom := "", message
|
|
|
|
parts = strings.Split(message, "\n")
|
|
|
|
if len(parts) > 1 {
|
|
|
|
top, bottom = parts[0], parts[1]
|
|
|
|
}
|
|
|
|
|
2020-05-04 21:29:06 +00:00
|
|
|
if top == "_" {
|
|
|
|
message = bottom
|
|
|
|
} else if bottom == "_" {
|
|
|
|
message = top
|
|
|
|
}
|
|
|
|
|
2020-05-01 14:39:42 +00:00
|
|
|
id, err := p.genMeme(format, top, bottom)
|
|
|
|
if err != nil {
|
|
|
|
msg := fmt.Sprintf("Hey %s, I couldn't download that image you asked for.", from)
|
|
|
|
p.bot.Send(c, bot.Message, channel, msg)
|
|
|
|
return
|
|
|
|
}
|
2020-04-29 19:35:13 +00:00
|
|
|
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)
|
|
|
|
}()
|
2020-05-01 16:27:47 +00:00
|
|
|
}
|
2020-04-28 15:32:52 +00:00
|
|
|
}
|
|
|
|
|
2020-05-05 20:23:12 +00:00
|
|
|
func randEntry(m map[string]string) string {
|
|
|
|
for _, e := range m {
|
|
|
|
return e
|
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2020-05-01 14:39:42 +00:00
|
|
|
func DownloadTemplate(u *url.URL) (image.Image, error) {
|
2020-04-28 18:58:23 +00:00
|
|
|
res, err := http.Get(u.String())
|
2020-04-28 15:32:52 +00:00
|
|
|
if err != nil {
|
2020-04-28 18:58:23 +00:00
|
|
|
log.Error().Msgf("template from %s failed because of %v", u.String(), err)
|
2020-05-01 14:39:42 +00:00
|
|
|
return nil, err
|
2020-04-28 15:32:52 +00:00
|
|
|
}
|
|
|
|
defer res.Body.Close()
|
|
|
|
image, _, err := image.Decode(res.Body)
|
|
|
|
if err != nil {
|
2020-04-28 18:58:23 +00:00
|
|
|
log.Error().Msgf("Could not decode %v because of %v", u, err)
|
2020-05-01 14:39:42 +00:00
|
|
|
return nil, err
|
2020-04-28 15:32:52 +00:00
|
|
|
}
|
2020-05-01 14:39:42 +00:00
|
|
|
return image, nil
|
2020-04-28 15:32:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var defaultFormats = map[string]string{
|
|
|
|
"fry": "Futurama-Fry.jpg",
|
|
|
|
"aliens": "Ancient-Aliens.jpg",
|
|
|
|
"doge": "Doge.jpg",
|
|
|
|
"simply": "One-Does-Not-Simply.jpg",
|
|
|
|
"wonka": "Creepy-Condescending-Wonka.jpg",
|
|
|
|
"grumpy": "Grumpy-Cat.jpg",
|
|
|
|
"raptor": "Philosoraptor.jpg",
|
|
|
|
}
|
|
|
|
|
2020-05-01 14:39:42 +00:00
|
|
|
func (p *MemePlugin) genMeme(meme, top, bottom string) (string, error) {
|
2020-04-29 16:09:05 +00:00
|
|
|
fontSizes := []float64{48, 36, 24, 16, 12}
|
2020-04-29 15:58:14 +00:00
|
|
|
fontSize := fontSizes[0]
|
2020-04-28 15:32:52 +00:00
|
|
|
|
|
|
|
formats := p.c.GetMap("meme.memes", defaultFormats)
|
|
|
|
|
|
|
|
path := uuid.New().String()
|
|
|
|
|
|
|
|
imgName, ok := formats[meme]
|
|
|
|
if !ok {
|
|
|
|
imgName = meme
|
|
|
|
}
|
2020-04-28 18:58:23 +00:00
|
|
|
|
|
|
|
u, err := url.Parse(imgName)
|
2020-04-28 19:16:45 +00:00
|
|
|
if err != nil || u.Scheme == "" {
|
|
|
|
log.Debug().Err(err).Str("imgName", imgName).Msgf("url not detected")
|
2020-04-28 18:58:23 +00:00
|
|
|
u, _ = url.Parse("https://imgflip.com/s/meme/" + imgName)
|
|
|
|
}
|
|
|
|
|
2020-04-28 19:16:45 +00:00
|
|
|
log.Debug().Msgf("Attempting to download url: %s", u.String())
|
|
|
|
|
2020-05-01 14:39:42 +00:00
|
|
|
img, err := DownloadTemplate(u)
|
|
|
|
if err != nil {
|
|
|
|
log.Debug().Msgf("failed to download image: %s", err)
|
|
|
|
return "", err
|
|
|
|
}
|
2020-04-29 19:35:13 +00:00
|
|
|
|
2020-04-28 15:32:52 +00:00
|
|
|
r := img.Bounds()
|
|
|
|
w := r.Dx()
|
|
|
|
h := r.Dy()
|
|
|
|
|
2020-04-29 19:35:13 +00:00
|
|
|
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)
|
|
|
|
|
2020-04-28 15:32:52 +00:00
|
|
|
m := gg.NewContext(w, h)
|
|
|
|
m.DrawImage(img, 0, 0)
|
|
|
|
fontLocation := p.c.Get("meme.font", "impact.ttf")
|
2020-04-29 15:58:14 +00:00
|
|
|
for _, sz := range fontSizes {
|
|
|
|
err = m.LoadFontFace(fontLocation, sz) // problem
|
|
|
|
if err != nil {
|
|
|
|
log.Error().Err(err).Msg("could not load font")
|
|
|
|
}
|
|
|
|
topW, _ := m.MeasureString(top)
|
|
|
|
botW, _ := m.MeasureString(bottom)
|
|
|
|
if topW < float64(w) && botW < float64(w) {
|
|
|
|
fontSize = sz
|
|
|
|
break
|
|
|
|
}
|
2020-04-28 15:32:52 +00:00
|
|
|
}
|
|
|
|
|
2020-05-04 21:29:06 +00:00
|
|
|
if top == "_" {
|
|
|
|
top = ""
|
|
|
|
}
|
|
|
|
if bottom == "_" {
|
|
|
|
bottom = ""
|
|
|
|
}
|
|
|
|
|
2020-04-28 15:32:52 +00:00
|
|
|
// Apply black stroke
|
|
|
|
m.SetHexColor("#000")
|
|
|
|
strokeSize := 6
|
|
|
|
for dy := -strokeSize; dy <= strokeSize; dy++ {
|
|
|
|
for dx := -strokeSize; dx <= strokeSize; dx++ {
|
|
|
|
// give it rounded corners
|
|
|
|
if dx*dx+dy*dy >= strokeSize*strokeSize {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
x := float64(w/2 + dx)
|
2020-04-29 15:58:14 +00:00
|
|
|
y := float64(h) - fontSize + float64(dy)
|
2020-04-29 16:09:05 +00:00
|
|
|
y0 := fontSize + float64(dy)
|
|
|
|
m.DrawStringAnchored(top, x, y0, 0.5, 0.5)
|
2020-04-29 15:58:14 +00:00
|
|
|
m.DrawStringAnchored(bottom, x, y, 0.5, 0.5)
|
2020-04-28 15:32:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Apply white fill
|
|
|
|
m.SetHexColor("#FFF")
|
2020-04-29 15:58:14 +00:00
|
|
|
m.DrawStringAnchored(top, float64(w)/2, fontSize, 0.5, 0.5)
|
|
|
|
m.DrawStringAnchored(bottom, float64(w)/2, float64(h)-fontSize, 0.5, 0.5)
|
2020-04-28 15:32:52 +00:00
|
|
|
|
|
|
|
i := bytes.Buffer{}
|
|
|
|
png.Encode(&i, m.Image())
|
2020-04-29 15:29:54 +00:00
|
|
|
p.images[path] = &cachedImage{time.Now(), i.Bytes()}
|
2020-04-28 15:32:52 +00:00
|
|
|
|
|
|
|
log.Debug().Msgf("Saved to %s\n", path)
|
|
|
|
|
2020-05-01 14:39:42 +00:00
|
|
|
return path, nil
|
2020-04-28 15:32:52 +00:00
|
|
|
}
|