3
0
mirror of https://github.com/ergochat/ergo.git synced 2024-11-10 22:19:31 +01:00
ergo/irc/channel.go

602 lines
16 KiB
Go
Raw Normal View History

// 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
package irc
import (
2016-06-19 06:55:24 +02:00
"fmt"
2014-02-27 06:52:17 +01:00
"log"
"strconv"
)
type Channel struct {
2016-06-19 06:55:24 +02:00
flags ChannelModeSet
lists map[ChannelMode]*UserMaskSet
key string
members MemberSet
name Name
nameString string
server *Server
topic string
userLimit uint64
}
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-04-21 11:29:50 +02:00
func NewChannel(s *Server, name Name, addDefaultModes bool) *Channel {
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-06-19 06:55:24 +02:00
members: make(MemberSet),
name: name,
nameString: name.String(),
server: s,
2012-12-09 21:51:50 +01:00
}
2016-04-21 11:29:50 +02:00
if addDefaultModes {
for _, mode := range DefaultChannelModes {
channel.flags[mode] = true
}
}
s.channels.Add(channel)
return channel
}
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
maxNamLen := 480 - len(client.server.name) - len(client.nickString)
var buffer string
for _, nick := range currentNicks {
if buffer == "" {
buffer += nick
continue
}
if len(buffer)+1+len(nick) > maxNamLen {
client.Send(nil, client.server.name, RPL_NAMREPLY, "=", channel.nameString, buffer)
buffer = nick
continue
}
buffer += " "
buffer += nick
}
client.Send(nil, client.server.name, RPL_NAMREPLY, "=", channel.nameString, buffer)
client.Send(nil, client.server.name, RPL_ENDOFNAMES, channel.nameString, "End of NAMES list")
2013-06-03 07:07:50 +02:00
}
func (channel *Channel) ClientIsOperator(client *Client) bool {
2014-02-17 22:22:35 +01:00
return client.flags[Operator] || channel.members.HasMode(client, ChannelOperator)
}
// 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
}
func (channel *Channel) Nicks(target *Client) []string {
isMultiPrefix := (target != nil) && target.capabilities[MultiPrefix]
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 {
nicks[i] += modes.Prefixes(isMultiPrefix)
2014-03-09 21:45:36 +01:00
nicks[i] += client.Nick().String()
2014-02-05 04:28:24 +01:00
i += 1
}
return nicks
}
2014-03-09 21:45:36 +01:00
func (channel *Channel) Id() Name {
return channel.name
}
2014-03-09 21:45:36 +01:00
func (channel *Channel) Nick() Name {
2013-06-03 07:07:50 +02:00
return channel.name
}
func (channel *Channel) String() string {
2014-03-09 21:45:36 +01:00
return channel.Id().String()
}
2014-02-09 03:14:39 +01:00
// <mode> <mode params>
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
}
if showUserLimit {
str += UserLimit.String()
}
2014-02-15 06:57:08 +01:00
// flags
2014-02-15 06:57:08 +01:00
for mode := range channel.flags {
str += mode.String()
}
str = "+" + str
2014-02-15 06:57:08 +01:00
// args for flags with args: The order must match above to keep
// positional arguments in place.
if showKey {
2016-06-19 06:55:24 +02:00
str += " " + channel.key
2014-02-15 06:57:08 +01:00
}
if showUserLimit {
str += " " + strconv.FormatUint(channel.userLimit, 10)
}
2014-02-15 06:57:08 +01:00
2014-02-09 08:33:56 +01:00
return
2014-02-09 03:14:39 +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 {
return (channel.key == "") || (channel.key == key)
}
2016-06-19 06:55:24 +02:00
func (channel *Channel) Join(client *Client, key string) {
if channel.members.Has(client) {
// already joined, no message?
return
}
if channel.IsFull() {
2016-06-19 06:55:24 +02:00
client.Send(nil, client.server.name, ERR_CHANNELISFULL, channel.nameString, "Cannot join channel (+l)")
return
}
if !channel.CheckKey(key) {
2016-06-19 06:55:24 +02:00
client.Send(nil, client.server.name, ERR_BADCHANNELKEY, channel.nameString, "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-06-19 06:55:24 +02:00
client.Send(nil, client.server.name, ERR_INVITEONLYCHAN, channel.nameString, "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-06-19 06:55:24 +02:00
client.Send(nil, client.server.name, ERR_BANNEDFROMCHAN, channel.nameString, "Cannot join channel (+b)")
2014-03-08 02:09:49 +01:00
return
}
2014-02-19 04:31:59 +01:00
client.channels.Add(channel)
channel.members.Add(client)
if !channel.flags[Persistent] && (len(channel.members) == 1) {
channel.members[client][ChannelFounder] = true
channel.members[client][ChannelOperator] = true
}
2016-06-19 06:55:24 +02:00
client.Send(nil, client.nickMaskString, "JOIN", channel.nameString)
return
//TODO(dan): should we be continuing here????
// return was above this originally, is it required?
/*
for member := range channel.members {
member.Reply(reply)
}
channel.GetTopic(client)
channel.Names(client)
*/
}
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-06-19 06:55:24 +02:00
client.Send(nil, client.server.name, ERR_NOTONCHANNEL, channel.nameString, "You're not on that channel")
return
}
for member := range channel.members {
2016-06-19 06:55:24 +02:00
member.Send(nil, client.nickMaskString, "PART", channel.nameString, message)
}
2014-02-17 08:29:11 +01:00
channel.Quit(client)
}
func (channel *Channel) GetTopic(client *Client) {
2014-02-09 08:15:05 +01:00
if !channel.members.Has(client) {
2016-06-19 06:55:24 +02:00
client.Send(nil, client.server.name, ERR_NOTONCHANNEL, channel.nameString, "You're not on that channel")
return
}
if channel.topic == "" {
// clients appear not to expect this
//replier.Reply(RplNoTopic(channel))
return
}
2016-06-19 06:55:24 +02:00
client.Send(nil, client.server.name, RPL_TOPIC, channel.nameString, channel.topic)
}
2016-06-19 06:55:24 +02:00
func (channel *Channel) SetTopic(client *Client, topic string) {
if !(client.flags[Operator] || channel.members.Has(client)) {
2016-06-19 06:55:24 +02:00
client.Send(nil, client.server.name, ERR_NOTONCHANNEL, channel.nameString, "You're not on that channel")
return
}
2014-02-21 05:47:05 +01:00
if channel.flags[OpOnlyTopic] && !channel.ClientIsOperator(client) {
2016-06-19 06:55:24 +02:00
client.Send(nil, client.server.name, ERR_CHANOPRIVSNEEDED, channel.nameString, "You're not a channel operator")
2014-02-16 04:56:38 +01:00
return
}
channel.topic = topic
2014-02-21 05:47:05 +01:00
for member := range channel.members {
2016-06-19 06:55:24 +02:00
member.Send(nil, client.nickMaskString, "TOPIC", channel.nameString, channel.topic)
}
2014-02-26 00:57:35 +01:00
2014-02-27 06:52:17 +01:00
if err := channel.Persist(); err != nil {
2014-03-08 02:09:49 +01:00
log.Println("Channel.Persist:", channel, err)
2014-02-27 06:52:17 +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) {
return false
}
if channel.flags[Moderated] && !(channel.members.HasMode(client, Voice) ||
channel.members.HasMode(client, ChannelOperator)) {
return false
}
return true
}
2016-06-19 06:55:24 +02:00
func (channel *Channel) PrivMsg(client *Client, message string) {
if !channel.CanSpeak(client) {
2016-06-19 06:55:24 +02:00
client.Send(nil, client.server.name, ERR_CANNOTSENDTOCHAN, channel.nameString, "Cannot send to channel")
2014-02-09 08:33:56 +01:00
return
}
for member := range channel.members {
if member == client {
continue
}
2016-06-19 06:55:24 +02:00
//TODO(dan): use nickmask instead of nickString here lel
member.Send(nil, client.nickMaskString, "PRIVMSG", channel.nameString, message)
}
}
2014-02-23 00:01:11 +01:00
func (channel *Channel) applyModeFlag(client *Client, mode ChannelMode,
op ModeOp) bool {
if !channel.ClientIsOperator(client) {
2016-06-19 06:55:24 +02:00
client.Send(nil, client.server.name, ERR_CHANOPRIVSNEEDED, channel.nameString, "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-19 06:55:24 +02:00
op ModeOp, nick string) bool {
2014-02-23 00:01:11 +01:00
if !channel.ClientIsOperator(client) {
2016-06-19 06:55:24 +02:00
client.Send(nil, client.server.name, ERR_CHANOPRIVSNEEDED, channel.nameString, "You're not a channel operator")
2014-02-23 00:01:11 +01:00
return false
}
if nick == "" {
2016-06-19 06:55:24 +02:00
//TODO(dan): shouldn't this be handled before it reaches this function?
client.Send(nil, client.server.name, ERR_NEEDMOREPARAMS, "MODE", "Not enough parameters")
2014-02-23 00:01:11 +01:00
return false
}
2016-06-19 06:55:24 +02:00
target := channel.server.clients.Get(Name(nick))
2014-02-23 00:01:11 +01:00
if target == nil {
2016-06-19 06:55:24 +02:00
//TODO(dan): investigate using NOSUCHNICK and NOSUCHCHANNEL specifically as that other IRCd (insp?) does,
// since I think that would make sense
client.Send(nil, client.server.name, ERR_NOSUCHNICK, nick, "No such nick/channel")
2014-02-23 00:01:11 +01:00
return false
}
if !channel.members.Has(target) {
2016-06-19 06:55:24 +02:00
client.Send(nil, client.server.name, ERR_USERNOTINCHANNEL, client.nickString, channel.nameString, "They aren't on that channel")
2014-02-23 00:01:11 +01:00
return false
}
switch op {
case Add:
2014-02-26 07:16:23 +01:00
if channel.members[target][mode] {
return false
}
2014-02-23 00:01:11 +01:00
channel.members[target][mode] = true
return true
case Remove:
2014-02-26 07:16:23 +01:00
if !channel.members[target][mode] {
return false
}
2014-02-23 00:01:11 +01:00
channel.members[target][mode] = false
return true
}
return false
}
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
2014-03-08 02:35:58 +01:00
func (channel *Channel) applyModeMask(client *Client, mode ChannelMode, op ModeOp,
2014-03-09 21:45:36 +01:00
mask Name) 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
}
if !channel.ClientIsOperator(client) {
2016-06-19 06:55:24 +02:00
client.Send(nil, client.server.name, ERR_CHANOPRIVSNEEDED, channel.nameString, "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
}
func (channel *Channel) applyMode(client *Client, change *ChannelModeChange) bool {
switch change.mode {
2014-02-23 00:01:11 +01:00
case BanMask, ExceptMask, InviteMask:
2014-03-09 21:45:36 +01:00
return channel.applyModeMask(client, change.mode, change.op,
NewName(change.arg))
case InviteOnly, Moderated, NoOutside, OpOnlyTopic, Persistent, Secret:
2014-02-23 00:01:11 +01:00
return channel.applyModeFlag(client, change.mode, change.op)
2014-02-15 06:57:08 +01:00
case Key:
if !channel.ClientIsOperator(client) {
2016-06-19 06:55:24 +02:00
client.Send(nil, client.server.name, ERR_CHANOPRIVSNEEDED, channel.nameString, "You're not a channel operator")
return false
}
2014-02-15 06:57:08 +01:00
switch change.op {
case Add:
if change.arg == "" {
2016-06-19 06:55:24 +02:00
client.Send(nil, client.server.name, ERR_NEEDMOREPARAMS, "MODE", "Not enough parameters")
return false
2014-02-15 06:57:08 +01:00
}
2016-06-19 06:55:24 +02:00
key := change.arg
2014-03-09 21:45:36 +01:00
if key == channel.key {
2014-02-26 07:16:23 +01:00
return false
}
2014-02-15 06:57:08 +01:00
2014-03-09 21:45:36 +01:00
channel.key = key
return true
2014-02-15 06:57:08 +01:00
case Remove:
channel.key = ""
return true
}
2014-02-15 06:57:08 +01:00
case UserLimit:
limit, err := strconv.ParseUint(change.arg, 10, 64)
if err != nil {
2016-06-19 06:55:24 +02:00
client.Send(nil, client.server.name, ERR_NEEDMOREPARAMS, "MODE", "Not enough parameters")
return false
}
2014-02-26 07:16:23 +01:00
if (limit == 0) || (limit == channel.userLimit) {
return false
}
2014-02-15 06:57:08 +01:00
channel.userLimit = limit
return true
2014-02-15 06:57:08 +01:00
case ChannelFounder, ChannelAdmin, ChannelOperator, Halfop, Voice:
var hasPrivs bool
// make sure client has privs to edit the given prefix
for _, mode := range ChannelPrivModes {
if channel.members[client][mode] {
hasPrivs = true
// Admins can't give other people Admin or remove it from others,
// standard for that channel mode, we worry about this later
if mode == ChannelAdmin && change.mode == ChannelAdmin {
hasPrivs = false
}
break
} else if mode == change.mode {
break
}
}
name := NewName(change.arg)
if !hasPrivs {
if change.op == Remove && name.ToLower() == client.nick.ToLower() {
// success!
} else {
2016-06-19 06:55:24 +02:00
client.Send(nil, client.server.name, ERR_CHANOPRIVSNEEDED, channel.nameString, "You're not a channel operator")
return false
}
}
2016-06-19 06:55:24 +02:00
return channel.applyModeMember(client, change.mode, change.op, name.String())
2014-02-22 21:15:34 +01:00
default:
2016-06-19 06:55:24 +02:00
client.Send(nil, client.server.name, ERR_UNKNOWNMODE, change.mode.String(), fmt.Sprintf(":is an unknown mode char to me for %s", channel))
}
return false
}
func (channel *Channel) Mode(client *Client, changes ChannelModeChanges) {
if len(changes) == 0 {
2016-06-19 06:55:24 +02:00
client.Send(nil, client.server.name, RPL_CHANNELMODEIS, channel.nameString, channel.ModeString(client))
return
}
applied := make(ChannelModeChanges, 0)
for _, change := range changes {
if channel.applyMode(client, change) {
applied = append(applied, change)
}
}
if len(applied) > 0 {
2016-06-19 06:55:24 +02:00
appliedString := applied.String()
for member := range channel.members {
2016-06-19 06:55:24 +02:00
member.Send(nil, client.nickMaskString, "MODE", channel.nameString, appliedString)
}
2014-02-27 06:52:17 +01:00
if err := channel.Persist(); err != nil {
2014-03-08 02:09:49 +01:00
log.Println("Channel.Persist:", channel, err)
2014-02-27 06:52:17 +01:00
}
2014-02-26 00:57:35 +01:00
}
}
2014-02-27 06:52:17 +01:00
func (channel *Channel) Persist() (err error) {
2014-02-26 00:57:35 +01:00
if channel.flags[Persistent] {
2014-02-27 06:52:17 +01:00
_, err = channel.server.db.Exec(`
2014-02-26 00:57:35 +01:00
INSERT OR REPLACE INTO channel
2014-03-08 03:14:02 +01:00
(name, flags, key, topic, user_limit, ban_list, except_list,
invite_list)
VALUES (?, ?, ?, ?, ?, ?, ?, ?)`,
2016-06-19 06:55:24 +02:00
channel.name.String(), channel.flags.String(), channel.key,
channel.topic, channel.userLimit, channel.lists[BanMask].String(),
2014-03-08 03:14:02 +01:00
channel.lists[ExceptMask].String(), channel.lists[InviteMask].String())
2014-02-26 00:57:35 +01:00
} else {
2014-02-27 06:52:17 +01:00
_, err = channel.server.db.Exec(`
2014-03-16 03:18:57 +01:00
DELETE FROM channel WHERE name = ?`, channel.name.String())
}
2014-02-27 06:52:17 +01:00
return
}
2014-02-12 02:11:59 +01:00
2016-06-19 06:55:24 +02:00
func (channel *Channel) Notice(client *Client, message string) {
if !channel.CanSpeak(client) {
2016-06-19 06:55:24 +02:00
client.Send(nil, client.server.name, ERR_CANNOTSENDTOCHAN, channel.nameString, "Cannot send to channel")
2014-02-12 02:11:59 +01:00
return
}
for member := range channel.members {
if member == client {
continue
}
2016-06-19 06:55:24 +02:00
member.Send(nil, client.nickMaskString, "NOTICE", channel.nameString, message)
}
2014-02-12 02:11:59 +01:00
}
func (channel *Channel) Quit(client *Client) {
channel.members.Remove(client)
2014-02-17 08:29:11 +01:00
client.channels.Remove(channel)
if !channel.flags[Persistent] && channel.IsEmpty() {
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) {
if !(client.flags[Operator] || channel.members.Has(client)) {
2016-06-19 06:55:24 +02:00
client.Send(nil, client.server.name, ERR_NOTONCHANNEL, channel.nameString, "You're not on that channel")
2014-02-17 08:29:11 +01:00
return
}
if !channel.ClientIsOperator(client) {
2016-06-19 06:55:24 +02:00
client.Send(nil, client.server.name, ERR_CANNOTSENDTOCHAN, channel.nameString, "Cannot send to channel")
2014-02-17 08:29:11 +01:00
return
}
if !channel.members.Has(target) {
2016-06-19 06:55:24 +02:00
client.Send(nil, client.server.name, ERR_USERNOTINCHANNEL, client.nickString, channel.nameString, "They aren't on that channel")
2014-02-17 08:29:11 +01:00
return
}
for member := range channel.members {
2016-06-19 06:55:24 +02:00
member.Send(nil, client.nickMaskString, "KICK", channel.nameString, target.nickString, comment)
}
2014-02-17 08:29:11 +01:00
channel.Quit(target)
}
2014-02-25 16:28:09 +01:00
func (channel *Channel) Invite(invitee *Client, inviter *Client) {
if channel.flags[InviteOnly] && !channel.ClientIsOperator(inviter) {
2016-06-19 06:55:24 +02:00
inviter.Send(nil, inviter.server.name, ERR_CHANOPRIVSNEEDED, channel.nameString, "You're not a channel operator")
2014-02-25 16:28:09 +01:00
return
}
if !channel.members.Has(inviter) {
2016-06-19 06:55:24 +02:00
inviter.Send(nil, inviter.server.name, ERR_NOTONCHANNEL, channel.nameString, "You're not on that channel")
2014-02-25 16:28:09 +01:00
return
}
2014-03-08 02:09:49 +01:00
if channel.flags[InviteOnly] {
channel.lists[InviteMask].Add(invitee.UserHost())
if err := channel.Persist(); err != nil {
log.Println("Channel.Persist:", channel, err)
}
}
2016-06-19 06:55:24 +02:00
//TODO(dan): should inviter.server.name here be inviter.nickMaskString ?
inviter.Send(nil, inviter.server.name, RPL_INVITING, invitee.nickString, channel.nameString)
invitee.Send(nil, inviter.nickMaskString, "INVITE", invitee.nickString, channel.nameString)
2014-02-25 16:28:09 +01:00
if invitee.flags[Away] {
2016-06-19 06:55:24 +02:00
inviter.Send(nil, inviter.server.name, RPL_AWAY, invitee.nickString, invitee.awayMessage)
2014-02-25 16:28:09 +01:00
}
}