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 {
|
2012-12-15 23:34:20 +01:00
|
|
|
commands chan<- ChannelCommand
|
2012-12-12 08:04:03 +01:00
|
|
|
key string
|
2014-02-05 04:28:24 +01:00
|
|
|
members ClientSet
|
|
|
|
name string
|
2012-12-12 08:04:03 +01:00
|
|
|
noOutside bool
|
|
|
|
password string
|
2014-02-05 04:28:24 +01:00
|
|
|
replies chan<- Reply
|
|
|
|
server *Server
|
|
|
|
topic string
|
2012-12-09 07:54:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
type ChannelSet map[*Channel]bool
|
|
|
|
|
2012-12-15 23:34:20 +01:00
|
|
|
type ChannelCommand interface {
|
|
|
|
Command
|
|
|
|
HandleChannel(channel *Channel)
|
|
|
|
}
|
|
|
|
|
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{
|
|
|
|
name: name,
|
2014-02-05 04:28:24 +01:00
|
|
|
members: make(ClientSet),
|
2012-12-15 23:34:20 +01:00
|
|
|
server: s,
|
|
|
|
commands: commands,
|
|
|
|
replies: replies,
|
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
|
|
|
}
|
|
|
|
|
2013-05-26 22:28:22 +02:00
|
|
|
func (channel *Channel) receiveCommands(commands <-chan ChannelCommand) {
|
|
|
|
for command := range commands {
|
|
|
|
if DEBUG_CHANNEL {
|
|
|
|
log.Printf("%s → %s : %s", command.Source(), channel, command)
|
|
|
|
}
|
|
|
|
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 {
|
2013-05-26 22:28:22 +02:00
|
|
|
log.Printf("%s ← %s : %s", channel, reply.Source(), reply)
|
2013-05-12 03:28:18 +02:00
|
|
|
}
|
2014-02-05 04:28:24 +01:00
|
|
|
for client := range channel.members {
|
2014-02-09 02:10:04 +01:00
|
|
|
var dest Identifier = client
|
|
|
|
if reply.Source() != dest {
|
|
|
|
client.replies <- 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 == "" {
|
|
|
|
replier.Replies() <- RplNoTopic(channel)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
replier.Replies() <- RplTopic(channel)
|
|
|
|
}
|
|
|
|
|
2013-06-03 07:07:50 +02:00
|
|
|
func (channel *Channel) GetUsers(replier Replier) {
|
|
|
|
replier.Replies() <- NewNamesReply(channel)
|
|
|
|
}
|
|
|
|
|
2014-02-05 04:28:24 +01:00
|
|
|
func (channel *Channel) Nicks() []string {
|
|
|
|
nicks := make([]string, len(channel.members))
|
|
|
|
i := 0
|
|
|
|
for client := range channel.members {
|
|
|
|
nicks[i] = client.Nick()
|
|
|
|
i += 1
|
|
|
|
}
|
|
|
|
return nicks
|
|
|
|
}
|
|
|
|
|
2013-05-26 22:28:22 +02:00
|
|
|
func (channel *Channel) Replies() chan<- Reply {
|
|
|
|
return channel.replies
|
|
|
|
}
|
|
|
|
|
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-05 04:28:24 +01:00
|
|
|
func (channel *Channel) Join(client *Client) {
|
|
|
|
channel.members[client] = true
|
|
|
|
client.channels[channel] = true
|
2014-02-09 02:53:06 +01:00
|
|
|
reply := RplJoin(client, channel)
|
|
|
|
client.replies <- reply
|
2014-02-05 04:28:24 +01:00
|
|
|
channel.replies <- reply
|
|
|
|
channel.GetTopic(client)
|
|
|
|
channel.GetUsers(client)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (channel *Channel) HasMember(client *Client) bool {
|
|
|
|
return channel.members[client]
|
2013-06-03 07:07:50 +02: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()
|
2012-12-17 04:13:53 +01:00
|
|
|
if channel.key != m.channels[channel.name] {
|
2014-02-05 04:28:24 +01:00
|
|
|
client.replies <- ErrBadChannelKey(channel)
|
2012-12-09 07:54:58 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2014-02-05 04:28:24 +01:00
|
|
|
channel.Join(client)
|
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 02:53:06 +01:00
|
|
|
if !channel.HasMember(client) {
|
|
|
|
client.replies <- ErrNotOnChannel(channel)
|
2012-12-09 07:54:58 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2014-02-09 02:53:06 +01:00
|
|
|
reply := RplPart(client, channel, m.Message())
|
|
|
|
client.replies <- reply
|
|
|
|
channel.replies <- reply
|
2012-12-09 19:15:01 +01:00
|
|
|
|
2014-02-09 02:53:06 +01:00
|
|
|
delete(channel.members, client)
|
|
|
|
delete(client.channels, channel)
|
2012-12-13 08:27:17 +01:00
|
|
|
|
2014-02-09 02:53:06 +01:00
|
|
|
// TODO persistent channels
|
|
|
|
if channel.IsEmpty() {
|
2012-12-15 23:34:20 +01:00
|
|
|
channel.server.DeleteChannel(channel)
|
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-05 04:28:24 +01:00
|
|
|
if !channel.HasMember(client) {
|
|
|
|
client.replies <- ErrNotOnChannel(channel)
|
2012-12-09 07:54:58 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2012-12-17 04:13:53 +01:00
|
|
|
if m.topic == "" {
|
2014-02-05 04:28:24 +01:00
|
|
|
channel.GetTopic(client)
|
2012-12-09 07:54:58 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2012-12-15 23:34:20 +01:00
|
|
|
channel.topic = m.topic
|
2012-12-09 07:54:58 +01:00
|
|
|
|
2014-02-05 04:28:24 +01:00
|
|
|
channel.GetTopic(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 02:10:04 +01:00
|
|
|
channel.replies <- RplPrivMsg(m.Client(), channel, m.message)
|
2012-12-09 07:54:58 +01:00
|
|
|
}
|