// Set the user's nick. func (u *User) SetNick(nick string) os.Error { if !parser.ValidNick(nick) { return parser.NewNumeric(parser.ERR_ERRONEUSNICKNAME, nick) } lownick := parser.ToLower(nick) userMutex.Lock() defer userMutex.Unlock() if id, used := userNicks[lownick]; used { if id == u.ID() { return nil } return parser.NewNumeric(parser.ERR_NICKNAMEINUSE, nick) } userNicks[lownick] = u.ID() lownick = parser.ToLower(u.nick) userNicks[lownick] = "", false u.mutex.Lock() defer u.mutex.Unlock() u.nick = nick u.ts = time.Nanoseconds() return nil }
func Import(uid, nick, user, host, ip, hops, ts, name string) os.Error { userMutex.Lock() defer userMutex.Unlock() if _, ok := userMap[uid]; ok { return os.NewError("UID collision") } lownick := parser.ToLower(nick) if _, ok := userNicks[lownick]; ok { return os.NewError("NICK collision") } its, _ := strconv.Atoi64(ts) u := &User{ mutex: new(sync.RWMutex), ts: its, id: uid, user: user, nick: nick, name: name, utyp: RegisteredAsUser, } userMap[uid] = u userNicks[lownick] = uid return nil }
// Get the Channel structure for the given channel. If it does not exist and // create is true, it is created. func Get(name string, create bool) (*Channel, os.Error) { chanMutex.Lock() defer chanMutex.Unlock() if !parser.ValidChannel(name) { return nil, parser.NewNumeric(parser.ERR_NOSUCHCHANNEL, name) } lowname := parser.ToLower(name) // Database lookup? if c, ok := chanMap[lowname]; ok { return c, nil } else if !create { return nil, parser.NewNumeric(parser.ERR_NOSUCHCHANNEL, name) } c := &Channel{ mutex: new(sync.RWMutex), name: name, users: make(map[string]string), } chanMap[lowname] = c return c, nil }
// Get the ID for a particular nick. func GetID(nick string) (id string, err os.Error) { userMutex.RLock() defer userMutex.RUnlock() lownick := parser.ToLower(nick) if _, ok := userMap[nick]; ok { return nick, nil } var ok bool if id, ok = userNicks[lownick]; !ok { err = parser.NewNumeric(parser.ERR_NOSUCHNICK, nick) } return }