|
@@ -22,10 +22,11 @@ var (
|
22
|
22
|
ErrNicknameMismatch = errors.New("nickname mismatch")
|
23
|
23
|
)
|
24
|
24
|
|
|
25
|
+// ExpandUserHost takes a userhost, and returns an expanded version.
|
25
|
26
|
func ExpandUserHost(userhost string) (expanded string) {
|
26
|
27
|
expanded = userhost
|
27
|
28
|
// fill in missing wildcards for nicks
|
28
|
|
- //TODO(dan): this would fail with dan@lol, do we want to accommodate that?
|
|
29
|
+ //TODO(dan): this would fail with dan@lol, fix that.
|
29
|
30
|
if !strings.Contains(expanded, "!") {
|
30
|
31
|
expanded += "!*"
|
31
|
32
|
}
|
|
@@ -35,17 +36,20 @@ func ExpandUserHost(userhost string) (expanded string) {
|
35
|
36
|
return
|
36
|
37
|
}
|
37
|
38
|
|
|
39
|
+// ClientLookupSet represents a way to store, search and lookup clients.
|
38
|
40
|
type ClientLookupSet struct {
|
39
|
41
|
ByNickMutex sync.RWMutex
|
40
|
42
|
ByNick map[string]*Client
|
41
|
43
|
}
|
42
|
44
|
|
|
45
|
+// NewClientLookupSet returns a new lookup set.
|
43
|
46
|
func NewClientLookupSet() *ClientLookupSet {
|
44
|
47
|
return &ClientLookupSet{
|
45
|
48
|
ByNick: make(map[string]*Client),
|
46
|
49
|
}
|
47
|
50
|
}
|
48
|
51
|
|
|
52
|
+// Count returns how many clients are in the lookup set.
|
49
|
53
|
func (clients *ClientLookupSet) Count() int {
|
50
|
54
|
clients.ByNickMutex.RLock()
|
51
|
55
|
defer clients.ByNickMutex.RUnlock()
|
|
@@ -53,7 +57,8 @@ func (clients *ClientLookupSet) Count() int {
|
53
|
57
|
return count
|
54
|
58
|
}
|
55
|
59
|
|
56
|
|
-//TODO(dan): wouldn't it be best to always use Get rather than this?
|
|
60
|
+// Has returns whether or not the given client exists.
|
|
61
|
+//TODO(dan): This seems like ripe ground for a race, if code does Has then Get, and assumes the Get will return a client.
|
57
|
62
|
func (clients *ClientLookupSet) Has(nick string) bool {
|
58
|
63
|
casefoldedName, err := CasefoldName(nick)
|
59
|
64
|
if err == nil {
|
|
@@ -75,6 +80,7 @@ func (clients *ClientLookupSet) getNoMutex(nick string) *Client {
|
75
|
80
|
return nil
|
76
|
81
|
}
|
77
|
82
|
|
|
83
|
+// Get retrieves a client from the set, if they exist.
|
78
|
84
|
func (clients *ClientLookupSet) Get(nick string) *Client {
|
79
|
85
|
casefoldedName, err := CasefoldName(nick)
|
80
|
86
|
if err == nil {
|
|
@@ -86,6 +92,7 @@ func (clients *ClientLookupSet) Get(nick string) *Client {
|
86
|
92
|
return nil
|
87
|
93
|
}
|
88
|
94
|
|
|
95
|
+// Add adds a client to the lookup set.
|
89
|
96
|
func (clients *ClientLookupSet) Add(client *Client, nick string) error {
|
90
|
97
|
nick, err := CasefoldName(nick)
|
91
|
98
|
if err != nil {
|
|
@@ -100,6 +107,7 @@ func (clients *ClientLookupSet) Add(client *Client, nick string) error {
|
100
|
107
|
return nil
|
101
|
108
|
}
|
102
|
109
|
|
|
110
|
+// Remove removes a client from the lookup set.
|
103
|
111
|
func (clients *ClientLookupSet) Remove(client *Client) error {
|
104
|
112
|
if !client.HasNick() {
|
105
|
113
|
return ErrNickMissing
|
|
@@ -113,6 +121,7 @@ func (clients *ClientLookupSet) Remove(client *Client) error {
|
113
|
121
|
return nil
|
114
|
122
|
}
|
115
|
123
|
|
|
124
|
+// Replace renames an existing client in the lookup set.
|
116
|
125
|
func (clients *ClientLookupSet) Replace(oldNick, newNick string, client *Client) error {
|
117
|
126
|
// get casefolded nicknames
|
118
|
127
|
oldNick, err := CasefoldName(oldNick)
|
|
@@ -145,6 +154,7 @@ func (clients *ClientLookupSet) Replace(oldNick, newNick string, client *Client)
|
145
|
154
|
return nil
|
146
|
155
|
}
|
147
|
156
|
|
|
157
|
+// AllWithCaps returns all clients with the given capabilities.
|
148
|
158
|
func (clients *ClientLookupSet) AllWithCaps(caps ...Capability) (set ClientSet) {
|
149
|
159
|
set = make(ClientSet)
|
150
|
160
|
|
|
@@ -165,6 +175,7 @@ func (clients *ClientLookupSet) AllWithCaps(caps ...Capability) (set ClientSet)
|
165
|
175
|
return set
|
166
|
176
|
}
|
167
|
177
|
|
|
178
|
+// FindAll returns all clients that match the given userhost mask.
|
168
|
179
|
func (clients *ClientLookupSet) FindAll(userhost string) (set ClientSet) {
|
169
|
180
|
set = make(ClientSet)
|
170
|
181
|
|
|
@@ -185,6 +196,7 @@ func (clients *ClientLookupSet) FindAll(userhost string) (set ClientSet) {
|
185
|
196
|
return set
|
186
|
197
|
}
|
187
|
198
|
|
|
199
|
+// Find returns the first client that matches the given userhost mask.
|
188
|
200
|
func (clients *ClientLookupSet) Find(userhost string) *Client {
|
189
|
201
|
userhost, err := Casefold(ExpandUserHost(userhost))
|
190
|
202
|
if err != nil {
|