Limit the number of nicks that are sent in a single NAMREPLY message

This commit is contained in:
ed 2016-03-09 03:04:04 -05:00
parent 7cf71b33fa
commit 8449c7e81a
2 changed files with 15 additions and 3 deletions

View File

@ -85,7 +85,9 @@ func (c *Client) joinChannel(channelName string) {
c.reply(rplNoTopic, channel.name) c.reply(rplNoTopic, channel.name)
} }
nicks := make([]string, 0, 100) //The capacity sets the max number of nicks to send per message
nicks := make([]string, 0, 128)
for _, client := range channel.clientMap { for _, client := range channel.clientMap {
prefix := "" prefix := ""
@ -93,12 +95,21 @@ func (c *Client) joinChannel(channelName string) {
prefix = mode.Prefix() prefix = mode.Prefix()
} }
if len(nicks) >= cap(nicks) {
c.reply(rplNames, channelName, strings.Join(nicks, " "))
nicks = nicks[:0]
}
nicks = append(nicks, fmt.Sprintf("%s%s", prefix, client.nick)) nicks = append(nicks, fmt.Sprintf("%s%s", prefix, client.nick))
} }
if len(nicks) > 0 {
c.reply(rplNames, channelName, strings.Join(nicks, " ")) c.reply(rplNames, channelName, strings.Join(nicks, " "))
} }
c.reply(rplEndOfNames, channelName)
}
func (c *Client) partChannel(channelName, reason string) { func (c *Client) partChannel(channelName, reason string) {
channelKey := strings.ToLower(channelName) channelKey := strings.ToLower(channelName)
channel, exists := c.server.channelMap[channelKey] channel, exists := c.server.channelMap[channelKey]
@ -148,8 +159,8 @@ func (c *Client) reply(code replyCode, args ...string) {
case rplNoTopic: case rplNoTopic:
c.outputChan <- fmt.Sprintf(":%s 331 %s %s :No topic is set", c.server.name, c.nick, args[0]) c.outputChan <- fmt.Sprintf(":%s 331 %s %s :No topic is set", c.server.name, c.nick, args[0])
case rplNames: case rplNames:
//TODO: break long lists up into multiple messages
c.outputChan <- fmt.Sprintf(":%s 353 %s = %s :%s", c.server.name, c.nick, args[0], args[1]) c.outputChan <- fmt.Sprintf(":%s 353 %s = %s :%s", c.server.name, c.nick, args[0], args[1])
case rplEndOfNames:
c.outputChan <- fmt.Sprintf(":%s 366 %s %s :End of NAMES list", c.server.name, c.nick, args[0]) c.outputChan <- fmt.Sprintf(":%s 366 %s %s :End of NAMES list", c.server.name, c.nick, args[0])
case rplNickChange: case rplNickChange:
c.outputChan <- fmt.Sprintf(":%s NICK %s", args[0], args[1]) c.outputChan <- fmt.Sprintf(":%s NICK %s", args[0], args[1])

View File

@ -116,6 +116,7 @@ const (
rplTopic rplTopic
rplNoTopic rplNoTopic
rplNames rplNames
rplEndOfNames
rplNickChange rplNickChange
rplKill rplKill
rplMsg rplMsg