Browse Source

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

master
Trevor Slocum 6 years ago
parent
commit
233083c606
  1. 29
      anonircd.go
  2. 2
      entity.go
  3. 21
      server.go

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

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

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

Loading…
Cancel
Save