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

277 lines
5.5 KiB
Go
Raw Normal View History

package irc
import (
"log"
2014-02-14 17:42:56 +01:00
"sync"
)
type Channel struct {
banList []UserMask
commands chan<- ChannelCommand
2014-02-13 06:04:50 +01:00
destroyed bool
key string
2014-02-05 04:28:24 +01:00
members ClientSet
2014-02-14 17:42:56 +01:00
mutex *sync.Mutex
2014-02-05 04:28:24 +01:00
name string
noOutside bool
password string
2014-02-05 04:28:24 +01:00
replies chan<- Reply
server *Server
topic string
}
2014-02-09 03:14:39 +01:00
func IsChannel(target string) bool {
if target == "" {
return false
}
switch target[0] {
case '&', '#', '+', '!':
return true
}
return false
}
2014-02-05 04:28:24 +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 {
2013-06-03 01:53:06 +02:00
commands := make(chan ChannelCommand)
replies := make(chan Reply)
channel := &Channel{
banList: make([]UserMask, 0),
commands: commands,
members: make(ClientSet),
2014-02-14 17:42:56 +01:00
mutex: &sync.Mutex{},
name: name,
replies: replies,
server: s,
2012-12-09 21:51:50 +01:00
}
go channel.receiveCommands(commands)
2013-05-26 22:28:22 +02:00
go channel.receiveReplies(replies)
return channel
}
2014-02-13 06:04:50 +01:00
func (channel *Channel) Destroy() {
if channel.destroyed {
return
2014-02-09 17:48:11 +01:00
}
2014-02-13 06:04:50 +01:00
channel.destroyed = true
2014-02-14 17:57:17 +01:00
channel.members = make(ClientSet)
channel.server.channels.Remove(channel)
2014-02-09 17:48:11 +01:00
}
2014-02-13 18:35:59 +01:00
func (channel *Channel) Command(command ChannelCommand) {
channel.commands <- command
}
2014-02-13 06:04:50 +01:00
func (channel *Channel) Reply(replies ...Reply) {
2014-02-12 00:44:58 +01:00
for _, reply := range replies {
channel.replies <- reply
}
2014-02-09 17:48:11 +01:00
}
2013-05-26 22:28:22 +02:00
func (channel *Channel) receiveCommands(commands <-chan ChannelCommand) {
for command := range commands {
2014-02-14 17:57:17 +01:00
if channel.destroyed {
if DEBUG_CHANNEL {
log.Printf("%s → %s %s dropped", command.Source(), channel, command)
}
continue
}
2013-05-26 22:28:22 +02:00
if DEBUG_CHANNEL {
log.Printf("%s → %s %s", command.Source(), channel, command)
2013-05-26 22:28:22 +02:00
}
command.HandleChannel(channel)
}
}
2013-05-12 20:20:55 +02:00
func (channel *Channel) receiveReplies(replies <-chan Reply) {
for reply := range replies {
2014-02-14 17:57:17 +01:00
if channel.destroyed {
if DEBUG_CHANNEL {
log.Printf("%s ← %s %s dropped", channel, reply.Source(), reply)
}
continue
}
if DEBUG_CHANNEL {
log.Printf("%s ← %s %s", channel, reply.Source(), reply)
}
2014-02-14 17:42:56 +01:00
channel.mutex.Lock()
2014-02-05 04:28:24 +01:00
for client := range channel.members {
2014-02-10 04:03:56 +01:00
if reply.Source() != Identifier(client) {
2014-02-09 17:48:11 +01:00
client.Reply(reply)
}
}
2014-02-14 17:42:56 +01:00
channel.mutex.Unlock()
}
}
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 == "" {
2014-02-09 17:48:11 +01:00
replier.Reply(RplNoTopic(channel))
return
}
2014-02-09 17:48:11 +01:00
replier.Reply(RplTopic(channel))
}
2013-06-03 07:07:50 +02:00
func (channel *Channel) GetUsers(replier Replier) {
2014-02-09 17:48:11 +01:00
replier.Reply(NewNamesReply(channel))
2013-06-03 07:07:50 +02:00
}
func (channel *Channel) ClientIsOperator(client *Client) bool {
// TODO client-channel relations
return false
}
2014-02-05 04:28:24 +01:00
func (channel *Channel) Nicks() []string {
nicks := make([]string, len(channel.members))
i := 0
for client := range channel.members {
nicks[i] = client.Nick()
i += 1
}
return nicks
}
func (channel *Channel) Id() string {
return channel.name
}
2013-06-03 07:07:50 +02:00
func (channel *Channel) Nick() string {
return channel.name
}
func (channel *Channel) String() string {
return channel.Id()
}
2014-02-09 03:14:39 +01:00
// <mode> <mode params>
2014-02-09 08:33:56 +01:00
func (channel *Channel) ModeString() (str string) {
if channel.noOutside {
str += NoOutside.String()
}
2014-02-09 17:53:06 +01:00
if len(str) > 0 {
str = "+" + str
}
2014-02-09 08:33:56 +01:00
return
2014-02-09 03:14:39 +01:00
}
2014-02-05 04:28:24 +01:00
func (channel *Channel) Join(client *Client) {
2014-02-09 08:15:05 +01:00
channel.members.Add(client)
client.channels.Add(channel)
2014-02-09 02:53:06 +01:00
reply := RplJoin(client, channel)
2014-02-09 17:48:11 +01:00
client.Reply(reply)
channel.Reply(reply)
2014-02-05 04:28:24 +01:00
channel.GetTopic(client)
channel.GetUsers(client)
}
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()
if channel.key != m.channels[channel.name] {
2014-02-09 17:48:11 +01:00
client.Reply(ErrBadChannelKey(channel))
return
}
2014-02-05 04:28:24 +01:00
channel.Join(client)
}
func (m *PartCommand) HandleChannel(channel *Channel) {
2014-02-09 02:53:06 +01:00
client := m.Client()
2014-02-09 08:15:05 +01:00
if !channel.members.Has(client) {
2014-02-09 17:48:11 +01:00
client.Reply(ErrNotOnChannel(channel))
return
}
2014-02-09 02:53:06 +01:00
reply := RplPart(client, channel, m.Message())
2014-02-09 17:48:11 +01:00
client.Reply(reply)
channel.Reply(reply)
2012-12-09 19:15:01 +01:00
2014-02-09 08:15:05 +01:00
channel.members.Remove(client)
client.channels.Remove(channel)
2012-12-13 08:27:17 +01:00
2014-02-09 02:53:06 +01:00
// TODO persistent channels
if channel.IsEmpty() {
2014-02-13 06:04:50 +01:00
channel.Destroy()
2012-12-13 08:27:17 +01:00
}
}
func (m *TopicCommand) HandleChannel(channel *Channel) {
2014-02-05 04:28:24 +01:00
client := m.Client()
2014-02-09 08:15:05 +01:00
if !channel.members.Has(client) {
2014-02-09 17:48:11 +01:00
client.Reply(ErrNotOnChannel(channel))
return
}
if m.setTopic {
channel.topic = m.topic
2014-02-05 04:28:24 +01:00
channel.GetTopic(client)
reply := RplTopicMsg(client, channel)
client.Reply(reply)
channel.Reply(reply)
return
}
channel.GetTopic(client)
return
}
func (m *PrivMsgCommand) HandleChannel(channel *Channel) {
2014-02-09 08:33:56 +01:00
client := m.Client()
if channel.noOutside && !channel.members.Has(client) {
2014-02-09 17:48:11 +01:00
client.Reply(ErrCannotSendToChan(channel))
2014-02-09 08:33:56 +01:00
return
}
2014-02-09 17:48:11 +01:00
channel.Reply(RplPrivMsg(client, channel, m.message))
}
func (msg *ChannelModeCommand) HandleChannel(channel *Channel) {
client := msg.Client()
for _, modeOp := range msg.modeOps {
switch modeOp.mode {
case BanMask:
// TODO add/remove
for _, banMask := range channel.banList {
2014-02-09 17:48:11 +01:00
client.Reply(RplBanList(channel, banMask))
}
2014-02-09 17:48:11 +01:00
client.Reply(RplEndOfBanList(channel))
2014-02-09 08:33:56 +01:00
case NoOutside:
if channel.ClientIsOperator(client) {
switch modeOp.op {
case Add:
channel.noOutside = true
case Remove:
channel.noOutside = false
}
} else {
client.Reply(ErrChanOPrivIsNeeded(channel))
2014-02-09 08:33:56 +01:00
}
}
}
2014-02-09 17:48:11 +01:00
client.Reply(RplChannelModeIs(channel))
}
2014-02-12 02:11:59 +01:00
func (m *NoticeCommand) HandleChannel(channel *Channel) {
client := m.Client()
if channel.noOutside && !channel.members.Has(client) {
client.Reply(ErrCannotSendToChan(channel))
return
}
channel.Reply(RplNotice(client, channel, m.message))
}