2012-12-09 07:54:58 +01:00
|
|
|
package irc
|
|
|
|
|
|
|
|
type Channel struct {
|
2012-12-12 08:04:03 +01:00
|
|
|
server *Server
|
|
|
|
name string
|
|
|
|
key string
|
|
|
|
topic string
|
|
|
|
members ClientSet
|
|
|
|
noOutside bool
|
|
|
|
password string
|
2012-12-09 07:54:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
type ChannelSet map[*Channel]bool
|
|
|
|
|
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-09 21:51:50 +01:00
|
|
|
return &Channel{
|
|
|
|
name: name,
|
|
|
|
members: make(ClientSet),
|
|
|
|
server: s,
|
|
|
|
}
|
2012-12-09 07:54:58 +01:00
|
|
|
}
|
|
|
|
|
2012-12-09 21:51:50 +01:00
|
|
|
// Send a `Reply` to all `Client`s of the `Channel`. Skip `fromClient`, if it is
|
|
|
|
// provided.
|
2012-12-09 07:54:58 +01:00
|
|
|
func (ch *Channel) Send(reply Reply, fromClient *Client) {
|
|
|
|
for client := range ch.members {
|
|
|
|
if client != fromClient {
|
|
|
|
client.send <- reply
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-09 21:51:50 +01:00
|
|
|
//
|
2012-12-09 07:54:58 +01:00
|
|
|
// channel functionality
|
2012-12-09 21:51:50 +01:00
|
|
|
//
|
2012-12-09 07:54:58 +01:00
|
|
|
|
|
|
|
func (ch *Channel) Join(cl *Client, key string) {
|
|
|
|
if ch.key != key {
|
|
|
|
cl.send <- ErrBadChannelKey(ch)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
ch.members[cl] = true
|
|
|
|
cl.channels[ch] = true
|
|
|
|
|
|
|
|
ch.Send(RplJoin(ch, cl), nil)
|
|
|
|
ch.GetTopic(cl)
|
2012-12-09 21:51:50 +01:00
|
|
|
cl.send <- RplNamReply(ch)
|
2012-12-09 07:54:58 +01:00
|
|
|
cl.send <- RplEndOfNames(ch.server)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ch *Channel) Part(cl *Client, message string) {
|
|
|
|
if !ch.members[cl] {
|
|
|
|
cl.send <- ErrNotOnChannel(ch)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2012-12-09 19:15:01 +01:00
|
|
|
if message == "" {
|
|
|
|
message = cl.Nick()
|
|
|
|
}
|
2012-12-09 07:54:58 +01:00
|
|
|
|
|
|
|
ch.Send(RplPart(ch, cl, message), nil)
|
2012-12-09 19:15:01 +01:00
|
|
|
|
|
|
|
delete(ch.members, cl)
|
|
|
|
delete(cl.channels, ch)
|
2012-12-13 08:27:17 +01:00
|
|
|
|
|
|
|
if len(ch.members) == 0 {
|
|
|
|
ch.server.DeleteChannel(ch)
|
|
|
|
}
|
2012-12-09 07:54:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (ch *Channel) PrivMsg(cl *Client, message string) {
|
|
|
|
ch.Send(RplPrivMsgChannel(ch, cl, message), cl)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ch *Channel) GetTopic(cl *Client) {
|
|
|
|
if !ch.members[cl] {
|
|
|
|
cl.send <- ErrNotOnChannel(ch)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if ch.topic != "" {
|
|
|
|
cl.send <- RplTopic(ch)
|
|
|
|
} else {
|
|
|
|
cl.send <- RplNoTopic(ch)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ch *Channel) ChangeTopic(cl *Client, newTopic string) {
|
|
|
|
if !ch.members[cl] {
|
|
|
|
cl.send <- ErrNotOnChannel(ch)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
ch.topic = newTopic
|
|
|
|
|
|
|
|
if ch.topic != "" {
|
|
|
|
ch.Send(RplTopic(ch), nil)
|
|
|
|
} else {
|
|
|
|
ch.Send(RplNoTopic(ch), nil)
|
|
|
|
}
|
|
|
|
}
|