forked from MemeLabs/overrustlelogs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnicklist.go
188 lines (165 loc) · 3.85 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
package common
import (
"bytes"
"errors"
"io"
"os"
"path/filepath"
"strings"
"time"
)
var empty struct{}
// NickStore nick list data
type NickStore interface {
Add(string)
Remove(string)
}
// ReadNickList adds nicks from the disk
func ReadNickList(n NickStore, path string) error {
buf, err := ReadCompressedFile(path)
if err != nil {
return err
}
offset := 0
for i, v := range buf {
if v == 0 {
n.Add(string(buf[offset:i]))
offset = i + 1
}
}
return nil
}
// NickList list of unique nicks
type NickList map[string]struct{}
// Add append nick to list
func (n NickList) Add(nick string) {
n[nick] = empty
}
// Remove deletes a nick from the list
func (n NickList) Remove(nick string) {
delete(n, nick)
}
// WriteTo writes nicks to the disk
func (n NickList) WriteTo(path string) error {
buf := bytes.NewBuffer([]byte{})
for nick := range n {
buf.WriteString(nick)
buf.WriteByte(0)
}
f, err := WriteCompressedFile(path+".writing", buf.Bytes())
if err != nil {
return err
}
return os.Rename(f.Name(), strings.Replace(f.Name(), ".writing", "", -1))
}
// NickListLower lower case nick list for case insensitive search
type NickListLower map[string]struct{}
// Add implement NickStore
func (n NickListLower) Add(nick string) {
n[strings.ToLower(nick)] = empty
}
// Remove deletes a nick from the list
func (n NickListLower) Remove(nick string) {
delete(n, strings.ToLower(nick))
}
// NickCaseMap map from lower case nicks
type NickCaseMap map[string]string
// Add implement NickStore
func (n NickCaseMap) Add(nick string) {
n[strings.ToLower(nick)] = nick
}
// Remove deletes a nick from the list
func (n NickCaseMap) Remove(nick string) {
delete(n, strings.ToLower(nick))
}
// NickSearch scans nick indexes in reverse chronological order
type NickSearch struct {
nick string
path string
months map[string]struct{}
date time.Time
}
// NewNickSearch create scanner
func NewNickSearch(path string, nick string) (*NickSearch, error) {
f, err := os.Open(path)
if err != nil {
return nil, err
}
names, err := f.Readdirnames(0)
if err != nil {
return nil, err
}
months := make(map[string]struct{}, len(names))
for _, name := range names {
months[name] = struct{}{}
}
return &NickSearch{
nick: strings.ToLower(nick),
path: path,
months: months,
date: time.Now().UTC().Add(24 * time.Hour),
}, nil
}
// Next find next occurrence
func (n *NickSearch) Next() (*NickSearchResult, error) {
for {
n.date = n.date.Add(-24 * time.Hour)
if _, ok := n.months[n.date.Format("January 2006")]; !ok {
return nil, io.EOF
}
nicks := NickCaseMap{}
ReadNickList(nicks, n.path+n.date.Format("/January 2006/2006-01-02")+".nicks")
if nick, ok := nicks[n.nick]; ok {
return &NickSearchResult{nick, n.date}, nil
}
}
}
// Month searches for a nick in m
func (n *NickSearch) Month(m string) (string, error) {
if _, ok := n.months[m]; !ok {
return "", errors.New("month not found")
}
f, err := os.Open(filepath.Join(n.path, m))
if err != nil {
return "", err
}
nickfiles, err := f.Readdirnames(0)
if err != nil {
return "", err
}
for _, file := range nickfiles {
if !strings.Contains(file, ".nicks") {
continue
}
nicks := NickCaseMap{}
err := ReadNickList(nicks, filepath.Join(n.path, m, file))
if err != nil {
return "", err
}
if nick, ok := nicks[n.nick]; ok {
return nick, nil
}
}
return "", errors.New("user not found in " + m)
}
// NickSearchResult nick/path data
type NickSearchResult struct {
nick string
date time.Time
}
// Nick case corrected nick
func (n *NickSearchResult) Nick() string {
return n.nick
}
// Month path string
func (n *NickSearchResult) Month() string {
return n.date.Format("January 2006")
}
// Day path string
func (n *NickSearchResult) Day() string {
return n.date.Format("2006-01-02")
}
// Date time object
func (n *NickSearchResult) Date() time.Time {
return n.date
}