Revert back to HearsHandlers - CommandHandlers require valid shellwords

modernc-sqlite
Nick Thomas 6 years ago
parent c208a0e00c
commit 73cd922f2c
  1. 4
      adapters/prefix_adapter.go
  2. 87
      handlers/quotedb/handlers.go
  3. 31
      lysenko.go

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

@ -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
}
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])
}

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

Loading…
Cancel
Save