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,11 +74,52 @@ 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"])
return p.addEmojy(r, name)
},
},
{
Kind: bot.Message, IsCmd: true,
Regex: regexp.MustCompile(`(?i)^rmemojy (?P<name>.+)$`),
Handler: func(r bot.Request) bool {
name := sanitizeName(r.Values["name"])
return p.rmEmojy(r, name)
},
},
}
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)) onServerList := invertEmojyList(p.b.GetEmojiList(false))
if _, ok := onServerList[name]; ok { if _, ok := onServerList[name]; ok {
p.b.Send(r.Conn, bot.Message, r.Msg.Channel, "Emoji already exists") p.b.Send(r.Conn, bot.Message, r.Msg.Channel, "Emoji already exists")
@ -96,28 +137,6 @@ func (p *EmojyPlugin) register() {
} }
} }
return true return true
},
},
{
Kind: bot.Message, IsCmd: true,
Regex: regexp.MustCompile(`(?i)^rmemojy (?P<name>.+)$`),
Handler: func(r bot.Request) bool {
name := sanitizeName(r.Values["name"])
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
},
},
}
p.b.RegisterTable(p, ht)
} }
func (p *EmojyPlugin) recordReaction(emojy string) error { func (p *EmojyPlugin) recordReaction(emojy string) error {

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") {