From e1c841ecaefe7190b7cad9936b2e68ad7ae53b25 Mon Sep 17 00:00:00 2001 From: Chris Sexton Date: Fri, 17 Aug 2012 17:37:49 -0400 Subject: [PATCH] Added mgo dependencies, basis for database access among modules --- bot/bot.go | 33 +++++++++++++++++++++++---------- bot/handlers.go | 6 +++--- config/config.go | 14 +++++++------- main.go | 6 +++--- 4 files changed, 36 insertions(+), 23 deletions(-) diff --git a/bot/bot.go b/bot/bot.go index 5ae2bd8..e74f1bf 100644 --- a/bot/bot.go +++ b/bot/bot.go @@ -1,41 +1,54 @@ package bot import irc "github.com/fluffle/goirc/client" +import "labix.org/v2/mgo" +import "godeepintir/config" // Bot type provides storage for bot-wide information, configs, and database connections type Bot struct { Plugins []Handler - Users []User - Conn *irc.Conn + Users []User + Conn *irc.Conn // mongodb connection will go here + DbSession *mgo.Session + Db *mgo.Database } // User type stores user history. This is a vehicle that will follow the user for the active // session type User struct { // Current nickname known - Name string + Name string // LastSeen DateTime // Alternative nicknames seen - Alts []string + Alts []string // Last N messages sent to the user - MessageLog []string + MessageLog []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(c *irc.Conn, p ...Handler) *Bot { +func NewBot(config *config.Config, c *irc.Conn, p ...Handler) *Bot { + session, err := mgo.Dial(config.DbServer) + if err != nil { + panic(err) + } + + db := session.DB(config.DbName) + return &Bot{ - Plugins: p, - Users: make([]User, 10), - Conn: c, + Plugins: p, + Users: make([]User, 10), + Conn: c, + DbSession: session, + Db: db, } } // Adds a constructed handler to the bots handlers list func (b *Bot) AddHandler(h Handler) { b.Plugins = append(b.Plugins, h) -} \ No newline at end of file +} diff --git a/bot/handlers.go b/bot/handlers.go index 7d9df30..f7c9996 100644 --- a/bot/handlers.go +++ b/bot/handlers.go @@ -22,8 +22,8 @@ func (b *Bot) checkuser(nick string) *User { if user == nil { fmt.Println("Making a new user") user = &User{ - Name: nick, - Alts: make([]string, 1), + Name: nick, + Alts: make([]string, 1), MessageLog: make([]string, 50), } b.Users = append(b.Users, *user) @@ -58,4 +58,4 @@ func (b *Bot) filter(input string) string { // Sends message to channel func (b *Bot) SendMessage(channel, message string) { b.Conn.Privmsg(channel, message) -} \ No newline at end of file +} diff --git a/config/config.go b/config/config.go index f1732c3..d224202 100644 --- a/config/config.go +++ b/config/config.go @@ -7,14 +7,14 @@ import "io/ioutil" // Config stores any system-wide startup information that cannot be easily configured via // the database type Config struct { - Dbname string - Dbserver string - Channels []string - Plugins []string - Nick, Server, Pass string - } + DbName string + DbServer string + Channels []string + Plugins []string + Nick, Server, Pass string +} - // Readconfig loads the config data out of a JSON file located in cfile +// Readconfig loads the config data out of a JSON file located in cfile func Readconfig(cfile string) *Config { fmt.Printf("Using %s as config file.\n", cfile) file, e := ioutil.ReadFile(cfile) diff --git a/main.go b/main.go index 55839df..900b454 100644 --- a/main.go +++ b/main.go @@ -3,8 +3,8 @@ package main import ( "flag" "fmt" - "godeepintir/config" "godeepintir/bot" + "godeepintir/config" "godeepintir/plugins" ) import irc "github.com/fluffle/goirc/client" @@ -30,7 +30,7 @@ func main() { // Add handlers to do things here! // e.g. join a channel on connect. c.AddHandler("connected", - func(conn *irc.Conn, line *irc.Line) { + func(conn *irc.Conn, line *irc.Line) { for _, channel := range config.Channels { conn.Join(channel) } @@ -41,7 +41,7 @@ func main() { c.AddHandler("disconnected", func(conn *irc.Conn, line *irc.Line) { quit <- true }) - b := bot.NewBot(c) + b := bot.NewBot(config, c) b.AddHandler(plugins.NewTestPlugin(b)) c.AddHandler("PRIVMSG", func(conn *irc.Conn, line *irc.Line) {