2016-06-15 13:50:56 +02:00
|
|
|
// Copyright (c) 2012-2014 Jeremy Latt
|
|
|
|
// Copyright (c) 2014-2015 Edmund Huber
|
|
|
|
// Copyright (c) 2016- Daniel Oaks <daniel@danieloaks.net>
|
|
|
|
// released under the MIT license
|
|
|
|
|
2012-12-09 07:54:58 +01:00
|
|
|
package irc
|
|
|
|
|
2014-02-22 21:49:33 +01:00
|
|
|
import (
|
2016-10-11 15:51:46 +02:00
|
|
|
"fmt"
|
2014-02-27 06:52:17 +01:00
|
|
|
"log"
|
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
|
|
|
|
|
|
|
"github.com/DanielOaks/girc-go/ircmsg"
|
2014-02-22 21:49:33 +01:00
|
|
|
)
|
|
|
|
|
2012-12-09 07:54:58 +01:00
|
|
|
type Channel struct {
|
2016-10-11 15:51:46 +02:00
|
|
|
flags ChannelModeSet
|
|
|
|
lists map[ChannelMode]*UserMaskSet
|
|
|
|
key string
|
|
|
|
members MemberSet
|
|
|
|
name string
|
|
|
|
nameCasefolded string
|
|
|
|
server *Server
|
|
|
|
createdTime time.Time
|
|
|
|
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.
|
2016-10-11 15:51:46 +02:00
|
|
|
func NewChannel(s *Server, name string, addDefaultModes bool) *Channel {
|
|
|
|
casefoldedName, err := CasefoldChannel(name)
|
|
|
|
if err != nil {
|
|
|
|
log.Println(fmt.Sprintf("ERROR: Channel name is bad: [%s]", name), err.Error())
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2012-12-15 23:34:20 +01:00
|
|
|
channel := &Channel{
|
2014-02-23 00:01:11 +01:00
|
|
|
flags: make(ChannelModeSet),
|
2014-03-08 02:09:49 +01:00
|
|
|
lists: map[ChannelMode]*UserMaskSet{
|
|
|
|
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 {
|
|
|
|
for _, mode := range DefaultChannelModes {
|
|
|
|
channel.flags[mode] = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-26 05:17:26 +01:00
|
|
|
s.channels.Add(channel)
|
2014-02-25 20:11:34 +01:00
|
|
|
|
2012-12-15 23:34:20 +01:00
|
|
|
return channel
|
2012-12-09 07:54:58 +01:00
|
|
|
}
|
|
|
|
|
2013-05-12 20:20:55 +02:00
|
|
|
func (channel *Channel) IsEmpty() bool {
|
|
|
|
return len(channel.members) == 0
|
2012-12-10 06:46:22 +01:00
|
|
|
}
|
|
|
|
|
2014-02-18 06:02:03 +01:00
|
|
|
func (channel *Channel) Names(client *Client) {
|
2016-06-19 06:55:24 +02:00
|
|
|
currentNicks := channel.Nicks(client)
|
|
|
|
// 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.
|
|
|
|
func (channel *Channel) ClientIsAtLeast(client *Client, permission ChannelMode) bool {
|
|
|
|
// 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.
|
|
|
|
func (modes ChannelModeSet) Prefixes(isMultiPrefix bool) string {
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2014-03-01 04:21:33 +01:00
|
|
|
func (channel *Channel) Nicks(target *Client) []string {
|
|
|
|
isMultiPrefix := (target != nil) && target.capabilities[MultiPrefix]
|
2016-08-14 03:59:33 +02:00
|
|
|
isUserhostInNames := (target != nil) && target.capabilities[UserhostInNames]
|
2014-02-05 04:28:24 +01:00
|
|
|
nicks := make([]string, len(channel.members))
|
|
|
|
i := 0
|
2014-02-15 06:57:08 +01:00
|
|
|
for client, modes := range channel.members {
|
2016-04-14 01:35:36 +02:00
|
|
|
nicks[i] += modes.Prefixes(isMultiPrefix)
|
2016-08-14 03:59:33 +02:00
|
|
|
if isUserhostInNames {
|
|
|
|
nicks[i] += client.nickMaskString
|
|
|
|
} else {
|
2016-10-11 15:51:46 +02:00
|
|
|
nicks[i] += client.nick
|
2016-08-14 03:59:33 +02:00
|
|
|
}
|
2014-02-05 04:28:24 +01:00
|
|
|
i += 1
|
|
|
|
}
|
|
|
|
return nicks
|
|
|
|
}
|
|
|
|
|
2016-10-11 15:51:46 +02:00
|
|
|
func (channel *Channel) Id() string {
|
2012-12-17 04:13:53 +01:00
|
|
|
return channel.name
|
|
|
|
}
|
|
|
|
|
2016-10-11 15:51:46 +02:00
|
|
|
func (channel *Channel) Nick() string {
|
2013-06-03 07:07:50 +02:00
|
|
|
return channel.name
|
|
|
|
}
|
|
|
|
|
2014-02-09 03:14:39 +01:00
|
|
|
// <mode> <mode params>
|
2014-02-22 21:49:33 +01:00
|
|
|
func (channel *Channel) ModeString(client *Client) (str string) {
|
|
|
|
isMember := client.flags[Operator] || channel.members.Has(client)
|
|
|
|
showKey := isMember && (channel.key != "")
|
|
|
|
showUserLimit := channel.userLimit > 0
|
|
|
|
|
|
|
|
// flags with args
|
|
|
|
if showKey {
|
2014-02-15 06:57:08 +01:00
|
|
|
str += Key.String()
|
2014-02-09 08:33:56 +01:00
|
|
|
}
|
2014-02-22 21:49:33 +01:00
|
|
|
if showUserLimit {
|
|
|
|
str += UserLimit.String()
|
|
|
|
}
|
2014-02-15 06:57:08 +01:00
|
|
|
|
2014-02-22 21:49:33 +01:00
|
|
|
// flags
|
2014-02-15 06:57:08 +01:00
|
|
|
for mode := range channel.flags {
|
|
|
|
str += mode.String()
|
|
|
|
}
|
|
|
|
|
2015-12-02 17:05:05 +01:00
|
|
|
str = "+" + str
|
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 {
|
2016-06-19 06:55:24 +02:00
|
|
|
str += " " + channel.key
|
2014-02-15 06:57:08 +01:00
|
|
|
}
|
2014-02-22 21:49:33 +01:00
|
|
|
if showUserLimit {
|
|
|
|
str += " " + strconv.FormatUint(channel.userLimit, 10)
|
|
|
|
}
|
2014-02-15 06:57:08 +01:00
|
|
|
|
2016-06-22 13:35:26 +02:00
|
|
|
return str
|
2014-02-09 03:14:39 +01:00
|
|
|
}
|
|
|
|
|
2014-02-22 21:49:33 +01:00
|
|
|
func (channel *Channel) IsFull() bool {
|
|
|
|
return (channel.userLimit > 0) &&
|
|
|
|
(uint64(len(channel.members)) >= channel.userLimit)
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2016-06-19 06:55:24 +02:00
|
|
|
func (channel *Channel) Join(client *Client, key string) {
|
2014-02-22 21:49:33 +01:00
|
|
|
if channel.members.Has(client) {
|
|
|
|
// already joined, no message?
|
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
|
|
|
|
}
|
|
|
|
|
2014-03-08 02:09:49 +01:00
|
|
|
isInvited := channel.lists[InviteMask].Match(client.UserHost())
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
if channel.lists[BanMask].Match(client.UserHost()) &&
|
|
|
|
!isInvited &&
|
|
|
|
!channel.lists[ExceptMask].Match(client.UserHost()) {
|
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
|
|
|
|
}
|
|
|
|
|
2016-06-22 13:35:26 +02:00
|
|
|
for member := range channel.members {
|
2016-08-14 03:59:33 +02:00
|
|
|
if member.capabilities[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
|
|
|
}
|
|
|
|
|
2014-02-19 04:31:59 +01:00
|
|
|
client.channels.Add(channel)
|
2014-02-17 02:23:47 +01:00
|
|
|
channel.members.Add(client)
|
2016-10-14 13:04:04 +02:00
|
|
|
if len(channel.members) == 1 {
|
2016-06-26 13:06:28 +02:00
|
|
|
channel.createdTime = time.Now()
|
2016-04-14 01:35:36 +02:00
|
|
|
channel.members[client][ChannelFounder] = true
|
2014-02-17 02:23:47 +01:00
|
|
|
channel.members[client][ChannelOperator] = true
|
|
|
|
}
|
|
|
|
|
2016-08-14 03:59:33 +02:00
|
|
|
if client.capabilities[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
|
|
|
}
|
2016-06-22 13:35:26 +02:00
|
|
|
channel.GetTopic(client)
|
|
|
|
channel.Names(client)
|
2014-02-17 02:23:47 +01:00
|
|
|
}
|
|
|
|
|
2016-06-19 06:55:24 +02:00
|
|
|
func (channel *Channel) Part(client *Client, message string) {
|
2014-02-09 08:15:05 +01:00
|
|
|
if !channel.members.Has(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
|
|
|
|
}
|
|
|
|
|
2014-02-20 07:20:34 +01: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
|
|
|
}
|
2014-02-17 08:29:11 +01:00
|
|
|
channel.Quit(client)
|
2012-12-09 07:54:58 +01:00
|
|
|
}
|
|
|
|
|
2014-02-17 07:20:42 +01:00
|
|
|
func (channel *Channel) GetTopic(client *Client) {
|
2014-02-09 08:15:05 +01:00
|
|
|
if !channel.members.Has(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
|
|
|
|
}
|
|
|
|
|
2014-02-17 07:20:42 +01:00
|
|
|
if channel.topic == "" {
|
2016-10-11 15:51:46 +02:00
|
|
|
client.Send(nil, client.server.name, RPL_NOTOPIC, client.nick, channel.name, "No topic is set")
|
2012-12-09 07:54:58 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-10-11 15:51:46 +02:00
|
|
|
client.Send(nil, client.server.name, RPL_TOPIC, client.nick, channel.name, channel.topic)
|
|
|
|
client.Send(nil, client.server.name, RPL_TOPICTIME, client.nick, channel.name, channel.topicSetBy, strconv.FormatInt(channel.topicSetTime.Unix(), 10))
|
2014-02-17 07:20:42 +01:00
|
|
|
}
|
|
|
|
|
2016-06-19 06:55:24 +02:00
|
|
|
func (channel *Channel) SetTopic(client *Client, topic string) {
|
2014-02-22 21:49:33 +01:00
|
|
|
if !(client.flags[Operator] || channel.members.Has(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
|
|
|
|
}
|
|
|
|
|
2016-10-22 16:45:51 +02:00
|
|
|
if channel.flags[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
|
|
|
}
|
|
|
|
|
2014-02-17 07:20:42 +01:00
|
|
|
channel.topic = topic
|
2016-10-11 15:51:46 +02:00
|
|
|
channel.topicSetBy = client.nick
|
2016-06-26 13:06:28 +02:00
|
|
|
channel.topicSetTime = time.Now()
|
2014-02-21 05:47:05 +01:00
|
|
|
|
2014-02-20 07:20:34 +01:00
|
|
|
for member := range channel.members {
|
2016-10-11 15:51:46 +02:00
|
|
|
member.Send(nil, client.nickMaskString, "TOPIC", channel.name, channel.topic)
|
2014-02-20 07:20:34 +01:00
|
|
|
}
|
2012-12-15 23:34:20 +01:00
|
|
|
}
|
|
|
|
|
2014-02-22 21:49:33 +01:00
|
|
|
func (channel *Channel) CanSpeak(client *Client) bool {
|
|
|
|
if client.flags[Operator] {
|
|
|
|
return true
|
|
|
|
}
|
2014-02-15 06:57:08 +01:00
|
|
|
if channel.flags[NoOutside] && !channel.members.Has(client) {
|
2014-02-22 21:49:33 +01:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
if channel.flags[Moderated] && !(channel.members.HasMode(client, Voice) ||
|
|
|
|
channel.members.HasMode(client, ChannelOperator)) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2016-10-22 16:45:51 +02:00
|
|
|
// PrivMsg sends a private message to everyone in this channel.
|
|
|
|
func (channel *Channel) PrivMsg(minPrefix *ChannelMode, 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
|
|
|
|
}
|
2016-10-22 16:45:51 +02:00
|
|
|
// for STATUSMSG
|
|
|
|
var minPrefixMode ChannelMode
|
|
|
|
if minPrefix != nil {
|
|
|
|
minPrefixMode = *minPrefix
|
|
|
|
}
|
2014-02-20 07:20:34 +01:00
|
|
|
for member := range channel.members {
|
2016-10-22 16:45:51 +02:00
|
|
|
if minPrefix != nil && !channel.ClientIsAtLeast(member, minPrefixMode) {
|
|
|
|
// STATUSMSG
|
|
|
|
continue
|
|
|
|
}
|
2016-10-22 14:29:01 +02:00
|
|
|
if member == client && !client.capabilities[EchoMessage] {
|
2014-02-20 07:20:34 +01:00
|
|
|
continue
|
|
|
|
}
|
2016-10-16 04:54:15 +02:00
|
|
|
if member.capabilities[MessageTags] {
|
|
|
|
member.SendFromClient(client, clientOnlyTags, client.nickMaskString, "PRIVMSG", channel.name, message)
|
|
|
|
} else {
|
|
|
|
member.SendFromClient(client, nil, client.nickMaskString, "PRIVMSG", channel.name, message)
|
|
|
|
}
|
2014-02-20 07:20:34 +01:00
|
|
|
}
|
2012-12-09 07:54:58 +01:00
|
|
|
}
|
2014-02-09 07:06:10 +01:00
|
|
|
|
2014-02-23 00:01:11 +01:00
|
|
|
func (channel *Channel) applyModeFlag(client *Client, mode ChannelMode,
|
|
|
|
op ModeOp) bool {
|
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-02-23 00:01:11 +01:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
switch op {
|
|
|
|
case Add:
|
2014-02-26 07:16:23 +01:00
|
|
|
if channel.flags[mode] {
|
|
|
|
return false
|
|
|
|
}
|
2014-02-23 00:01:11 +01:00
|
|
|
channel.flags[mode] = true
|
|
|
|
return true
|
|
|
|
|
|
|
|
case Remove:
|
2014-02-26 07:16:23 +01:00
|
|
|
if !channel.flags[mode] {
|
|
|
|
return false
|
|
|
|
}
|
2014-02-23 00:01:11 +01:00
|
|
|
delete(channel.flags, mode)
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func (channel *Channel) applyModeMember(client *Client, mode ChannelMode,
|
2016-06-22 13:35:26 +02:00
|
|
|
op ModeOp, nick string) *ChannelModeChange {
|
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 {
|
|
|
|
client.Send(nil, client.server.name, ERR_NOSUCHNICK, nick, "No such nick")
|
2016-06-22 13:35:26 +02:00
|
|
|
return nil
|
2014-02-23 00:01:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if !channel.members.Has(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")
|
2016-06-22 13:35:26 +02:00
|
|
|
return nil
|
2014-02-23 00:01:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
switch op {
|
|
|
|
case Add:
|
2014-02-26 07:16:23 +01:00
|
|
|
if channel.members[target][mode] {
|
2016-06-22 13:35:26 +02:00
|
|
|
return nil
|
2014-02-26 07:16:23 +01:00
|
|
|
}
|
2014-02-23 00:01:11 +01:00
|
|
|
channel.members[target][mode] = true
|
2016-06-22 13:35:26 +02:00
|
|
|
return &ChannelModeChange{
|
|
|
|
op: Add,
|
|
|
|
mode: mode,
|
|
|
|
arg: nick,
|
|
|
|
}
|
2014-02-23 00:01:11 +01:00
|
|
|
|
|
|
|
case Remove:
|
2014-02-26 07:16:23 +01:00
|
|
|
if !channel.members[target][mode] {
|
2016-06-22 13:35:26 +02:00
|
|
|
return nil
|
2014-02-26 07:16:23 +01:00
|
|
|
}
|
2014-02-23 00:01:11 +01:00
|
|
|
channel.members[target][mode] = false
|
2016-06-22 13:35:26 +02:00
|
|
|
return &ChannelModeChange{
|
|
|
|
op: Remove,
|
|
|
|
mode: mode,
|
|
|
|
arg: nick,
|
|
|
|
}
|
2014-02-23 00:01:11 +01:00
|
|
|
}
|
2016-06-22 13:35:26 +02:00
|
|
|
return nil
|
2014-02-23 00:01:11 +01:00
|
|
|
}
|
|
|
|
|
2014-03-08 02:35:58 +01:00
|
|
|
func (channel *Channel) ShowMaskList(client *Client, mode ChannelMode) {
|
2016-06-19 06:55:24 +02:00
|
|
|
//TODO(dan): WE NEED TO fiX this PROPERLY
|
|
|
|
log.Fatal("Implement ShowMaskList")
|
|
|
|
/*
|
|
|
|
for lmask := range channel.lists[mode].masks {
|
|
|
|
client.RplMaskList(mode, channel, lmask)
|
|
|
|
}
|
|
|
|
client.RplEndOfMaskList(mode, channel)*/
|
2014-03-08 02:35:58 +01:00
|
|
|
}
|
2014-03-08 02:09:49 +01:00
|
|
|
|
2016-10-11 15:51:46 +02:00
|
|
|
func (channel *Channel) applyModeMask(client *Client, mode ChannelMode, 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
|
|
|
}
|
|
|
|
|
2016-06-19 06:55:24 +02:00
|
|
|
func (channel *Channel) Notice(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-12 02:11:59 +01:00
|
|
|
return
|
|
|
|
}
|
2014-02-20 07:20:34 +01:00
|
|
|
for member := range channel.members {
|
|
|
|
if member == client {
|
|
|
|
continue
|
|
|
|
}
|
2016-10-11 15:51:46 +02:00
|
|
|
member.SendFromClient(client, nil, client.nickMaskString, "NOTICE", channel.name, message)
|
2014-02-20 07:20:34 +01:00
|
|
|
}
|
2014-02-12 02:11:59 +01:00
|
|
|
}
|
2014-02-17 02:23:47 +01:00
|
|
|
|
2014-02-17 07:20:42 +01:00
|
|
|
func (channel *Channel) Quit(client *Client) {
|
|
|
|
channel.members.Remove(client)
|
2014-02-17 08:29:11 +01:00
|
|
|
client.channels.Remove(channel)
|
2014-02-28 03:33:03 +01:00
|
|
|
|
2016-10-14 13:04:04 +02:00
|
|
|
if channel.IsEmpty() {
|
2014-02-28 03:33:03 +01:00
|
|
|
channel.server.channels.Remove(channel)
|
|
|
|
}
|
2014-02-17 08:29:11 +01:00
|
|
|
}
|
|
|
|
|
2016-06-19 06:55:24 +02:00
|
|
|
func (channel *Channel) Kick(client *Client, target *Client, comment string) {
|
2014-02-22 21:49:33 +01:00
|
|
|
if !(client.flags[Operator] || channel.members.Has(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
|
|
|
|
}
|
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_CANNOTSENDTOCHAN, channel.name, "Cannot send to channel")
|
2014-02-17 08:29:11 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if !channel.members.Has(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
|
|
|
|
}
|
|
|
|
|
2016-09-12 04:40:09 +02:00
|
|
|
if len(comment) > client.server.limits.KickLen {
|
|
|
|
comment = comment[:client.server.limits.KickLen]
|
2016-09-12 04:22:50 +02:00
|
|
|
}
|
|
|
|
|
2014-02-20 07:20:34 +01:00
|
|
|
for member := range channel.members {
|
2016-10-11 15:51:46 +02:00
|
|
|
member.Send(nil, client.nickMaskString, "KICK", channel.name, target.nick, comment)
|
2014-02-20 07:20:34 +01:00
|
|
|
}
|
2014-02-17 08:29:11 +01:00
|
|
|
channel.Quit(target)
|
2014-02-17 02:23:47 +01:00
|
|
|
}
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
if !channel.members.Has(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] {
|
|
|
|
channel.lists[InviteMask].Add(invitee.UserHost())
|
|
|
|
}
|
|
|
|
|
2016-10-16 06:14:55 +02:00
|
|
|
// send invite-notify
|
|
|
|
for member := range channel.members {
|
2016-10-22 16:45:51 +02:00
|
|
|
if member.capabilities[InviteNotify] && member != inviter && member != invitee && channel.ClientIsAtLeast(member, Halfop) {
|
2016-10-16 06:14:55 +02:00
|
|
|
member.Send(nil, inviter.nickMaskString, "INVITE", invitee.nick, channel.name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|