2012-08-17 20:38:15 +00:00
|
|
|
package bot
|
|
|
|
|
|
|
|
import irc "github.com/fluffle/goirc/client"
|
2012-08-17 21:37:49 +00:00
|
|
|
import "labix.org/v2/mgo"
|
|
|
|
import "godeepintir/config"
|
2012-08-17 20:38:15 +00:00
|
|
|
|
|
|
|
// Bot type provides storage for bot-wide information, configs, and database connections
|
|
|
|
type Bot struct {
|
|
|
|
Plugins []Handler
|
2012-08-17 21:37:49 +00:00
|
|
|
Users []User
|
|
|
|
Conn *irc.Conn
|
2012-08-17 20:38:15 +00:00
|
|
|
// mongodb connection will go here
|
2012-08-17 21:37:49 +00:00
|
|
|
DbSession *mgo.Session
|
|
|
|
Db *mgo.Database
|
2012-08-17 20:38:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// User type stores user history. This is a vehicle that will follow the user for the active
|
|
|
|
// session
|
|
|
|
type User struct {
|
|
|
|
// Current nickname known
|
2012-08-17 21:37:49 +00:00
|
|
|
Name string
|
2012-08-17 20:38:15 +00:00
|
|
|
|
|
|
|
// LastSeen DateTime
|
|
|
|
|
|
|
|
// Alternative nicknames seen
|
2012-08-17 21:37:49 +00:00
|
|
|
Alts []string
|
2012-08-17 20:38:15 +00:00
|
|
|
|
|
|
|
// Last N messages sent to the user
|
2012-08-17 21:37:49 +00:00
|
|
|
MessageLog []string
|
2012-08-17 20:38:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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)
|
2012-08-17 21:37:49 +00:00
|
|
|
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)
|
|
|
|
|
2012-08-17 20:38:15 +00:00
|
|
|
return &Bot{
|
2012-08-17 21:37:49 +00:00
|
|
|
Plugins: p,
|
|
|
|
Users: make([]User, 10),
|
|
|
|
Conn: c,
|
|
|
|
DbSession: session,
|
|
|
|
Db: db,
|
2012-08-17 20:38:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Adds a constructed handler to the bots handlers list
|
|
|
|
func (b *Bot) AddHandler(h Handler) {
|
|
|
|
b.Plugins = append(b.Plugins, h)
|
2012-08-17 21:37:49 +00:00
|
|
|
}
|