mirror of https://github.com/velour/catbase.git
emojy: add swap and disallow upload overwrites
This commit is contained in:
parent
feb42b8293
commit
56120b168c
|
@ -74,28 +74,23 @@ func (p *EmojyPlugin) register() {
|
|||
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,
|
||||
Regex: regexp.MustCompile(`(?i)^addemojy (?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 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
|
||||
return p.addEmojy(r, name)
|
||||
},
|
||||
},
|
||||
{
|
||||
|
@ -103,23 +98,47 @@ func (p *EmojyPlugin) register() {
|
|||
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
|
||||
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))
|
||||
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 {
|
||||
q := `insert into emojyLog (emojy, observed) values (?, ?)`
|
||||
_, err := p.db.Exec(q, emojy, time.Now())
|
||||
|
|
|
@ -102,6 +102,10 @@ func (p *EmojyPlugin) FileSave(r *http.Request) (string, error) {
|
|||
return "", fmt.Errorf("error opening part %q: %s", fileHeader.Filename, err)
|
||||
}
|
||||
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")
|
||||
contentType := fileHeader.Header.Get("Content-Type")
|
||||
if !strings.HasPrefix(contentType, "image") {
|
||||
|
|
Loading…
Reference in New Issue