-
Notifications
You must be signed in to change notification settings - Fork 1
/
nicklist.go
192 lines (165 loc) · 3.58 KB
/
nicklist.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
package main
import (
"regexp"
"sort"
"strings"
"sync"
)
type nick struct {
prefix, name string
host string
}
func (n *nick) String() string {
if len(n.prefix) > 0 {
return string(n.prefix[0]) + n.name
}
return n.name
}
var nickRegex = regexp.MustCompile("^([~&@%+]*)(.+)$")
func newNick(prefixed string) *nick {
m := nickRegex.FindAllStringSubmatch(prefixed, -1)
return &nick{m[0][1], m[0][2], ""}
}
type nickList struct {
data []string // underlying sorted string array for searching
lookup map[string]*nick // lookup map for prefixes
mu *sync.Mutex
}
func newNickList() *nickList {
nl := &nickList{
data: []string{},
lookup: map[string]*nick{},
mu: &sync.Mutex{},
}
return nl
}
func (nl *nickList) SetHost(nick, host string) {
nl.mu.Lock()
defer nl.mu.Unlock()
n, ok := nl.lookup[nick]
if ok {
n.host = host
}
}
func (nl *nickList) Add(n string) {
nl.mu.Lock()
defer nl.mu.Unlock()
nick := newNick(n)
i := sort.SearchStrings(nl.data, nick.name)
if i < len(nl.data) {
if nl.data[i] != nick.name {
nl.data = append(nl.data[:i], append([]string{nick.name}, nl.data[i:]...)...)
nl.lookup[nick.name] = nick
} else if nick.prefix != "" && nl.lookup[nick.name].prefix != nick.prefix {
nl.lookup[nick.name] = nick
}
} else {
nl.data = append(nl.data, nick.name)
nl.lookup[nick.name] = nick
}
}
func (nl *nickList) Remove(n string) {
nl.mu.Lock()
defer nl.mu.Unlock()
if nl.Has(n) {
nick := newNick(n)
i := sort.SearchStrings(nl.data, nick.name)
nl.data = append(nl.data[0:i], nl.data[i+1:]...)
delete(nl.lookup, n)
}
}
func (nl *nickList) Has(n string) bool {
nick := newNick(n)
i := sort.SearchStrings(nl.data, nick.name)
return i < len(nl.data) && nl.data[i] == nick.name
}
func (nl *nickList) Get(n string) *nick {
nl.mu.Lock()
defer nl.mu.Unlock()
nick := nl.lookup[n]
return nick
}
func (nl *nickList) Set(n string, newNick *nick) {
nl.mu.Lock()
oldNick, ok := nl.lookup[n]
nl.mu.Unlock()
if !ok {
panic("nick \"" + n + "\" not in lookup table")
}
if oldNick.name != newNick.name {
nl.Remove(oldNick.name)
nl.Add(newNick.String())
} else if oldNick.prefix != newNick.prefix {
nl.mu.Lock()
nl.lookup[n] = newNick
nl.mu.Unlock()
}
}
func (nl *nickList) Search(search string) []string {
res := []string{}
for _, nick := range nl.data {
if strings.HasPrefix(strings.ToLower(nick), strings.ToLower(search)) {
res = append(res, nick)
}
}
return res
}
func (nl *nickList) StringSlice() []string {
nl.mu.Lock()
defer nl.mu.Unlock()
n := nickListByPrefix{}
for _, nick := range nl.lookup {
nick.prefix = sortPrefix(nick.prefix)
n = append(n, nick)
}
sort.Sort(n)
s := []string{}
for _, nick := range n {
s = append(s, nick.String())
}
return s
}
func cmpPrefix(a, b byte) bool {
switch a {
case '~':
return true
case '&':
return b != '~'
case '@':
return b != '~' && b != '&'
case '%':
return b != '~' && b != '&' && b != '@'
case '+':
return false
}
panic("unhandled prefix: " + string(a))
return false
}
func sortPrefix(prefix string) string {
s := []byte(prefix)
sort.Slice(s, func(i, j int) bool {
a, b := s[i], s[j]
return cmpPrefix(a, b)
})
return string(s)
}
type nickListByPrefix []*nick
func (nl nickListByPrefix) Len() int {
return len(nl)
}
func (nl nickListByPrefix) Less(i, j int) bool {
a, b := nl[i].prefix, nl[j].prefix
if a == b {
return nl[i].name < nl[j].name
}
if len(a) == 0 {
return false
}
if len(b) == 0 {
return true
}
return cmpPrefix(a[0], b[0])
}
func (nl nickListByPrefix) Swap(i, j int) {
nl[i], nl[j] = nl[j], nl[i]
}