Move from an ignore handler to hacking the IRC adapter

This commit is contained in:
2016-07-05 03:01:56 +01:00
parent 41e45e8f13
commit e8853b3be9
4 changed files with 120 additions and 127 deletions

View File

@@ -1,59 +0,0 @@
// package ignore is a Hugot handler that will force any messages received from
// a set of matching idents to be ignored
package ignore
import (
"github.com/tcolgate/hugot"
"golang.org/x/net/context"
)
type IgnoreHearsHandler struct {
hugot.HearsHandler
Ignores map[string]struct{}
}
type IgnoreCommandHandler struct {
hugot.CommandHandler
Ignores map[string]struct{}
}
func NewCommand(list []string, next hugot.CommandHandler) hugot.CommandHandler {
return &IgnoreCommandHandler{
CommandHandler: next,
Ignores: buildIgnores(list),
}
}
func NewHears(list []string, next hugot.HearsHandler) hugot.HearsHandler {
return &IgnoreHearsHandler{
HearsHandler: next,
Ignores: buildIgnores(list),
}
}
func buildIgnores(ignores []string) map[string]struct{} {
out := make(map[string]struct{}, len(ignores))
for _, nick := range ignores {
out[nick] = struct{}{}
}
return out
}
func (i *IgnoreHearsHandler) Heard(ctx context.Context, w hugot.ResponseWriter, m *hugot.Message, matches [][]string) {
if _, present := i.Ignores[m.From]; present {
return
}
i.HearsHandler.Heard(ctx, w, m, matches)
}
func (i *IgnoreCommandHandler) Command(ctx context.Context, w hugot.ResponseWriter, m *hugot.Message) error {
if _, present := i.Ignores[m.From]; present {
return nil
}
return i.CommandHandler.Command(ctx, w, m)
}

View File

@@ -1,46 +0,0 @@
package ignore_test
import (
"regexp"
"testing"
"github.com/tcolgate/hugot"
"golang.org/x/net/context"
"ur.gs/lysenko/handlers/ignore"
)
type DummyHearsHandler struct {
Called bool
}
func (h *DummyHearsHandler) Describe() (string, string) {
return "", ""
}
func (h *DummyHearsHandler) Hears() *regexp.Regexp {
return regexp.MustCompile(``)
}
func (h *DummyHearsHandler) Heard(ctx context.Context, w hugot.ResponseWriter, m *hugot.Message, submatches [][]string) {
h.Called = true
}
func TestIgnoreHearsPassesOKNick(t *testing.T) {
msg := hugot.Message{}
dummy := &DummyHearsHandler{}
ignorer := ignore.NewHears([]string{"foo"}, dummy)
ignorer.Heard(context.TODO(), hugot.NewNullResponseWriter(msg), &msg, [][]string{})
if !dummy.Called {
t.Fatal("Dummy not called when it should have been")
}
}
func TestIgnoreHearsBlocksBadNick(t *testing.T) {
msg := hugot.Message{From: "foo"}
dummy := &DummyHearsHandler{}
ignorer := ignore.NewHears([]string{"foo"}, dummy)
ignorer.Heard(context.TODO(), hugot.NewNullResponseWriter(msg), &msg, [][]string{})
if dummy.Called {
t.Fatal("Dummy called when it shouldn't have been")
}
}