diff --git a/bot/bot.go b/bot/bot.go index 5c427ee..b35a021 100644 --- a/bot/bot.go +++ b/bot/bot.go @@ -17,6 +17,8 @@ type Bot struct { // Users holds information about all of our friends Users []User + // Represents the bot + Me User // Conn allows us to send messages and modify our connection state Conn *irc.Conn @@ -129,6 +131,7 @@ func NewBot(config *config.Config, c *irc.Conn) *Bot { Plugins: make(map[string]Handler), PluginOrdering: make([]string, 0), Users: users, + Me: users[0], Conn: c, DbSession: session, Db: db, @@ -148,9 +151,37 @@ func (b *Bot) AddHandler(name string, h Handler) { // Sends message to channel func (b *Bot) SendMessage(channel, message string) { b.Conn.Privmsg(channel, message) + + // Notify plugins that we've said something + b.selfSaid(channel, message) } // Sends action to channel func (b *Bot) SendAction(channel, message string) { b.Conn.Action(channel, message) + + // Notify plugins that we've said something + b.selfSaid(channel, message) +} + +// Handles incomming PRIVMSG requests +func (b *Bot) MsgRecieved(conn *irc.Conn, line *irc.Line) { + msg := b.buildMessage(conn, line) + + if strings.HasPrefix(msg.Body, "help") && msg.Command { + parts := strings.Fields(strings.ToLower(msg.Body)) + b.checkHelp(msg.Channel, parts) + goto RET + } + + for _, name := range b.PluginOrdering { + p := b.Plugins[name] + if p.Message(msg) { + break + } + } + +RET: + b.logIn <- msg + return } diff --git a/bot/handlers.go b/bot/handlers.go index 8bf516c..b065b88 100644 --- a/bot/handlers.go +++ b/bot/handlers.go @@ -16,6 +16,7 @@ import irc "github.com/fluffle/goirc/client" type Handler interface { Message(message Message) bool Event(kind string, message Message) bool + BotMessage(message Message) bool Help(channel string, parts []string) } @@ -152,28 +153,6 @@ func (b *Bot) LastMessage() (Message, error) { return log[len(log)-1], nil } -// Handles incomming PRIVMSG requests -func (b *Bot) MsgRecieved(conn *irc.Conn, line *irc.Line) { - msg := b.buildMessage(conn, line) - - if strings.HasPrefix(msg.Body, "help") && msg.Command { - parts := strings.Fields(strings.ToLower(msg.Body)) - b.checkHelp(msg.Channel, parts) - goto RET - } - - for _, name := range b.PluginOrdering { - p := b.Plugins[name] - if p.Message(msg) { - break - } - } - -RET: - b.logIn <- msg - return -} - // Take an input string and mutate it based on $vars in the string func (b *Bot) Filter(message Message, input string) string { rand.Seed(time.Now().Unix()) @@ -257,3 +236,23 @@ func (b *Bot) ActionRecieved(conn *irc.Conn, line *irc.Line) { } } } + +// Send our own musings to the plugins +func (b *Bot) selfSaid(channel, message string) { + msg := Message{ + User: &b.Me, // hack + Channel: channel, + Body: message, + Raw: message, // hack + Command: false, + Time: time.Now(), + Host: "0.0.0.0", // hack + } + + for _, name := range b.PluginOrdering { + p := b.Plugins[name] + if p.BotMessage(msg) { + break + } + } +} diff --git a/plugins/admin.go b/plugins/admin.go index d1d35d9..6e39010 100644 --- a/plugins/admin.go +++ b/plugins/admin.go @@ -1,8 +1,8 @@ package plugins import ( - "github.com/chrissexton/alepale/bot" "fmt" + "github.com/chrissexton/alepale/bot" "labix.org/v2/mgo" "labix.org/v2/mgo/bson" "math/rand" @@ -91,3 +91,8 @@ func (p *AdminPlugin) Help(channel string, parts []string) { func (p *AdminPlugin) Event(kind string, message bot.Message) bool { return false } + +// Handler for bot's own messages +func (p *AdminPlugin) BotMessage(message bot.Message) bool { + return false +} diff --git a/plugins/beers.go b/plugins/beers.go index 83a83a4..f390c2f 100644 --- a/plugins/beers.go +++ b/plugins/beers.go @@ -1,10 +1,10 @@ package plugins import ( - "github.com/chrissexton/alepale/bot" "encoding/json" "errors" "fmt" + "github.com/chrissexton/alepale/bot" "io/ioutil" "labix.org/v2/mgo" "labix.org/v2/mgo/bson" @@ -369,3 +369,8 @@ func (p *BeersPlugin) checkUntappd(channel string) { } } } + +// Handler for bot's own messages +func (p *BeersPlugin) BotMessage(message bot.Message) bool { + return false +} diff --git a/plugins/counter.go b/plugins/counter.go index a1b7810..96c7504 100644 --- a/plugins/counter.go +++ b/plugins/counter.go @@ -189,3 +189,8 @@ func (p *CounterPlugin) Help(channel string, parts []string) { func (p *CounterPlugin) Event(kind string, message bot.Message) bool { return false } + +// Handler for bot's own messages +func (p *CounterPlugin) BotMessage(message bot.Message) bool { + return false +} diff --git a/plugins/dice.go b/plugins/dice.go index a295982..602e489 100644 --- a/plugins/dice.go +++ b/plugins/dice.go @@ -93,3 +93,8 @@ func (p *DicePlugin) Help(channel string, parts []string) { func (p *DicePlugin) Event(kind string, message bot.Message) bool { return false } + +// Handler for bot's own messages +func (p *DicePlugin) BotMessage(message bot.Message) bool { + return false +} diff --git a/plugins/downtime.go b/plugins/downtime.go index e399a1d..5572669 100644 --- a/plugins/downtime.go +++ b/plugins/downtime.go @@ -148,3 +148,8 @@ func (p *DowntimePlugin) Event(kind string, message bot.Message) bool { } return false } + +// Handler for bot's own messages +func (p *DowntimePlugin) BotMessage(message bot.Message) bool { + return false +} diff --git a/plugins/factoid.go b/plugins/factoid.go index e6e7807..0af1638 100644 --- a/plugins/factoid.go +++ b/plugins/factoid.go @@ -1,8 +1,8 @@ package plugins import ( - "github.com/chrissexton/alepale/bot" "fmt" + "github.com/chrissexton/alepale/bot" "labix.org/v2/mgo" "labix.org/v2/mgo/bson" "math/rand" @@ -437,3 +437,8 @@ func (p *FactoidPlugin) factTimer(channel string) { } } } + +// Handler for bot's own messages +func (p *FactoidPlugin) BotMessage(message bot.Message) bool { + return false +} diff --git a/plugins/first.go b/plugins/first.go index 4ff9c60..b150a85 100644 --- a/plugins/first.go +++ b/plugins/first.go @@ -1,8 +1,8 @@ package plugins import ( - "github.com/chrissexton/alepale/bot" "fmt" + "github.com/chrissexton/alepale/bot" "labix.org/v2/mgo" "labix.org/v2/mgo/bson" "log" @@ -141,3 +141,8 @@ func (p *FirstPlugin) Help(channel string, parts []string) { func (p *FirstPlugin) Event(kind string, message bot.Message) bool { return false } + +// Handler for bot's own messages +func (p *FirstPlugin) BotMessage(message bot.Message) bool { + return false +} diff --git a/plugins/plugins.go b/plugins/plugins.go index aabae6a..4cd8e28 100644 --- a/plugins/plugins.go +++ b/plugins/plugins.go @@ -7,6 +7,7 @@ import "github.com/chrissexton/alepale/bot" type Plugin interface { Message(message bot.Message) bool Event(kind string, message bot.Message) bool + BotMessage(message bot.Message) bool LoadData() Help() } @@ -61,6 +62,11 @@ func (p *TestPlugin) Event(kind string, message bot.Message) bool { return false } +// Handler for bot's own messages +func (p *TestPlugin) BotMessage(message bot.Message) bool { + return false +} + type PluginConfig struct { Name string Values map[string]interface{} @@ -94,3 +100,8 @@ func (fp FalsePlugin) LoadData() { func (p *FalsePlugin) Event(kind string, message bot.Message) bool { return false } + +// Handler for bot's own messages +func (p *FalsePlugin) BotMessage(message bot.Message) bool { + return false +} diff --git a/plugins/remember.go b/plugins/remember.go index 90296a4..6250c87 100644 --- a/plugins/remember.go +++ b/plugins/remember.go @@ -5,6 +5,7 @@ import ( "github.com/chrissexton/alepale/bot" "labix.org/v2/mgo" "labix.org/v2/mgo/bson" + "log" "math/rand" "strings" "time" @@ -37,7 +38,6 @@ func NewRememberPlugin(b *bot.Bot) *RememberPlugin { // users message. Otherwise, the function returns false and the bot continues // execution of other plugins. func (p *RememberPlugin) Message(message bot.Message) bool { - // This bot does not reply to anything if message.Body == "quote" && message.Command { q := p.randQuote() @@ -58,7 +58,11 @@ func (p *RememberPlugin) Message(message bot.Message) bool { for i := len(p.Log[message.Channel]) - 1; i >= 0; i-- { entry := p.Log[message.Channel][i] - if entry.User.Name == nick && strings.Contains(entry.Body, snip) { + if strings.ToLower(entry.User.Name) == strings.ToLower(nick) && + strings.Contains( + strings.ToLower(entry.Body), + strings.ToLower(snip), + ) { // insert new remember entry var msg string @@ -171,3 +175,19 @@ func (p *RememberPlugin) quoteTimer(channel string) { func (p *RememberPlugin) Event(kind string, message bot.Message) bool { return false } + +// Record what the bot says in the log +func (p *RememberPlugin) BotMessage(message bot.Message) bool { + log.Printf("Recieved: %+v\n", message) + p.Log[message.Channel] = append(p.Log[message.Channel], message) + + for ch, _ := range p.Log { + log.Printf("Channel: %+v\n", ch) + for _, msg := range p.Log[ch] { + log.Println(msg) + } + } + log.Printf("Log:\n%+v\n", p.Log) + + return false +} diff --git a/plugins/skeleton.go b/plugins/skeleton.go index 4eea857..a3ab3f4 100644 --- a/plugins/skeleton.go +++ b/plugins/skeleton.go @@ -39,3 +39,8 @@ func (p *SkeletonPlugin) Help(channel string, parts []string) { func (p *SkeletonPlugin) Event(kind string, message bot.Message) bool { return false } + +// Handler for bot's own messages +func (p *SkeletonPlugin) BotMessage(message bot.Message) bool { + return false +} diff --git a/plugins/talker.go b/plugins/talker.go index 05fd4b9..0070bfb 100644 --- a/plugins/talker.go +++ b/plugins/talker.go @@ -1,8 +1,8 @@ package plugins import ( - "github.com/chrissexton/alepale/bot" "fmt" + "github.com/chrissexton/alepale/bot" "math/rand" "strings" "time" @@ -105,3 +105,8 @@ func (p *TalkerPlugin) Event(kind string, message bot.Message) bool { } return false } + +// Handler for bot's own messages +func (p *TalkerPlugin) BotMessage(message bot.Message) bool { + return false +} diff --git a/plugins/twitter.go b/plugins/twitter.go index 9637524..d0a71f5 100644 --- a/plugins/twitter.go +++ b/plugins/twitter.go @@ -1,8 +1,8 @@ package plugins import ( - "github.com/chrissexton/alepale/bot" "bitbucket.org/phlyingpenguin/twitter" + "github.com/chrissexton/alepale/bot" "github.com/garyburd/go-oauth/oauth" "labix.org/v2/mgo" "labix.org/v2/mgo/bson" @@ -129,3 +129,8 @@ func (p *TwitterPlugin) checkMessages() { } } } + +// Handler for bot's own messages +func (p *TwitterPlugin) BotMessage(message bot.Message) bool { + return false +} diff --git a/plugins/your.go b/plugins/your.go index d442ba4..fc5ba71 100644 --- a/plugins/your.go +++ b/plugins/your.go @@ -52,3 +52,8 @@ func (p *YourPlugin) Help(channel string, parts []string) { func (p *YourPlugin) Event(kind string, message bot.Message) bool { return false } + +// Handler for bot's own messages +func (p *YourPlugin) BotMessage(message bot.Message) bool { + return false +}