Sort LIST by client count, obey +s and +p

This commit is contained in:
Trevor Slocum 2016-09-06 17:38:11 -07:00
parent 6d106f4e47
commit 233083c606
3 changed files with 44 additions and 8 deletions

View File

@ -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 {

View File

@ -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 {

View File

@ -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] == '#') {