From e816e7274327cea2b7a6218fbd776646938eb8fa Mon Sep 17 00:00:00 2001 From: Robert Gerus Date: Mon, 9 Nov 2015 01:46:08 +0100 Subject: [PATCH] Add a separate dispatcher for PRIVMSG MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit We're going to use that one a lot more than the others… --- bot/dispatcher.go | 17 +++++++++++++++++ bot/msgping.go | 17 +++++++++++++++++ 2 files changed, 34 insertions(+) create mode 100644 bot/msgping.go diff --git a/bot/dispatcher.go b/bot/dispatcher.go index ec26782..9356d34 100644 --- a/bot/dispatcher.go +++ b/bot/dispatcher.go @@ -2,6 +2,7 @@ package bot import ( "log" + "strings" "github.com/arachnist/gorepost/irc" ) @@ -16,11 +17,27 @@ func RemoveCallback(command string) { delete(Callbacks, command) } +var MSGCallbacks = make(map[string]func(chan irc.Message, irc.Message)) + +func AddMSGCallback(command string, callback func(chan irc.Message, irc.Message)) { + MSGCallbacks[command] = callback +} + +func RemoveMSGCallback(command string) { + delete(MSGCallbacks, command) +} + func Dispatcher(quit chan struct{}, output chan irc.Message, input chan irc.Message) { log.Println("spawned Dispatcher") for { select { case msg := <-input: + if msg.Command == "PRIVMSG" { + cmd := strings.Split(msg.Trailing, " ")[0] + if MSGCallbacks[cmd] != nil { + go MSGCallbacks[cmd](output, msg) + } + } if Callbacks[msg.Command] != nil { go Callbacks[msg.Command](output, msg) } diff --git a/bot/msgping.go b/bot/msgping.go new file mode 100644 index 0000000..c2a687f --- /dev/null +++ b/bot/msgping.go @@ -0,0 +1,17 @@ +package bot + +import ( + "github.com/arachnist/gorepost/irc" +) + +func papiez(output chan irc.Message, msg irc.Message) { + output <- irc.Message{ + Command: "PRIVMSG", + Params: []string{msg.Prefix.Name}, + Trailing: "pingity pong", + } +} + +func init() { + AddMSGCallback(":ping", papiez) +}