2016-06-15 13:50:56 +02:00
|
|
|
// 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>
|
2016-06-15 13:50:56 +02:00
|
|
|
// released under the MIT license
|
|
|
|
|
2012-12-09 07:54:58 +01:00
|
|
|
package irc
|
|
|
|
|
2014-02-22 21:49:33 +01:00
|
|
|
import (
|
2018-04-24 11:46:01 +02:00
|
|
|
"bytes"
|
2016-10-11 15:51:46 +02:00
|
|
|
"fmt"
|
2014-02-22 21:49:33 +01:00
|
|
|
"strconv"
|
2019-01-02 23:52:36 +01:00
|
|
|
"strings"
|
2016-06-26 13:06:28 +02:00
|
|
|
"time"
|
2016-10-16 04:54:15 +02:00
|
|
|
|
2017-01-10 17:09:08 +01:00
|
|
|
"sync"
|
|
|
|
|
2017-06-15 18:14:19 +02:00
|
|
|
"github.com/goshuirc/irc-go/ircmsg"
|
2017-09-29 04:07:52 +02:00
|
|
|
"github.com/oragono/oragono/irc/caps"
|
2018-11-26 11:23:27 +01:00
|
|
|
"github.com/oragono/oragono/irc/history"
|
2018-02-03 11:21:32 +01:00
|
|
|
"github.com/oragono/oragono/irc/modes"
|
2018-11-26 11:23:27 +01:00
|
|
|
"github.com/oragono/oragono/irc/utils"
|
2017-11-09 04:19:50 +01:00
|
|
|
)
|
|
|
|
|
2017-04-16 03:31:33 +02:00
|
|
|
// Channel represents a channel that clients can join.
|
2012-12-09 07:54:58 +01:00
|
|
|
type Channel struct {
|
2018-04-23 00:47:10 +02:00
|
|
|
flags *modes.ModeSet
|
2018-02-03 11:21:32 +01:00
|
|
|
lists map[modes.Mode]*UserMaskSet
|
2017-11-07 20:38:18 +01:00
|
|
|
key string
|
|
|
|
members MemberSet
|
2018-04-24 11:46:01 +02:00
|
|
|
membersCache []*Client // allow iteration over channel members without holding the lock
|
2017-11-07 20:38:18 +01:00
|
|
|
name string
|
|
|
|
nameCasefolded string
|
|
|
|
server *Server
|
|
|
|
createdTime time.Time
|
2017-11-09 04:19:50 +01:00
|
|
|
registeredFounder string
|
|
|
|
registeredTime time.Time
|
2017-11-22 10:41:11 +01:00
|
|
|
stateMutex sync.RWMutex // tier 1
|
2018-04-24 09:11:11 +02:00
|
|
|
joinPartMutex sync.Mutex // tier 3
|
2017-11-07 20:38:18 +01:00
|
|
|
topic string
|
|
|
|
topicSetBy string
|
|
|
|
topicSetTime time.Time
|
2018-12-28 19:45:55 +01:00
|
|
|
userLimit int
|
2018-04-04 03:49:40 +02:00
|
|
|
accountToUMode map[string]modes.Mode
|
2018-11-26 11:23:27 +01:00
|
|
|
history history.Buffer
|
2012-12-09 07:54:58 +01:00
|
|
|
}
|
|
|
|
|
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.
|
2018-04-04 03:49:40 +02:00
|
|
|
func NewChannel(s *Server, name string, regInfo *RegisteredChannel) *Channel {
|
2016-10-11 15:51:46 +02:00
|
|
|
casefoldedName, err := CasefoldChannel(name)
|
|
|
|
if err != nil {
|
2018-12-31 17:33:42 +01:00
|
|
|
s.logger.Error("internal", "Bad channel name", name, err.Error())
|
2016-10-11 15:51:46 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2012-12-15 23:34:20 +01:00
|
|
|
channel := &Channel{
|
2017-11-09 04:19:50 +01:00
|
|
|
createdTime: time.Now(), // may be overwritten by applyRegInfo
|
2018-04-23 00:47:10 +02:00
|
|
|
flags: modes.NewModeSet(),
|
2018-02-03 11:21:32 +01:00
|
|
|
lists: map[modes.Mode]*UserMaskSet{
|
|
|
|
modes.BanMask: NewUserMaskSet(),
|
|
|
|
modes.ExceptMask: NewUserMaskSet(),
|
|
|
|
modes.InviteMask: NewUserMaskSet(),
|
2014-02-23 00:01:11 +01:00
|
|
|
},
|
2016-10-11 15:51:46 +02:00
|
|
|
members: make(MemberSet),
|
|
|
|
name: name,
|
|
|
|
nameCasefolded: casefoldedName,
|
|
|
|
server: s,
|
2018-04-04 03:49:40 +02:00
|
|
|
accountToUMode: make(map[string]modes.Mode),
|
2012-12-09 21:51:50 +01:00
|
|
|
}
|
2014-02-25 20:11:34 +01:00
|
|
|
|
2018-11-26 11:23:27 +01:00
|
|
|
config := s.Config()
|
|
|
|
|
2018-04-04 03:49:40 +02:00
|
|
|
if regInfo != nil {
|
|
|
|
channel.applyRegInfo(regInfo)
|
|
|
|
} else {
|
2018-11-26 11:23:27 +01:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-26 11:23:27 +01:00
|
|
|
channel.history.Initialize(config.History.ChannelLength)
|
|
|
|
|
2012-12-15 23:34:20 +01:00
|
|
|
return channel
|
2012-12-09 07:54:58 +01:00
|
|
|
}
|
|
|
|
|
2017-11-09 04:19:50 +01:00
|
|
|
// read in channel state that was persisted in the DB
|
|
|
|
func (channel *Channel) applyRegInfo(chanReg *RegisteredChannel) {
|
|
|
|
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
|
2018-04-04 03:49:40 +02:00
|
|
|
channel.key = chanReg.Key
|
|
|
|
|
|
|
|
for _, mode := range chanReg.Modes {
|
2018-04-23 00:47:10 +02:00
|
|
|
channel.flags.SetMode(mode, true)
|
2018-04-04 03:49:40 +02:00
|
|
|
}
|
2017-11-09 04:19:50 +01:00
|
|
|
for _, mask := range chanReg.Banlist {
|
2018-02-03 11:21:32 +01:00
|
|
|
channel.lists[modes.BanMask].Add(mask)
|
2017-11-09 04:19:50 +01:00
|
|
|
}
|
|
|
|
for _, mask := range chanReg.Exceptlist {
|
2018-02-03 11:21:32 +01:00
|
|
|
channel.lists[modes.ExceptMask].Add(mask)
|
2017-11-09 04:19:50 +01:00
|
|
|
}
|
|
|
|
for _, mask := range chanReg.Invitelist {
|
2018-02-03 11:21:32 +01:00
|
|
|
channel.lists[modes.InviteMask].Add(mask)
|
2017-11-09 04:19:50 +01:00
|
|
|
}
|
2018-04-04 03:49:40 +02:00
|
|
|
for account, mode := range chanReg.AccountToUMode {
|
|
|
|
channel.accountToUMode[account] = mode
|
|
|
|
}
|
2017-11-09 04:19:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// obtain a consistent snapshot of the channel state that can be persisted to the DB
|
2018-04-04 03:49:40 +02:00
|
|
|
func (channel *Channel) ExportRegistration(includeFlags uint) (info RegisteredChannel) {
|
2017-11-09 04:19:50 +01:00
|
|
|
channel.stateMutex.RLock()
|
|
|
|
defer channel.stateMutex.RUnlock()
|
|
|
|
|
|
|
|
info.Name = channel.name
|
|
|
|
info.Founder = channel.registeredFounder
|
|
|
|
info.RegisteredAt = channel.registeredTime
|
|
|
|
|
2018-04-04 03:49:40 +02:00
|
|
|
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()
|
2018-04-04 03:49:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if includeFlags&IncludeLists != 0 {
|
2018-02-03 11:21:32 +01:00
|
|
|
for mask := range channel.lists[modes.BanMask].masks {
|
2017-11-09 04:19:50 +01:00
|
|
|
info.Banlist = append(info.Banlist, mask)
|
|
|
|
}
|
2018-02-03 11:21:32 +01:00
|
|
|
for mask := range channel.lists[modes.ExceptMask].masks {
|
2017-11-09 04:19:50 +01:00
|
|
|
info.Exceptlist = append(info.Exceptlist, mask)
|
|
|
|
}
|
2018-02-03 11:21:32 +01:00
|
|
|
for mask := range channel.lists[modes.InviteMask].masks {
|
2017-11-09 04:19:50 +01:00
|
|
|
info.Invitelist = append(info.Invitelist, mask)
|
|
|
|
}
|
2018-04-04 03:49:40 +02:00
|
|
|
info.AccountToUMode = make(map[string]modes.Mode)
|
|
|
|
for account, mode := range channel.accountToUMode {
|
|
|
|
info.AccountToUMode[account] = mode
|
|
|
|
}
|
2017-11-09 04:19:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
2017-11-09 04:19:50 +01:00
|
|
|
}
|
|
|
|
channel.registeredFounder = founder
|
|
|
|
channel.registeredTime = time.Now()
|
2018-04-04 03:49:40 +02:00
|
|
|
channel.accountToUMode[founder] = modes.ChannelFounder
|
2017-11-09 04:19:50 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-06-04 11:02:22 +02:00
|
|
|
// SetUnregistered deletes the channel's registration information.
|
|
|
|
func (channel *Channel) SetUnregistered() {
|
|
|
|
channel.stateMutex.Lock()
|
|
|
|
defer channel.stateMutex.Unlock()
|
|
|
|
|
|
|
|
channel.registeredFounder = ""
|
|
|
|
var zeroTime time.Time
|
|
|
|
channel.registeredTime = zeroTime
|
|
|
|
channel.accountToUMode = make(map[string]modes.Mode)
|
|
|
|
}
|
|
|
|
|
2017-11-09 04:19:50 +01:00
|
|
|
// IsRegistered returns whether the channel is registered.
|
|
|
|
func (channel *Channel) IsRegistered() bool {
|
|
|
|
channel.stateMutex.RLock()
|
|
|
|
defer channel.stateMutex.RUnlock()
|
|
|
|
return channel.registeredFounder != ""
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2017-03-27 06:29:51 +02: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) {
|
2018-04-24 11:46:01 +02:00
|
|
|
isMultiPrefix := client.capabilities.Has(caps.MultiPrefix)
|
|
|
|
isUserhostInNames := client.capabilities.Has(caps.UserhostInNames)
|
|
|
|
|
|
|
|
maxNamLen := 480 - len(client.server.name) - len(client.Nick())
|
|
|
|
var namesLines []string
|
|
|
|
var buffer bytes.Buffer
|
|
|
|
for _, target := range channel.Members() {
|
|
|
|
var nick string
|
|
|
|
if isUserhostInNames {
|
|
|
|
nick = target.NickMaskString()
|
|
|
|
} else {
|
|
|
|
nick = target.Nick()
|
2016-06-19 06:55:24 +02:00
|
|
|
}
|
2018-04-24 11:46:01 +02:00
|
|
|
channel.stateMutex.RLock()
|
|
|
|
modes := channel.members[target]
|
|
|
|
channel.stateMutex.RUnlock()
|
|
|
|
if modes == nil {
|
2016-06-19 06:55:24 +02:00
|
|
|
continue
|
|
|
|
}
|
2018-04-24 11:46:01 +02:00
|
|
|
prefix := modes.Prefixes(isMultiPrefix)
|
|
|
|
if buffer.Len()+len(nick)+len(prefix)+1 > maxNamLen {
|
|
|
|
namesLines = append(namesLines, buffer.String())
|
2018-11-26 11:23:27 +01:00
|
|
|
buffer.Reset()
|
2018-04-24 11:46:01 +02:00
|
|
|
}
|
|
|
|
if buffer.Len() > 0 {
|
|
|
|
buffer.WriteString(" ")
|
|
|
|
}
|
|
|
|
buffer.WriteString(prefix)
|
|
|
|
buffer.WriteString(nick)
|
|
|
|
}
|
|
|
|
if buffer.Len() > 0 {
|
|
|
|
namesLines = append(namesLines, buffer.String())
|
2016-06-19 06:55:24 +02:00
|
|
|
}
|
|
|
|
|
2018-04-24 11:46:01 +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
|
|
|
}
|
|
|
|
|
2016-10-22 16:45:51 +02:00
|
|
|
// ClientIsAtLeast returns whether the client has at least the given channel privilege.
|
2018-02-03 11:21:32 +01:00
|
|
|
func (channel *Channel) ClientIsAtLeast(client *Client, permission modes.Mode) bool {
|
2017-10-23 01:50:16 +02:00
|
|
|
channel.stateMutex.RLock()
|
2018-04-23 00:47:10 +02:00
|
|
|
clientModes := channel.members[client]
|
2018-08-17 18:44:49 +02:00
|
|
|
channel.stateMutex.RUnlock()
|
|
|
|
|
|
|
|
if clientModes == nil {
|
|
|
|
return false
|
|
|
|
}
|
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
|
2014-02-10 04:59:59 +01:00
|
|
|
}
|
|
|
|
|
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()
|
|
|
|
defer channel.stateMutex.RUnlock()
|
|
|
|
|
|
|
|
clientModes := channel.members[client]
|
|
|
|
targetModes := channel.members[target]
|
|
|
|
result := false
|
2018-02-03 11:21:32 +01:00
|
|
|
for _, mode := range modes.ChannelPrivModes {
|
2018-04-23 00:47:10 +02:00
|
|
|
if clientModes.HasMode(mode) {
|
2017-10-23 01:50:16 +02:00
|
|
|
result = true
|
|
|
|
// admins cannot kick other admins
|
2018-04-23 00:47:10 +02:00
|
|
|
if mode == modes.ChannelAdmin && targetModes.HasMode(modes.ChannelAdmin) {
|
2017-10-23 01:50:16 +02:00
|
|
|
result = false
|
|
|
|
}
|
|
|
|
break
|
2018-04-23 00:47:10 +02:00
|
|
|
} else if targetModes.HasMode(mode) {
|
2017-10-23 01:50:16 +02:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
|
|
|
func (channel *Channel) hasClient(client *Client) bool {
|
|
|
|
channel.stateMutex.RLock()
|
|
|
|
defer channel.stateMutex.RUnlock()
|
|
|
|
_, present := channel.members[client]
|
|
|
|
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) {
|
2018-02-03 11:21:32 +01:00
|
|
|
isMember := client.HasMode(modes.Operator) || channel.hasClient(client)
|
2014-02-22 21:49:33 +01:00
|
|
|
showKey := isMember && (channel.key != "")
|
|
|
|
showUserLimit := channel.userLimit > 0
|
|
|
|
|
2018-02-03 11:21:32 +01:00
|
|
|
mods := "+"
|
2017-10-23 01:50:16 +02:00
|
|
|
|
2014-02-22 21:49:33 +01:00
|
|
|
// flags with args
|
|
|
|
if showKey {
|
2018-02-03 11:21:32 +01:00
|
|
|
mods += modes.Key.String()
|
2014-02-09 08:33:56 +01:00
|
|
|
}
|
2014-02-22 21:49:33 +01:00
|
|
|
if showUserLimit {
|
2018-02-03 11:21:32 +01:00
|
|
|
mods += modes.UserLimit.String()
|
2014-02-22 21:49:33 +01:00
|
|
|
}
|
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()
|
|
|
|
|
2018-02-03 11:21:32 +01:00
|
|
|
result = []string{mods}
|
2014-02-15 06:57:08 +01:00
|
|
|
|
2014-02-22 21:49:33 +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
|
|
|
}
|
2014-02-22 21:49:33 +01:00
|
|
|
if showUserLimit {
|
2018-12-28 19:45:55 +01:00
|
|
|
result = append(result, strconv.Itoa(channel.userLimit))
|
2014-02-22 21:49:33 +01:00
|
|
|
}
|
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
|
|
|
|
}
|
|
|
|
|
2017-03-27 06:29:51 +02:00
|
|
|
// Join joins the given client to this channel (if they can be joined).
|
2018-05-25 08:46:36 +02:00
|
|
|
func (channel *Channel) Join(client *Client, key string, isSajoin bool, rb *ResponseBuffer) {
|
2019-01-01 19:15:38 +01:00
|
|
|
details := client.Details()
|
2018-12-31 00:28:56 +01:00
|
|
|
|
2018-05-25 08:46:36 +02:00
|
|
|
channel.stateMutex.RLock()
|
|
|
|
chname := channel.name
|
2018-12-23 19:25:02 +01:00
|
|
|
chcfname := channel.nameCasefolded
|
2018-05-25 08:46:36 +02:00
|
|
|
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]
|
2018-05-25 08:46:36 +02:00
|
|
|
channel.stateMutex.RUnlock()
|
2018-12-28 19:45:55 +01:00
|
|
|
|
|
|
|
if alreadyJoined {
|
|
|
|
// no message needs to be sent
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-12-31 00:28:56 +01:00
|
|
|
// 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 {
|
2018-04-24 09:11:11 +02:00
|
|
|
rb.Add(nil, client.server.name, ERR_CHANNELISFULL, chname, fmt.Sprintf(client.t("Cannot join channel (+%s)"), "l"))
|
2014-02-22 21:49:33 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-12-28 19:45:55 +01:00
|
|
|
if !hasPrivs && chkey != "" && !utils.SecretTokensMatch(chkey, key) {
|
2018-04-24 09:11:11 +02:00
|
|
|
rb.Add(nil, client.server.name, ERR_BADCHANNELKEY, 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)
|
2018-05-25 08:46:36 +02:00
|
|
|
if !hasPrivs && channel.flags.HasMode(modes.InviteOnly) && !isInvited {
|
2018-04-24 09:11:11 +02:00
|
|
|
rb.Add(nil, client.server.name, ERR_INVITEONLYCHAN, 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) {
|
2018-04-24 09:11:11 +02:00
|
|
|
rb.Add(nil, client.server.name, ERR_BANNEDFROMCHAN, chname, fmt.Sprintf(client.t("Cannot join channel (+%s)"), "b"))
|
2014-03-08 02:09:49 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-01-01 19:15:38 +01:00
|
|
|
client.server.logger.Debug("join", fmt.Sprintf("%s joined channel %s", details.nick, chname))
|
2017-03-06 13:11:10 +01:00
|
|
|
|
2018-04-24 09:11:11 +02:00
|
|
|
newChannel, givenMode := func() (newChannel bool, givenMode modes.Mode) {
|
|
|
|
channel.joinPartMutex.Lock()
|
|
|
|
defer channel.joinPartMutex.Unlock()
|
|
|
|
|
|
|
|
func() {
|
|
|
|
channel.stateMutex.Lock()
|
|
|
|
defer channel.stateMutex.Unlock()
|
|
|
|
|
|
|
|
channel.members.Add(client)
|
|
|
|
firstJoin := len(channel.members) == 1
|
|
|
|
newChannel = firstJoin && channel.registeredFounder == ""
|
|
|
|
if newChannel {
|
|
|
|
givenMode = modes.ChannelOperator
|
2018-02-05 15:21:08 +01:00
|
|
|
} else {
|
2018-12-31 00:28:56 +01:00
|
|
|
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
|
|
|
|
|
|
|
channel.history.Add(history.Item{
|
|
|
|
Type: history.Join,
|
|
|
|
Nick: details.nickMask,
|
|
|
|
AccountName: details.accountName,
|
|
|
|
Msgid: details.realname,
|
|
|
|
})
|
|
|
|
|
2018-04-24 09:11:11 +02:00
|
|
|
return
|
|
|
|
}()
|
2017-10-23 01:50:16 +02:00
|
|
|
|
|
|
|
client.addChannel(channel)
|
2017-03-27 06:52:34 +02:00
|
|
|
|
2018-04-24 09:11:11 +02:00
|
|
|
var modestr string
|
|
|
|
if givenMode != 0 {
|
|
|
|
modestr = fmt.Sprintf("+%v", givenMode)
|
2017-11-09 04:19:50 +01:00
|
|
|
}
|
2018-04-24 09:11:11 +02:00
|
|
|
|
|
|
|
for _, member := range channel.Members() {
|
|
|
|
if member == client {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if member.capabilities.Has(caps.ExtendedJoin) {
|
2019-01-01 19:15:38 +01:00
|
|
|
member.Send(nil, details.nickMask, "JOIN", chname, details.accountName, details.realname)
|
2018-04-24 09:11:11 +02:00
|
|
|
} else {
|
2019-01-01 19:15:38 +01:00
|
|
|
member.Send(nil, details.nickMask, "JOIN", chname)
|
2018-04-24 09:11:11 +02:00
|
|
|
}
|
|
|
|
if givenMode != 0 {
|
2019-01-01 19:15:38 +01:00
|
|
|
member.Send(nil, client.server.name, "MODE", chname, modestr, details.nick)
|
2018-04-24 09:11:11 +02:00
|
|
|
}
|
2017-11-09 04:19:50 +01:00
|
|
|
}
|
2014-02-17 02:23:47 +01:00
|
|
|
|
2017-09-29 09:25:58 +02:00
|
|
|
if client.capabilities.Has(caps.ExtendedJoin) {
|
2019-01-01 19:15:38 +01:00
|
|
|
rb.Add(nil, details.nickMask, "JOIN", chname, details.accountName, details.realname)
|
2016-08-14 03:59:33 +02:00
|
|
|
} else {
|
2019-01-01 19:15:38 +01:00
|
|
|
rb.Add(nil, details.nickMask, "JOIN", chname)
|
2016-08-14 03:59:33 +02:00
|
|
|
}
|
2018-04-24 09:11:11 +02:00
|
|
|
|
2017-11-09 04:19:50 +01:00
|
|
|
// don't send topic when it's an entirely new channel
|
2017-09-26 00:01:10 +02:00
|
|
|
if !newChannel {
|
2018-02-05 15:21:08 +01:00
|
|
|
channel.SendTopic(client, rb)
|
2017-09-26 00:01:10 +02:00
|
|
|
}
|
2018-04-24 09:11:11 +02:00
|
|
|
|
2018-02-05 15:21:08 +01:00
|
|
|
channel.Names(client, rb)
|
2018-04-24 09:11:11 +02:00
|
|
|
|
|
|
|
if givenMode != 0 {
|
2019-01-01 19:15:38 +01:00
|
|
|
rb.Add(nil, client.server.name, "MODE", chname, modestr, details.nick)
|
2017-03-24 03:38:07 +01:00
|
|
|
}
|
2018-11-26 11:23:27 +01: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)
|
|
|
|
|
|
|
|
replayLimit := channel.server.Config().History.AutoreplayOnJoin
|
|
|
|
if replayLimit > 0 {
|
2019-01-02 23:52:36 +01:00
|
|
|
items := channel.history.Latest(replayLimit)
|
2018-12-28 19:45:55 +01:00
|
|
|
channel.replayHistoryItems(rb, items)
|
|
|
|
rb.Flush(true)
|
|
|
|
}
|
2014-02-17 02:23:47 +01:00
|
|
|
}
|
|
|
|
|
2017-03-27 06:29:51 +02:00
|
|
|
// 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) {
|
2018-04-24 09:11:11 +02:00
|
|
|
rb.Add(nil, client.server.name, ERR_NOTONCHANNEL, chname, client.t("You're not on that channel"))
|
2012-12-09 07:54:58 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-04-24 09:11:11 +02:00
|
|
|
channel.Quit(client)
|
|
|
|
|
2019-01-01 19:15:38 +01:00
|
|
|
details := client.Details()
|
2017-10-23 01:50:16 +02:00
|
|
|
for _, member := range channel.Members() {
|
2019-01-01 19:15:38 +01:00
|
|
|
member.Send(nil, details.nickMask, "PART", chname, message)
|
2014-02-20 07:20:34 +01:00
|
|
|
}
|
2019-01-01 19:15:38 +01:00
|
|
|
rb.Add(nil, details.nickMask, "PART", chname, message)
|
2017-03-06 13:11:10 +01:00
|
|
|
|
2018-11-26 11:23:27 +01:00
|
|
|
channel.history.Add(history.Item{
|
|
|
|
Type: history.Part,
|
2019-01-01 19:15:38 +01:00
|
|
|
Nick: details.nickMask,
|
|
|
|
AccountName: details.accountName,
|
2018-11-26 11:23:27 +01:00
|
|
|
Message: utils.MakeSplitMessage(message, true),
|
|
|
|
})
|
|
|
|
|
2019-01-01 19:15:38 +01:00
|
|
|
client.server.logger.Debug("part", fmt.Sprintf("%s left channel %s", details.nick, chname))
|
2012-12-09 07:54:58 +01:00
|
|
|
}
|
|
|
|
|
2018-11-26 11:23:27 +01:00
|
|
|
// 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
|
|
|
|
func (channel *Channel) Resume(newClient, oldClient *Client, timestamp time.Time) {
|
|
|
|
now := time.Now()
|
|
|
|
channel.resumeAndAnnounce(newClient, oldClient)
|
|
|
|
if !timestamp.IsZero() {
|
2018-12-28 19:45:55 +01:00
|
|
|
channel.replayHistoryForResume(newClient, timestamp, now)
|
2018-11-26 11:23:27 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (channel *Channel) resumeAndAnnounce(newClient, oldClient *Client) {
|
|
|
|
var oldModeSet *modes.ModeSet
|
|
|
|
|
|
|
|
func() {
|
|
|
|
channel.joinPartMutex.Lock()
|
|
|
|
defer channel.joinPartMutex.Unlock()
|
|
|
|
|
|
|
|
defer channel.regenerateMembersCache()
|
|
|
|
|
|
|
|
channel.stateMutex.Lock()
|
|
|
|
defer channel.stateMutex.Unlock()
|
|
|
|
|
|
|
|
newClient.channels[channel] = true
|
|
|
|
oldModeSet = channel.members[oldClient]
|
|
|
|
if oldModeSet == nil {
|
|
|
|
oldModeSet = modes.NewModeSet()
|
|
|
|
}
|
|
|
|
channel.members.Remove(oldClient)
|
|
|
|
channel.members[newClient] = oldModeSet
|
|
|
|
}()
|
|
|
|
|
|
|
|
// construct fake modestring if necessary
|
|
|
|
oldModes := oldModeSet.String()
|
|
|
|
if 0 < len(oldModes) {
|
|
|
|
oldModes = "+" + oldModes
|
|
|
|
}
|
|
|
|
|
|
|
|
// send join for old clients
|
|
|
|
nick := newClient.Nick()
|
|
|
|
nickMask := newClient.NickMaskString()
|
|
|
|
accountName := newClient.AccountName()
|
|
|
|
realName := newClient.Realname()
|
|
|
|
for _, member := range channel.Members() {
|
|
|
|
if member.capabilities.Has(caps.Resume) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if member.capabilities.Has(caps.ExtendedJoin) {
|
|
|
|
member.Send(nil, nickMask, "JOIN", channel.name, accountName, realName)
|
|
|
|
} else {
|
|
|
|
member.Send(nil, nickMask, "JOIN", channel.name)
|
|
|
|
}
|
|
|
|
|
|
|
|
if 0 < len(oldModes) {
|
|
|
|
member.Send(nil, channel.server.name, "MODE", channel.name, oldModes, nick)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
rb := NewResponseBuffer(newClient)
|
|
|
|
// use blocking i/o to synchronize with the later history replay
|
|
|
|
if newClient.capabilities.Has(caps.ExtendedJoin) {
|
|
|
|
rb.Add(nil, nickMask, "JOIN", channel.name, accountName, realName)
|
|
|
|
} else {
|
|
|
|
rb.Add(nil, nickMask, "JOIN", channel.name)
|
|
|
|
}
|
|
|
|
channel.SendTopic(newClient, rb)
|
|
|
|
channel.Names(newClient, rb)
|
|
|
|
if 0 < len(oldModes) {
|
|
|
|
rb.Add(nil, newClient.server.name, "MODE", channel.name, oldModes, nick)
|
|
|
|
}
|
2018-12-28 19:45:55 +01:00
|
|
|
rb.Send(true)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (channel *Channel) replayHistoryForResume(newClient *Client, after time.Time, before time.Time) {
|
|
|
|
items, complete := channel.history.Between(after, before)
|
|
|
|
rb := NewResponseBuffer(newClient)
|
|
|
|
channel.replayHistoryItems(rb, items)
|
|
|
|
if !complete && !newClient.resumeDetails.HistoryIncomplete {
|
|
|
|
// warn here if we didn't warn already
|
|
|
|
rb.Add(nil, "HistServ", "NOTICE", channel.Name(), newClient.t("Some additional message history may have been lost"))
|
|
|
|
}
|
|
|
|
rb.Send(true)
|
2018-11-26 11:23:27 +01:00
|
|
|
}
|
|
|
|
|
2019-01-02 23:52:36 +01:00
|
|
|
func stripMaskFromNick(nickMask string) (nick string) {
|
|
|
|
index := strings.Index(nickMask, "!")
|
|
|
|
if index == -1 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
return nickMask[0:index]
|
|
|
|
}
|
|
|
|
|
2018-12-28 19:45:55 +01:00
|
|
|
func (channel *Channel) replayHistoryItems(rb *ResponseBuffer, items []history.Item) {
|
2018-11-26 11:23:27 +01:00
|
|
|
chname := channel.Name()
|
2018-12-28 19:45:55 +01:00
|
|
|
client := rb.target
|
|
|
|
serverTime := client.capabilities.Has(caps.ServerTime)
|
2018-11-26 11:23:27 +01:00
|
|
|
|
|
|
|
for _, item := range items {
|
2018-12-28 19:45:55 +01:00
|
|
|
var tags Tags
|
|
|
|
if serverTime {
|
|
|
|
tags = ensureTag(tags, "time", item.Time.Format(IRCv3TimestampFormat))
|
|
|
|
}
|
|
|
|
|
2018-11-26 11:23:27 +01:00
|
|
|
switch item.Type {
|
|
|
|
case history.Privmsg:
|
2018-12-28 19:45:55 +01:00
|
|
|
rb.AddSplitMessageFromClient(item.Msgid, item.Nick, item.AccountName, tags, "PRIVMSG", chname, item.Message)
|
2018-11-26 11:23:27 +01:00
|
|
|
case history.Notice:
|
2018-12-28 19:45:55 +01:00
|
|
|
rb.AddSplitMessageFromClient(item.Msgid, item.Nick, item.AccountName, tags, "NOTICE", chname, item.Message)
|
2018-11-26 11:23:27 +01:00
|
|
|
case history.Join:
|
2019-01-02 23:52:36 +01:00
|
|
|
nick := stripMaskFromNick(item.Nick)
|
|
|
|
var message string
|
|
|
|
if item.AccountName == "*" {
|
|
|
|
message = fmt.Sprintf(client.t("%s joined the channel"), nick)
|
2018-11-26 11:23:27 +01:00
|
|
|
} else {
|
2019-01-02 23:52:36 +01:00
|
|
|
message = fmt.Sprintf(client.t("%s [account: %s] joined the channel"), nick, item.AccountName)
|
2018-11-26 11:23:27 +01:00
|
|
|
}
|
2019-01-02 23:52:36 +01:00
|
|
|
rb.Add(tags, "HistServ", "PRIVMSG", chname, message)
|
2018-11-26 11:23:27 +01:00
|
|
|
case history.Part:
|
2019-01-02 23:52:36 +01:00
|
|
|
nick := stripMaskFromNick(item.Nick)
|
|
|
|
message := fmt.Sprintf(client.t("%s left the channel (%s)"), nick, item.Message.Original)
|
|
|
|
rb.Add(tags, "HistServ", "PRIVMSG", chname, message)
|
|
|
|
case history.Quit:
|
|
|
|
nick := stripMaskFromNick(item.Nick)
|
|
|
|
message := fmt.Sprintf(client.t("%s quit (%s)"), nick, item.Message.Original)
|
|
|
|
rb.Add(tags, "HistServ", "PRIVMSG", chname, message)
|
2018-11-26 11:23:27 +01:00
|
|
|
case history.Kick:
|
2019-01-02 23:52:36 +01:00
|
|
|
nick := stripMaskFromNick(item.Nick)
|
2018-12-28 19:45:55 +01:00
|
|
|
// XXX Msgid is the kick target
|
2019-01-02 23:52:36 +01:00
|
|
|
message := fmt.Sprintf(client.t("%s kicked %s (%s)"), nick, item.Msgid, item.Message.Original)
|
|
|
|
rb.Add(tags, "HistServ", "PRIVMSG", chname, message)
|
2018-11-26 11:23:27 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-23 01:50:16 +02:00
|
|
|
// SendTopic sends the channel topic to the given client.
|
2018-02-05 15:21:08 +01:00
|
|
|
func (channel *Channel) SendTopic(client *Client, rb *ResponseBuffer) {
|
2017-10-23 01:50:16 +02:00
|
|
|
if !channel.hasClient(client) {
|
2018-02-05 15:21:08 +01:00
|
|
|
rb.Add(nil, client.server.name, ERR_NOTONCHANNEL, client.nick, channel.name, client.t("You're not on that channel"))
|
2012-12-09 07:54:58 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-10-23 01:50:16 +02:00
|
|
|
channel.stateMutex.RLock()
|
|
|
|
name := channel.name
|
|
|
|
topic := channel.topic
|
|
|
|
topicSetBy := channel.topicSetBy
|
|
|
|
topicSetTime := channel.topicSetTime
|
|
|
|
channel.stateMutex.RUnlock()
|
|
|
|
|
|
|
|
if topic == "" {
|
2018-02-05 15:21:08 +01:00
|
|
|
rb.Add(nil, client.server.name, RPL_NOTOPIC, client.nick, name, client.t("No topic is set"))
|
2012-12-09 07:54:58 +01:00
|
|
|
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))
|
2014-02-17 07:20:42 +01:00
|
|
|
}
|
|
|
|
|
2017-03-27 06:29:51 +02:00
|
|
|
// 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)) {
|
2018-02-05 15:21:08 +01:00
|
|
|
rb.Add(nil, client.server.name, ERR_NOTONCHANNEL, channel.name, client.t("You're not on that channel"))
|
2014-02-17 07:20:42 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-04-23 00:47:10 +02:00
|
|
|
if channel.flags.HasMode(modes.OpOnlyTopic) && !channel.ClientIsAtLeast(client, modes.ChannelOperator) {
|
2018-02-05 15:21:08 +01:00
|
|
|
rb.Add(nil, client.server.name, ERR_CHANOPRIVSNEEDED, channel.name, client.t("You're not a channel operator"))
|
2014-02-16 04:56:38 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-07-16 09:46:40 +02:00
|
|
|
topicLimit := client.server.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()
|
2014-02-17 07:20:42 +01:00
|
|
|
channel.topic = topic
|
2017-03-24 04:44:54 +01:00
|
|
|
channel.topicSetBy = client.nickMaskString
|
2016-06-26 13:06:28 +02:00
|
|
|
channel.topicSetTime = time.Now()
|
2017-10-23 01:50:16 +02:00
|
|
|
channel.stateMutex.Unlock()
|
2014-02-21 05:47:05 +01:00
|
|
|
|
2017-10-23 01:50:16 +02:00
|
|
|
for _, member := range channel.Members() {
|
2018-02-05 15:21:08 +01:00
|
|
|
if member == client {
|
|
|
|
rb.Add(nil, client.nickMaskString, "TOPIC", channel.name, topic)
|
|
|
|
} else {
|
|
|
|
member.Send(nil, client.nickMaskString, "TOPIC", channel.name, topic)
|
|
|
|
}
|
2014-02-20 07:20:34 +01:00
|
|
|
}
|
2017-03-24 04:44:54 +01:00
|
|
|
|
2018-04-04 03:49:40 +02:00
|
|
|
go channel.server.channelRegistry.StoreChannel(channel, IncludeTopic)
|
2012-12-15 23:34:20 +01:00
|
|
|
}
|
|
|
|
|
2017-03-27 06:29:51 +02:00
|
|
|
// CanSpeak returns true if the client can speak on this channel.
|
2014-02-22 21:49:33 +01:00
|
|
|
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 {
|
2014-02-22 21:49:33 +01:00
|
|
|
return false
|
|
|
|
}
|
2018-04-23 00:47:10 +02:00
|
|
|
if channel.flags.HasMode(modes.Moderated) && !channel.ClientIsAtLeast(client, modes.Voice) {
|
2014-02-22 21:49:33 +01:00
|
|
|
return false
|
|
|
|
}
|
2018-04-23 00:47:10 +02:00
|
|
|
if channel.flags.HasMode(modes.RegisteredOnly) && client.Account() == "" {
|
2017-03-28 09:32:03 +02:00
|
|
|
return false
|
|
|
|
}
|
2014-02-22 21:49:33 +01:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2017-01-14 06:52:32 +01:00
|
|
|
// TagMsg sends a tag message to everyone in this channel who can accept them.
|
2018-02-05 15:21:08 +01:00
|
|
|
func (channel *Channel) TagMsg(msgid string, minPrefix *modes.Mode, clientOnlyTags *map[string]ircmsg.TagValue, client *Client, rb *ResponseBuffer) {
|
|
|
|
channel.sendMessage(msgid, "TAGMSG", []caps.Capability{caps.MessageTags}, minPrefix, clientOnlyTags, client, nil, rb)
|
2017-01-14 06:52:32 +01:00
|
|
|
}
|
|
|
|
|
2017-03-27 06:29:51 +02:00
|
|
|
// sendMessage sends a given message to everyone on this channel.
|
2018-02-05 15:21:08 +01:00
|
|
|
func (channel *Channel) sendMessage(msgid, cmd string, requiredCaps []caps.Capability, minPrefix *modes.Mode, clientOnlyTags *map[string]ircmsg.TagValue, client *Client, message *string, rb *ResponseBuffer) {
|
2014-02-22 21:49:33 +01:00
|
|
|
if !channel.CanSpeak(client) {
|
2018-02-05 15:21:08 +01:00
|
|
|
rb.Add(nil, client.server.name, ERR_CANNOTSENDTOCHAN, channel.name, client.t("Cannot send to channel"))
|
2014-02-09 08:33:56 +01:00
|
|
|
return
|
|
|
|
}
|
2017-01-10 17:09:08 +01:00
|
|
|
|
2016-10-22 16:45:51 +02:00
|
|
|
// for STATUSMSG
|
2018-02-03 11:21:32 +01:00
|
|
|
var minPrefixMode modes.Mode
|
2016-10-22 16:45:51 +02:00
|
|
|
if minPrefix != nil {
|
|
|
|
minPrefixMode = *minPrefix
|
|
|
|
}
|
2018-02-10 23:57:15 +01:00
|
|
|
// send echo-message
|
|
|
|
if client.capabilities.Has(caps.EchoMessage) {
|
|
|
|
var messageTagsToUse *map[string]ircmsg.TagValue
|
|
|
|
if client.capabilities.Has(caps.MessageTags) {
|
|
|
|
messageTagsToUse = clientOnlyTags
|
|
|
|
}
|
|
|
|
|
2018-12-28 19:45:55 +01:00
|
|
|
nickMaskString := client.NickMaskString()
|
|
|
|
accountName := client.AccountName()
|
2018-02-10 23:57:15 +01:00
|
|
|
if message == nil {
|
2018-12-28 19:45:55 +01:00
|
|
|
rb.AddFromClient(msgid, nickMaskString, accountName, messageTagsToUse, cmd, channel.name)
|
2018-02-10 23:57:15 +01:00
|
|
|
} else {
|
2018-12-28 19:45:55 +01:00
|
|
|
rb.AddFromClient(msgid, nickMaskString, accountName, messageTagsToUse, cmd, channel.name, *message)
|
2018-02-10 23:57:15 +01:00
|
|
|
}
|
|
|
|
}
|
2017-10-23 01:50:16 +02:00
|
|
|
for _, member := range channel.Members() {
|
2016-10-22 16:45:51 +02:00
|
|
|
if minPrefix != nil && !channel.ClientIsAtLeast(member, minPrefixMode) {
|
|
|
|
// STATUSMSG
|
|
|
|
continue
|
|
|
|
}
|
2018-02-10 23:57:15 +01:00
|
|
|
// echo-message is handled above, so skip sending the msg to the user themselves as well
|
|
|
|
if member == client {
|
2014-02-20 07:20:34 +01:00
|
|
|
continue
|
|
|
|
}
|
2017-01-14 06:52:32 +01:00
|
|
|
|
|
|
|
canReceive := true
|
|
|
|
for _, capName := range requiredCaps {
|
2017-09-29 09:25:58 +02:00
|
|
|
if !member.capabilities.Has(capName) {
|
2017-01-14 06:52:32 +01:00
|
|
|
canReceive = false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !canReceive {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2017-01-14 10:52:47 +01:00
|
|
|
var messageTagsToUse *map[string]ircmsg.TagValue
|
2017-09-29 09:25:58 +02:00
|
|
|
if member.capabilities.Has(caps.MessageTags) {
|
2017-01-14 10:52:47 +01:00
|
|
|
messageTagsToUse = clientOnlyTags
|
|
|
|
}
|
|
|
|
|
|
|
|
if message == nil {
|
2018-02-10 23:57:15 +01:00
|
|
|
member.SendFromClient(msgid, client, messageTagsToUse, cmd, channel.name)
|
2016-10-16 04:54:15 +02:00
|
|
|
} else {
|
2018-02-10 23:57:15 +01:00
|
|
|
member.SendFromClient(msgid, client, messageTagsToUse, cmd, channel.name, *message)
|
2016-10-16 04:54:15 +02:00
|
|
|
}
|
2014-02-20 07:20:34 +01:00
|
|
|
}
|
2012-12-09 07:54:58 +01:00
|
|
|
}
|
2014-02-09 07:06:10 +01:00
|
|
|
|
2017-01-14 06:28:50 +01:00
|
|
|
// SplitPrivMsg sends a private message to everyone in this channel.
|
2018-11-26 11:23:27 +01:00
|
|
|
func (channel *Channel) SplitPrivMsg(msgid string, minPrefix *modes.Mode, clientOnlyTags *map[string]ircmsg.TagValue, client *Client, message utils.SplitMessage, rb *ResponseBuffer) {
|
|
|
|
channel.sendSplitMessage(msgid, "PRIVMSG", history.Privmsg, minPrefix, clientOnlyTags, client, &message, rb)
|
2017-01-14 06:28:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// SplitNotice sends a private message to everyone in this channel.
|
2018-11-26 11:23:27 +01:00
|
|
|
func (channel *Channel) SplitNotice(msgid string, minPrefix *modes.Mode, clientOnlyTags *map[string]ircmsg.TagValue, client *Client, message utils.SplitMessage, rb *ResponseBuffer) {
|
|
|
|
channel.sendSplitMessage(msgid, "NOTICE", history.Notice, minPrefix, clientOnlyTags, client, &message, rb)
|
2017-01-14 06:28:50 +01:00
|
|
|
}
|
|
|
|
|
2018-11-26 11:23:27 +01:00
|
|
|
func (channel *Channel) sendSplitMessage(msgid, cmd string, histType history.ItemType, minPrefix *modes.Mode, clientOnlyTags *map[string]ircmsg.TagValue, client *Client, message *utils.SplitMessage, rb *ResponseBuffer) {
|
2017-01-14 06:28:50 +01:00
|
|
|
if !channel.CanSpeak(client) {
|
2018-02-05 15:21:08 +01:00
|
|
|
rb.Add(nil, client.server.name, ERR_CANNOTSENDTOCHAN, channel.name, client.t("Cannot send to channel"))
|
2017-01-14 06:28:50 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// for STATUSMSG
|
2018-02-03 11:21:32 +01:00
|
|
|
var minPrefixMode modes.Mode
|
2017-01-14 06:28:50 +01:00
|
|
|
if minPrefix != nil {
|
|
|
|
minPrefixMode = *minPrefix
|
|
|
|
}
|
2018-02-10 23:57:15 +01:00
|
|
|
// send echo-message
|
|
|
|
if client.capabilities.Has(caps.EchoMessage) {
|
|
|
|
var tagsToUse *map[string]ircmsg.TagValue
|
|
|
|
if client.capabilities.Has(caps.MessageTags) {
|
|
|
|
tagsToUse = clientOnlyTags
|
|
|
|
}
|
2018-12-28 19:45:55 +01:00
|
|
|
nickMaskString := client.NickMaskString()
|
|
|
|
accountName := client.AccountName()
|
2018-02-10 23:57:15 +01:00
|
|
|
if message == nil {
|
2018-12-28 19:45:55 +01:00
|
|
|
rb.AddFromClient(msgid, nickMaskString, accountName, tagsToUse, cmd, channel.name)
|
2018-02-10 23:57:15 +01:00
|
|
|
} else {
|
2018-12-28 19:45:55 +01:00
|
|
|
rb.AddSplitMessageFromClient(msgid, nickMaskString, accountName, tagsToUse, cmd, channel.name, *message)
|
2018-02-10 23:57:15 +01:00
|
|
|
}
|
|
|
|
}
|
2018-11-26 11:23:27 +01:00
|
|
|
|
|
|
|
nickmask := client.NickMaskString()
|
|
|
|
account := client.AccountName()
|
|
|
|
|
2018-12-30 21:20:44 +01:00
|
|
|
now := time.Now().UTC()
|
|
|
|
|
2017-10-23 01:50:16 +02:00
|
|
|
for _, member := range channel.Members() {
|
2017-01-14 06:28:50 +01:00
|
|
|
if minPrefix != nil && !channel.ClientIsAtLeast(member, minPrefixMode) {
|
|
|
|
// STATUSMSG
|
|
|
|
continue
|
|
|
|
}
|
2018-02-10 23:57:15 +01:00
|
|
|
// echo-message is handled above, so skip sending the msg to the user themselves as well
|
|
|
|
if member == client {
|
2017-01-14 06:28:50 +01:00
|
|
|
continue
|
|
|
|
}
|
2017-01-14 12:48:57 +01:00
|
|
|
var tagsToUse *map[string]ircmsg.TagValue
|
2017-09-29 09:25:58 +02:00
|
|
|
if member.capabilities.Has(caps.MessageTags) {
|
2017-01-14 12:48:57 +01:00
|
|
|
tagsToUse = clientOnlyTags
|
|
|
|
}
|
|
|
|
|
2018-02-10 23:57:15 +01:00
|
|
|
if message == nil {
|
2018-12-30 21:20:44 +01:00
|
|
|
member.sendFromClientInternal(false, now, msgid, nickmask, account, tagsToUse, cmd, channel.name)
|
2017-01-14 06:28:50 +01:00
|
|
|
} else {
|
2018-12-30 21:20:44 +01:00
|
|
|
member.sendSplitMsgFromClientInternal(false, now, msgid, nickmask, account, tagsToUse, cmd, channel.name, *message)
|
2017-01-14 06:28:50 +01:00
|
|
|
}
|
|
|
|
}
|
2018-11-26 11:23:27 +01:00
|
|
|
|
|
|
|
channel.history.Add(history.Item{
|
|
|
|
Type: histType,
|
|
|
|
Msgid: msgid,
|
|
|
|
Message: *message,
|
|
|
|
Nick: nickmask,
|
|
|
|
AccountName: account,
|
2018-12-30 21:20:44 +01:00
|
|
|
Time: now,
|
2018-11-26 11:23:27 +01:00
|
|
|
})
|
2017-01-14 06:28:50 +01:00
|
|
|
}
|
|
|
|
|
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) {
|
2016-10-11 15:51:46 +02:00
|
|
|
casefoldedName, err := CasefoldName(nick)
|
|
|
|
target := channel.server.clients.Get(casefoldedName)
|
|
|
|
if err != nil || target == nil {
|
2018-02-05 15:21:08 +01:00
|
|
|
rb.Add(nil, client.server.name, ERR_NOSUCHNICK, client.nick, 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 {
|
2018-02-05 15:21:08 +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
|
|
|
}
|
|
|
|
|
2017-03-27 06:29:51 +02: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) {
|
2017-03-26 12:37:13 +02:00
|
|
|
// choose appropriate modes
|
|
|
|
var rpllist, rplendoflist string
|
2018-02-03 11:21:32 +01:00
|
|
|
if mode == modes.BanMask {
|
2017-03-26 12:37:13 +02:00
|
|
|
rpllist = RPL_BANLIST
|
|
|
|
rplendoflist = RPL_ENDOFBANLIST
|
2018-02-03 11:21:32 +01:00
|
|
|
} else if mode == modes.ExceptMask {
|
2017-03-26 12:37:13 +02:00
|
|
|
rpllist = RPL_EXCEPTLIST
|
|
|
|
rplendoflist = RPL_ENDOFEXCEPTLIST
|
2018-02-03 11:21:32 +01:00
|
|
|
} else if mode == modes.InviteMask {
|
2017-03-26 12:37:13 +02:00
|
|
|
rpllist = RPL_INVITELIST
|
|
|
|
rplendoflist = RPL_ENDOFINVITELIST
|
|
|
|
}
|
|
|
|
|
2017-11-03 07:36:55 +01:00
|
|
|
nick := client.Nick()
|
2017-10-23 01:50:16 +02:00
|
|
|
channel.stateMutex.RLock()
|
|
|
|
// XXX don't acquire any new locks in this section, besides Socket.Write
|
2017-03-26 12:37:13 +02:00
|
|
|
for mask := range channel.lists[mode].masks {
|
2018-02-05 15:21:08 +01:00
|
|
|
rb.Add(nil, client.server.name, rpllist, nick, channel.name, mask)
|
2017-03-26 12:37:13 +02:00
|
|
|
}
|
2017-10-23 01:50:16 +02:00
|
|
|
channel.stateMutex.RUnlock()
|
|
|
|
|
2018-02-05 15:21:08 +01:00
|
|
|
rb.Add(nil, client.server.name, rplendoflist, nick, channel.name, client.t("End of list"))
|
2014-03-08 02:35:58 +01:00
|
|
|
}
|
2014-03-08 02:09:49 +01:00
|
|
|
|
2018-02-05 15:21:08 +01:00
|
|
|
func (channel *Channel) applyModeMask(client *Client, mode modes.Mode, op modes.ModeOp, mask string, rb *ResponseBuffer) bool {
|
2014-03-08 02:09:49 +01:00
|
|
|
list := channel.lists[mode]
|
|
|
|
if list == nil {
|
|
|
|
// This should never happen, but better safe than panicky.
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2018-02-03 11:21:32 +01:00
|
|
|
if (op == modes.List) || (mask == "") {
|
2018-02-05 15:21:08 +01:00
|
|
|
channel.ShowMaskList(client, mode, rb)
|
2014-03-08 02:35:58 +01:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2018-02-03 11:21:32 +01:00
|
|
|
if !channel.ClientIsAtLeast(client, modes.ChannelOperator) {
|
2018-02-05 15:21:08 +01:00
|
|
|
rb.Add(nil, client.server.name, ERR_CHANOPRIVSNEEDED, channel.name, client.t("You're not a channel operator"))
|
2014-03-08 02:35:58 +01:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2018-02-03 11:21:32 +01:00
|
|
|
if op == modes.Add {
|
2014-03-08 02:35:58 +01:00
|
|
|
return list.Add(mask)
|
2014-03-08 02:09:49 +01:00
|
|
|
}
|
|
|
|
|
2018-02-03 11:21:32 +01:00
|
|
|
if op == modes.Remove {
|
2014-03-08 02:35:58 +01:00
|
|
|
return list.Remove(mask)
|
2014-03-08 02:09:49 +01:00
|
|
|
}
|
2014-03-08 02:35:58 +01:00
|
|
|
|
|
|
|
return false
|
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-03-01 16:37:30 +01:00
|
|
|
|
2018-04-24 09:11:11 +02:00
|
|
|
if channelEmpty {
|
2018-03-01 16:37:30 +01:00
|
|
|
client.server.channels.Cleanup(channel)
|
|
|
|
}
|
2018-04-24 09:11:11 +02:00
|
|
|
client.removeChannel(channel)
|
2014-02-17 08:29:11 +01:00
|
|
|
}
|
|
|
|
|
2018-02-05 15:21:08 +01:00
|
|
|
func (channel *Channel) Kick(client *Client, target *Client, comment string, rb *ResponseBuffer) {
|
2018-04-23 00:47:10 +02:00
|
|
|
if !(client.HasMode(modes.Operator) || channel.hasClient(client)) {
|
2018-02-05 15:21:08 +01:00
|
|
|
rb.Add(nil, client.server.name, ERR_NOTONCHANNEL, channel.name, client.t("You're not on that channel"))
|
2014-02-17 08:29:11 +01:00
|
|
|
return
|
|
|
|
}
|
2018-02-03 11:21:32 +01:00
|
|
|
if !channel.ClientIsAtLeast(client, modes.ChannelOperator) {
|
2018-02-05 15:21:08 +01:00
|
|
|
rb.Add(nil, client.server.name, ERR_CANNOTSENDTOCHAN, channel.name, client.t("Cannot send to channel"))
|
2014-02-17 08:29:11 +01:00
|
|
|
return
|
|
|
|
}
|
2017-10-23 01:50:16 +02:00
|
|
|
if !channel.hasClient(target) {
|
2018-02-05 15:21:08 +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
|
|
|
|
}
|
2017-10-23 01:50:16 +02:00
|
|
|
if !channel.ClientHasPrivsOver(client, target) {
|
2018-05-25 08:46:36 +02:00
|
|
|
rb.Add(nil, client.server.name, ERR_CHANOPRIVSNEEDED, channel.name, client.t("You don't have enough channel privileges"))
|
2017-10-23 01:50:16 +02:00
|
|
|
return
|
|
|
|
}
|
2014-02-17 08:29:11 +01:00
|
|
|
|
2017-11-03 07:36:55 +01:00
|
|
|
kicklimit := client.server.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
|
|
|
}
|
|
|
|
|
2017-11-03 07:36:55 +01:00
|
|
|
clientMask := client.NickMaskString()
|
|
|
|
targetNick := target.Nick()
|
2017-10-23 01:50:16 +02:00
|
|
|
for _, member := range channel.Members() {
|
|
|
|
member.Send(nil, clientMask, "KICK", channel.name, targetNick, comment)
|
2014-02-20 07:20:34 +01:00
|
|
|
}
|
2017-10-23 01:50:16 +02:00
|
|
|
|
2018-11-26 11:23:27 +01:00
|
|
|
channel.history.Add(history.Item{
|
|
|
|
Type: history.Kick,
|
|
|
|
Nick: clientMask,
|
|
|
|
Message: utils.MakeSplitMessage(comment, true),
|
|
|
|
AccountName: target.AccountName(),
|
|
|
|
Msgid: targetNick, // XXX abuse this field
|
|
|
|
})
|
|
|
|
|
2017-10-23 01:50:16 +02:00
|
|
|
channel.Quit(target)
|
2014-02-17 02:23:47 +01:00
|
|
|
}
|
2014-02-25 16:28:09 +01:00
|
|
|
|
2017-03-27 06:29:51 +02: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) {
|
2018-12-23 19:49:03 +01:00
|
|
|
chname := channel.Name()
|
2018-04-23 00:47:10 +02:00
|
|
|
if channel.flags.HasMode(modes.InviteOnly) && !channel.ClientIsAtLeast(inviter, modes.ChannelOperator) {
|
2018-12-23 19:49:03 +01:00
|
|
|
rb.Add(nil, inviter.server.name, ERR_CHANOPRIVSNEEDED, chname, 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) {
|
2018-12-23 19:49:03 +01:00
|
|
|
rb.Add(nil, inviter.server.name, ERR_NOTONCHANNEL, chname, 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() {
|
2018-02-03 11:21:32 +01:00
|
|
|
if member.capabilities.Has(caps.InviteNotify) && member != inviter && member != invitee && channel.ClientIsAtLeast(member, modes.Halfop) {
|
2018-12-23 19:49:03 +01:00
|
|
|
member.Send(nil, inviter.NickMaskString(), "INVITE", invitee.Nick(), chname)
|
2016-10-16 06:14:55 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-23 19:49:03 +01:00
|
|
|
rb.Add(nil, inviter.server.name, RPL_INVITING, inviter.Nick(), invitee.Nick(), chname)
|
|
|
|
invitee.Send(nil, inviter.nickMaskString, "INVITE", invitee.nick, chname)
|
2018-04-23 00:47:10 +02:00
|
|
|
if invitee.HasMode(modes.Away) {
|
2018-02-05 15:21:08 +01:00
|
|
|
rb.Add(nil, inviter.server.name, RPL_AWAY, invitee.nick, invitee.awayMessage)
|
2014-02-25 16:28:09 +01:00
|
|
|
}
|
|
|
|
}
|