3
0
mirror of https://github.com/ergochat/ergo.git synced 2024-11-10 22:19:31 +01:00
ergo/src/irc/channel.go

115 lines
2.0 KiB
Go
Raw Normal View History

package irc
type Channel struct {
server *Server
name string
key string
topic string
members ClientSet
noOutside bool
password string
}
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.
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 21:51:50 +01:00
// Send a `Reply` to all `Client`s of the `Channel`. Skip `fromClient`, if it is
// provided.
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
//
// channel functionality
2012-12-09 21:51:50 +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)
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()
}
ch.Send(RplPart(ch, cl, message), nil)
2012-12-09 19:15:01 +01:00
delete(ch.members, cl)
delete(cl.channels, ch)
}
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)
}
}