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

186 lines
3.5 KiB
Go
Raw Normal View History

package irc
import (
"log"
)
const (
DEBUG_CHANNEL = true
)
type Channel struct {
server *Server
commands chan<- ChannelCommand
replies chan<- Reply
name string
key string
topic string
members UserSet
noOutside bool
password string
}
type ChannelSet map[*Channel]bool
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.
func NewChannel(s *Server, name string) *Channel {
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
}
go channel.receiveReplies(replies)
go channel.receiveCommands(commands)
return channel
}
// Forward `Reply`s to all `User`s of the `Channel`.
2013-05-12 20:20:55 +02:00
func (channel *Channel) receiveReplies(replies <-chan Reply) {
for reply := range replies {
if DEBUG_CHANNEL {
2013-05-12 20:20:55 +02:00
log.Printf("%s → %s", channel, reply)
}
2013-05-12 20:20:55 +02:00
for user := range channel.members {
if user != reply.Source() {
user.replies <- reply
}
}
}
}
2013-05-12 20:20:55 +02:00
func (channel *Channel) receiveCommands(commands <-chan ChannelCommand) {
for command := range commands {
if DEBUG_CHANNEL {
2013-05-12 20:20:55 +02:00
log.Printf("%s ← %s %s", channel, command.Source(), command)
}
2013-05-12 20:20:55 +02:00
command.HandleChannel(channel)
}
}
2013-05-12 20:20:55 +02:00
func (channel *Channel) Nicks() []string {
return channel.members.Nicks()
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
}
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
}
func (channel *Channel) Commands() chan<- ChannelCommand {
2013-05-09 20:05:10 +02:00
return channel.commands
}
func (channel *Channel) Replies() chan<- Reply {
return channel.replies
}
func (channel *Channel) String() string {
return channel.Id()
}
2012-12-09 21:51:50 +01:00
//
// commands
2012-12-09 21:51:50 +01:00
//
func (m *JoinCommand) HandleChannel(channel *Channel) {
client := m.Client()
user := client.user
if channel.key != m.channels[channel.name] {
client.user.replies <- ErrBadChannelKey(channel)
return
}
channel.members.Add(user)
user.channels.Add(channel)
channel.replies <- RplJoin(channel, user)
channel.GetTopic(user)
user.replies <- RplNamReply(channel)
user.replies <- RplEndOfNames(channel.server)
}
func (m *PartCommand) HandleChannel(channel *Channel) {
user := m.Client().user
if !channel.members[user] {
user.replies <- ErrNotOnChannel(channel)
return
}
msg := m.message
if msg == "" {
msg = user.Nick()
2012-12-09 19:15:01 +01:00
}
channel.replies <- RplPart(channel, user, msg)
2012-12-09 19:15:01 +01:00
channel.members.Remove(user)
user.channels.Remove(channel)
2012-12-13 08:27:17 +01:00
2013-05-12 20:20:55 +02:00
if channel.IsEmpty() {
channel.server.DeleteChannel(channel)
2012-12-13 08:27:17 +01:00
}
}
func (m *TopicCommand) HandleChannel(channel *Channel) {
user := m.User()
if !channel.members[user] {
user.replies <- ErrNotOnChannel(channel)
return
}
if m.topic == "" {
channel.GetTopic(user)
return
}
channel.topic = m.topic
if channel.topic == "" {
channel.replies <- RplNoTopic(channel)
return
}
channel.replies <- RplTopic(channel)
}
func (m *PrivMsgCommand) HandleChannel(channel *Channel) {
channel.Replies() <- RplPrivMsgChannel(channel, m.User(), m.message)
}