2017-10-30 10:21:47 +01:00
|
|
|
// Copyright (c) 2017 Shivaram Lingamneni <slingamn@cs.stanford.edu>
|
|
|
|
// released under the MIT license
|
|
|
|
|
|
|
|
package irc
|
|
|
|
|
|
|
|
import (
|
|
|
|
"sync"
|
|
|
|
)
|
|
|
|
|
|
|
|
type channelManagerEntry struct {
|
|
|
|
channel *Channel
|
|
|
|
// this is a refcount for joins, so we can avoid a race where we incorrectly
|
|
|
|
// think the channel is empty (without holding a lock across the entire Channel.Join()
|
|
|
|
// call)
|
|
|
|
pendingJoins int
|
2019-12-17 19:21:26 +01:00
|
|
|
skeleton string
|
2017-10-30 10:21:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// ChannelManager keeps track of all the channels on the server,
|
|
|
|
// providing synchronization for creation of new channels on first join,
|
|
|
|
// cleanup of empty channels on last part, and renames.
|
|
|
|
type ChannelManager struct {
|
2019-12-17 19:21:26 +01:00
|
|
|
sync.RWMutex // tier 2
|
|
|
|
// chans is the main data structure, mapping casefolded name -> *Channel
|
|
|
|
chans map[string]*channelManagerEntry
|
|
|
|
chansSkeletons StringSet // skeletons of *unregistered* chans
|
|
|
|
registeredChannels StringSet // casefolds of registered chans
|
|
|
|
registeredSkeletons StringSet // skeletons of registered chans
|
|
|
|
purgedChannels StringSet // casefolds of purged chans
|
|
|
|
server *Server
|
2017-10-30 10:21:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewChannelManager returns a new ChannelManager.
|
2019-03-12 00:24:45 +01:00
|
|
|
func (cm *ChannelManager) Initialize(server *Server) {
|
|
|
|
cm.chans = make(map[string]*channelManagerEntry)
|
2019-12-17 19:21:26 +01:00
|
|
|
cm.chansSkeletons = make(StringSet)
|
2019-03-12 00:24:45 +01:00
|
|
|
cm.server = server
|
|
|
|
|
2019-12-22 14:17:56 +01:00
|
|
|
cm.loadRegisteredChannels(server.Config())
|
2019-12-17 19:21:26 +01:00
|
|
|
// purging should work even if registration is disabled
|
|
|
|
cm.purgedChannels = cm.server.channelRegistry.PurgedChannels()
|
2017-10-30 10:21:47 +01:00
|
|
|
}
|
|
|
|
|
2019-12-22 14:17:56 +01:00
|
|
|
func (cm *ChannelManager) loadRegisteredChannels(config *Config) {
|
|
|
|
if !config.Channels.Registration.Enabled {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-12-17 19:21:26 +01:00
|
|
|
rawNames := cm.server.channelRegistry.AllChannels()
|
|
|
|
registeredChannels := make(StringSet, len(rawNames))
|
|
|
|
registeredSkeletons := make(StringSet, len(rawNames))
|
|
|
|
for _, name := range rawNames {
|
|
|
|
cfname, err := CasefoldChannel(name)
|
|
|
|
if err == nil {
|
|
|
|
registeredChannels.Add(cfname)
|
|
|
|
}
|
|
|
|
skeleton, err := Skeleton(name)
|
|
|
|
if err == nil {
|
|
|
|
registeredSkeletons.Add(skeleton)
|
|
|
|
}
|
|
|
|
}
|
2019-03-12 00:24:45 +01:00
|
|
|
cm.Lock()
|
|
|
|
defer cm.Unlock()
|
|
|
|
cm.registeredChannels = registeredChannels
|
2019-12-17 19:21:26 +01:00
|
|
|
cm.registeredSkeletons = registeredSkeletons
|
2019-03-12 00:24:45 +01:00
|
|
|
}
|
|
|
|
|
2017-10-30 10:21:47 +01:00
|
|
|
// Get returns an existing channel with name equivalent to `name`, or nil
|
2019-03-12 00:24:45 +01:00
|
|
|
func (cm *ChannelManager) Get(name string) (channel *Channel) {
|
2017-10-30 10:21:47 +01:00
|
|
|
name, err := CasefoldChannel(name)
|
|
|
|
if err == nil {
|
|
|
|
cm.RLock()
|
|
|
|
defer cm.RUnlock()
|
2017-11-13 08:42:20 +01:00
|
|
|
entry := cm.chans[name]
|
2019-03-12 00:24:45 +01:00
|
|
|
// if the channel is still loading, pretend we don't have it
|
|
|
|
if entry != nil && entry.channel.IsLoaded() {
|
2017-11-13 08:42:20 +01:00
|
|
|
return entry.channel
|
|
|
|
}
|
2017-10-30 10:21:47 +01:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Join causes `client` to join the channel named `name`, creating it if necessary.
|
2018-05-25 08:46:36 +02:00
|
|
|
func (cm *ChannelManager) Join(client *Client, name string, key string, isSajoin bool, rb *ResponseBuffer) error {
|
2017-10-30 10:21:47 +01:00
|
|
|
server := client.server
|
|
|
|
casefoldedName, err := CasefoldChannel(name)
|
2019-12-17 19:21:26 +01:00
|
|
|
skeleton, skerr := Skeleton(name)
|
|
|
|
if err != nil || skerr != nil || len(casefoldedName) > server.Config().Limits.ChannelLen {
|
2018-02-03 13:03:36 +01:00
|
|
|
return errNoSuchChannel
|
2017-10-30 10:21:47 +01:00
|
|
|
}
|
|
|
|
|
2019-12-17 19:21:26 +01:00
|
|
|
channel, err := func() (*Channel, error) {
|
2019-05-30 11:33:59 +02:00
|
|
|
cm.Lock()
|
|
|
|
defer cm.Unlock()
|
|
|
|
|
2019-12-17 19:21:26 +01:00
|
|
|
if cm.purgedChannels.Has(casefoldedName) {
|
|
|
|
return nil, errChannelPurged
|
2019-12-17 01:50:15 +01:00
|
|
|
}
|
2019-05-30 11:33:59 +02:00
|
|
|
entry := cm.chans[casefoldedName]
|
|
|
|
if entry == nil {
|
2019-12-17 19:21:26 +01:00
|
|
|
registered := cm.registeredChannels.Has(casefoldedName)
|
2019-05-30 11:33:59 +02:00
|
|
|
// enforce OpOnlyCreation
|
|
|
|
if !registered && server.Config().Channels.OpOnlyCreation && !client.HasRoleCapabs("chanreg") {
|
2019-12-17 19:21:26 +01:00
|
|
|
return nil, errInsufficientPrivs
|
|
|
|
}
|
|
|
|
// enforce confusables
|
|
|
|
if cm.chansSkeletons.Has(skeleton) || (!registered && cm.registeredSkeletons.Has(skeleton)) {
|
|
|
|
return nil, errConfusableIdentifier
|
2019-05-30 11:33:59 +02:00
|
|
|
}
|
|
|
|
entry = &channelManagerEntry{
|
2019-12-17 19:21:26 +01:00
|
|
|
channel: NewChannel(server, name, casefoldedName, registered),
|
2019-05-30 11:33:59 +02:00
|
|
|
pendingJoins: 0,
|
|
|
|
}
|
2019-12-17 19:21:26 +01:00
|
|
|
if !registered {
|
|
|
|
// for an unregistered channel, we already have the correct unfolded name
|
|
|
|
// and therefore the final skeleton. for a registered channel, we don't have
|
|
|
|
// the unfolded name yet (it needs to be loaded from the db), but we already
|
|
|
|
// have the final skeleton in `registeredSkeletons` so we don't need to track it
|
|
|
|
cm.chansSkeletons.Add(skeleton)
|
|
|
|
entry.skeleton = skeleton
|
|
|
|
}
|
2019-05-30 11:33:59 +02:00
|
|
|
cm.chans[casefoldedName] = entry
|
2017-10-30 10:21:47 +01:00
|
|
|
}
|
2019-05-30 11:33:59 +02:00
|
|
|
entry.pendingJoins += 1
|
2019-12-17 19:21:26 +01:00
|
|
|
return entry.channel, nil
|
2019-05-30 11:33:59 +02:00
|
|
|
}()
|
|
|
|
|
2019-12-17 19:21:26 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2017-10-30 10:21:47 +01:00
|
|
|
}
|
|
|
|
|
2019-03-12 00:24:45 +01:00
|
|
|
channel.EnsureLoaded()
|
|
|
|
channel.Join(client, key, isSajoin, rb)
|
2017-10-30 10:21:47 +01:00
|
|
|
|
2019-03-12 00:24:45 +01:00
|
|
|
cm.maybeCleanup(channel, true)
|
2017-10-30 10:21:47 +01:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-03-01 16:37:30 +01:00
|
|
|
func (cm *ChannelManager) maybeCleanup(channel *Channel, afterJoin bool) {
|
2017-10-30 10:21:47 +01:00
|
|
|
cm.Lock()
|
|
|
|
defer cm.Unlock()
|
|
|
|
|
2019-12-17 19:21:26 +01:00
|
|
|
cfname := channel.NameCasefolded()
|
|
|
|
|
|
|
|
entry := cm.chans[cfname]
|
2018-03-01 16:37:30 +01:00
|
|
|
if entry == nil || entry.channel != channel {
|
2017-10-30 10:21:47 +01:00
|
|
|
return
|
|
|
|
}
|
2018-03-01 16:37:30 +01:00
|
|
|
|
2017-10-30 10:21:47 +01:00
|
|
|
if afterJoin {
|
|
|
|
entry.pendingJoins -= 1
|
|
|
|
}
|
2019-03-12 00:24:45 +01:00
|
|
|
if entry.pendingJoins == 0 && entry.channel.IsClean() {
|
2019-12-17 19:21:26 +01:00
|
|
|
delete(cm.chans, cfname)
|
|
|
|
if entry.skeleton != "" {
|
|
|
|
delete(cm.chansSkeletons, entry.skeleton)
|
|
|
|
}
|
2017-10-30 10:21:47 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Part parts `client` from the channel named `name`, deleting it if it's empty.
|
2018-02-05 15:21:08 +01:00
|
|
|
func (cm *ChannelManager) Part(client *Client, name string, message string, rb *ResponseBuffer) error {
|
2019-03-12 00:24:45 +01:00
|
|
|
var channel *Channel
|
|
|
|
|
2017-10-30 10:21:47 +01:00
|
|
|
casefoldedName, err := CasefoldChannel(name)
|
|
|
|
if err != nil {
|
2018-02-03 13:03:36 +01:00
|
|
|
return errNoSuchChannel
|
2017-10-30 10:21:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
cm.RLock()
|
|
|
|
entry := cm.chans[casefoldedName]
|
2019-03-12 00:24:45 +01:00
|
|
|
if entry != nil {
|
|
|
|
channel = entry.channel
|
|
|
|
}
|
2017-10-30 10:21:47 +01:00
|
|
|
cm.RUnlock()
|
|
|
|
|
2019-03-12 00:24:45 +01:00
|
|
|
if channel == nil {
|
2018-02-03 13:03:36 +01:00
|
|
|
return errNoSuchChannel
|
2017-10-30 10:21:47 +01:00
|
|
|
}
|
2019-03-12 00:24:45 +01:00
|
|
|
channel.Part(client, message, rb)
|
2017-10-30 10:21:47 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-03-01 16:37:30 +01:00
|
|
|
func (cm *ChannelManager) Cleanup(channel *Channel) {
|
|
|
|
cm.maybeCleanup(channel, false)
|
|
|
|
}
|
|
|
|
|
2019-03-12 00:24:45 +01:00
|
|
|
func (cm *ChannelManager) SetRegistered(channelName string, account string) (err error) {
|
|
|
|
var channel *Channel
|
|
|
|
cfname, err := CasefoldChannel(channelName)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
var entry *channelManagerEntry
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
if err == nil && channel != nil {
|
|
|
|
// registration was successful: make the database reflect it
|
|
|
|
err = channel.Store(IncludeAllChannelAttrs)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
cm.Lock()
|
|
|
|
defer cm.Unlock()
|
|
|
|
entry = cm.chans[cfname]
|
|
|
|
if entry == nil {
|
|
|
|
return errNoSuchChannel
|
|
|
|
}
|
|
|
|
channel = entry.channel
|
|
|
|
err = channel.SetRegistered(account)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-12-17 19:21:26 +01:00
|
|
|
cm.registeredChannels.Add(cfname)
|
2020-01-12 17:20:30 +01:00
|
|
|
if skel, err := Skeleton(channel.Name()); err == nil {
|
|
|
|
cm.registeredSkeletons.Add(skel)
|
|
|
|
}
|
2019-03-12 00:24:45 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cm *ChannelManager) SetUnregistered(channelName string, account string) (err error) {
|
|
|
|
cfname, err := CasefoldChannel(channelName)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
var info RegisteredChannel
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
if err == nil {
|
|
|
|
err = cm.server.channelRegistry.Delete(info)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
cm.Lock()
|
|
|
|
defer cm.Unlock()
|
|
|
|
entry := cm.chans[cfname]
|
|
|
|
if entry == nil {
|
|
|
|
return errNoSuchChannel
|
|
|
|
}
|
|
|
|
info = entry.channel.ExportRegistration(0)
|
|
|
|
if info.Founder != account {
|
|
|
|
return errChannelNotOwnedByAccount
|
|
|
|
}
|
|
|
|
entry.channel.SetUnregistered(account)
|
|
|
|
delete(cm.registeredChannels, cfname)
|
2020-01-12 17:20:30 +01:00
|
|
|
if skel, err := Skeleton(entry.channel.Name()); err == nil {
|
|
|
|
delete(cm.registeredSkeletons, skel)
|
|
|
|
}
|
2019-03-12 00:24:45 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-10-30 10:21:47 +01:00
|
|
|
// Rename renames a channel (but does not notify the members)
|
2019-12-17 19:21:26 +01:00
|
|
|
func (cm *ChannelManager) Rename(name string, newName string) (err error) {
|
2017-10-30 10:21:47 +01:00
|
|
|
cfname, err := CasefoldChannel(name)
|
|
|
|
if err != nil {
|
2018-02-03 13:03:36 +01:00
|
|
|
return errNoSuchChannel
|
2017-10-30 10:21:47 +01:00
|
|
|
}
|
|
|
|
|
2019-12-17 19:21:26 +01:00
|
|
|
newCfname, err := CasefoldChannel(newName)
|
|
|
|
if err != nil {
|
|
|
|
return errInvalidChannelName
|
|
|
|
}
|
|
|
|
newSkeleton, err := Skeleton(newName)
|
2017-10-30 10:21:47 +01:00
|
|
|
if err != nil {
|
2018-02-03 13:03:36 +01:00
|
|
|
return errInvalidChannelName
|
2017-10-30 10:21:47 +01:00
|
|
|
}
|
|
|
|
|
2019-03-12 00:24:45 +01:00
|
|
|
var channel *Channel
|
|
|
|
var info RegisteredChannel
|
|
|
|
defer func() {
|
|
|
|
if channel != nil && info.Founder != "" {
|
|
|
|
channel.Store(IncludeAllChannelAttrs)
|
|
|
|
// we just flushed the channel under its new name, therefore this delete
|
|
|
|
// cannot be overwritten by a write to the old name:
|
|
|
|
cm.server.channelRegistry.Delete(info)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2017-10-30 10:21:47 +01:00
|
|
|
cm.Lock()
|
|
|
|
defer cm.Unlock()
|
|
|
|
|
2019-12-17 19:21:26 +01:00
|
|
|
if cm.chans[newCfname] != nil || cm.registeredChannels.Has(newCfname) {
|
|
|
|
return errChannelNameInUse
|
|
|
|
}
|
|
|
|
if cm.chansSkeletons.Has(newSkeleton) || cm.registeredSkeletons.Has(newSkeleton) {
|
2018-02-03 13:03:36 +01:00
|
|
|
return errChannelNameInUse
|
2017-10-30 10:21:47 +01:00
|
|
|
}
|
|
|
|
entry := cm.chans[cfname]
|
2019-12-17 19:21:26 +01:00
|
|
|
if entry == nil || !entry.channel.IsLoaded() {
|
2018-02-03 13:03:36 +01:00
|
|
|
return errNoSuchChannel
|
2017-10-30 10:21:47 +01:00
|
|
|
}
|
2019-03-12 00:24:45 +01:00
|
|
|
channel = entry.channel
|
|
|
|
info = channel.ExportRegistration(IncludeInitial)
|
2019-12-17 19:21:26 +01:00
|
|
|
registered := info.Founder != ""
|
2017-10-30 10:21:47 +01:00
|
|
|
delete(cm.chans, cfname)
|
2019-12-17 19:21:26 +01:00
|
|
|
cm.chans[newCfname] = entry
|
|
|
|
if registered {
|
2019-03-14 08:21:45 +01:00
|
|
|
delete(cm.registeredChannels, cfname)
|
2019-12-17 19:21:26 +01:00
|
|
|
if oldSkeleton, err := Skeleton(info.Name); err == nil {
|
|
|
|
delete(cm.registeredSkeletons, oldSkeleton)
|
|
|
|
}
|
|
|
|
cm.registeredChannels.Add(newCfname)
|
|
|
|
cm.registeredSkeletons.Add(newSkeleton)
|
|
|
|
} else {
|
|
|
|
delete(cm.chansSkeletons, entry.skeleton)
|
|
|
|
cm.chansSkeletons.Add(newSkeleton)
|
|
|
|
entry.skeleton = newSkeleton
|
|
|
|
cm.chans[cfname] = entry
|
2019-03-14 08:21:45 +01:00
|
|
|
}
|
2019-12-17 19:21:26 +01:00
|
|
|
entry.channel.Rename(newName, newCfname)
|
2017-10-30 10:21:47 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Len returns the number of channels
|
|
|
|
func (cm *ChannelManager) Len() int {
|
|
|
|
cm.RLock()
|
|
|
|
defer cm.RUnlock()
|
|
|
|
return len(cm.chans)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Channels returns a slice containing all current channels
|
|
|
|
func (cm *ChannelManager) Channels() (result []*Channel) {
|
|
|
|
cm.RLock()
|
|
|
|
defer cm.RUnlock()
|
2019-03-12 00:24:45 +01:00
|
|
|
result = make([]*Channel, 0, len(cm.chans))
|
2017-10-30 10:21:47 +01:00
|
|
|
for _, entry := range cm.chans {
|
2019-03-12 00:24:45 +01:00
|
|
|
if entry.channel.IsLoaded() {
|
|
|
|
result = append(result, entry.channel)
|
|
|
|
}
|
2017-10-30 10:21:47 +01:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
2019-12-17 01:50:15 +01:00
|
|
|
|
|
|
|
// Purge marks a channel as purged.
|
|
|
|
func (cm *ChannelManager) Purge(chname string, record ChannelPurgeRecord) (err error) {
|
|
|
|
chname, err = CasefoldChannel(chname)
|
|
|
|
if err != nil {
|
|
|
|
return errInvalidChannelName
|
|
|
|
}
|
|
|
|
|
|
|
|
cm.Lock()
|
2019-12-17 19:21:26 +01:00
|
|
|
cm.purgedChannels.Add(chname)
|
2019-12-17 01:50:15 +01:00
|
|
|
cm.Unlock()
|
|
|
|
|
|
|
|
cm.server.channelRegistry.PurgeChannel(chname, record)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsPurged queries whether a channel is purged.
|
|
|
|
func (cm *ChannelManager) IsPurged(chname string) (result bool) {
|
|
|
|
chname, err := CasefoldChannel(chname)
|
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2020-01-12 17:20:30 +01:00
|
|
|
cm.RLock()
|
2019-12-17 19:21:26 +01:00
|
|
|
result = cm.purgedChannels.Has(chname)
|
2020-01-12 17:20:30 +01:00
|
|
|
cm.RUnlock()
|
2019-12-17 01:50:15 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Unpurge deletes a channel's purged status.
|
|
|
|
func (cm *ChannelManager) Unpurge(chname string) (err error) {
|
|
|
|
chname, err = CasefoldChannel(chname)
|
|
|
|
if err != nil {
|
|
|
|
return errNoSuchChannel
|
|
|
|
}
|
|
|
|
|
|
|
|
cm.Lock()
|
2019-12-17 19:21:26 +01:00
|
|
|
found := cm.purgedChannels.Has(chname)
|
2019-12-17 01:50:15 +01:00
|
|
|
delete(cm.purgedChannels, chname)
|
|
|
|
cm.Unlock()
|
|
|
|
|
|
|
|
cm.server.channelRegistry.UnpurgeChannel(chname)
|
|
|
|
if !found {
|
|
|
|
return errNoSuchChannel
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|