emojy: add swap and disallow upload overwrites

This commit is contained in:
Chris Sexton 2022-06-08 21:21:59 -04:00
parent feb42b8293
commit 56120b168c
2 changed files with 51 additions and 28 deletions

View File

@ -74,28 +74,23 @@ func (p *EmojyPlugin) register() {
return false return false
}, },
}, },
{
Kind: bot.Message, IsCmd: true,
Regex: regexp.MustCompile(`(?i)^swapemojy (?P<old>.+) (?P<new>.+)$`),
Handler: func(r bot.Request) bool {
old := sanitizeName(r.Values["old"])
new := sanitizeName(r.Values["new"])
p.rmEmojy(r, old)
p.addEmojy(r, new)
return true
},
},
{ {
Kind: bot.Message, IsCmd: true, Kind: bot.Message, IsCmd: true,
Regex: regexp.MustCompile(`(?i)^addemojy (?P<name>.+)$`), Regex: regexp.MustCompile(`(?i)^addemojy (?P<name>.+)$`),
Handler: func(r bot.Request) bool { Handler: func(r bot.Request) bool {
name := sanitizeName(r.Values["name"]) name := sanitizeName(r.Values["name"])
onServerList := invertEmojyList(p.b.GetEmojiList(false)) return p.addEmojy(r, name)
if _, ok := onServerList[name]; ok {
p.b.Send(r.Conn, bot.Message, r.Msg.Channel, "Emoji already exists")
return true
}
if err := p.uploadEmojy(r.Conn, name); err != nil {
p.b.Send(r.Conn, bot.Message, r.Msg.Channel, fmt.Sprintf("error adding emojy: %v", err))
return true
}
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
}, },
}, },
{ {
@ -103,23 +98,47 @@ func (p *EmojyPlugin) register() {
Regex: regexp.MustCompile(`(?i)^rmemojy (?P<name>.+)$`), Regex: regexp.MustCompile(`(?i)^rmemojy (?P<name>.+)$`),
Handler: func(r bot.Request) bool { Handler: func(r bot.Request) bool {
name := sanitizeName(r.Values["name"]) name := sanitizeName(r.Values["name"])
onServerList := invertEmojyList(p.b.GetEmojiList(false)) return p.rmEmojy(r, name)
if _, ok := onServerList[name]; !ok {
p.b.Send(r.Conn, bot.Message, r.Msg.Channel, "Emoji does not exist")
return true
}
if err := r.Conn.DeleteEmojy(name); err != nil {
p.b.Send(r.Conn, bot.Message, r.Msg.Channel, "error "+err.Error())
return true
}
p.b.Send(r.Conn, bot.Message, r.Msg.Channel, "removed emojy "+name)
return true
}, },
}, },
} }
p.b.RegisterTable(p, ht) p.b.RegisterTable(p, ht)
} }
func (p *EmojyPlugin) rmEmojy(r bot.Request, name string) bool {
onServerList := invertEmojyList(p.b.GetEmojiList(false))
if _, ok := onServerList[name]; !ok {
p.b.Send(r.Conn, bot.Message, r.Msg.Channel, "Emoji does not exist")
return true
}
if err := r.Conn.DeleteEmojy(name); err != nil {
p.b.Send(r.Conn, bot.Message, r.Msg.Channel, "error "+err.Error())
return true
}
p.b.Send(r.Conn, bot.Message, r.Msg.Channel, "removed emojy "+name)
return true
}
func (p *EmojyPlugin) addEmojy(r bot.Request, name string) bool {
onServerList := invertEmojyList(p.b.GetEmojiList(false))
if _, ok := onServerList[name]; ok {
p.b.Send(r.Conn, bot.Message, r.Msg.Channel, "Emoji already exists")
return true
}
if err := p.uploadEmojy(r.Conn, name); err != nil {
p.b.Send(r.Conn, bot.Message, r.Msg.Channel, fmt.Sprintf("error adding emojy: %v", err))
return true
}
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
}
func (p *EmojyPlugin) recordReaction(emojy string) error { func (p *EmojyPlugin) recordReaction(emojy string) error {
q := `insert into emojyLog (emojy, observed) values (?, ?)` q := `insert into emojyLog (emojy, observed) values (?, ?)`
_, err := p.db.Exec(q, emojy, time.Now()) _, err := p.db.Exec(q, emojy, time.Now())

View File

@ -102,6 +102,10 @@ func (p *EmojyPlugin) FileSave(r *http.Request) (string, error) {
return "", fmt.Errorf("error opening part %q: %s", fileHeader.Filename, err) return "", fmt.Errorf("error opening part %q: %s", fileHeader.Filename, err)
} }
emojyFileName := fileHeader.Filename emojyFileName := fileHeader.Filename
emojyName := strings.TrimSuffix(emojyFileName, filepath.Ext(emojyFileName))
if ok, _, _, _ := p.isKnownEmojy(emojyName); ok {
return "", fmt.Errorf("emojy already exists")
}
emojyPath := p.c.Get("emojy.path", "emojy") emojyPath := p.c.Get("emojy.path", "emojy")
contentType := fileHeader.Header.Get("Content-Type") contentType := fileHeader.Header.Get("Content-Type")
if !strings.HasPrefix(contentType, "image") { if !strings.HasPrefix(contentType, "image") {