Revert back to HearsHandlers - CommandHandlers require valid shellwords

This commit is contained in:
2016-10-19 02:31:32 +01:00
parent c208a0e00c
commit 73cd922f2c
3 changed files with 95 additions and 29 deletions

View File

@@ -7,6 +7,7 @@ import (
"fmt"
"log"
"math/rand"
"regexp"
"sort"
"strconv"
"strings"
@@ -38,27 +39,39 @@ func (h *AddQuoteHandler) Describe() (string, string) {
return "addquote", "Add a quote to the database"
}
func (h *AddQuoteHandler) do(ctx context.Context, w hugot.ResponseWriter, m *hugot.Message, quote string) error {
rsp, err := h.QuoteDB.AddQuote(m.Channel, quote, m.From)
if err != nil {
log.Printf("%s: AddQuote error: %v", m.Channel, err)
return fmt.Errorf("Failed to add quote, sorry!")
}
w.Send(ctx, m.Reply(fmt.Sprintf("Quote %d added", rsp.QuoteId)))
return nil
}
func (h *AddQuoteHandler) Hears() *regexp.Regexp {
return regexp.MustCompile(`^!addquote (.*)$`)
}
func (h *AddQuoteHandler) Command(ctx context.Context, w hugot.ResponseWriter, m *hugot.Message) error {
parts := strings.SplitN(m.Text, " ", 2)
if len(parts) != 2 {
return nil
}
quote, err := h.QuoteDB.AddQuote(m.Channel, parts[1], m.From)
if err != nil {
log.Printf("%s: AddQuote error: %v", m.Channel, err)
return fmt.Errorf("Failed to add quote, sorry!")
}
return h.do(ctx, w, m, parts[1])
}
w.Send(ctx, m.Reply(fmt.Sprintf("Quote %d added", quote.QuoteId)))
return nil
func (h *AddQuoteHandler) Heard(ctx context.Context, w hugot.ResponseWriter, m *hugot.Message, submatches [][]string) {
h.do(ctx, w, m, submatches[0][1])
}
func (h *LastQuoteHandler) Describe() (string, string) {
return "lastquote", "Show the last-added quote"
}
func (h *LastQuoteHandler) Command(ctx context.Context, w hugot.ResponseWriter, m *hugot.Message) error {
func (h *LastQuoteHandler) do(ctx context.Context, w hugot.ResponseWriter, m *hugot.Message) error {
quote, err := h.QuoteDB.FindLastQuote(m.Channel)
if err != nil {
log.Printf("%s: Error in LastQuote: %v", m.Channel, err)
@@ -67,6 +80,19 @@ func (h *LastQuoteHandler) Command(ctx context.Context, w hugot.ResponseWriter,
w.Send(ctx, m.Reply(fmt.Sprintf("Quote %d by %s: %s", quote.QuoteId, quote.Author, quote.Data)))
return nil
}
func (h *LastQuoteHandler) Command(ctx context.Context, w hugot.ResponseWriter, m *hugot.Message) error {
return h.do(ctx, w, m)
}
func (h *LastQuoteHandler) Hears() *regexp.Regexp {
return regexp.MustCompile(`^!lastquote$`)
}
func (h *LastQuoteHandler) Heard(ctx context.Context, w hugot.ResponseWriter, m *hugot.Message, submatches [][]string) {
h.do(ctx, w, m)
}
func (h *FindQuoteHandler) Describe() (string, string) {
@@ -117,21 +143,33 @@ func (h *FindQuoteHandler) find(channel, term string) string {
return fmt.Sprintf("%s matches. IDs: %s. Most recent: %s: %s", quoteCount, idStr, quote.Author, quote.Data)
}
func (h *FindQuoteHandler) do(ctx context.Context, w hugot.ResponseWriter, m *hugot.Message, term string) error {
response := h.find(m.Channel, term)
w.Send(ctx, m.Reply(response))
return nil
}
func (h *FindQuoteHandler) Command(ctx context.Context, w hugot.ResponseWriter, m *hugot.Message) error {
parts := strings.SplitN(m.Text, " ", 2)
if len(parts) != 2 {
return nil
}
return h.do(ctx, w, m, parts[1])
}
w.Send(ctx, m.Reply(h.find(m.Channel, parts[1])))
return nil
func (h *FindQuoteHandler) Hears() *regexp.Regexp {
return regexp.MustCompile(`^!findquote (.*)$`)
}
func (h *FindQuoteHandler) Heard(ctx context.Context, w hugot.ResponseWriter, m *hugot.Message, submatches [][]string) {
h.do(ctx, w, m, submatches[0][1])
}
func (h *RandQuoteHandler) Describe() (string, string) {
return "randquote", "Show a random quote"
}
func (h *RandQuoteHandler) Command(ctx context.Context, w hugot.ResponseWriter, m *hugot.Message) error {
func (h *RandQuoteHandler) do(ctx context.Context, w hugot.ResponseWriter, m *hugot.Message) error {
quote, err := h.QuoteDB.FindRandomQuote(m.Channel)
if err != nil {
log.Printf("%s: Error in RandQuote: %v", m.Channel, err)
@@ -142,6 +180,18 @@ func (h *RandQuoteHandler) Command(ctx context.Context, w hugot.ResponseWriter,
return nil
}
func (h *RandQuoteHandler) Command(ctx context.Context, w hugot.ResponseWriter, m *hugot.Message) error {
return h.do(ctx, w, m)
}
func (h *RandQuoteHandler) Hears() *regexp.Regexp {
return regexp.MustCompile(`^!randquote$`)
}
func (h *RandQuoteHandler) Heard(ctx context.Context, w hugot.ResponseWriter, m *hugot.Message, submatches [][]string) {
h.do(ctx, w, m)
}
func (h *QuoteHandler) Describe() (string, string) {
return "quote", "show a specific quote, identified by ID"
}
@@ -169,12 +219,23 @@ func (h *QuoteHandler) show(channel, qid string) string {
return fmt.Sprintf("%s: %s", quote.Author, quote.Data)
}
func (h *QuoteHandler) do(ctx context.Context, w hugot.ResponseWriter, m *hugot.Message, qid string) error {
w.Send(ctx, m.Reply(h.show(m.Channel, qid)))
return nil
}
func (h *QuoteHandler) Command(ctx context.Context, w hugot.ResponseWriter, m *hugot.Message) error {
parts := strings.SplitN(m.Text, " ", 2)
if len(parts) != 2 {
return nil
}
w.Send(ctx, m.Reply(h.show(m.Channel, parts[1])))
return nil
return h.do(ctx, w, m, parts[1])
}
func (h *QuoteHandler) Hears() *regexp.Regexp {
return regexp.MustCompile(`^!quote (.*)$`)
}
func (h *QuoteHandler) Heard(ctx context.Context, w hugot.ResponseWriter, m *hugot.Message, submatches [][]string) {
h.do(ctx, w, m, submatches[0][1])
}