mirror of
https://github.com/gissleh/irc.git
synced 2026-11-17 05:05:49 +00:00
Initial Commit
This commit is contained in:
+281
@@ -0,0 +1,281 @@
|
||||
package list
|
||||
|
||||
import (
|
||||
"sort"
|
||||
"strings"
|
||||
"sync"
|
||||
|
||||
"git.aiterp.net/gisle/irc/isupport"
|
||||
)
|
||||
|
||||
// The List of users in a channel. It has all operations one would perform on
|
||||
// users, like adding/removing modes and changing nicks.
|
||||
type List struct {
|
||||
mutex sync.RWMutex
|
||||
isupport *isupport.ISupport
|
||||
users []*User
|
||||
index map[string]*User
|
||||
autosort bool
|
||||
}
|
||||
|
||||
// New creates a new list with the ISupport. The list can be reused between connections since the
|
||||
// ISupport is simply cleared and repopulated, but it should be cleared.
|
||||
func New(isupport *isupport.ISupport) *List {
|
||||
return &List{
|
||||
isupport: isupport,
|
||||
users: make([]*User, 0, 64),
|
||||
index: make(map[string]*User, 64),
|
||||
autosort: true,
|
||||
}
|
||||
}
|
||||
|
||||
// InsertFromNamesToken inserts using a NAMES token to get the nick, user, host and prefixes.
|
||||
// The format is `"@+Nick@user!hostmask.example.com"`
|
||||
func (list *List) InsertFromNamesToken(namestoken string) (ok bool) {
|
||||
user := User{}
|
||||
|
||||
// Parse prefixes and modes. @ and ! (It's IRCHighWay if you were wondering) are both
|
||||
// mode prefixes and that just makes a mess if leave them for last. It also supports
|
||||
// `multi-prefix`
|
||||
for i, ch := range namestoken {
|
||||
mode := list.isupport.Mode(ch)
|
||||
if mode == 0 {
|
||||
if i != 0 {
|
||||
namestoken = namestoken[i:]
|
||||
}
|
||||
break
|
||||
}
|
||||
|
||||
user.Prefixes += string(ch)
|
||||
user.Modes += string(mode)
|
||||
}
|
||||
|
||||
// Get the nick
|
||||
split := strings.Split(namestoken, "!")
|
||||
user.Nick = split[0]
|
||||
|
||||
// Support `userhost-in-names`
|
||||
if len(split) == 2 {
|
||||
userhost := strings.Split(split[1], "@")
|
||||
if len(userhost) == 2 {
|
||||
user.User = userhost[0]
|
||||
user.Host = userhost[1]
|
||||
}
|
||||
}
|
||||
|
||||
return list.Insert(user)
|
||||
}
|
||||
|
||||
// Insert a user. Modes and prefixes will be cleaned up before insertion.
|
||||
func (list *List) Insert(user User) (ok bool) {
|
||||
if len(user.Modes) > 0 {
|
||||
// IRCv3 promises they'll be ordered by rank in WHO and NAMES replies,
|
||||
// but one can never be too sure with IRC.
|
||||
user.Modes = list.isupport.SortModes(user.Modes)
|
||||
if len(user.Prefixes) < len(user.Modes) {
|
||||
user.Prefixes = list.isupport.Prefixes(user.Modes)
|
||||
} else {
|
||||
user.Prefixes = list.isupport.SortPrefixes(user.Prefixes)
|
||||
}
|
||||
user.updatePrefixedNick()
|
||||
} else {
|
||||
user.Prefixes = ""
|
||||
user.updatePrefixedNick()
|
||||
}
|
||||
|
||||
list.mutex.Lock()
|
||||
defer list.mutex.Unlock()
|
||||
|
||||
if list.index[strings.ToLower(user.Nick)] != nil {
|
||||
return false
|
||||
}
|
||||
|
||||
list.users = append(list.users, &user)
|
||||
list.index[strings.ToLower(user.Nick)] = &user
|
||||
|
||||
if list.autosort {
|
||||
list.sort()
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
// AddMode adds a mode to a user. Redundant modes will be ignored. It returns true if
|
||||
// the user can be found, even if the mode was redundant.
|
||||
func (list *List) AddMode(nick string, mode rune) (ok bool) {
|
||||
if !list.isupport.IsPermissionMode(mode) {
|
||||
return false
|
||||
}
|
||||
|
||||
list.mutex.RLock()
|
||||
defer list.mutex.RUnlock()
|
||||
|
||||
user := list.index[strings.ToLower(nick)]
|
||||
if user == nil {
|
||||
return false
|
||||
}
|
||||
if strings.ContainsRune(user.Modes, mode) {
|
||||
return true
|
||||
}
|
||||
|
||||
prevHighest := user.HighestMode()
|
||||
user.Modes = list.isupport.SortModes(user.Modes + string(mode))
|
||||
user.Prefixes = list.isupport.Prefixes(user.Modes)
|
||||
user.updatePrefixedNick()
|
||||
|
||||
// Only sort if the new mode changed the highest mode.
|
||||
if list.autosort && prevHighest != user.HighestMode() {
|
||||
list.sort()
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
// RemoveMode adds a mode to a user. It returns true if
|
||||
// the user can be found, even if the mode was not there.
|
||||
func (list *List) RemoveMode(nick string, mode rune) (ok bool) {
|
||||
if !list.isupport.IsPermissionMode(mode) {
|
||||
return false
|
||||
}
|
||||
|
||||
list.mutex.RLock()
|
||||
defer list.mutex.RUnlock()
|
||||
|
||||
user := list.index[strings.ToLower(nick)]
|
||||
if user == nil {
|
||||
return false
|
||||
}
|
||||
if !strings.ContainsRune(user.Modes, mode) {
|
||||
return true
|
||||
}
|
||||
|
||||
prevHighest := user.HighestMode()
|
||||
user.Modes = strings.Replace(user.Modes, string(mode), "", 1)
|
||||
user.Prefixes = strings.Replace(user.Prefixes, string(list.isupport.Prefix(mode)), "", 1)
|
||||
user.updatePrefixedNick()
|
||||
|
||||
// Only sort if the new mode changed the highest mode.
|
||||
if list.autosort && prevHighest != user.HighestMode() {
|
||||
list.sort()
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
// Rename renames a user. It will return true if user by `from` exists, or if user by `to` does not exist.
|
||||
func (list *List) Rename(from, to string) (ok bool) {
|
||||
fromKey := strings.ToLower(from)
|
||||
toKey := strings.ToLower(to)
|
||||
|
||||
list.mutex.Lock()
|
||||
defer list.mutex.Unlock()
|
||||
|
||||
// Sanitiy check
|
||||
user := list.index[fromKey]
|
||||
if user == nil {
|
||||
return false
|
||||
}
|
||||
if from == to {
|
||||
return true
|
||||
}
|
||||
existing := list.index[toKey]
|
||||
if existing != nil {
|
||||
return false
|
||||
}
|
||||
|
||||
user.Nick = to
|
||||
user.updatePrefixedNick()
|
||||
|
||||
delete(list.index, fromKey)
|
||||
list.index[toKey] = user
|
||||
|
||||
if list.autosort {
|
||||
list.sort()
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
// Remove a user from the userlist.
|
||||
func (list *List) Remove(nick string) (ok bool) {
|
||||
list.mutex.Lock()
|
||||
defer list.mutex.Unlock()
|
||||
|
||||
user := list.index[strings.ToLower(nick)]
|
||||
if user == nil {
|
||||
return false
|
||||
}
|
||||
|
||||
for i := range list.users {
|
||||
if list.users[i] == user {
|
||||
list.users = append(list.users[:i], list.users[i+1:]...)
|
||||
break
|
||||
}
|
||||
}
|
||||
delete(list.index, strings.ToLower(nick))
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
// User gets a copy of the user by nick, or an empty user if there is none.
|
||||
func (list *List) User(nick string) (u User, ok bool) {
|
||||
list.mutex.RLock()
|
||||
defer list.mutex.RUnlock()
|
||||
|
||||
user := list.index[strings.ToLower(nick)]
|
||||
if user == nil {
|
||||
return User{}, false
|
||||
}
|
||||
|
||||
return *user, true
|
||||
}
|
||||
|
||||
// Users gets a copy of the users in the list's current state.
|
||||
func (list *List) Users() []User {
|
||||
result := make([]User, len(list.users))
|
||||
list.mutex.RLock()
|
||||
for i := range list.users {
|
||||
result[i] = *list.users[i]
|
||||
}
|
||||
list.mutex.RUnlock()
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
// SetAutoSort enables or disables automatic sorting, which by default is enabled.
|
||||
// Dislabing it makes sense when doing a massive operation. Enabling it will trigger
|
||||
// a sort.
|
||||
func (list *List) SetAutoSort(autosort bool) {
|
||||
list.mutex.Lock()
|
||||
list.autosort = autosort
|
||||
list.sort()
|
||||
list.mutex.Unlock()
|
||||
}
|
||||
|
||||
// Clear removes all users in a list.
|
||||
func (list *List) Clear() {
|
||||
list.mutex.Lock()
|
||||
|
||||
list.users = list.users[:0]
|
||||
for key := range list.index {
|
||||
delete(list.index, key)
|
||||
}
|
||||
|
||||
list.mutex.Unlock()
|
||||
}
|
||||
|
||||
func (list *List) sort() {
|
||||
sort.Slice(list.users, func(i, j int) bool {
|
||||
a := list.users[i]
|
||||
b := list.users[j]
|
||||
|
||||
aMode := a.HighestMode()
|
||||
bMode := b.HighestMode()
|
||||
|
||||
if aMode != bMode {
|
||||
return list.isupport.IsModeHigher(aMode, bMode)
|
||||
}
|
||||
|
||||
return strings.ToLower(a.Nick) < strings.ToLower(b.Nick)
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,453 @@
|
||||
package list_test
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"git.aiterp.net/gisle/irc/isupport"
|
||||
"git.aiterp.net/gisle/irc/list"
|
||||
)
|
||||
|
||||
var testISupport isupport.ISupport
|
||||
|
||||
func TestList(t *testing.T) {
|
||||
table := []struct {
|
||||
namestoken string
|
||||
shouldInsert bool
|
||||
user list.User
|
||||
order []string
|
||||
}{
|
||||
{
|
||||
"@+Test!~test@example.com", true,
|
||||
list.User{
|
||||
Nick: "Test",
|
||||
User: "~test",
|
||||
Host: "example.com",
|
||||
Modes: "ov",
|
||||
Prefixes: "@+",
|
||||
PrefixedNick: "@Test",
|
||||
},
|
||||
[]string{"@Test"},
|
||||
},
|
||||
{
|
||||
"+@Test2!~test2@example.com", true,
|
||||
list.User{
|
||||
Nick: "Test2",
|
||||
User: "~test2",
|
||||
Host: "example.com",
|
||||
Modes: "ov",
|
||||
Prefixes: "@+",
|
||||
PrefixedNick: "@Test2",
|
||||
},
|
||||
[]string{"@Test", "@Test2"},
|
||||
},
|
||||
{
|
||||
"+Gissleh", true,
|
||||
list.User{
|
||||
Nick: "Gissleh",
|
||||
User: "",
|
||||
Host: "",
|
||||
Modes: "v",
|
||||
Prefixes: "+",
|
||||
PrefixedNick: "+Gissleh",
|
||||
},
|
||||
[]string{"@Test", "@Test2", "+Gissleh"},
|
||||
},
|
||||
{
|
||||
"Guest!~guest@10.72.3.15", true,
|
||||
list.User{
|
||||
Nick: "Guest",
|
||||
User: "~guest",
|
||||
Host: "10.72.3.15",
|
||||
Modes: "",
|
||||
Prefixes: "",
|
||||
PrefixedNick: "Guest",
|
||||
},
|
||||
[]string{"@Test", "@Test2", "+Gissleh", "Guest"},
|
||||
},
|
||||
{
|
||||
"@AOP!actualIdent@10.32.8.174", true,
|
||||
list.User{
|
||||
Nick: "AOP",
|
||||
User: "actualIdent",
|
||||
Host: "10.32.8.174",
|
||||
Modes: "o",
|
||||
Prefixes: "@",
|
||||
PrefixedNick: "@AOP",
|
||||
},
|
||||
[]string{"@AOP", "@Test", "@Test2", "+Gissleh", "Guest"},
|
||||
},
|
||||
{
|
||||
"@ZOP!actualIdent@10.32.8.174", true,
|
||||
list.User{
|
||||
Nick: "ZOP",
|
||||
User: "actualIdent",
|
||||
Host: "10.32.8.174",
|
||||
Modes: "o",
|
||||
Prefixes: "@",
|
||||
PrefixedNick: "@ZOP",
|
||||
},
|
||||
[]string{"@AOP", "@Test", "@Test2", "@ZOP", "+Gissleh", "Guest"},
|
||||
},
|
||||
{
|
||||
"+ZVoice!~zv@10.32.8.174", true,
|
||||
list.User{
|
||||
Nick: "ZVoice",
|
||||
User: "~zv",
|
||||
Host: "10.32.8.174",
|
||||
Modes: "v",
|
||||
Prefixes: "+",
|
||||
PrefixedNick: "+ZVoice",
|
||||
},
|
||||
[]string{"@AOP", "@Test", "@Test2", "@ZOP", "+Gissleh", "+ZVoice", "Guest"},
|
||||
},
|
||||
{
|
||||
"+ZVoice!~zv@10.32.8.174", false,
|
||||
list.User{},
|
||||
[]string{"@AOP", "@Test", "@Test2", "@ZOP", "+Gissleh", "+ZVoice", "Guest"},
|
||||
},
|
||||
}
|
||||
|
||||
list := list.New(&testISupport)
|
||||
|
||||
for _, row := range table {
|
||||
t.Run("Insert_"+row.namestoken, func(t *testing.T) {
|
||||
ok := list.InsertFromNamesToken(row.namestoken)
|
||||
if ok && !row.shouldInsert {
|
||||
t.Error("Insert should have failed!")
|
||||
return
|
||||
}
|
||||
if !ok && row.shouldInsert {
|
||||
t.Error("Insert should NOT have failed!")
|
||||
return
|
||||
}
|
||||
|
||||
if row.shouldInsert {
|
||||
user, ok := list.User(row.user.Nick)
|
||||
if !ok {
|
||||
t.Error("Could not find user.")
|
||||
return
|
||||
}
|
||||
|
||||
jsonA, _ := json.MarshalIndent(user, "", " ")
|
||||
jsonB, _ := json.MarshalIndent(row.user, "", " ")
|
||||
|
||||
t.Log("result =", string(jsonA))
|
||||
|
||||
if string(jsonA) != string(jsonB) {
|
||||
t.Log("expectation =", string(jsonB))
|
||||
t.Error("Users did not match!")
|
||||
}
|
||||
}
|
||||
|
||||
order := make([]string, 0, 16)
|
||||
for _, user := range list.Users() {
|
||||
order = append(order, user.PrefixedNick)
|
||||
}
|
||||
|
||||
orderA := strings.Join(order, ", ")
|
||||
orderB := strings.Join(row.order, ", ")
|
||||
|
||||
t.Log("order =", orderA)
|
||||
if orderA != orderB {
|
||||
t.Log("orderExpected =", orderB)
|
||||
t.Error("Order did not match!")
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
modeTable := []struct {
|
||||
add bool
|
||||
mode rune
|
||||
nick string
|
||||
ok bool
|
||||
order []string
|
||||
}{
|
||||
{
|
||||
true, 'o', "Gissleh", true,
|
||||
[]string{"@AOP", "@Gissleh", "@Test", "@Test2", "@ZOP", "+ZVoice", "Guest"},
|
||||
},
|
||||
{
|
||||
false, 'o', "Gissleh", true,
|
||||
[]string{"@AOP", "@Test", "@Test2", "@ZOP", "+Gissleh", "+ZVoice", "Guest"},
|
||||
},
|
||||
{
|
||||
true, 'o', "InvalidNick", false,
|
||||
[]string{"@AOP", "@Test", "@Test2", "@ZOP", "+Gissleh", "+ZVoice", "Guest"},
|
||||
},
|
||||
{
|
||||
true, 'v', "AOP", true,
|
||||
[]string{"@AOP", "@Test", "@Test2", "@ZOP", "+Gissleh", "+ZVoice", "Guest"},
|
||||
},
|
||||
{
|
||||
true, 'v', "ZOP", true,
|
||||
[]string{"@AOP", "@Test", "@Test2", "@ZOP", "+Gissleh", "+ZVoice", "Guest"},
|
||||
},
|
||||
{
|
||||
true, 'v', "Guest", true,
|
||||
[]string{"@AOP", "@Test", "@Test2", "@ZOP", "+Gissleh", "+Guest", "+ZVoice"},
|
||||
},
|
||||
{
|
||||
true, 'v', "Test", true,
|
||||
[]string{"@AOP", "@Test", "@Test2", "@ZOP", "+Gissleh", "+Guest", "+ZVoice"},
|
||||
},
|
||||
{
|
||||
false, 'v', "Test", true,
|
||||
[]string{"@AOP", "@Test", "@Test2", "@ZOP", "+Gissleh", "+Guest", "+ZVoice"},
|
||||
},
|
||||
{
|
||||
false, 'o', "Test", true,
|
||||
[]string{"@AOP", "@Test2", "@ZOP", "+Gissleh", "+Guest", "+ZVoice", "Test"},
|
||||
},
|
||||
{
|
||||
false, 'o', "AOP", true,
|
||||
[]string{"@Test2", "@ZOP", "+AOP", "+Gissleh", "+Guest", "+ZVoice", "Test"},
|
||||
},
|
||||
{
|
||||
true, 'x', "AOP", false,
|
||||
[]string{"@Test2", "@ZOP", "+AOP", "+Gissleh", "+Guest", "+ZVoice", "Test"},
|
||||
},
|
||||
{
|
||||
false, 'x', "ZOP", false,
|
||||
[]string{"@Test2", "@ZOP", "+AOP", "+Gissleh", "+Guest", "+ZVoice", "Test"},
|
||||
},
|
||||
{
|
||||
true, 'o', "UNKNOWN_USER", false,
|
||||
[]string{"@Test2", "@ZOP", "+AOP", "+Gissleh", "+Guest", "+ZVoice", "Test"},
|
||||
},
|
||||
{
|
||||
false, 'o', "UNKNOWN_USER", false,
|
||||
[]string{"@Test2", "@ZOP", "+AOP", "+Gissleh", "+Guest", "+ZVoice", "Test"},
|
||||
},
|
||||
}
|
||||
|
||||
for i, row := range modeTable {
|
||||
t.Run(fmt.Sprintf("Mode_%d_%s", i, row.nick), func(t *testing.T) {
|
||||
var ok bool
|
||||
|
||||
if row.add {
|
||||
ok = list.AddMode(row.nick, row.mode)
|
||||
} else {
|
||||
ok = list.RemoveMode(row.nick, row.mode)
|
||||
}
|
||||
|
||||
if ok && !row.ok {
|
||||
t.Error("This should be not ok, but it is ok.")
|
||||
}
|
||||
if !ok && row.ok {
|
||||
t.Error("This is not ok.")
|
||||
}
|
||||
|
||||
order := make([]string, 0, 16)
|
||||
for _, user := range list.Users() {
|
||||
order = append(order, user.PrefixedNick)
|
||||
}
|
||||
|
||||
orderA := strings.Join(order, ", ")
|
||||
orderB := strings.Join(row.order, ", ")
|
||||
|
||||
t.Log("order =", orderA)
|
||||
if orderA != orderB {
|
||||
t.Log("orderExpected =", orderB)
|
||||
t.Error("Order did not match!")
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
renameTable := []struct {
|
||||
from string
|
||||
to string
|
||||
ok bool
|
||||
order []string
|
||||
}{
|
||||
{
|
||||
"ZOP", "AAOP", true,
|
||||
[]string{"@AAOP", "@Test2", "+AOP", "+Gissleh", "+Guest", "+ZVoice", "Test"},
|
||||
},
|
||||
{
|
||||
"Test", "ATest", true,
|
||||
[]string{"@AAOP", "@Test2", "+AOP", "+Gissleh", "+Guest", "+ZVoice", "ATest"},
|
||||
},
|
||||
{
|
||||
"AOP", "ZOP", true,
|
||||
[]string{"@AAOP", "@Test2", "+Gissleh", "+Guest", "+ZOP", "+ZVoice", "ATest"},
|
||||
},
|
||||
{
|
||||
"AOP", "ZOP", false,
|
||||
[]string{"@AAOP", "@Test2", "+Gissleh", "+Guest", "+ZOP", "+ZVoice", "ATest"},
|
||||
},
|
||||
{
|
||||
"ATest", "Test", true,
|
||||
[]string{"@AAOP", "@Test2", "+Gissleh", "+Guest", "+ZOP", "+ZVoice", "Test"},
|
||||
},
|
||||
{
|
||||
"Test2", "AAATest", true,
|
||||
[]string{"@AAATest", "@AAOP", "+Gissleh", "+Guest", "+ZOP", "+ZVoice", "Test"},
|
||||
},
|
||||
{
|
||||
"ZOP", "AAATest", false,
|
||||
[]string{"@AAATest", "@AAOP", "+Gissleh", "+Guest", "+ZOP", "+ZVoice", "Test"},
|
||||
},
|
||||
{
|
||||
"AAATest", "AAATest", true,
|
||||
[]string{"@AAATest", "@AAOP", "+Gissleh", "+Guest", "+ZOP", "+ZVoice", "Test"},
|
||||
},
|
||||
}
|
||||
|
||||
for i, row := range renameTable {
|
||||
t.Run(fmt.Sprintf("Rename_%d_%s_%s", i, row.from, row.to), func(t *testing.T) {
|
||||
ok := list.Rename(row.from, row.to)
|
||||
if ok && !row.ok {
|
||||
t.Error("This should be not ok, but it is ok.")
|
||||
}
|
||||
if !ok && row.ok {
|
||||
t.Error("This is not ok.")
|
||||
}
|
||||
|
||||
order := make([]string, 0, 16)
|
||||
for _, user := range list.Users() {
|
||||
order = append(order, user.PrefixedNick)
|
||||
}
|
||||
|
||||
orderA := strings.Join(order, ", ")
|
||||
orderB := strings.Join(row.order, ", ")
|
||||
|
||||
t.Log("order =", orderA)
|
||||
if orderA != orderB {
|
||||
t.Log("orderExpected =", orderB)
|
||||
t.Error("Order did not match!")
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
removeTable := []struct {
|
||||
nick string
|
||||
ok bool
|
||||
order []string
|
||||
}{
|
||||
{
|
||||
"AAOP", true,
|
||||
[]string{"@AAATest", "+Gissleh", "+Guest", "+ZOP", "+ZVoice", "Test"},
|
||||
},
|
||||
{
|
||||
"AAOP", false,
|
||||
[]string{"@AAATest", "+Gissleh", "+Guest", "+ZOP", "+ZVoice", "Test"},
|
||||
},
|
||||
{
|
||||
"Guest", true,
|
||||
[]string{"@AAATest", "+Gissleh", "+ZOP", "+ZVoice", "Test"},
|
||||
},
|
||||
{
|
||||
"ZOP", true,
|
||||
[]string{"@AAATest", "+Gissleh", "+ZVoice", "Test"},
|
||||
},
|
||||
{
|
||||
"ATest", false,
|
||||
[]string{"@AAATest", "+Gissleh", "+ZVoice", "Test"},
|
||||
},
|
||||
{
|
||||
"Test", true,
|
||||
[]string{"@AAATest", "+Gissleh", "+ZVoice"},
|
||||
},
|
||||
}
|
||||
|
||||
for i, row := range removeTable {
|
||||
t.Run(fmt.Sprintf("Rename_%d_%s", i, row.nick), func(t *testing.T) {
|
||||
ok := list.Remove(row.nick)
|
||||
if ok && !row.ok {
|
||||
t.Error("This should be not ok, but it is ok.")
|
||||
}
|
||||
if !ok && row.ok {
|
||||
t.Error("This is not ok.")
|
||||
}
|
||||
|
||||
order := make([]string, 0, 16)
|
||||
for _, user := range list.Users() {
|
||||
order = append(order, user.PrefixedNick)
|
||||
}
|
||||
|
||||
if _, ok := list.User(row.nick); ok {
|
||||
t.Error("User is still there")
|
||||
}
|
||||
|
||||
orderA := strings.Join(order, ", ")
|
||||
orderB := strings.Join(row.order, ", ")
|
||||
|
||||
t.Log("order =", orderA)
|
||||
if orderA != orderB {
|
||||
t.Log("orderExpected =", orderB)
|
||||
t.Error("Order did not match!")
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
t.Run("AutoSort", func(t *testing.T) {
|
||||
list.SetAutoSort(false)
|
||||
|
||||
if ok := list.InsertFromNamesToken("@+AAAAAAAAA"); !ok {
|
||||
t.Error("Failed to insert user @+AAAAAAAAA")
|
||||
}
|
||||
|
||||
users := list.Users()
|
||||
last := users[len(users)-1]
|
||||
|
||||
if last.PrefixedNick != "@AAAAAAAAA" {
|
||||
t.Error("@+AAAAAAAAA isn't last, "+last.PrefixedNick, "is.")
|
||||
}
|
||||
|
||||
list.SetAutoSort(true)
|
||||
|
||||
users = list.Users()
|
||||
last = users[len(users)-1]
|
||||
|
||||
if last.PrefixedNick == "@AAAAAAAAA" {
|
||||
t.Error("@+AAAAAAAAA is still last after autosort was enabled. That's not right.")
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("Clear", func(t *testing.T) {
|
||||
list.Clear()
|
||||
|
||||
if len(list.Users()) != 0 {
|
||||
t.Error("Clear failed!")
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func init() {
|
||||
isupportData := map[string]string{
|
||||
"FNC": "",
|
||||
"SAFELIST": "",
|
||||
"ELIST": "CTU",
|
||||
"MONITOR": "100",
|
||||
"WHOX": "",
|
||||
"ETRACE": "",
|
||||
"KNOCK": "",
|
||||
"CHANTYPES": "#&",
|
||||
"EXCEPTS": "",
|
||||
"INVEX": "",
|
||||
"CHANMODES": "eIbq,k,flj,CFLNPQcgimnprstz",
|
||||
"CHANLIMIT": "#&:15",
|
||||
"PREFIX": "(ov)@+",
|
||||
"MAXLIST": "bqeI:100",
|
||||
"MODES": "4",
|
||||
"NETWORK": "TestServer",
|
||||
"STATUSMSG": "@+",
|
||||
"CALLERID": "g",
|
||||
"CASEMAPPING": "rfc1459",
|
||||
"NICKLEN": "30",
|
||||
"MAXNICKLEN": "31",
|
||||
"CHANNELLEN": "50",
|
||||
"TOPICLEN": "390",
|
||||
"DEAF": "D",
|
||||
"TARGMAX": "NAMES:1,LIST:1,KICK:1,WHOIS:1,PRIVMSG:4,NOTICE:4,ACCEPT:,MONITOR:",
|
||||
"EXTBAN": "$,&acjmorsuxz|",
|
||||
"CLIENTVER": "3.0",
|
||||
}
|
||||
|
||||
for key, value := range isupportData {
|
||||
testISupport.Set(key, value)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
package list
|
||||
|
||||
// A User represents a member of a userlist.
|
||||
type User struct {
|
||||
Nick string `json:"nick"`
|
||||
User string `json:"user,omitempty"`
|
||||
Host string `json:"host,omitempty"`
|
||||
Account string `json:"account,omitempty"`
|
||||
Modes string `json:"modes"`
|
||||
Prefixes string `json:"prefixes"`
|
||||
PrefixedNick string `json:"prefixedNick"`
|
||||
}
|
||||
|
||||
// HighestMode returns the highest mode.
|
||||
func (user *User) HighestMode() rune {
|
||||
if len(user.Modes) == 0 {
|
||||
return 0
|
||||
}
|
||||
|
||||
return rune(user.Modes[0])
|
||||
}
|
||||
|
||||
// PrefixedNick gets the full nick.
|
||||
func (user *User) updatePrefixedNick() {
|
||||
if len(user.Prefixes) == 0 {
|
||||
user.PrefixedNick = user.Nick
|
||||
return
|
||||
}
|
||||
|
||||
user.PrefixedNick = string(user.Prefixes[0]) + user.Nick
|
||||
}
|
||||
Reference in New Issue
Block a user