2015-01-17 01:37:21 +00:00
|
|
|
package storage
|
|
|
|
|
|
|
|
import (
|
2015-01-25 01:35:46 +00:00
|
|
|
"strings"
|
2015-01-17 01:37:21 +00:00
|
|
|
"sync"
|
|
|
|
)
|
|
|
|
|
|
|
|
type ChannelStore struct {
|
2015-01-21 02:06:34 +00:00
|
|
|
users map[string]map[string][]string
|
|
|
|
userLock sync.Mutex
|
|
|
|
|
|
|
|
topic map[string]map[string]string
|
|
|
|
topicLock sync.Mutex
|
2015-01-17 01:37:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewChannelStore() *ChannelStore {
|
|
|
|
return &ChannelStore{
|
2015-01-21 02:06:34 +00:00
|
|
|
users: make(map[string]map[string][]string),
|
|
|
|
topic: make(map[string]map[string]string),
|
2015-01-17 01:37:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *ChannelStore) GetUsers(server, channel string) []string {
|
2015-01-21 02:06:34 +00:00
|
|
|
c.userLock.Lock()
|
2015-01-17 01:37:21 +00:00
|
|
|
|
2015-01-21 02:06:34 +00:00
|
|
|
users := make([]string, len(c.users[server][channel]))
|
|
|
|
copy(users, c.users[server][channel])
|
2015-01-17 01:37:21 +00:00
|
|
|
|
2015-01-21 02:06:34 +00:00
|
|
|
c.userLock.Unlock()
|
2015-01-17 01:37:21 +00:00
|
|
|
|
|
|
|
return users
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *ChannelStore) SetUsers(users []string, server, channel string) {
|
2015-01-21 02:06:34 +00:00
|
|
|
c.userLock.Lock()
|
2015-01-17 01:37:21 +00:00
|
|
|
|
2015-01-21 02:06:34 +00:00
|
|
|
if _, ok := c.users[server]; !ok {
|
|
|
|
c.users[server] = make(map[string][]string)
|
2015-01-17 01:37:21 +00:00
|
|
|
}
|
|
|
|
|
2015-01-21 02:06:34 +00:00
|
|
|
c.users[server][channel] = users
|
|
|
|
c.userLock.Unlock()
|
2015-01-17 01:37:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *ChannelStore) AddUser(user, server, channel string) {
|
2015-01-21 02:06:34 +00:00
|
|
|
c.userLock.Lock()
|
2015-01-17 01:37:21 +00:00
|
|
|
|
2015-01-21 02:06:34 +00:00
|
|
|
if _, ok := c.users[server]; !ok {
|
|
|
|
c.users[server] = make(map[string][]string)
|
2015-01-17 01:37:21 +00:00
|
|
|
}
|
|
|
|
|
2015-01-21 02:06:34 +00:00
|
|
|
if users, ok := c.users[server][channel]; ok {
|
2017-02-21 20:38:48 +00:00
|
|
|
for _, nick := range users {
|
|
|
|
if nick == user {
|
2016-01-22 03:45:41 +00:00
|
|
|
c.userLock.Unlock()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-21 02:06:34 +00:00
|
|
|
c.users[server][channel] = append(users, user)
|
2015-01-17 01:37:21 +00:00
|
|
|
} else {
|
2015-01-21 02:06:34 +00:00
|
|
|
c.users[server][channel] = []string{user}
|
2015-01-17 01:37:21 +00:00
|
|
|
}
|
|
|
|
|
2015-01-21 02:06:34 +00:00
|
|
|
c.userLock.Unlock()
|
2015-01-17 01:37:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *ChannelStore) RemoveUser(user, server, channel string) {
|
2015-01-21 02:06:34 +00:00
|
|
|
c.userLock.Lock()
|
|
|
|
c.removeUser(user, server, channel)
|
|
|
|
c.userLock.Unlock()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *ChannelStore) RemoveUserAll(user, server string) {
|
|
|
|
c.userLock.Lock()
|
|
|
|
|
2017-02-21 20:38:48 +00:00
|
|
|
for channel := range c.users[server] {
|
2015-01-21 02:06:34 +00:00
|
|
|
c.removeUser(user, server, channel)
|
|
|
|
}
|
|
|
|
|
|
|
|
c.userLock.Unlock()
|
|
|
|
}
|
|
|
|
|
2015-01-31 22:35:38 +00:00
|
|
|
func (c *ChannelStore) RenameUser(oldNick, newNick, server string) {
|
|
|
|
c.userLock.Lock()
|
|
|
|
c.renameAll(server, oldNick, newNick)
|
|
|
|
c.userLock.Unlock()
|
|
|
|
}
|
|
|
|
|
2015-01-25 01:35:46 +00:00
|
|
|
func (c *ChannelStore) SetMode(server, channel, user, add, remove string) {
|
|
|
|
c.userLock.Lock()
|
|
|
|
|
|
|
|
if strings.Contains(add, "o") {
|
2017-02-21 12:56:04 +00:00
|
|
|
c.setPrefix(server, channel, user, "@")
|
2015-01-25 01:35:46 +00:00
|
|
|
} else if strings.Contains(add, "v") {
|
2017-02-21 12:56:04 +00:00
|
|
|
c.setPrefix(server, channel, user, "+")
|
2015-01-25 01:35:46 +00:00
|
|
|
} else if strings.IndexAny(remove, "ov") > -1 {
|
2017-02-21 12:56:04 +00:00
|
|
|
c.setPrefix(server, channel, user, "")
|
2015-01-25 01:35:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
c.userLock.Unlock()
|
|
|
|
}
|
|
|
|
|
2015-01-21 02:06:34 +00:00
|
|
|
func (c *ChannelStore) GetTopic(server, channel string) string {
|
|
|
|
c.topicLock.Lock()
|
|
|
|
defer c.topicLock.Unlock()
|
|
|
|
|
|
|
|
return c.topic[server][channel]
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *ChannelStore) SetTopic(topic, server, channel string) {
|
|
|
|
c.topicLock.Lock()
|
|
|
|
|
|
|
|
if _, ok := c.topic[server]; !ok {
|
|
|
|
c.topic[server] = make(map[string]string)
|
|
|
|
}
|
|
|
|
|
|
|
|
c.topic[server][channel] = topic
|
|
|
|
c.topicLock.Unlock()
|
|
|
|
}
|
2015-01-17 01:37:21 +00:00
|
|
|
|
2015-01-25 01:35:46 +00:00
|
|
|
func (c *ChannelStore) rename(server, channel, oldNick, newNick string) {
|
2017-02-21 20:38:48 +00:00
|
|
|
for i, nick := range c.users[server][channel] {
|
|
|
|
if strings.TrimLeft(nick, "@+") == oldNick {
|
|
|
|
if nick[0] == '@' || nick[0] == '+' {
|
|
|
|
newNick = nick[:1] + newNick
|
2017-02-21 12:56:04 +00:00
|
|
|
}
|
2015-01-25 01:35:46 +00:00
|
|
|
|
|
|
|
c.users[server][channel][i] = newNick
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-21 12:56:04 +00:00
|
|
|
func (c *ChannelStore) setPrefix(server, channel, user, prefix string) {
|
2017-02-21 20:38:48 +00:00
|
|
|
for i, nick := range c.users[server][channel] {
|
|
|
|
if strings.TrimLeft(nick, "@+") == user {
|
2017-02-21 12:56:04 +00:00
|
|
|
c.users[server][channel][i] = prefix + user
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-31 22:35:38 +00:00
|
|
|
func (c *ChannelStore) renameAll(server, oldNick, newNick string) {
|
2017-02-21 12:56:04 +00:00
|
|
|
for channel := range c.users[server] {
|
2015-01-31 22:35:38 +00:00
|
|
|
c.rename(server, channel, oldNick, newNick)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-21 02:06:34 +00:00
|
|
|
func (c *ChannelStore) removeUser(user, server, channel string) {
|
2017-02-21 20:38:48 +00:00
|
|
|
for i, nick := range c.users[server][channel] {
|
|
|
|
if strings.TrimLeft(nick, "@+") == user {
|
2015-01-21 02:06:34 +00:00
|
|
|
users := c.users[server][channel]
|
|
|
|
c.users[server][channel] = append(users[:i], users[i+1:]...)
|
2015-01-17 01:37:21 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|