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 (
|
2017-11-09 04:19:50 +01:00
|
|
|
"errors"
|
2016-10-11 15:51:46 +02:00
|
|
|
"fmt"
|
2014-02-22 21:49:33 +01:00
|
|
|
"strconv"
|
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"
|
2017-11-09 04:19:50 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
ChannelAlreadyRegistered = errors.New("Channel is already registered")
|
2014-02-22 21:49:33 +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 {
|
2017-11-07 20:38:18 +01:00
|
|
|
flags ModeSet
|
|
|
|
lists map[Mode]*UserMaskSet
|
|
|
|
key string
|
|
|
|
members MemberSet
|
|
|
|
membersCache []*Client // allow iteration over channel members without holding the lock
|
|
|
|
membersCacheMutex sync.Mutex // tier 2; see `regenerateMembersCache`
|
|
|
|
name string
|
|
|
|
nameCasefolded string
|
|
|
|
server *Server
|
|
|
|
createdTime time.Time
|
2017-11-09 04:19:50 +01:00
|
|
|
registeredFounder string
|
|
|
|
registeredTime time.Time
|
2017-11-07 20:38:18 +01:00
|
|
|
stateMutex sync.RWMutex
|
|
|
|
topic string
|
|
|
|
topicSetBy string
|
|
|
|
topicSetTime time.Time
|
|
|
|
userLimit uint64
|
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.
|
2017-11-09 04:19:50 +01:00
|
|
|
func NewChannel(s *Server, name string, addDefaultModes bool, regInfo *RegisteredChannel) *Channel {
|
2016-10-11 15:51:46 +02:00
|
|
|
casefoldedName, err := CasefoldChannel(name)
|
|
|
|
if err != nil {
|
2017-10-30 10:21:47 +01:00
|
|
|
s.logger.Error("internal", fmt.Sprintf("Bad channel name %s: %v", name, err))
|
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
|
|
|
|
flags: make(ModeSet),
|
2017-03-24 03:23:21 +01:00
|
|
|
lists: map[Mode]*UserMaskSet{
|
2014-03-08 02:09:49 +01:00
|
|
|
BanMask: NewUserMaskSet(),
|
|
|
|
ExceptMask: NewUserMaskSet(),
|
|
|
|
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,
|
2012-12-09 21:51:50 +01:00
|
|
|
}
|
2014-02-25 20:11:34 +01:00
|
|
|
|
2016-04-21 11:29:50 +02:00
|
|
|
if addDefaultModes {
|
2017-10-30 10:21:47 +01:00
|
|
|
for _, mode := range s.DefaultChannelModes() {
|
2016-04-21 11:29:50 +02:00
|
|
|
channel.flags[mode] = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-09 04:19:50 +01:00
|
|
|
if regInfo != nil {
|
|
|
|
channel.applyRegInfo(regInfo)
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
for _, mask := range chanReg.Banlist {
|
|
|
|
channel.lists[BanMask].Add(mask)
|
|
|
|
}
|
|
|
|
for _, mask := range chanReg.Exceptlist {
|
|
|
|
channel.lists[ExceptMask].Add(mask)
|
|
|
|
}
|
|
|
|
for _, mask := range chanReg.Invitelist {
|
|
|
|
channel.lists[InviteMask].Add(mask)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// obtain a consistent snapshot of the channel state that can be persisted to the DB
|
|
|
|
func (channel *Channel) ExportRegistration(includeLists bool) (info RegisteredChannel) {
|
|
|
|
channel.stateMutex.RLock()
|
|
|
|
defer channel.stateMutex.RUnlock()
|
|
|
|
|
|
|
|
info.Name = channel.name
|
|
|
|
info.Topic = channel.topic
|
|
|
|
info.TopicSetBy = channel.topicSetBy
|
|
|
|
info.TopicSetTime = channel.topicSetTime
|
|
|
|
info.Founder = channel.registeredFounder
|
|
|
|
info.RegisteredAt = channel.registeredTime
|
|
|
|
|
|
|
|
if includeLists {
|
|
|
|
for mask := range channel.lists[BanMask].masks {
|
|
|
|
info.Banlist = append(info.Banlist, mask)
|
|
|
|
}
|
|
|
|
for mask := range channel.lists[ExceptMask].masks {
|
|
|
|
info.Exceptlist = append(info.Exceptlist, mask)
|
|
|
|
}
|
|
|
|
for mask := range channel.lists[InviteMask].masks {
|
|
|
|
info.Invitelist = append(info.Invitelist, mask)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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 != "" {
|
|
|
|
return ChannelAlreadyRegistered
|
|
|
|
}
|
|
|
|
channel.registeredFounder = founder
|
|
|
|
channel.registeredTime = time.Now()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsRegistered returns whether the channel is registered.
|
|
|
|
func (channel *Channel) IsRegistered() bool {
|
|
|
|
channel.stateMutex.RLock()
|
|
|
|
defer channel.stateMutex.RUnlock()
|
|
|
|
return channel.registeredFounder != ""
|
|
|
|
}
|
|
|
|
|
2017-10-23 01:50:16 +02:00
|
|
|
func (channel *Channel) regenerateMembersCache() {
|
2017-11-07 20:38:18 +01:00
|
|
|
// this is eventually consistent even without holding stateMutex.Lock()
|
2017-10-23 01:50:16 +02:00
|
|
|
// throughout the update; all updates to `members` while holding Lock()
|
|
|
|
// have a serial order, so the call to `regenerateMembersCache` that
|
2017-11-07 20:38:18 +01:00
|
|
|
// happens-after the last one will see *all* the updates. then,
|
|
|
|
// `membersCacheMutex` ensures that this final read is correctly paired
|
|
|
|
// with the final write to `membersCache`.
|
|
|
|
channel.membersCacheMutex.Lock()
|
|
|
|
defer channel.membersCacheMutex.Unlock()
|
|
|
|
|
2017-10-23 01:50:16 +02:00
|
|
|
channel.stateMutex.RLock()
|
|
|
|
result := make([]*Client, len(channel.members))
|
|
|
|
i := 0
|
|
|
|
for client := range channel.members {
|
|
|
|
result[i] = client
|
|
|
|
i++
|
|
|
|
}
|
|
|
|
channel.stateMutex.RUnlock()
|
|
|
|
channel.stateMutex.Lock()
|
|
|
|
channel.membersCache = result
|
|
|
|
channel.stateMutex.Unlock()
|
|
|
|
return
|
2017-01-11 03:16:51 +01:00
|
|
|
|
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.
|
2014-02-18 06:02:03 +01:00
|
|
|
func (channel *Channel) Names(client *Client) {
|
2017-10-23 01:50:16 +02:00
|
|
|
currentNicks := channel.nicks(client)
|
2016-06-19 06:55:24 +02:00
|
|
|
// assemble and send replies
|
2016-10-11 15:51:46 +02:00
|
|
|
maxNamLen := 480 - len(client.server.name) - len(client.nick)
|
2016-06-19 06:55:24 +02:00
|
|
|
var buffer string
|
|
|
|
for _, nick := range currentNicks {
|
|
|
|
if buffer == "" {
|
|
|
|
buffer += nick
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(buffer)+1+len(nick) > maxNamLen {
|
2016-10-11 15:51:46 +02:00
|
|
|
client.Send(nil, client.server.name, RPL_NAMREPLY, client.nick, "=", channel.name, buffer)
|
2016-06-19 06:55:24 +02:00
|
|
|
buffer = nick
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
buffer += " "
|
|
|
|
buffer += nick
|
|
|
|
}
|
|
|
|
|
2016-10-11 15:51:46 +02:00
|
|
|
client.Send(nil, client.server.name, RPL_NAMREPLY, client.nick, "=", channel.name, buffer)
|
|
|
|
client.Send(nil, client.server.name, RPL_ENDOFNAMES, client.nick, channel.name, "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.
|
2017-03-24 03:23:21 +01:00
|
|
|
func (channel *Channel) ClientIsAtLeast(client *Client, permission Mode) bool {
|
2017-10-23 01:50:16 +02:00
|
|
|
channel.stateMutex.RLock()
|
|
|
|
defer channel.stateMutex.RUnlock()
|
2017-01-11 03:16:51 +01:00
|
|
|
|
2016-10-22 16:45:51 +02:00
|
|
|
// get voice, since it's not a part of ChannelPrivModes
|
|
|
|
if channel.members.HasMode(client, permission) {
|
|
|
|
return true
|
|
|
|
}
|
2016-10-16 06:14:55 +02:00
|
|
|
|
2016-10-22 16:45:51 +02:00
|
|
|
// check regular modes
|
|
|
|
for _, mode := range ChannelPrivModes {
|
|
|
|
if channel.members.HasMode(client, mode) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
if mode == permission {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
2014-02-10 04:59:59 +01:00
|
|
|
}
|
|
|
|
|
2016-04-14 01:35:36 +02:00
|
|
|
// Prefixes returns a list of prefixes for the given set of channel modes.
|
2017-03-24 03:23:21 +01:00
|
|
|
func (modes ModeSet) Prefixes(isMultiPrefix bool) string {
|
2016-04-14 01:35:36 +02:00
|
|
|
var prefixes string
|
|
|
|
|
|
|
|
// add prefixes in order from highest to lowest privs
|
|
|
|
for _, mode := range ChannelPrivModes {
|
|
|
|
if modes[mode] {
|
|
|
|
prefixes += ChannelModePrefixes[mode]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if modes[Voice] {
|
|
|
|
prefixes += ChannelModePrefixes[Voice]
|
|
|
|
}
|
|
|
|
|
|
|
|
if !isMultiPrefix && len(prefixes) > 1 {
|
|
|
|
prefixes = string(prefixes[0])
|
|
|
|
}
|
|
|
|
|
|
|
|
return prefixes
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
for _, mode := range ChannelPrivModes {
|
|
|
|
if clientModes[mode] {
|
|
|
|
result = true
|
|
|
|
// admins cannot kick other admins
|
|
|
|
if mode == ChannelAdmin && targetModes[ChannelAdmin] {
|
|
|
|
result = false
|
|
|
|
}
|
|
|
|
break
|
|
|
|
} else if channel.members[target][mode] {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
|
|
|
func (channel *Channel) nicks(target *Client) []string {
|
2017-09-29 09:25:58 +02:00
|
|
|
isMultiPrefix := (target != nil) && target.capabilities.Has(caps.MultiPrefix)
|
|
|
|
isUserhostInNames := (target != nil) && target.capabilities.Has(caps.UserhostInNames)
|
2017-10-23 01:50:16 +02:00
|
|
|
|
|
|
|
// slightly cumbersome: get the mutex and copy both the client pointers and
|
|
|
|
// the mode prefixes
|
|
|
|
channel.stateMutex.RLock()
|
|
|
|
length := len(channel.members)
|
|
|
|
clients := make([]*Client, length)
|
|
|
|
result := make([]string, length)
|
2014-02-05 04:28:24 +01:00
|
|
|
i := 0
|
2014-02-15 06:57:08 +01:00
|
|
|
for client, modes := range channel.members {
|
2017-10-23 01:50:16 +02:00
|
|
|
clients[i] = client
|
|
|
|
result[i] = modes.Prefixes(isMultiPrefix)
|
|
|
|
i++
|
|
|
|
}
|
|
|
|
channel.stateMutex.RUnlock()
|
|
|
|
|
|
|
|
i = 0
|
|
|
|
for i < length {
|
2016-08-14 03:59:33 +02:00
|
|
|
if isUserhostInNames {
|
2017-11-03 07:36:55 +01:00
|
|
|
result[i] += clients[i].NickMaskString()
|
2016-08-14 03:59:33 +02:00
|
|
|
} else {
|
2017-11-03 07:36:55 +01:00
|
|
|
result[i] += clients[i].Nick()
|
2016-08-14 03:59:33 +02:00
|
|
|
}
|
2017-03-27 06:29:51 +02:00
|
|
|
i++
|
2014-02-05 04:28:24 +01:00
|
|
|
}
|
2017-10-23 01:50:16 +02:00
|
|
|
|
|
|
|
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) {
|
|
|
|
isMember := client.HasMode(Operator) || channel.hasClient(client)
|
2014-02-22 21:49:33 +01:00
|
|
|
showKey := isMember && (channel.key != "")
|
|
|
|
showUserLimit := channel.userLimit > 0
|
|
|
|
|
2017-10-23 01:50:16 +02:00
|
|
|
modes := "+"
|
|
|
|
|
2014-02-22 21:49:33 +01:00
|
|
|
// flags with args
|
|
|
|
if showKey {
|
2017-10-23 01:50:16 +02:00
|
|
|
modes += Key.String()
|
2014-02-09 08:33:56 +01:00
|
|
|
}
|
2014-02-22 21:49:33 +01:00
|
|
|
if showUserLimit {
|
2017-10-23 01:50:16 +02:00
|
|
|
modes += UserLimit.String()
|
2014-02-22 21:49:33 +01:00
|
|
|
}
|
2014-02-15 06:57:08 +01:00
|
|
|
|
2017-10-23 01:50:16 +02:00
|
|
|
channel.stateMutex.RLock()
|
|
|
|
defer channel.stateMutex.RUnlock()
|
|
|
|
|
2014-02-22 21:49:33 +01:00
|
|
|
// flags
|
2014-02-15 06:57:08 +01:00
|
|
|
for mode := range channel.flags {
|
2017-10-23 01:50:16 +02:00
|
|
|
modes += mode.String()
|
2014-02-15 06:57:08 +01:00
|
|
|
}
|
|
|
|
|
2017-10-23 01:50:16 +02:00
|
|
|
result = []string{modes}
|
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 {
|
2017-10-23 01:50:16 +02:00
|
|
|
result = append(result, strconv.FormatUint(channel.userLimit, 10))
|
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-03-27 06:29:51 +02:00
|
|
|
// IsFull returns true if this channel is at its' members limit.
|
2014-02-22 21:49:33 +01:00
|
|
|
func (channel *Channel) IsFull() bool {
|
2017-10-23 01:50:16 +02:00
|
|
|
channel.stateMutex.RLock()
|
|
|
|
defer channel.stateMutex.RUnlock()
|
2017-03-26 14:11:09 +02:00
|
|
|
return (channel.userLimit > 0) && (uint64(len(channel.members)) >= channel.userLimit)
|
2014-02-22 21:49:33 +01:00
|
|
|
}
|
|
|
|
|
2017-03-27 06:29:51 +02:00
|
|
|
// CheckKey returns true if the key is not set or matches the given key.
|
2016-06-19 06:55:24 +02:00
|
|
|
func (channel *Channel) CheckKey(key string) bool {
|
2014-02-22 21:49:33 +01:00
|
|
|
return (channel.key == "") || (channel.key == key)
|
|
|
|
}
|
|
|
|
|
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).
|
|
|
|
//TODO(dan): /SAJOIN and maybe a ForceJoin function?
|
2016-06-19 06:55:24 +02:00
|
|
|
func (channel *Channel) Join(client *Client, key string) {
|
2017-10-23 01:50:16 +02:00
|
|
|
if channel.hasClient(client) {
|
2017-03-26 14:11:09 +02:00
|
|
|
// already joined, no message needs to be sent
|
2012-12-09 07:54:58 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2014-02-22 21:49:33 +01:00
|
|
|
if channel.IsFull() {
|
2016-10-11 15:51:46 +02:00
|
|
|
client.Send(nil, client.server.name, ERR_CHANNELISFULL, channel.name, "Cannot join channel (+l)")
|
2014-02-22 21:49:33 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if !channel.CheckKey(key) {
|
2016-10-11 15:51:46 +02:00
|
|
|
client.Send(nil, client.server.name, ERR_BADCHANNELKEY, channel.name, "Cannot join channel (+k)")
|
2014-02-21 03:56:13 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-10-23 03:48:57 +02:00
|
|
|
isInvited := channel.lists[InviteMask].Match(client.nickMaskCasefolded)
|
2014-03-08 02:09:49 +01:00
|
|
|
if channel.flags[InviteOnly] && !isInvited {
|
2016-10-11 15:51:46 +02:00
|
|
|
client.Send(nil, client.server.name, ERR_INVITEONLYCHAN, channel.name, "Cannot join channel (+i)")
|
2014-03-08 02:09:49 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-10-23 03:48:57 +02:00
|
|
|
if channel.lists[BanMask].Match(client.nickMaskCasefolded) &&
|
2014-03-08 02:09:49 +01:00
|
|
|
!isInvited &&
|
2016-10-23 03:48:57 +02:00
|
|
|
!channel.lists[ExceptMask].Match(client.nickMaskCasefolded) {
|
2016-10-11 15:51:46 +02:00
|
|
|
client.Send(nil, client.server.name, ERR_BANNEDFROMCHAN, channel.name, "Cannot join channel (+b)")
|
2014-03-08 02:09:49 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-03-10 13:02:08 +01:00
|
|
|
client.server.logger.Debug("join", fmt.Sprintf("%s joined channel %s", client.nick, channel.name))
|
2017-03-06 13:11:10 +01:00
|
|
|
|
2017-10-23 01:50:16 +02:00
|
|
|
for _, member := range channel.Members() {
|
2017-09-29 09:25:58 +02:00
|
|
|
if member.capabilities.Has(caps.ExtendedJoin) {
|
2016-10-11 15:51:46 +02:00
|
|
|
member.Send(nil, client.nickMaskString, "JOIN", channel.name, client.account.Name, client.realname)
|
2016-08-14 03:59:33 +02:00
|
|
|
} else {
|
2016-10-11 15:51:46 +02:00
|
|
|
member.Send(nil, client.nickMaskString, "JOIN", channel.name)
|
2016-08-14 03:59:33 +02:00
|
|
|
}
|
2016-06-22 13:35:26 +02:00
|
|
|
}
|
|
|
|
|
2017-10-23 01:50:16 +02:00
|
|
|
channel.stateMutex.Lock()
|
2014-02-17 02:23:47 +01:00
|
|
|
channel.members.Add(client)
|
2017-10-23 01:50:16 +02:00
|
|
|
firstJoin := len(channel.members) == 1
|
|
|
|
channel.stateMutex.Unlock()
|
|
|
|
channel.regenerateMembersCache()
|
|
|
|
|
|
|
|
client.addChannel(channel)
|
2017-03-27 06:52:34 +02:00
|
|
|
|
|
|
|
// give channel mode if necessary
|
2017-11-09 04:19:50 +01:00
|
|
|
newChannel := firstJoin && !channel.IsRegistered()
|
2017-03-24 03:38:07 +01:00
|
|
|
var givenMode *Mode
|
2017-11-09 04:19:50 +01:00
|
|
|
if client.AccountName() == channel.registeredFounder {
|
|
|
|
givenMode = &ChannelFounder
|
|
|
|
} else if newChannel {
|
|
|
|
givenMode = &ChannelOperator
|
|
|
|
}
|
|
|
|
if givenMode != nil {
|
|
|
|
channel.stateMutex.Lock()
|
|
|
|
channel.members[client][*givenMode] = true
|
|
|
|
channel.stateMutex.Unlock()
|
|
|
|
}
|
2014-02-17 02:23:47 +01:00
|
|
|
|
2017-09-29 09:25:58 +02:00
|
|
|
if client.capabilities.Has(caps.ExtendedJoin) {
|
2016-10-11 15:51:46 +02:00
|
|
|
client.Send(nil, client.nickMaskString, "JOIN", channel.name, client.account.Name, client.realname)
|
2016-08-14 03:59:33 +02:00
|
|
|
} else {
|
2016-10-11 15:51:46 +02:00
|
|
|
client.Send(nil, client.nickMaskString, "JOIN", channel.name)
|
2016-08-14 03:59:33 +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 {
|
2017-10-23 01:50:16 +02:00
|
|
|
channel.SendTopic(client)
|
2017-09-26 00:01:10 +02:00
|
|
|
}
|
2017-10-23 01:50:16 +02:00
|
|
|
channel.Names(client)
|
2017-03-24 03:38:07 +01:00
|
|
|
if givenMode != nil {
|
2017-10-23 01:50:16 +02:00
|
|
|
for _, member := range channel.Members() {
|
2017-03-27 06:52:34 +02:00
|
|
|
member.Send(nil, client.server.name, "MODE", channel.name, fmt.Sprintf("+%v", *givenMode), client.nick)
|
|
|
|
}
|
2017-03-24 03:38:07 +01:00
|
|
|
}
|
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.
|
2016-06-19 06:55:24 +02:00
|
|
|
func (channel *Channel) Part(client *Client, message string) {
|
2017-10-23 01:50:16 +02:00
|
|
|
if !channel.hasClient(client) {
|
2016-10-11 15:51:46 +02:00
|
|
|
client.Send(nil, client.server.name, ERR_NOTONCHANNEL, channel.name, "You're not on that channel")
|
2012-12-09 07:54:58 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-10-23 01:50:16 +02:00
|
|
|
for _, member := range channel.Members() {
|
2016-10-11 15:51:46 +02:00
|
|
|
member.Send(nil, client.nickMaskString, "PART", channel.name, message)
|
2014-02-20 07:20:34 +01:00
|
|
|
}
|
2017-10-23 01:50:16 +02:00
|
|
|
channel.Quit(client)
|
2017-03-06 13:11:10 +01:00
|
|
|
|
2017-03-10 13:02:08 +01:00
|
|
|
client.server.logger.Debug("part", fmt.Sprintf("%s left channel %s", client.nick, channel.name))
|
2012-12-09 07:54:58 +01:00
|
|
|
}
|
|
|
|
|
2017-10-23 01:50:16 +02:00
|
|
|
// SendTopic sends the channel topic to the given client.
|
|
|
|
func (channel *Channel) SendTopic(client *Client) {
|
|
|
|
if !channel.hasClient(client) {
|
2016-10-11 15:51:46 +02:00
|
|
|
client.Send(nil, client.server.name, ERR_NOTONCHANNEL, client.nick, channel.name, "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 == "" {
|
|
|
|
client.Send(nil, client.server.name, RPL_NOTOPIC, client.nick, name, "No topic is set")
|
2012-12-09 07:54:58 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-10-23 01:50:16 +02:00
|
|
|
client.Send(nil, client.server.name, RPL_TOPIC, client.nick, name, topic)
|
|
|
|
client.Send(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.
|
2016-06-19 06:55:24 +02:00
|
|
|
func (channel *Channel) SetTopic(client *Client, topic string) {
|
2017-10-23 01:50:16 +02:00
|
|
|
if !(client.flags[Operator] || channel.hasClient(client)) {
|
2016-10-11 15:51:46 +02:00
|
|
|
client.Send(nil, client.server.name, ERR_NOTONCHANNEL, channel.name, "You're not on that channel")
|
2014-02-17 07:20:42 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-10-23 01:50:16 +02:00
|
|
|
if channel.HasMode(OpOnlyTopic) && !channel.ClientIsAtLeast(client, ChannelOperator) {
|
2016-10-11 15:51:46 +02:00
|
|
|
client.Send(nil, client.server.name, ERR_CHANOPRIVSNEEDED, channel.name, "You're not a channel operator")
|
2014-02-16 04:56:38 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-09-12 04:40:09 +02:00
|
|
|
if len(topic) > client.server.limits.TopicLen {
|
|
|
|
topic = topic[:client.server.limits.TopicLen]
|
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() {
|
|
|
|
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
|
|
|
|
2017-11-09 04:19:50 +01:00
|
|
|
go channel.server.channelRegistry.StoreChannel(channel, false)
|
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]
|
|
|
|
if channel.flags[NoOutside] && !hasClient {
|
2014-02-22 21:49:33 +01:00
|
|
|
return false
|
|
|
|
}
|
2017-10-23 01:50:16 +02:00
|
|
|
if channel.flags[Moderated] && !channel.ClientIsAtLeast(client, Voice) {
|
2014-02-22 21:49:33 +01:00
|
|
|
return false
|
|
|
|
}
|
2017-03-28 09:32:03 +02:00
|
|
|
if channel.flags[RegisteredOnly] && client.account == &NoAccount {
|
|
|
|
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.
|
2017-03-24 03:23:21 +01:00
|
|
|
func (channel *Channel) TagMsg(msgid string, minPrefix *Mode, clientOnlyTags *map[string]ircmsg.TagValue, client *Client) {
|
2017-09-29 04:07:52 +02:00
|
|
|
channel.sendMessage(msgid, "TAGMSG", []caps.Capability{caps.MessageTags}, minPrefix, clientOnlyTags, client, nil)
|
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.
|
2017-09-29 04:07:52 +02:00
|
|
|
func (channel *Channel) sendMessage(msgid, cmd string, requiredCaps []caps.Capability, minPrefix *Mode, clientOnlyTags *map[string]ircmsg.TagValue, client *Client, message *string) {
|
2014-02-22 21:49:33 +01:00
|
|
|
if !channel.CanSpeak(client) {
|
2016-10-11 15:51:46 +02:00
|
|
|
client.Send(nil, client.server.name, ERR_CANNOTSENDTOCHAN, channel.name, "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
|
2017-03-24 03:23:21 +01:00
|
|
|
var minPrefixMode Mode
|
2016-10-22 16:45:51 +02:00
|
|
|
if minPrefix != nil {
|
|
|
|
minPrefixMode = *minPrefix
|
|
|
|
}
|
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
|
|
|
|
}
|
2017-09-29 09:25:58 +02:00
|
|
|
if member == client && !client.capabilities.Has(caps.EchoMessage) {
|
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 {
|
2017-01-14 12:48:57 +01:00
|
|
|
member.SendFromClient(msgid, client, messageTagsToUse, cmd, channel.name)
|
2016-10-16 04:54:15 +02:00
|
|
|
} else {
|
2017-01-14 12:48:57 +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.
|
2017-03-24 03:23:21 +01:00
|
|
|
func (channel *Channel) SplitPrivMsg(msgid string, minPrefix *Mode, clientOnlyTags *map[string]ircmsg.TagValue, client *Client, message SplitMessage) {
|
2017-01-14 12:48:57 +01:00
|
|
|
channel.sendSplitMessage(msgid, "PRIVMSG", minPrefix, clientOnlyTags, client, &message)
|
2017-01-14 06:28:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// SplitNotice sends a private message to everyone in this channel.
|
2017-03-24 03:23:21 +01:00
|
|
|
func (channel *Channel) SplitNotice(msgid string, minPrefix *Mode, clientOnlyTags *map[string]ircmsg.TagValue, client *Client, message SplitMessage) {
|
2017-01-14 12:48:57 +01:00
|
|
|
channel.sendSplitMessage(msgid, "NOTICE", minPrefix, clientOnlyTags, client, &message)
|
2017-01-14 06:28:50 +01:00
|
|
|
}
|
|
|
|
|
2017-03-24 03:23:21 +01:00
|
|
|
func (channel *Channel) sendSplitMessage(msgid, cmd string, minPrefix *Mode, clientOnlyTags *map[string]ircmsg.TagValue, client *Client, message *SplitMessage) {
|
2017-01-14 06:28:50 +01:00
|
|
|
if !channel.CanSpeak(client) {
|
|
|
|
client.Send(nil, client.server.name, ERR_CANNOTSENDTOCHAN, channel.name, "Cannot send to channel")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// for STATUSMSG
|
2017-03-24 03:23:21 +01:00
|
|
|
var minPrefixMode Mode
|
2017-01-14 06:28:50 +01:00
|
|
|
if minPrefix != nil {
|
|
|
|
minPrefixMode = *minPrefix
|
|
|
|
}
|
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
|
|
|
|
}
|
2017-09-29 09:25:58 +02:00
|
|
|
if member == client && !client.capabilities.Has(caps.EchoMessage) {
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
if message == nil {
|
|
|
|
member.SendFromClient(msgid, client, tagsToUse, cmd, channel.name)
|
2017-01-14 06:28:50 +01:00
|
|
|
} else {
|
2017-01-14 12:48:57 +01:00
|
|
|
member.SendSplitMsgFromClient(msgid, client, tagsToUse, cmd, channel.name, *message)
|
2017-01-14 06:28:50 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-24 03:23:21 +01:00
|
|
|
func (channel *Channel) applyModeMemberNoMutex(client *Client, mode Mode,
|
|
|
|
op ModeOp, nick string) *ModeChange {
|
2014-02-23 00:01:11 +01:00
|
|
|
if nick == "" {
|
2016-06-19 06:55:24 +02:00
|
|
|
//TODO(dan): shouldn't this be handled before it reaches this function?
|
2016-10-11 15:51:46 +02:00
|
|
|
client.Send(nil, client.server.name, ERR_NEEDMOREPARAMS, "MODE", "Not enough parameters")
|
2016-06-22 13:35:26 +02:00
|
|
|
return nil
|
2014-02-23 00:01:11 +01:00
|
|
|
}
|
|
|
|
|
2016-10-11 15:51:46 +02:00
|
|
|
casefoldedName, err := CasefoldName(nick)
|
|
|
|
target := channel.server.clients.Get(casefoldedName)
|
|
|
|
if err != nil || target == nil {
|
2017-09-25 23:52:43 +02:00
|
|
|
client.Send(nil, client.server.name, ERR_NOSUCHNICK, client.nick, nick, "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]
|
|
|
|
var already bool
|
|
|
|
if exists {
|
|
|
|
enable := op == Add
|
|
|
|
already = modeset[mode] == enable
|
|
|
|
modeset[mode] = enable
|
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 {
|
|
|
|
client.Send(nil, client.server.name, ERR_USERNOTINCHANNEL, client.nick, channel.name, "They aren't on that channel")
|
|
|
|
return nil
|
|
|
|
} else if already {
|
|
|
|
return nil
|
|
|
|
} else {
|
2017-03-24 03:23:21 +01:00
|
|
|
return &ModeChange{
|
2017-10-23 01:50:16 +02:00
|
|
|
op: op,
|
2016-06-22 13:35:26 +02:00
|
|
|
mode: mode,
|
|
|
|
arg: nick,
|
|
|
|
}
|
2014-02-23 00:01:11 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-27 06:29:51 +02:00
|
|
|
// ShowMaskList shows the given list to the client.
|
2017-03-24 03:23:21 +01:00
|
|
|
func (channel *Channel) ShowMaskList(client *Client, mode Mode) {
|
2017-03-26 12:37:13 +02:00
|
|
|
// choose appropriate modes
|
|
|
|
var rpllist, rplendoflist string
|
|
|
|
if mode == BanMask {
|
|
|
|
rpllist = RPL_BANLIST
|
|
|
|
rplendoflist = RPL_ENDOFBANLIST
|
|
|
|
} else if mode == ExceptMask {
|
|
|
|
rpllist = RPL_EXCEPTLIST
|
|
|
|
rplendoflist = RPL_ENDOFEXCEPTLIST
|
|
|
|
} else if mode == InviteMask {
|
|
|
|
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 {
|
2017-10-23 01:50:16 +02:00
|
|
|
client.Send(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()
|
|
|
|
|
|
|
|
client.Send(nil, client.server.name, rplendoflist, nick, channel.name, "End of list")
|
2014-03-08 02:35:58 +01:00
|
|
|
}
|
2014-03-08 02:09:49 +01:00
|
|
|
|
2017-03-24 03:23:21 +01:00
|
|
|
func (channel *Channel) applyModeMask(client *Client, mode Mode, op ModeOp, mask string) 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
|
|
|
|
}
|
|
|
|
|
2014-03-08 02:35:58 +01:00
|
|
|
if (op == List) || (mask == "") {
|
|
|
|
channel.ShowMaskList(client, mode)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2016-10-22 16:45:51 +02:00
|
|
|
if !channel.ClientIsAtLeast(client, ChannelOperator) {
|
2016-10-11 15:51:46 +02:00
|
|
|
client.Send(nil, client.server.name, ERR_CHANOPRIVSNEEDED, channel.name, "You're not a channel operator")
|
2014-03-08 02:35:58 +01:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2014-03-08 02:09:49 +01:00
|
|
|
if op == Add {
|
2014-03-08 02:35:58 +01:00
|
|
|
return list.Add(mask)
|
2014-03-08 02:09:49 +01:00
|
|
|
}
|
|
|
|
|
2014-03-08 02:35:58 +01:00
|
|
|
if op == Remove {
|
|
|
|
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) {
|
|
|
|
channel.stateMutex.Lock()
|
2014-02-17 07:20:42 +01:00
|
|
|
channel.members.Remove(client)
|
2017-10-23 01:50:16 +02:00
|
|
|
channel.stateMutex.Unlock()
|
|
|
|
channel.regenerateMembersCache()
|
|
|
|
|
|
|
|
client.removeChannel(channel)
|
2014-02-17 08:29:11 +01:00
|
|
|
}
|
|
|
|
|
2017-10-23 01:50:16 +02:00
|
|
|
func (channel *Channel) Kick(client *Client, target *Client, comment string) {
|
|
|
|
if !(client.flags[Operator] || channel.hasClient(client)) {
|
2016-10-11 15:51:46 +02:00
|
|
|
client.Send(nil, client.server.name, ERR_NOTONCHANNEL, channel.name, "You're not on that channel")
|
2014-02-17 08:29:11 +01:00
|
|
|
return
|
|
|
|
}
|
2017-10-23 01:50:16 +02:00
|
|
|
if !channel.ClientIsAtLeast(client, ChannelOperator) {
|
2016-10-11 15:51:46 +02:00
|
|
|
client.Send(nil, client.server.name, ERR_CANNOTSENDTOCHAN, channel.name, "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) {
|
2016-10-11 15:51:46 +02:00
|
|
|
client.Send(nil, client.server.name, ERR_USERNOTINCHANNEL, client.nick, channel.name, "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) {
|
|
|
|
client.Send(nil, client.server.name, ERR_CHANOPRIVSNEEDED, channel.name, "You're not a channel operator")
|
|
|
|
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
|
|
|
|
|
|
|
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.
|
2014-02-25 16:28:09 +01:00
|
|
|
func (channel *Channel) Invite(invitee *Client, inviter *Client) {
|
2016-10-22 16:45:51 +02:00
|
|
|
if channel.flags[InviteOnly] && !channel.ClientIsAtLeast(inviter, ChannelOperator) {
|
2016-10-11 15:51:46 +02:00
|
|
|
inviter.Send(nil, inviter.server.name, ERR_CHANOPRIVSNEEDED, channel.name, "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) {
|
2016-10-11 15:51:46 +02:00
|
|
|
inviter.Send(nil, inviter.server.name, ERR_NOTONCHANNEL, channel.name, "You're not on that channel")
|
2014-02-25 16:28:09 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-10-16 06:14:55 +02:00
|
|
|
//TODO(dan): handle this more nicely, keep a list of last X invited channels on invitee rather than explicitly modifying the invite list?
|
2014-03-08 02:09:49 +01:00
|
|
|
if channel.flags[InviteOnly] {
|
2017-11-03 07:36:55 +01:00
|
|
|
nmc := invitee.NickCasefolded()
|
2017-10-23 01:50:16 +02:00
|
|
|
channel.stateMutex.Lock()
|
|
|
|
channel.lists[InviteMask].Add(nmc)
|
|
|
|
channel.stateMutex.Unlock()
|
2014-03-08 02:09:49 +01:00
|
|
|
}
|
|
|
|
|
2017-10-23 01:50:16 +02:00
|
|
|
for _, member := range channel.Members() {
|
2017-09-29 09:25:58 +02:00
|
|
|
if member.capabilities.Has(caps.InviteNotify) && member != inviter && member != invitee && channel.ClientIsAtLeast(member, Halfop) {
|
2017-11-03 07:36:55 +01:00
|
|
|
member.Send(nil, inviter.NickMaskString(), "INVITE", invitee.Nick(), channel.name)
|
2016-10-16 06:14:55 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-11 15:51:46 +02:00
|
|
|
//TODO(dan): should inviter.server.name here be inviter.nickMaskString ?
|
|
|
|
inviter.Send(nil, inviter.server.name, RPL_INVITING, invitee.nick, channel.name)
|
|
|
|
invitee.Send(nil, inviter.nickMaskString, "INVITE", invitee.nick, channel.name)
|
2014-02-25 16:28:09 +01:00
|
|
|
if invitee.flags[Away] {
|
2016-10-11 15:51:46 +02:00
|
|
|
inviter.Send(nil, inviter.server.name, RPL_AWAY, invitee.nick, invitee.awayMessage)
|
2014-02-25 16:28:09 +01:00
|
|
|
}
|
|
|
|
}
|