2018-12-26 17:50:01 +00:00
|
|
|
package xmpp // import "gosrc.io/xmpp"
|
2016-01-06 15:51:12 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"strings"
|
2019-06-05 08:02:24 +00:00
|
|
|
"unicode"
|
2016-01-06 15:51:12 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type Jid struct {
|
|
|
|
username string
|
|
|
|
domain string
|
|
|
|
resource string
|
|
|
|
}
|
|
|
|
|
2019-06-05 08:02:24 +00:00
|
|
|
func NewJid(sjid string) (*Jid, error) {
|
|
|
|
s1 := strings.SplitN(sjid, "@", 2)
|
2016-01-06 15:51:12 +00:00
|
|
|
if len(s1) != 2 {
|
2019-06-05 08:02:24 +00:00
|
|
|
return nil, errors.New("invalid JID, missing domain: " + sjid)
|
2016-01-06 15:51:12 +00:00
|
|
|
}
|
2019-06-05 08:02:24 +00:00
|
|
|
jid := new(Jid)
|
2016-01-06 15:51:12 +00:00
|
|
|
jid.username = s1[0]
|
2019-06-05 08:02:24 +00:00
|
|
|
if !isUsernameValid(jid.username) {
|
|
|
|
return jid, errors.New("invalid domain: " + jid.username)
|
2016-01-06 15:51:12 +00:00
|
|
|
}
|
2019-06-05 08:02:24 +00:00
|
|
|
|
|
|
|
s2 := strings.SplitN(s1[1], "/", 2)
|
|
|
|
|
2016-01-06 15:51:12 +00:00
|
|
|
jid.domain = s2[0]
|
2019-06-05 08:02:24 +00:00
|
|
|
if !isDomainValid(jid.domain) {
|
|
|
|
return jid, errors.New("invalid domain: " + jid.domain)
|
|
|
|
}
|
|
|
|
|
2016-01-06 15:51:12 +00:00
|
|
|
if len(s2) == 2 {
|
|
|
|
jid.resource = s2[1]
|
|
|
|
}
|
|
|
|
|
2019-06-05 08:02:24 +00:00
|
|
|
return jid, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func isUsernameValid(username string) bool {
|
|
|
|
invalidRunes := []rune{'@', '/', '\'', '"', ':', '<', '>'}
|
|
|
|
return strings.IndexFunc(username, isInvalid(invalidRunes)) < 0
|
|
|
|
}
|
|
|
|
|
|
|
|
func isDomainValid(domain string) bool {
|
|
|
|
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
|
2016-01-06 15:51:12 +00:00
|
|
|
}
|