-
Notifications
You must be signed in to change notification settings - Fork 181
/
snomanager.go
117 lines (91 loc) · 2.52 KB
/
snomanager.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package irc
import (
"fmt"
"sync"
"github.com/goshuirc/irc-go/ircfmt"
"github.com/oragono/oragono/irc/sno"
)
// SnoManager keeps track of which clients to send snomasks to.
type SnoManager struct {
sendListMutex sync.RWMutex
sendLists map[sno.Mask]map[*Client]bool
}
// NewSnoManager returns a new SnoManager
func NewSnoManager() *SnoManager {
var m SnoManager
m.sendLists = make(map[sno.Mask]map[*Client]bool)
return &m
}
// 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]
if currentClientList == nil || len(currentClientList) == 0 {
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]
if currentClientList == nil || len(currentClientList) == 0 {
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]
if currentClientList == nil || len(currentClientList) == 0 {
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)
}
}
// String returns the snomasks currently enabled.
func (m *SnoManager) String(client *Client) string {
m.sendListMutex.RLock()
defer m.sendListMutex.RUnlock()
var masks string
for mask, clients := range m.sendLists {
for c := range clients {
if c == client {
masks += string(mask)
break
}
}
}
return masks
}