2017-03-27 14:15:02 +02:00
|
|
|
// Copyright (c) 2016-2017 Daniel Oaks <daniel@danieloaks.net>
|
2016-10-16 12:14:56 +02:00
|
|
|
// released under the MIT license
|
|
|
|
|
|
|
|
package irc
|
|
|
|
|
|
|
|
import (
|
2017-10-04 06:57:03 +02:00
|
|
|
"sync"
|
2016-10-16 12:14:56 +02:00
|
|
|
|
2017-06-15 18:14:19 +02:00
|
|
|
"github.com/goshuirc/irc-go/ircmsg"
|
2016-10-16 12:14:56 +02:00
|
|
|
)
|
|
|
|
|
2017-10-05 15:29:34 +02:00
|
|
|
// MonitorManager keeps track of who's monitoring which nicks.
|
2017-10-04 06:57:03 +02:00
|
|
|
type MonitorManager struct {
|
2017-11-22 10:41:11 +01:00
|
|
|
sync.RWMutex // tier 2
|
2017-10-04 06:57:03 +02:00
|
|
|
// client -> nicks it's watching
|
|
|
|
watching map[*Client]map[string]bool
|
|
|
|
// nick -> clients watching it
|
|
|
|
watchedby map[string]map[*Client]bool
|
|
|
|
// (all nicks must be normalized externally by casefolding)
|
|
|
|
}
|
|
|
|
|
2019-05-12 10:30:48 +02:00
|
|
|
func (mm *MonitorManager) Initialize() {
|
|
|
|
mm.watching = make(map[*Client]map[string]bool)
|
|
|
|
mm.watchedby = make(map[string]map[*Client]bool)
|
2016-10-16 12:14:56 +02:00
|
|
|
}
|
|
|
|
|
2017-10-04 08:59:59 +02:00
|
|
|
// AlertAbout alerts everyone monitoring `client`'s nick that `client` is now {on,off}line.
|
2020-05-28 23:55:53 +02:00
|
|
|
func (manager *MonitorManager) AlertAbout(nick, cfnick string, online bool) {
|
2017-10-04 06:57:03 +02:00
|
|
|
var watchers []*Client
|
|
|
|
// safely copy the list of clients watching our nick
|
|
|
|
manager.RLock()
|
|
|
|
for client := range manager.watchedby[cfnick] {
|
|
|
|
watchers = append(watchers, client)
|
|
|
|
}
|
|
|
|
manager.RUnlock()
|
|
|
|
|
|
|
|
command := RPL_MONOFFLINE
|
|
|
|
if online {
|
|
|
|
command = RPL_MONONLINE
|
|
|
|
}
|
|
|
|
|
2017-10-30 10:21:47 +01:00
|
|
|
for _, mClient := range watchers {
|
2020-05-28 23:55:53 +02:00
|
|
|
mClient.Send(nil, mClient.server.name, command, mClient.Nick(), nick)
|
2017-10-30 10:21:47 +01:00
|
|
|
}
|
2017-10-04 06:57:03 +02:00
|
|
|
}
|
|
|
|
|
2017-10-04 08:59:59 +02:00
|
|
|
// Add registers `client` to receive notifications about `nick`.
|
|
|
|
func (manager *MonitorManager) Add(client *Client, nick string, limit int) error {
|
2017-10-04 06:57:03 +02:00
|
|
|
manager.Lock()
|
|
|
|
defer manager.Unlock()
|
|
|
|
|
|
|
|
if manager.watching[client] == nil {
|
|
|
|
manager.watching[client] = make(map[string]bool)
|
|
|
|
}
|
|
|
|
if manager.watchedby[nick] == nil {
|
|
|
|
manager.watchedby[nick] = make(map[*Client]bool)
|
2016-10-16 12:14:56 +02:00
|
|
|
}
|
|
|
|
|
2017-10-04 06:57:03 +02:00
|
|
|
if len(manager.watching[client]) >= limit {
|
2018-02-03 13:03:36 +01:00
|
|
|
return errMonitorLimitExceeded
|
2017-10-04 06:57:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
manager.watching[client][nick] = true
|
|
|
|
manager.watchedby[nick][client] = true
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-10-04 08:59:59 +02:00
|
|
|
// Remove unregisters `client` from receiving notifications about `nick`.
|
|
|
|
func (manager *MonitorManager) Remove(client *Client, nick string) error {
|
2017-10-04 06:57:03 +02:00
|
|
|
manager.Lock()
|
|
|
|
defer manager.Unlock()
|
|
|
|
// deleting from nil maps is fine
|
|
|
|
delete(manager.watching[client], nick)
|
|
|
|
delete(manager.watchedby[nick], client)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-10-04 08:59:59 +02:00
|
|
|
// RemoveAll unregisters `client` from receiving notifications about *all* nicks.
|
|
|
|
func (manager *MonitorManager) RemoveAll(client *Client) {
|
|
|
|
manager.Lock()
|
|
|
|
defer manager.Unlock()
|
|
|
|
|
2017-10-05 15:29:34 +02:00
|
|
|
for nick := range manager.watching[client] {
|
2017-10-04 08:59:59 +02:00
|
|
|
delete(manager.watchedby[nick], client)
|
|
|
|
}
|
|
|
|
delete(manager.watching, client)
|
|
|
|
}
|
|
|
|
|
|
|
|
// List lists all nicks that `client` is registered to receive notifications about.
|
|
|
|
func (manager *MonitorManager) List(client *Client) (nicks []string) {
|
2017-10-04 06:57:03 +02:00
|
|
|
manager.RLock()
|
|
|
|
defer manager.RUnlock()
|
|
|
|
for nick := range manager.watching[client] {
|
|
|
|
nicks = append(nicks, nick)
|
|
|
|
}
|
|
|
|
return nicks
|
2016-10-16 12:14:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
2018-04-12 02:11:45 +02:00
|
|
|
monitorSubcommands = map[string]func(server *Server, client *Client, msg ircmsg.IrcMessage, rb *ResponseBuffer) bool{
|
2016-10-16 12:14:56 +02:00
|
|
|
"-": monitorRemoveHandler,
|
|
|
|
"+": monitorAddHandler,
|
|
|
|
"c": monitorClearHandler,
|
|
|
|
"l": monitorListHandler,
|
|
|
|
"s": monitorStatusHandler,
|
|
|
|
}
|
|
|
|
)
|