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

@@ -42,8 +42,8 @@ func (p *PrefixAdapter) Run(ctx context.Context) {
if m == nil { if m == nil {
return return
} }
// No use for this now, as we're using HearsHandler instead
m = p.fixup(m) //m = p.fixup(m)
if !p.isBlacklisted(m) { if !p.isBlacklisted(m) {
p.c <- m p.c <- m
} }

View File

@@ -7,6 +7,7 @@ import (
"fmt" "fmt"
"log" "log"
"math/rand" "math/rand"
"regexp"
"sort" "sort"
"strconv" "strconv"
"strings" "strings"
@@ -38,27 +39,39 @@ func (h *AddQuoteHandler) Describe() (string, string) {
return "addquote", "Add a quote to the database" 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 { func (h *AddQuoteHandler) Command(ctx context.Context, w hugot.ResponseWriter, m *hugot.Message) error {
parts := strings.SplitN(m.Text, " ", 2) parts := strings.SplitN(m.Text, " ", 2)
if len(parts) != 2 { if len(parts) != 2 {
return nil return nil
} }
quote, err := h.QuoteDB.AddQuote(m.Channel, parts[1], m.From) return h.do(ctx, w, m, parts[1])
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", quote.QuoteId))) func (h *AddQuoteHandler) Heard(ctx context.Context, w hugot.ResponseWriter, m *hugot.Message, submatches [][]string) {
return nil h.do(ctx, w, m, submatches[0][1])
} }
func (h *LastQuoteHandler) Describe() (string, string) { func (h *LastQuoteHandler) Describe() (string, string) {
return "lastquote", "Show the last-added quote" 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) quote, err := h.QuoteDB.FindLastQuote(m.Channel)
if err != nil { if err != nil {
log.Printf("%s: Error in LastQuote: %v", m.Channel, err) 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))) w.Send(ctx, m.Reply(fmt.Sprintf("Quote %d by %s: %s", quote.QuoteId, quote.Author, quote.Data)))
return nil 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) { 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) 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 { func (h *FindQuoteHandler) Command(ctx context.Context, w hugot.ResponseWriter, m *hugot.Message) error {
parts := strings.SplitN(m.Text, " ", 2) parts := strings.SplitN(m.Text, " ", 2)
if len(parts) != 2 { if len(parts) != 2 {
return nil return nil
} }
return h.do(ctx, w, m, parts[1])
}
w.Send(ctx, m.Reply(h.find(m.Channel, parts[1]))) func (h *FindQuoteHandler) Hears() *regexp.Regexp {
return nil 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) { func (h *RandQuoteHandler) Describe() (string, string) {
return "randquote", "Show a random quote" 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) quote, err := h.QuoteDB.FindRandomQuote(m.Channel)
if err != nil { if err != nil {
log.Printf("%s: Error in RandQuote: %v", m.Channel, err) 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 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) { func (h *QuoteHandler) Describe() (string, string) {
return "quote", "show a specific quote, identified by ID" 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) 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 { func (h *QuoteHandler) Command(ctx context.Context, w hugot.ResponseWriter, m *hugot.Message) error {
parts := strings.SplitN(m.Text, " ", 2) parts := strings.SplitN(m.Text, " ", 2)
if len(parts) != 2 { if len(parts) != 2 {
return nil return nil
} }
return h.do(ctx, w, m, parts[1])
w.Send(ctx, m.Reply(h.show(m.Channel, parts[1]))) }
return nil
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])
} }

View File

@@ -27,6 +27,7 @@ var (
nickserv = flag.String("nickserv", "", "NickServ password") nickserv = flag.String("nickserv", "", "NickServ password")
quotes = flag.String("quotedb", ":memory:", "sqlite3 quote database") quotes = flag.String("quotedb", ":memory:", "sqlite3 quote database")
ssl = flag.Bool("ssl", true, "Enable SSL") ssl = flag.Bool("ssl", true, "Enable SSL")
testmode = flag.Bool("testmode", false, "Use STDIN/STDOUT instead of IRC for messages")
) )
func init() { func init() {
@@ -34,10 +35,9 @@ func init() {
rand.Seed(time.Now().Unix()) rand.Seed(time.Now().Unix())
} }
func main() { func ircAdapter() hugot.Adapter {
flag.Parse()
channelList := strings.Split(*channels, ",") channelList := strings.Split(*channels, ",")
config := client.NewConfig(*nick, "lysenko", "Comrade Trofim Lysenko") config := client.NewConfig(*nick, "lysenko", "Comrade Trofim Lysenko")
config.Server = *host config.Server = *host
config.Pass = *nickserv config.Pass = *nickserv
@@ -53,12 +53,17 @@ func main() {
} }
} }
adapter := irc.New(config, channelList...) return irc.New(config, channelList...)
prefix_adapter := adapters.NewPrefixAdapter(adapter, "!") }
prefix_adapter.PrivateHelpOnly = true
func main() {
flag.Parse()
adapter := adapters.NewPrefixAdapter(ircAdapter(), "!")
adapter.PrivateHelpOnly = true
ctx := context.Background() ctx := context.Background()
go prefix_adapter.Run(ctx) go adapter.Run(ctx)
db, err := quotedb.New(*quotes) db, err := quotedb.New(*quotes)
if err != nil { if err != nil {
@@ -67,11 +72,11 @@ func main() {
defer db.DB.Close() defer db.DB.Close()
mux := hugot.DefaultMux mux := hugot.DefaultMux
mux.HandleCommand(&quotedb.AddQuoteHandler{QuoteDB: db}) mux.HandleHears(&quotedb.AddQuoteHandler{QuoteDB: db})
mux.HandleCommand(deadline.NewCommand(&quotedb.LastQuoteHandler{QuoteDB: db})) mux.HandleHears(deadline.NewHears(&quotedb.LastQuoteHandler{QuoteDB: db}))
mux.HandleCommand(deadline.NewCommand(&quotedb.FindQuoteHandler{QuoteDB: db})) mux.HandleHears(deadline.NewHears(&quotedb.FindQuoteHandler{QuoteDB: db}))
mux.HandleCommand(deadline.NewCommand(&quotedb.RandQuoteHandler{QuoteDB: db})) mux.HandleHears(deadline.NewHears(&quotedb.RandQuoteHandler{QuoteDB: db}))
mux.HandleCommand(deadline.NewCommand(&quotedb.QuoteHandler{QuoteDB: db})) mux.HandleHears(deadline.NewHears(&quotedb.QuoteHandler{QuoteDB: db}))
hugot.ListenAndServe(ctx, mux, prefix_adapter) hugot.ListenAndServe(ctx, mux, adapter)
} }