2017-03-27 14:15:02 +02:00
|
|
|
// Copyright (c) 2016-2017 Daniel Oaks <daniel@danieloaks.net>
|
2017-03-11 13:01:40 +01:00
|
|
|
// released under the MIT license
|
|
|
|
|
|
|
|
package irc
|
|
|
|
|
|
|
|
import (
|
2020-08-05 03:46:16 +02:00
|
|
|
"encoding/json"
|
2017-03-11 13:01:40 +01:00
|
|
|
"fmt"
|
|
|
|
"strconv"
|
2018-04-04 03:49:40 +02:00
|
|
|
"strings"
|
2017-03-11 13:01:40 +01:00
|
|
|
"time"
|
|
|
|
|
2020-08-05 03:46:16 +02:00
|
|
|
"github.com/tidwall/buntdb"
|
2017-03-26 12:37:13 +02:00
|
|
|
|
2018-04-04 03:49:40 +02:00
|
|
|
"github.com/oragono/oragono/irc/modes"
|
2020-08-05 03:46:16 +02:00
|
|
|
"github.com/oragono/oragono/irc/utils"
|
2017-03-11 13:01:40 +01:00
|
|
|
)
|
|
|
|
|
2017-11-09 04:19:50 +01:00
|
|
|
// this is exclusively the *persistence* layer for channel registration;
|
|
|
|
// channel creation/tracking/destruction is in channelmanager.go
|
|
|
|
|
2017-03-11 13:01:40 +01:00
|
|
|
const (
|
2018-04-04 03:49:40 +02:00
|
|
|
keyChannelExists = "channel.exists %s"
|
|
|
|
keyChannelName = "channel.name %s" // stores the 'preferred name' of the channel, not casemapped
|
|
|
|
keyChannelRegTime = "channel.registered.time %s"
|
|
|
|
keyChannelFounder = "channel.founder %s"
|
|
|
|
keyChannelTopic = "channel.topic %s"
|
|
|
|
keyChannelTopicSetBy = "channel.topic.setby %s"
|
|
|
|
keyChannelTopicSetTime = "channel.topic.settime %s"
|
|
|
|
keyChannelBanlist = "channel.banlist %s"
|
|
|
|
keyChannelExceptlist = "channel.exceptlist %s"
|
|
|
|
keyChannelInvitelist = "channel.invitelist %s"
|
|
|
|
keyChannelPassword = "channel.key %s"
|
|
|
|
keyChannelModes = "channel.modes %s"
|
|
|
|
keyChannelAccountToUMode = "channel.accounttoumode %s"
|
2020-01-08 08:14:41 +01:00
|
|
|
keyChannelUserLimit = "channel.userlimit %s"
|
2020-02-19 01:38:42 +01:00
|
|
|
keyChannelSettings = "channel.settings %s"
|
2020-12-14 11:00:21 +01:00
|
|
|
keyChannelForward = "channel.forward %s"
|
2019-12-17 01:50:15 +01:00
|
|
|
|
|
|
|
keyChannelPurged = "channel.purged %s"
|
2017-03-11 13:01:40 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2017-11-09 04:19:50 +01:00
|
|
|
channelKeyStrings = []string{
|
|
|
|
keyChannelExists,
|
|
|
|
keyChannelName,
|
|
|
|
keyChannelRegTime,
|
|
|
|
keyChannelFounder,
|
|
|
|
keyChannelTopic,
|
|
|
|
keyChannelTopicSetBy,
|
|
|
|
keyChannelTopicSetTime,
|
|
|
|
keyChannelBanlist,
|
|
|
|
keyChannelExceptlist,
|
|
|
|
keyChannelInvitelist,
|
2018-04-04 03:49:40 +02:00
|
|
|
keyChannelPassword,
|
|
|
|
keyChannelModes,
|
|
|
|
keyChannelAccountToUMode,
|
2020-01-08 08:14:41 +01:00
|
|
|
keyChannelUserLimit,
|
2020-02-19 01:38:42 +01:00
|
|
|
keyChannelSettings,
|
2020-12-14 11:00:21 +01:00
|
|
|
keyChannelForward,
|
2017-11-09 04:19:50 +01:00
|
|
|
}
|
2017-03-11 13:01:40 +01:00
|
|
|
)
|
|
|
|
|
2018-04-04 03:49:40 +02:00
|
|
|
// these are bit flags indicating what part of the channel status is "dirty"
|
|
|
|
// and needs to be read from memory and written to the db
|
|
|
|
const (
|
|
|
|
IncludeInitial uint = 1 << iota
|
|
|
|
IncludeTopic
|
|
|
|
IncludeModes
|
|
|
|
IncludeLists
|
2020-02-19 01:38:42 +01:00
|
|
|
IncludeSettings
|
2018-04-04 03:49:40 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// this is an OR of all possible flags
|
|
|
|
const (
|
2020-03-02 07:46:22 +01:00
|
|
|
IncludeAllAttrs = ^uint(0)
|
2018-04-04 03:49:40 +02:00
|
|
|
)
|
|
|
|
|
2017-03-11 13:01:40 +01:00
|
|
|
// RegisteredChannel holds details about a given registered channel.
|
|
|
|
type RegisteredChannel struct {
|
|
|
|
// Name of the channel.
|
|
|
|
Name string
|
2019-03-12 00:24:45 +01:00
|
|
|
// Casefolded name of the channel.
|
|
|
|
NameCasefolded string
|
2017-03-11 13:01:40 +01:00
|
|
|
// RegisteredAt represents the time that the channel was registered.
|
|
|
|
RegisteredAt time.Time
|
|
|
|
// Founder indicates the founder of the channel.
|
|
|
|
Founder string
|
|
|
|
// Topic represents the channel topic.
|
|
|
|
Topic string
|
|
|
|
// TopicSetBy represents the host that set the topic.
|
|
|
|
TopicSetBy string
|
|
|
|
// TopicSetTime represents the time the topic was set.
|
|
|
|
TopicSetTime time.Time
|
2018-04-04 03:49:40 +02:00
|
|
|
// Modes represents the channel modes
|
|
|
|
Modes []modes.Mode
|
|
|
|
// Key represents the channel key / password
|
|
|
|
Key string
|
2020-12-14 11:00:21 +01:00
|
|
|
// Forward is the forwarding/overflow (+f) channel
|
|
|
|
Forward string
|
2020-01-08 08:14:41 +01:00
|
|
|
// UserLimit is the user limit (0 for no limit)
|
|
|
|
UserLimit int
|
2018-04-04 03:49:40 +02:00
|
|
|
// AccountToUMode maps user accounts to their persistent channel modes (e.g., +q, +h)
|
|
|
|
AccountToUMode map[string]modes.Mode
|
2019-10-10 10:17:44 +02:00
|
|
|
// Bans represents the bans set on the channel.
|
|
|
|
Bans map[string]MaskInfo
|
|
|
|
// Excepts represents the exceptions set on the channel.
|
|
|
|
Excepts map[string]MaskInfo
|
|
|
|
// Invites represents the invite exceptions set on the channel.
|
|
|
|
Invites map[string]MaskInfo
|
2020-02-19 01:38:42 +01:00
|
|
|
// Settings are the chanserv-modifiable settings
|
|
|
|
Settings ChannelSettings
|
2017-03-11 13:01:40 +01:00
|
|
|
}
|
|
|
|
|
2019-12-17 01:50:15 +01:00
|
|
|
type ChannelPurgeRecord struct {
|
|
|
|
Oper string
|
|
|
|
PurgedAt time.Time
|
|
|
|
Reason string
|
|
|
|
}
|
|
|
|
|
2018-02-03 12:15:07 +01:00
|
|
|
// ChannelRegistry manages registered channels.
|
2017-11-09 04:19:50 +01:00
|
|
|
type ChannelRegistry struct {
|
2019-03-12 00:24:45 +01:00
|
|
|
server *Server
|
2017-06-05 04:01:37 +02:00
|
|
|
}
|
|
|
|
|
2018-02-03 12:15:07 +01:00
|
|
|
// NewChannelRegistry returns a new ChannelRegistry.
|
2019-03-12 00:24:45 +01:00
|
|
|
func (reg *ChannelRegistry) Initialize(server *Server) {
|
|
|
|
reg.server = server
|
|
|
|
}
|
|
|
|
|
2019-12-17 19:21:26 +01:00
|
|
|
// AllChannels returns the uncasefolded names of all registered channels.
|
|
|
|
func (reg *ChannelRegistry) AllChannels() (result []string) {
|
|
|
|
prefix := fmt.Sprintf(keyChannelName, "")
|
2019-03-12 00:24:45 +01:00
|
|
|
reg.server.store.View(func(tx *buntdb.Tx) error {
|
|
|
|
return tx.AscendGreaterOrEqual("", prefix, func(key, value string) bool {
|
|
|
|
if !strings.HasPrefix(key, prefix) {
|
|
|
|
return false
|
|
|
|
}
|
2019-12-17 19:21:26 +01:00
|
|
|
result = append(result, value)
|
2019-03-12 00:24:45 +01:00
|
|
|
return true
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
return
|
2017-11-09 04:19:50 +01:00
|
|
|
}
|
|
|
|
|
2019-12-17 19:21:26 +01:00
|
|
|
// PurgedChannels returns the set of all casefolded channel names that have been purged
|
2020-08-05 03:46:16 +02:00
|
|
|
func (reg *ChannelRegistry) PurgedChannels() (result utils.StringSet) {
|
|
|
|
result = make(utils.StringSet)
|
2019-12-17 01:50:15 +01:00
|
|
|
|
|
|
|
prefix := fmt.Sprintf(keyChannelPurged, "")
|
|
|
|
reg.server.store.View(func(tx *buntdb.Tx) error {
|
|
|
|
return tx.AscendGreaterOrEqual("", prefix, func(key, value string) bool {
|
|
|
|
if !strings.HasPrefix(key, prefix) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
channel := strings.TrimPrefix(key, prefix)
|
2020-08-05 03:46:16 +02:00
|
|
|
result.Add(channel)
|
2019-12-17 01:50:15 +01:00
|
|
|
return true
|
|
|
|
})
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-11-09 04:19:50 +01:00
|
|
|
// StoreChannel obtains a consistent view of a channel, then persists it to the store.
|
2019-03-12 00:24:45 +01:00
|
|
|
func (reg *ChannelRegistry) StoreChannel(info RegisteredChannel, includeFlags uint) (err error) {
|
2017-11-09 04:19:50 +01:00
|
|
|
if !reg.server.ChannelRegistrationEnabled() {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if info.Founder == "" {
|
|
|
|
// sanity check, don't try to store an unregistered channel
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
reg.server.store.Update(func(tx *buntdb.Tx) error {
|
2019-03-12 00:24:45 +01:00
|
|
|
reg.saveChannel(tx, info, includeFlags)
|
2017-11-09 04:19:50 +01:00
|
|
|
return nil
|
|
|
|
})
|
2019-03-12 00:24:45 +01:00
|
|
|
|
|
|
|
return nil
|
2017-11-09 04:19:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// LoadChannel loads a channel from the store.
|
2019-03-12 00:24:45 +01:00
|
|
|
func (reg *ChannelRegistry) LoadChannel(nameCasefolded string) (info RegisteredChannel, err error) {
|
2017-11-09 04:19:50 +01:00
|
|
|
if !reg.server.ChannelRegistrationEnabled() {
|
2019-03-12 00:24:45 +01:00
|
|
|
err = errFeatureDisabled
|
|
|
|
return
|
2017-03-11 13:01:40 +01:00
|
|
|
}
|
|
|
|
|
2017-11-09 04:19:50 +01:00
|
|
|
channelKey := nameCasefolded
|
|
|
|
// nice to have: do all JSON (de)serialization outside of the buntdb transaction
|
2019-03-12 00:24:45 +01:00
|
|
|
err = reg.server.store.View(func(tx *buntdb.Tx) error {
|
|
|
|
_, dberr := tx.Get(fmt.Sprintf(keyChannelExists, channelKey))
|
|
|
|
if dberr == buntdb.ErrNotFound {
|
2017-11-09 04:19:50 +01:00
|
|
|
// chan does not already exist, return
|
2019-03-12 00:24:45 +01:00
|
|
|
return errNoSuchChannel
|
2017-11-09 04:19:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// channel exists, load it
|
|
|
|
name, _ := tx.Get(fmt.Sprintf(keyChannelName, channelKey))
|
|
|
|
regTime, _ := tx.Get(fmt.Sprintf(keyChannelRegTime, channelKey))
|
|
|
|
regTimeInt, _ := strconv.ParseInt(regTime, 10, 64)
|
|
|
|
founder, _ := tx.Get(fmt.Sprintf(keyChannelFounder, channelKey))
|
|
|
|
topic, _ := tx.Get(fmt.Sprintf(keyChannelTopic, channelKey))
|
|
|
|
topicSetBy, _ := tx.Get(fmt.Sprintf(keyChannelTopicSetBy, channelKey))
|
2020-10-06 23:38:47 +02:00
|
|
|
var topicSetTime time.Time
|
|
|
|
topicSetTimeStr, _ := tx.Get(fmt.Sprintf(keyChannelTopicSetTime, channelKey))
|
|
|
|
if topicSetTimeInt, topicSetTimeErr := strconv.ParseInt(topicSetTimeStr, 10, 64); topicSetTimeErr == nil {
|
|
|
|
topicSetTime = time.Unix(0, topicSetTimeInt).UTC()
|
|
|
|
}
|
2018-04-04 03:49:40 +02:00
|
|
|
password, _ := tx.Get(fmt.Sprintf(keyChannelPassword, channelKey))
|
|
|
|
modeString, _ := tx.Get(fmt.Sprintf(keyChannelModes, channelKey))
|
2020-01-08 08:14:41 +01:00
|
|
|
userLimitString, _ := tx.Get(fmt.Sprintf(keyChannelUserLimit, channelKey))
|
2020-12-14 11:00:21 +01:00
|
|
|
forward, _ := tx.Get(fmt.Sprintf(keyChannelForward, channelKey))
|
2017-11-09 04:19:50 +01:00
|
|
|
banlistString, _ := tx.Get(fmt.Sprintf(keyChannelBanlist, channelKey))
|
|
|
|
exceptlistString, _ := tx.Get(fmt.Sprintf(keyChannelExceptlist, channelKey))
|
|
|
|
invitelistString, _ := tx.Get(fmt.Sprintf(keyChannelInvitelist, channelKey))
|
2018-04-04 03:49:40 +02:00
|
|
|
accountToUModeString, _ := tx.Get(fmt.Sprintf(keyChannelAccountToUMode, channelKey))
|
2020-02-19 01:38:42 +01:00
|
|
|
settingsString, _ := tx.Get(fmt.Sprintf(keyChannelSettings, channelKey))
|
2018-04-04 03:49:40 +02:00
|
|
|
|
|
|
|
modeSlice := make([]modes.Mode, len(modeString))
|
|
|
|
for i, mode := range modeString {
|
|
|
|
modeSlice[i] = modes.Mode(mode)
|
|
|
|
}
|
2017-11-09 04:19:50 +01:00
|
|
|
|
2020-01-08 08:14:41 +01:00
|
|
|
userLimit, _ := strconv.Atoi(userLimitString)
|
|
|
|
|
2019-10-10 10:17:44 +02:00
|
|
|
var banlist map[string]MaskInfo
|
2017-11-09 04:19:50 +01:00
|
|
|
_ = json.Unmarshal([]byte(banlistString), &banlist)
|
2019-10-10 10:17:44 +02:00
|
|
|
var exceptlist map[string]MaskInfo
|
2017-11-09 04:19:50 +01:00
|
|
|
_ = json.Unmarshal([]byte(exceptlistString), &exceptlist)
|
2019-10-10 10:17:44 +02:00
|
|
|
var invitelist map[string]MaskInfo
|
2017-11-09 04:19:50 +01:00
|
|
|
_ = json.Unmarshal([]byte(invitelistString), &invitelist)
|
2018-04-04 03:49:40 +02:00
|
|
|
accountToUMode := make(map[string]modes.Mode)
|
|
|
|
_ = json.Unmarshal([]byte(accountToUModeString), &accountToUMode)
|
2017-11-09 04:19:50 +01:00
|
|
|
|
2020-02-19 01:38:42 +01:00
|
|
|
var settings ChannelSettings
|
|
|
|
_ = json.Unmarshal([]byte(settingsString), &settings)
|
|
|
|
|
2019-03-12 00:24:45 +01:00
|
|
|
info = RegisteredChannel{
|
2018-04-04 03:49:40 +02:00
|
|
|
Name: name,
|
2020-03-20 19:40:14 +01:00
|
|
|
NameCasefolded: nameCasefolded,
|
2020-10-02 22:48:37 +02:00
|
|
|
RegisteredAt: time.Unix(0, regTimeInt).UTC(),
|
2018-04-04 03:49:40 +02:00
|
|
|
Founder: founder,
|
|
|
|
Topic: topic,
|
|
|
|
TopicSetBy: topicSetBy,
|
2020-10-06 23:38:47 +02:00
|
|
|
TopicSetTime: topicSetTime,
|
2018-04-04 03:49:40 +02:00
|
|
|
Key: password,
|
|
|
|
Modes: modeSlice,
|
2019-10-10 10:17:44 +02:00
|
|
|
Bans: banlist,
|
|
|
|
Excepts: exceptlist,
|
|
|
|
Invites: invitelist,
|
2018-04-04 03:49:40 +02:00
|
|
|
AccountToUMode: accountToUMode,
|
2020-01-08 08:14:41 +01:00
|
|
|
UserLimit: int(userLimit),
|
2020-02-19 01:38:42 +01:00
|
|
|
Settings: settings,
|
2020-12-14 11:00:21 +01:00
|
|
|
Forward: forward,
|
2017-11-09 04:19:50 +01:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
2019-03-12 00:24:45 +01:00
|
|
|
return
|
2018-06-04 11:02:22 +02:00
|
|
|
}
|
|
|
|
|
2019-03-12 00:24:45 +01:00
|
|
|
// Delete deletes a channel corresponding to `info`. If no such channel
|
|
|
|
// is present in the database, no error is returned.
|
|
|
|
func (reg *ChannelRegistry) Delete(info RegisteredChannel) (err error) {
|
2017-11-09 04:19:50 +01:00
|
|
|
if !reg.server.ChannelRegistrationEnabled() {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
reg.server.store.Update(func(tx *buntdb.Tx) error {
|
2019-03-12 00:24:45 +01:00
|
|
|
reg.deleteChannel(tx, info.NameCasefolded, info)
|
2017-11-09 04:19:50 +01:00
|
|
|
return nil
|
|
|
|
})
|
2019-03-12 00:24:45 +01:00
|
|
|
return nil
|
2017-11-09 04:19:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// delete a channel, unless it was overwritten by another registration of the same channel
|
|
|
|
func (reg *ChannelRegistry) deleteChannel(tx *buntdb.Tx, key string, info RegisteredChannel) {
|
|
|
|
_, err := tx.Get(fmt.Sprintf(keyChannelExists, key))
|
|
|
|
if err == nil {
|
|
|
|
regTime, _ := tx.Get(fmt.Sprintf(keyChannelRegTime, key))
|
|
|
|
regTimeInt, _ := strconv.ParseInt(regTime, 10, 64)
|
2020-10-02 22:48:37 +02:00
|
|
|
registeredAt := time.Unix(0, regTimeInt).UTC()
|
2017-11-09 04:19:50 +01:00
|
|
|
founder, _ := tx.Get(fmt.Sprintf(keyChannelFounder, key))
|
|
|
|
|
|
|
|
// to see if we're deleting the right channel, confirm the founder and the registration time
|
2020-10-06 23:38:47 +02:00
|
|
|
if founder == info.Founder && registeredAt.Equal(info.RegisteredAt) {
|
2017-11-09 04:19:50 +01:00
|
|
|
for _, keyFmt := range channelKeyStrings {
|
|
|
|
tx.Delete(fmt.Sprintf(keyFmt, key))
|
|
|
|
}
|
2019-02-06 10:32:04 +01:00
|
|
|
|
|
|
|
// remove this channel from the client's list of registered channels
|
|
|
|
channelsKey := fmt.Sprintf(keyAccountChannels, info.Founder)
|
|
|
|
channelsStr, err := tx.Get(channelsKey)
|
|
|
|
if err == buntdb.ErrNotFound {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
registeredChannels := unmarshalRegisteredChannels(channelsStr)
|
|
|
|
var nowRegisteredChannels []string
|
|
|
|
for _, channel := range registeredChannels {
|
|
|
|
if channel != key {
|
|
|
|
nowRegisteredChannels = append(nowRegisteredChannels, channel)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
tx.Set(channelsKey, strings.Join(nowRegisteredChannels, ","), nil)
|
2017-11-09 04:19:50 +01:00
|
|
|
}
|
|
|
|
}
|
2017-03-11 13:01:40 +01:00
|
|
|
}
|
|
|
|
|
2019-12-17 01:50:15 +01:00
|
|
|
func (reg *ChannelRegistry) updateAccountToChannelMapping(tx *buntdb.Tx, channelInfo RegisteredChannel) {
|
2019-03-12 00:24:45 +01:00
|
|
|
channelKey := channelInfo.NameCasefolded
|
2019-12-17 01:50:15 +01:00
|
|
|
chanFounderKey := fmt.Sprintf(keyChannelFounder, channelKey)
|
|
|
|
founder, existsErr := tx.Get(chanFounderKey)
|
|
|
|
if existsErr == buntdb.ErrNotFound || founder != channelInfo.Founder {
|
|
|
|
// add to new founder's list
|
2019-02-06 10:32:04 +01:00
|
|
|
accountChannelsKey := fmt.Sprintf(keyAccountChannels, channelInfo.Founder)
|
|
|
|
alreadyChannels, _ := tx.Get(accountChannelsKey)
|
|
|
|
newChannels := channelKey // this is the casefolded channel name
|
|
|
|
if alreadyChannels != "" {
|
|
|
|
newChannels = fmt.Sprintf("%s,%s", alreadyChannels, newChannels)
|
|
|
|
}
|
|
|
|
tx.Set(accountChannelsKey, newChannels, nil)
|
|
|
|
}
|
2019-12-17 01:50:15 +01:00
|
|
|
if existsErr == nil && founder != channelInfo.Founder {
|
|
|
|
// remove from old founder's list
|
|
|
|
accountChannelsKey := fmt.Sprintf(keyAccountChannels, founder)
|
|
|
|
alreadyChannelsRaw, _ := tx.Get(accountChannelsKey)
|
|
|
|
var newChannels []string
|
|
|
|
if alreadyChannelsRaw != "" {
|
|
|
|
for _, chname := range strings.Split(alreadyChannelsRaw, ",") {
|
|
|
|
if chname != channelInfo.NameCasefolded {
|
|
|
|
newChannels = append(newChannels, chname)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
tx.Set(accountChannelsKey, strings.Join(newChannels, ","), nil)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// saveChannel saves a channel to the store.
|
|
|
|
func (reg *ChannelRegistry) saveChannel(tx *buntdb.Tx, channelInfo RegisteredChannel, includeFlags uint) {
|
|
|
|
channelKey := channelInfo.NameCasefolded
|
|
|
|
// maintain the mapping of account -> registered channels
|
|
|
|
reg.updateAccountToChannelMapping(tx, channelInfo)
|
2019-02-06 10:32:04 +01:00
|
|
|
|
2018-04-04 03:49:40 +02:00
|
|
|
if includeFlags&IncludeInitial != 0 {
|
2019-12-17 01:50:15 +01:00
|
|
|
tx.Set(fmt.Sprintf(keyChannelExists, channelKey), "1", nil)
|
2018-04-04 03:49:40 +02:00
|
|
|
tx.Set(fmt.Sprintf(keyChannelName, channelKey), channelInfo.Name, nil)
|
2020-10-02 22:48:37 +02:00
|
|
|
tx.Set(fmt.Sprintf(keyChannelRegTime, channelKey), strconv.FormatInt(channelInfo.RegisteredAt.UnixNano(), 10), nil)
|
2018-04-04 03:49:40 +02:00
|
|
|
tx.Set(fmt.Sprintf(keyChannelFounder, channelKey), channelInfo.Founder, nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
if includeFlags&IncludeTopic != 0 {
|
|
|
|
tx.Set(fmt.Sprintf(keyChannelTopic, channelKey), channelInfo.Topic, nil)
|
2020-10-06 23:38:47 +02:00
|
|
|
var topicSetTimeStr string
|
|
|
|
if !channelInfo.TopicSetTime.IsZero() {
|
|
|
|
topicSetTimeStr = strconv.FormatInt(channelInfo.TopicSetTime.UnixNano(), 10)
|
|
|
|
}
|
|
|
|
tx.Set(fmt.Sprintf(keyChannelTopicSetTime, channelKey), topicSetTimeStr, nil)
|
2018-04-04 03:49:40 +02:00
|
|
|
tx.Set(fmt.Sprintf(keyChannelTopicSetBy, channelKey), channelInfo.TopicSetBy, nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
if includeFlags&IncludeModes != 0 {
|
|
|
|
tx.Set(fmt.Sprintf(keyChannelPassword, channelKey), channelInfo.Key, nil)
|
2020-10-12 21:06:17 +02:00
|
|
|
modeString := modes.Modes(channelInfo.Modes).String()
|
|
|
|
tx.Set(fmt.Sprintf(keyChannelModes, channelKey), modeString, nil)
|
2020-01-08 08:14:41 +01:00
|
|
|
tx.Set(fmt.Sprintf(keyChannelUserLimit, channelKey), strconv.Itoa(channelInfo.UserLimit), nil)
|
2020-12-14 11:00:21 +01:00
|
|
|
tx.Set(fmt.Sprintf(keyChannelForward, channelKey), channelInfo.Forward, nil)
|
2018-04-04 03:49:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if includeFlags&IncludeLists != 0 {
|
2019-10-10 10:17:44 +02:00
|
|
|
banlistString, _ := json.Marshal(channelInfo.Bans)
|
2017-11-09 04:19:50 +01:00
|
|
|
tx.Set(fmt.Sprintf(keyChannelBanlist, channelKey), string(banlistString), nil)
|
2019-10-10 10:17:44 +02:00
|
|
|
exceptlistString, _ := json.Marshal(channelInfo.Excepts)
|
2017-11-09 04:19:50 +01:00
|
|
|
tx.Set(fmt.Sprintf(keyChannelExceptlist, channelKey), string(exceptlistString), nil)
|
2019-10-10 10:17:44 +02:00
|
|
|
invitelistString, _ := json.Marshal(channelInfo.Invites)
|
2017-11-09 04:19:50 +01:00
|
|
|
tx.Set(fmt.Sprintf(keyChannelInvitelist, channelKey), string(invitelistString), nil)
|
2018-04-04 03:49:40 +02:00
|
|
|
accountToUModeString, _ := json.Marshal(channelInfo.AccountToUMode)
|
|
|
|
tx.Set(fmt.Sprintf(keyChannelAccountToUMode, channelKey), string(accountToUModeString), nil)
|
2017-11-09 04:19:50 +01:00
|
|
|
}
|
2020-02-19 01:38:42 +01:00
|
|
|
|
|
|
|
if includeFlags&IncludeSettings != 0 {
|
|
|
|
settingsString, _ := json.Marshal(channelInfo.Settings)
|
|
|
|
tx.Set(fmt.Sprintf(keyChannelSettings, channelKey), string(settingsString), nil)
|
|
|
|
}
|
2017-03-11 13:01:40 +01:00
|
|
|
}
|
2019-12-17 01:50:15 +01:00
|
|
|
|
|
|
|
// PurgeChannel records a channel purge.
|
|
|
|
func (reg *ChannelRegistry) PurgeChannel(chname string, record ChannelPurgeRecord) (err error) {
|
|
|
|
serialized, err := json.Marshal(record)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
serializedStr := string(serialized)
|
|
|
|
key := fmt.Sprintf(keyChannelPurged, chname)
|
|
|
|
|
|
|
|
return reg.server.store.Update(func(tx *buntdb.Tx) error {
|
|
|
|
tx.Set(key, serializedStr, nil)
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// LoadPurgeRecord retrieves information about whether and how a channel was purged.
|
|
|
|
func (reg *ChannelRegistry) LoadPurgeRecord(chname string) (record ChannelPurgeRecord, err error) {
|
|
|
|
var rawRecord string
|
|
|
|
key := fmt.Sprintf(keyChannelPurged, chname)
|
|
|
|
reg.server.store.View(func(tx *buntdb.Tx) error {
|
|
|
|
rawRecord, _ = tx.Get(key)
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if rawRecord == "" {
|
|
|
|
err = errNoSuchChannel
|
|
|
|
return
|
|
|
|
}
|
|
|
|
err = json.Unmarshal([]byte(rawRecord), &record)
|
|
|
|
if err != nil {
|
|
|
|
reg.server.logger.Error("internal", "corrupt purge record", chname, err.Error())
|
|
|
|
err = errNoSuchChannel
|
|
|
|
return
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// UnpurgeChannel deletes the record of a channel purge.
|
|
|
|
func (reg *ChannelRegistry) UnpurgeChannel(chname string) (err error) {
|
|
|
|
key := fmt.Sprintf(keyChannelPurged, chname)
|
|
|
|
return reg.server.store.Update(func(tx *buntdb.Tx) error {
|
|
|
|
tx.Delete(key)
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|