Rework lysenko to use latest hugot, removing go-ircevent

This commit is contained in:
2016-10-14 23:34:58 +01:00
parent c093390efa
commit 62eaba8408
3 changed files with 44 additions and 93 deletions

View File

@@ -4,10 +4,10 @@
package deadline
import (
"context"
"time"
"github.com/tcolgate/hugot"
"golang.org/x/net/context"
)
type HearsDeadline struct {

View File

@@ -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) Command(ctx context.Context, w hugot.ResponseWriter, m *hugot.Message) error {
parts := strings.SplitN(m.Text, " ", 2)
if len(parts) != 2 {
return nil
}
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
}
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
}

View File

@@ -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(&quotedb.AddQuoteHandler{QuoteDB: db})
mux.AddHearsHandler(deadline.NewHears(&quotedb.LastQuoteHandler{QuoteDB: db}))
mux.AddHearsHandler(deadline.NewHears(&quotedb.FindQuoteHandler{QuoteDB: db}))
mux.AddHearsHandler(deadline.NewHears(&quotedb.RandQuoteHandler{QuoteDB: db}))
mux.AddHearsHandler(deadline.NewHears(&quotedb.QuoteHandler{QuoteDB: db}))
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}))
go hugot.ListenAndServe(ctx, adapter, mux)
conn.Loop()
hugot.ListenAndServe(context.Background(), mux, adapter)
}