Added plugin ordering so that all queries are handled by the correct plugin

This commit is contained in:
Chris Sexton 2012-08-26 12:15:26 -04:00
parent c50a908af5
commit 94063c3967
2 changed files with 15 additions and 10 deletions

View File

@ -10,6 +10,7 @@ type Bot struct {
// Each plugin must be registered in our plugins handler. To come: a map so that this // 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 // 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 holds information about all of our friends
Users []User Users []User
@ -61,7 +62,8 @@ func NewBot(config *config.Config, c *irc.Conn) *Bot {
return &Bot{ return &Bot{
Config: config, Config: config,
Plugins: make(map[string]Handler), Plugins: make(map[string]Handler),
Users: make([]User, 1), PluginOrdering: make([]string, 0),
Users: make([]User, 0),
Conn: c, Conn: c,
DbSession: session, DbSession: session,
Db: db, Db: db,
@ -72,4 +74,5 @@ func NewBot(config *config.Config, c *irc.Conn) *Bot {
// Adds a constructed handler to the bots handlers list // Adds a constructed handler to the bots handlers list
func (b *Bot) AddHandler(name string, h Handler) { func (b *Bot) AddHandler(name string, h Handler) {
b.Plugins[strings.ToLower(name)] = h b.Plugins[strings.ToLower(name)] = h
b.PluginOrdering = append(b.PluginOrdering, name)
} }

View File

@ -130,7 +130,8 @@ func (b *Bot) MsgRecieved(conn *irc.Conn, line *irc.Line) {
return return
} }
for _, p := range b.Plugins { for _, name := range b.PluginOrdering {
p := b.Plugins[name]
if p.Message(msg) { if p.Message(msg) {
break break
} }
@ -163,7 +164,8 @@ func (b *Bot) Help(channel string, parts []string) {
func (b *Bot) UserJoined(conn *irc.Conn, line *irc.Line) { func (b *Bot) UserJoined(conn *irc.Conn, line *irc.Line) {
msg := b.buildMessage(conn, 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) { if p.Event(line.Cmd, msg) {
break break
} }