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-12 08:04:03 +01:00
|
|
|
server *Server
|
2012-12-15 23:34:20 +01:00
|
|
|
commands chan<- ChannelCommand
|
2012-12-17 04:13:53 +01:00
|
|
|
replies chan<- Reply
|
2012-12-12 08:04:03 +01:00
|
|
|
name string
|
|
|
|
key string
|
|
|
|
topic string
|
2012-12-15 23:34:20 +01:00
|
|
|
members UserSet
|
2012-12-12 08:04:03 +01:00
|
|
|
noOutside bool
|
|
|
|
password string
|
2012-12-09 07:54:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
type ChannelSet map[*Channel]bool
|
|
|
|
|
2012-12-15 23:34:20 +01:00
|
|
|
func (set ChannelSet) Add(channel *Channel) {
|
|
|
|
set[channel] = true
|
|
|
|
}
|
|
|
|
|
|
|
|
func (set ChannelSet) Remove(channel *Channel) {
|
|
|
|
delete(set, channel)
|
|
|
|
}
|
|
|
|
|
|
|
|
type ChannelCommand interface {
|
|
|
|
Command
|
|
|
|
HandleChannel(channel *Channel)
|
|
|
|
}
|
|
|
|
|
2012-12-09 21:51:50 +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
|
|
|
replies := make(chan Reply)
|
|
|
|
commands := make(chan ChannelCommand)
|
|
|
|
channel := &Channel{
|
|
|
|
name: name,
|
|
|
|
members: make(UserSet),
|
|
|
|
server: s,
|
|
|
|
commands: commands,
|
|
|
|
replies: replies,
|
2012-12-09 21:51:50 +01:00
|
|
|
}
|
2012-12-15 23:34:20 +01:00
|
|
|
go channel.receiveReplies(replies)
|
|
|
|
go channel.receiveCommands(commands)
|
|
|
|
return channel
|
2012-12-09 07:54:58 +01:00
|
|
|
}
|
|
|
|
|
2012-12-15 23:34:20 +01:00
|
|
|
// Forward `Reply`s to all `User`s of the `Channel`.
|
|
|
|
func (ch *Channel) receiveReplies(replies <-chan Reply) {
|
|
|
|
for reply := range replies {
|
2012-12-17 04:13:53 +01:00
|
|
|
for user := range ch.members {
|
|
|
|
if user != reply.Source() {
|
|
|
|
user.replies <- reply
|
|
|
|
}
|
2012-12-09 07:54:58 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-12-15 23:34:20 +01:00
|
|
|
func (ch *Channel) receiveCommands(commands <-chan ChannelCommand) {
|
|
|
|
for command := range commands {
|
2012-12-17 04:13:53 +01:00
|
|
|
log.Printf("%s %T %+v", ch.Id(), command, command)
|
2012-12-15 23:34:20 +01:00
|
|
|
command.HandleChannel(ch)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-12-09 21:51:50 +01:00
|
|
|
func (ch *Channel) Nicks() []string {
|
|
|
|
nicks := make([]string, len(ch.members))
|
|
|
|
i := 0
|
|
|
|
for member := range ch.members {
|
|
|
|
nicks[i] = member.Nick()
|
|
|
|
i++
|
|
|
|
}
|
|
|
|
return nicks
|
|
|
|
}
|
|
|
|
|
2012-12-10 06:46:22 +01:00
|
|
|
func (ch *Channel) IsEmpty() bool {
|
|
|
|
return len(ch.members) == 0
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (channel *Channel) Id() string {
|
|
|
|
return channel.name
|
|
|
|
}
|
|
|
|
|
|
|
|
func (channel *Channel) PublicId() string {
|
|
|
|
return channel.name
|
|
|
|
}
|
|
|
|
|
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()
|
|
|
|
user := client.user
|
|
|
|
|
2012-12-17 04:13:53 +01:00
|
|
|
if channel.key != m.channels[channel.name] {
|
2012-12-15 23:34:20 +01:00
|
|
|
client.user.replies <- ErrBadChannelKey(channel)
|
2012-12-09 07:54:58 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2012-12-17 04:13:53 +01:00
|
|
|
channel.members.Add(user)
|
|
|
|
user.channels.Add(channel)
|
2012-12-09 07:54:58 +01:00
|
|
|
|
2012-12-15 23:34:20 +01:00
|
|
|
channel.replies <- RplJoin(channel, user)
|
|
|
|
channel.GetTopic(user)
|
2012-12-17 04:13:53 +01:00
|
|
|
user.replies <- RplNamReply(channel)
|
|
|
|
user.replies <- RplEndOfNames(channel.server)
|
2012-12-09 07:54:58 +01:00
|
|
|
}
|
|
|
|
|
2012-12-17 04:13:53 +01:00
|
|
|
func (m *PartCommand) HandleChannel(channel *Channel) {
|
2012-12-15 23:34:20 +01:00
|
|
|
user := m.Client().user
|
|
|
|
|
|
|
|
if !channel.members[user] {
|
|
|
|
user.replies <- ErrNotOnChannel(channel)
|
2012-12-09 07:54:58 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2012-12-15 23:34:20 +01:00
|
|
|
msg := m.message
|
|
|
|
if msg == "" {
|
|
|
|
msg = user.Nick()
|
2012-12-09 19:15:01 +01:00
|
|
|
}
|
2012-12-09 07:54:58 +01:00
|
|
|
|
2012-12-15 23:34:20 +01:00
|
|
|
channel.replies <- RplPart(channel, user, msg)
|
2012-12-09 19:15:01 +01:00
|
|
|
|
2012-12-15 23:34:20 +01:00
|
|
|
channel.members.Remove(user)
|
|
|
|
user.channels.Remove(channel)
|
2012-12-13 08:27:17 +01:00
|
|
|
|
2012-12-15 23:34:20 +01:00
|
|
|
if len(channel.members) == 0 {
|
|
|
|
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) {
|
|
|
|
user := m.Client().user
|
|
|
|
|
2012-12-15 23:34:20 +01:00
|
|
|
if !channel.members[user] {
|
|
|
|
user.replies <- ErrNotOnChannel(channel)
|
2012-12-09 07:54:58 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2012-12-17 04:13:53 +01:00
|
|
|
if m.topic == "" {
|
|
|
|
channel.GetTopic(user)
|
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
|
|
|
|
2012-12-15 23:34:20 +01:00
|
|
|
if channel.topic == "" {
|
|
|
|
channel.replies <- RplNoTopic(channel)
|
|
|
|
return
|
2012-12-09 07:54:58 +01:00
|
|
|
}
|
2012-12-15 23:34:20 +01:00
|
|
|
|
|
|
|
channel.replies <- RplTopic(channel)
|
|
|
|
}
|
|
|
|
|
2012-12-17 04:13:53 +01:00
|
|
|
func (m *PrivMsgCommand) HandleChannel(channel *Channel) {
|
2012-12-15 23:34:20 +01:00
|
|
|
channel.replies <- RplPrivMsgChannel(channel, m.Client().user, m.message)
|
2012-12-09 07:54:58 +01:00
|
|
|
}
|