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 (
|
2020-12-15 10:00:44 +01:00
|
|
|
"sort"
|
2017-10-30 10:21:47 +01:00
|
|
|
"sync"
|
2023-01-04 11:06:21 +01:00
|
|
|
"time"
|
2020-08-05 03:46:16 +02:00
|
|
|
|
2023-01-04 11:06:21 +01:00
|
|
|
"github.com/ergochat/ergo/irc/datastore"
|
2021-05-25 06:34:38 +02:00
|
|
|
"github.com/ergochat/ergo/irc/utils"
|
2017-10-30 10:21:47 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
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
|
2023-01-04 11:06:21 +01:00
|
|
|
chans map[string]*channelManagerEntry
|
|
|
|
chansSkeletons utils.HashSet[string]
|
|
|
|
purgedChannels map[string]ChannelPurgeRecord // casefolded name to purge record
|
|
|
|
server *Server
|
2017-10-30 10:21:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewChannelManager returns a new ChannelManager.
|
2023-01-04 11:06:21 +01:00
|
|
|
func (cm *ChannelManager) Initialize(server *Server, config *Config) (err error) {
|
2019-03-12 00:24:45 +01:00
|
|
|
cm.chans = make(map[string]*channelManagerEntry)
|
2022-03-30 06:44:51 +02:00
|
|
|
cm.chansSkeletons = make(utils.HashSet[string])
|
2019-03-12 00:24:45 +01:00
|
|
|
cm.server = server
|
2023-01-04 11:06:21 +01:00
|
|
|
return cm.loadRegisteredChannels(config)
|
2017-10-30 10:21:47 +01:00
|
|
|
}
|
|
|
|
|
2023-01-04 11:06:21 +01:00
|
|
|
func (cm *ChannelManager) loadRegisteredChannels(config *Config) (err error) {
|
|
|
|
allChannels, err := FetchAndDeserializeAll[RegisteredChannel](datastore.TableChannels, cm.server.dstore, cm.server.logger)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
allPurgeRecords, err := FetchAndDeserializeAll[ChannelPurgeRecord](datastore.TableChannelPurges, cm.server.dstore, cm.server.logger)
|
|
|
|
if err != nil {
|
2019-12-22 14:17:56 +01:00
|
|
|
return
|
|
|
|
}
|
2021-02-04 21:26:03 +01:00
|
|
|
|
|
|
|
cm.Lock()
|
|
|
|
defer cm.Unlock()
|
|
|
|
|
2023-01-04 11:06:21 +01:00
|
|
|
cm.purgedChannels = make(map[string]ChannelPurgeRecord, len(allPurgeRecords))
|
|
|
|
for _, purge := range allPurgeRecords {
|
|
|
|
cm.purgedChannels[purge.NameCasefolded] = purge
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, regInfo := range allChannels {
|
|
|
|
cfname, err := CasefoldChannel(regInfo.Name)
|
|
|
|
if err != nil {
|
|
|
|
cm.server.logger.Error("channels", "couldn't casefold registered channel, skipping", regInfo.Name, err.Error())
|
|
|
|
continue
|
|
|
|
} else {
|
|
|
|
cm.server.logger.Debug("channels", "initializing registered channel", regInfo.Name)
|
2019-12-17 19:21:26 +01:00
|
|
|
}
|
2023-01-04 11:06:21 +01:00
|
|
|
skeleton, err := Skeleton(regInfo.Name)
|
2019-12-17 19:21:26 +01:00
|
|
|
if err == nil {
|
2023-01-04 11:06:21 +01:00
|
|
|
cm.chansSkeletons.Add(skeleton)
|
2021-02-04 21:26:03 +01:00
|
|
|
}
|
|
|
|
|
2023-01-04 11:06:21 +01:00
|
|
|
if _, ok := cm.purgedChannels[cfname]; !ok {
|
|
|
|
ch := NewChannel(cm.server, regInfo.Name, cfname, true, regInfo)
|
|
|
|
cm.chans[cfname] = &channelManagerEntry{
|
|
|
|
channel: ch,
|
|
|
|
pendingJoins: 0,
|
|
|
|
skeleton: skeleton,
|
2021-02-04 21:26:03 +01:00
|
|
|
}
|
2019-12-17 19:21:26 +01:00
|
|
|
}
|
|
|
|
}
|
2023-01-04 11:06:21 +01:00
|
|
|
|
|
|
|
return nil
|
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)
|
2023-01-04 11:06:21 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
cm.RLock()
|
|
|
|
defer cm.RUnlock()
|
|
|
|
entry := cm.chans[name]
|
|
|
|
if entry != nil {
|
|
|
|
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.
|
2020-12-14 11:00:21 +01:00
|
|
|
func (cm *ChannelManager) Join(client *Client, name string, key string, isSajoin bool, rb *ResponseBuffer) (err error, forward string) {
|
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 {
|
2020-12-14 11:00:21 +01:00
|
|
|
return errNoSuchChannel, ""
|
2017-10-30 10:21:47 +01:00
|
|
|
}
|
|
|
|
|
2021-07-15 11:59:45 +02:00
|
|
|
channel, err, newChannel := func() (*Channel, error, bool) {
|
|
|
|
var newChannel bool
|
2019-05-30 11:33:59 +02:00
|
|
|
cm.Lock()
|
|
|
|
defer cm.Unlock()
|
|
|
|
|
2023-01-04 11:06:21 +01:00
|
|
|
// check purges first; a registered purged channel will still be present in `chans`
|
|
|
|
if _, ok := cm.purgedChannels[casefoldedName]; ok {
|
2021-07-15 11:59:45 +02:00
|
|
|
return nil, errChannelPurged, false
|
2019-12-17 01:50:15 +01:00
|
|
|
}
|
2019-05-30 11:33:59 +02:00
|
|
|
entry := cm.chans[casefoldedName]
|
|
|
|
if entry == nil {
|
2023-01-04 11:06:21 +01:00
|
|
|
if server.Config().Channels.OpOnlyCreation &&
|
2021-07-15 10:26:38 +02:00
|
|
|
!(isSajoin || client.HasRoleCapabs("chanreg")) {
|
2021-07-15 11:59:45 +02:00
|
|
|
return nil, errInsufficientPrivs, false
|
2019-12-17 19:21:26 +01:00
|
|
|
}
|
|
|
|
// enforce confusables
|
2023-01-04 11:06:21 +01:00
|
|
|
if cm.chansSkeletons.Has(skeleton) {
|
2021-07-15 11:59:45 +02:00
|
|
|
return nil, errConfusableIdentifier, false
|
2019-05-30 11:33:59 +02:00
|
|
|
}
|
|
|
|
entry = &channelManagerEntry{
|
2023-01-04 11:06:21 +01:00
|
|
|
channel: NewChannel(server, name, casefoldedName, false, RegisteredChannel{}),
|
2019-05-30 11:33:59 +02:00
|
|
|
pendingJoins: 0,
|
|
|
|
}
|
2023-01-04 11:06:21 +01:00
|
|
|
cm.chansSkeletons.Add(skeleton)
|
|
|
|
entry.skeleton = skeleton
|
2019-05-30 11:33:59 +02:00
|
|
|
cm.chans[casefoldedName] = entry
|
2021-07-15 11:59:45 +02:00
|
|
|
newChannel = true
|
2017-10-30 10:21:47 +01:00
|
|
|
}
|
2019-05-30 11:33:59 +02:00
|
|
|
entry.pendingJoins += 1
|
2021-07-15 11:59:45 +02:00
|
|
|
return entry.channel, nil, newChannel
|
2019-05-30 11:33:59 +02:00
|
|
|
}()
|
|
|
|
|
2019-12-17 19:21:26 +01:00
|
|
|
if err != nil {
|
2020-12-14 11:00:21 +01:00
|
|
|
return err, ""
|
2017-10-30 10:21:47 +01:00
|
|
|
}
|
|
|
|
|
2021-07-15 11:59:45 +02:00
|
|
|
err, forward = channel.Join(client, key, isSajoin || newChannel, 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
|
|
|
|
2020-12-14 11:00:21 +01:00
|
|
|
return
|
2017-10-30 10:21:47 +01:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2021-04-19 02:03:17 +02:00
|
|
|
cm.maybeCleanupInternal(cfname, entry, afterJoin)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cm *ChannelManager) maybeCleanupInternal(cfname string, entry *channelManagerEntry, afterJoin bool) {
|
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) {
|
2020-07-08 11:32:14 +02:00
|
|
|
if cm.server.Defcon() <= 4 {
|
|
|
|
return errFeatureDisabled
|
|
|
|
}
|
|
|
|
|
2019-03-12 00:24:45 +01:00
|
|
|
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
|
2020-03-02 07:46:22 +01:00
|
|
|
err = channel.Store(IncludeAllAttrs)
|
2019-03-12 00:24:45 +01:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cm *ChannelManager) SetUnregistered(channelName string, account string) (err error) {
|
|
|
|
cfname, err := CasefoldChannel(channelName)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-01-04 11:06:21 +01:00
|
|
|
var uuid utils.UUID
|
2019-03-12 00:24:45 +01:00
|
|
|
|
|
|
|
defer func() {
|
|
|
|
if err == nil {
|
2023-01-04 11:06:21 +01:00
|
|
|
if delErr := cm.server.dstore.Delete(datastore.TableChannels, uuid); delErr != nil {
|
|
|
|
cm.server.logger.Error("datastore", "couldn't delete channel registration", cfname, delErr.Error())
|
|
|
|
}
|
2019-03-12 00:24:45 +01:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
cm.Lock()
|
|
|
|
defer cm.Unlock()
|
|
|
|
entry := cm.chans[cfname]
|
2020-03-20 19:40:14 +01:00
|
|
|
if entry != nil {
|
2023-01-04 11:06:21 +01:00
|
|
|
if entry.channel.Founder() != account {
|
|
|
|
return errChannelNotOwnedByAccount
|
2020-03-20 19:40:14 +01:00
|
|
|
}
|
2023-01-04 11:06:21 +01:00
|
|
|
uuid = entry.channel.UUID()
|
|
|
|
entry.channel.SetUnregistered(account) // changes the UUID
|
2021-04-19 02:03:17 +02:00
|
|
|
// #1619: if the channel has 0 members and was only being retained
|
|
|
|
// because it was registered, clean it up:
|
|
|
|
cm.maybeCleanupInternal(cfname, entry, false)
|
2020-01-12 17:20:30 +01:00
|
|
|
}
|
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) {
|
2020-12-15 05:01:41 +01:00
|
|
|
oldCfname, err := CasefoldChannel(name)
|
2017-10-30 10:21:47 +01:00
|
|
|
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 != "" {
|
2023-01-04 11:06:21 +01:00
|
|
|
channel.MarkDirty(IncludeAllAttrs)
|
|
|
|
}
|
|
|
|
// always-on clients need to update their saved channel memberships
|
|
|
|
for _, member := range channel.Members() {
|
|
|
|
member.markDirty(IncludeChannels)
|
2019-03-12 00:24:45 +01:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2017-10-30 10:21:47 +01:00
|
|
|
cm.Lock()
|
|
|
|
defer cm.Unlock()
|
|
|
|
|
2020-12-15 05:01:41 +01:00
|
|
|
entry := cm.chans[oldCfname]
|
2023-01-04 11:06:21 +01:00
|
|
|
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
|
2023-01-04 11:06:21 +01:00
|
|
|
info = channel.ExportRegistration()
|
2019-12-17 19:21:26 +01:00
|
|
|
registered := info.Founder != ""
|
2020-12-15 05:01:41 +01:00
|
|
|
|
|
|
|
oldSkeleton, err := Skeleton(info.Name)
|
|
|
|
if err != nil {
|
|
|
|
return errNoSuchChannel // ugh
|
|
|
|
}
|
|
|
|
|
|
|
|
if newCfname != oldCfname {
|
2023-01-04 11:06:21 +01:00
|
|
|
if cm.chans[newCfname] != nil {
|
2020-12-15 05:01:41 +01:00
|
|
|
return errChannelNameInUse
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if oldSkeleton != newSkeleton {
|
2023-01-04 11:06:21 +01:00
|
|
|
if cm.chansSkeletons.Has(newSkeleton) {
|
2020-12-15 05:01:41 +01:00
|
|
|
return errConfusableIdentifier
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
delete(cm.chans, oldCfname)
|
|
|
|
if !registered {
|
|
|
|
entry.skeleton = newSkeleton
|
|
|
|
}
|
2019-12-17 19:21:26 +01:00
|
|
|
cm.chans[newCfname] = entry
|
2023-01-04 11:06:21 +01:00
|
|
|
delete(cm.chansSkeletons, oldSkeleton)
|
|
|
|
cm.chansSkeletons.Add(newSkeleton)
|
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 {
|
2023-01-04 11:06:21 +01:00
|
|
|
result = append(result, entry.channel)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// ListableChannels returns a slice of all non-purged channels.
|
|
|
|
func (cm *ChannelManager) ListableChannels() (result []*Channel) {
|
|
|
|
cm.RLock()
|
|
|
|
defer cm.RUnlock()
|
|
|
|
result = make([]*Channel, 0, len(cm.chans))
|
|
|
|
for cfname, entry := range cm.chans {
|
|
|
|
if _, ok := cm.purgedChannels[cfname]; !ok {
|
2019-03-12 00:24:45 +01:00
|
|
|
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
|
|
|
|
}
|
2023-01-04 11:06:21 +01:00
|
|
|
|
|
|
|
record.NameCasefolded = chname
|
|
|
|
record.UUID = utils.GenerateUUIDv4()
|
|
|
|
|
|
|
|
channel, err := func() (channel *Channel, err error) {
|
|
|
|
cm.Lock()
|
|
|
|
defer cm.Unlock()
|
|
|
|
|
|
|
|
if _, ok := cm.purgedChannels[chname]; ok {
|
|
|
|
return nil, errChannelPurgedAlready
|
|
|
|
}
|
|
|
|
|
|
|
|
entry := cm.chans[chname]
|
|
|
|
// atomically prevent anyone from rejoining
|
|
|
|
cm.purgedChannels[chname] = record
|
|
|
|
if entry != nil {
|
|
|
|
channel = entry.channel
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}()
|
|
|
|
|
2021-02-04 21:26:03 +01:00
|
|
|
if err != nil {
|
2023-01-04 11:06:21 +01:00
|
|
|
return err
|
2021-02-04 21:26:03 +01:00
|
|
|
}
|
2019-12-17 01:50:15 +01:00
|
|
|
|
2023-01-04 11:06:21 +01:00
|
|
|
if channel != nil {
|
|
|
|
// actually kick everyone off the channel
|
|
|
|
channel.Purge("")
|
2021-02-04 21:26:03 +01:00
|
|
|
}
|
2019-12-17 01:50:15 +01:00
|
|
|
|
2023-01-04 11:06:21 +01:00
|
|
|
var purgeBytes []byte
|
|
|
|
if purgeBytes, err = record.Serialize(); err != nil {
|
|
|
|
cm.server.logger.Error("internal", "couldn't serialize purge record", channel.Name(), err.Error())
|
2021-02-04 21:26:03 +01:00
|
|
|
}
|
2023-01-04 11:06:21 +01:00
|
|
|
// TODO we need a better story about error handling for later
|
|
|
|
if err = cm.server.dstore.Set(datastore.TableChannelPurges, record.UUID, purgeBytes, time.Time{}); err != nil {
|
|
|
|
cm.server.logger.Error("datastore", "couldn't store purge record", chname, err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
2019-12-17 01:50:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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()
|
2023-01-04 11:06:21 +01:00
|
|
|
_, result = cm.purgedChannels[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()
|
2023-01-04 11:06:21 +01:00
|
|
|
record, found := cm.purgedChannels[chname]
|
2019-12-17 01:50:15 +01:00
|
|
|
delete(cm.purgedChannels, chname)
|
|
|
|
cm.Unlock()
|
|
|
|
|
|
|
|
if !found {
|
|
|
|
return errNoSuchChannel
|
|
|
|
}
|
2023-01-04 11:06:21 +01:00
|
|
|
if err := cm.server.dstore.Delete(datastore.TableChannelPurges, record.UUID); err != nil {
|
|
|
|
cm.server.logger.Error("datastore", "couldn't delete purge record", chname, err.Error())
|
|
|
|
}
|
2019-12-17 01:50:15 +01:00
|
|
|
return nil
|
|
|
|
}
|
2020-12-15 10:00:44 +01:00
|
|
|
|
|
|
|
func (cm *ChannelManager) ListPurged() (result []string) {
|
|
|
|
cm.RLock()
|
|
|
|
result = make([]string, 0, len(cm.purgedChannels))
|
|
|
|
for c := range cm.purgedChannels {
|
|
|
|
result = append(result, c)
|
|
|
|
}
|
|
|
|
cm.RUnlock()
|
|
|
|
sort.Strings(result)
|
|
|
|
return
|
|
|
|
}
|
2021-04-07 11:40:39 +02:00
|
|
|
|
|
|
|
func (cm *ChannelManager) UnfoldName(cfname string) (result string) {
|
|
|
|
cm.RLock()
|
|
|
|
entry := cm.chans[cfname]
|
|
|
|
cm.RUnlock()
|
2023-01-04 11:06:21 +01:00
|
|
|
if entry != nil {
|
2021-04-07 11:40:39 +02:00
|
|
|
return entry.channel.Name()
|
|
|
|
}
|
|
|
|
return cfname
|
|
|
|
}
|
2023-01-04 11:06:21 +01:00
|
|
|
|
|
|
|
func (cm *ChannelManager) LoadPurgeRecord(cfchname string) (record ChannelPurgeRecord, err error) {
|
|
|
|
cm.RLock()
|
|
|
|
defer cm.RUnlock()
|
|
|
|
|
|
|
|
if record, ok := cm.purgedChannels[cfchname]; ok {
|
|
|
|
return record, nil
|
|
|
|
} else {
|
|
|
|
return record, errNoSuchChannel
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cm *ChannelManager) ChannelsForAccount(account string) (channels []string) {
|
|
|
|
cm.RLock()
|
|
|
|
defer cm.RUnlock()
|
|
|
|
|
|
|
|
for cfname, entry := range cm.chans {
|
|
|
|
if entry.channel.Founder() == account {
|
|
|
|
channels = append(channels, cfname)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// AllChannels returns the uncasefolded names of all registered channels.
|
|
|
|
func (cm *ChannelManager) AllRegisteredChannels() (result []string) {
|
|
|
|
cm.RLock()
|
|
|
|
defer cm.RUnlock()
|
|
|
|
|
|
|
|
for cfname, entry := range cm.chans {
|
|
|
|
if entry.channel.Founder() != "" {
|
|
|
|
result = append(result, cfname)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|