Added mgo dependencies, basis for database access among modules

This commit is contained in:
Chris Sexton 2012-08-17 17:37:49 -04:00
parent 4b9b8fa682
commit e1c841ecae
4 changed files with 36 additions and 23 deletions

View File

@ -1,37 +1,50 @@
package bot package bot
import irc "github.com/fluffle/goirc/client" 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 // Bot type provides storage for bot-wide information, configs, and database connections
type Bot struct { type Bot struct {
Plugins []Handler Plugins []Handler
Users []User Users []User
Conn *irc.Conn Conn *irc.Conn
// mongodb connection will go here // 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 // User type stores user history. This is a vehicle that will follow the user for the active
// session // session
type User struct { type User struct {
// Current nickname known // Current nickname known
Name string Name string
// LastSeen DateTime // LastSeen DateTime
// Alternative nicknames seen // Alternative nicknames seen
Alts []string Alts []string
// Last N messages sent to the user // 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 // 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) // 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{ return &Bot{
Plugins: p, Plugins: p,
Users: make([]User, 10), Users: make([]User, 10),
Conn: c, Conn: c,
DbSession: session,
Db: db,
} }
} }

View File

@ -22,8 +22,8 @@ func (b *Bot) checkuser(nick string) *User {
if user == nil { if user == nil {
fmt.Println("Making a new user") fmt.Println("Making a new user")
user = &User{ user = &User{
Name: nick, Name: nick,
Alts: make([]string, 1), Alts: make([]string, 1),
MessageLog: make([]string, 50), MessageLog: make([]string, 50),
} }
b.Users = append(b.Users, *user) b.Users = append(b.Users, *user)

View File

@ -7,14 +7,14 @@ import "io/ioutil"
// Config stores any system-wide startup information that cannot be easily configured via // Config stores any system-wide startup information that cannot be easily configured via
// the database // the database
type Config struct { type Config struct {
Dbname string DbName string
Dbserver string DbServer string
Channels []string Channels []string
Plugins []string Plugins []string
Nick, Server, Pass 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 { func Readconfig(cfile string) *Config {
fmt.Printf("Using %s as config file.\n", cfile) fmt.Printf("Using %s as config file.\n", cfile)
file, e := ioutil.ReadFile(cfile) file, e := ioutil.ReadFile(cfile)

View File

@ -3,8 +3,8 @@ package main
import ( import (
"flag" "flag"
"fmt" "fmt"
"godeepintir/config"
"godeepintir/bot" "godeepintir/bot"
"godeepintir/config"
"godeepintir/plugins" "godeepintir/plugins"
) )
import irc "github.com/fluffle/goirc/client" import irc "github.com/fluffle/goirc/client"
@ -41,7 +41,7 @@ func main() {
c.AddHandler("disconnected", c.AddHandler("disconnected",
func(conn *irc.Conn, line *irc.Line) { quit <- true }) func(conn *irc.Conn, line *irc.Line) { quit <- true })
b := bot.NewBot(c) b := bot.NewBot(config, c)
b.AddHandler(plugins.NewTestPlugin(b)) b.AddHandler(plugins.NewTestPlugin(b))
c.AddHandler("PRIVMSG", func(conn *irc.Conn, line *irc.Line) { c.AddHandler("PRIVMSG", func(conn *irc.Conn, line *irc.Line) {