3
0
mirror of https://github.com/ergochat/ergo.git synced 2024-12-28 13:42:35 +01:00
ergo/irc/channelmanager.go

270 lines
6.4 KiB
Go
Raw Normal View History

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
}
// 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-03-12 00:24:45 +01:00
sync.RWMutex // tier 2
chans map[string]*channelManagerEntry
registeredChannels map[string]bool
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)
cm.server = server
if server.Config().Channels.Registration.Enabled {
cm.loadRegisteredChannels()
2017-10-30 10:21:47 +01:00
}
}
2019-03-12 00:24:45 +01:00
func (cm *ChannelManager) loadRegisteredChannels() {
registeredChannels := cm.server.channelRegistry.AllChannels()
cm.Lock()
defer cm.Unlock()
cm.registeredChannels = registeredChannels
}
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()
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() {
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.
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-05-23 01:07:12 +02:00
if err != 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-05-30 11:33:59 +02:00
channel := func() *Channel {
cm.Lock()
defer cm.Unlock()
entry := cm.chans[casefoldedName]
if entry == nil {
registered := cm.registeredChannels[casefoldedName]
// enforce OpOnlyCreation
if !registered && server.Config().Channels.OpOnlyCreation && !client.HasRoleCapabs("chanreg") {
return nil
}
entry = &channelManagerEntry{
channel: NewChannel(server, name, registered),
pendingJoins: 0,
}
cm.chans[casefoldedName] = entry
2017-10-30 10:21:47 +01:00
}
2019-05-30 11:33:59 +02:00
entry.pendingJoins += 1
return entry.channel
}()
if channel == nil {
return errNoSuchChannel
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
}
func (cm *ChannelManager) maybeCleanup(channel *Channel, afterJoin bool) {
2017-10-30 10:21:47 +01:00
cm.Lock()
defer cm.Unlock()
2019-03-12 00:24:45 +01:00
nameCasefolded := channel.NameCasefolded()
entry := cm.chans[nameCasefolded]
if entry == nil || entry.channel != channel {
2017-10-30 10:21:47 +01:00
return
}
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() {
delete(cm.chans, nameCasefolded)
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
}
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
}
cm.registeredChannels[cfname] = true
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)
return nil
}
2017-10-30 10:21:47 +01:00
// Rename renames a channel (but does not notify the members)
2019-03-12 00:24:45 +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
}
cfnewname, err := CasefoldChannel(newname)
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()
if cm.chans[cfnewname] != nil || cm.registeredChannels[cfnewname] {
2018-02-03 13:03:36 +01:00
return errChannelNameInUse
2017-10-30 10:21:47 +01:00
}
entry := cm.chans[cfname]
if entry == 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 = entry.channel
info = channel.ExportRegistration(IncludeInitial)
2017-10-30 10:21:47 +01:00
delete(cm.chans, cfname)
cm.chans[cfnewname] = entry
if cm.registeredChannels[cfname] {
delete(cm.registeredChannels, cfname)
cm.registeredChannels[cfnewname] = true
}
2019-03-12 00:24:45 +01:00
entry.channel.Rename(newname, cfnewname)
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
}