From 94063c39673b681d427ca903b4d2dc9915e1fb39 Mon Sep 17 00:00:00 2001 From: Chris Sexton Date: Sun, 26 Aug 2012 12:15:26 -0400 Subject: [PATCH] Added plugin ordering so that all queries are handled by the correct plugin --- bot/bot.go | 19 +++++++++++-------- bot/handlers.go | 6 ++++-- 2 files changed, 15 insertions(+), 10 deletions(-) diff --git a/bot/bot.go b/bot/bot.go index e8b0c76..84caea0 100644 --- a/bot/bot.go +++ b/bot/bot.go @@ -9,7 +9,8 @@ import "strings" type Bot struct { // Each plugin must be registered in our plugins handler. To come: a map so that this // will allow plugins to respond to specific kinds of events - Plugins map[string]Handler + Plugins map[string]Handler + PluginOrdering []string // Users holds information about all of our friends Users []User @@ -59,17 +60,19 @@ func NewBot(config *config.Config, c *irc.Conn) *Bot { db := session.DB(config.DbName) return &Bot{ - Config: config, - Plugins: make(map[string]Handler), - Users: make([]User, 1), - Conn: c, - DbSession: session, - Db: db, - Version: config.Version, + Config: config, + Plugins: make(map[string]Handler), + PluginOrdering: make([]string, 0), + Users: make([]User, 0), + Conn: c, + DbSession: session, + Db: db, + Version: config.Version, } } // Adds a constructed handler to the bots handlers list func (b *Bot) AddHandler(name string, h Handler) { b.Plugins[strings.ToLower(name)] = h + b.PluginOrdering = append(b.PluginOrdering, name) } diff --git a/bot/handlers.go b/bot/handlers.go index 56db520..32d17f8 100644 --- a/bot/handlers.go +++ b/bot/handlers.go @@ -130,7 +130,8 @@ func (b *Bot) MsgRecieved(conn *irc.Conn, line *irc.Line) { return } - for _, p := range b.Plugins { + for _, name := range b.PluginOrdering { + p := b.Plugins[name] if p.Message(msg) { break } @@ -163,7 +164,8 @@ func (b *Bot) Help(channel string, parts []string) { func (b *Bot) UserJoined(conn *irc.Conn, line *irc.Line) { msg := b.buildMessage(conn, line) - for _, p := range b.Plugins { + for _, name := range b.PluginOrdering { + p := b.Plugins[name] if p.Event(line.Cmd, msg) { break }