-
Notifications
You must be signed in to change notification settings - Fork 0
/
monitor.go
240 lines (196 loc) · 6 KB
/
monitor.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
// Copyright (c) 2016-2017 Daniel Oaks <daniel@danieloaks.net>
// released under the MIT license
package irc
import (
"strconv"
"strings"
"github.com/goshuirc/irc-go/ircmsg"
)
// alertMonitors alerts everyone monitoring us that we're online.
func (client *Client) alertMonitors() {
// get monitors
client.server.monitoringMutex.RLock()
monitors := client.server.monitoring[client.nickCasefolded]
client.server.monitoringMutex.RUnlock()
// alert monitors
for _, mClient := range monitors {
// don't have to notify ourselves
if mClient != client {
mClient.SendFromClient("", client, nil, RPL_MONONLINE, mClient.nick, client.nickMaskString)
}
}
}
// clearMonitorList clears our MONITOR list.
func (client *Client) clearMonitorList() {
// lockin' everything
client.monitoringMutex.Lock()
defer client.monitoringMutex.Unlock()
client.server.monitoringMutex.Lock()
defer client.server.monitoringMutex.Unlock()
for name := range client.monitoring {
// just removes current client from the list
orig := client.server.monitoring[name]
var index int
for i, cli := range orig {
if cli == client {
index = i
break
}
}
client.server.monitoring[name] = append(orig[:index], orig[index+1:]...)
}
client.monitoring = make(map[string]bool)
}
var (
metadataSubcommands = map[string]func(server *Server, client *Client, msg ircmsg.IrcMessage) bool{
"-": monitorRemoveHandler,
"+": monitorAddHandler,
"c": monitorClearHandler,
"l": monitorListHandler,
"s": monitorStatusHandler,
}
)
func monitorHandler(server *Server, client *Client, msg ircmsg.IrcMessage) bool {
handler, exists := metadataSubcommands[strings.ToLower(msg.Params[0])]
if !exists {
client.Send(nil, server.name, ERR_UNKNOWNERROR, client.nick, "MONITOR", msg.Params[0], "Unknown subcommand")
return false
}
return handler(server, client, msg)
}
func monitorRemoveHandler(server *Server, client *Client, msg ircmsg.IrcMessage) bool {
if len(msg.Params) < 2 {
client.Send(nil, server.name, ERR_NEEDMOREPARAMS, client.nick, msg.Command, "Not enough parameters")
return false
}
targets := strings.Split(msg.Params[1], ",")
for len(targets) > 0 {
// check name length
if len(targets[0]) < 1 {
targets = targets[1:]
continue
}
// remove target
casefoldedTarget, err := CasefoldName(targets[0])
if err != nil {
// skip silently I guess
targets = targets[1:]
continue
}
client.monitoringMutex.Lock()
client.server.monitoringMutex.Lock()
if client.monitoring[casefoldedTarget] {
// just removes current client from the list
orig := server.monitoring[casefoldedTarget]
var index int
for i, cli := range orig {
if cli == client {
index = i
break
}
}
server.monitoring[casefoldedTarget] = append(orig[:index], orig[index+1:]...)
delete(client.monitoring, casefoldedTarget)
}
client.monitoringMutex.Unlock()
client.server.monitoringMutex.Unlock()
// remove first element of targets list
targets = targets[1:]
}
return false
}
func monitorAddHandler(server *Server, client *Client, msg ircmsg.IrcMessage) bool {
if len(msg.Params) < 2 {
client.Send(nil, server.name, ERR_NEEDMOREPARAMS, client.nick, msg.Command, "Not enough parameters")
return false
}
var online []string
var offline []string
targets := strings.Split(msg.Params[1], ",")
for len(targets) > 0 {
// check name length
if len(targets[0]) < 1 || len(targets[0]) > server.limits.NickLen {
targets = targets[1:]
continue
}
// check the monitor list length
if len(client.monitoring) >= server.limits.MonitorEntries {
client.Send(nil, server.name, ERR_MONLISTFULL, client.nick, strconv.Itoa(server.limits.MonitorEntries), strings.Join(targets, ","))
break
}
// add target
casefoldedTarget, err := CasefoldName(targets[0])
if err != nil {
// skip silently I guess
targets = targets[1:]
continue
}
client.monitoringMutex.Lock()
client.server.monitoringMutex.Lock()
if !client.monitoring[casefoldedTarget] {
client.monitoring[casefoldedTarget] = true
orig := server.monitoring[casefoldedTarget]
server.monitoring[casefoldedTarget] = append(orig, client)
}
client.monitoringMutex.Unlock()
client.server.monitoringMutex.Unlock()
// add to online / offline lists
target := server.clients.Get(casefoldedTarget)
if target == nil {
offline = append(offline, targets[0])
} else {
online = append(online, target.nickMaskString)
}
// remove first element of targets list
targets = targets[1:]
}
if len(online) > 0 {
client.Send(nil, server.name, RPL_MONONLINE, client.nick, strings.Join(online, ","))
}
if len(offline) > 0 {
client.Send(nil, server.name, RPL_MONOFFLINE, client.nick, strings.Join(offline, ","))
}
return false
}
func monitorClearHandler(server *Server, client *Client, msg ircmsg.IrcMessage) bool {
client.clearMonitorList()
return false
}
func monitorListHandler(server *Server, client *Client, msg ircmsg.IrcMessage) bool {
var monitorList []string
client.monitoringMutex.RLock()
for name := range client.monitoring {
monitorList = append(monitorList, name)
}
client.monitoringMutex.RUnlock()
for _, line := range argsToStrings(maxLastArgLength, monitorList, ",") {
client.Send(nil, server.name, RPL_MONLIST, client.nick, line)
}
return false
}
func monitorStatusHandler(server *Server, client *Client, msg ircmsg.IrcMessage) bool {
var online []string
var offline []string
client.monitoringMutex.RLock()
monitoring := client.monitoring
client.monitoringMutex.RUnlock()
for name := range monitoring {
target := server.clients.Get(name)
if target == nil {
offline = append(offline, name)
} else {
online = append(online, target.nickMaskString)
}
}
if len(online) > 0 {
for _, line := range argsToStrings(maxLastArgLength, online, ",") {
client.Send(nil, server.name, RPL_MONONLINE, client.nick, line)
}
}
if len(offline) > 0 {
for _, line := range argsToStrings(maxLastArgLength, offline, ",") {
client.Send(nil, server.name, RPL_MONOFFLINE, client.nick, line)
}
}
return false
}