From 0f51bfba4f2f079236030884e14722b7f3199061 Mon Sep 17 00:00:00 2001 From: Robert Gerus Date: Sat, 8 Jan 2022 16:18:32 +0100 Subject: [PATCH] Some initial work towards having actual plugins. --- at.go | 2 +- helpers.go | 2 +- notifier.go | 29 +++++++++++++++++++++++++---- 3 files changed, 27 insertions(+), 6 deletions(-) diff --git a/at.go b/at.go index 263a29d..2f2f2bb 100644 --- a/at.go +++ b/at.go @@ -3,8 +3,8 @@ package main import ( "encoding/json" "fmt" + "log" "time" - "log" "gopkg.in/irc.v3" ) diff --git a/helpers.go b/helpers.go index 846c9f6..2784530 100644 --- a/helpers.go +++ b/helpers.go @@ -1,9 +1,9 @@ package main import ( - "time" "io" "net/http" + "time" ) func listSubstract(a, b []string) (ret []string) { diff --git a/notifier.go b/notifier.go index adbd2f1..4a4f98f 100644 --- a/notifier.go +++ b/notifier.go @@ -7,16 +7,37 @@ import ( "gopkg.in/irc.v3" ) -func genericHandler(c *irc.Client, m *irc.Message) { +type dispatchFunc func(*irc.Client, *irc.Message) + +func handlerFactory(dispatchers []dispatchFunc) func(*irc.Client, *irc.Message) { + return func(c *irc.Client, m *irc.Message) { + for _, f := range dispatchers { + go f(c, m.Copy()) + } + } +} + +func logger(_ *irc.Client, m *irc.Message) { log.Println(m) - if m.Command == "001" { - c.Write("JOIN #hswaw-members") +} + +func joinerFactory(channels []string) func(*irc.Client, *irc.Message) { + return func(c *irc.Client, m *irc.Message) { + if m.Command == "001" { + for _, ch := range channels { + c.Write("JOIN " + ch) + } + } } } func main() { done := make(chan bool) var a atMonitor + var dispatchers = []dispatchFunc{ + logger, + joinerFactory([]string{"#hswaw-members"}), + } conn, err := net.Dial("tcp", "irc.libera.chat:6667") if err != nil { @@ -28,7 +49,7 @@ func main() { Pass: "***", User: "bot", Name: "notbot", - Handler: irc.HandlerFunc(genericHandler), + Handler: irc.HandlerFunc(handlerFactory(dispatchers)), } client := irc.NewClient(conn, config)