fluffle/goirc

Allow registering a global handler

Opened this issue · 1 comments

belak commented

I'm working on a bot plugin framework and it would be useful to register a handler which gets all events so I can dispatch the events with a different handler type (so my own Bot object is available).

There are ways to achieve this. If you take a look at the way I've done it with sp0rkle you can see an example -- though it's not a great one for your purposes cos I only really care about PRIVMSG, and I expose package-global functions from sp0rkle/bot to make life easier:

https://github.com/fluffle/sp0rkle/blob/master/bot/commandset.go#L86

^^ here is where Handle() for the commandset injects a bot.Context.

https://github.com/fluffle/sp0rkle/blob/master/bot/pollerset.go#L47

^^ pollerset does the same thing, and uses a switch to determine what to do for different events.

https://github.com/fluffle/sp0rkle/blob/master/bot/context.go#L34

^^ context() does the actual wrapping of Conn and Line.

https://github.com/fluffle/sp0rkle/blob/master/bot/bot.go#L47

^^ and here is where the various sets are registered as handlers.

If you are trying to hook all the events the bot receives to inject your own context, that's currently not possible, but I can see reasons why you might want to do that. The client doesn't expose it's own event loop either.

@StalkR has other ways of achieving the same thing: https://github.com/StalkR/goircbot

I don't have a huge amount of spare time to work on this code, but if you have a good idea and a reasonably self-contained pull request i'd be happy to look at it.