3
0
mirror of https://github.com/ergochat/ergo.git synced 2024-12-22 18:52:41 +01:00

channels: Fix

This commit is contained in:
Daniel Oaks 2016-06-19 14:55:24 +10:00
parent c2e5386966
commit 7fcc6bdbc4
4 changed files with 140 additions and 105 deletions

View File

@ -6,6 +6,7 @@
package irc package irc
import ( import (
"fmt"
"log" "log"
"strconv" "strconv"
) )
@ -13,11 +14,12 @@ import (
type Channel struct { type Channel struct {
flags ChannelModeSet flags ChannelModeSet
lists map[ChannelMode]*UserMaskSet lists map[ChannelMode]*UserMaskSet
key Text key string
members MemberSet members MemberSet
name Name name Name
nameString string
server *Server server *Server
topic Text topic string
userLimit uint64 userLimit uint64
} }
@ -33,6 +35,7 @@ func NewChannel(s *Server, name Name, addDefaultModes bool) *Channel {
}, },
members: make(MemberSet), members: make(MemberSet),
name: name, name: name,
nameString: name.String(),
server: s, server: s,
} }
@ -52,8 +55,28 @@ func (channel *Channel) IsEmpty() bool {
} }
func (channel *Channel) Names(client *Client) { func (channel *Channel) Names(client *Client) {
client.RplNamReply(channel) currentNicks := channel.Nicks(client)
client.RplEndOfNames(channel) // assemble and send replies
maxNamLen := 480 - len(client.server.name) - len(client.nickString)
var buffer string
for _, nick := range currentNicks {
if buffer == "" {
buffer += nick
continue
}
if len(buffer)+1+len(nick) > maxNamLen {
client.Send(nil, client.server.name, RPL_NAMREPLY, "=", channel.nameString, buffer)
buffer = nick
continue
}
buffer += " "
buffer += nick
}
client.Send(nil, client.server.name, RPL_NAMREPLY, "=", channel.nameString, buffer)
client.Send(nil, client.server.name, RPL_ENDOFNAMES, channel.nameString, "End of NAMES list")
} }
func (channel *Channel) ClientIsOperator(client *Client) bool { func (channel *Channel) ClientIsOperator(client *Client) bool {
@ -129,7 +152,7 @@ func (channel *Channel) ModeString(client *Client) (str string) {
// args for flags with args: The order must match above to keep // args for flags with args: The order must match above to keep
// positional arguments in place. // positional arguments in place.
if showKey { if showKey {
str += " " + channel.key.String() str += " " + channel.key
} }
if showUserLimit { if showUserLimit {
str += " " + strconv.FormatUint(channel.userLimit, 10) str += " " + strconv.FormatUint(channel.userLimit, 10)
@ -143,36 +166,36 @@ func (channel *Channel) IsFull() bool {
(uint64(len(channel.members)) >= channel.userLimit) (uint64(len(channel.members)) >= channel.userLimit)
} }
func (channel *Channel) CheckKey(key Text) bool { func (channel *Channel) CheckKey(key string) bool {
return (channel.key == "") || (channel.key == key) return (channel.key == "") || (channel.key == key)
} }
func (channel *Channel) Join(client *Client, key Text) { func (channel *Channel) Join(client *Client, key string) {
if channel.members.Has(client) { if channel.members.Has(client) {
// already joined, no message? // already joined, no message?
return return
} }
if channel.IsFull() { if channel.IsFull() {
client.ErrChannelIsFull(channel) client.Send(nil, client.server.name, ERR_CHANNELISFULL, channel.nameString, "Cannot join channel (+l)")
return return
} }
if !channel.CheckKey(key) { if !channel.CheckKey(key) {
client.ErrBadChannelKey(channel) client.Send(nil, client.server.name, ERR_BADCHANNELKEY, channel.nameString, "Cannot join channel (+k)")
return return
} }
isInvited := channel.lists[InviteMask].Match(client.UserHost()) isInvited := channel.lists[InviteMask].Match(client.UserHost())
if channel.flags[InviteOnly] && !isInvited { if channel.flags[InviteOnly] && !isInvited {
client.ErrInviteOnlyChan(channel) client.Send(nil, client.server.name, ERR_INVITEONLYCHAN, channel.nameString, "Cannot join channel (+i)")
return return
} }
if channel.lists[BanMask].Match(client.UserHost()) && if channel.lists[BanMask].Match(client.UserHost()) &&
!isInvited && !isInvited &&
!channel.lists[ExceptMask].Match(client.UserHost()) { !channel.lists[ExceptMask].Match(client.UserHost()) {
client.ErrBannedFromChan(channel) client.Send(nil, client.server.name, ERR_BANNEDFROMCHAN, channel.nameString, "Cannot join channel (+b)")
return return
} }
@ -183,30 +206,34 @@ func (channel *Channel) Join(client *Client, key Text) {
channel.members[client][ChannelOperator] = true channel.members[client][ChannelOperator] = true
} }
reply := RplJoin(client, channel) client.Send(nil, client.nickMaskString, "JOIN", channel.nameString)
return
//TODO(dan): should we be continuing here????
// return was above this originally, is it required?
/*
for member := range channel.members { for member := range channel.members {
member.Reply(reply) member.Reply(reply)
} }
channel.GetTopic(client) channel.GetTopic(client)
channel.Names(client) channel.Names(client)
*/
} }
func (channel *Channel) Part(client *Client, message Text) { func (channel *Channel) Part(client *Client, message string) {
if !channel.members.Has(client) { if !channel.members.Has(client) {
client.ErrNotOnChannel(channel) client.Send(nil, client.server.name, ERR_NOTONCHANNEL, channel.nameString, "You're not on that channel")
return return
} }
reply := RplPart(client, channel, message)
for member := range channel.members { for member := range channel.members {
member.Reply(reply) member.Send(nil, client.nickMaskString, "PART", channel.nameString, message)
} }
channel.Quit(client) channel.Quit(client)
} }
func (channel *Channel) GetTopic(client *Client) { func (channel *Channel) GetTopic(client *Client) {
if !channel.members.Has(client) { if !channel.members.Has(client) {
client.ErrNotOnChannel(channel) client.Send(nil, client.server.name, ERR_NOTONCHANNEL, channel.nameString, "You're not on that channel")
return return
} }
@ -216,25 +243,24 @@ func (channel *Channel) GetTopic(client *Client) {
return return
} }
client.RplTopic(channel) client.Send(nil, client.server.name, RPL_TOPIC, channel.nameString, channel.topic)
} }
func (channel *Channel) SetTopic(client *Client, topic Text) { func (channel *Channel) SetTopic(client *Client, topic string) {
if !(client.flags[Operator] || channel.members.Has(client)) { if !(client.flags[Operator] || channel.members.Has(client)) {
client.ErrNotOnChannel(channel) client.Send(nil, client.server.name, ERR_NOTONCHANNEL, channel.nameString, "You're not on that channel")
return return
} }
if channel.flags[OpOnlyTopic] && !channel.ClientIsOperator(client) { if channel.flags[OpOnlyTopic] && !channel.ClientIsOperator(client) {
client.ErrChanOPrivIsNeeded(channel) client.Send(nil, client.server.name, ERR_CHANOPRIVSNEEDED, channel.nameString, "You're not a channel operator")
return return
} }
channel.topic = topic channel.topic = topic
reply := RplTopicMsg(client, channel)
for member := range channel.members { for member := range channel.members {
member.Reply(reply) member.Send(nil, client.nickMaskString, "TOPIC", channel.nameString, channel.topic)
} }
if err := channel.Persist(); err != nil { if err := channel.Persist(); err != nil {
@ -256,24 +282,24 @@ func (channel *Channel) CanSpeak(client *Client) bool {
return true return true
} }
func (channel *Channel) PrivMsg(client *Client, message Text) { func (channel *Channel) PrivMsg(client *Client, message string) {
if !channel.CanSpeak(client) { if !channel.CanSpeak(client) {
client.ErrCannotSendToChan(channel) client.Send(nil, client.server.name, ERR_CANNOTSENDTOCHAN, channel.nameString, "Cannot send to channel")
return return
} }
reply := RplPrivMsg(client, channel, message)
for member := range channel.members { for member := range channel.members {
if member == client { if member == client {
continue continue
} }
member.Reply(reply) //TODO(dan): use nickmask instead of nickString here lel
member.Send(nil, client.nickMaskString, "PRIVMSG", channel.nameString, message)
} }
} }
func (channel *Channel) applyModeFlag(client *Client, mode ChannelMode, func (channel *Channel) applyModeFlag(client *Client, mode ChannelMode,
op ModeOp) bool { op ModeOp) bool {
if !channel.ClientIsOperator(client) { if !channel.ClientIsOperator(client) {
client.ErrChanOPrivIsNeeded(channel) client.Send(nil, client.server.name, ERR_CHANOPRIVSNEEDED, channel.nameString, "You're not a channel operator")
return false return false
} }
@ -296,25 +322,28 @@ func (channel *Channel) applyModeFlag(client *Client, mode ChannelMode,
} }
func (channel *Channel) applyModeMember(client *Client, mode ChannelMode, func (channel *Channel) applyModeMember(client *Client, mode ChannelMode,
op ModeOp, nick Name) bool { op ModeOp, nick string) bool {
if !channel.ClientIsOperator(client) { if !channel.ClientIsOperator(client) {
client.ErrChanOPrivIsNeeded(channel) client.Send(nil, client.server.name, ERR_CHANOPRIVSNEEDED, channel.nameString, "You're not a channel operator")
return false return false
} }
if nick == "" { if nick == "" {
client.ErrNeedMoreParams("MODE") //TODO(dan): shouldn't this be handled before it reaches this function?
client.Send(nil, client.server.name, ERR_NEEDMOREPARAMS, "MODE", "Not enough parameters")
return false return false
} }
target := channel.server.clients.Get(nick) target := channel.server.clients.Get(Name(nick))
if target == nil { if target == nil {
client.ErrNoSuchNick(nick) //TODO(dan): investigate using NOSUCHNICK and NOSUCHCHANNEL specifically as that other IRCd (insp?) does,
// since I think that would make sense
client.Send(nil, client.server.name, ERR_NOSUCHNICK, nick, "No such nick/channel")
return false return false
} }
if !channel.members.Has(target) { if !channel.members.Has(target) {
client.ErrUserNotInChannel(channel, target) client.Send(nil, client.server.name, ERR_USERNOTINCHANNEL, client.nickString, channel.nameString, "They aren't on that channel")
return false return false
} }
@ -337,10 +366,13 @@ func (channel *Channel) applyModeMember(client *Client, mode ChannelMode,
} }
func (channel *Channel) ShowMaskList(client *Client, mode ChannelMode) { func (channel *Channel) ShowMaskList(client *Client, mode ChannelMode) {
//TODO(dan): WE NEED TO fiX this PROPERLY
log.Fatal("Implement ShowMaskList")
/*
for lmask := range channel.lists[mode].masks { for lmask := range channel.lists[mode].masks {
client.RplMaskList(mode, channel, lmask) client.RplMaskList(mode, channel, lmask)
} }
client.RplEndOfMaskList(mode, channel) client.RplEndOfMaskList(mode, channel)*/
} }
func (channel *Channel) applyModeMask(client *Client, mode ChannelMode, op ModeOp, func (channel *Channel) applyModeMask(client *Client, mode ChannelMode, op ModeOp,
@ -357,7 +389,7 @@ func (channel *Channel) applyModeMask(client *Client, mode ChannelMode, op ModeO
} }
if !channel.ClientIsOperator(client) { if !channel.ClientIsOperator(client) {
client.ErrChanOPrivIsNeeded(channel) client.Send(nil, client.server.name, ERR_CHANOPRIVSNEEDED, channel.nameString, "You're not a channel operator")
return false return false
} }
@ -383,17 +415,17 @@ func (channel *Channel) applyMode(client *Client, change *ChannelModeChange) boo
case Key: case Key:
if !channel.ClientIsOperator(client) { if !channel.ClientIsOperator(client) {
client.ErrChanOPrivIsNeeded(channel) client.Send(nil, client.server.name, ERR_CHANOPRIVSNEEDED, channel.nameString, "You're not a channel operator")
return false return false
} }
switch change.op { switch change.op {
case Add: case Add:
if change.arg == "" { if change.arg == "" {
client.ErrNeedMoreParams("MODE") client.Send(nil, client.server.name, ERR_NEEDMOREPARAMS, "MODE", "Not enough parameters")
return false return false
} }
key := NewText(change.arg) key := change.arg
if key == channel.key { if key == channel.key {
return false return false
} }
@ -409,7 +441,7 @@ func (channel *Channel) applyMode(client *Client, change *ChannelModeChange) boo
case UserLimit: case UserLimit:
limit, err := strconv.ParseUint(change.arg, 10, 64) limit, err := strconv.ParseUint(change.arg, 10, 64)
if err != nil { if err != nil {
client.ErrNeedMoreParams("MODE") client.Send(nil, client.server.name, ERR_NEEDMOREPARAMS, "MODE", "Not enough parameters")
return false return false
} }
if (limit == 0) || (limit == channel.userLimit) { if (limit == 0) || (limit == channel.userLimit) {
@ -445,22 +477,22 @@ func (channel *Channel) applyMode(client *Client, change *ChannelModeChange) boo
if change.op == Remove && name.ToLower() == client.nick.ToLower() { if change.op == Remove && name.ToLower() == client.nick.ToLower() {
// success! // success!
} else { } else {
client.ErrChanOPrivIsNeeded(channel) client.Send(nil, client.server.name, ERR_CHANOPRIVSNEEDED, channel.nameString, "You're not a channel operator")
return false return false
} }
} }
return channel.applyModeMember(client, change.mode, change.op, name) return channel.applyModeMember(client, change.mode, change.op, name.String())
default: default:
client.ErrUnknownMode(change.mode, channel) client.Send(nil, client.server.name, ERR_UNKNOWNMODE, change.mode.String(), fmt.Sprintf(":is an unknown mode char to me for %s", channel))
} }
return false return false
} }
func (channel *Channel) Mode(client *Client, changes ChannelModeChanges) { func (channel *Channel) Mode(client *Client, changes ChannelModeChanges) {
if len(changes) == 0 { if len(changes) == 0 {
client.RplChannelModeIs(channel) client.Send(nil, client.server.name, RPL_CHANNELMODEIS, channel.nameString, channel.ModeString(client))
return return
} }
@ -472,9 +504,9 @@ func (channel *Channel) Mode(client *Client, changes ChannelModeChanges) {
} }
if len(applied) > 0 { if len(applied) > 0 {
reply := RplChannelMode(client, channel, applied) appliedString := applied.String()
for member := range channel.members { for member := range channel.members {
member.Reply(reply) member.Send(nil, client.nickMaskString, "MODE", channel.nameString, appliedString)
} }
if err := channel.Persist(); err != nil { if err := channel.Persist(); err != nil {
@ -490,8 +522,8 @@ func (channel *Channel) Persist() (err error) {
(name, flags, key, topic, user_limit, ban_list, except_list, (name, flags, key, topic, user_limit, ban_list, except_list,
invite_list) invite_list)
VALUES (?, ?, ?, ?, ?, ?, ?, ?)`, VALUES (?, ?, ?, ?, ?, ?, ?, ?)`,
channel.name.String(), channel.flags.String(), channel.key.String(), channel.name.String(), channel.flags.String(), channel.key,
channel.topic.String(), channel.userLimit, channel.lists[BanMask].String(), channel.topic, channel.userLimit, channel.lists[BanMask].String(),
channel.lists[ExceptMask].String(), channel.lists[InviteMask].String()) channel.lists[ExceptMask].String(), channel.lists[InviteMask].String())
} else { } else {
_, err = channel.server.db.Exec(` _, err = channel.server.db.Exec(`
@ -500,17 +532,16 @@ func (channel *Channel) Persist() (err error) {
return return
} }
func (channel *Channel) Notice(client *Client, message Text) { func (channel *Channel) Notice(client *Client, message string) {
if !channel.CanSpeak(client) { if !channel.CanSpeak(client) {
client.ErrCannotSendToChan(channel) client.Send(nil, client.server.name, ERR_CANNOTSENDTOCHAN, channel.nameString, "Cannot send to channel")
return return
} }
reply := RplNotice(client, channel, message)
for member := range channel.members { for member := range channel.members {
if member == client { if member == client {
continue continue
} }
member.Reply(reply) member.Send(nil, client.nickMaskString, "NOTICE", channel.nameString, message)
} }
} }
@ -523,35 +554,34 @@ func (channel *Channel) Quit(client *Client) {
} }
} }
func (channel *Channel) Kick(client *Client, target *Client, comment Text) { func (channel *Channel) Kick(client *Client, target *Client, comment string) {
if !(client.flags[Operator] || channel.members.Has(client)) { if !(client.flags[Operator] || channel.members.Has(client)) {
client.ErrNotOnChannel(channel) client.Send(nil, client.server.name, ERR_NOTONCHANNEL, channel.nameString, "You're not on that channel")
return return
} }
if !channel.ClientIsOperator(client) { if !channel.ClientIsOperator(client) {
client.ErrChanOPrivIsNeeded(channel) client.Send(nil, client.server.name, ERR_CANNOTSENDTOCHAN, channel.nameString, "Cannot send to channel")
return return
} }
if !channel.members.Has(target) { if !channel.members.Has(target) {
client.ErrUserNotInChannel(channel, target) client.Send(nil, client.server.name, ERR_USERNOTINCHANNEL, client.nickString, channel.nameString, "They aren't on that channel")
return return
} }
reply := RplKick(channel, client, target, comment)
for member := range channel.members { for member := range channel.members {
member.Reply(reply) member.Send(nil, client.nickMaskString, "KICK", channel.nameString, target.nickString, comment)
} }
channel.Quit(target) channel.Quit(target)
} }
func (channel *Channel) Invite(invitee *Client, inviter *Client) { func (channel *Channel) Invite(invitee *Client, inviter *Client) {
if channel.flags[InviteOnly] && !channel.ClientIsOperator(inviter) { if channel.flags[InviteOnly] && !channel.ClientIsOperator(inviter) {
inviter.ErrChanOPrivIsNeeded(channel) inviter.Send(nil, inviter.server.name, ERR_CHANOPRIVSNEEDED, channel.nameString, "You're not a channel operator")
return return
} }
if !channel.members.Has(inviter) { if !channel.members.Has(inviter) {
inviter.ErrNotOnChannel(channel) inviter.Send(nil, inviter.server.name, ERR_NOTONCHANNEL, channel.nameString, "You're not on that channel")
return return
} }
@ -562,9 +592,10 @@ func (channel *Channel) Invite(invitee *Client, inviter *Client) {
} }
} }
inviter.RplInviting(invitee, channel.name) //TODO(dan): should inviter.server.name here be inviter.nickMaskString ?
invitee.Reply(RplInviteMsg(inviter, invitee, channel.name)) inviter.Send(nil, inviter.server.name, RPL_INVITING, invitee.nickString, channel.nameString)
invitee.Send(nil, inviter.nickMaskString, "INVITE", invitee.nickString, channel.nameString)
if invitee.flags[Away] { if invitee.flags[Away] {
inviter.RplAway(invitee) inviter.Send(nil, inviter.server.name, RPL_AWAY, invitee.nickString, invitee.awayMessage)
} }
} }

View File

@ -21,7 +21,7 @@ const (
type Client struct { type Client struct {
atime time.Time atime time.Time
authorized bool authorized bool
awayMessage Text awayMessage string
capabilities CapabilitySet capabilities CapabilitySet
capState CapState capState CapState
channels ChannelSet channels ChannelSet
@ -32,7 +32,8 @@ type Client struct {
hostname Name hostname Name
idleTimer *time.Timer idleTimer *time.Timer
nick Name nick Name
nickString string // cache for nick string since it's used with every reply nickString string // cache for nick string since it's used with most numerics
nickMaskString string // cache for nickmask string since it's used with every reply
quitTimer *time.Timer quitTimer *time.Timer
realname Text realname Text
registered bool registered bool
@ -255,8 +256,8 @@ func (client *Client) Reply(reply string) error {
} }
func (client *Client) Quit(message string) { func (client *Client) Quit(message string) {
client.Send(nil, client.nickString, "QUIT", message) client.Send(nil, client.nickMaskString, "QUIT", message)
client.Send(nil, client.nickString, "ERROR", message) client.Send(nil, client.nickMaskString, "ERROR", message)
} }
func (client *Client) destroy() { func (client *Client) destroy() {
@ -288,7 +289,7 @@ func (client *Client) destroy() {
client.socket.Close() client.socket.Close()
for friend := range client.Friends() { for friend := range client.Friends() {
//TODO(dan): store quit message in user, if exists use that instead here //TODO(dan): store quit message in user, if exists use that instead here
friend.Send(nil, client.nickString, "QUIT", "Exited") friend.Send(nil, client.nickMaskString, "QUIT", "Exited")
} }
} }

View File

@ -11,7 +11,9 @@ func nickHandler(server *Server, client *Client, msg ircmsg.IrcMessage) bool {
// check NICK validity // check NICK validity
// send NICK change to primary server thread for processing // send NICK change to primary server thread for processing
// |-> ensure no other client exists with that nickname // |-> ensure no other client exists with that nickname
//TODO(dan): SET client.nickString APPROPRIATELY
// do this after replacing nickname
client.updateNickMask()
return true return true
} }

View File

@ -436,6 +436,7 @@ func userHandler(server *Server, client *Client, msg ircmsg.IrcMessage) bool {
if client.realname != "" { if client.realname != "" {
client.realname = msg.realname client.realname = msg.realname
} }
client.updateNickMask()
server.clients.Add(client) server.clients.Add(client)