From b418096bf6dc42634816535df1f4fbd9aa1bc650 Mon Sep 17 00:00:00 2001 From: Chris Sexton Date: Sun, 26 Aug 2012 19:23:51 -0400 Subject: [PATCH] Added dynamic variables (I'm sure this is going to backfire) --- bot/bot.go | 9 +++++++++ bot/handlers.go | 33 +++++++++++++++++++++++++++++++++ config.json | 1 + config/config.go | 1 + main.go | 1 + 5 files changed, 45 insertions(+) diff --git a/bot/bot.go b/bot/bot.go index 3e2eab3..92a4220 100644 --- a/bot/bot.go +++ b/bot/bot.go @@ -24,6 +24,8 @@ type Bot struct { DbSession *mgo.Session Db *mgo.Database + varColl *mgo.Collection + Version string } @@ -40,6 +42,8 @@ type User struct { // Last N messages sent to the user MessageLog []string + + Admin bool } type Message struct { @@ -50,6 +54,10 @@ type Message struct { Action bool } +type Variable struct { + Variable, Value string +} + // NewBot creates a Bot for a given connection and set of handlers. func NewBot(config *config.Config, c *irc.Conn) *Bot { session, err := mgo.Dial(config.DbServer) @@ -67,6 +75,7 @@ func NewBot(config *config.Config, c *irc.Conn) *Bot { Conn: c, DbSession: session, Db: db, + varColl: db.C("variables"), Version: config.Version, } } diff --git a/bot/handlers.go b/bot/handlers.go index 8700d06..2c03b29 100644 --- a/bot/handlers.go +++ b/bot/handlers.go @@ -6,6 +6,9 @@ import ( "strconv" "strings" "time" + // "labix.org/v2/mgo" + "labix.org/v2/mgo/bson" + "regexp" ) import irc "github.com/fluffle/goirc/client" @@ -27,10 +30,17 @@ func (b *Bot) checkuser(nick string) *User { } } if user == nil { + isadmin := false + for _, u := range b.Config.Admins { + if nick == u { + isadmin = true + } + } user = &User{ Name: nick, Alts: make([]string, 1), MessageLog: make([]string, 50), + Admin: isadmin, } b.Users = append(b.Users, *user) } @@ -150,6 +160,8 @@ func (b *Bot) Filter(message Message, input string) string { input = strings.Replace(input, "$NICK", nick, -1) } + // Let's be bucket compatible for this var + strings.Replace(input, "$who", "$nick", -1) if strings.Contains(input, "$nick") { nick := message.User.Name input = strings.Replace(input, "$nick", nick, -1) @@ -170,6 +182,27 @@ func (b *Bot) Filter(message Message, input string) string { input = strings.Replace(input, "$nonzero", num, 1) } + r, err := regexp.Compile("\\$[A-z]+") + if err != nil { + panic(err) + } + + varname := r.FindString(input) + blacklist := make(map[string]bool) + blacklist["$and"] = true + for len(varname) > 0 && !blacklist[varname] { + fmt.Printf("Trying to match '%s'\n", varname) + var result []Variable + b.varColl.Find(bson.M{"variable": varname}).All(&result) + if len(result) == 0 { + blacklist[varname] = true + continue + } + variable := result[rand.Intn(len(result))] + input = strings.Replace(input, varname, variable.Value, 1) + varname = r.FindString(input) + } + return input } diff --git a/config.json b/config.json index 81cd8a2..efc8051 100644 --- a/config.json +++ b/config.json @@ -11,6 +11,7 @@ "QuoteChance": 0.10, "QuoteTime": 30, "LogLength": 50, + "Admins": ["flyngpngn"], "comment": "Follows is the old bot", diff --git a/config/config.go b/config/config.go index f188b6e..7d63fd3 100644 --- a/config/config.go +++ b/config/config.go @@ -18,6 +18,7 @@ type Config struct { QuoteChance float64 QuoteTime int LogLength int + Admins []string } // Readconfig loads the config data out of a JSON file located in cfile diff --git a/main.go b/main.go index 2efcd9c..a69d77c 100644 --- a/main.go +++ b/main.go @@ -43,6 +43,7 @@ func main() { b := bot.NewBot(config, c) // b.AddHandler(plugins.NewTestPlugin(b)) + b.AddHandler("admin", plugins.NewAdminPlugin(b)) b.AddHandler("talker", plugins.NewTalkerPlugin(b)) b.AddHandler("beers", plugins.NewBeersPlugin(b)) b.AddHandler("remember", plugins.NewRememberPlugin(b))