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

321 lines
6.5 KiB
Go
Raw Normal View History

package irc
type Channel struct {
banList []UserMask
flags ChannelModeSet
key string
members MemberSet
name string
server *Server
topic string
}
2014-02-09 03:14:39 +01:00
func IsChannel(target string) bool {
return ChannelNameExpr.MatchString(target)
2014-02-09 03:14:39 +01:00
}
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 {
channel := &Channel{
banList: make([]UserMask, 0),
flags: make(ChannelModeSet),
members: make(MemberSet),
name: name,
server: s,
2012-12-09 21:51:50 +01:00
}
return channel
}
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
}
2014-02-18 06:02:03 +01:00
func (channel *Channel) Names(client *Client) {
client.MultilineReply(channel.Nicks(), RPL_NAMREPLY,
"= %s :%s", channel.name)
client.RplEndOfNames(channel)
2013-06-03 07:07:50 +02:00
}
func (channel *Channel) ClientIsOperator(client *Client) bool {
2014-02-17 22:22:35 +01:00
return client.flags[Operator] || channel.members.HasMode(client, ChannelOperator)
}
2014-02-05 04:28:24 +01:00
func (channel *Channel) Nicks() []string {
nicks := make([]string, len(channel.members))
i := 0
2014-02-15 06:57:08 +01:00
for client, modes := range channel.members {
switch {
case modes[ChannelOperator]:
nicks[i] = "@" + client.Nick()
case modes[Voice]:
nicks[i] = "+" + client.Nick()
default:
nicks[i] = client.Nick()
}
2014-02-05 04:28:24 +01:00
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) {
2014-02-15 06:57:08 +01:00
if channel.key != "" {
str += Key.String()
2014-02-09 08:33:56 +01:00
}
2014-02-15 06:57:08 +01:00
for mode := range channel.flags {
str += mode.String()
}
2014-02-09 17:53:06 +01:00
if len(str) > 0 {
str = "+" + str
}
2014-02-15 06:57:08 +01:00
if channel.key != "" {
str += " " + channel.key
}
2014-02-09 08:33:56 +01:00
return
2014-02-09 03:14:39 +01:00
}
func (channel *Channel) Join(client *Client, key string) {
if (channel.key != "") && (channel.key != key) {
client.ErrBadChannelKey(channel)
return
}
2014-02-21 03:56:13 +01:00
if channel.members[client] != nil {
// already joined, no message?
return
}
2014-02-19 04:31:59 +01:00
client.channels.Add(channel)
channel.members.Add(client)
if len(channel.members) == 1 {
channel.members[client][ChannelCreator] = true
channel.members[client][ChannelOperator] = true
}
reply := RplJoin(client, channel)
for member := range channel.members {
2014-02-22 20:40:32 +01:00
member.Reply(reply)
}
channel.GetTopic(client)
2014-02-18 06:02:03 +01:00
channel.Names(client)
}
func (channel *Channel) Part(client *Client, message string) {
2014-02-09 08:15:05 +01:00
if !channel.members.Has(client) {
client.ErrNotOnChannel(channel)
return
}
reply := RplPart(client, channel, message)
for member := range channel.members {
2014-02-22 20:40:32 +01:00
member.Reply(reply)
}
2014-02-17 08:29:11 +01:00
channel.Quit(client)
2012-12-13 08:27:17 +01:00
2014-02-09 02:53:06 +01:00
if channel.IsEmpty() {
channel.server.channels.Remove(channel)
2012-12-13 08:27:17 +01:00
}
}
func (channel *Channel) GetTopic(client *Client) {
2014-02-09 08:15:05 +01:00
if !channel.members.Has(client) {
client.ErrNotOnChannel(channel)
return
}
if channel.topic == "" {
// clients appear not to expect this
//replier.Reply(RplNoTopic(channel))
return
}
client.RplTopic(channel)
}
func (channel *Channel) SetTopic(client *Client, topic string) {
if !channel.members.Has(client) {
client.ErrNotOnChannel(channel)
return
}
2014-02-21 05:47:05 +01:00
if channel.flags[OpOnlyTopic] && !channel.ClientIsOperator(client) {
client.ErrChanOPrivIsNeeded(channel)
2014-02-16 04:56:38 +01:00
return
}
channel.topic = topic
2014-02-21 05:47:05 +01:00
reply := RplTopicMsg(client, channel)
for member := range channel.members {
2014-02-22 20:40:32 +01:00
member.Reply(reply)
}
}
func (channel *Channel) PrivMsg(client *Client, message string) {
2014-02-15 06:57:08 +01:00
if channel.flags[NoOutside] && !channel.members.Has(client) {
client.ErrCannotSendToChan(channel)
2014-02-09 08:33:56 +01:00
return
}
for member := range channel.members {
if member == client {
continue
}
2014-02-22 20:40:32 +01:00
member.Reply(RplPrivMsg(client, channel, message))
}
}
func (channel *Channel) Mode(client *Client, changes ChannelModeChanges) {
if len(changes) == 0 {
client.RplChannelModeIs(channel)
return
}
applied := make(ChannelModeChanges, 0)
for _, change := range changes {
switch change.mode {
case BanMask:
// TODO add/remove
for _, banMask := range channel.banList {
client.RplBanList(channel, banMask)
}
client.RplEndOfBanList(channel)
2014-02-15 06:57:08 +01:00
case NoOutside, Private, Secret, OpOnlyTopic:
if !channel.ClientIsOperator(client) {
client.ErrChanOPrivIsNeeded(channel)
2014-02-15 06:57:08 +01:00
continue
}
switch change.op {
2014-02-15 06:57:08 +01:00
case Add:
channel.flags[change.mode] = true
applied = append(applied, change)
2014-02-15 06:57:08 +01:00
case Remove:
delete(channel.flags, change.mode)
applied = append(applied, change)
2014-02-15 06:57:08 +01:00
}
case Key:
if !channel.ClientIsOperator(client) {
client.ErrChanOPrivIsNeeded(channel)
2014-02-15 06:57:08 +01:00
continue
}
switch change.op {
2014-02-15 06:57:08 +01:00
case Add:
if change.arg == "" {
2014-02-22 21:15:34 +01:00
client.ErrNeedMoreParams("MODE")
2014-02-15 06:57:08 +01:00
continue
}
2014-02-15 06:57:08 +01:00
channel.key = change.arg
applied = append(applied, change)
2014-02-15 06:57:08 +01:00
case Remove:
channel.key = ""
applied = append(applied, change)
2014-02-15 06:57:08 +01:00
}
case ChannelOperator, Voice:
if !channel.ClientIsOperator(client) {
client.ErrChanOPrivIsNeeded(channel)
2014-02-15 06:57:08 +01:00
continue
}
if change.arg == "" {
2014-02-22 21:15:34 +01:00
client.ErrNeedMoreParams("MODE")
2014-02-15 06:57:08 +01:00
continue
}
2014-02-22 20:40:32 +01:00
target := channel.server.clients.Get(change.arg)
2014-02-15 06:57:08 +01:00
if target == nil {
2014-02-22 21:15:34 +01:00
client.ErrNoSuchNick(change.arg)
2014-02-15 06:57:08 +01:00
continue
}
2014-02-22 21:15:34 +01:00
if !channel.members.Has(target) {
client.ErrUserNotInChannel(channel, target)
2014-02-15 06:57:08 +01:00
continue
}
switch change.op {
2014-02-15 06:57:08 +01:00
case Add:
channel.members[target][change.mode] = true
applied = append(applied, change)
2014-02-15 06:57:08 +01:00
case Remove:
channel.members[target][change.mode] = false
applied = append(applied, change)
2014-02-09 08:33:56 +01:00
}
2014-02-22 21:15:34 +01:00
default:
client.ErrUnknownMode(change.mode, channel)
}
}
if len(applied) > 0 {
for member := range channel.members {
2014-02-22 20:40:32 +01:00
member.Reply(RplChannelMode(client, channel, applied))
}
}
}
2014-02-12 02:11:59 +01:00
func (channel *Channel) Notice(client *Client, message string) {
2014-02-15 06:57:08 +01:00
if channel.flags[NoOutside] && !channel.members.Has(client) {
client.ErrCannotSendToChan(channel)
2014-02-12 02:11:59 +01:00
return
}
for member := range channel.members {
if member == client {
continue
}
2014-02-22 20:40:32 +01:00
member.Reply(RplNotice(client, channel, message))
}
2014-02-12 02:11:59 +01:00
}
func (channel *Channel) Quit(client *Client) {
channel.members.Remove(client)
2014-02-17 08:29:11 +01:00
client.channels.Remove(channel)
}
func (channel *Channel) Kick(client *Client, target *Client, comment string) {
2014-02-17 22:22:35 +01:00
if !client.flags[Operator] && !channel.members.Has(client) {
client.ErrNotOnChannel(channel)
2014-02-17 08:29:11 +01:00
return
}
if !channel.ClientIsOperator(client) {
client.ErrChanOPrivIsNeeded(channel)
2014-02-17 08:29:11 +01:00
return
}
if !channel.members.Has(target) {
client.ErrUserNotInChannel(channel, target)
2014-02-17 08:29:11 +01:00
return
}
2014-02-21 05:47:05 +01:00
reply := RplKick(channel, client, target, comment)
for member := range channel.members {
2014-02-22 20:40:32 +01:00
member.Reply(reply)
}
2014-02-17 08:29:11 +01:00
channel.Quit(target)
}