Allow people to be ignored by nick

This commit is contained in:
2021-02-13 01:26:08 +00:00
parent 264c591495
commit 3d5d18ce44
2 changed files with 45 additions and 5 deletions

34
handlers/ignore/ignore.go Normal file
View File

@@ -0,0 +1,34 @@
package ignore
import (
"context"
"fmt"
"regexp"
"github.com/tcolgate/hugot"
)
type HearsIgnore struct {
hugot.HearsHandler
Ignore []*regexp.Regexp
}
func NewHears(next hugot.HearsHandler, who ...string) hugot.HearsHandler {
var ignoreCompiled []*regexp.Regexp
for _, str := range who {
ignoreCompiled = append(ignoreCompiled, regexp.MustCompile(str))
}
return &HearsIgnore{HearsHandler: next, Ignore: ignoreCompiled}
}
func (i *HearsIgnore) Heard(ctx context.Context, w hugot.ResponseWriter, m *hugot.Message, matches [][]string) {
for _, re := range i.Ignore {
if re.MatchString(m.From) {
return
}
}
i.HearsHandler.Heard(ctx, w, m, matches)
}

View File

@@ -16,11 +16,13 @@ import (
"ur.gs/lysenko/adapters"
"ur.gs/lysenko/handlers/deadline"
"ur.gs/lysenko/handlers/ignore"
"ur.gs/lysenko/handlers/quotedb"
)
var (
channels = flag.String("channels", "##testing,##test", "Channels to join (separated by comma)")
ignores = flag.String("ignore", "", "Nicks to ignore (separated by comma)")
cooldown = flag.Int("cooldown", 120, "Command rate-limit, in seconds")
host = flag.String("host", "chat.freenode.net", "Server host[:port]")
ident = flag.String("ident", "lysenko", "Lysenko Bot")
@@ -59,6 +61,10 @@ func ircAdapter() hugot.Adapter {
func main() {
flag.Parse()
var ignoresList []string
if *ignores != "" {
ignoresList = strings.Split(*ignores, ",")
}
adapter := adapters.NewPrefixAdapter(ircAdapter(), "!")
adapter.PrivateHelpOnly = true
@@ -75,11 +81,11 @@ func main() {
deadline.Cooldown = time.Second * time.Duration(*cooldown)
mux := hugot.DefaultMux
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}))
mux.HandleHears(ignore.NewHears(&quotedb.AddQuoteHandler{QuoteDB: db}, ignoresList...))
mux.HandleHears(ignore.NewHears(deadline.NewHears(&quotedb.LastQuoteHandler{QuoteDB: db}), ignoresList...))
mux.HandleHears(ignore.NewHears(deadline.NewHears(&quotedb.FindQuoteHandler{QuoteDB: db}), ignoresList...))
mux.HandleHears(ignore.NewHears(deadline.NewHears(&quotedb.RandQuoteHandler{QuoteDB: db}), ignoresList...))
mux.HandleHears(ignore.NewHears(deadline.NewHears(&quotedb.QuoteHandler{QuoteDB: db}), ignoresList...))
hugot.ListenAndServe(ctx, mux, adapter)
}