-
Notifications
You must be signed in to change notification settings - Fork 3
/
database.go
310 lines (273 loc) · 5.97 KB
/
database.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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
package main
import (
"database/sql"
"io/ioutil"
"strings"
"sync"
"time"
"github.com/jmoiron/sqlx"
_ "github.com/mattn/go-sqlite3"
)
type database struct {
db *sqlx.DB
insertban chan *dbInsertBan
deleteban chan *dbDeleteBan
sync.Mutex
}
type dbInsertBan struct {
uid Userid
targetuid Userid
ipaddress *sql.NullString
reason string
starttime int64
endtime int64
retries uint8
}
type dbDeleteBan struct {
uid Userid
}
var db = &database{
insertban: make(chan *dbInsertBan, 10),
deleteban: make(chan *dbDeleteBan, 10),
}
func initDatabase(dbfile string, init bool) {
db.db = sqlx.MustConnect("sqlite3", dbfile)
if init {
sql, err := ioutil.ReadFile("db-init.sql")
if err != nil {
panic(err)
}
stmt, err := db.db.Prepare(string(sql))
if err != nil {
panic(err)
}
stmt.Exec()
stmt.Close()
}
bans.loadActive()
go db.runInsertBan() // TODO ???
go db.runDeleteBan()
}
func (db *database) getStatement(name string, sql string) *sql.Stmt {
db.Lock()
stmt, err := db.db.Prepare(sql)
db.Unlock()
if err != nil {
D("Unable to create", name, "statement:", err)
time.Sleep(100 * time.Millisecond)
return db.getStatement(name, sql)
}
return stmt
}
func (db *database) getInsertBanStatement() *sql.Stmt {
return db.getStatement("insertBan", `
INSERT INTO bans
VALUES (
?,
?,
?,
?,
?,
?
)`)
}
func (db *database) getDeleteBanStatement() *sql.Stmt {
return db.getStatement("deleteBan", `
UPDATE bans
SET endtimestamp = strftime('%s', 'now')
WHERE
targetuserid = ? AND
(
endtimestamp IS NULL OR
endtimestamp > strftime('%s', 'now')
)
`)
}
func (db *database) runInsertBan() {
t := time.NewTimer(time.Minute)
stmt := db.getInsertBanStatement()
for {
select {
case <-t.C:
stmt.Close()
stmt = nil
case data := <-db.insertban:
t.Reset(time.Minute)
if stmt == nil {
stmt = db.getInsertBanStatement()
}
if data.retries > 2 {
continue
}
db.Lock()
_, err := stmt.Exec(data.uid, data.targetuid, data.ipaddress, data.reason, data.starttime, data.endtime)
db.Unlock()
if err != nil {
data.retries++
D("Unable to insert event", err)
go (func() {
db.insertban <- data
})()
}
}
}
}
func (db *database) runDeleteBan() {
t := time.NewTimer(time.Minute)
stmt := db.getDeleteBanStatement()
for {
select {
case <-t.C:
stmt.Close()
stmt = nil
case data := <-db.deleteban:
t.Reset(time.Minute)
if stmt == nil {
stmt = db.getDeleteBanStatement()
}
db.Lock()
_, err := stmt.Exec(data.uid)
db.Unlock()
if err != nil {
D("Unable to insert event", err)
go (func() {
db.deleteban <- data
})()
}
}
}
}
func (db *database) insertBan(uid Userid, targetuid Userid, ban *BanIn, ip string) {
ipaddress := &sql.NullString{}
if ban.BanIP && len(ip) != 0 {
ipaddress.String = ip
ipaddress.Valid = true
}
starttime := time.Now().UTC()
var endtimestamp int64
if ban.Ispermanent {
endtimestamp = getFuturetimeUTC().Unix()
} else {
endtimestamp = starttime.Add(time.Duration(ban.Duration)).Unix()
}
starttimestamp := starttime.Unix()
db.insertban <- &dbInsertBan{uid, targetuid, ipaddress, ban.Reason, starttimestamp, endtimestamp, 0}
}
func (db *database) deleteBan(targetuid Userid) {
db.deleteban <- &dbDeleteBan{targetuid}
}
func (db *database) getBans(f func(Userid, sql.NullString, time.Time)) {
db.Lock()
defer db.Unlock()
rows, err := db.db.Query(`
SELECT
targetuserid,
ipaddress,
endtimestamp
FROM bans
WHERE
endtimestamp IS NULL OR
endtimestamp > strftime('%s', 'now')
GROUP BY targetuserid, ipaddress
`)
if err != nil {
D("Unable to get active bans: ", err)
return
}
defer rows.Close()
for rows.Next() {
var uid Userid
var ipaddress sql.NullString
var endtimestamp time.Time
var t int64
err = rows.Scan(&uid, &ipaddress, &t)
if err != nil {
D("Unable to scan bans row: ", err)
continue
}
endtimestamp = time.Unix(t, 0).UTC()
f(uid, ipaddress, endtimestamp)
}
}
func (db *database) getUser(nick string) (Userid, bool) {
stmt := db.getStatement("getUser", `
SELECT
u.userid,
instr(u.features, 'admin') OR
instr(u.features, 'protected')
FROM users AS u
WHERE u.nick = ?
`)
db.Lock()
defer stmt.Close()
defer db.Unlock()
var uid int32
var protected bool
err := stmt.QueryRow(nick).Scan(&uid, &protected)
if err != nil {
D("error looking up", nick, err)
return 0, false
}
return Userid(uid), protected
}
// TODO ... for uuid-id conversion
func (db *database) getUserInfo(uuid string) ([]string, int, error) {
stmt := db.getStatement("getUserInfo", `
SELECT
userid, features
FROM users
WHERE uuid = ?
`)
db.Lock()
defer stmt.Close()
defer db.Unlock()
var f string
var uid int
err := stmt.QueryRow(uuid).Scan(&uid, &f)
if err != nil {
D("features err", err)
return []string{}, -1, err // TODO -1 implications...
}
features := strings.Split(f, ",") // TODO features are placed into db like this...
return features, uid, nil
}
func (db *database) newUser(uuid string, name string, ip string) error {
// TODO
// chat-internal uid is autoincrement primary key...
// UNIQUE check on uuid makes sure of no double intserts.
stmt := db.getStatement("newUser", `
INSERT INTO users (
uuid, nick, features, firstlogin, lastlogin, lastip
)
VALUES (
?, ?, "", strftime('%s', 'now'), strftime('%s', 'now'), ?
)
`)
db.Lock()
defer stmt.Close()
defer db.Unlock()
_, err := stmt.Exec(uuid, name, ip)
if err != nil {
D("newuser err", err) // TODO this is actually expected and normal for existing users...
return err
}
return nil
}
func (db *database) updateUser(id Userid, name string, ip string) error {
stmt := db.getStatement("updateUser", `
UPDATE users SET
nick = ?,
lastlogin = strftime('%s', 'now'),
lastip = ?
WHERE userid = ?
`)
db.Lock()
defer stmt.Close()
defer db.Unlock()
_, err := stmt.Exec(name, ip, id)
if err != nil {
D("updateUser err", err)
return err
}
return nil
}