diff --git a/plugins/emojy/emojy.go b/plugins/emojy/emojy.go index fc94512..d28211a 100644 --- a/plugins/emojy/emojy.go +++ b/plugins/emojy/emojy.go @@ -74,28 +74,23 @@ func (p *EmojyPlugin) register() { return false }, }, + { + Kind: bot.Message, IsCmd: true, + Regex: regexp.MustCompile(`(?i)^swapemojy (?P.+) (?P.+)$`), + 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.+)$`), 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.+)$`), 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()) diff --git a/plugins/emojy/web.go b/plugins/emojy/web.go index 7c16156..9275269 100644 --- a/plugins/emojy/web.go +++ b/plugins/emojy/web.go @@ -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") {