2012-12-09 07:54:58 +01:00
|
|
|
package irc
|
|
|
|
|
2012-12-17 04:13:53 +01:00
|
|
|
import (
|
|
|
|
"log"
|
|
|
|
)
|
|
|
|
|
2012-12-09 07:54:58 +01:00
|
|
|
type Channel struct {
|
2014-02-17 07:20:42 +01:00
|
|
|
banList []UserMask
|
|
|
|
flags ChannelModeSet
|
|
|
|
key string
|
|
|
|
members MemberSet
|
|
|
|
name string
|
|
|
|
server *Server
|
|
|
|
topic string
|
2012-12-09 07:54:58 +01:00
|
|
|
}
|
|
|
|
|
2014-02-09 03:14:39 +01:00
|
|
|
func IsChannel(target string) bool {
|
2014-02-18 02:58:22 +01:00
|
|
|
return ChannelNameExpr.MatchString(target)
|
2014-02-09 03:14:39 +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.
|
2012-12-09 07:54:58 +01:00
|
|
|
func NewChannel(s *Server, name string) *Channel {
|
2012-12-15 23:34:20 +01:00
|
|
|
channel := &Channel{
|
2014-02-17 07:20:42 +01:00
|
|
|
banList: make([]UserMask, 0),
|
|
|
|
flags: make(ChannelModeSet),
|
|
|
|
members: make(MemberSet),
|
|
|
|
name: name,
|
|
|
|
server: s,
|
2012-12-09 21:51:50 +01:00
|
|
|
}
|
2012-12-15 23:34:20 +01:00
|
|
|
return channel
|
2012-12-09 07:54:58 +01:00
|
|
|
}
|
|
|
|
|
2014-02-15 04:35:25 +01:00
|
|
|
func (channel *Channel) Reply(reply Reply) {
|
2014-02-17 07:20:42 +01:00
|
|
|
if DEBUG_CHANNEL {
|
|
|
|
log.Printf("%s ← %s %s", channel, reply.Source(), reply)
|
2013-05-26 22:28:22 +02:00
|
|
|
}
|
|
|
|
|
2014-02-17 07:20:42 +01:00
|
|
|
for client := range channel.members {
|
|
|
|
if (reply.Code() == ReplyCode(PRIVMSG)) &&
|
|
|
|
(reply.Source() == Identifier(client)) {
|
|
|
|
continue
|
2014-02-17 02:23:47 +01:00
|
|
|
}
|
2014-02-17 07:20:42 +01:00
|
|
|
client.Reply(reply)
|
2012-12-09 07:54:58 +01:00
|
|
|
}
|
|
|
|
}
|
2012-12-09 21:51:50 +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) {
|
|
|
|
client.Reply(NewNamesReply(channel))
|
2013-06-03 07:07:50 +02:00
|
|
|
}
|
|
|
|
|
2014-02-10 04:59:59 +01: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)
|
2014-02-10 04:59:59 +01:00
|
|
|
}
|
|
|
|
|
2014-02-05 04:28:24 +01:00
|
|
|
func (channel *Channel) Nicks() []string {
|
|
|
|
nicks := make([]string, len(channel.members))
|
|
|
|
i := 0
|
2014-02-15 06:57:08 +01:00
|
|
|
for client, modes := range channel.members {
|
|
|
|
switch {
|
|
|
|
case modes[ChannelOperator]:
|
|
|
|
nicks[i] = "@" + client.Nick()
|
|
|
|
case modes[Voice]:
|
|
|
|
nicks[i] = "+" + client.Nick()
|
|
|
|
default:
|
|
|
|
nicks[i] = client.Nick()
|
|
|
|
}
|
2014-02-05 04:28:24 +01:00
|
|
|
i += 1
|
|
|
|
}
|
|
|
|
return nicks
|
|
|
|
}
|
|
|
|
|
2013-05-12 03:28:18 +02:00
|
|
|
func (channel *Channel) Id() string {
|
2012-12-17 04:13:53 +01:00
|
|
|
return channel.name
|
|
|
|
}
|
|
|
|
|
2013-06-03 07:07:50 +02:00
|
|
|
func (channel *Channel) Nick() string {
|
|
|
|
return channel.name
|
|
|
|
}
|
|
|
|
|
2013-05-12 03:28:18 +02:00
|
|
|
func (channel *Channel) String() string {
|
|
|
|
return channel.Id()
|
|
|
|
}
|
|
|
|
|
2014-02-09 03:14:39 +01:00
|
|
|
// <mode> <mode params>
|
2014-02-09 08:33:56 +01:00
|
|
|
func (channel *Channel) ModeString() (str string) {
|
2014-02-15 06:57:08 +01:00
|
|
|
if channel.key != "" {
|
|
|
|
str += Key.String()
|
2014-02-09 08:33:56 +01:00
|
|
|
}
|
2014-02-15 06:57:08 +01:00
|
|
|
|
|
|
|
for mode := range channel.flags {
|
|
|
|
str += mode.String()
|
|
|
|
}
|
|
|
|
|
2014-02-09 17:53:06 +01:00
|
|
|
if len(str) > 0 {
|
|
|
|
str = "+" + str
|
|
|
|
}
|
2014-02-15 06:57:08 +01:00
|
|
|
|
|
|
|
if channel.key != "" {
|
|
|
|
str += " " + channel.key
|
|
|
|
}
|
|
|
|
|
2014-02-09 08:33:56 +01:00
|
|
|
return
|
2014-02-09 03:14:39 +01:00
|
|
|
}
|
|
|
|
|
2014-02-17 07:20:42 +01:00
|
|
|
func (channel *Channel) Join(client *Client, key string) {
|
|
|
|
if (channel.key != "") && (channel.key != key) {
|
2014-02-09 17:48:11 +01:00
|
|
|
client.Reply(ErrBadChannelKey(channel))
|
2012-12-09 07:54:58 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2014-02-17 02:23:47 +01:00
|
|
|
channel.members.Add(client)
|
|
|
|
if len(channel.members) == 1 {
|
|
|
|
channel.members[client][ChannelCreator] = true
|
|
|
|
channel.members[client][ChannelOperator] = true
|
|
|
|
}
|
|
|
|
|
2014-02-17 07:20:42 +01:00
|
|
|
client.channels.Add(channel)
|
2014-02-17 02:23:47 +01:00
|
|
|
|
|
|
|
for member := range channel.members {
|
2014-02-17 07:20:42 +01:00
|
|
|
client.AddFriend(member)
|
|
|
|
member.AddFriend(client)
|
2014-02-17 02:23:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
channel.Reply(RplJoin(client, channel))
|
|
|
|
channel.GetTopic(client)
|
2014-02-18 06:02:03 +01:00
|
|
|
channel.Names(client)
|
2014-02-17 02:23:47 +01:00
|
|
|
}
|
|
|
|
|
2014-02-17 07:20:42 +01:00
|
|
|
func (channel *Channel) Part(client *Client, message string) {
|
2014-02-09 08:15:05 +01:00
|
|
|
if !channel.members.Has(client) {
|
2014-02-09 17:48:11 +01:00
|
|
|
client.Reply(ErrNotOnChannel(channel))
|
2012-12-09 07:54:58 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2014-02-17 07:20:42 +01:00
|
|
|
channel.Reply(RplPart(client, channel, message))
|
2014-02-17 08:29:11 +01:00
|
|
|
channel.Quit(client)
|
2012-12-13 08:27:17 +01:00
|
|
|
|
2014-02-09 02:53:06 +01:00
|
|
|
if channel.IsEmpty() {
|
2014-02-17 07:20:42 +01:00
|
|
|
channel.server.channels.Remove(channel)
|
2012-12-13 08:27:17 +01:00
|
|
|
}
|
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) {
|
2014-02-09 17:48:11 +01:00
|
|
|
client.Reply(ErrNotOnChannel(channel))
|
2012-12-09 07:54:58 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2014-02-17 07:20:42 +01:00
|
|
|
if channel.topic == "" {
|
|
|
|
// clients appear not to expect this
|
|
|
|
//replier.Reply(RplNoTopic(channel))
|
2012-12-09 07:54:58 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2014-02-17 07:20:42 +01:00
|
|
|
client.Reply(RplTopic(channel))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (channel *Channel) SetTopic(client *Client, topic string) {
|
|
|
|
if !channel.members.Has(client) {
|
|
|
|
client.Reply(ErrNotOnChannel(channel))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if channel.flags[OpOnlyTopic] && !channel.members[client][ChannelOperator] {
|
2014-02-16 04:56:38 +01:00
|
|
|
client.Reply(ErrChanOPrivIsNeeded(channel))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2014-02-17 07:20:42 +01:00
|
|
|
channel.topic = topic
|
2014-02-16 04:56:38 +01:00
|
|
|
channel.Reply(RplTopicMsg(client, channel))
|
2012-12-15 23:34:20 +01:00
|
|
|
}
|
|
|
|
|
2014-02-17 07:20:42 +01:00
|
|
|
func (channel *Channel) PrivMsg(client *Client, message string) {
|
2014-02-15 06:57:08 +01:00
|
|
|
if channel.flags[NoOutside] && !channel.members.Has(client) {
|
2014-02-09 17:48:11 +01:00
|
|
|
client.Reply(ErrCannotSendToChan(channel))
|
2014-02-09 08:33:56 +01:00
|
|
|
return
|
|
|
|
}
|
2014-02-17 07:20:42 +01:00
|
|
|
channel.Reply(RplPrivMsg(client, channel, message))
|
2012-12-09 07:54:58 +01:00
|
|
|
}
|
2014-02-09 07:06:10 +01:00
|
|
|
|
2014-02-17 07:20:42 +01:00
|
|
|
func (channel *Channel) Mode(client *Client, changes ChannelModeChanges) {
|
|
|
|
if len(changes) == 0 {
|
2014-02-16 04:49:20 +01:00
|
|
|
client.Reply(RplChannelModeIs(channel))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2014-02-17 07:20:42 +01:00
|
|
|
applied := make(ChannelModeChanges, 0)
|
2014-02-16 04:49:20 +01:00
|
|
|
|
2014-02-17 07:20:42 +01:00
|
|
|
for _, change := range changes {
|
2014-02-16 04:49:20 +01:00
|
|
|
switch change.mode {
|
2014-02-09 07:06:10 +01:00
|
|
|
case BanMask:
|
|
|
|
// TODO add/remove
|
2014-02-16 04:49:20 +01:00
|
|
|
|
2014-02-09 07:06:10 +01:00
|
|
|
for _, banMask := range channel.banList {
|
2014-02-09 17:48:11 +01:00
|
|
|
client.Reply(RplBanList(channel, banMask))
|
2014-02-09 07:06:10 +01:00
|
|
|
}
|
2014-02-09 17:48:11 +01:00
|
|
|
client.Reply(RplEndOfBanList(channel))
|
2014-02-15 06:57:08 +01:00
|
|
|
|
|
|
|
case NoOutside, Private, Secret, OpOnlyTopic:
|
|
|
|
if !channel.ClientIsOperator(client) {
|
|
|
|
client.Reply(ErrChanOPrivIsNeeded(channel))
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2014-02-16 04:49:20 +01:00
|
|
|
switch change.op {
|
2014-02-15 06:57:08 +01:00
|
|
|
case Add:
|
2014-02-16 04:49:20 +01:00
|
|
|
channel.flags[change.mode] = true
|
2014-02-17 07:20:42 +01:00
|
|
|
applied = append(applied, change)
|
2014-02-15 06:57:08 +01:00
|
|
|
|
|
|
|
case Remove:
|
2014-02-16 04:49:20 +01:00
|
|
|
delete(channel.flags, change.mode)
|
2014-02-17 07:20:42 +01:00
|
|
|
applied = append(applied, change)
|
2014-02-15 06:57:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
case Key:
|
|
|
|
if !channel.ClientIsOperator(client) {
|
|
|
|
client.Reply(ErrChanOPrivIsNeeded(channel))
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2014-02-16 04:49:20 +01:00
|
|
|
switch change.op {
|
2014-02-15 06:57:08 +01:00
|
|
|
case Add:
|
2014-02-16 04:49:20 +01:00
|
|
|
if change.arg == "" {
|
2014-02-15 06:57:08 +01:00
|
|
|
// TODO err reply
|
|
|
|
continue
|
2014-02-10 04:59:59 +01:00
|
|
|
}
|
2014-02-15 06:57:08 +01:00
|
|
|
|
2014-02-16 04:49:20 +01:00
|
|
|
channel.key = change.arg
|
2014-02-17 07:20:42 +01:00
|
|
|
applied = append(applied, change)
|
2014-02-15 06:57:08 +01:00
|
|
|
|
|
|
|
case Remove:
|
|
|
|
channel.key = ""
|
2014-02-17 07:20:42 +01:00
|
|
|
applied = append(applied, change)
|
2014-02-15 06:57:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
case ChannelOperator, Voice:
|
|
|
|
if !channel.ClientIsOperator(client) {
|
2014-02-10 04:59:59 +01:00
|
|
|
client.Reply(ErrChanOPrivIsNeeded(channel))
|
2014-02-15 06:57:08 +01:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2014-02-16 04:49:20 +01:00
|
|
|
if change.arg == "" {
|
2014-02-15 06:57:08 +01:00
|
|
|
// TODO err reply
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2014-02-16 04:49:20 +01:00
|
|
|
target := channel.server.clients[change.arg]
|
2014-02-15 06:57:08 +01:00
|
|
|
if target == nil {
|
|
|
|
// TODO err reply
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if channel.members[target] == nil {
|
|
|
|
// TODO err reply
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2014-02-16 04:49:20 +01:00
|
|
|
switch change.op {
|
2014-02-15 06:57:08 +01:00
|
|
|
case Add:
|
2014-02-16 04:49:20 +01:00
|
|
|
channel.members[target][change.mode] = true
|
2014-02-17 07:20:42 +01:00
|
|
|
applied = append(applied, change)
|
2014-02-16 04:49:20 +01:00
|
|
|
|
2014-02-15 06:57:08 +01:00
|
|
|
case Remove:
|
2014-02-16 04:49:20 +01:00
|
|
|
channel.members[target][change.mode] = false
|
2014-02-17 07:20:42 +01:00
|
|
|
applied = append(applied, change)
|
2014-02-09 08:33:56 +01:00
|
|
|
}
|
2014-02-09 07:06:10 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-17 07:20:42 +01:00
|
|
|
if len(applied) > 0 {
|
|
|
|
channel.Reply(RplChannelMode(client, channel, applied))
|
2014-02-16 04:49:20 +01:00
|
|
|
}
|
2014-02-09 07:06:10 +01:00
|
|
|
}
|
2014-02-12 02:11:59 +01:00
|
|
|
|
2014-02-17 07:20:42 +01:00
|
|
|
func (channel *Channel) Notice(client *Client, message string) {
|
2014-02-15 06:57:08 +01:00
|
|
|
if channel.flags[NoOutside] && !channel.members.Has(client) {
|
2014-02-12 02:11:59 +01:00
|
|
|
client.Reply(ErrCannotSendToChan(channel))
|
|
|
|
return
|
|
|
|
}
|
2014-02-17 07:20:42 +01:00
|
|
|
channel.Reply(RplNotice(client, channel, message))
|
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) {
|
2014-02-17 02:23:47 +01:00
|
|
|
for member := range channel.members {
|
2014-02-17 07:20:42 +01:00
|
|
|
client.RemoveFriend(member)
|
|
|
|
member.RemoveFriend(client)
|
2014-02-17 02:23:47 +01:00
|
|
|
}
|
|
|
|
|
2014-02-17 07:20:42 +01:00
|
|
|
channel.members.Remove(client)
|
2014-02-17 08:29:11 +01:00
|
|
|
client.channels.Remove(channel)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (channel *Channel) Kick(client *Client, target *Client, comment string) {
|
2014-02-17 22:22:35 +01:00
|
|
|
if !client.flags[Operator] && !channel.members.Has(client) {
|
2014-02-17 08:29:11 +01:00
|
|
|
client.Reply(ErrNotOnChannel(channel))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if !channel.ClientIsOperator(client) {
|
|
|
|
client.Reply(ErrChanOPrivIsNeeded(channel))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if !channel.members.Has(target) {
|
|
|
|
client.Reply(ErrUserNotInChannel(channel, target))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
channel.Reply(RplKick(channel, client, target, comment))
|
|
|
|
channel.Quit(target)
|
2014-02-17 02:23:47 +01:00
|
|
|
}
|