catbase/bot/users.go

113 lines
2.1 KiB
Go
Raw Normal View History

package bot
import (
// "labix.org/v2/mgo"
2013-09-02 17:34:20 +00:00
"labix.org/v2/mgo"
"labix.org/v2/mgo/bson"
"log"
)
// 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
// LastSeen DateTime
// Alternative nicknames seen
Alts []string
Parent string
Admin bool
2013-09-02 17:34:20 +00:00
//bot *Bot
}
2013-09-02 17:34:20 +00:00
func (b *Bot) NewUser(nick string) *User {
return &User{
Name: nick,
2013-09-02 17:34:20 +00:00
Admin: b.checkAdmin(nick),
}
}
func (b *Bot) GetUser(nick string) *User {
coll := b.Db.C("users")
2013-09-02 17:34:20 +00:00
query := coll.Find(bson.M{"name": nick})
var user *User
2013-09-02 17:34:20 +00:00
count, err := query.Count()
if err != nil {
user = b.NewUser(nick)
coll.Insert(*user)
} else if count == 1 {
2013-09-02 17:34:20 +00:00
err = query.One(&user)
if err != nil {
2013-09-02 17:35:38 +00:00
log.Printf("ERROR adding user: %s -- %s\n", nick, err)
2013-09-02 17:34:20 +00:00
}
} else if count == 0 {
// create the user
2013-09-02 17:34:20 +00:00
log.Printf("Creating new user: %s\n", nick)
user = b.NewUser(nick)
coll.Insert(user)
} else {
log.Printf("Error: %s appears to have more than one user?\n", nick)
query.One(user)
}
// grab linked user, if any
if user.Parent != "" {
query := coll.Find(bson.M{"Name": user.Parent})
if count, err := query.Count(); err != nil && count == 1 {
query.One(user)
} else {
log.Printf("Error: bad linkage on %s -> %s.\n",
user.Name,
user.Parent)
}
}
found := false
for _, u := range b.Users {
if u.Name == user.Name {
found = true
}
}
if !found {
b.Users = append(b.Users, *user)
}
return user
}
// Modify user entry to be a link to other, return other
2013-09-02 17:34:20 +00:00
func (u *User) LinkUser(coll *mgo.Collection, other *User) *User {
user := u
2013-09-02 17:34:20 +00:00
other.Alts = append(other.Alts, user.Alts...)
user.Alts = []string{}
2013-09-02 17:34:20 +00:00
user.Parent = other.Name
2013-09-02 17:34:20 +00:00
err := coll.Update(bson.M{"name": u.Name}, u)
if err != nil {
log.Printf("Error updating user: %s\n", u.Name)
}
2013-09-02 17:34:20 +00:00
err = coll.Update(bson.M{"name": other.Name}, other)
if err != nil {
2013-09-02 17:34:20 +00:00
log.Printf("Error updating other user: %s\n", other.Name)
}
2013-09-02 17:34:20 +00:00
return other
}
func (b *Bot) checkAdmin(nick string) bool {
for _, u := range b.Config.Admins {
if nick == u {
return true
}
}
return false
}