Rework lysenko to use latest hugot, removing go-ircevent
This commit is contained in:
@@ -4,10 +4,10 @@
|
|||||||
package deadline
|
package deadline
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/tcolgate/hugot"
|
"github.com/tcolgate/hugot"
|
||||||
"golang.org/x/net/context"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type HearsDeadline struct {
|
type HearsDeadline struct {
|
||||||
|
@@ -3,24 +3,15 @@
|
|||||||
package quotedb
|
package quotedb
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
"math/rand"
|
"math/rand"
|
||||||
"regexp"
|
|
||||||
"sort"
|
"sort"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/tcolgate/hugot"
|
"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 {
|
type AddQuoteHandler struct {
|
||||||
@@ -47,55 +38,41 @@ 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) Hears() *regexp.Regexp {
|
func (h *AddQuoteHandler) Command(ctx context.Context, w hugot.ResponseWriter, m *hugot.Message) error {
|
||||||
return hearAddQuote
|
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, parts[1], m.From)
|
||||||
quote, err := h.QuoteDB.AddQuote(m.Channel, submatches[0][1], m.From)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("%s: AddQuote error: %v", m.Channel, err)
|
log.Printf("%s: AddQuote error: %v", m.Channel, err)
|
||||||
rsp = "Failed to add quote, sorry!"
|
return fmt.Errorf("Failed to add quote, sorry!")
|
||||||
} else {
|
|
||||||
rsp = fmt.Sprintf("Quote %d added", quote.QuoteId)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
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) {
|
func (h *LastQuoteHandler) Describe() (string, string) {
|
||||||
return "lastquote", "Show the last-added quote"
|
return "lastquote", "Show the last-added quote"
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *LastQuoteHandler) Hears() *regexp.Regexp {
|
func (h *LastQuoteHandler) Command(ctx context.Context, w hugot.ResponseWriter, m *hugot.Message) error {
|
||||||
return hearLastQuote
|
|
||||||
}
|
|
||||||
|
|
||||||
func (h *LastQuoteHandler) Heard(ctx context.Context, w hugot.ResponseWriter, m *hugot.Message, submatches [][]string) {
|
|
||||||
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)
|
||||||
return
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
rsp := 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)))
|
||||||
w.Send(ctx, m.Reply(rsp))
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *FindQuoteHandler) Describe() (string, string) {
|
func (h *FindQuoteHandler) Describe() (string, string) {
|
||||||
return "findquote", "Search for a quote by substring"
|
return "findquote", "Search for a quote by substring"
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *FindQuoteHandler) Hears() *regexp.Regexp {
|
|
||||||
return hearFindQuote
|
|
||||||
}
|
|
||||||
|
|
||||||
func (h *FindQuoteHandler) find(channel, term string) string {
|
func (h *FindQuoteHandler) find(channel, term string) string {
|
||||||
quotes, err := h.QuoteDB.FindQuotesByString(term, channel)
|
quotes, err := h.QuoteDB.FindQuotesByString(term, channel)
|
||||||
if err != nil {
|
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)
|
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) {
|
func (h *FindQuoteHandler) Command(ctx context.Context, w hugot.ResponseWriter, m *hugot.Message) error {
|
||||||
if len(submatches) != 1 || len(submatches[0]) != 2 {
|
parts := strings.SplitN(m.Text, " ", 2)
|
||||||
log.Printf("Submatches weird! %+v", submatches)
|
if len(parts) != 2 {
|
||||||
return
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
rsp := h.find(m.Channel, submatches[0][1])
|
w.Send(ctx, m.Reply(h.find(m.Channel, parts[1])))
|
||||||
w.Send(ctx, m.Reply(rsp))
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
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) Hears() *regexp.Regexp {
|
func (h *RandQuoteHandler) Command(ctx context.Context, w hugot.ResponseWriter, m *hugot.Message) error {
|
||||||
return hearRandQuote
|
|
||||||
}
|
|
||||||
|
|
||||||
func (h *RandQuoteHandler) Heard(ctx context.Context, w hugot.ResponseWriter, m *hugot.Message, submatches [][]string) {
|
|
||||||
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)
|
||||||
return
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
rsp := 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)))
|
||||||
w.Send(ctx, m.Reply(rsp))
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
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"
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *QuoteHandler) Hears() *regexp.Regexp {
|
|
||||||
return hearQuote
|
|
||||||
}
|
|
||||||
|
|
||||||
func (h *QuoteHandler) show(channel, qid string) string {
|
func (h *QuoteHandler) show(channel, qid string) string {
|
||||||
id, err := strconv.Atoi(qid)
|
id, err := strconv.Atoi(qid)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -200,12 +169,12 @@ 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) Heard(ctx context.Context, w hugot.ResponseWriter, m *hugot.Message, submatches [][]string) {
|
func (h *QuoteHandler) Command(ctx context.Context, w hugot.ResponseWriter, m *hugot.Message) error {
|
||||||
if len(submatches) != 1 || len(submatches[0]) != 2 {
|
parts := strings.SplitN(m.Text, " ", 2)
|
||||||
log.Printf("Submatches weird! %+v", submatches)
|
if len(parts) != 2 {
|
||||||
return
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
rsp := h.show(m.Channel, submatches[0][1])
|
w.Send(ctx, m.Reply(h.show(m.Channel, parts[1])))
|
||||||
w.Send(ctx, m.Reply(rsp))
|
return nil
|
||||||
}
|
}
|
||||||
|
46
lysenko.go
46
lysenko.go
@@ -1,18 +1,17 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"flag"
|
"flag"
|
||||||
"log"
|
"log"
|
||||||
"math/rand"
|
"math/rand"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/fluffle/goirc/client"
|
||||||
"github.com/tcolgate/hugot"
|
"github.com/tcolgate/hugot"
|
||||||
"github.com/tcolgate/hugot/adapters/irc"
|
"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/deadline"
|
||||||
"ur.gs/lysenko/handlers/quotedb"
|
"ur.gs/lysenko/handlers/quotedb"
|
||||||
)
|
)
|
||||||
@@ -34,30 +33,14 @@ func init() {
|
|||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
|
|
||||||
channelList := strings.Split(*channels, ",")
|
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)
|
db, err := quotedb.New(*quotes)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
@@ -65,12 +48,11 @@ func main() {
|
|||||||
defer db.DB.Close()
|
defer db.DB.Close()
|
||||||
|
|
||||||
mux := hugot.DefaultMux
|
mux := hugot.DefaultMux
|
||||||
mux.AddHearsHandler("edb.AddQuoteHandler{QuoteDB: db})
|
mux.HandleCommand("edb.AddQuoteHandler{QuoteDB: db})
|
||||||
mux.AddHearsHandler(deadline.NewHears("edb.LastQuoteHandler{QuoteDB: db}))
|
mux.HandleCommand(deadline.NewCommand("edb.LastQuoteHandler{QuoteDB: db}))
|
||||||
mux.AddHearsHandler(deadline.NewHears("edb.FindQuoteHandler{QuoteDB: db}))
|
mux.HandleCommand(deadline.NewCommand("edb.FindQuoteHandler{QuoteDB: db}))
|
||||||
mux.AddHearsHandler(deadline.NewHears("edb.RandQuoteHandler{QuoteDB: db}))
|
mux.HandleCommand(deadline.NewCommand("edb.RandQuoteHandler{QuoteDB: db}))
|
||||||
mux.AddHearsHandler(deadline.NewHears("edb.QuoteHandler{QuoteDB: db}))
|
mux.HandleCommand(deadline.NewCommand("edb.QuoteHandler{QuoteDB: db}))
|
||||||
|
|
||||||
go hugot.ListenAndServe(ctx, adapter, mux)
|
hugot.ListenAndServe(context.Background(), mux, adapter)
|
||||||
conn.Loop()
|
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user