Clean up ChannelStore

This commit is contained in:
Ken-Håvard Lieng 2017-02-21 21:38:48 +01:00
parent 9f055da1a0
commit 7c7821f829

View File

@ -39,7 +39,6 @@ func (c *ChannelStore) SetUsers(users []string, server, channel string) {
} }
c.users[server][channel] = users c.users[server][channel] = users
c.userLock.Unlock() c.userLock.Unlock()
} }
@ -51,8 +50,8 @@ func (c *ChannelStore) AddUser(user, server, channel string) {
} }
if users, ok := c.users[server][channel]; ok { if users, ok := c.users[server][channel]; ok {
for _, u := range users { for _, nick := range users {
if u == user { if nick == user {
c.userLock.Unlock() c.userLock.Unlock()
return return
} }
@ -75,7 +74,7 @@ func (c *ChannelStore) RemoveUser(user, server, channel string) {
func (c *ChannelStore) RemoveUserAll(user, server string) { func (c *ChannelStore) RemoveUserAll(user, server string) {
c.userLock.Lock() c.userLock.Lock()
for channel, _ := range c.users[server] { for channel := range c.users[server] {
c.removeUser(user, server, channel) c.removeUser(user, server, channel)
} }
@ -106,14 +105,16 @@ func (c *ChannelStore) FindUserChannels(user, server string) []string {
var channels []string var channels []string
c.userLock.Lock() c.userLock.Lock()
for channel, users := range c.users[server] { for channel, users := range c.users[server] {
for _, nick := range users { for _, nick := range users {
if user == strings.TrimLeft(nick, "@+") { if strings.TrimLeft(nick, "@+") == user {
channels = append(channels, channel) channels = append(channels, channel)
break break
} }
} }
} }
c.userLock.Unlock() c.userLock.Unlock()
return channels return channels
@ -138,10 +139,10 @@ func (c *ChannelStore) SetTopic(topic, server, channel string) {
} }
func (c *ChannelStore) rename(server, channel, oldNick, newNick string) { func (c *ChannelStore) rename(server, channel, oldNick, newNick string) {
for i, u := range c.users[server][channel] { for i, nick := range c.users[server][channel] {
if strings.TrimLeft(u, "@+") == oldNick { if strings.TrimLeft(nick, "@+") == oldNick {
if u[0] == '@' || u[0] == '+' { if nick[0] == '@' || nick[0] == '+' {
newNick = u[:1] + newNick newNick = nick[:1] + newNick
} }
c.users[server][channel][i] = newNick c.users[server][channel][i] = newNick
@ -151,8 +152,8 @@ func (c *ChannelStore) rename(server, channel, oldNick, newNick string) {
} }
func (c *ChannelStore) setPrefix(server, channel, user, prefix string) { func (c *ChannelStore) setPrefix(server, channel, user, prefix string) {
for i, u := range c.users[server][channel] { for i, nick := range c.users[server][channel] {
if strings.TrimLeft(u, "@+") == user { if strings.TrimLeft(nick, "@+") == user {
c.users[server][channel][i] = prefix + user c.users[server][channel][i] = prefix + user
return return
} }
@ -166,10 +167,8 @@ func (c *ChannelStore) renameAll(server, oldNick, newNick string) {
} }
func (c *ChannelStore) removeUser(user, server, channel string) { func (c *ChannelStore) removeUser(user, server, channel string) {
for i, u := range c.users[server][channel] { for i, nick := range c.users[server][channel] {
u = strings.TrimLeft(u, "@+") if strings.TrimLeft(nick, "@+") == user {
if u == user {
users := c.users[server][channel] users := c.users[server][channel]
c.users[server][channel] = append(users[:i], users[i+1:]...) c.users[server][channel] = append(users[:i], users[i+1:]...)
return return