From f31362fca4b7ac857ab826d0d3ef0a11f91a2833 Mon Sep 17 00:00:00 2001 From: Chris Sexton Date: Fri, 17 Aug 2012 18:09:29 -0400 Subject: [PATCH] Added the felpsbot talker which exhibits the formatter a little bit. This is a function that will be used to transform strings that include $variables into strings that include the original meaning. This eventually needs to be some kind of configurable thing so that new $vars can be added dynamically, however some of the $vars are static and need not change. --- bot/bot.go | 21 ++++++++++++++++++--- bot/handlers.go | 19 ++++++++++++++++--- config.json | 1 + config/config.go | 1 + main.go | 3 ++- plugins/plugins.go | 12 ++++++++---- plugins/talker.go | 39 +++++++++++++++++++++++++++++++++++++++ 7 files changed, 85 insertions(+), 11 deletions(-) create mode 100644 plugins/talker.go diff --git a/bot/bot.go b/bot/bot.go index e74f1bf..1cfa667 100644 --- a/bot/bot.go +++ b/bot/bot.go @@ -6,10 +6,19 @@ import "godeepintir/config" // Bot type provides storage for bot-wide information, configs, and database connections 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 []Handler - Users []User - Conn *irc.Conn - // mongodb connection will go here + + // Users holds information about all of our friends + Users []User + + // Conn allows us to send messages and modify our connection state + Conn *irc.Conn + + Config *config.Config + + // Mongo connection and db allow botwide access to the database DbSession *mgo.Session Db *mgo.Database } @@ -29,6 +38,11 @@ type User struct { MessageLog []string } +type Message struct { + User *User + Channel, Body string +} + // NewBot creates a Bot for a given connection and set of handlers. The handlers must not // require the bot as input for their creation (so use AddHandler instead to add handlers) func NewBot(config *config.Config, c *irc.Conn, p ...Handler) *Bot { @@ -40,6 +54,7 @@ func NewBot(config *config.Config, c *irc.Conn, p ...Handler) *Bot { db := session.DB(config.DbName) return &Bot{ + Config: config, Plugins: p, Users: make([]User, 10), Conn: c, diff --git a/bot/handlers.go b/bot/handlers.go index f7c9996..d311fc5 100644 --- a/bot/handlers.go +++ b/bot/handlers.go @@ -2,12 +2,13 @@ package bot import ( "fmt" + "strings" ) import irc "github.com/fluffle/goirc/client" // Interface used for compatibility with the Plugin interface type Handler interface { - Message(user *User, channel, message string) bool + Message(message Message) bool } // Checks to see if our user exists and if any changes have occured to it @@ -44,14 +45,26 @@ func (b *Bot) Msg_recieved(conn *irc.Conn, line *irc.Line) { fmt.Printf("In %s, %s said: '%s'\n", channel, line.Nick, message) for _, p := range b.Plugins { - if p.Message(user, channel, message) { + msg := Message{ + User: user, + Channel: channel, + Body: message, + } + if p.Message(msg) { break } } } // Take an input string and mutate it based on $vars in the string -func (b *Bot) filter(input string) string { +func (b *Bot) Filter(message Message, input string) string { + if strings.Contains(input, "$NICK") { + nick := strings.ToUpper(message.User.Name) + input = strings.Replace(input, "$NICK", nick, -1) + } else if strings.Contains(input, "$nick") { + nick := message.User.Name + input = strings.Replace(input, "$nick", nick, -1) + } return input } diff --git a/config.json b/config.json index a433788..29c8190 100644 --- a/config.json +++ b/config.json @@ -2,6 +2,7 @@ "Dbname": "deepintir", "Dbserver": "127.0.0.1", "Channels": ["#AlePaleTest"], + "MainChannel": "#AlePaleTest", "Plugins": [], "Server": "127.0.0.1:6666", "Nick": "AlePaleTest", diff --git a/config/config.go b/config/config.go index d224202..0bc9a55 100644 --- a/config/config.go +++ b/config/config.go @@ -10,6 +10,7 @@ type Config struct { DbName string DbServer string Channels []string + MainChannel string Plugins []string Nick, Server, Pass string } diff --git a/main.go b/main.go index 900b454..db30d7a 100644 --- a/main.go +++ b/main.go @@ -42,7 +42,8 @@ func main() { func(conn *irc.Conn, line *irc.Line) { quit <- true }) b := bot.NewBot(config, c) - b.AddHandler(plugins.NewTestPlugin(b)) + // b.AddHandler(plugins.NewTestPlugin(b)) + b.AddHandler(plugins.NewTalkerPlugin(b)) c.AddHandler("PRIVMSG", func(conn *irc.Conn, line *irc.Line) { b.Msg_recieved(conn, line) diff --git a/plugins/plugins.go b/plugins/plugins.go index 3a6ca4e..441156a 100644 --- a/plugins/plugins.go +++ b/plugins/plugins.go @@ -5,7 +5,7 @@ import "godeepintir/bot" // Plugin interface defines the methods needed to accept a plugin type Plugin interface { - Message(user *bot.User, channel, message string) bool + Message(message bot.Message) bool LoadData() } @@ -33,10 +33,14 @@ func (p *TestPlugin) LoadData() { p.Feces = config.Values["Feces"].(string) } -func (p *TestPlugin) Message(user *bot.User, channel, message string) bool { - fmt.Println(user, message) +func (p *TestPlugin) Message(message bot.Message) bool { + user := message.User + channel := message.Channel + body := message.Body + + fmt.Println(user, body) fmt.Println("My plugin name is:", p.Name, " My feces are:", p.Feces) - p.Bot.SendMessage(channel, message) + p.Bot.SendMessage(channel, body) return true } diff --git a/plugins/talker.go b/plugins/talker.go new file mode 100644 index 0000000..fa80280 --- /dev/null +++ b/plugins/talker.go @@ -0,0 +1,39 @@ +package plugins + +import ( + "godeepintir/bot" + "strings" +) + +type TalkerPlugin struct { + Bot *bot.Bot +} + +func NewTalkerPlugin(bot *bot.Bot) *TalkerPlugin { + return &TalkerPlugin{ + Bot: bot, + } +} + +func (p *TalkerPlugin) Message(message bot.Message) bool { + channel := message.Channel + body := message.Body + + if channel != p.Bot.Config.MainChannel { + return false + } + + lowermessage := strings.ToLower(body) + + if strings.Contains(lowermessage, "felps") || strings.Contains(lowermessage, "fredfelps") { + outmsg := p.Bot.Filter(message, "GOD HATES $NICK") + p.Bot.SendMessage(channel, outmsg) + return true + } + + return false +} + +func (p *TalkerPlugin) LoadData() { + // no data to load yet? +}