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 02:23:47 +01:00
|
|
|
banList []UserMask
|
|
|
|
commands chan<- ChannelCommand
|
|
|
|
flags ChannelModeSet
|
|
|
|
key string
|
|
|
|
members MemberSet
|
|
|
|
name string
|
|
|
|
replies chan<- Reply
|
|
|
|
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 {
|
|
|
|
if target == "" {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
switch target[0] {
|
|
|
|
case '&', '#', '+', '!':
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
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 {
|
2013-06-03 01:53:06 +02:00
|
|
|
commands := make(chan ChannelCommand)
|
|
|
|
replies := make(chan Reply)
|
2012-12-15 23:34:20 +01:00
|
|
|
channel := &Channel{
|
2014-02-09 07:06:10 +01:00
|
|
|
banList: make([]UserMask, 0),
|
2012-12-15 23:34:20 +01:00
|
|
|
commands: commands,
|
2014-02-17 02:23:47 +01:00
|
|
|
flags: make(ChannelModeSet),
|
|
|
|
members: make(MemberSet),
|
2014-02-09 07:06:10 +01:00
|
|
|
name: name,
|
2012-12-15 23:34:20 +01:00
|
|
|
replies: replies,
|
2014-02-09 07:06:10 +01:00
|
|
|
server: s,
|
2012-12-09 21:51:50 +01:00
|
|
|
}
|
2012-12-15 23:34:20 +01:00
|
|
|
go channel.receiveCommands(commands)
|
2013-05-26 22:28:22 +02:00
|
|
|
go channel.receiveReplies(replies)
|
2012-12-15 23:34:20 +01:00
|
|
|
return channel
|
2012-12-09 07:54:58 +01:00
|
|
|
}
|
|
|
|
|
2014-02-17 02:23:47 +01:00
|
|
|
type DestroyChannel struct {
|
|
|
|
BaseCommand
|
|
|
|
channel *Channel
|
|
|
|
}
|
2014-02-16 19:42:25 +01:00
|
|
|
|
2014-02-17 02:23:47 +01:00
|
|
|
func (channel *Channel) Destroy() {
|
|
|
|
channel.server.Command(&DestroyChannel{
|
|
|
|
channel: channel,
|
2014-02-16 19:42:25 +01:00
|
|
|
})
|
2014-02-17 02:23:47 +01:00
|
|
|
close(channel.commands)
|
|
|
|
close(channel.replies)
|
2014-02-09 17:48:11 +01:00
|
|
|
}
|
|
|
|
|
2014-02-13 18:35:59 +01:00
|
|
|
func (channel *Channel) Command(command ChannelCommand) {
|
|
|
|
channel.commands <- command
|
|
|
|
}
|
|
|
|
|
2014-02-15 04:35:25 +01:00
|
|
|
func (channel *Channel) Reply(reply Reply) {
|
|
|
|
channel.replies <- reply
|
2014-02-09 17:48:11 +01:00
|
|
|
}
|
|
|
|
|
2013-05-26 22:28:22 +02:00
|
|
|
func (channel *Channel) receiveCommands(commands <-chan ChannelCommand) {
|
|
|
|
for command := range commands {
|
|
|
|
if DEBUG_CHANNEL {
|
2014-02-11 23:32:17 +01:00
|
|
|
log.Printf("%s → %s %s", command.Source(), channel, command)
|
2013-05-26 22:28:22 +02:00
|
|
|
}
|
|
|
|
command.HandleChannel(channel)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-12 20:20:55 +02:00
|
|
|
func (channel *Channel) receiveReplies(replies <-chan Reply) {
|
2012-12-15 23:34:20 +01:00
|
|
|
for reply := range replies {
|
2013-05-12 03:28:18 +02:00
|
|
|
if DEBUG_CHANNEL {
|
2014-02-11 23:32:17 +01:00
|
|
|
log.Printf("%s ← %s %s", channel, reply.Source(), reply)
|
2013-05-12 03:28:18 +02:00
|
|
|
}
|
2014-02-17 02:23:47 +01:00
|
|
|
|
|
|
|
for client := range channel.members {
|
|
|
|
if (reply.Code() == ReplyCode(PRIVMSG)) &&
|
|
|
|
(reply.Source() == Identifier(client)) {
|
|
|
|
continue
|
2014-02-09 02:10:04 +01:00
|
|
|
}
|
2014-02-17 02:23:47 +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
|
|
|
}
|
|
|
|
|
2012-12-17 04:13:53 +01:00
|
|
|
func (channel *Channel) GetTopic(replier Replier) {
|
|
|
|
if channel.topic == "" {
|
2014-02-16 18:39:11 +01:00
|
|
|
// clients appear not to expect this
|
|
|
|
//replier.Reply(RplNoTopic(channel))
|
2012-12-17 04:13:53 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2014-02-09 17:48:11 +01:00
|
|
|
replier.Reply(RplTopic(channel))
|
2012-12-17 04:13:53 +01:00
|
|
|
}
|
|
|
|
|
2013-06-03 07:07:50 +02:00
|
|
|
func (channel *Channel) GetUsers(replier Replier) {
|
2014-02-09 17:48:11 +01:00
|
|
|
replier.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-15 06:57:08 +01:00
|
|
|
return 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 02:23:47 +01:00
|
|
|
type JoinChannel struct {
|
|
|
|
BaseCommand
|
|
|
|
channel *Channel
|
2014-02-05 04:28:24 +01:00
|
|
|
}
|
|
|
|
|
2012-12-09 21:51:50 +01:00
|
|
|
//
|
2012-12-15 23:34:20 +01:00
|
|
|
// commands
|
2012-12-09 21:51:50 +01:00
|
|
|
//
|
2012-12-09 07:54:58 +01:00
|
|
|
|
2012-12-17 04:13:53 +01:00
|
|
|
func (m *JoinCommand) HandleChannel(channel *Channel) {
|
2012-12-15 23:34:20 +01:00
|
|
|
client := m.Client()
|
2014-02-15 06:57:08 +01:00
|
|
|
if (channel.key != "") && (channel.key != m.channels[channel.name]) {
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
client.commands <- &JoinChannel{
|
|
|
|
channel: channel,
|
|
|
|
}
|
|
|
|
|
|
|
|
addClient := &AddFriend{
|
|
|
|
client: client,
|
|
|
|
}
|
|
|
|
for member := range channel.members {
|
|
|
|
client.commands <- &AddFriend{
|
|
|
|
client: member,
|
|
|
|
}
|
|
|
|
member.commands <- addClient
|
|
|
|
}
|
|
|
|
|
|
|
|
channel.Reply(RplJoin(client, channel))
|
|
|
|
channel.GetTopic(client)
|
|
|
|
channel.GetUsers(client)
|
|
|
|
}
|
|
|
|
|
|
|
|
type PartChannel struct {
|
|
|
|
channel *Channel
|
2012-12-09 07:54:58 +01:00
|
|
|
}
|
|
|
|
|
2012-12-17 04:13:53 +01:00
|
|
|
func (m *PartCommand) HandleChannel(channel *Channel) {
|
2014-02-09 02:53:06 +01:00
|
|
|
client := m.Client()
|
2012-12-15 23:34:20 +01:00
|
|
|
|
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-16 04:49:20 +01:00
|
|
|
channel.Reply(RplPart(client, channel, m.Message()))
|
2012-12-09 19:15:01 +01:00
|
|
|
|
2014-02-09 08:15:05 +01:00
|
|
|
channel.members.Remove(client)
|
2014-02-17 02:23:47 +01:00
|
|
|
client.commands <- &PartChannel{
|
|
|
|
channel: channel,
|
|
|
|
}
|
|
|
|
for member := range channel.members {
|
|
|
|
member.commands <- &RemoveFriend{
|
|
|
|
client: client,
|
|
|
|
}
|
|
|
|
}
|
2012-12-13 08:27:17 +01:00
|
|
|
|
2014-02-09 02:53:06 +01:00
|
|
|
if channel.IsEmpty() {
|
2014-02-13 06:04:50 +01:00
|
|
|
channel.Destroy()
|
2012-12-13 08:27:17 +01:00
|
|
|
}
|
2012-12-09 07:54:58 +01:00
|
|
|
}
|
|
|
|
|
2012-12-17 04:13:53 +01:00
|
|
|
func (m *TopicCommand) HandleChannel(channel *Channel) {
|
2014-02-05 04:28:24 +01:00
|
|
|
client := m.Client()
|
2012-12-17 04:13:53 +01:00
|
|
|
|
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-16 04:56:38 +01:00
|
|
|
if !m.setTopic {
|
2014-02-05 04:28:24 +01:00
|
|
|
channel.GetTopic(client)
|
2012-12-09 07:54:58 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2014-02-16 04:56:38 +01:00
|
|
|
if channel.flags[OpOnlyTopic] {
|
|
|
|
client.Reply(ErrChanOPrivIsNeeded(channel))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
channel.topic = m.topic
|
|
|
|
channel.Reply(RplTopicMsg(client, channel))
|
2012-12-15 23:34:20 +01:00
|
|
|
}
|
|
|
|
|
2012-12-17 04:13:53 +01:00
|
|
|
func (m *PrivMsgCommand) HandleChannel(channel *Channel) {
|
2014-02-09 08:33:56 +01:00
|
|
|
client := m.Client()
|
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-09 17:48:11 +01:00
|
|
|
channel.Reply(RplPrivMsg(client, channel, m.message))
|
2012-12-09 07:54:58 +01:00
|
|
|
}
|
2014-02-09 07:06:10 +01:00
|
|
|
|
|
|
|
func (msg *ChannelModeCommand) HandleChannel(channel *Channel) {
|
|
|
|
client := msg.Client()
|
|
|
|
|
2014-02-16 04:49:20 +01:00
|
|
|
if len(msg.changes) == 0 {
|
|
|
|
client.Reply(RplChannelModeIs(channel))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
changes := make(ChannelModeChanges, 0)
|
|
|
|
|
|
|
|
for _, change := range msg.changes {
|
|
|
|
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
|
|
|
|
changes = append(changes, change)
|
2014-02-15 06:57:08 +01:00
|
|
|
|
|
|
|
case Remove:
|
2014-02-16 04:49:20 +01:00
|
|
|
delete(channel.flags, change.mode)
|
|
|
|
changes = append(changes, 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
|
|
|
|
changes = append(changes, change)
|
2014-02-15 06:57:08 +01:00
|
|
|
|
|
|
|
case Remove:
|
|
|
|
channel.key = ""
|
2014-02-16 04:49:20 +01:00
|
|
|
changes = append(changes, 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
|
|
|
|
changes = append(changes, change)
|
|
|
|
|
2014-02-15 06:57:08 +01:00
|
|
|
case Remove:
|
2014-02-16 04:49:20 +01:00
|
|
|
channel.members[target][change.mode] = false
|
|
|
|
changes = append(changes, change)
|
2014-02-09 08:33:56 +01:00
|
|
|
}
|
2014-02-09 07:06:10 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-16 04:49:20 +01:00
|
|
|
if len(changes) > 0 {
|
|
|
|
channel.Reply(RplChannelMode(client, channel, changes))
|
|
|
|
}
|
2014-02-09 07:06:10 +01:00
|
|
|
}
|
2014-02-12 02:11:59 +01:00
|
|
|
|
|
|
|
func (m *NoticeCommand) HandleChannel(channel *Channel) {
|
|
|
|
client := m.Client()
|
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
|
|
|
|
}
|
|
|
|
channel.Reply(RplNotice(client, channel, m.message))
|
|
|
|
}
|
2014-02-17 02:23:47 +01:00
|
|
|
|
|
|
|
func (msg *QuitCommand) HandleChannel(channel *Channel) {
|
|
|
|
client := msg.Client()
|
|
|
|
removeClient := &RemoveFriend{
|
|
|
|
client: client,
|
|
|
|
}
|
|
|
|
for member := range channel.members {
|
|
|
|
member.commands <- removeClient
|
|
|
|
}
|
|
|
|
channel.members.Remove(client)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (msg *DestroyClient) HandleChannel(channel *Channel) {
|
|
|
|
channel.members.Remove(msg.client)
|
|
|
|
}
|