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 {
|
2017-05-10 03:44:59 +00:00
|
|
|
users map[string]map[string][]*ChannelStoreUser
|
2015-01-21 02:06:34 +00:00
|
|
|
userLock sync.Mutex
|
|
|
|
|
|
|
|
topic map[string]map[string]string
|
|
|
|
topicLock sync.Mutex
|
2015-01-17 01:37:21 +00:00
|
|
|
}
|
|
|
|
|
2017-05-10 03:44:59 +00:00
|
|
|
const userModePrefixes = "~&@%+"
|
|
|
|
const userModeChars = "qaohv"
|
|
|
|
|
|
|
|
type ChannelStoreUser struct {
|
|
|
|
nick string
|
|
|
|
modes string
|
|
|
|
prefix string
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewChannelStoreUser(nick string) *ChannelStoreUser {
|
|
|
|
user := &ChannelStoreUser{nick: nick}
|
|
|
|
|
|
|
|
if i := strings.IndexAny(nick, userModePrefixes); i == 0 {
|
|
|
|
i = strings.Index(userModePrefixes, string(nick[0]))
|
|
|
|
user.modes = string(userModeChars[i])
|
|
|
|
user.nick = nick[1:]
|
|
|
|
user.updatePrefix()
|
|
|
|
}
|
|
|
|
|
|
|
|
return user
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *ChannelStoreUser) String() string {
|
|
|
|
return c.prefix + c.nick
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *ChannelStoreUser) addModes(modes string) {
|
|
|
|
for _, mode := range modes {
|
|
|
|
if strings.Contains(c.modes, string(mode)) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
c.modes += string(mode)
|
|
|
|
}
|
|
|
|
c.updatePrefix()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *ChannelStoreUser) removeModes(modes string) {
|
|
|
|
for _, mode := range modes {
|
|
|
|
c.modes = strings.Replace(c.modes, string(mode), "", 1)
|
|
|
|
}
|
|
|
|
c.updatePrefix()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *ChannelStoreUser) updatePrefix() {
|
|
|
|
for i, mode := range userModeChars {
|
|
|
|
if strings.Contains(c.modes, string(mode)) {
|
|
|
|
c.prefix = string(userModePrefixes[i])
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
c.prefix = ""
|
|
|
|
}
|
|
|
|
|
2015-01-17 01:37:21 +00:00
|
|
|
func NewChannelStore() *ChannelStore {
|
|
|
|
return &ChannelStore{
|
2017-05-10 03:44:59 +00:00
|
|
|
users: make(map[string]map[string][]*ChannelStoreUser),
|
2015-01-21 02:06:34 +00:00
|
|
|
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]))
|
2017-05-10 03:44:59 +00:00
|
|
|
for i, user := range c.users[server][channel] {
|
|
|
|
users[i] = user.String()
|
|
|
|
}
|
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 {
|
2017-05-10 03:44:59 +00:00
|
|
|
c.users[server] = make(map[string][]*ChannelStoreUser)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, nick := range users {
|
|
|
|
c.users[server][channel] = append(c.users[server][channel], NewChannelStoreUser(nick))
|
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) 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 {
|
2017-05-10 03:44:59 +00:00
|
|
|
c.users[server] = make(map[string][]*ChannelStoreUser)
|
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-05-10 03:44:59 +00:00
|
|
|
for _, u := range users {
|
|
|
|
if u.nick == user {
|
2016-01-22 03:45:41 +00:00
|
|
|
c.userLock.Unlock()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-10 03:44:59 +00:00
|
|
|
c.users[server][channel] = append(users, NewChannelStoreUser(user))
|
2015-01-17 01:37:21 +00:00
|
|
|
} else {
|
2017-05-10 03:44:59 +00:00
|
|
|
c.users[server][channel] = []*ChannelStoreUser{NewChannelStoreUser(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()
|
|
|
|
|
2017-05-10 03:44:59 +00:00
|
|
|
for _, u := range c.users[server][channel] {
|
|
|
|
if u.nick == user {
|
|
|
|
u.addModes(add)
|
|
|
|
u.removeModes(remove)
|
|
|
|
|
|
|
|
c.userLock.Unlock()
|
|
|
|
return
|
|
|
|
}
|
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()
|
2017-05-10 03:44:59 +00:00
|
|
|
topic := c.topic[server][channel]
|
|
|
|
c.topicLock.Unlock()
|
|
|
|
return topic
|
2015-01-21 02:06:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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-05-10 03:44:59 +00:00
|
|
|
for _, user := range c.users[server][channel] {
|
|
|
|
if user.nick == oldNick {
|
|
|
|
user.nick = newNick
|
2017-02-21 12:56:04 +00:00
|
|
|
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-05-10 03:44:59 +00:00
|
|
|
for i, u := range c.users[server][channel] {
|
|
|
|
if u.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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|