2016-06-15 13:50:56 +02:00
|
|
|
// Copyright (c) 2012-2014 Jeremy Latt
|
|
|
|
// released under the MIT license
|
|
|
|
|
2014-03-06 22:55:25 +01:00
|
|
|
package irc
|
|
|
|
|
|
|
|
type WhoWasList struct {
|
|
|
|
buffer []*WhoWas
|
2014-04-10 20:53:34 +02:00
|
|
|
start int
|
|
|
|
end int
|
2014-03-06 22:55:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
type WhoWas struct {
|
2014-03-09 21:45:36 +01:00
|
|
|
nickname Name
|
|
|
|
username Name
|
|
|
|
hostname Name
|
2016-06-19 13:59:18 +02:00
|
|
|
realname string
|
2014-03-06 22:55:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewWhoWasList(size uint) *WhoWasList {
|
|
|
|
return &WhoWasList{
|
|
|
|
buffer: make([]*WhoWas, size),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (list *WhoWasList) Append(client *Client) {
|
|
|
|
list.buffer[list.end] = &WhoWas{
|
|
|
|
nickname: client.Nick(),
|
|
|
|
username: client.username,
|
|
|
|
hostname: client.hostname,
|
|
|
|
realname: client.realname,
|
|
|
|
}
|
2014-04-10 20:53:34 +02:00
|
|
|
list.end = (list.end + 1) % len(list.buffer)
|
2014-03-06 22:55:25 +01:00
|
|
|
if list.end == list.start {
|
2014-04-10 20:53:34 +02:00
|
|
|
list.start = (list.end + 1) % len(list.buffer)
|
2014-03-06 22:55:25 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-09 21:45:36 +01:00
|
|
|
func (list *WhoWasList) Find(nickname Name, limit int64) []*WhoWas {
|
2014-03-06 22:55:25 +01:00
|
|
|
results := make([]*WhoWas, 0)
|
|
|
|
for whoWas := range list.Each() {
|
|
|
|
if nickname != whoWas.nickname {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
results = append(results, whoWas)
|
|
|
|
if int64(len(results)) >= limit {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return results
|
|
|
|
}
|
|
|
|
|
2014-04-10 20:53:34 +02:00
|
|
|
func (list *WhoWasList) prev(index int) int {
|
2014-03-06 22:55:25 +01:00
|
|
|
index -= 1
|
|
|
|
if index < 0 {
|
2014-04-10 20:53:34 +02:00
|
|
|
index += len(list.buffer)
|
2014-03-06 22:55:25 +01:00
|
|
|
}
|
|
|
|
return index
|
|
|
|
}
|
|
|
|
|
|
|
|
// Iterate the buffer in reverse.
|
|
|
|
func (list *WhoWasList) Each() <-chan *WhoWas {
|
|
|
|
ch := make(chan *WhoWas)
|
|
|
|
go func() {
|
|
|
|
defer close(ch)
|
|
|
|
if list.start == list.end {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
start := list.prev(list.end)
|
|
|
|
end := list.prev(list.start)
|
|
|
|
for start != end {
|
|
|
|
ch <- list.buffer[start]
|
|
|
|
start = list.prev(start)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
return ch
|
|
|
|
}
|