2022-05-30 20:34:42 +00:00
|
|
|
package emojy
|
|
|
|
|
|
|
|
import (
|
2022-06-08 20:57:24 +00:00
|
|
|
"bytes"
|
|
|
|
"encoding/base64"
|
|
|
|
"fmt"
|
2022-07-23 04:17:44 +00:00
|
|
|
"image"
|
|
|
|
"image/draw"
|
2022-06-08 20:57:24 +00:00
|
|
|
"math"
|
2022-06-06 14:20:50 +00:00
|
|
|
"os"
|
|
|
|
"path"
|
2022-06-08 20:57:24 +00:00
|
|
|
"path/filepath"
|
2022-05-30 20:34:42 +00:00
|
|
|
"regexp"
|
2022-06-06 14:20:50 +00:00
|
|
|
"strings"
|
2022-06-02 15:00:48 +00:00
|
|
|
"time"
|
2022-06-08 20:57:24 +00:00
|
|
|
|
2022-07-21 15:25:10 +00:00
|
|
|
"github.com/fogleman/gg"
|
|
|
|
"github.com/gabriel-vasile/mimetype"
|
|
|
|
|
2022-06-08 20:57:24 +00:00
|
|
|
"github.com/forPelevin/gomoji"
|
|
|
|
"github.com/jmoiron/sqlx"
|
|
|
|
"github.com/rs/zerolog/log"
|
|
|
|
"github.com/velour/catbase/bot"
|
|
|
|
"github.com/velour/catbase/config"
|
2022-05-30 20:34:42 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type EmojyPlugin struct {
|
|
|
|
b bot.Bot
|
|
|
|
c *config.Config
|
|
|
|
db *sqlx.DB
|
2022-07-22 13:44:42 +00:00
|
|
|
|
|
|
|
emojyPath string
|
|
|
|
baseURL string
|
2022-05-30 20:34:42 +00:00
|
|
|
}
|
|
|
|
|
2022-06-08 20:57:24 +00:00
|
|
|
const maxLen = 32
|
|
|
|
|
2022-05-30 20:34:42 +00:00
|
|
|
func New(b bot.Bot) *EmojyPlugin {
|
2022-07-23 04:17:44 +00:00
|
|
|
p := NewAPI(b)
|
|
|
|
p.register()
|
|
|
|
p.registerWeb()
|
|
|
|
return p
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewAPI creates a version used only for API purposes (no callbacks registered)
|
|
|
|
func NewAPI(b bot.Bot) *EmojyPlugin {
|
2022-07-22 13:44:42 +00:00
|
|
|
emojyPath := b.Config().Get("emojy.path", "emojy")
|
|
|
|
baseURL := b.Config().Get("emojy.baseURL", "/emojy/file")
|
2022-05-30 20:34:42 +00:00
|
|
|
p := &EmojyPlugin{
|
2022-07-22 13:44:42 +00:00
|
|
|
b: b,
|
|
|
|
c: b.Config(),
|
|
|
|
db: b.DB(),
|
|
|
|
emojyPath: emojyPath,
|
|
|
|
baseURL: baseURL,
|
2022-05-30 20:34:42 +00:00
|
|
|
}
|
|
|
|
p.setupDB()
|
|
|
|
return p
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *EmojyPlugin) setupDB() {
|
2022-06-02 15:00:48 +00:00
|
|
|
p.db.MustExec(`create table if not exists emojyLog (
|
|
|
|
id integer primary key autoincrement,
|
|
|
|
emojy text,
|
|
|
|
observed datetime
|
2022-05-30 20:34:42 +00:00
|
|
|
)`)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *EmojyPlugin) register() {
|
|
|
|
ht := bot.HandlerTable{
|
2022-06-06 14:20:50 +00:00
|
|
|
{
|
|
|
|
Kind: bot.Message, IsCmd: false,
|
|
|
|
Regex: regexp.MustCompile(`.*`),
|
|
|
|
Handler: func(request bot.Request) bool {
|
|
|
|
r := regexp.MustCompile(`:[a-zA-Z0-9_-]+:`)
|
|
|
|
for _, match := range r.FindAllString(request.Msg.Body, -1) {
|
2022-06-07 14:27:17 +00:00
|
|
|
match = strings.Trim(match, ":")
|
2022-06-06 14:20:50 +00:00
|
|
|
log.Debug().Msgf("Emojy detected: %s", match)
|
|
|
|
p.recordReaction(match)
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
},
|
|
|
|
},
|
2022-05-30 20:34:42 +00:00
|
|
|
{
|
|
|
|
Kind: bot.Reaction, IsCmd: false,
|
|
|
|
Regex: regexp.MustCompile(`.*`),
|
|
|
|
Handler: func(request bot.Request) bool {
|
|
|
|
log.Debug().Msgf("Emojy detected: %s", request.Msg.Body)
|
|
|
|
p.recordReaction(request.Msg.Body)
|
|
|
|
return false
|
|
|
|
},
|
|
|
|
},
|
2022-06-09 01:21:59 +00:00
|
|
|
{
|
|
|
|
Kind: bot.Message, IsCmd: true,
|
|
|
|
Regex: regexp.MustCompile(`(?i)^swapemojy (?P<old>.+) (?P<new>.+)$`),
|
|
|
|
Handler: func(r bot.Request) bool {
|
2022-07-23 04:17:44 +00:00
|
|
|
old := SanitizeName(r.Values["old"])
|
|
|
|
new := SanitizeName(r.Values["new"])
|
|
|
|
p.rmEmojyHandler(r, old)
|
|
|
|
p.addEmojyHandler(r, new)
|
2022-06-09 01:21:59 +00:00
|
|
|
return true
|
|
|
|
},
|
|
|
|
},
|
2022-06-08 20:57:24 +00:00
|
|
|
{
|
|
|
|
Kind: bot.Message, IsCmd: true,
|
|
|
|
Regex: regexp.MustCompile(`(?i)^addemojy (?P<name>.+)$`),
|
|
|
|
Handler: func(r bot.Request) bool {
|
2022-07-23 04:17:44 +00:00
|
|
|
name := SanitizeName(r.Values["name"])
|
|
|
|
return p.addEmojyHandler(r, name)
|
2022-06-08 20:57:24 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Kind: bot.Message, IsCmd: true,
|
|
|
|
Regex: regexp.MustCompile(`(?i)^rmemojy (?P<name>.+)$`),
|
|
|
|
Handler: func(r bot.Request) bool {
|
2022-07-23 04:17:44 +00:00
|
|
|
name := SanitizeName(r.Values["name"])
|
|
|
|
return p.rmEmojyHandler(r, name)
|
2022-06-08 20:57:24 +00:00
|
|
|
},
|
|
|
|
},
|
2022-05-30 20:34:42 +00:00
|
|
|
}
|
|
|
|
p.b.RegisterTable(p, ht)
|
|
|
|
}
|
|
|
|
|
2022-07-23 04:17:44 +00:00
|
|
|
func (p *EmojyPlugin) RmEmojy(c bot.Connector, name string) error {
|
2022-06-09 01:21:59 +00:00
|
|
|
onServerList := invertEmojyList(p.b.GetEmojiList(false))
|
2022-07-23 04:17:44 +00:00
|
|
|
// Call a non-existent emojy a successful remove
|
2022-06-09 01:21:59 +00:00
|
|
|
if _, ok := onServerList[name]; !ok {
|
2022-07-23 04:17:44 +00:00
|
|
|
return fmt.Errorf("could not find emojy %s", name)
|
2022-06-09 01:21:59 +00:00
|
|
|
}
|
2022-07-23 04:17:44 +00:00
|
|
|
if err := c.DeleteEmojy(name); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *EmojyPlugin) rmEmojyHandler(r bot.Request, name string) bool {
|
|
|
|
err := p.RmEmojy(r.Conn, name)
|
|
|
|
if err != nil {
|
|
|
|
p.b.Send(r.Conn, bot.Message, r.Msg.Channel, "Emoji does not exist")
|
2022-06-09 01:21:59 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
p.b.Send(r.Conn, bot.Message, r.Msg.Channel, "removed emojy "+name)
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2022-07-23 04:17:44 +00:00
|
|
|
func (p *EmojyPlugin) AddEmojy(c bot.Connector, name string) error {
|
2022-06-09 01:21:59 +00:00
|
|
|
onServerList := invertEmojyList(p.b.GetEmojiList(false))
|
|
|
|
if _, ok := onServerList[name]; ok {
|
2022-07-23 04:17:44 +00:00
|
|
|
return fmt.Errorf("emojy already exists")
|
2022-06-09 01:21:59 +00:00
|
|
|
}
|
2022-07-23 04:17:44 +00:00
|
|
|
if err := p.UploadEmojyInCache(c, name); err != nil {
|
|
|
|
return err
|
2022-06-09 01:21:59 +00:00
|
|
|
}
|
2022-07-23 04:17:44 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *EmojyPlugin) addEmojyHandler(r bot.Request, name string) bool {
|
|
|
|
p.AddEmojy(r.Conn, name)
|
2022-06-09 01:21:59 +00:00
|
|
|
list := r.Conn.GetEmojiList(true)
|
|
|
|
for k, v := range list {
|
|
|
|
if v == name {
|
|
|
|
p.b.Send(r.Conn, bot.Message, r.Msg.Channel, fmt.Sprintf("added emojy: %s <:%s:%s>", name, name, k))
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2022-05-30 20:34:42 +00:00
|
|
|
func (p *EmojyPlugin) recordReaction(emojy string) error {
|
2022-06-02 15:00:48 +00:00
|
|
|
q := `insert into emojyLog (emojy, observed) values (?, ?)`
|
|
|
|
_, err := p.db.Exec(q, emojy, time.Now())
|
2022-05-30 20:34:42 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Error().Err(err).Msgf("recordReaction")
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type EmojyEntry struct {
|
2022-06-02 15:00:48 +00:00
|
|
|
Emojy string `db:"emojy"`
|
|
|
|
Observed time.Time `db:"observed"`
|
2022-05-30 20:34:42 +00:00
|
|
|
}
|
|
|
|
|
2022-06-02 15:00:48 +00:00
|
|
|
type EmojyCount struct {
|
2022-06-06 14:47:11 +00:00
|
|
|
Emojy string `json:"emojy"`
|
|
|
|
URL string `json:"url"`
|
|
|
|
Count int `json:"count"`
|
|
|
|
OnServer bool `json:"onServer"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func invertEmojyList(emojy map[string]string) map[string]string {
|
|
|
|
out := map[string]string{}
|
|
|
|
for k, v := range emojy {
|
|
|
|
out[v] = k
|
|
|
|
}
|
|
|
|
return out
|
2022-06-02 15:00:48 +00:00
|
|
|
}
|
|
|
|
|
2022-06-06 14:20:50 +00:00
|
|
|
func (p *EmojyPlugin) allCounts() (map[string][]EmojyCount, error) {
|
|
|
|
out := map[string][]EmojyCount{}
|
2022-06-08 20:57:24 +00:00
|
|
|
onServerList := invertEmojyList(p.b.GetEmojiList(true))
|
2022-06-02 15:00:48 +00:00
|
|
|
q := `select emojy, count(observed) as count from emojyLog group by emojy order by count desc`
|
|
|
|
result := []EmojyCount{}
|
2022-05-30 20:34:42 +00:00
|
|
|
err := p.db.Select(&result, q)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-06-06 14:20:50 +00:00
|
|
|
for _, e := range result {
|
2022-06-06 14:47:11 +00:00
|
|
|
_, e.OnServer = onServerList[e.Emojy]
|
2022-06-06 14:20:50 +00:00
|
|
|
if isEmoji(e.Emojy) {
|
|
|
|
out["emoji"] = append(out["emoji"], e)
|
2022-06-08 20:57:24 +00:00
|
|
|
} else if ok, _, eURL, _ := p.isKnownEmojy(e.Emojy); ok {
|
|
|
|
e.URL = eURL
|
2022-06-06 14:20:50 +00:00
|
|
|
out["emojy"] = append(out["emojy"], e)
|
|
|
|
} else {
|
|
|
|
out["unknown"] = append(out["unknown"], e)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return out, nil
|
|
|
|
}
|
|
|
|
|
2022-07-22 13:44:42 +00:00
|
|
|
func AllFiles(emojyPath, baseURL string) (map[string]string, map[string]string, error) {
|
2022-06-08 20:57:24 +00:00
|
|
|
files := map[string]string{}
|
|
|
|
urls := map[string]string{}
|
2022-06-06 14:20:50 +00:00
|
|
|
entries, err := os.ReadDir(emojyPath)
|
|
|
|
if err != nil {
|
2022-06-08 20:57:24 +00:00
|
|
|
return nil, nil, err
|
2022-06-06 14:20:50 +00:00
|
|
|
}
|
|
|
|
for _, e := range entries {
|
2022-06-08 20:57:24 +00:00
|
|
|
if !e.IsDir() {
|
|
|
|
baseName := strings.TrimSuffix(e.Name(), filepath.Ext(e.Name()))
|
2022-06-06 14:20:50 +00:00
|
|
|
url := path.Join(baseURL, e.Name())
|
2022-06-08 20:57:24 +00:00
|
|
|
files[baseName] = path.Join(emojyPath, e.Name())
|
|
|
|
urls[baseName] = url
|
2022-06-06 14:20:50 +00:00
|
|
|
}
|
|
|
|
}
|
2022-06-08 20:57:24 +00:00
|
|
|
return files, urls, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *EmojyPlugin) isKnownEmojy(name string) (bool, string, string, error) {
|
2022-07-22 13:44:42 +00:00
|
|
|
allFiles, allURLs, err := AllFiles(p.emojyPath, p.baseURL)
|
2022-06-08 20:57:24 +00:00
|
|
|
if err != nil {
|
|
|
|
return false, "", "", err
|
|
|
|
}
|
|
|
|
if _, ok := allURLs[name]; !ok {
|
|
|
|
return false, "", "", nil
|
|
|
|
}
|
|
|
|
return true, allFiles[name], allURLs[name], nil
|
2022-06-06 14:20:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func isEmoji(in string) bool {
|
|
|
|
return gomoji.ContainsEmoji(in)
|
2022-05-30 20:34:42 +00:00
|
|
|
}
|
2022-06-08 20:57:24 +00:00
|
|
|
|
2022-07-23 04:17:44 +00:00
|
|
|
func (p *EmojyPlugin) UploadEmojyInCache(c bot.Connector, name string) error {
|
2022-06-08 20:57:24 +00:00
|
|
|
ok, fname, _, err := p.isKnownEmojy(name)
|
|
|
|
if !ok || err != nil {
|
|
|
|
u := p.c.Get("baseurl", "")
|
|
|
|
u = u + "/emojy"
|
|
|
|
return fmt.Errorf("error getting emojy, the known emojy list can be found at: %s", u)
|
|
|
|
}
|
2022-07-23 04:17:44 +00:00
|
|
|
f, err := os.Open(fname)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
img, _, err := image.Decode(f)
|
2022-06-08 20:57:24 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-07-23 04:17:44 +00:00
|
|
|
return p.UploadEmojyImage(c, name, img)
|
|
|
|
}
|
|
|
|
|
|
|
|
func imageToRGBA(src image.Image) *image.RGBA {
|
|
|
|
// No conversion needed if image is an *image.RGBA.
|
|
|
|
if dst, ok := src.(*image.RGBA); ok {
|
|
|
|
return dst
|
|
|
|
}
|
|
|
|
|
|
|
|
// Use the image/draw package to convert to *image.RGBA.
|
|
|
|
b := src.Bounds()
|
|
|
|
dst := image.NewRGBA(image.Rect(0, 0, b.Dx(), b.Dy()))
|
|
|
|
draw.Draw(dst, dst.Bounds(), src, b.Min, draw.Src)
|
|
|
|
return dst
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *EmojyPlugin) UploadEmojyImage(c bot.Connector, name string, data image.Image) error {
|
|
|
|
maxEmojySz := p.c.GetFloat64("emoji.maxsize", 128.0)
|
|
|
|
ctx := gg.NewContextForRGBA(imageToRGBA(data))
|
2022-06-08 20:57:24 +00:00
|
|
|
max := math.Max(float64(ctx.Width()), float64(ctx.Height()))
|
|
|
|
ctx.Scale(maxEmojySz/max, maxEmojySz/max)
|
|
|
|
w := bytes.NewBuffer([]byte{})
|
2022-07-23 04:17:44 +00:00
|
|
|
err := ctx.EncodePNG(w)
|
2022-06-08 20:57:24 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
mtype := mimetype.Detect(w.Bytes())
|
|
|
|
base64Img := "data:" + mtype.String() + ";base64," + base64.StdEncoding.EncodeToString(w.Bytes())
|
|
|
|
if err := c.UploadEmojy(name, base64Img); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-07-23 04:17:44 +00:00
|
|
|
func SanitizeName(name string) string {
|
2022-06-08 20:57:24 +00:00
|
|
|
name = strings.ReplaceAll(name, "-", "_")
|
|
|
|
nameLen := len(name)
|
|
|
|
if nameLen > maxLen {
|
|
|
|
nameLen = maxLen
|
|
|
|
}
|
|
|
|
return name[:nameLen]
|
|
|
|
}
|