forked from tinode/chat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.go
583 lines (511 loc) · 13.1 KB
/
utils.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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
// Generic data manipulation utilities.
package main
import (
"errors"
"path/filepath"
"regexp"
"sort"
"strconv"
"strings"
"time"
"unicode"
"unicode/utf8"
"github.com/tinode/chat/server/auth"
"github.com/tinode/chat/server/store/types"
)
var tagPrefixRegexp = regexp.MustCompile(`^([a-z]\w{0,5}):\S`)
// Convert a list of IDs into ranges
func delrangeDeserialize(in []types.Range) []MsgDelRange {
if len(in) == 0 {
return nil
}
var out []MsgDelRange
for _, r := range in {
out = append(out, MsgDelRange{LowId: r.Low, HiId: r.Hi})
}
return out
}
func delrangeSerialize(in []MsgDelRange) []types.Range {
if len(in) == 0 {
return nil
}
var out []types.Range
for _, r := range in {
if r.HiId <= r.LowId+1 {
// High end is exclusive, i.e. range 1..2 is equivalent to 1.
out = append(out, types.Range{Low: r.LowId})
} else {
out = append(out, types.Range{Low: r.LowId, Hi: r.HiId})
}
}
return out
}
// Trim whitespace, remove short/empty tags and duplicates, convert to lowercase, ensure
// the number of tags does not exceed the maximum.
func normalizeTags(src []string) types.StringSlice {
if len(src) == 0 {
return nil
}
// Make sure the number of tags does not exceed the maximum.
// Technically it may result in fewer tags than the maximum due to empty tags and
// duplicates, but that's user's fault.
if len(src) > globals.maxTagCount {
src = src[:globals.maxTagCount]
}
// Trim whitespace and force to lowercase.
for i := 0; i < len(src); i++ {
src[i] = strings.ToLower(strings.TrimSpace(src[i]))
}
// Sort tags
sort.Strings(src)
// Remove short, invalid tags and de-dupe keeping the order. It may result in fewer tags than could have
// been if length were enforced later, but that's client's fault.
var prev string
var dst []string
for _, curr := range src {
if isNullValue(curr) {
// Return non-nil empty array
return make([]string, 0, 1)
}
// Unicode handling
ucurr := []rune(curr)
// Make sure the tag starts with a letter or a number.
if !unicode.IsLetter(ucurr[0]) && !unicode.IsDigit(ucurr[0]) {
continue
}
// Enforce length in characters, not in bytes.
if len(ucurr) < minTagLength || len(ucurr) > maxTagLength || curr == prev {
continue
}
dst = append(dst, curr)
prev = curr
}
return types.StringSlice(dst)
}
// stringDelta extracts the slices of added and removed strings from two slices:
// added := newSlice - (oldSlice & newSlice) -- present in new but missing in old
// removed := oldSlice - (oldSlice & newSlice) -- present in old but missing in new
func stringSliceDelta(rold, rnew []string) (added, removed []string) {
if len(rold) == 0 && len(rnew) == 0 {
return nil, nil
}
if len(rold) == 0 {
return rnew, nil
}
if len(rnew) == 0 {
return nil, rold
}
sort.Strings(rold)
sort.Strings(rnew)
// Match old slice against the new slice and separate removed strings from added.
o, n := 0, 0
lold, lnew := len(rold), len(rnew)
for o < lold || n < lnew {
if o == lold || (n < lnew && rold[o] > rnew[n]) {
// Present in new, missing in old: added
added = append(added, rnew[n])
n++
} else if n == lnew || rold[o] < rnew[n] {
// Present in old, missing in new: removed
removed = append(removed, rold[o])
o++
} else {
// present in both
if o < lold {
o++
}
if n < lnew {
n++
}
}
}
return added, removed
}
// restrictedTagsEqual checks if two sets of tags contain the same set of restricted tags:
// true - same, false - different.
func restrictedTagsEqual(oldTags, newTags []string, namespaces map[string]bool) bool {
rold := filterRestrictedTags(oldTags, namespaces)
rnew := filterRestrictedTags(newTags, namespaces)
if len(rold) != len(rnew) {
return false
}
sort.Strings(rold)
sort.Strings(rnew)
// Match old tags against the new tags.
for i := 0; i < len(rnew); i++ {
if rold[i] != rnew[i] {
return false
}
}
return true
}
// Process credentials for correctness: remove duplicates and unknown methods.
// If valueRequired is true, keep only those where Value is non-empty.
func normalizeCredentials(creds []MsgAccCred, valueRequired bool) []MsgAccCred {
if len(creds) == 0 {
return nil
}
index := make(map[string]*MsgAccCred)
for i := range creds {
c := &creds[i]
if _, ok := globals.validators[c.Method]; ok && (!valueRequired || c.Value != "") {
index[c.Method] = c
}
}
creds = make([]MsgAccCred, 0, len(index))
for _, c := range index {
creds = append(creds, *index[c.Method])
}
return creds
}
// Get a string slice with methods of credentials.
func credentialMethods(creds []MsgAccCred) []string {
var out []string
for i := range creds {
out = append(out, creds[i].Method)
}
return out
}
// Take a slice of tags, return a slice of restricted namespace tags contained in the input.
// Tags to filter, restricted namespaces to filter.
func filterRestrictedTags(tags []string, namespaces map[string]bool) []string {
var out []string
if len(namespaces) > 0 && len(tags) > 0 {
for _, s := range tags {
parts := tagPrefixRegexp.FindStringSubmatch(s)
if len(parts) < 2 {
continue
}
if namespaces[parts[1]] {
out = append(out, s)
}
}
}
return out
}
// Takes MsgClientGet query parameters, returns database query parameters
func msgOpts2storeOpts(req *MsgGetOpts) *types.QueryOpt {
var opts *types.QueryOpt
if req != nil {
opts = &types.QueryOpt{
User: types.ParseUserId(req.User),
Topic: req.Topic,
IfModifiedSince: req.IfModifiedSince,
Limit: req.Limit,
Since: req.SinceId,
Before: req.BeforeId,
}
}
return opts
}
// Check if the interface contains a string with a single Unicode Del control character.
func isNullValue(i interface{}) bool {
const clearValue = "\u2421"
if str, ok := i.(string); ok {
return str == clearValue
}
return false
}
func decodeStoreError(err error, id, topic string, timestamp time.Time,
params map[string]interface{}) *ServerComMessage {
var errmsg *ServerComMessage
if err == nil {
errmsg = NoErr(id, topic, timestamp)
} else if storeErr, ok := err.(types.StoreError); !ok {
errmsg = ErrUnknown(id, topic, timestamp)
} else {
switch storeErr {
case types.ErrInternal:
errmsg = ErrUnknown(id, topic, timestamp)
case types.ErrMalformed:
errmsg = ErrMalformed(id, topic, timestamp)
case types.ErrFailed:
errmsg = ErrAuthFailed(id, topic, timestamp)
case types.ErrPermissionDenied:
errmsg = ErrPermissionDenied(id, topic, timestamp)
case types.ErrDuplicate:
errmsg = ErrDuplicateCredential(id, topic, timestamp)
case types.ErrUnsupported:
errmsg = ErrNotImplemented(id, topic, timestamp)
case types.ErrExpired:
errmsg = ErrAuthFailed(id, topic, timestamp)
case types.ErrPolicy:
errmsg = ErrPolicy(id, topic, timestamp)
case types.ErrCredentials:
errmsg = InfoValidateCredentials(id, timestamp)
case types.ErrNotFound:
errmsg = ErrNotFound(id, topic, timestamp)
default:
errmsg = ErrUnknown(id, topic, timestamp)
}
}
errmsg.Ctrl.Params = params
return errmsg
}
// Helper function to select access mode for the given auth level
func selectAccessMode(authLvl auth.Level, anonMode, authMode, rootMode types.AccessMode) types.AccessMode {
switch authLvl {
case auth.LevelNone:
return types.ModeNone
case auth.LevelAnon:
return anonMode
case auth.LevelAuth:
return authMode
case auth.LevelRoot:
return rootMode
default:
return types.ModeNone
}
}
// Get default modeWant for the given topic category
func getDefaultAccess(cat types.TopicCat, authUser bool) types.AccessMode {
if !authUser {
return types.ModeNone
}
switch cat {
case types.TopicCatP2P:
return types.ModeCP2P
case types.TopicCatFnd:
return types.ModeNone
case types.TopicCatGrp:
return types.ModeCPublic
case types.TopicCatMe:
return types.ModeCSelf
default:
panic("Unknown topic category")
}
}
// Parse topic access parameters
func parseTopicAccess(acs *MsgDefaultAcsMode, defAuth, defAnon types.AccessMode) (authMode, anonMode types.AccessMode,
err error) {
authMode, anonMode = defAuth, defAnon
if acs.Auth != "" {
err = authMode.UnmarshalText([]byte(acs.Auth))
}
if acs.Anon != "" {
err = anonMode.UnmarshalText([]byte(acs.Anon))
}
return
}
// Parses version in the following formats:
// 1.2 | 1.2abc | 1.2.3 | 1.2.3abc
// The major and minor parts must be valid, the trailer is ignored if missing or unparceable.
func parseVersion(vers string) int {
var major, minor, trailer int
var err error
dot := strings.Index(vers, ".")
if dot < 0 {
major, err = strconv.Atoi(vers)
if err != nil || major > 0x1fff || major < 0 {
return 0
}
return major << 16
}
major, err = strconv.Atoi(vers[:dot])
if err != nil {
return 0
}
vers = vers[dot+1:]
dot2 := strings.IndexFunc(vers, func(r rune) bool {
return !unicode.IsDigit(r)
})
if dot2 > 0 {
minor, err = strconv.Atoi(vers[:dot2])
// Ignoring the error here
trailer, _ = strconv.Atoi(vers[dot2+1:])
} else if len(vers) > 0 {
minor, err = strconv.Atoi(vers)
}
if err != nil {
return 0
}
if major < 0 || minor < 0 || trailer < 0 || major > 0x1fff || minor >= 0xff || trailer >= 0xff {
return 0
}
return (major << 16) | (minor << 8) | trailer
}
func versionToString(vers int) string {
str := strconv.Itoa(vers>>16) + "." + strconv.Itoa((vers>>8)&0xff)
if vers&0xff != 0 {
str += "-" + strconv.Itoa(vers&0xff)
}
return str
}
// Parser for search queries. The query may contain non-ASCII
// characters, i.e. length of string in bytes != length of string in runes.
// Returns AND tags (all must be present in every result), OR tags (one or more present), error.
func parseSearchQuery(query string) ([]string, []string, error) {
const (
NONE = iota
QUO
AND
OR
END
ORD
)
type token struct {
op int
val string
}
type context struct {
// Pre-token operand
preOp int
// Post-token operand
postOp int
// Inside quoted string
quo bool
// Current token is a quoted string
unquote bool
// Start of the current token
start int
// End of the current token
end int
}
var ctx = context{preOp: AND}
var out []token
var prev int
query = strings.TrimSpace(query)
// Split query into tokens.
for i, w := 0, 0; prev != END; i += w {
//
var emit bool
// Lexer: get next rune.
var r rune
curr := ORD
r, w = utf8.DecodeRuneInString(query[i:])
if w == 0 {
curr = END
} else if r == '"' {
curr = QUO
} else if !ctx.quo {
if r == ' ' || r == '\t' {
curr = AND
} else if r == ',' {
curr = OR
}
}
if curr == QUO {
if ctx.quo {
// End of the quoted string. Close the quote.
ctx.quo = false
} else {
if prev == ORD {
// Reject strings like a"b
return nil, nil, errors.New("missing operator")
}
// Start of the quoted string. Open the quote.
ctx.quo = true
ctx.unquote = true
}
curr = ORD
}
// Parser: process the current lexem in context.
switch curr {
case OR:
if ctx.postOp == OR {
// More than one comma: ' , ,,'
return nil, nil, errors.New("invalid operator sequence")
}
// Ensure context is not "and", i.e. the case like ' ,' -> ','
ctx.postOp = OR
if prev == ORD {
// Close the current token.
ctx.end = i
}
case AND:
if prev == ORD {
// Close the current token.
ctx.end = i
ctx.postOp = AND
} else if ctx.postOp != OR {
// "and" does not change the "or" context.
ctx.postOp = AND
}
case ORD:
if prev == OR || prev == AND {
// Ordinary character after a comma or a space: ' a' or ',a'.
// Emit without changing the operation.
emit = true
}
case END:
if prev == ORD {
// Close the current token.
ctx.end = i
}
emit = true
}
if emit {
if ctx.quo {
return nil, nil, errors.New("unterminated quoted string")
}
// Emit the new token.
op := ctx.preOp
if ctx.postOp == OR {
op = OR
}
start, end := ctx.start, ctx.end
if ctx.unquote {
start++
end--
}
// Add token if non-empty.
if start < end {
out = append(out, token{val: query[start:end], op: op})
}
ctx.start = i
ctx.preOp = ctx.postOp
ctx.postOp = NONE
ctx.unquote = false
}
prev = curr
}
if len(out) == 0 {
return nil, nil, nil
}
// Convert tokens to two string slices.
var and, or []string
for _, t := range out {
switch t.op {
case AND:
and = append(and, t.val)
case OR:
or = append(or, t.val)
}
}
return and, or, nil
}
// Returns > 0 if v1 > v2; zero if equal; < 0 if v1 < v2
// Only Major and Minor parts are compared, the trailer is ignored.
func versionCompare(v1, v2 int) int {
return (v1 >> 8) - (v2 >> 8)
}
func max(a, b int) int {
if a > b {
return a
}
return b
}
// Truncate string if it's too long. Used in logging.
func truncateStringIfTooLong(s string) string {
if len(s) <= 1024 {
return s
}
return s[:1024] + "..."
}
// Convert relative filepath to absolute.
func toAbsolutePath(base, path string) string {
if filepath.IsAbs(path) {
return path
}
return filepath.Clean(filepath.Join(base, path))
}
// Detect platform from the UserAgent string.
func platformFromUA(ua string) string {
switch {
case strings.Contains(ua, "tinodejs"):
return "web"
case strings.Contains(ua, "tindroid"):
return "android"
}
return ""
}