mirror of
https://github.com/ergochat/ergo.git
synced 2025-12-23 19:41:58 -08:00
Another massive refactor to support mutliple connections per nick.
This commit is contained in:
parent
559445d9a8
commit
f2aedbaffd
11 changed files with 636 additions and 443 deletions
|
|
@ -2,33 +2,82 @@ package irc
|
|||
|
||||
type Channel struct {
|
||||
server *Server
|
||||
replies chan<- Reply
|
||||
commands chan<- ChannelCommand
|
||||
name string
|
||||
key string
|
||||
topic string
|
||||
members ClientSet
|
||||
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)
|
||||
}
|
||||
|
||||
type JoinChannelCommand struct {
|
||||
*JoinCommand
|
||||
key string
|
||||
}
|
||||
|
||||
type PartChannelCommand struct {
|
||||
Command
|
||||
message string
|
||||
}
|
||||
|
||||
type GetTopicChannelCommand struct {
|
||||
*TopicCommand
|
||||
}
|
||||
|
||||
type SetTopicChannelCommand struct {
|
||||
*TopicCommand
|
||||
}
|
||||
|
||||
type PrivMsgChannelCommand struct {
|
||||
*PrivMsgCommand
|
||||
}
|
||||
|
||||
// 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 {
|
||||
return &Channel{
|
||||
name: name,
|
||||
members: make(ClientSet),
|
||||
server: s,
|
||||
replies := make(chan Reply)
|
||||
commands := make(chan ChannelCommand)
|
||||
channel := &Channel{
|
||||
name: name,
|
||||
members: make(UserSet),
|
||||
server: s,
|
||||
commands: commands,
|
||||
replies: replies,
|
||||
}
|
||||
go channel.receiveReplies(replies)
|
||||
go channel.receiveCommands(commands)
|
||||
return channel
|
||||
}
|
||||
|
||||
// Forward `Reply`s to all `User`s of the `Channel`.
|
||||
func (ch *Channel) receiveReplies(replies <-chan Reply) {
|
||||
for reply := range replies {
|
||||
for client := range ch.members {
|
||||
client.replies <- reply
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 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
|
||||
}
|
||||
func (ch *Channel) receiveCommands(commands <-chan ChannelCommand) {
|
||||
for command := range commands {
|
||||
command.HandleChannel(ch)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -47,72 +96,86 @@ func (ch *Channel) IsEmpty() bool {
|
|||
}
|
||||
|
||||
//
|
||||
// channel functionality
|
||||
// commands
|
||||
//
|
||||
|
||||
func (ch *Channel) Join(cl *Client, key string) {
|
||||
if ch.key != key {
|
||||
cl.send <- ErrBadChannelKey(ch)
|
||||
func (m *JoinChannelCommand) HandleChannel(channel *Channel) {
|
||||
client := m.Client()
|
||||
user := client.user
|
||||
|
||||
if channel.key != m.key {
|
||||
client.user.replies <- ErrBadChannelKey(channel)
|
||||
return
|
||||
}
|
||||
|
||||
ch.members[cl] = true
|
||||
cl.channels[ch] = true
|
||||
channel.members.Add(client.user)
|
||||
client.user.channels.Add(channel)
|
||||
|
||||
ch.Send(RplJoin(ch, cl), nil)
|
||||
ch.GetTopic(cl)
|
||||
cl.send <- RplNamReply(ch)
|
||||
cl.send <- RplEndOfNames(ch.server)
|
||||
channel.replies <- RplJoin(channel, user)
|
||||
channel.GetTopic(user)
|
||||
client.user.replies <- RplNamReply(channel)
|
||||
client.user.replies <- RplEndOfNames(channel.server)
|
||||
}
|
||||
|
||||
func (ch *Channel) Part(cl *Client, message string) {
|
||||
if !ch.members[cl] {
|
||||
cl.send <- ErrNotOnChannel(ch)
|
||||
func (m *PartChannelCommand) HandleChannel(channel *Channel) {
|
||||
user := m.Client().user
|
||||
|
||||
if !channel.members[user] {
|
||||
user.replies <- ErrNotOnChannel(channel)
|
||||
return
|
||||
}
|
||||
|
||||
if message == "" {
|
||||
message = cl.Nick()
|
||||
msg := m.message
|
||||
if msg == "" {
|
||||
msg = user.Nick()
|
||||
}
|
||||
|
||||
ch.Send(RplPart(ch, cl, message), nil)
|
||||
channel.replies <- RplPart(channel, user, msg)
|
||||
|
||||
delete(ch.members, cl)
|
||||
delete(cl.channels, ch)
|
||||
channel.members.Remove(user)
|
||||
user.channels.Remove(channel)
|
||||
|
||||
if len(ch.members) == 0 {
|
||||
ch.server.DeleteChannel(ch)
|
||||
if len(channel.members) == 0 {
|
||||
channel.server.DeleteChannel(channel)
|
||||
}
|
||||
}
|
||||
|
||||
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)
|
||||
func (channel *Channel) GetTopic(user *User) {
|
||||
if !channel.members[user] {
|
||||
user.replies <- ErrNotOnChannel(channel)
|
||||
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)
|
||||
if channel.topic == "" {
|
||||
user.replies <- RplNoTopic(channel)
|
||||
return
|
||||
}
|
||||
|
||||
ch.topic = newTopic
|
||||
|
||||
if ch.topic != "" {
|
||||
ch.Send(RplTopic(ch), nil)
|
||||
} else {
|
||||
ch.Send(RplNoTopic(ch), nil)
|
||||
}
|
||||
user.replies <- RplTopic(channel)
|
||||
}
|
||||
|
||||
func (m *GetTopicChannelCommand) HandleChannel(channel *Channel) {
|
||||
channel.GetTopic(m.Client().user)
|
||||
}
|
||||
|
||||
func (m *SetTopicChannelCommand) HandleChannel(channel *Channel) {
|
||||
user := m.Client().user
|
||||
|
||||
if !channel.members[user] {
|
||||
user.replies <- ErrNotOnChannel(channel)
|
||||
return
|
||||
}
|
||||
|
||||
channel.topic = m.topic
|
||||
|
||||
if channel.topic == "" {
|
||||
channel.replies <- RplNoTopic(channel)
|
||||
return
|
||||
}
|
||||
|
||||
channel.replies <- RplTopic(channel)
|
||||
}
|
||||
|
||||
func (m *PrivMsgChannelCommand) HandleChannel(channel *Channel) {
|
||||
channel.replies <- RplPrivMsgChannel(channel, m.Client().user, m.message)
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue