forked from FluuxIO/go-xmpp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjid.go
91 lines (76 loc) · 1.86 KB
/
jid.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
package xmpp
import (
"fmt"
"strings"
"unicode"
)
type Jid struct {
Node string
Domain string
Resource string
}
func NewJid(sjid string) (*Jid, error) {
jid := new(Jid)
if sjid == "" {
return jid, fmt.Errorf("jid cannot be empty")
}
s1 := strings.SplitN(sjid, "@", 2)
if len(s1) == 1 { // This is a server or component JID
jid.Domain = s1[0]
} else { // JID has a local username part
if s1[0] == "" {
return jid, fmt.Errorf("invalid jid '%s", sjid)
}
jid.Node = s1[0]
if s1[1] == "" {
return jid, fmt.Errorf("domain cannot be empty")
}
jid.Domain = s1[1]
}
// Extract resource from domain field
s2 := strings.SplitN(jid.Domain, "/", 2)
if len(s2) == 2 { // If len = 1, domain is already correct, and resource is already empty
jid.Domain = s2[0]
jid.Resource = s2[1]
}
if !isUsernameValid(jid.Node) {
return jid, fmt.Errorf("invalid Node in JID '%s'", sjid)
}
if !isDomainValid(jid.Domain) {
return jid, fmt.Errorf("invalid domain in JID '%s'", sjid)
}
return jid, nil
}
func (j *Jid) Full() string {
return j.Node + "@" + j.Domain + "/" + j.Resource
}
func (j *Jid) Bare() string {
return j.Node + "@" + j.Domain
}
// ============================================================================
// Helpers, for parsing / validation
func isUsernameValid(username string) bool {
invalidRunes := []rune{'@', '/', '\'', '"', ':', '<', '>'}
return strings.IndexFunc(username, isInvalid(invalidRunes)) < 0
}
func isDomainValid(domain string) bool {
if len(domain) == 0 {
return false
}
invalidRunes := []rune{'@', '/'}
return strings.IndexFunc(domain, isInvalid(invalidRunes)) < 0
}
func isInvalid(invalidRunes []rune) func(c rune) bool {
isInvalid := func(c rune) bool {
if unicode.IsSpace(c) {
return true
}
for _, r := range invalidRunes {
if c == r {
return true
}
}
return false
}
return isInvalid
}