2017-05-08 01:15:16 +02:00
|
|
|
package irc
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"sync"
|
|
|
|
|
2021-05-25 06:34:38 +02:00
|
|
|
"github.com/ergochat/ergo/irc/sno"
|
2017-06-15 18:14:19 +02:00
|
|
|
"github.com/goshuirc/irc-go/ircfmt"
|
2017-05-08 01:15:16 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// SnoManager keeps track of which clients to send snomasks to.
|
|
|
|
type SnoManager struct {
|
2017-11-22 10:41:11 +01:00
|
|
|
sendListMutex sync.RWMutex // tier 2
|
2017-05-08 01:15:16 +02:00
|
|
|
sendLists map[sno.Mask]map[*Client]bool
|
|
|
|
}
|
|
|
|
|
2019-05-12 10:30:48 +02:00
|
|
|
func (m *SnoManager) Initialize() {
|
2017-05-08 01:15:16 +02:00
|
|
|
m.sendLists = make(map[sno.Mask]map[*Client]bool)
|
|
|
|
}
|
|
|
|
|
|
|
|
// AddMasks adds the given snomasks to the client.
|
|
|
|
func (m *SnoManager) AddMasks(client *Client, masks ...sno.Mask) {
|
|
|
|
m.sendListMutex.Lock()
|
|
|
|
defer m.sendListMutex.Unlock()
|
|
|
|
|
|
|
|
for _, mask := range masks {
|
|
|
|
currentClientList := m.sendLists[mask]
|
|
|
|
|
|
|
|
if currentClientList == nil {
|
|
|
|
currentClientList = map[*Client]bool{}
|
|
|
|
}
|
|
|
|
|
|
|
|
currentClientList[client] = true
|
|
|
|
|
|
|
|
m.sendLists[mask] = currentClientList
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// RemoveMasks removes the given snomasks from the client.
|
|
|
|
func (m *SnoManager) RemoveMasks(client *Client, masks ...sno.Mask) {
|
|
|
|
m.sendListMutex.Lock()
|
|
|
|
defer m.sendListMutex.Unlock()
|
|
|
|
|
|
|
|
for _, mask := range masks {
|
|
|
|
currentClientList := m.sendLists[mask]
|
|
|
|
|
2019-05-10 07:44:14 +02:00
|
|
|
if len(currentClientList) == 0 {
|
2017-05-08 01:15:16 +02:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
delete(currentClientList, client)
|
|
|
|
|
|
|
|
m.sendLists[mask] = currentClientList
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// RemoveClient removes the given client from all of our lists.
|
|
|
|
func (m *SnoManager) RemoveClient(client *Client) {
|
|
|
|
m.sendListMutex.Lock()
|
|
|
|
defer m.sendListMutex.Unlock()
|
|
|
|
|
|
|
|
for mask := range m.sendLists {
|
|
|
|
currentClientList := m.sendLists[mask]
|
|
|
|
|
2019-05-10 07:44:14 +02:00
|
|
|
if len(currentClientList) == 0 {
|
2017-05-08 01:15:16 +02:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
delete(currentClientList, client)
|
|
|
|
|
|
|
|
m.sendLists[mask] = currentClientList
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Send sends the given snomask to all users signed up for it.
|
|
|
|
func (m *SnoManager) Send(mask sno.Mask, content string) {
|
|
|
|
m.sendListMutex.RLock()
|
|
|
|
defer m.sendListMutex.RUnlock()
|
|
|
|
|
|
|
|
currentClientList := m.sendLists[mask]
|
|
|
|
|
2019-05-10 07:44:14 +02:00
|
|
|
if len(currentClientList) == 0 {
|
2017-05-08 01:15:16 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// make the message
|
|
|
|
name := sno.NoticeMaskNames[mask]
|
|
|
|
if name == "" {
|
|
|
|
name = string(mask)
|
|
|
|
}
|
|
|
|
message := fmt.Sprintf(ircfmt.Unescape("$c[grey]-$r%s$c[grey]-$c %s"), name, content)
|
|
|
|
|
|
|
|
// send it out
|
|
|
|
for client := range currentClientList {
|
|
|
|
client.Notice(message)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-08 17:43:13 +01:00
|
|
|
// MasksEnabled returns the snomasks currently enabled.
|
|
|
|
func (m *SnoManager) MasksEnabled(client *Client) (result sno.Masks) {
|
2017-05-08 01:15:16 +02:00
|
|
|
m.sendListMutex.RLock()
|
|
|
|
defer m.sendListMutex.RUnlock()
|
|
|
|
|
|
|
|
for mask, clients := range m.sendLists {
|
|
|
|
for c := range clients {
|
|
|
|
if c == client {
|
2021-02-08 17:43:13 +01:00
|
|
|
result = append(result, mask)
|
2017-05-08 01:15:16 +02:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-02-08 17:43:13 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *SnoManager) String(client *Client) string {
|
|
|
|
masks := m.MasksEnabled(client)
|
|
|
|
return masks.String()
|
2017-05-08 01:15:16 +02:00
|
|
|
}
|