From 233083c606e39bc5b3d131370bf0fed4d325b4a5 Mon Sep 17 00:00:00 2001 From: Trevor Slocum Date: Tue, 6 Sep 2016 17:38:11 -0700 Subject: [PATCH] Sort LIST by client count, obey +s and +p --- anonircd.go | 29 +++++++++++++++++++++++++++++ entity.go | 2 +- server.go | 21 ++++++++++++++------- 3 files changed, 44 insertions(+), 8 deletions(-) diff --git a/anonircd.go b/anonircd.go index 2956f7a..d543060 100644 --- a/anonircd.go +++ b/anonircd.go @@ -19,6 +19,7 @@ package main import ( + "sort" "sync" "math/rand" "time" @@ -46,6 +47,34 @@ type Config struct { SSLKey string } +type Pair struct { + Key string + Value int +} + +type PairList []Pair + +func (p PairList) Len() int { + return len(p) +} +func (p PairList) Less(i, j int) bool { + return p[i].Value < p[j].Value +} +func (p PairList) Swap(i, j int) { + p[i], p[j] = p[j], p[i] +} + +func sortMapByValues(m map[string]int) PairList { + pl := make(PairList, len(m)) + i := 0 + for k, v := range m { + pl[i] = Pair{k, v} + i++ + } + sort.Sort(sort.Reverse(pl)) + return pl +} + func randomIdentifier() string { b := make([]byte, 10) for i := range b { diff --git a/entity.go b/entity.go index 975f81d..c8a7615 100644 --- a/entity.go +++ b/entity.go @@ -10,7 +10,7 @@ const ENTITY_CLIENT = 0 const ENTITY_CHANNEL = 1 const CLIENT_MODES = "c" -const CHANNEL_MODES = "cistz" +const CHANNEL_MODES = "cipstz" const CHANNEL_MODES_ARG = "kl" type Entity struct { diff --git a/server.go b/server.go index 441d24b..1ced399 100644 --- a/server.go +++ b/server.go @@ -381,15 +381,22 @@ func (s *Server) handleRead(c *Client) { s.joinChannel("#", c.identifier) } else if (msg.Command == irc.LIST) { - c.writebuffer <- &irc.Message{&anonirc, irc.RPL_LISTSTART, []string{"Channel", "Users Name"}} + var ccount int + chans := make(map[string]int) for channelname, channel := range s.channels { - var ccount int - if c.hasMode("c") || channel.hasMode("c") { - ccount = 2 - } else { - ccount = len(channel.clients) + if !channel.hasMode("p") && !channel.hasMode("s") { + if c.hasMode("c") || channel.hasMode("c") { + ccount = 2 + } else { + ccount = len(channel.clients) + } + chans[channelname] = ccount } - c.writebuffer <- &irc.Message{&anonirc, irc.RPL_LIST, []string{channelname, strconv.Itoa(ccount), "[" + channel.printModes(nil) + "] " + channel.topic}} + } + + c.writebuffer <- &irc.Message{&anonirc, irc.RPL_LISTSTART, []string{"Channel", "Users Name"}} + for _, pl := range sortMapByValues(chans) { + c.writebuffer <- &irc.Message{&anonirc, irc.RPL_LIST, []string{pl.Key, strconv.Itoa(pl.Value), "[" + s.channels[pl.Key].printModes(nil) + "] " + s.channels[pl.Key].topic}} } c.writebuffer <- &irc.Message{&anonirc, irc.RPL_LISTEND, []string{"End of /LIST"}} } else if (msg.Command == irc.JOIN && len(msg.Params) > 0 && len(msg.Params[0]) > 0 && msg.Params[0][0] == '#') {