From 62eaba8408bddc14b2ff5ef41998289684fd4390 Mon Sep 17 00:00:00 2001 From: Nick Thomas Date: Fri, 14 Oct 2016 23:34:58 +0100 Subject: [PATCH] Rework lysenko to use latest hugot, removing go-ircevent --- handlers/deadline/deadline.go | 2 +- handlers/quotedb/handlers.go | 89 ++++++++++++----------------------- lysenko.go | 46 ++++++------------ 3 files changed, 44 insertions(+), 93 deletions(-) diff --git a/handlers/deadline/deadline.go b/handlers/deadline/deadline.go index 9697924..2470549 100644 --- a/handlers/deadline/deadline.go +++ b/handlers/deadline/deadline.go @@ -4,10 +4,10 @@ package deadline import ( + "context" "time" "github.com/tcolgate/hugot" - "golang.org/x/net/context" ) type HearsDeadline struct { diff --git a/handlers/quotedb/handlers.go b/handlers/quotedb/handlers.go index 9c09b6a..3c103a8 100644 --- a/handlers/quotedb/handlers.go +++ b/handlers/quotedb/handlers.go @@ -3,24 +3,15 @@ package quotedb import ( + "context" "fmt" "log" "math/rand" - "regexp" "sort" "strconv" "strings" "github.com/tcolgate/hugot" - "golang.org/x/net/context" -) - -var ( - hearAddQuote = regexp.MustCompile(`^\!addquote (.*)$`) - hearLastQuote = regexp.MustCompile(`^\!lastquote$`) - hearFindQuote = regexp.MustCompile(`^\!findquote (.*)$`) - hearRandQuote = regexp.MustCompile(`^\!randquote$`) - hearQuote = regexp.MustCompile(`^\!quote (\d+)$`) ) type AddQuoteHandler struct { @@ -47,55 +38,41 @@ func (h *AddQuoteHandler) Describe() (string, string) { return "addquote", "Add a quote to the database" } -func (h *AddQuoteHandler) Hears() *regexp.Regexp { - return hearAddQuote -} - -func (h *AddQuoteHandler) Heard(ctx context.Context, w hugot.ResponseWriter, m *hugot.Message, submatches [][]string) { - if len(submatches) != 1 || len(submatches[0]) != 2 { - log.Printf("Submatches weird! %+v", submatches) - return +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 } - var rsp string - quote, err := h.QuoteDB.AddQuote(m.Channel, submatches[0][1], m.From) + quote, err := h.QuoteDB.AddQuote(m.Channel, parts[1], m.From) if err != nil { log.Printf("%s: AddQuote error: %v", m.Channel, err) - rsp = "Failed to add quote, sorry!" - } else { - rsp = fmt.Sprintf("Quote %d added", quote.QuoteId) + return fmt.Errorf("Failed to add quote, sorry!") } - w.Send(ctx, m.Reply(rsp)) + w.Send(ctx, m.Reply(fmt.Sprintf("Quote %d added", quote.QuoteId))) + return nil } func (h *LastQuoteHandler) Describe() (string, string) { return "lastquote", "Show the last-added quote" } -func (h *LastQuoteHandler) Hears() *regexp.Regexp { - return hearLastQuote -} - -func (h *LastQuoteHandler) Heard(ctx context.Context, w hugot.ResponseWriter, m *hugot.Message, submatches [][]string) { +func (h *LastQuoteHandler) Command(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) - return + return nil } - rsp := fmt.Sprintf("Quote %d by %s: %s", quote.QuoteId, quote.Author, quote.Data) - w.Send(ctx, m.Reply(rsp)) + w.Send(ctx, m.Reply(fmt.Sprintf("Quote %d by %s: %s", quote.QuoteId, quote.Author, quote.Data))) + return nil } func (h *FindQuoteHandler) Describe() (string, string) { return "findquote", "Search for a quote by substring" } -func (h *FindQuoteHandler) Hears() *regexp.Regexp { - return hearFindQuote -} - func (h *FindQuoteHandler) find(channel, term string) string { quotes, err := h.QuoteDB.FindQuotesByString(term, channel) if err != nil { @@ -140,43 +117,35 @@ 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) Heard(ctx context.Context, w hugot.ResponseWriter, m *hugot.Message, submatches [][]string) { - if len(submatches) != 1 || len(submatches[0]) != 2 { - log.Printf("Submatches weird! %+v", submatches) - return +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 } - rsp := h.find(m.Channel, submatches[0][1]) - w.Send(ctx, m.Reply(rsp)) + w.Send(ctx, m.Reply(h.find(m.Channel, parts[1]))) + return nil } func (h *RandQuoteHandler) Describe() (string, string) { return "randquote", "Show a random quote" } -func (h *RandQuoteHandler) Hears() *regexp.Regexp { - return hearRandQuote -} - -func (h *RandQuoteHandler) Heard(ctx context.Context, w hugot.ResponseWriter, m *hugot.Message, submatches [][]string) { +func (h *RandQuoteHandler) Command(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) - return + return nil } - rsp := fmt.Sprintf("Quote %d by %s: %s", quote.QuoteId, quote.Author, quote.Data) - w.Send(ctx, m.Reply(rsp)) + w.Send(ctx, m.Reply(fmt.Sprintf("Quote %d by %s: %s", quote.QuoteId, quote.Author, quote.Data))) + return nil } func (h *QuoteHandler) Describe() (string, string) { return "quote", "show a specific quote, identified by ID" } -func (h *QuoteHandler) Hears() *regexp.Regexp { - return hearQuote -} - func (h *QuoteHandler) show(channel, qid string) string { id, err := strconv.Atoi(qid) if err != nil { @@ -200,12 +169,12 @@ func (h *QuoteHandler) show(channel, qid string) string { return fmt.Sprintf("%s: %s", quote.Author, quote.Data) } -func (h *QuoteHandler) Heard(ctx context.Context, w hugot.ResponseWriter, m *hugot.Message, submatches [][]string) { - if len(submatches) != 1 || len(submatches[0]) != 2 { - log.Printf("Submatches weird! %+v", submatches) - return +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 } - rsp := h.show(m.Channel, submatches[0][1]) - w.Send(ctx, m.Reply(rsp)) + w.Send(ctx, m.Reply(h.show(m.Channel, parts[1]))) + return nil } diff --git a/lysenko.go b/lysenko.go index a395dcb..9044f3c 100644 --- a/lysenko.go +++ b/lysenko.go @@ -1,18 +1,17 @@ package main import ( + "context" "flag" "log" "math/rand" "strings" "time" + "github.com/fluffle/goirc/client" "github.com/tcolgate/hugot" "github.com/tcolgate/hugot/adapters/irc" - irce "github.com/thoj/go-ircevent" - "golang.org/x/net/context" - "ur.gs/lysenko/handlers/deadline" "ur.gs/lysenko/handlers/quotedb" ) @@ -34,30 +33,14 @@ func init() { func main() { flag.Parse() + channelList := strings.Split(*channels, ",") + config := client.NewConfig(*nick) + config.Server = *host + config.Pass = *nickserv + config.SSL = *ssl + adapter := irc.New(config, channelList...) - conn := irce.IRC(*nick, *ident) - conn.UseTLS = *ssl - conn.Password = *nickserv - conn.Debug = true - - conn.AddCallback("001", func(e *irce.Event) { - for _, channel := range channelList { - conn.Join(channel) - } - }) - - if err := conn.Connect(*host); err != nil { - log.Fatal(err) - } - defer conn.Quit() - - adapter, err := irc.New(conn) - if err != nil { - log.Fatal(err) - } - - ctx := context.Background() db, err := quotedb.New(*quotes) if err != nil { log.Fatal(err) @@ -65,12 +48,11 @@ func main() { defer db.DB.Close() mux := hugot.DefaultMux - mux.AddHearsHandler("edb.AddQuoteHandler{QuoteDB: db}) - mux.AddHearsHandler(deadline.NewHears("edb.LastQuoteHandler{QuoteDB: db})) - mux.AddHearsHandler(deadline.NewHears("edb.FindQuoteHandler{QuoteDB: db})) - mux.AddHearsHandler(deadline.NewHears("edb.RandQuoteHandler{QuoteDB: db})) - mux.AddHearsHandler(deadline.NewHears("edb.QuoteHandler{QuoteDB: db})) + mux.HandleCommand("edb.AddQuoteHandler{QuoteDB: db}) + mux.HandleCommand(deadline.NewCommand("edb.LastQuoteHandler{QuoteDB: db})) + mux.HandleCommand(deadline.NewCommand("edb.FindQuoteHandler{QuoteDB: db})) + mux.HandleCommand(deadline.NewCommand("edb.RandQuoteHandler{QuoteDB: db})) + mux.HandleCommand(deadline.NewCommand("edb.QuoteHandler{QuoteDB: db})) - go hugot.ListenAndServe(ctx, adapter, mux) - conn.Loop() + hugot.ListenAndServe(context.Background(), mux, adapter) }