3
0
mirror of https://github.com/ergochat/ergo.git synced 2024-12-28 21:52:36 +01:00
ergo/irc/channel.go

1297 lines
41 KiB
Go
Raw Normal View History

// Copyright (c) 2012-2014 Jeremy Latt
// Copyright (c) 2014-2015 Edmund Huber
2017-03-27 14:15:02 +02:00
// Copyright (c) 2016-2017 Daniel Oaks <daniel@danieloaks.net>
// released under the MIT license
package irc
import (
"bytes"
"fmt"
"strconv"
"strings"
"time"
2017-01-10 17:09:08 +01:00
"sync"
"github.com/oragono/oragono/irc/caps"
"github.com/oragono/oragono/irc/history"
"github.com/oragono/oragono/irc/modes"
"github.com/oragono/oragono/irc/utils"
)
2019-05-22 03:40:25 +02:00
const (
histServMask = "HistServ!HistServ@localhost"
)
2017-04-16 03:31:33 +02:00
// Channel represents a channel that clients can join.
type Channel struct {
2019-03-12 00:24:45 +01:00
flags modes.ModeSet
lists map[modes.Mode]*UserMaskSet
key string
members MemberSet
membersCache []*Client // allow iteration over channel members without holding the lock
name string
nameCasefolded string
server *Server
createdTime time.Time
registeredFounder string
registeredTime time.Time
transferPendingTo string
topic string
topicSetBy string
topicSetTime time.Time
2018-12-28 19:45:55 +01:00
userLimit int
accountToUMode map[string]modes.Mode
history history.Buffer
2019-04-15 17:13:13 +02:00
stateMutex sync.RWMutex // tier 1
writerSemaphore utils.Semaphore // tier 1.5
joinPartMutex sync.Mutex // tier 3
ensureLoaded utils.Once // manages loading stored registration info from the database
2019-03-12 00:24:45 +01:00
dirtyBits uint
}
2014-02-05 04:28:24 +01:00
// NewChannel creates a new channel from a `Server` and a `name`
// string, which must be unique on the server.
2019-12-17 19:21:26 +01:00
func NewChannel(s *Server, name, casefoldedName string, registered bool) *Channel {
config := s.Config()
channel := &Channel{
createdTime: time.Now().UTC(), // may be overwritten by applyRegInfo
members: make(MemberSet),
name: name,
nameCasefolded: casefoldedName,
server: s,
2012-12-09 21:51:50 +01:00
}
channel.initializeLists()
2019-03-12 00:24:45 +01:00
channel.writerSemaphore.Initialize(1)
2019-05-19 22:34:52 +02:00
channel.history.Initialize(config.History.ChannelLength, config.History.AutoresizeWindow)
2019-03-12 00:24:45 +01:00
if !registered {
for _, mode := range config.Channels.defaultModes {
2018-04-23 00:47:10 +02:00
channel.flags.SetMode(mode, true)
2016-04-21 11:29:50 +02:00
}
2019-03-12 00:24:45 +01:00
// no loading to do, so "mark" the load operation as "done":
channel.ensureLoaded.Do(func() {})
} // else: modes will be loaded before first join
return channel
}
func (channel *Channel) initializeLists() {
channel.lists = map[modes.Mode]*UserMaskSet{
modes.BanMask: NewUserMaskSet(),
modes.ExceptMask: NewUserMaskSet(),
modes.InviteMask: NewUserMaskSet(),
}
channel.accountToUMode = make(map[string]modes.Mode)
}
2019-03-12 00:24:45 +01:00
// EnsureLoaded blocks until the channel's registration info has been loaded
// from the database.
func (channel *Channel) EnsureLoaded() {
channel.ensureLoaded.Do(func() {
nmc := channel.NameCasefolded()
info, err := channel.server.channelRegistry.LoadChannel(nmc)
if err == nil {
channel.applyRegInfo(info)
} else {
channel.server.logger.Error("internal", "couldn't load channel", nmc, err.Error())
}
})
}
func (channel *Channel) IsLoaded() bool {
return channel.ensureLoaded.Done()
}
// read in channel state that was persisted in the DB
2019-03-12 00:24:45 +01:00
func (channel *Channel) applyRegInfo(chanReg RegisteredChannel) {
channel.stateMutex.Lock()
defer channel.stateMutex.Unlock()
channel.registeredFounder = chanReg.Founder
channel.registeredTime = chanReg.RegisteredAt
channel.topic = chanReg.Topic
channel.topicSetBy = chanReg.TopicSetBy
channel.topicSetTime = chanReg.TopicSetTime
channel.name = chanReg.Name
channel.createdTime = chanReg.RegisteredAt
channel.key = chanReg.Key
for _, mode := range chanReg.Modes {
2018-04-23 00:47:10 +02:00
channel.flags.SetMode(mode, true)
}
for account, mode := range chanReg.AccountToUMode {
channel.accountToUMode[account] = mode
}
2019-10-10 10:17:44 +02:00
channel.lists[modes.BanMask].SetMasks(chanReg.Bans)
channel.lists[modes.InviteMask].SetMasks(chanReg.Invites)
channel.lists[modes.ExceptMask].SetMasks(chanReg.Excepts)
}
// obtain a consistent snapshot of the channel state that can be persisted to the DB
func (channel *Channel) ExportRegistration(includeFlags uint) (info RegisteredChannel) {
channel.stateMutex.RLock()
defer channel.stateMutex.RUnlock()
info.Name = channel.name
2019-03-12 00:24:45 +01:00
info.NameCasefolded = channel.nameCasefolded
info.Founder = channel.registeredFounder
info.RegisteredAt = channel.registeredTime
if includeFlags&IncludeTopic != 0 {
info.Topic = channel.topic
info.TopicSetBy = channel.topicSetBy
info.TopicSetTime = channel.topicSetTime
}
if includeFlags&IncludeModes != 0 {
info.Key = channel.key
2018-04-23 00:47:10 +02:00
info.Modes = channel.flags.AllModes()
}
if includeFlags&IncludeLists != 0 {
2019-10-10 10:17:44 +02:00
info.Bans = channel.lists[modes.BanMask].Masks()
info.Invites = channel.lists[modes.InviteMask].Masks()
info.Excepts = channel.lists[modes.ExceptMask].Masks()
info.AccountToUMode = make(map[string]modes.Mode)
for account, mode := range channel.accountToUMode {
info.AccountToUMode[account] = mode
}
}
return
}
2019-03-12 00:24:45 +01:00
// begin: asynchronous database writeback implementation, modeled on irc/socket.go
// MarkDirty marks part (or all) of a channel's data as needing to be written back
// to the database, then starts a writer goroutine if necessary.
// This is the equivalent of Socket.Write().
func (channel *Channel) MarkDirty(dirtyBits uint) {
channel.stateMutex.Lock()
isRegistered := channel.registeredFounder != ""
channel.dirtyBits = channel.dirtyBits | dirtyBits
channel.stateMutex.Unlock()
if !isRegistered {
return
}
channel.wakeWriter()
}
// IsClean returns whether a channel can be safely removed from the server.
// To avoid the obvious TOCTOU race condition, it must be called while holding
// ChannelManager's lock (that way, no one can join and make the channel dirty again
// between this method exiting and the actual deletion).
func (channel *Channel) IsClean() bool {
if !channel.writerSemaphore.TryAcquire() {
// a database write (which may fail) is in progress, the channel cannot be cleaned up
return false
}
defer channel.writerSemaphore.Release()
channel.stateMutex.RLock()
defer channel.stateMutex.RUnlock()
// the channel must be empty, and either be unregistered or fully written to the DB
return len(channel.members) == 0 && (channel.registeredFounder == "" || channel.dirtyBits == 0)
}
func (channel *Channel) wakeWriter() {
if channel.writerSemaphore.TryAcquire() {
go channel.writeLoop()
}
}
// equivalent of Socket.send()
func (channel *Channel) writeLoop() {
for {
// TODO(#357) check the error value of this and implement timed backoff
channel.performWrite(0)
channel.writerSemaphore.Release()
channel.stateMutex.RLock()
isDirty := channel.dirtyBits != 0
isEmpty := len(channel.members) == 0
channel.stateMutex.RUnlock()
if !isDirty {
if isEmpty {
channel.server.channels.Cleanup(channel)
}
return // nothing to do
} // else: isDirty, so we need to write again
if !channel.writerSemaphore.TryAcquire() {
return
}
}
}
// Store writes part (or all) of the channel's data back to the database,
// blocking until the write is complete. This is the equivalent of
// Socket.BlockingWrite.
func (channel *Channel) Store(dirtyBits uint) (err error) {
defer func() {
channel.stateMutex.Lock()
isDirty := channel.dirtyBits != 0
isEmpty := len(channel.members) == 0
channel.stateMutex.Unlock()
if isDirty {
channel.wakeWriter()
} else if isEmpty {
channel.server.channels.Cleanup(channel)
}
}()
channel.writerSemaphore.Acquire()
defer channel.writerSemaphore.Release()
return channel.performWrite(dirtyBits)
}
// do an individual write; equivalent of Socket.send()
func (channel *Channel) performWrite(additionalDirtyBits uint) (err error) {
channel.stateMutex.Lock()
dirtyBits := channel.dirtyBits | additionalDirtyBits
channel.dirtyBits = 0
isRegistered := channel.registeredFounder != ""
channel.stateMutex.Unlock()
if !isRegistered || dirtyBits == 0 {
return
}
info := channel.ExportRegistration(dirtyBits)
err = channel.server.channelRegistry.StoreChannel(info, dirtyBits)
if err != nil {
channel.stateMutex.Lock()
channel.dirtyBits = channel.dirtyBits | dirtyBits
channel.stateMutex.Unlock()
}
return
}
// SetRegistered registers the channel, returning an error if it was already registered.
func (channel *Channel) SetRegistered(founder string) error {
channel.stateMutex.Lock()
defer channel.stateMutex.Unlock()
if channel.registeredFounder != "" {
2018-02-03 13:03:36 +01:00
return errChannelAlreadyRegistered
}
channel.registeredFounder = founder
channel.registeredTime = time.Now().UTC()
channel.accountToUMode[founder] = modes.ChannelFounder
return nil
}
2018-06-04 11:02:22 +02:00
// SetUnregistered deletes the channel's registration information.
func (channel *Channel) SetUnregistered(expectedFounder string) {
2018-06-04 11:02:22 +02:00
channel.stateMutex.Lock()
defer channel.stateMutex.Unlock()
if channel.registeredFounder != expectedFounder {
return
}
2018-06-04 11:02:22 +02:00
channel.registeredFounder = ""
var zeroTime time.Time
channel.registeredTime = zeroTime
channel.accountToUMode = make(map[string]modes.Mode)
}
// implements `CHANSERV CLEAR #chan ACCESS` (resets bans, invites, excepts, and amodes)
func (channel *Channel) resetAccess() {
defer channel.MarkDirty(IncludeLists)
channel.stateMutex.Lock()
defer channel.stateMutex.Unlock()
channel.initializeLists()
if channel.registeredFounder != "" {
channel.accountToUMode[channel.registeredFounder] = modes.ChannelFounder
}
}
// IsRegistered returns whether the channel is registered.
func (channel *Channel) IsRegistered() bool {
channel.stateMutex.RLock()
defer channel.stateMutex.RUnlock()
return channel.registeredFounder != ""
}
type channelTransferStatus uint
const (
channelTransferComplete channelTransferStatus = iota
channelTransferPending
channelTransferCancelled
channelTransferFailed
)
// Transfer transfers ownership of a registered channel to a different account
func (channel *Channel) Transfer(client *Client, target string, hasPrivs bool) (status channelTransferStatus, err error) {
status = channelTransferFailed
defer func() {
if status == channelTransferComplete && err == nil {
channel.Store(IncludeAllChannelAttrs)
}
}()
cftarget, err := CasefoldName(target)
if err != nil {
err = errAccountDoesNotExist
return
}
channel.stateMutex.Lock()
defer channel.stateMutex.Unlock()
if channel.registeredFounder == "" {
err = errChannelNotOwnedByAccount
return
}
if hasPrivs {
channel.transferOwnership(cftarget)
return channelTransferComplete, nil
} else {
if channel.registeredFounder == cftarget {
// transferring back to yourself cancels a pending transfer
channel.transferPendingTo = ""
return channelTransferCancelled, nil
} else {
channel.transferPendingTo = cftarget
return channelTransferPending, nil
}
}
}
func (channel *Channel) transferOwnership(newOwner string) {
delete(channel.accountToUMode, channel.registeredFounder)
channel.registeredFounder = newOwner
channel.accountToUMode[channel.registeredFounder] = modes.ChannelFounder
channel.transferPendingTo = ""
}
// AcceptTransfer implements `CS TRANSFER #chan ACCEPT`
func (channel *Channel) AcceptTransfer(client *Client) (err error) {
defer func() {
if err == nil {
channel.Store(IncludeAllChannelAttrs)
}
}()
account := client.Account()
if account == "" {
return errAccountNotLoggedIn
}
channel.stateMutex.Lock()
defer channel.stateMutex.Unlock()
if account != channel.transferPendingTo {
return errChannelTransferNotOffered
}
channel.transferOwnership(account)
return nil
}
2018-04-24 09:11:11 +02:00
func (channel *Channel) regenerateMembersCache() {
channel.stateMutex.RLock()
2017-10-23 01:50:16 +02:00
result := make([]*Client, len(channel.members))
i := 0
for client := range channel.members {
result[i] = client
i++
}
2018-04-24 09:11:11 +02:00
channel.stateMutex.RUnlock()
channel.stateMutex.Lock()
2017-10-23 01:50:16 +02:00
channel.membersCache = result
2018-04-24 09:11:11 +02:00
channel.stateMutex.Unlock()
2012-12-10 06:46:22 +01:00
}
// Names sends the list of users joined to the channel to the given client.
2018-02-05 15:21:08 +01:00
func (channel *Channel) Names(client *Client, rb *ResponseBuffer) {
2019-04-23 07:52:26 +02:00
isJoined := channel.hasClient(client)
2019-04-29 03:09:56 +02:00
isOper := client.HasMode(modes.Operator)
isMultiPrefix := rb.session.capabilities.Has(caps.MultiPrefix)
isUserhostInNames := rb.session.capabilities.Has(caps.UserhostInNames)
maxNamLen := 480 - len(client.server.name) - len(client.Nick())
var namesLines []string
var buffer bytes.Buffer
2019-04-29 03:09:56 +02:00
if isJoined || !channel.flags.HasMode(modes.Secret) || isOper {
2019-04-23 07:52:26 +02:00
for _, target := range channel.Members() {
var nick string
if isUserhostInNames {
nick = target.NickMaskString()
} else {
nick = target.Nick()
}
channel.stateMutex.RLock()
modeSet := channel.members[target]
channel.stateMutex.RUnlock()
if modeSet == nil {
continue
}
2019-04-29 03:09:56 +02:00
if !isJoined && target.flags.HasMode(modes.Invisible) && !isOper {
2019-04-23 07:52:26 +02:00
continue
}
prefix := modeSet.Prefixes(isMultiPrefix)
if buffer.Len()+len(nick)+len(prefix)+1 > maxNamLen {
namesLines = append(namesLines, buffer.String())
buffer.Reset()
}
if buffer.Len() > 0 {
buffer.WriteString(" ")
}
buffer.WriteString(prefix)
buffer.WriteString(nick)
}
if buffer.Len() > 0 {
2019-04-23 07:52:26 +02:00
namesLines = append(namesLines, buffer.String())
}
2016-06-19 06:55:24 +02:00
}
for _, line := range namesLines {
if buffer.Len() > 0 {
rb.Add(nil, client.server.name, RPL_NAMREPLY, client.nick, "=", channel.name, line)
}
}
2018-02-05 15:21:08 +01:00
rb.Add(nil, client.server.name, RPL_ENDOFNAMES, client.nick, channel.name, client.t("End of NAMES list"))
2013-06-03 07:07:50 +02:00
}
// does `clientMode` give you privileges to grant/remove `targetMode` to/from people,
// or to kick them?
func channelUserModeHasPrivsOver(clientMode modes.Mode, targetMode modes.Mode) bool {
switch clientMode {
case modes.ChannelFounder:
return true
case modes.ChannelAdmin, modes.ChannelOperator:
// admins cannot kick other admins, operators *can* kick other operators
return targetMode != modes.ChannelFounder && targetMode != modes.ChannelAdmin
case modes.Halfop:
// halfops cannot kick other halfops
return targetMode == modes.Voice || targetMode == modes.Mode(0)
default:
// voice and unprivileged cannot kick anyone
return false
}
}
// ClientIsAtLeast returns whether the client has at least the given channel privilege.
func (channel *Channel) ClientIsAtLeast(client *Client, permission modes.Mode) bool {
channel.stateMutex.RLock()
clientModes := channel.members[client]
channel.stateMutex.RUnlock()
2018-04-23 00:47:10 +02:00
2018-05-23 21:35:50 +02:00
for _, mode := range modes.ChannelUserModes {
2018-04-23 00:47:10 +02:00
if clientModes.HasMode(mode) {
2016-10-22 16:45:51 +02:00
return true
}
if mode == permission {
break
}
}
return false
}
2017-10-23 01:50:16 +02:00
func (channel *Channel) ClientPrefixes(client *Client, isMultiPrefix bool) string {
channel.stateMutex.RLock()
defer channel.stateMutex.RUnlock()
modes, present := channel.members[client]
if !present {
return ""
} else {
return modes.Prefixes(isMultiPrefix)
}
}
func (channel *Channel) ClientHasPrivsOver(client *Client, target *Client) bool {
channel.stateMutex.RLock()
clientModes := channel.members[client]
targetModes := channel.members[target]
channel.stateMutex.RUnlock()
return channelUserModeHasPrivsOver(clientModes.HighestChannelUserMode(), targetModes.HighestChannelUserMode())
2017-10-23 01:50:16 +02:00
}
func (channel *Channel) hasClient(client *Client) bool {
channel.stateMutex.RLock()
_, present := channel.members[client]
channel.stateMutex.RUnlock()
2017-10-23 01:50:16 +02:00
return present
2014-02-05 04:28:24 +01:00
}
2014-02-09 03:14:39 +01:00
// <mode> <mode params>
2017-10-23 01:50:16 +02:00
func (channel *Channel) modeStrings(client *Client) (result []string) {
isMember := client.HasMode(modes.Operator) || channel.hasClient(client)
showKey := isMember && (channel.key != "")
showUserLimit := channel.userLimit > 0
mods := "+"
2017-10-23 01:50:16 +02:00
// flags with args
if showKey {
mods += modes.Key.String()
2014-02-09 08:33:56 +01:00
}
if showUserLimit {
mods += modes.UserLimit.String()
}
2014-02-15 06:57:08 +01:00
2018-04-23 00:47:10 +02:00
mods += channel.flags.String()
2017-10-23 01:50:16 +02:00
channel.stateMutex.RLock()
defer channel.stateMutex.RUnlock()
result = []string{mods}
2014-02-15 06:57:08 +01:00
// args for flags with args: The order must match above to keep
// positional arguments in place.
if showKey {
2017-10-23 01:50:16 +02:00
result = append(result, channel.key)
2014-02-15 06:57:08 +01:00
}
if showUserLimit {
2018-12-28 19:45:55 +01:00
result = append(result, strconv.Itoa(channel.userLimit))
}
2014-02-15 06:57:08 +01:00
2017-10-23 01:50:16 +02:00
return
2014-02-09 03:14:39 +01:00
}
2017-10-30 10:21:47 +01:00
func (channel *Channel) IsEmpty() bool {
channel.stateMutex.RLock()
defer channel.stateMutex.RUnlock()
return len(channel.members) == 0
}
// Join joins the given client to this channel (if they can be joined).
func (channel *Channel) Join(client *Client, key string, isSajoin bool, rb *ResponseBuffer) {
2019-01-01 19:15:38 +01:00
details := client.Details()
channel.stateMutex.RLock()
chname := channel.name
2018-12-23 19:25:02 +01:00
chcfname := channel.nameCasefolded
founder := channel.registeredFounder
2018-12-28 19:45:55 +01:00
chkey := channel.key
limit := channel.userLimit
chcount := len(channel.members)
_, alreadyJoined := channel.members[client]
2019-01-01 19:15:38 +01:00
persistentMode := channel.accountToUMode[details.account]
channel.stateMutex.RUnlock()
2018-12-28 19:45:55 +01:00
if alreadyJoined {
// no message needs to be sent
return
}
// the founder can always join (even if they disabled auto +q on join);
// anyone who automatically receives halfop or higher can always join
2019-01-01 19:15:38 +01:00
hasPrivs := isSajoin || (founder != "" && founder == details.account) || (persistentMode != 0 && persistentMode != modes.Voice)
2018-04-24 09:11:11 +02:00
2018-12-28 19:45:55 +01:00
if !hasPrivs && limit != 0 && chcount >= limit {
2019-03-19 08:35:49 +01:00
rb.Add(nil, client.server.name, ERR_CHANNELISFULL, details.nick, chname, fmt.Sprintf(client.t("Cannot join channel (+%s)"), "l"))
return
}
2018-12-28 19:45:55 +01:00
if !hasPrivs && chkey != "" && !utils.SecretTokensMatch(chkey, key) {
2019-03-19 08:35:49 +01:00
rb.Add(nil, client.server.name, ERR_BADCHANNELKEY, details.nick, chname, fmt.Sprintf(client.t("Cannot join channel (+%s)"), "k"))
2014-02-21 03:56:13 +01:00
return
}
2019-01-01 19:15:38 +01:00
isInvited := client.CheckInvited(chcfname) || channel.lists[modes.InviteMask].Match(details.nickMaskCasefolded)
if !hasPrivs && channel.flags.HasMode(modes.InviteOnly) && !isInvited {
2019-03-19 08:35:49 +01:00
rb.Add(nil, client.server.name, ERR_INVITEONLYCHAN, details.nick, chname, fmt.Sprintf(client.t("Cannot join channel (+%s)"), "i"))
2014-03-08 02:09:49 +01:00
return
}
2019-01-01 19:15:38 +01:00
if !hasPrivs && channel.lists[modes.BanMask].Match(details.nickMaskCasefolded) &&
2014-03-08 02:09:49 +01:00
!isInvited &&
2019-01-01 19:15:38 +01:00
!channel.lists[modes.ExceptMask].Match(details.nickMaskCasefolded) {
2019-03-19 08:35:49 +01:00
rb.Add(nil, client.server.name, ERR_BANNEDFROMCHAN, details.nick, chname, fmt.Sprintf(client.t("Cannot join channel (+%s)"), "b"))
2014-03-08 02:09:49 +01:00
return
}
2019-05-12 10:49:45 +02:00
if !hasPrivs && channel.flags.HasMode(modes.RegisteredOnly) && details.account == "" && !isInvited {
rb.Add(nil, client.server.name, ERR_BANNEDFROMCHAN, details.nick, chname, client.t("You must be registered to join that channel"))
return
}
2019-01-01 19:15:38 +01:00
client.server.logger.Debug("join", fmt.Sprintf("%s joined channel %s", details.nick, chname))
2019-05-07 05:17:57 +02:00
var message utils.SplitMessage
2019-02-13 19:22:00 +01:00
givenMode := func() (givenMode modes.Mode) {
2018-04-24 09:11:11 +02:00
channel.joinPartMutex.Lock()
defer channel.joinPartMutex.Unlock()
func() {
channel.stateMutex.Lock()
defer channel.stateMutex.Unlock()
channel.members.Add(client)
firstJoin := len(channel.members) == 1
2019-02-13 19:22:00 +01:00
newChannel := firstJoin && channel.registeredFounder == ""
2018-04-24 09:11:11 +02:00
if newChannel {
givenMode = modes.ChannelOperator
2018-02-05 15:21:08 +01:00
} else {
givenMode = persistentMode
2018-02-05 15:21:08 +01:00
}
2018-04-24 09:11:11 +02:00
if givenMode != 0 {
channel.members[client].SetMode(givenMode, true)
2018-02-05 15:21:08 +01:00
}
2018-04-24 09:11:11 +02:00
}()
2016-06-22 13:35:26 +02:00
2018-04-24 09:11:11 +02:00
channel.regenerateMembersCache()
2019-01-01 19:15:38 +01:00
2019-05-07 05:17:57 +02:00
message = utils.MakeSplitMessage("", true)
histItem := history.Item{
2019-01-01 19:15:38 +01:00
Type: history.Join,
Nick: details.nickMask,
AccountName: details.accountName,
Message: message,
2019-05-07 05:17:57 +02:00
}
histItem.Params[0] = details.realname
channel.history.Add(histItem)
2019-01-01 19:15:38 +01:00
2018-04-24 09:11:11 +02:00
return
}()
2017-10-23 01:50:16 +02:00
client.addChannel(channel)
2018-04-24 09:11:11 +02:00
var modestr string
if givenMode != 0 {
modestr = fmt.Sprintf("+%v", givenMode)
}
2018-04-24 09:11:11 +02:00
for _, member := range channel.Members() {
for _, session := range member.Sessions() {
if session == rb.session {
continue
} else if client == session.client {
channel.playJoinForSession(session)
continue
}
if session.capabilities.Has(caps.ExtendedJoin) {
2019-05-07 05:17:57 +02:00
session.sendFromClientInternal(false, message.Time, message.Msgid, details.nickMask, details.accountName, nil, "JOIN", chname, details.accountName, details.realname)
} else {
2019-05-07 05:17:57 +02:00
session.sendFromClientInternal(false, message.Time, message.Msgid, details.nickMask, details.accountName, nil, "JOIN", chname)
}
if givenMode != 0 {
session.Send(nil, client.server.name, "MODE", chname, modestr, details.nick)
}
2018-04-24 09:11:11 +02:00
}
}
if rb.session.capabilities.Has(caps.ExtendedJoin) {
2019-05-07 05:17:57 +02:00
rb.AddFromClient(message.Time, message.Msgid, details.nickMask, details.accountName, nil, "JOIN", chname, details.accountName, details.realname)
} else {
2019-05-07 05:17:57 +02:00
rb.AddFromClient(message.Time, message.Msgid, details.nickMask, details.accountName, nil, "JOIN", chname)
}
2018-04-24 09:11:11 +02:00
if rb.session.client == client {
// don't send topic and names for a SAJOIN of a different client
channel.SendTopic(client, rb, false)
channel.Names(client, rb)
}
2018-04-24 09:11:11 +02:00
2018-12-28 19:45:55 +01:00
// TODO #259 can be implemented as Flush(false) (i.e., nonblocking) while holding joinPartMutex
rb.Flush(true)
2019-05-30 01:23:46 +02:00
channel.autoReplayHistory(client, rb, message.Msgid)
}
func (channel *Channel) autoReplayHistory(client *Client, rb *ResponseBuffer, skipMsgid string) {
2019-05-21 01:08:57 +02:00
// autoreplay any messages as necessary
config := channel.server.Config()
var items []history.Item
2019-05-30 01:23:46 +02:00
if rb.session.zncPlaybackTimes != nil && (rb.session.zncPlaybackTimes.targets == nil || rb.session.zncPlaybackTimes.targets[channel.NameCasefolded()]) {
2019-05-21 01:08:57 +02:00
items, _ = channel.history.Between(rb.session.zncPlaybackTimes.after, rb.session.zncPlaybackTimes.before, false, config.History.ChathistoryMax)
2019-05-30 01:23:46 +02:00
} else if !rb.session.HasHistoryCaps() {
2019-05-21 01:08:57 +02:00
var replayLimit int
customReplayLimit := client.AccountSettings().AutoreplayLines
if customReplayLimit != nil {
replayLimit = *customReplayLimit
maxLimit := channel.server.Config().History.ChathistoryMax
if maxLimit < replayLimit {
replayLimit = maxLimit
}
} else {
replayLimit = channel.server.Config().History.AutoreplayOnJoin
}
if 0 < replayLimit {
items = channel.history.Latest(replayLimit)
2019-05-07 05:17:57 +02:00
}
2018-12-28 19:45:55 +01:00
}
2019-05-21 01:08:57 +02:00
// remove the client's own JOIN line from the replay
numItems := len(items)
for i := len(items) - 1; 0 <= i; i-- {
2019-05-30 01:23:46 +02:00
if items[i].Message.Msgid == skipMsgid {
2019-05-21 01:08:57 +02:00
// zero'ed items will not be replayed because their `Type` field is not recognized
items[i] = history.Item{}
numItems--
break
}
}
if 0 < numItems {
channel.replayHistoryItems(rb, items, true)
rb.Flush(true)
}
}
// plays channel join messages (the JOIN line, topic, and names) to a session.
// this is used when attaching a new session to an existing client that already has
// channels, and also when one session of a client initiates a JOIN and the other
// sessions need to receive the state change
func (channel *Channel) playJoinForSession(session *Session) {
client := session.client
sessionRb := NewResponseBuffer(session)
2019-05-22 22:10:56 +02:00
details := client.Details()
if session.capabilities.Has(caps.ExtendedJoin) {
2019-05-22 22:10:56 +02:00
sessionRb.Add(nil, details.nickMask, "JOIN", channel.Name(), details.accountName, details.realname)
} else {
2019-05-22 22:10:56 +02:00
sessionRb.Add(nil, details.nickMask, "JOIN", channel.Name())
}
channel.SendTopic(client, sessionRb, false)
channel.Names(client, sessionRb)
sessionRb.Send(false)
}
// Part parts the given client from this channel, with the given message.
2018-02-05 15:21:08 +01:00
func (channel *Channel) Part(client *Client, message string, rb *ResponseBuffer) {
2018-04-24 09:11:11 +02:00
chname := channel.Name()
2017-10-23 01:50:16 +02:00
if !channel.hasClient(client) {
2019-03-19 08:35:49 +01:00
rb.Add(nil, client.server.name, ERR_NOTONCHANNEL, client.Nick(), chname, client.t("You're not on that channel"))
return
}
2018-04-24 09:11:11 +02:00
channel.Quit(client)
2019-05-07 05:17:57 +02:00
splitMessage := utils.MakeSplitMessage(message, true)
2019-01-01 19:15:38 +01:00
details := client.Details()
params := make([]string, 1, 2)
params[0] = chname
if message != "" {
params = append(params, message)
}
2017-10-23 01:50:16 +02:00
for _, member := range channel.Members() {
member.sendFromClientInternal(false, splitMessage.Time, splitMessage.Msgid, details.nickMask, details.accountName, nil, "PART", params...)
}
rb.AddFromClient(splitMessage.Time, splitMessage.Msgid, details.nickMask, details.accountName, nil, "PART", params...)
for _, session := range client.Sessions() {
if session != rb.session {
session.sendFromClientInternal(false, splitMessage.Time, splitMessage.Msgid, details.nickMask, details.accountName, nil, "PART", params...)
}
}
channel.history.Add(history.Item{
Type: history.Part,
2019-01-01 19:15:38 +01:00
Nick: details.nickMask,
AccountName: details.accountName,
2019-05-07 05:17:57 +02:00
Message: splitMessage,
})
2019-01-01 19:15:38 +01:00
client.server.logger.Debug("part", fmt.Sprintf("%s left channel %s", details.nick, chname))
}
// Resume is called after a successful global resume to:
// 1. Replace the old client with the new in the channel's data structures
// 2. Send JOIN and MODE lines to channel participants (including the new client)
// 3. Replay missed message history to the client
2019-05-22 03:40:25 +02:00
func (channel *Channel) Resume(session *Session, timestamp time.Time) {
now := time.Now().UTC()
2019-05-22 03:40:25 +02:00
channel.resumeAndAnnounce(session)
if !timestamp.IsZero() {
2019-05-22 03:40:25 +02:00
channel.replayHistoryForResume(session, timestamp, now)
}
}
2019-05-22 03:40:25 +02:00
func (channel *Channel) resumeAndAnnounce(session *Session) {
channel.stateMutex.RLock()
modeSet := channel.members[session.client]
channel.stateMutex.RUnlock()
if modeSet == nil {
return
}
oldModes := modeSet.String()
if 0 < len(oldModes) {
oldModes = "+" + oldModes
}
// send join for old clients
2019-05-22 03:40:25 +02:00
chname := channel.Name()
details := session.client.Details()
for _, member := range channel.Members() {
for _, session := range member.Sessions() {
if session.capabilities.Has(caps.Resume) {
continue
}
if session.capabilities.Has(caps.ExtendedJoin) {
2019-05-22 22:10:56 +02:00
session.Send(nil, details.nickMask, "JOIN", chname, details.accountName, details.realname)
} else {
2019-05-22 03:40:25 +02:00
session.Send(nil, details.nickMask, "JOIN", chname)
}
if 0 < len(oldModes) {
2019-05-22 03:40:25 +02:00
session.Send(nil, channel.server.name, "MODE", chname, oldModes, details.nick)
}
}
}
2019-05-22 03:40:25 +02:00
rb := NewResponseBuffer(session)
// use blocking i/o to synchronize with the later history replay
if rb.session.capabilities.Has(caps.ExtendedJoin) {
2019-05-22 22:10:56 +02:00
rb.Add(nil, details.nickMask, "JOIN", channel.name, details.accountName, details.realname)
} else {
2019-05-22 03:40:25 +02:00
rb.Add(nil, details.nickMask, "JOIN", channel.name)
}
2019-05-22 03:40:25 +02:00
channel.SendTopic(session.client, rb, false)
channel.Names(session.client, rb)
2018-12-28 19:45:55 +01:00
rb.Send(true)
}
2019-05-22 03:40:25 +02:00
func (channel *Channel) replayHistoryForResume(session *Session, after time.Time, before time.Time) {
items, complete := channel.history.Between(after, before, false, 0)
2019-05-22 03:40:25 +02:00
rb := NewResponseBuffer(session)
2019-05-07 05:17:57 +02:00
channel.replayHistoryItems(rb, items, false)
2019-05-22 03:40:25 +02:00
if !complete && !session.resumeDetails.HistoryIncomplete {
2018-12-28 19:45:55 +01:00
// warn here if we didn't warn already
2019-05-22 03:40:25 +02:00
rb.Add(nil, histServMask, "NOTICE", channel.Name(), session.client.t("Some additional message history may have been lost"))
2018-12-28 19:45:55 +01:00
}
rb.Send(true)
}
func stripMaskFromNick(nickMask string) (nick string) {
index := strings.Index(nickMask, "!")
if index == -1 {
2019-05-07 05:17:57 +02:00
return nickMask
}
return nickMask[0:index]
}
2019-05-07 05:17:57 +02:00
func (channel *Channel) replayHistoryItems(rb *ResponseBuffer, items []history.Item, autoreplay bool) {
2019-12-18 23:38:14 +01:00
if len(items) == 0 {
return
}
chname := channel.Name()
2018-12-28 19:45:55 +01:00
client := rb.target
2019-05-07 05:17:57 +02:00
eventPlayback := rb.session.capabilities.Has(caps.EventPlayback)
extendedJoin := rb.session.capabilities.Has(caps.ExtendedJoin)
2019-12-18 23:38:14 +01:00
var playJoinsAsPrivmsg bool
if !eventPlayback {
switch client.AccountSettings().ReplayJoins {
case ReplayJoinsCommandsOnly:
playJoinsAsPrivmsg = !autoreplay
case ReplayJoinsAlways:
playJoinsAsPrivmsg = true
case ReplayJoinsNever:
playJoinsAsPrivmsg = false
}
2019-05-07 05:17:57 +02:00
}
2019-12-18 23:38:14 +01:00
2019-05-07 05:17:57 +02:00
batchID := rb.StartNestedHistoryBatch(chname)
defer rb.EndNestedBatch(batchID)
2018-12-28 19:45:55 +01:00
2019-05-07 05:17:57 +02:00
for _, item := range items {
nick := stripMaskFromNick(item.Nick)
switch item.Type {
case history.Privmsg:
2019-05-07 05:17:57 +02:00
rb.AddSplitMessageFromClient(item.Nick, item.AccountName, item.Tags, "PRIVMSG", chname, item.Message)
case history.Notice:
2019-05-07 05:17:57 +02:00
rb.AddSplitMessageFromClient(item.Nick, item.AccountName, item.Tags, "NOTICE", chname, item.Message)
case history.Tagmsg:
if rb.session.capabilities.Has(caps.MessageTags) {
rb.AddSplitMessageFromClient(item.Nick, item.AccountName, item.Tags, "TAGMSG", chname, item.Message)
}
case history.Join:
2019-05-07 05:17:57 +02:00
if eventPlayback {
if extendedJoin {
rb.AddFromClient(item.Message.Time, item.Message.Msgid, item.Nick, item.AccountName, nil, "HEVENT", "JOIN", chname, item.AccountName, item.Params[0])
} else {
rb.AddFromClient(item.Message.Time, item.Message.Msgid, item.Nick, item.AccountName, nil, "HEVENT", "JOIN", chname)
}
} else {
2019-05-19 10:27:44 +02:00
if !playJoinsAsPrivmsg {
2019-05-07 05:17:57 +02:00
continue // #474
}
var message string
if item.AccountName == "*" {
message = fmt.Sprintf(client.t("%s joined the channel"), nick)
} else {
message = fmt.Sprintf(client.t("%[1]s [account: %[2]s] joined the channel"), nick, item.AccountName)
}
2019-05-22 03:40:25 +02:00
rb.AddFromClient(item.Message.Time, utils.MungeSecretToken(item.Message.Msgid), histServMask, "*", nil, "PRIVMSG", chname, message)
}
case history.Part:
2019-05-07 05:17:57 +02:00
if eventPlayback {
rb.AddFromClient(item.Message.Time, item.Message.Msgid, item.Nick, item.AccountName, nil, "HEVENT", "PART", chname, item.Message.Message)
} else {
2019-05-19 10:27:44 +02:00
if !playJoinsAsPrivmsg {
2019-05-07 05:17:57 +02:00
continue // #474
}
message := fmt.Sprintf(client.t("%[1]s left the channel (%[2]s)"), nick, item.Message.Message)
2019-05-22 03:40:25 +02:00
rb.AddFromClient(item.Message.Time, utils.MungeSecretToken(item.Message.Msgid), histServMask, "*", nil, "PRIVMSG", chname, message)
2019-05-07 05:17:57 +02:00
}
case history.Kick:
2019-05-07 05:17:57 +02:00
if eventPlayback {
rb.AddFromClient(item.Message.Time, item.Message.Msgid, item.Nick, item.AccountName, nil, "HEVENT", "KICK", chname, item.Params[0], item.Message.Message)
} else {
message := fmt.Sprintf(client.t("%[1]s kicked %[2]s (%[3]s)"), nick, item.Params[0], item.Message.Message)
2019-05-22 03:40:25 +02:00
rb.AddFromClient(item.Message.Time, utils.MungeSecretToken(item.Message.Msgid), histServMask, "*", nil, "PRIVMSG", chname, message)
2019-05-07 05:17:57 +02:00
}
case history.Quit:
if eventPlayback {
rb.AddFromClient(item.Message.Time, item.Message.Msgid, item.Nick, item.AccountName, nil, "HEVENT", "QUIT", item.Message.Message)
} else {
2019-05-19 10:27:44 +02:00
if !playJoinsAsPrivmsg {
2019-05-07 05:17:57 +02:00
continue // #474
}
message := fmt.Sprintf(client.t("%[1]s quit (%[2]s)"), nick, item.Message.Message)
2019-05-22 03:40:25 +02:00
rb.AddFromClient(item.Message.Time, utils.MungeSecretToken(item.Message.Msgid), histServMask, "*", nil, "PRIVMSG", chname, message)
2019-05-07 05:17:57 +02:00
}
case history.Nick:
if eventPlayback {
rb.AddFromClient(item.Message.Time, item.Message.Msgid, item.Nick, item.AccountName, nil, "HEVENT", "NICK", item.Params[0])
} else {
message := fmt.Sprintf(client.t("%[1]s changed nick to %[2]s"), nick, item.Params[0])
2019-05-22 03:40:25 +02:00
rb.AddFromClient(item.Message.Time, utils.MungeSecretToken(item.Message.Msgid), histServMask, "*", nil, "PRIVMSG", chname, message)
2019-05-07 05:17:57 +02:00
}
}
}
}
2017-10-23 01:50:16 +02:00
// SendTopic sends the channel topic to the given client.
2019-02-13 19:22:00 +01:00
// `sendNoTopic` controls whether RPL_NOTOPIC is sent when the topic is unset
func (channel *Channel) SendTopic(client *Client, rb *ResponseBuffer, sendNoTopic bool) {
2017-10-23 01:50:16 +02:00
channel.stateMutex.RLock()
name := channel.name
topic := channel.topic
topicSetBy := channel.topicSetBy
topicSetTime := channel.topicSetTime
_, hasClient := channel.members[client]
2017-10-23 01:50:16 +02:00
channel.stateMutex.RUnlock()
if !hasClient {
rb.Add(nil, client.server.name, ERR_NOTONCHANNEL, client.Nick(), channel.name, client.t("You're not on that channel"))
return
}
2017-10-23 01:50:16 +02:00
if topic == "" {
2019-02-13 19:22:00 +01:00
if sendNoTopic {
rb.Add(nil, client.server.name, RPL_NOTOPIC, client.nick, name, client.t("No topic is set"))
}
return
}
2018-02-05 15:21:08 +01:00
rb.Add(nil, client.server.name, RPL_TOPIC, client.nick, name, topic)
rb.Add(nil, client.server.name, RPL_TOPICTIME, client.nick, name, topicSetBy, strconv.FormatInt(topicSetTime.Unix(), 10))
}
// SetTopic sets the topic of this channel, if the client is allowed to do so.
2018-02-05 15:21:08 +01:00
func (channel *Channel) SetTopic(client *Client, topic string, rb *ResponseBuffer) {
2018-04-23 00:47:10 +02:00
if !(client.HasMode(modes.Operator) || channel.hasClient(client)) {
2019-03-19 08:35:49 +01:00
rb.Add(nil, client.server.name, ERR_NOTONCHANNEL, client.Nick(), channel.Name(), client.t("You're not on that channel"))
return
}
2018-04-23 00:47:10 +02:00
if channel.flags.HasMode(modes.OpOnlyTopic) && !channel.ClientIsAtLeast(client, modes.ChannelOperator) {
2019-03-19 08:35:49 +01:00
rb.Add(nil, client.server.name, ERR_CHANOPRIVSNEEDED, client.Nick(), channel.Name(), client.t("You're not a channel operator"))
2014-02-16 04:56:38 +01:00
return
}
2019-05-23 01:07:12 +02:00
topicLimit := client.server.Config().Limits.TopicLen
if len(topic) > topicLimit {
topic = topic[:topicLimit]
2016-09-12 04:22:50 +02:00
}
2017-10-23 01:50:16 +02:00
channel.stateMutex.Lock()
channel.topic = topic
channel.topicSetBy = client.nickMaskString
channel.topicSetTime = time.Now().UTC()
2017-10-23 01:50:16 +02:00
channel.stateMutex.Unlock()
2014-02-21 05:47:05 +01:00
prefix := client.NickMaskString()
2017-10-23 01:50:16 +02:00
for _, member := range channel.Members() {
for _, session := range member.Sessions() {
if session == rb.session {
rb.Add(nil, prefix, "TOPIC", channel.name, topic)
} else {
session.Send(nil, prefix, "TOPIC", channel.name, topic)
}
2018-02-05 15:21:08 +01:00
}
}
2019-03-12 00:24:45 +01:00
channel.MarkDirty(IncludeTopic)
}
// CanSpeak returns true if the client can speak on this channel.
func (channel *Channel) CanSpeak(client *Client) bool {
2017-10-23 01:50:16 +02:00
channel.stateMutex.RLock()
defer channel.stateMutex.RUnlock()
2017-01-10 17:09:08 +01:00
2017-10-23 01:50:16 +02:00
_, hasClient := channel.members[client]
2018-04-23 00:47:10 +02:00
if channel.flags.HasMode(modes.NoOutside) && !hasClient {
return false
}
2018-04-23 00:47:10 +02:00
if channel.flags.HasMode(modes.Moderated) && !channel.ClientIsAtLeast(client, modes.Voice) {
return false
}
2018-04-23 00:47:10 +02:00
if channel.flags.HasMode(modes.RegisteredOnly) && client.Account() == "" {
return false
}
return true
}
2019-12-23 21:26:37 +01:00
func msgCommandToHistType(command string) (history.ItemType, error) {
switch command {
case "PRIVMSG":
2019-03-19 08:35:49 +01:00
return history.Privmsg, nil
case "NOTICE":
2019-03-19 08:35:49 +01:00
return history.Notice, nil
case "TAGMSG":
2019-03-19 08:35:49 +01:00
return history.Tagmsg, nil
default:
2019-03-19 08:35:49 +01:00
return history.ItemType(0), errInvalidParams
}
}
func (channel *Channel) SendSplitMessage(command string, minPrefixMode modes.Mode, clientOnlyTags map[string]string, client *Client, message utils.SplitMessage, rb *ResponseBuffer) {
2019-12-23 21:26:37 +01:00
histType, err := msgCommandToHistType(command)
2019-03-19 08:35:49 +01:00
if err != nil {
2014-02-09 08:33:56 +01:00
return
}
2017-01-10 17:09:08 +01:00
if !channel.CanSpeak(client) {
2019-03-19 08:35:49 +01:00
if histType != history.Notice {
rb.Add(nil, client.server.name, ERR_CANNOTSENDTOCHAN, client.Nick(), channel.Name(), client.t("Cannot send to channel"))
}
return
}
nickmask := client.NickMaskString()
account := client.AccountName()
chname := channel.Name()
// STATUSMSG targets are prefixed with the supplied min-prefix, e.g., @#channel
if minPrefixMode != modes.Mode(0) {
chname = fmt.Sprintf("%s%s", modes.ChannelModePrefixes[minPrefixMode], chname)
}
// send echo-message
if rb.session.capabilities.Has(caps.EchoMessage) {
var tagsToUse map[string]string
if rb.session.capabilities.Has(caps.MessageTags) {
tagsToUse = clientOnlyTags
}
if histType == history.Tagmsg && rb.session.capabilities.Has(caps.MessageTags) {
2019-05-07 05:17:57 +02:00
rb.AddFromClient(message.Time, message.Msgid, nickmask, account, tagsToUse, command, chname)
} else {
rb.AddSplitMessageFromClient(nickmask, account, tagsToUse, command, chname, message)
}
}
// send echo-message to other connected sessions
for _, session := range client.Sessions() {
2019-05-19 10:27:44 +02:00
if session == rb.session {
continue
}
var tagsToUse map[string]string
if session.capabilities.Has(caps.MessageTags) {
tagsToUse = clientOnlyTags
}
if histType == history.Tagmsg && session.capabilities.Has(caps.MessageTags) {
2019-05-13 06:39:59 +02:00
session.sendFromClientInternal(false, message.Time, message.Msgid, nickmask, account, tagsToUse, command, chname)
2019-05-19 10:27:44 +02:00
} else if histType != history.Tagmsg {
2019-05-13 06:39:59 +02:00
session.sendSplitMsgFromClientInternal(false, nickmask, account, tagsToUse, command, chname, message)
}
}
for _, member := range channel.Members() {
// echo-message is handled above, so skip sending the msg to the user themselves as well
if member == client {
continue
}
if minPrefixMode != modes.Mode(0) && !channel.ClientIsAtLeast(member, minPrefixMode) {
// STATUSMSG
continue
2017-01-14 12:48:57 +01:00
}
for _, session := range member.Sessions() {
var tagsToUse map[string]string
if session.capabilities.Has(caps.MessageTags) {
tagsToUse = clientOnlyTags
} else if histType == history.Tagmsg {
continue
}
if histType == history.Tagmsg {
2019-05-13 06:39:59 +02:00
session.sendFromClientInternal(false, message.Time, message.Msgid, nickmask, account, tagsToUse, command, chname)
} else {
2019-05-13 06:39:59 +02:00
session.sendSplitMsgFromClientInternal(false, nickmask, account, tagsToUse, command, chname, message)
}
}
}
channel.history.Add(history.Item{
Type: histType,
Message: message,
Nick: nickmask,
AccountName: account,
2019-05-07 05:17:57 +02:00
Tags: clientOnlyTags,
})
}
2018-04-23 00:47:10 +02:00
func (channel *Channel) applyModeToMember(client *Client, mode modes.Mode, op modes.ModeOp, nick string, rb *ResponseBuffer) (result *modes.ModeChange) {
target := channel.server.clients.Get(nick)
if target == nil {
rb.Add(nil, client.server.name, ERR_NOSUCHNICK, client.Nick(), utils.SafeErrorParam(nick), client.t("No such nick"))
2016-06-22 13:35:26 +02:00
return nil
2014-02-23 00:01:11 +01:00
}
2017-10-23 01:50:16 +02:00
channel.stateMutex.Lock()
modeset, exists := channel.members[target]
if exists {
2018-04-24 02:03:26 +02:00
if modeset.SetMode(mode, op == modes.Add) {
2018-04-23 00:47:10 +02:00
result = &modes.ModeChange{
Op: op,
Mode: mode,
Arg: nick,
}
}
2014-02-23 00:01:11 +01:00
}
2017-10-23 01:50:16 +02:00
channel.stateMutex.Unlock()
2014-02-23 00:01:11 +01:00
2017-10-23 01:50:16 +02:00
if !exists {
2019-02-17 12:51:48 +01:00
rb.Add(nil, client.server.name, ERR_USERNOTINCHANNEL, client.Nick(), channel.Name(), client.t("They aren't on that channel"))
2014-02-23 00:01:11 +01:00
}
2018-04-23 00:47:10 +02:00
return
2014-02-23 00:01:11 +01:00
}
// ShowMaskList shows the given list to the client.
2018-02-05 15:21:08 +01:00
func (channel *Channel) ShowMaskList(client *Client, mode modes.Mode, rb *ResponseBuffer) {
// choose appropriate modes
var rpllist, rplendoflist string
if mode == modes.BanMask {
rpllist = RPL_BANLIST
rplendoflist = RPL_ENDOFBANLIST
} else if mode == modes.ExceptMask {
rpllist = RPL_EXCEPTLIST
rplendoflist = RPL_ENDOFEXCEPTLIST
} else if mode == modes.InviteMask {
rpllist = RPL_INVITELIST
rplendoflist = RPL_ENDOFINVITELIST
}
nick := client.Nick()
2019-10-10 10:17:44 +02:00
chname := channel.Name()
for mask, info := range channel.lists[mode].Masks() {
rb.Add(nil, client.server.name, rpllist, nick, chname, mask, info.CreatorNickmask, strconv.FormatInt(info.TimeCreated.Unix(), 10))
}
2017-10-23 01:50:16 +02:00
2019-10-10 10:17:44 +02:00
rb.Add(nil, client.server.name, rplendoflist, nick, chname, client.t("End of list"))
2014-03-08 02:35:58 +01:00
}
2014-03-08 02:09:49 +01:00
2017-10-23 01:50:16 +02:00
// Quit removes the given client from the channel
func (channel *Channel) Quit(client *Client) {
2018-04-24 09:11:11 +02:00
channelEmpty := func() bool {
channel.joinPartMutex.Lock()
defer channel.joinPartMutex.Unlock()
2017-10-23 01:50:16 +02:00
2018-04-24 09:11:11 +02:00
channel.stateMutex.Lock()
channel.members.Remove(client)
channelEmpty := len(channel.members) == 0
channel.stateMutex.Unlock()
channel.regenerateMembersCache()
return channelEmpty
}()
2018-04-24 09:11:11 +02:00
if channelEmpty {
client.server.channels.Cleanup(channel)
}
2018-04-24 09:11:11 +02:00
client.removeChannel(channel)
2014-02-17 08:29:11 +01:00
}
func (channel *Channel) Kick(client *Client, target *Client, comment string, rb *ResponseBuffer, hasPrivs bool) {
if !hasPrivs {
if !(client.HasMode(modes.Operator) || channel.hasClient(client)) {
rb.Add(nil, client.server.name, ERR_NOTONCHANNEL, client.Nick(), channel.Name(), client.t("You're not on that channel"))
return
}
if !channel.ClientHasPrivsOver(client, target) {
rb.Add(nil, client.server.name, ERR_CHANOPRIVSNEEDED, client.Nick(), channel.Name(), client.t("You don't have enough channel privileges"))
return
}
2014-02-17 08:29:11 +01:00
}
2017-10-23 01:50:16 +02:00
if !channel.hasClient(target) {
2019-03-19 08:35:49 +01:00
rb.Add(nil, client.server.name, ERR_USERNOTINCHANNEL, client.Nick(), channel.Name(), client.t("They aren't on that channel"))
2014-02-17 08:29:11 +01:00
return
}
kicklimit := channel.server.Config().Limits.KickLen
2017-10-23 01:50:16 +02:00
if len(comment) > kicklimit {
comment = comment[:kicklimit]
2016-09-12 04:22:50 +02:00
}
2019-05-07 05:17:57 +02:00
message := utils.MakeSplitMessage(comment, true)
clientMask := client.NickMaskString()
2019-05-07 05:17:57 +02:00
clientAccount := client.AccountName()
targetNick := target.Nick()
chname := channel.Name()
2017-10-23 01:50:16 +02:00
for _, member := range channel.Members() {
for _, session := range member.Sessions() {
if session != rb.session {
2019-05-07 05:17:57 +02:00
session.sendFromClientInternal(false, message.Time, message.Msgid, clientMask, clientAccount, nil, "KICK", chname, targetNick, comment)
}
}
}
rb.Add(nil, clientMask, "KICK", chname, targetNick, comment)
2017-10-23 01:50:16 +02:00
2019-05-07 05:17:57 +02:00
histItem := history.Item{
Type: history.Kick,
Nick: clientMask,
AccountName: target.AccountName(),
Message: message,
2019-05-07 05:17:57 +02:00
}
histItem.Params[0] = targetNick
channel.history.Add(histItem)
2017-10-23 01:50:16 +02:00
channel.Quit(target)
}
2014-02-25 16:28:09 +01:00
// Invite invites the given client to the channel, if the inviter can do so.
2018-02-05 15:21:08 +01:00
func (channel *Channel) Invite(invitee *Client, inviter *Client, rb *ResponseBuffer) {
chname := channel.Name()
2018-04-23 00:47:10 +02:00
if channel.flags.HasMode(modes.InviteOnly) && !channel.ClientIsAtLeast(inviter, modes.ChannelOperator) {
2019-03-19 08:35:49 +01:00
rb.Add(nil, inviter.server.name, ERR_CHANOPRIVSNEEDED, inviter.Nick(), channel.Name(), inviter.t("You're not a channel operator"))
2014-02-25 16:28:09 +01:00
return
}
2017-10-23 01:50:16 +02:00
if !channel.hasClient(inviter) {
2019-03-19 08:35:49 +01:00
rb.Add(nil, inviter.server.name, ERR_NOTONCHANNEL, inviter.Nick(), channel.Name(), inviter.t("You're not on that channel"))
2014-02-25 16:28:09 +01:00
return
}
2018-04-23 00:47:10 +02:00
if channel.flags.HasMode(modes.InviteOnly) {
2018-12-23 19:25:02 +01:00
invitee.Invite(channel.NameCasefolded())
2014-03-08 02:09:49 +01:00
}
2017-10-23 01:50:16 +02:00
for _, member := range channel.Members() {
if member == inviter || member == invitee || !channel.ClientIsAtLeast(member, modes.Halfop) {
continue
}
for _, session := range member.Sessions() {
if session.capabilities.Has(caps.InviteNotify) {
session.Send(nil, inviter.NickMaskString(), "INVITE", invitee.Nick(), chname)
}
2016-10-16 06:14:55 +02:00
}
}
2019-02-17 20:29:04 +01:00
cnick := inviter.Nick()
tnick := invitee.Nick()
rb.Add(nil, inviter.server.name, RPL_INVITING, cnick, tnick, chname)
invitee.Send(nil, inviter.NickMaskString(), "INVITE", tnick, chname)
2019-04-28 21:10:03 +02:00
if invitee.Away() {
2019-02-17 20:29:04 +01:00
rb.Add(nil, inviter.server.name, RPL_AWAY, cnick, tnick, invitee.AwayMessage())
2014-02-25 16:28:09 +01:00
}
}