-
Notifications
You must be signed in to change notification settings - Fork 0
/
strings.go
85 lines (70 loc) · 2.12 KB
/
strings.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
// Copyright (c) 2012-2014 Jeremy Latt
// Copyright (c) 2014-2015 Edmund Huber
// Copyright (c) 2016-2017 Daniel Oaks <daniel@danieloaks.net>
// released under the MIT license
package irc
import (
"errors"
"strings"
"golang.org/x/text/secure/precis"
)
const (
casemappingName = "rfc7613"
)
var (
errInvalidCharacter = errors.New("Invalid character")
errEmpty = errors.New("String is empty")
)
// Casefold returns a casefolded string, without doing any name or channel character checks.
func Casefold(str string) (string, error) {
return precis.UsernameCaseMapped.CompareKey(str)
}
// CasefoldChannel returns a casefolded version of a channel name.
func CasefoldChannel(name string) (string, error) {
lowered, err := Casefold(name)
if err != nil {
return "", err
} else if len(lowered) == 0 {
return "", errEmpty
}
if lowered[0] != '#' {
return "", errInvalidCharacter
}
// space can't be used
// , is used as a separator
// * is used in mask matching
// ? is used in mask matching
if strings.Contains(lowered, " ") || strings.Contains(lowered, ",") ||
strings.Contains(lowered, "*") || strings.Contains(lowered, "?") {
return "", errInvalidCharacter
}
return lowered, err
}
// CasefoldName returns a casefolded version of a nick/user name.
func CasefoldName(name string) (string, error) {
lowered, err := Casefold(name)
if err != nil {
return "", err
} else if len(lowered) == 0 {
return "", errEmpty
}
// space can't be used
// , is used as a separator
// * is used in mask matching
// ? is used in mask matching
// . denotes a server name
// ! separates nickname from username
// @ separates username from hostname
// : means trailing
// # is a channel prefix
// ~&@%+ are channel membership prefixes
// - I feel like disallowing
if strings.Contains(lowered, " ") || strings.Contains(lowered, ",") ||
strings.Contains(lowered, "*") || strings.Contains(lowered, "?") ||
strings.Contains(lowered, ".") || strings.Contains(lowered, "!") ||
strings.Contains(lowered, "@") ||
strings.Contains("#~&@%+-", string(lowered[0])) {
return "", errInvalidCharacter
}
return lowered, err
}